* changed src/vm/jit/m68k/asmpart.S (asm_patcher_wrapper): Removed.
[cacao.git] / src / vm / jit / m68k / codegen.c
index 824ebe5b0c34b56382f5fe8000809763c3bbaddd..02c79329c2baf28a20bef93f16da9ff015a0fb22 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"
 
 #include "vm/jit/asmpart.h"
 #include "vm/jit/codegen-common.h"
+#include "vm/jit/patcher-common.h"
 #include "vm/jit/dseg.h"
 #include "vm/jit/emit-common.h"
 #include "vm/jit/jit.h"
+#include "vm/jit/abi.h"
 #include "vm/jit/parse.h"
-#include "vm/jit/patcher.h"
 #include "vm/jit/reg.h"
 #include "vm/jit/replace.h"
 #include "vm/jit/stacktrace.h"
@@ -74,11 +75,9 @@ bool codegen_emit(jitdata *jd)
        codegendata        *cd;
        registerdata       *rd;
        s4                  len, s1, s2, s3, d, disp;
-       ptrint              a;
        varinfo            *var;
        basicblock         *bptr;
        instruction        *iptr;
-       exception_entry    *ex;
        u2                  currentline;
        methodinfo         *lm;             /* local methodinfo for ICMD_INVOKE*  */
        unresolved_method  *um;
@@ -109,22 +108,24 @@ 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 3 words, 
-                * 1 word the lock word, which may be unused and resides @ rd->memuse * 4
-                * + 2 words to either save the return value for LOCK_monitor_exit @ rd->memuse * 4 + 4
+               /* 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 words, and this keeps the code simple */
-               cd->stackframesize += 3;        
+                * so we always _need_ at least 2 slots, and this keeps the code simple */
+               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 (checksync && code_is_synchronized(code))    {
                        if (IS_2_WORD_TYPE(m->parseddesc->returntype.type))     {
                                cd->stackframesize += 2;
                        } else  {
@@ -136,14 +137,16 @@ bool codegen_emit(jitdata *jd)
        
                /* create method header */
                (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
-               (void) dseg_add_unique_s4(cd, cd->stackframesize * 4); /* 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, cd->stackframesize);         /* FrameSize       */
+
+               code->synchronizedoffset = rd->memuse * 8;
+
+               /* REMOVEME: We still need it for exception handling in assembler. */
+
+               if (code_is_leafmethod(code))
+                       (void) dseg_add_unique_s4(cd, 1);
                else
-#endif
-               (void) dseg_add_unique_s4(cd, 0);                      /* IsSync          */
-               (void) dseg_add_unique_s4(cd, jd->isleafmethod);       /* IsLeaf          */
+                       (void) dseg_add_unique_s4(cd, 0);
 
                /* XXX we use the IntSave a split field for the adr now */
                (void) dseg_add_unique_s4(cd, (ADR_SAV_CNT - rd->savadrreguse) << 16 | (INT_SAV_CNT - rd->savintreguse)); /* IntSave */
@@ -151,16 +154,6 @@ bool codegen_emit(jitdata *jd)
 
                dseg_addlinenumbertablesize(cd);
 
-               (void) dseg_add_unique_s4(cd, jd->exceptiontablelength); /* ExTableSize   */
-
-               /* create exception table */
-               for (ex = jd->exceptiontable; ex != NULL; ex = ex->down) {
-                       dseg_add_target(cd, ex->start);
-                       dseg_add_target(cd, ex->end);
-                       dseg_add_target(cd, ex->handler);
-                       (void) dseg_add_unique_address(cd, ex->catchtype.any);
-               }
-
 #if defined(ENABLE_PROFILING)
                assert(0);
 #endif
@@ -169,19 +162,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);
@@ -214,22 +207,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 * 4 + s1 + 4);
+                                               M_LLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        } else {
-                                               M_ILD(var->vv.regoff, REG_SP, cd->stackframesize * 4 + s1 + 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 * 4 + s1 + 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 * 4 + s1 + 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 * 4 + s1;
-#endif
                                } 
                                break;
 #if !defined(ENABLE_SOFTFLOAT)
@@ -237,37 +225,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 * 4 + s1 + 4);
+                                               M_DLD(var->vv.regoff, REG_SP, cd->stackframesize + s1 + 4);
                                        } else {
-                                               M_FLD(var->vv.regoff, REG_SP, cd->stackframesize * 4 + s1 + 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 * 4 + s1 + 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 * 4 + s1 + 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 * 4 + 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 * 4 + s1 + 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 * 4 + s1 + 4);
+                                       M_ALD(REG_ATMP1, REG_SP, cd->stackframesize + s1 + 4);
                                        M_AST(REG_ATMP1, REG_SP, var->vv.regoff);
-#else
-                               /* Reuse Memory Position on Caller Stack */
-                               var->vv.regoff = cd->stackframesize * 4 + s1;
-#endif
                                }
                                break;
                        default: assert(0);
@@ -276,18 +254,18 @@ bool codegen_emit(jitdata *jd)
 
 #if defined(ENABLE_THREADS)
        /* call lock_monitor_enter function */
-       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+       if (checksync && code_is_synchronized(code))    {
                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);
        }
@@ -1128,21 +1106,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)
@@ -1182,15 +1166,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);
@@ -1340,7 +1327,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;
 
@@ -1351,7 +1338,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 */
@@ -1368,7 +1355,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 */
@@ -1385,7 +1372,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);
                
@@ -1403,7 +1390,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 */
@@ -1419,7 +1406,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 */
@@ -1433,7 +1420,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 */
@@ -1453,7 +1440,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 */
@@ -1474,7 +1461,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);
        
@@ -1490,7 +1477,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 */
@@ -1504,7 +1491,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 */
@@ -1518,7 +1505,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 */
@@ -1532,7 +1519,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 */
@@ -1546,7 +1533,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 */
@@ -1560,7 +1547,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 */
@@ -1579,7 +1566,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 */
@@ -1601,19 +1588,19 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ATMP2);
 
                        /* XXX what if array is NULL */
-                       disp = dseg_add_functionptr(cd, BUILTIN_canstore);
+                       disp = dseg_add_functionptr(cd, BUILTIN_FAST_canstore);
 
                        M_AST(s1, REG_SP, 0*4);
                        M_AST(s3, REG_SP, 1*4);
-                       M_JSR_IMM(BUILTIN_canstore);    
-                       emit_exception_check(cd, iptr);
+                       M_JSR_IMM(BUILTIN_FAST_canstore);
+                       emit_arraystore_check(cd, iptr);
 
                        s1 = emit_load_s1(jd, iptr, REG_ATMP1);
                        s2 = emit_load_s2(jd, iptr, REG_ITMP1);
                        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 */
@@ -1624,6 +1611,8 @@ bool codegen_emit(jitdata *jd)
 
                /* METHOD INVOCATION *********************************************************/
                case ICMD_BUILTIN:      /* ..., [arg1, [arg2 ...]] ==> ...            */
+                       REPLACEMENT_POINT_FORGC_BUILTIN(cd, iptr);
+
                        bte = iptr->sx.s23.s3.bte;
                        md  = bte->md;
                        goto gen_method;
@@ -1693,13 +1682,15 @@ bool codegen_emit(jitdata *jd)
 
                        /* arguments in place now */
                        switch(iptr->opc)       {
-                               case ICMD_BUILTIN: 
-                                       disp = (ptrint) bte->fp;
+                               case ICMD_BUILTIN:
+                                       if (bte->stub == NULL)
+                                               disp = (ptrint) bte->fp;
+                                       else
+                                               disp = (ptrint) bte->stub;
                                        d = md->returntype.type;
                                        M_JSR_IMM(disp);
 
                                        REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr);
-                                       emit_exception_check(cd, iptr);
                                        break;
 
                                case ICMD_INVOKESPECIAL: 
@@ -1733,7 +1724,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);
@@ -1752,7 +1743,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);
 
@@ -1765,6 +1756,7 @@ bool codegen_emit(jitdata *jd)
                                }       /* switch (iptr->opc) */
 
                                REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr);
+                               REPLACEMENT_POINT_FORGC_BUILTIN_RETURN(cd, iptr);
                                
                                /* store return value */
                                d = md->returntype.type;
@@ -1886,8 +1878,8 @@ nowperformreturn:
 
 #if defined(ENABLE_THREADS)
                        /* call lock_monitor_exit */
-                       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
-                               M_ILD(REG_ITMP3, REG_SP, rd->memuse * 4);
+                       if (checksync && code_is_synchronized(code)) {
+                               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 */
@@ -1896,21 +1888,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
                                }
@@ -1925,21 +1917,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
                                }
@@ -1949,7 +1941,7 @@ nowperformreturn:
 
                        /* restore return address                                         */
 #if 0
-                       if (!jd->isleafmethod) {
+                       if (!code_is_leafmethod(code)) {
                                /* ATTENTION: Don't use REG_ZERO (r0) here, as M_ALD
                                   may have a displacement overflow. */
 
@@ -1960,18 +1952,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;
@@ -2040,7 +2032,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);
@@ -2070,7 +2062,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;
 
@@ -2160,7 +2152,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 */
@@ -2191,7 +2183,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;
 
@@ -2380,8 +2372,7 @@ nowperformreturn:
        dseg_createlinenumbertable(cd);
 
        /* generate stubs */
-       emit_patcher_stubs(jd);
-       REPLACEMENT_EMIT_STUBS(jd);
+       emit_patcher_traps(jd);
 
        return true;
 }
@@ -2409,22 +2400,20 @@ void codegen_emit_stub_compiler(jitdata *jd)
        M_AMOV_IMM(asm_call_jit_compiler, REG_ATMP3);
        M_JMP(REG_ATMP3);
 }
-
-
 /* codegen_emit_stub_native ****************************************************
 
    Emits a stub routine which calls a native method.
 
 *******************************************************************************/
 
-void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
+void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int skipparams)
 {
        methodinfo   *m;
        codeinfo     *code;
        codegendata  *cd;
        registerdata *rd;
        methoddesc   *md;
-       s4 nativeparams, i, j, t, s1, s2;
+       s4 i, j, t, s1, s2;
        
        /* get required compiler data */
 
@@ -2434,14 +2423,14 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        rd   = jd->rd;
 
        md = m->parseddesc;
-       nativeparams = (m->flags & ACC_STATIC) ? 2 : 1;
 
        /* calc stackframe size */
-       cd->stackframesize =    sizeof(stackframeinfo) / SIZEOF_VOID_P +
-                               sizeof(localref_table) / SIZEOF_VOID_P +
-                               nmd->memuse +
-                               1 +                                             /* functionptr */
-                               4;                                              /* args for codegen_start_native_call */
+       cd->stackframesize =
+               sizeof(stackframeinfo_t) / SIZEOF_VOID_P +
+               sizeof(localref_table) / SIZEOF_VOID_P +
+               nmd->memuse +
+               1 +                                             /* functionptr */
+               4;                                              /* args for codegen_start_native_call */
 
        /* create method header */
        (void) dseg_add_unique_address(cd, code);                      /* CodeinfoPointer */
@@ -2464,11 +2453,10 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        M_AADD_IMM(-(cd->stackframesize*4), REG_SP);
 
        /* get function address (this must happen before the stackframeinfo) */
-#if !defined(WITH_STATIC_CLASSPATH)
        if (f == NULL)  {
                codegen_addpatchref(cd, PATCHER_resolve_native_function, m, 0);
        }
-#endif
+
        M_AMOV_IMM(f, REG_ATMP2); /* do not move this line, the patcher is needed */
 
        M_AST(REG_ATMP2, REG_SP, 4 * 4);
@@ -2477,15 +2465,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        /* void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra) */
        
        M_AMOV(REG_SP, REG_ATMP1);
-       M_AADD_IMM(cd->stackframesize * 4, REG_ATMP1);
-
-       M_ALD(REG_ATMP3, REG_ATMP1, 0 * 4);
-       M_AST(REG_ATMP3, REG_SP, 3 * 4);                /* ra */
-
-       M_AST(REG_ATMP1, REG_SP, 0 * 4);                /* datasp */
-
-       M_AADD_IMM(1 * 4 , REG_ATMP1);                  
-       M_AST(REG_ATMP1, REG_SP, 2 * 4);                /* sp */
+       M_AST(REG_ATMP1, REG_SP, 0 * 4);                /* currentsp */
 
        M_AMOV_IMM(0, REG_ATMP2);                       /* 0 needs to patched */
        dseg_adddata(cd);                                   /* this patches it */
@@ -2494,11 +2474,15 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        M_JSR_IMM(codegen_start_native_call);
 
+       /* remember class argument */
+       if (m->flags & ACC_STATIC)
+               M_INT2ADRMOVE(REG_RESULT, REG_ATMP3);
+
        /* load function pointer */
        M_ALD(REG_ATMP2, REG_SP, 4 * 4);
 
        /* copy arguments into stackframe */
-       for (i = md->paramcount -1, j = i + nativeparams; i >= 0; --i, --j)     {
+       for (i = md->paramcount -1, j = i + skipparams; i >= 0; --i, --j)       {
                t = md->paramtypes[i].type;
                /* all arguments via stack */
                assert(md->params[i].inmemory);                                         
@@ -2516,10 +2500,9 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        }
 
        /* for static function class as second arg */
-       if (m->flags & ACC_STATIC)      {
-               M_AMOV_IMM(m->class, REG_ATMP1);
-               M_AST(REG_ATMP1, REG_SP, 1 * 4);
-       }
+       if (m->flags & ACC_STATIC)
+               M_AST(REG_ATMP3, REG_SP, 1 * 4);
+
        /* env ist first argument */
        M_AMOV_IMM(_Jv_env, REG_ATMP1);
        M_AST(REG_ATMP1, REG_SP, 0 * 4);
@@ -2540,7 +2523,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                case TYPE_FLT:
                case TYPE_INT:
                case TYPE_ADR:
-                       M_IST(REG_D0, REG_SP, 1 * 4);
+                       M_IST(REG_D0, REG_SP, 2 * 4);
                        break;
 
                default: assert(0);
@@ -2555,9 +2538,14 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        /* remove native stackframe info */
        /* therefore we call: java_objectheader *codegen_finish_native_call(u1 *datasp) */
 
-       M_AMOV(REG_SP, REG_ATMP3);
-       M_AADD_IMM(cd->stackframesize * 4, REG_ATMP3);
-       M_AST(REG_ATMP3, REG_SP, 0 * 4);                        /* datasp */
+       M_AMOV(REG_SP, REG_ATMP1);
+       M_AST(REG_ATMP1, REG_SP, 0 * 4);                /* currentsp */
+
+       M_AMOV_IMM(0, REG_ATMP2);                       /* 0 needs to patched */
+       dseg_adddata(cd);                                   /* this patches it */
+
+       M_AST(REG_ATMP2, REG_SP, 1 * 4);                /* pv */
+
        M_JSR_IMM(codegen_finish_native_call);
        
        M_INT2ADRMOVE(REG_RESULT, REG_ATMP1);
@@ -2572,7 +2560,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                case TYPE_FLT:
                case TYPE_INT:
                case TYPE_ADR:
-                       M_ILD(REG_D0, REG_SP, 1 * 4);
+                       M_ILD(REG_D0, REG_SP, 2 * 4);
                        break;
 
                default: assert(0);
@@ -2582,10 +2570,10 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                 * as cacao jit code expects them there */
        switch (md->returntype.type)    {
                case TYPE_FLT:
-                       M_FLD(REG_D0, REG_SP, 1 * 4);
+                       M_FLD(REG_D0, REG_SP, 2 * 4);
                        break;
                case TYPE_DBL:  
-                       M_DLD(REG_D0, REG_SP, 1 * 4);
+                       M_DLD(REG_D0, REG_SP, 2 * 4);
                        break;
        }
 #endif
@@ -2607,7 +2595,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        M_JSR_IMM(0);
 
        /* generate patcher stub call code */
-       emit_patcher_stubs(jd);
+       emit_patcher_traps(jd);
 }