* Removed all Id tags.
[cacao.git] / src / vm / jit / m68k / codegen.c
index 812ed9b31489b1c3cf1d020928408e0d5fc92af4..ad266464d27fa167e4584f396879386b06ea4365 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: codegen.c 7564 2007-03-23 23:36:17Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 
 #include "md-abi.h"
 #include "md-os.h"
@@ -40,6 +39,7 @@
 
 #include "mm/memory.h"
 #include "native/jni.h"
+#include "native/localref.h"
 #include "native/native.h"
 
 #include "threads/lock-common.h"
@@ -109,35 +109,39 @@ bool codegen_emit(jitdata *jd)
 
                savedregs_num += (INT_SAV_CNT - rd->savintreguse);
                savedregs_num += (ADR_SAV_CNT - rd->savadrreguse);
-               savedregs_num += (FLT_SAV_CNT - rd->savfltreguse) * 2;
+               savedregs_num += (FLT_SAV_CNT - rd->savfltreguse);
 
                cd->stackframesize = rd->memuse + savedregs_num;
+       
+               /* we always add 2 stack slots.
+                * 1 word the lock word, which may be unused and resides @ rd->memuse * 8
+                * + 2 words to either save the return value for LOCK_monitor_exit @ rd->memuse * 8 + 8
+                * on the other hand we could use 2 words when a builtin returns a doulbe which are
+                * returned in %d0, %d1 and need to be stored onto the stack and read in used a fmovemd
+                * so we always _need_ at least 2 slots, and this keeps the code simple */
+               cd->stackframesize += 2;        
 
-               /* FIXME: we could need 2 words to move a double result, which gets
-                * passed in %d0, %d1 into a floating point register, this is of 
-                * course onyl true when not using ENABLE_SOFTFLOAT, so this could be
-                * optimized away, for now always use 2 more words. When optimizing watch
-                * the threading code, which stores the lock word, the offset would change */
-               cd->stackframesize += 2;
+               cd->stackframesize *= 8;        /* we use 8 byte stack slots */
 
+#if 0
 #if defined(ENABLE_THREADS)
                /* we need additional space to save argument of monitor_enter */
                if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                        if (IS_2_WORD_TYPE(m->parseddesc->returntype.type))     {
                                cd->stackframesize += 2;
                        } else  {
-                               cd->stackframesize ++;
+                               cd->stackframesize += 1;
                        }
                }
 #endif
-               
+#endif 
        
                /* create method header */
                (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
-               (void) dseg_add_unique_s4(cd, cd->stackframesize * 4); /* FrameSize       */
+               (void) dseg_add_unique_s4(cd, cd->stackframesize);         /* FrameSize       */
 #if defined(ENABLE_THREADS)
                if (checksync && (m->flags & ACC_SYNCHRONIZED))
-                       (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 4);/* IsSync         */
+                       (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 8);/* IsSync         */
                else
 #endif
                (void) dseg_add_unique_s4(cd, 0);                      /* IsSync          */
@@ -167,19 +171,19 @@ bool codegen_emit(jitdata *jd)
                emit_verbosecall_enter(jd);
 #endif
                /* create stack frame */
-               M_AADD_IMM(-(cd->stackframesize*4), REG_SP);
+               M_AADD_IMM(-(cd->stackframesize), REG_SP);
 
                /* save used callee saved registers */
                p = cd->stackframesize;
                for (i=INT_SAV_CNT-1; i>=rd->savintreguse; --i) {
-                       p--; M_IST(rd->savintregs[i], REG_SP, p*4);
+                       p-=8; M_IST(rd->savintregs[i], REG_SP, p);
                }
                for (i=ADR_SAV_CNT-1; i>=rd->savadrreguse; --i) {
-                       p--; M_AST(rd->savadrregs[i], REG_SP, p*4);
+                       p-=8; M_AST(rd->savadrregs[i], REG_SP, p);
                }
 #if !defined(ENABLE_SOFTFLOAT)
                for (i=FLT_SAV_CNT-1; i>=rd->savfltreguse; --i) {
-                       p-=2; M_FSTORE(rd->savfltregs[i], REG_SP, p*4);
+                       p-=8; M_FSTORE(rd->savfltregs[i], REG_SP, p);
                }       
 #else
                assert(FLT_SAV_CNT == 0);
@@ -212,22 +216,17 @@ bool codegen_emit(jitdata *jd)
                        case TYPE_INT:
                                if (!IS_INMEMORY(var->flags)) {      /* stack arg -> register */
                                        if (IS_2_WORD_TYPE(t))  {
-                                               M_LLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_LLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        } else {
-                                               M_ILD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_ILD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        }
                                } else {                             /* stack arg -> spilled  */
-#if 1
-                                       M_ILD(REG_ITMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                       M_IST(REG_ITMP1, REG_SP, var->vv.regoff * 4);
+                                       M_ILD(REG_ITMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                       M_IST(REG_ITMP1, REG_SP, var->vv.regoff);
                                        if (IS_2_WORD_TYPE(t)) {
-                                               M_ILD(REG_ITMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4 + 4);
-                                               M_IST(REG_ITMP1, REG_SP, var->vv.regoff * 4 + 4);
+                                               M_ILD(REG_ITMP1, REG_SP, cd->stackframesize  + s1 + 4 + 4);
+                                               M_IST(REG_ITMP1, REG_SP, var->vv.regoff + 4);
                                        }
-#else
-                                       /* Reuse Memory Position on Caller Stack */
-                                       var->vv.regoff = cd->stackframesize + s1;
-#endif
                                } 
                                break;
 #if !defined(ENABLE_SOFTFLOAT)
@@ -235,37 +234,27 @@ bool codegen_emit(jitdata *jd)
                        case TYPE_DBL:
                                if (!IS_INMEMORY(var->flags)) {      /* stack-arg -> register */
                                        if (IS_2_WORD_TYPE(t))  {
-                                               M_DLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_DLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        } else {
-                                               M_FLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                               M_FLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        }
                                } else {                             /* stack-arg -> spilled  */
-#if 1
                                        if (IS_2_WORD_TYPE(t)) {
-                                               M_DLD(REG_FTMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                               M_DST(REG_FTMP1, REG_SP, var->vv.regoff * 4);
+                                               M_DLD(REG_FTMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                               M_DST(REG_FTMP1, REG_SP, var->vv.regoff);
                                        } else {
-                                               M_FLD(REG_FTMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                               M_FST(REG_FTMP1, REG_SP, var->vv.regoff * 4);
+                                               M_FLD(REG_FTMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                               M_FST(REG_FTMP1, REG_SP, var->vv.regoff);
                                        }
-#else
-                                       /* Reuse Memory Position on Caller Stack */
-                                       var->vv.regoff = cd->stackframesize + s1;
-#endif
                                }
                                break;
 #endif /* SOFTFLOAT */
                        case TYPE_ADR:
                                if (!IS_INMEMORY(var->flags)) {      /* stack-arg -> register */
-                                       M_ALD(var->vv.regoff, REG_SP, (cd->stackframesize + s1 + 1) * 4);
+                                       M_ALD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                } else {                             /* stack-arg -> spilled  */
-#if 1
-                                       M_ALD(REG_ATMP1, REG_SP, (cd->stackframesize + s1 + 1) * 4);
-                                       M_AST(REG_ATMP1, REG_SP, var->vv.regoff * 4);
-#else
-                               /* Reuse Memory Position on Caller Stack */
-                               var->vv.regoff = cd->stackframesize + s1;
-#endif
+                                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize + s1 + 4);
+                                       M_AST(REG_ATMP1, REG_SP, var->vv.regoff);
                                }
                                break;
                        default: assert(0);
@@ -273,19 +262,19 @@ bool codegen_emit(jitdata *jd)
                } /* end for argument out of stack*/
 
 #if defined(ENABLE_THREADS)
-       /* call monitor_enter function */
+       /* call lock_monitor_enter function */
        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                if (m->flags & ACC_STATIC)      {
                        M_AMOV_IMM((&m->class->object.header), REG_ATMP1);
                } else  {
                        /* for non-static case the first arg is the object */
-                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize*4 + 4);
+                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize + 4);
                        M_ATST(REG_ATMP1);
                        M_BNE(2);
                        M_TRAP(M68K_EXCEPTION_HARDWARE_NULLPOINTER);
                }
 
-               M_AST(REG_ATMP1, REG_SP, rd->memuse * 4);
+               M_AST(REG_ATMP1, REG_SP, rd->memuse * 8);
                M_AST(REG_ATMP1, REG_SP, 0 * 4);
                M_JSR_IMM(LOCK_monitor_enter);
        }
@@ -306,8 +295,35 @@ bool codegen_emit(jitdata *jd)
        /* branch resolving */
        codegen_resolve_branchrefs(cd, bptr);
 
+       /* handle replacement points */
+       REPLACEMENT_POINT_BLOCK_START(cd, bptr);
+
+#if defined(ENABLE_PROFILING)
+       assert(0);
+#endif
        /* FIXME there are still some constrcuts to copy in here */
 
+#if defined(ENABLE_LSRA)
+       assert(0);
+#endif
+
+       /* copy interface registers to their destination */
+       len = bptr->indepth;
+       MCODECHECK(64+len);
+
+       while (len > 0) {
+               len--;
+               var = VAR(bptr->invars[len]);
+               if ((len == bptr->indepth-1) && (bptr->type == BBTYPE_EXH)) {
+                       d = codegen_reg_of_var(0, var, REG_ATMP1_XPTR);
+                       M_ADRMOVE(REG_ATMP1_XPTR, d);
+                       emit_store(jd, NULL, var, d);
+               }
+               else {
+                       assert((var->flags & INOUT));
+               }
+       }
+
        /* walk through all instructions */
        len = bptr->icount;
        currentline = 0;
@@ -396,6 +412,76 @@ bool codegen_emit(jitdata *jd)
                        break;
 
 
+               /* some long operations *********************************************/
+               case ICMD_LADD:       /* ..., val1, val2  ==> ..., val1 + val2        */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP3);
+                       s2 = emit_load_s2_low(jd, iptr, REG_ITMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       M_INTMOVE(s2, REG_ITMP1);
+                       M_IADD(s1, REG_ITMP1);                  /* low word */
+                       s1 = emit_load_s1_high(jd, iptr, REG_ITMP3);
+                       s2 = emit_load_s2_high(jd, iptr, REG_ITMP2);
+                       M_INTMOVE(s2, REG_ITMP2);
+                       M_IADDX(s1, REG_ITMP2);                 /* high word */
+                       emit_store_dst(jd, iptr, d);
+                       break;
+                       
+               case ICMD_LADDCONST:  /* ..., value  ==> ..., value + constant        */
+                                     /* sx.val.l = constant                          */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s1_high(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       
+                       M_IMOV_IMM(iptr->sx.val.l >> 32, REG_ITMP3);
+
+                       s3 = iptr->sx.val.l & 0xffffffff;
+                       M_INTMOVE(s1, REG_ITMP1);
+                       M_IADD_IMM(s3, REG_ITMP1);              /* lower word in REG_ITMP1 now */
+
+                       M_IADDX(REG_ITMP3, REG_ITMP2);  /* high word in REG_ITMP2 now */
+                       M_LNGMOVE(REG_ITMP12_PACKED, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_LSUB:       /* ..., val1, val2  ==> ..., val1 - val2        */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2_low(jd, iptr, REG_ITMP3);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       M_INTMOVE(s1, REG_ITMP1);
+                       M_ISUB(s2, REG_ITMP1);                  /* low word */
+                       s1 = emit_load_s1_high(jd, iptr, REG_ITMP2);
+                       s2 = emit_load_s2_high(jd, iptr, REG_ITMP3);
+                       M_INTMOVE(s1, REG_ITMP2);
+                       M_ISUBX(s2, REG_ITMP2);                 /* high word */
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_LSUBCONST:  /* ..., value  ==> ..., value - constant        */
+                                     /* sx.val.l = constant                          */
+                       s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s1_high(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       
+                       M_IMOV_IMM( (-iptr->sx.val.l) >> 32, REG_ITMP3);
+
+                       s3 = (-iptr->sx.val.l) & 0xffffffff;
+                       M_INTMOVE(s1, REG_ITMP1);
+                       M_IADD_IMM(s3, REG_ITMP1);              /* lower word in REG_ITMP1 now */
+
+                       M_IADDX(REG_ITMP3, REG_ITMP2);  /* high word in REG_ITMP2 now */
+                       M_LNGMOVE(REG_ITMP12_PACKED, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+               case ICMD_LNEG:       /* ..., value  ==> ..., - value                 */
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP12_PACKED);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                       M_LNGMOVE(s1, REG_ITMP12_PACKED);
+                       M_INEG(GET_LOW_REG(REG_ITMP12_PACKED));
+                       M_INEGX(GET_HIGH_REG(REG_ITMP12_PACKED));
+                       M_LNGMOVE(REG_ITMP12_PACKED, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
 
                /* integer operations ************************************************/
                case ICMD_INEG:       /* ..., value  ==> ..., - value                 */
@@ -408,16 +494,6 @@ bool codegen_emit(jitdata *jd)
                        emit_store_dst(jd, iptr, d);
                        break;
 
-#if 0
-               case ICMD_LNEG:       /* ..., value  ==> ..., - value                 */
-
-                       s1 = emit_load_s1(jd, iptr, REG_ITMP12_PACKED);
-                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
-                       M_SUBFIC(GET_LOW_REG(s1), 0, GET_LOW_REG(d));
-                       M_SUBFZE(GET_HIGH_REG(s1), GET_HIGH_REG(d));
-                       emit_store_dst(jd, iptr, d);
-                       break;
-#endif
                case ICMD_I2L:        /* ..., value  ==> ..., value                   */
 
                        s1 = emit_load_s1(jd, iptr, REG_ITMP3);
@@ -1039,21 +1115,27 @@ bool codegen_emit(jitdata *jd)
 
 
                /* MEMORY *************************************************************/
-               case ICMD_GETSTATIC:
-                       if (INSTRUCTION_IS_UNRESOLVED(iptr))    {
+
+               case ICMD_GETSTATIC:  /* ...  ==> ..., value                          */
+
+                       if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
-                               codegen_addpatchref(cd, PATCHER_get_putstatic, uf, 0);
-                       } else  {
-                               fieldinfo *fi = iptr->sx.s23.s3.fmiref->p.field;
+                               disp      = 0;
 
+                               codegen_addpatchref(cd, PATCHER_get_putstatic, uf, 0);
+                       }
+                       else {
+                               fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
+                               disp      = (intptr_t) fi->value;
+
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
-                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class, 0);
+                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class,
+                                                                               0);
                                }
-
-                               disp = (ptrint) &(fi->value);
                        }
+
                        M_AMOV_IMM(disp, REG_ATMP1);
                        switch (fieldtype) {
 #if defined(ENABLE_SOFTFLOAT)
@@ -1093,15 +1175,18 @@ bool codegen_emit(jitdata *jd)
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
+                               disp      = 0;
 
                                codegen_addpatchref(cd, PATCHER_get_putstatic, uf, 0);
-                       } else {
+                       }
+                       else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp      = &(fi->value);
+                               disp      = (intptr_t) fi->value;
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class, 0);
+                                       codegen_addpatchref(cd, PATCHER_initialize_class, fi->class,
+                                                                               0);
                        }
                
                        M_AMOV_IMM(disp, REG_ATMP1);
@@ -1251,7 +1336,7 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ATMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        /* implicit null-pointer check */
-                       M_ILD(d, s1, OFFSET(java_arrayheader, size));
+                       M_ILD(d, s1, OFFSET(java_array_t, size));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1262,7 +1347,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_bytearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_bytearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1279,7 +1364,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_chararray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_chararray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1296,7 +1381,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_shortarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_shortarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                
@@ -1314,7 +1399,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_intarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_intarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1330,7 +1415,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP1);
                        M_ISSL_IMM(3, REG_ITMP1);
-                       M_IADD_IMM(OFFSET(java_longarray, data[0]), REG_ITMP1);
+                       M_IADD_IMM(OFFSET(java_longarray_t, data[0]), REG_ITMP1);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP1, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1344,7 +1429,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_floatarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_floatarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1364,7 +1449,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(3, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_doublearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_doublearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1385,7 +1470,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_objectarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_objectarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
        
@@ -1401,7 +1486,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_bytearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_bytearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1415,7 +1500,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_chararray, data[0]), REG_ITMP2); 
+                       M_IADD_IMM(OFFSET(java_chararray_t, data[0]), REG_ITMP2); 
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1429,7 +1514,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(1, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_shortarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_shortarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1443,7 +1528,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_intarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_intarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1457,7 +1542,7 @@ bool codegen_emit(jitdata *jd)
 
                        M_INTMOVE(s2, REG_ITMP1);
                        M_ISSL_IMM(3, REG_ITMP1);
-                       M_IADD_IMM(OFFSET(java_longarray, data[0]), REG_ITMP1);
+                       M_IADD_IMM(OFFSET(java_longarray_t, data[0]), REG_ITMP1);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP1, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1471,7 +1556,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(2, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_floatarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_floatarray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1490,7 +1575,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_INTMOVE(s2, REG_ITMP2);
                        M_ISSL_IMM(3, REG_ITMP2);
-                       M_IADD_IMM(OFFSET(java_doublearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(OFFSET(java_doublearray_t, data[0]), REG_ITMP2);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP2, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1524,7 +1609,7 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ATMP2);
                        M_INTMOVE(s2, REG_ITMP1);
                        M_ISSL_IMM(2, REG_ITMP1);
-                       M_IADD_IMM(OFFSET(java_objectarray, data[0]), REG_ITMP1);
+                       M_IADD_IMM(OFFSET(java_objectarray_t, data[0]), REG_ITMP1);
                        M_ADRMOVE(s1, REG_ATMP1);
                        M_AADDINT(REG_ITMP1, REG_ATMP1);
                        /* implicit null-pointer check */
@@ -1574,27 +1659,27 @@ bool codegen_emit(jitdata *jd)
 #endif
                                        case TYPE_LNG:
                                                d = emit_load(jd, iptr, var, REG_ITMP12_PACKED);
-                                               M_LST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_LST(d, REG_SP, md->params[s3].regoff);
                                                break;
 #if defined(ENABLE_SOFTFLOAT)
                                        case TYPE_FLT:
 #endif
                                        case TYPE_INT:
                                                d = emit_load(jd, iptr, var, REG_ITMP1);
-                                               M_IST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_IST(d, REG_SP, md->params[s3].regoff);
                                                break;
                                        case TYPE_ADR:
                                                d = emit_load(jd, iptr, var, REG_ATMP1);
-                                               M_AST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_AST(d, REG_SP, md->params[s3].regoff);
                                                break;
 #if !defined(ENABLE_SOFTFLOAT)
                                        case TYPE_FLT:
                                                d = emit_load(jd, iptr, var, REG_FTMP1);
-                                               M_FST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_FST(d, REG_SP, md->params[s3].regoff);
                                                break;
                                        case TYPE_DBL:
                                                d = emit_load(jd, iptr, var, REG_FTMP1);
-                                               M_DST(d, REG_SP, md->params[s3].regoff*4);
+                                               M_DST(d, REG_SP, md->params[s3].regoff);
                                                break;
 #endif
                                        default:
@@ -1644,7 +1729,7 @@ bool codegen_emit(jitdata *jd)
                                        /* load object pointer (==argument 0) */
                                        M_ALD(REG_ATMP1, REG_SP, 0);
                                        /* implicit null-pointer check */
-                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_object_t, vftbl));
                                        M_ALD(REG_ATMP3, REG_METHODPTR, s1);
                                        /* generate the actual call */
                                        M_JSR(REG_ATMP3);
@@ -1663,7 +1748,7 @@ bool codegen_emit(jitdata *jd)
                                        M_ALD(REG_ATMP1, REG_SP, 0);
 
                                        /* implicit null-pointer check */
-                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_METHODPTR, REG_ATMP1, OFFSET(java_object_t, vftbl));
                                        M_ALD(REG_METHODPTR, REG_METHODPTR, s1);
                                        M_ALD(REG_ATMP3, REG_METHODPTR, s2);
 
@@ -1798,7 +1883,7 @@ nowperformreturn:
 #if defined(ENABLE_THREADS)
                        /* call lock_monitor_exit */
                        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
-                               M_ILD(REG_ITMP3, REG_SP, rd->memuse * 4);
+                               M_ILD(REG_ITMP3, REG_SP, rd->memuse * 8);
 
                                /* we need to save the proper return value */
                                /* we do not care for the long -> doubel convert space here */
@@ -1807,21 +1892,21 @@ nowperformreturn:
                                case ICMD_DRETURN:
 #endif
                                case ICMD_LRETURN:
-                                       M_LST(REG_RESULT_PACKED, REG_SP, rd->memuse * 4 + 4);
+                                       M_LST(REG_RESULT_PACKED, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
 #endif
                                case ICMD_IRETURN:
                                case ICMD_ARETURN:
-                                       M_IST(REG_RESULT , REG_SP, rd->memuse * 4 + 4);
+                                       M_IST(REG_RESULT , REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if !defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
-                                       M_FST(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_FST(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
                                case ICMD_DRETURN:
-                                       M_DST(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_DST(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #endif
                                }
@@ -1836,21 +1921,21 @@ nowperformreturn:
                                case ICMD_DRETURN:
 #endif
                                case ICMD_LRETURN:
-                                       M_LLD(REG_RESULT_PACKED, REG_SP, rd->memuse * 4 + 4);
+                                       M_LLD(REG_RESULT_PACKED, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
 #endif
                                case ICMD_IRETURN:
                                case ICMD_ARETURN:
-                                       M_ILD(REG_RESULT , REG_SP, rd->memuse * 4 + 4);
+                                       M_ILD(REG_RESULT , REG_SP, rd->memuse * 8 + 8);
                                        break;
 #if !defined(ENABLE_SOFTFLOAT)
                                case ICMD_FRETURN:
-                                       M_FLD(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_FLD(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
                                case ICMD_DRETURN:
-                                       M_DLD(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_DLD(REG_FRESULT, REG_SP, rd->memuse * 8 + 8);
                                        break;
 #endif
                                }
@@ -1871,18 +1956,18 @@ nowperformreturn:
                        /* restore saved registers                                        */
 
                        for (i = INT_SAV_CNT - 1; i >= rd->savintreguse; i--) {
-                               p--; M_ILD(rd->savintregs[i], REG_SP, p * 4);
+                               p-=8; M_ILD(rd->savintregs[i], REG_SP, p);
                        }
                        for (i=ADR_SAV_CNT-1; i>=rd->savadrreguse; --i) {
-                               p--; M_ALD(rd->savadrregs[i], REG_SP, p*4);
+                               p-=8; M_ALD(rd->savadrregs[i], REG_SP, p);
                        }
 #if !defined(ENABLE_SOFTFLOAT)
                        for (i = FLT_SAV_CNT - 1; i >= rd->savfltreguse; i--) {
-                               p -= 2; M_FLOAD(rd->savfltregs[i], REG_SP, p * 4);
+                               p-=8; M_FLOAD(rd->savfltregs[i], REG_SP, p);
                        }
 #endif
                        /* deallocate stack                                               */
-                       M_AADD_IMM(cd->stackframesize*4, REG_SP);
+                       M_AADD_IMM(cd->stackframesize, REG_SP);
                        M_RET;
                        }
                        break;
@@ -1951,7 +2036,7 @@ nowperformreturn:
                                        emit_label_beq(cd, BRANCH_LABEL_3);
                                }
 
-                               M_ALD(REG_ATMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ATMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ILD(REG_ITMP3, REG_ATMP1, OFFSET(vftbl_t, interfacetablelength));
                                M_IADD_IMM(-superindex, REG_ITMP3);     /* -superindex may be patched patched */
                                M_ITST(REG_ITMP3);
@@ -1981,7 +2066,7 @@ nowperformreturn:
                                        emit_label_beq(cd, BRANCH_LABEL_5);
                                }
 
-                               M_ALD(REG_ATMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ATMP1, s1, OFFSET(java_object_t, vftbl));
 
                                CODEGEN_CRITICAL_SECTION_START;
 
@@ -2071,7 +2156,7 @@ nowperformreturn:
                                                emit_label_beq(cd, BRANCH_LABEL_3);
                                        }
 
-                                       M_ALD(REG_ATMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ATMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ILD(REG_ITMP3, REG_ATMP2, OFFSET(vftbl_t, interfacetablelength));
        
                                        M_IADD_IMM(-superindex, REG_ITMP3);     /* superindex patched */
@@ -2102,7 +2187,7 @@ nowperformreturn:
                                                emit_label_beq(cd, BRANCH_LABEL_5);
                                        }
 
-                                       M_ALD(REG_ATMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ATMP2, s1, OFFSET(java_object_t, vftbl));
 
                                        CODEGEN_CRITICAL_SECTION_START;
 
@@ -2270,8 +2355,21 @@ nowperformreturn:
                        exceptions_throw_internalerror("Unknown ICMD %d during code generation", iptr->opc);
                        return false;
        } /* switch */
-       M_TPF;
+       /* M_TPF; */ /* nop after each ICMD */
        } /* for each instruction */
+
+       /* At the end of a basic block we may have to append some nops,
+          because the patcher stub calling code might be longer than the
+          actual instruction. So codepatching does not change the
+          following block unintentionally. */
+
+       if (cd->mcodeptr < cd->lastmcodeptr) {
+               while (cd->mcodeptr < cd->lastmcodeptr) {
+                       M_NOP;
+               }
+       }
+
+
        } /* if (btpre->flags >= BBREACHED) */
        } /* for each basic block */
 
@@ -2279,7 +2377,6 @@ nowperformreturn:
 
        /* generate stubs */
        emit_patcher_stubs(jd);
-       REPLACEMENT_EMIT_STUBS(jd);
 
        return true;
 }
@@ -2401,8 +2498,8 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                /* all arguments via stack */
                assert(md->params[i].inmemory);                                         
 
-               s1 = (md->params[i].regoff + cd->stackframesize + 1) * 4;
-               s2 = nmd->params[j].regoff * 4;
+               s1 = md->params[i].regoff + cd->stackframesize * 4 + 4;
+               s2 = nmd->params[j].regoff;
 
                /* simply copy argument stack */
                M_ILD(REG_ITMP1, REG_SP, s1);