* Removed all Id tags.
[cacao.git] / src / vm / jit / powerpc64 / codegen.c
index 954f72d0113187f0c24b041c90e6883fb0f4f62f..104754408550fc77feaf2bd46ebb4766c35c48bc 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: codegen.c 7582 2007-03-26 09:27:10Z tbfg $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <signal.h>
 
 
 #include "mm/memory.h"
 
+#include "native/localref.h"
 #include "native/native.h"
 
+#include "threads/lock-common.h"
+
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/global.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
 
+#include "vm/jit/abi.h"
 #include "vm/jit/abi-asm.h"
 #include "vm/jit/md.h"
 #include "vm/jit/asmpart.h"
 # include "vm/jit/allocator/lsra.h"
 #endif
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/lock.h"
-#endif
-
 
 /* codegen_emit ****************************************************************
 
@@ -112,9 +111,11 @@ bool codegen_emit(jitdata *jd)
 
        /* prevent compiler warnings */
 
-       d = 0;
-       lm = NULL;
+       d   = 0;
+       lm  = NULL;
+       um  = NULL;
        bte = NULL;
+       uf  = NULL;
 
        {
        s4 i, p, t, l;
@@ -217,47 +218,43 @@ bool codegen_emit(jitdata *jd)
                        l++;
                if (varindex == UNUSED)
                        continue;
+
                var = VAR(varindex);
-               s1 = md->params[p].regoff;
-               if (IS_INT_LNG_TYPE(t)) {                    /* integer args          */
-                       if (!md->params[p].inmemory) {           /* register arguments    */
-                               s2 = rd->argintregs[s1];
-                               if (!IS_INMEMORY(var->flags))   {
-                                       M_INTMOVE(s2, var->vv.regoff);
-                               } else {                             /* reg arg -> spilled    */
-                                       M_LST(s2, REG_SP, var->vv.regoff * 8);
-                               } 
-                       } else {                                 /* stack arguments       */
-                               if (!IS_INMEMORY(var->flags)) {      /* stack arg -> register */
-                                       M_LLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 8);
-
-                               } else {                             /* stack arg -> spilled  */
-                                       var->vv.regoff = cd->stackframesize + s1;
-                               }
-                       }
+               s1  = md->params[p].regoff;
 
-               } else {                                     /* floating args         */
-                       if (!md->params[p].inmemory) {           /* register arguments    */
-                               s2 = rd->argfltregs[s1];
-                               if (!IS_INMEMORY(var->flags)) {      /* reg arg -> register   */
-                                       M_FLTMOVE(s2, var->vv.regoff);
-                               } else {                                         /* reg arg -> spilled    */
-                                       M_DST(s2, REG_SP, var->vv.regoff * 8);
+               if (IS_INT_LNG_TYPE(t)) {
+                       if (!md->params[p].inmemory) {
+                               if (!IS_INMEMORY(var->flags))
+                                       M_INTMOVE(s1, var->vv.regoff);
+                               else
+                                       M_LST(s1, REG_SP, var->vv.regoff);
+                       }
+                       else {
+                               if (!IS_INMEMORY(var->flags))
+                                       M_LLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
+                               else
+                                       var->vv.regoff = cd->stackframesize * 8 + s1;
+                       }
+               }
+               else {
+                       if (!md->params[p].inmemory) {
+                               if (!IS_INMEMORY(var->flags))
+                                       M_FLTMOVE(s1, var->vv.regoff);
+                               else
+                                       M_DST(s1, REG_SP, var->vv.regoff);
+                       }
+                       else {
+                               if (!(var->flags & INMEMORY)) {
+                                       if (IS_2_WORD_TYPE(t))
+                                               M_DLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
+                                       else
+                                               M_DLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
                                }
-
-                       } else {                                 /* stack arguments       */
-                               if (!(var->flags & INMEMORY)) {      /* stack-arg -> register */
-                                       if (IS_2_WORD_TYPE(t)) {
-                                               M_DLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 8);
-                                       } else {
-                                               M_DLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 8);
-                                       }
-                               } else {                             /* stack-arg -> spilled  */
-                                       var->vv.regoff = cd->stackframesize + s1;
-                               }
+                               else
+                                       var->vv.regoff = cd->stackframesize * 8 + s1;
                        }
                }
-       } /* end for */
+       }
 
        /* save monitorenter argument */
 
@@ -267,20 +264,22 @@ bool codegen_emit(jitdata *jd)
 
                /* stackoffset for argument used for LOCK_monitor_exit */
                s1 = rd->memuse;
+
 #if !defined (NDEBUG)
                if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
                        M_AADD_IMM(REG_SP, -((LA_SIZE_IN_POINTERS + PA_SIZE_IN_POINTERS + ARG_CNT) * 8), REG_SP);
 
                        for (p = 0; p < INT_ARG_CNT; p++)
-                               M_LST(rd->argintregs[p], REG_SP, LA_SIZE + PA_SIZE + p * 8);
+                               M_LST(abi_registers_integer_argument[p], REG_SP, LA_SIZE + PA_SIZE + p * 8);
 
                        for (p = 0; p < FLT_ARG_CNT; p++)
-                               M_DST(rd->argfltregs[p], REG_SP, LA_SIZE + PA_SIZE + (INT_ARG_CNT + p) * 8);
+                               M_DST(abi_registers_float_argument[p], REG_SP, LA_SIZE + PA_SIZE + (INT_ARG_CNT + p) * 8);
 
                        /* used for LOCK_monitor_exit, adopt size because we created another stackframe */
                        s1 += (LA_SIZE_IN_POINTERS + PA_SIZE_IN_POINTERS + ARG_CNT);
                }
 #endif
+
                p = dseg_add_functionptr(cd, LOCK_monitor_enter);
                M_ALD(REG_ITMP3, REG_PV, p);
                M_ALD(REG_ITMP3, REG_ITMP3, 0); /* TOC */
@@ -290,23 +289,24 @@ bool codegen_emit(jitdata *jd)
 
                if (m->flags & ACC_STATIC) {
                        p = dseg_add_address(cd, &m->class->object.header);
-                       M_ALD(rd->argintregs[0], REG_PV, p);
+                       M_ALD(REG_A0, REG_PV, p);
                }
                else {
-                       M_TST(rd->argintregs[0]);
+                       M_TST(REG_A0);
                        M_BNE(1);
                        M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
                }
 
-               M_AST(rd->argintregs[0], REG_SP, s1 * 8);       /* rd->memuse * 8 */
+               M_AST(REG_A0, REG_SP, s1 * 8);                      /* rd->memuse * 8 */
                M_JSR;
-#if !defined (NDEBUG)
+
+#if !defined(NDEBUG)
                if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
                        for (p = 0; p < INT_ARG_CNT; p++)
-                               M_LLD(rd->argintregs[p], REG_SP, LA_SIZE + PA_SIZE + p * 8);
+                               M_LLD(abi_registers_integer_argument[p], REG_SP, LA_SIZE + PA_SIZE + p * 8);
 
                        for (p = 0; p < FLT_ARG_CNT; p++)
-                               M_DLD(rd->argfltregs[p], REG_SP, LA_SIZE + PA_SIZE + (INT_ARG_CNT + p) * 8);
+                               M_DLD(abi_registers_float_argument[p], REG_SP, LA_SIZE + PA_SIZE + (INT_ARG_CNT + p) * 8);
 
                        M_AADD_IMM(REG_SP, (LA_SIZE_IN_POINTERS + PA_SIZE_IN_POINTERS + ARG_CNT) * 8, REG_SP);
                }
@@ -318,9 +318,9 @@ bool codegen_emit(jitdata *jd)
 #if !defined (NDEBUG)
        if (JITDATA_HAS_FLAG_VERBOSECALL(jd))
                emit_verbosecall_enter(jd);
+#endif
 
        }
-#endif
 
        /* end of header generation */
 
@@ -346,7 +346,7 @@ bool codegen_emit(jitdata *jd)
                /* copy interface registers to their destination */
 
                len = bptr->indepth;
-               MCODECHECK(64+len);
+               MCODECHECK(128+len);
 
 #if defined(ENABLE_LSRA)
                if (opt_lsra) {
@@ -387,13 +387,12 @@ bool codegen_emit(jitdata *jd)
                currentline = 0;
                        
                for (iptr = bptr->iinstr; len > 0; len--, iptr++) {
-                       bool sign_ext = false;
                        if (iptr->line != currentline) {
                                dseg_addlinenumber(cd, iptr->line);
                                currentline = iptr->line;
                        }
 
-                       MCODECHECK(64);   /* an instruction usually needs < 64 words      */
+                       MCODECHECK(128);   /* an instruction usually needs < 64 words      */
 
                switch (iptr->opc) {
                case ICMD_NOP:        /* ...  ==> ...                                 */
@@ -481,33 +480,38 @@ bool codegen_emit(jitdata *jd)
                case ICMD_ILOAD:      /* ...  ==> ..., content of local variable      */
                case ICMD_ALOAD:      /* s1.localindex = local variable               */
                case ICMD_LLOAD:
-               case ICMD_FLOAD:      /* ...  ==> ..., content of local variable      */
-               case ICMD_DLOAD:      /* ...  ==> ..., content of local variable      */
+               case ICMD_FLOAD:
+               case ICMD_DLOAD:
                case ICMD_ISTORE:     /* ..., value  ==> ...                          */
                case ICMD_LSTORE:
-               case ICMD_FSTORE:     /* ..., value  ==> ...                          */
-               case ICMD_DSTORE:     /* ..., value  ==> ...                          */
+               case ICMD_FSTORE:
+               case ICMD_DSTORE:
                case ICMD_COPY:
                case ICMD_MOVE:
 
-                       emit_copy(jd, iptr, VAROP(iptr->s1), VAROP(iptr->dst));
+                       emit_copy(jd, iptr);
                        break;
 
                case ICMD_ASTORE:
                        if (!(iptr->flags.bits & INS_FLAG_RETADDR))
-                               emit_copy(jd, iptr, VAROP(iptr->s1), VAROP(iptr->dst));
+                               emit_copy(jd, iptr);
                        break;
 
 
                /* integer operations *************************************************/
 
                case ICMD_INEG:       /* ..., value  ==> ..., - value                 */
-                       sign_ext = true;
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1); 
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_NEG(s1, d);
+                       M_EXTSW(d, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
                case ICMD_LNEG:    
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1); 
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        M_NEG(s1, d);
-                       if (sign_ext) M_EXTSW(d, d);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -608,6 +612,7 @@ bool codegen_emit(jitdata *jd)
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        M_SUB(s1, s2, d);
+                       M_EXTSW(d, d);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -651,7 +656,16 @@ bool codegen_emit(jitdata *jd)
                        break;
 
                case ICMD_IDIV:
-                       sign_ext = true;
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
+                       emit_arithmetic_check(cd, iptr, s2);
+
+                       M_DIV(s1, s2, d);
+                       M_EXTSW(d, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
                case ICMD_LDIV:       /* ..., val1, val2  ==> ..., val1 / val2        */
 
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
@@ -662,18 +676,28 @@ bool codegen_emit(jitdata *jd)
                        M_DIV(s1, s2, d);
                        /* we need to test if divident was 0x8000000000000, bit OV is set in XER in this case */
                        /* we only need to check this if we did a LDIV, not for IDIV */
-                       if (!sign_ext)  {
-                               M_MFXER(REG_ITMP2);
-                               M_ANDIS(REG_ITMP2, 0x4000, REG_ITMP2);  /* test OV */
-                               M_BLE(1);
-                               M_MOV(s1, d);                           /* java specs says result == dividend */
-                       }
-                       if (sign_ext) M_EXTSW(d, d);
+                       M_MFXER(REG_ITMP2);
+                       M_ANDIS(REG_ITMP2, 0x4000, REG_ITMP2);  /* test OV */
+                       M_BLE(1);
+                       M_MOV(s1, d);                           /* java specs says result == dividend */
                        emit_store_dst(jd, iptr, d);
                        break;
 
                case ICMD_IREM:
-                       sign_ext = true;
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       emit_arithmetic_check(cd, iptr, s2);
+
+                       M_DIV(s1, s2,  REG_ITMP3);      
+                       M_MUL(REG_ITMP3, s2, REG_ITMP2);
+                       M_SUB(s1, REG_ITMP2,  REG_ITMP3);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP1);
+
+                       M_MOV(REG_ITMP3, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
+
                case ICMD_LREM:       /* ..., val1, val2  ==> ..., val1 % val2        */
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
@@ -682,30 +706,34 @@ bool codegen_emit(jitdata *jd)
                        M_DIV(s1, s2,  REG_ITMP3);      
                        /* we need to test if divident was 0x8000000000000, bit OV is set in XER in this case */
                        /* we only need to check this if we did a LDIV, not for IDIV */
-                       if (!sign_ext)  {
-                               M_MFXER(REG_ITMP2);
-                               M_ANDIS(REG_ITMP2, 0x4000, REG_ITMP2);  /* test OV */
-                               M_BLE(2); 
-                               LCONST(REG_ITMP3, 0);                   /* result == 0 in this case */
-                               M_BR(2);
-                       }
+                       M_MFXER(REG_ITMP2);
+                       M_ANDIS(REG_ITMP2, 0x4000, REG_ITMP2);  /* test OV */
+                       M_BLE(2); 
+                       LCONST(REG_ITMP3, 0);                   /* result == 0 in this case */
+                       M_BR(2);
                        M_MUL(REG_ITMP3, s2, REG_ITMP2);
                        M_SUB(s1, REG_ITMP2,  REG_ITMP3);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP1);
 
                        M_MOV(REG_ITMP3, d);
-                       emit_store_dst(jd, iptr, REG_ITMP1);
+                       emit_store_dst(jd, iptr, d);
                        break;
 
                
                case ICMD_IMUL:       /* ..., val1, val2  ==> ..., val1 * val2        */
-                       sign_ext = true;
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_MUL(s1, s2, d);
+                       M_EXTSW(d, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
                case ICMD_LMUL:
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        M_MUL(s1, s2, d);
-                       if (sign_ext) M_EXTSW(d, d);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -736,11 +764,12 @@ bool codegen_emit(jitdata *jd)
                        break;
 
                case ICMD_IDIVPOW2:   /* ..., value  ==> ..., value << constant       */
-                                     
+                     
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
                        M_SRA_IMM(s1, iptr->sx.val.i, d);
                        M_ADDZE(d, d);
+                       M_EXTSW(d, d);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -790,7 +819,10 @@ bool codegen_emit(jitdata *jd)
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        M_AND_IMM(s2, 0x1f, REG_ITMP2);
-                       M_SRL(s1, REG_ITMP2, d);
+                       M_MOV(s1, REG_ITMP1);
+                       M_CLR_HIGH(REG_ITMP1);
+                       M_SRL(REG_ITMP1, REG_ITMP2, d);
+                       M_EXTSW(d,d);   /* for the case it was shift 0 bits */
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -800,16 +832,68 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        if (iptr->sx.val.i & 0x1f) {
-                               M_SRL_IMM(s1, iptr->sx.val.i & 0x1f, d);
+                               M_MOV(s1, REG_ITMP1);
+                               M_CLR_HIGH(REG_ITMP1);
+                               M_SRA_IMM(REG_ITMP1, iptr->sx.val.i & 0x1f, d);
                        } else {
                                M_INTMOVE(s1, d);
                        }
                        emit_store_dst(jd, iptr, d);
                        break;
+       
+               case ICMD_LSHLCONST:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_SLL_IMM(s1, iptr->sx.val.i & 0x3f, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+               case ICMD_LSHL:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_AND_IMM(s2, 0x3f, REG_ITMP2);
+                       M_SLL(s1, REG_ITMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+               case ICMD_LSHRCONST:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_SRA_IMM(s1, iptr->sx.val.i & 0x3f, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+               case ICMD_LSHR:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_AND_IMM(s2, 0x3f, REG_ITMP2);
+                       M_SRA(s1, REG_ITMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+               case ICMD_LUSHRCONST:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_SRL_IMM(s1, iptr->sx.val.i & 0x3f, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+               case ICMD_LUSHR:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+                       M_AND_IMM(s2, 0x3f, REG_ITMP2);
+                       M_SRL(s1, REG_ITMP2, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
 
                case ICMD_IAND:       /* ..., val1, val2  ==> ..., val1 & val2        */
-               case ICMD_LAND:
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
+                       M_AND(s1, s2, d);
+/*                     M_EXTSW(d, d);*/
+                       emit_store_dst(jd, iptr, d);
+                       break;
 
+               case ICMD_LAND:
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
@@ -860,11 +944,23 @@ bool codegen_emit(jitdata *jd)
                                      /* sx.val.i = constant                             */
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
+#if 0
+                       /* fast division, result in REG_ITMP3) */
+                       M_SRA_IMM(s1, iptr->sx.val.i, REG_ITMP3);
+                       M_ADDZE(REG_ITMP3, REG_ITMP3);
+
+                       M_SUB(s1, REG_ITMP3, d);
+                       M_EXTSW(d, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+#else
+                       
                        M_MOV(s1, REG_ITMP2);
                        M_CMPI(s1, 0);
-                       M_BGE(1 + 2*(iptr->sx.val.i >= 32768));
+                       M_BGE(1 + 3*(iptr->sx.val.i >= 32768));
                        if (iptr->sx.val.i >= 32768) {
                                M_ADDIS(REG_ZERO, iptr->sx.val.i >> 16, REG_ITMP2);
+                               M_EXTSW(REG_ITMP2, REG_ITMP2);
                                M_OR_IMM(REG_ITMP2, iptr->sx.val.i, REG_ITMP2);
                                M_IADD(s1, REG_ITMP2, REG_ITMP2);
                        } else {
@@ -880,8 +976,17 @@ bool codegen_emit(jitdata *jd)
                        M_EXTSW(d, d);
                        emit_store_dst(jd, iptr, d);
                        break;
+#endif
 
                case ICMD_IOR:        /* ..., val1, val2  ==> ..., val1 | val2        */
+                       s1 = emit_load_s1(jd, iptr, REG_ITMP1);
+                       s2 = emit_load_s2(jd, iptr, REG_ITMP2);
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
+                       M_OR(s1, s2, d);
+/*                     M_EXTSW(d,d);*/
+                       emit_store_dst(jd, iptr, d);
+                       break;
+
                case ICMD_LOR:
 
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
@@ -1236,7 +1341,7 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_nullpointer_check(cd, iptr, s1);
-                       M_ILD(d, s1, OFFSET(java_arrayheader, size));
+                       M_ILD(d, s1, OFFSET(java_array_t, size));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1246,7 +1351,7 @@ bool codegen_emit(jitdata *jd)
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
-                       M_IADD_IMM(s2, OFFSET(java_bytearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(s2, OFFSET(java_bytearray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LBZX(d, s1, REG_ITMP2);
                        M_BSEXT(d, d);
@@ -1260,7 +1365,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SLL_IMM(s2, 1, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_chararray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_chararray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LHZX(d, s1, REG_ITMP2);
                        emit_store_dst(jd, iptr, d);
@@ -1273,7 +1378,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SLL_IMM(s2, 1, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_shortarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_shortarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LHAX(d, s1, REG_ITMP2);
                        emit_store_dst(jd, iptr, d);
@@ -1286,7 +1391,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SLL_IMM(s2, 2, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_intarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_intarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LWAX(d, s1, REG_ITMP2);
                        emit_store_dst(jd, iptr, d);
@@ -1301,7 +1406,7 @@ bool codegen_emit(jitdata *jd)
                        M_SLL_IMM(s2, 3, REG_ITMP2);
                        M_IADD(s1, REG_ITMP2, REG_ITMP2);
                        /* implicit null-pointer check */
-                       M_LLD_INTERN(d, REG_ITMP2, OFFSET(java_longarray, data[0]));
+                       M_LLD_INTERN(d, REG_ITMP2, OFFSET(java_longarray_t, data[0]));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1312,7 +1417,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SLL_IMM(s2, 2, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_floatarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_floatarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LFSX(d, s1, REG_ITMP2);
                        emit_store_dst(jd, iptr, d);
@@ -1325,7 +1430,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SLL_IMM(s2, 3, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_doublearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_doublearray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LFDX(d, s1, REG_ITMP2);
                        emit_store_dst(jd, iptr, d);
@@ -1338,7 +1443,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SLL_IMM(s2, 3, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_objectarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_objectarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_ALDX(d, s1, REG_ITMP2);
                        emit_store_dst(jd, iptr, d);
@@ -1351,7 +1456,7 @@ bool codegen_emit(jitdata *jd)
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
-                       M_IADD_IMM(s2, OFFSET(java_bytearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(s2, OFFSET(java_bytearray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_STBX(s3, s1, REG_ITMP2);
                        break;
@@ -1363,7 +1468,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_SLL_IMM(s2, 1, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_chararray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_chararray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_STHX(s3, s1, REG_ITMP2);
                        break;
@@ -1375,7 +1480,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_SLL_IMM(s2, 1, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_shortarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_shortarray_t, data[0]), REG_ITMP2);
                        M_STHX(s3, s1, REG_ITMP2);
                        break;
 
@@ -1386,7 +1491,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_SLL_IMM(s2, 2, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_intarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_intarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_STWX(s3, s1, REG_ITMP2);
                        break;
@@ -1398,7 +1503,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_SLL_IMM(s2, 3, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_longarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_longarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_LSTX(s3, s1, REG_ITMP2);
                        break;
@@ -1410,7 +1515,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_FTMP3);
                        M_SLL_IMM(s2, 2, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_floatarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_floatarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_STFSX(s3, s1, REG_ITMP2);
                        break;
@@ -1422,7 +1527,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_FTMP3);
                        M_SLL_IMM(s2, 3, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_doublearray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_doublearray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_STFDX(s3, s1, REG_ITMP2);
                        break;
@@ -1449,7 +1554,7 @@ bool codegen_emit(jitdata *jd)
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_SLL_IMM(s2, 3, REG_ITMP2);
-                       M_IADD_IMM(REG_ITMP2, OFFSET(java_objectarray, data[0]), REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP2, OFFSET(java_objectarray_t, data[0]), REG_ITMP2);
                        /* implicit null-pointer check */
                        M_ASTX(s3, s1, REG_ITMP2);
                        break;
@@ -1458,19 +1563,18 @@ bool codegen_emit(jitdata *jd)
                case ICMD_GETSTATIC:  /* ...  ==> ..., value                          */
 
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                               uf = iptr->sx.s23.s3.uf;
-
+                               uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
-                               disp = dseg_add_unique_address(cd, NULL);
+                               disp      = dseg_add_unique_address(cd, NULL);
 
                                codegen_addpatchref(cd, PATCHER_get_putstatic,
                                                                        iptr->sx.s23.s3.uf, disp);
 
-                       } else {
-                               fieldinfo *fi = iptr->sx.s23.s3.fmiref->p.field;
-
+                       }
+                       else {
+                               fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp = dseg_add_address(cd, &(fi->value));
+                               disp      = dseg_add_address(cd, fi->value);
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
                                        codegen_addpatchref(cd, PATCHER_clinit, fi->class, disp);
@@ -1507,18 +1611,17 @@ bool codegen_emit(jitdata *jd)
 
 
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                               uf = iptr->sx.s23.s3.uf;
-
+                               uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
-                               disp = dseg_add_unique_address(cd, NULL);
+                               disp      = dseg_add_unique_address(cd, NULL);
 
                                codegen_addpatchref(cd, PATCHER_get_putstatic,
                                                                        iptr->sx.s23.s3.uf, disp);
-                       } else {
-                               fieldinfo *fi = iptr->sx.s23.s3.fmiref->p.field;
-
+                       }
+                       else {
+                               fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp = dseg_add_address(cd, &(fi->value));
+                               disp      = dseg_add_address(cd, fi->value);
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
                                        codegen_addpatchref(cd, PATCHER_clinit, fi->class, disp);
@@ -1556,15 +1659,16 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
 
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                               uf = iptr->sx.s23.s3.uf;
+                               uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
-                               disp = 0;
+                               disp      = 0;
 
                                codegen_addpatchref(cd, PATCHER_get_putfield, uf, 0);
-                       } else {
-                               fi = iptr->sx.s23.s3.fmiref->p.field;
+                       }
+                       else {
+                               fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp = fi->offset;
+                               disp      = fi->offset;
                        }
 
                        /* implicit null-pointer check */
@@ -1850,9 +1954,8 @@ nowperformreturn:
                        /* call trace function */
 
 #if !defined(NDEBUG)
-                       if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
+                       if (JITDATA_HAS_FLAG_VERBOSECALL(jd))
                                emit_verbosecall_exit(jd);
-                       }
 #endif         
 
 #if defined(ENABLE_THREADS)
@@ -1879,7 +1982,7 @@ nowperformreturn:
                                        break;
                                }
 
-                               M_ALD(rd->argintregs[0], REG_SP, rd->memuse * 8);
+                               M_ALD(REG_A0, REG_SP, rd->memuse * 8);
                                M_JSR;
 
                                /* and now restore the proper return value */
@@ -1989,7 +2092,7 @@ nowperformreturn:
 
                        i = iptr->sx.s23.s2.lookupcount;
                        
-                       MCODECHECK((i<<2)+8);
+                       MCODECHECK((i<<3)+8);
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        while (--i >= 0) {
                                val = lookup->value;
@@ -2039,36 +2142,38 @@ nowperformreturn:
 gen_method:
                        s3 = md->paramcount;
 
-                       MCODECHECK((s3 << 1) + 64);
+                       MCODECHECK((s3 << 2) + 128);
 
                        /* copy arguments to registers or stack location */
 
                        for (s3 = s3 - 1; s3 >= 0; s3--) {
                                var = VAR(iptr->sx.s23.s2.args[s3]);
+                               d   = md->params[s3].regoff;
 
                                if (var->flags & PREALLOC)
                                        continue;
 
                                if (IS_INT_LNG_TYPE(var->type)) {
                                        if (!md->params[s3].inmemory) {
-                                               s1 = rd->argintregs[md->params[s3].regoff];
-                                               d = emit_load(jd, iptr, var, s1);
-                                               M_LNGMOVE(d, s1);
-                                       } else {
-                                               d = emit_load(jd, iptr, var, REG_ITMP1);
-                                               M_LST(d, REG_SP, md->params[s3].regoff * 8);
+                                               s1 = emit_load(jd, iptr, var, d);
+                                               M_LNGMOVE(s1, d);
                                        }
-                               } else {
+                                       else {
+                                               s1 = emit_load(jd, iptr, var, REG_ITMP1);
+                                               M_LST(s1, REG_SP, d);
+                                       }
+                               }
+                               else {
                                        if (!md->params[s3].inmemory) {
-                                               s1 = rd->argfltregs[md->params[s3].regoff];
-                                               d = emit_load(jd, iptr, var, s1);
-                                               M_FLTMOVE(d, s1);
-                                       else {
-                                               d = emit_load(jd, iptr, var, REG_FTMP1);
-                                               M_DST(d, REG_SP, md->params[s3].regoff * 8);
+                                               s1 = emit_load(jd, iptr, var, d);
+                                               M_FLTMOVE(s1, d);
+                                       }
+                                       else {
+                                               s1 = emit_load(jd, iptr, var, REG_FTMP1);
+                                               M_DST(s1, REG_SP, d);
                                        }
                                }
-                       } /* end of for */
+                       }
 
                        switch (iptr->opc) {
                        case ICMD_BUILTIN:
@@ -2124,7 +2229,7 @@ gen_method:
                                }
 
                                /* implicit null-pointer check */
-                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_object_t, vftbl));
                                M_ALD(REG_PV, REG_METHODPTR, s1);
 
                                /* generate the actual call */
@@ -2152,7 +2257,7 @@ gen_method:
                                }
 
                                /* implicit null-pointer check */
-                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_objectheader, vftbl));    
+                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_object_t, vftbl));    
                                M_ALD(REG_METHODPTR, REG_METHODPTR, s1);
                                M_ALD(REG_PV, REG_METHODPTR, s2);
 
@@ -2207,42 +2312,25 @@ gen_method:
                                s4         superindex;
 
                                if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                                       super = NULL;
+                                       super      = NULL;
                                        superindex = 0;
-                               } else {
-                                       super = iptr->sx.s23.s3.c.cls;
+                               }
+                               else {
+                                       super      = iptr->sx.s23.s3.c.cls;
                                        superindex = super->index;
                                }
-                       
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritrestart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
-                               s1 = emit_load_s1(jd, iptr, REG_ITMP1);
-
-                               /* calculate interface checkcast code size */
-
-                               s2 = 9;
-#if defined(SOFTEX)
-                               s2 += CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) ? 2 : 0;
-#endif
-                               if (super == NULL)
-                                       s2 += (opt_shownops ? 1 : 0);
-
-                               /* calculate class checkcast code size */
+               
+                               if ((super == NULL) || !(super->flags & ACC_INTERFACE)) {
+                                       CODEGEN_CRITICAL_SECTION_NEW;
+                               }
 
-                               s3 = 10 + (s1 == REG_ITMP1);
-#if defined(SOFTEX)
-                               s3 += CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) ? 1 : 0;
-#endif
-                               if (super == NULL)
-                                       s3 += (opt_shownops ? 1 : 0);
+                               s1 = emit_load_s1(jd, iptr, REG_ITMP1);
 
                                /* if class is not resolved, check which code to call */
 
                                if (super == NULL) {
                                        M_TST(s1);
-                                       M_BEQ(3 + (opt_shownops ? 1 : 0) + s2 + 1 + s3);
-
+                                       emit_label_beq(cd, BRANCH_LABEL_1);
                                        disp = dseg_add_unique_s4(cd, 0);                     /* super->flags */
 
                                        codegen_addpatchref(cd,
@@ -2252,7 +2340,8 @@ gen_method:
 
                                        M_ILD(REG_ITMP2, REG_PV, disp);
                                        M_AND_IMM(REG_ITMP2, ACC_INTERFACE, REG_ITMP2);
-                                       M_BEQ(s2 + 1);
+
+                                       emit_label_beq(cd, BRANCH_LABEL_2);
                                }
 
                                /* interface checkcast code */
@@ -2265,10 +2354,10 @@ gen_method:
                                                                                        0);
                                        } else {
                                                M_TST(s1);
-                                               M_BEQ(s2);
+                                               emit_label_beq(cd, BRANCH_LABEL_3);
                                        }
 
-                                       M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, interfacetablelength));
                                        M_LDATST(REG_ITMP3, REG_ITMP3, -superindex);
                                        emit_classcast_check(cd, iptr, BRANCH_LE, REG_ITMP3, s1);
@@ -2278,14 +2367,19 @@ gen_method:
                                        M_TST(REG_ITMP3);
                                        emit_classcast_check(cd, iptr, BRANCH_EQ, REG_ITMP3, s1);
 
-                                       if (!super)
-                                               M_BR(s3);
+                                       if (super == NULL)      {
+                                               emit_label_br(cd, BRANCH_LABEL_4);
+                                       } else  {
+                                               emit_label(cd, BRANCH_LABEL_3);
+                                       }
                                }
 
                                /* class checkcast code */
 
                                if ((super == NULL) || !(super->flags & ACC_INTERFACE)) {
                                        if (super == NULL) {
+                                               emit_label(cd, BRANCH_LABEL_2);
+
                                                disp = dseg_add_unique_address(cd, NULL);
                                                codegen_addpatchref(cd, PATCHER_checkcast_class,
                                                                                        iptr->sx.s23.s3.c.ref,
@@ -2293,21 +2387,21 @@ gen_method:
                                        } else {
                                                disp = dseg_add_address(cd, super->vftbl);
                                                M_TST(s1);
-                                               M_BEQ(s3);
+                                               emit_label_beq(cd, BRANCH_LABEL_5);
                                        }
 
-                                       M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
-#if defined(ENABLE_THREADS)
-                                       codegen_threadcritstart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+                                       M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
+
+                                       CODEGEN_CRITICAL_SECTION_START;
+
                                        M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                        M_ALD(REG_ITMP2, REG_PV, disp);
                                        if (s1 != REG_ITMP1) {
                                                M_ILD(REG_ITMP1, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                                M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
-#if defined(ENABLE_THREADS)
-                                               codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                                               CODEGEN_CRITICAL_SECTION_END;
+
                                                M_SUB(REG_ITMP3, REG_ITMP1, REG_ITMP3);
                                                M_EXTSW(REG_ITMP3, REG_ITMP3);
                                        } else {
@@ -2316,20 +2410,28 @@ gen_method:
                                                M_EXTSW(REG_ITMP3, REG_ITMP3);
                                                M_ALD(REG_ITMP2, REG_PV, disp);
                                                M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
-#if defined(ENABLE_THREADS)
-                                               codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                                               CODEGEN_CRITICAL_SECTION_END;
+
                                        }
                                        M_CMPU(REG_ITMP3, REG_ITMP2);
                                        emit_classcast_check(cd, iptr, BRANCH_GT, REG_ITMP3, s1);
+                                       
+                                       if (super != NULL)
+                                               emit_label(cd, BRANCH_LABEL_5);
+                               }
+
+                               if (super == NULL) {
+                                       emit_label(cd, BRANCH_LABEL_1);
+                                       emit_label(cd, BRANCH_LABEL_4);
                                }
                                d = codegen_reg_of_dst(jd, iptr, s1);
 
                        } else {
                                /* array type cast-check */
 
-                               s1 = emit_load_s1(jd, iptr, rd->argintregs[0]);
-                               M_INTMOVE(s1, rd->argintregs[0]);
+                               s1 = emit_load_s1(jd, iptr, REG_A0);
+                               M_INTMOVE(s1, REG_A0);
 
 
                                if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
@@ -2341,7 +2443,7 @@ gen_method:
                                        disp = dseg_add_address(cd, iptr->sx.s23.s3.c.cls);
                                }
 
-                               M_ALD(rd->argintregs[1], REG_PV, disp);
+                               M_ALD(REG_A1, REG_PV, disp);
                                disp = dseg_add_functionptr(cd, BUILTIN_arraycheckcast);
                                M_ALD(REG_ITMP2, REG_PV, disp);
                                M_ALD(REG_ITMP2, REG_ITMP2, 0); /* TOC */
@@ -2376,23 +2478,21 @@ gen_method:
 
                        {
                        classinfo *super;
-                       vftbl_t   *supervftbl;
                        s4         superindex;
 
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                               super = NULL;
+                               super      = NULL;
                                superindex = 0;
-                               supervftbl = NULL;
                        }
                        else {
-                               super = iptr->sx.s23.s3.c.cls;
+                               super      = iptr->sx.s23.s3.c.cls;
                                superindex = super->index;
-                               supervftbl = super->vftbl;
                        }
                        
-#if defined(ENABLE_THREADS)
-            codegen_threadcritrestart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+                       if ((super == NULL) || !(super->flags & ACC_INTERFACE)) {
+                               CODEGEN_CRITICAL_SECTION_NEW;
+                       }
+
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        if (s1 == d) {
@@ -2400,26 +2500,13 @@ gen_method:
                                s1 = REG_ITMP1;
                        }
 
-                       /* calculate interface instanceof code size */
-
-                       s2 = 8;
-                       if (!super)
-                               s2 += (opt_shownops ? 1 : 0);
-
-                       /* calculate class instanceof code size */
-
-                       s3 = 11;
-                       if (super == NULL)
-                               s3 += (opt_shownops ? 1 : 0);
-
                        M_CLR(d);
 
                        /* if class is not resolved, check which code to call */
 
                        if (super == NULL) {
                                M_TST(s1);
-                               M_BEQ(3 + (opt_shownops ? 1 : 0) + s2 + 1 + s3);
-
+                               emit_label_beq(cd, BRANCH_LABEL_1);
                                disp = dseg_add_unique_s4(cd, 0);                     /* super->flags */
 
                                codegen_addpatchref(cd, PATCHER_checkcast_instanceof_flags,
@@ -2427,23 +2514,23 @@ gen_method:
 
                                M_ILD(REG_ITMP3, REG_PV, disp);
                                M_AND_IMM(REG_ITMP3, ACC_INTERFACE, REG_ITMP3);
-                               M_BEQ(s2 + 1);
+                               emit_label_beq(cd, BRANCH_LABEL_2);
                        }
 
                        /* interface instanceof code */
 
-                       if (!super || (super->flags & ACC_INTERFACE)) {
-                               if (super) {
-                                       M_TST(s1);
-                                       M_BEQ(s2);
-
-                               } else {
+                       if ((super == NULL) || (super->flags & ACC_INTERFACE)) {
+                               if (super == NULL) {
                                        codegen_addpatchref(cd,
                                                                                PATCHER_instanceof_interface,
                                                                                iptr->sx.s23.s3.c.ref, 0);
+
+                               } else {
+                                       M_TST(s1);
+                                       emit_label_beq(cd, BRANCH_LABEL_3);
                                }
 
-                               M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ILD(REG_ITMP3, REG_ITMP1, OFFSET(vftbl_t, interfacetablelength));
                                M_LDATST(REG_ITMP3, REG_ITMP3, -superindex);
                                M_BLE(4);
@@ -2454,44 +2541,58 @@ gen_method:
                                M_BEQ(1);
                                M_IADD_IMM(REG_ZERO, 1, d);
 
-                               if (super == NULL)
-                                       M_BR(s3);
+                               if (super == NULL)      {
+                                       emit_label_br(cd, BRANCH_LABEL_4);
+                               } else  {
+                                       emit_label(cd, BRANCH_LABEL_3);
+                               }
                        }
 
                        /* class instanceof code */
 
                        if ((super == NULL) || !(super->flags & ACC_INTERFACE)) {
 
-                               if (super) {
-                                       disp = dseg_add_address(cd, supervftbl);
-                                       M_TST(s1);
-                                       M_BEQ(s3);
+                               if (super == NULL) {
+                                       emit_label(cd, BRANCH_LABEL_2);
 
-                               } else {
                                        disp = dseg_add_unique_address(cd, NULL);
                                        codegen_addpatchref(cd, PATCHER_instanceof_class,
                                                                                iptr->sx.s23.s3.c.ref,
                                                                                disp);
+
+                               } else {
+                                       disp = dseg_add_address(cd, super->vftbl);
+                                       M_TST(s1);
+                                       emit_label_beq(cd, BRANCH_LABEL_5);
                                }
 
-                               M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ALD(REG_ITMP2, REG_PV, disp);
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritstart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                               CODEGEN_CRITICAL_SECTION_START;
+
                                M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
                                M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                               CODEGEN_CRITICAL_SECTION_END;
+
                                M_SUB(REG_ITMP1, REG_ITMP3, REG_ITMP1);
                                M_EXTSW(REG_ITMP1, REG_ITMP1);
                                M_CMPU(REG_ITMP1, REG_ITMP2);
                                M_CLR(d);
                                M_BGT(1);
                                M_IADD_IMM(REG_ZERO, 1, d);
+
+                               if (super != NULL)
+                                       emit_label(cd, BRANCH_LABEL_5);
                        }
+
+                       if (super == NULL) {
+                               emit_label(cd, BRANCH_LABEL_1);
+                               emit_label(cd, BRANCH_LABEL_4);
+                       }
+
                        emit_store_dst(jd, iptr, d);
                        }
                        break;
@@ -2500,7 +2601,7 @@ gen_method:
 
                        /* check for negative sizes and copy sizes to stack if necessary  */
 
-                       MCODECHECK((iptr->s1.argcount << 1) + 64);
+                       MCODECHECK((iptr->s1.argcount << 2) + 128);
 
                        for (s1 = iptr->s1.argcount; --s1 >= 0; ) {
 
@@ -2520,7 +2621,7 @@ gen_method:
 
                        /* a0 = dimension count */
 
-                       ICONST(rd->argintregs[0], iptr->s1.argcount);
+                       ICONST(REG_A0, iptr->s1.argcount);
 
                        /* is patcher function set? */
 
@@ -2535,14 +2636,14 @@ gen_method:
 
                        /* a1 = arraydescriptor */
 
-                       M_ALD(rd->argintregs[1], REG_PV, disp);
+                       M_ALD(REG_A1, REG_PV, disp);
 
                        /* a2 = pointer to dimensions = stack pointer */
 
 #if defined(__DARWIN__)
-                       M_LDA(rd->argintregs[2], REG_SP, LA_SIZE + INT_ARG_CNT * 8);
+                       M_LDA(REG_A2, REG_SP, LA_SIZE + INT_ARG_CNT * 8);
 #else
-                       M_LDA(rd->argintregs[2], REG_SP, LA_SIZE + 3 * 8);
+                       M_LDA(REG_A2, REG_SP, LA_SIZE + 3 * 8);
 #endif
 
                        disp = dseg_add_functionptr(cd, BUILTIN_multianewarray);
@@ -2575,7 +2676,6 @@ gen_method:
        /* generate stubs */
 
        emit_patcher_stubs(jd);
-       REPLACEMENT_EMIT_STUBS(jd);
 
        /* everything's ok */
 
@@ -2583,91 +2683,54 @@ gen_method:
 }
 
 
-/* createcompilerstub **********************************************************
+/* codegen_emit_stub_compiler **************************************************
 
-   Creates a stub routine which calls the compiler.
+   Emits a stub routine which calls the compiler.
        
 *******************************************************************************/
 
-#define COMPILERSTUB_DATASIZE    3 * SIZEOF_VOID_P
-#define COMPILERSTUB_CODESIZE    4 * 4
-
-#define COMPILERSTUB_SIZE        COMPILERSTUB_DATASIZE + COMPILERSTUB_CODESIZE
-
-
-u1 *createcompilerstub(methodinfo *m)
+void codegen_emit_stub_compiler(jitdata *jd)
 {
-       u1          *s;                     /* memory to hold the stub            */
-       ptrint      *d;
+       methodinfo  *m;
        codegendata *cd;
-       s4           dumpsize;
-
-       s = CNEW(u1, COMPILERSTUB_SIZE);
-
-       /* set data pointer and code pointer */
 
-       d = (ptrint *) s;
-       s = s + COMPILERSTUB_DATASIZE;
-
-       /* mark start of dump memory area */
-
-       dumpsize = dump_size();
-
-       cd = DNEW(codegendata);
-       cd->mcodeptr = s;
+       /* get required compiler data */
 
-       /* Store the codeinfo pointer in the same place as in the
-          methodheader for compiled methods. */
+       m  = jd->m;
+       cd = jd->cd;
 
-       d[0] = (ptrint) asm_call_jit_compiler;
-       d[1] = (ptrint) m;
-       d[2] = (ptrint) &d[1];                                    /* fake code->m */
+       /* code for the stub */
 
        M_ALD_INTERN(REG_ITMP1, REG_PV, -2 * SIZEOF_VOID_P);
        M_ALD_INTERN(REG_PV, REG_PV, -3 * SIZEOF_VOID_P);
        M_MTCTR(REG_PV);
        M_RTS;
-
-       md_cacheflush((u1 *) d, COMPILERSTUB_SIZE);
-
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               count_cstub_len += COMPILERSTUB_SIZE;
-#endif
-
-       /* release dump area */
-
-       dump_release(dumpsize);
-
-       return s;
 }
 
 
-/* createnativestub ************************************************************
+/* codegen_emit_stub_native ****************************************************
 
-   Creates a stub routine which calls a native method.
+   Emits a stub routine which calls a native method.
 
 *******************************************************************************/
 
-u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
+void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 {
-       methodinfo   *m;
-       codeinfo     *code;
-       codegendata  *cd;
-       registerdata *rd;
-       methoddesc   *md;
-       s4            nativeparams;
-       s4            i, j;                 /* count variables                    */
-       s4            t;
-       s4            s1, s2, disp;
-       s4            funcdisp;
+       methodinfo  *m;
+       codeinfo    *code;
+       codegendata *cd;
+       methoddesc  *md;
+       s4           nativeparams;
+       s4           i, j;
+       s4           t;
+       s4           s1, s2, disp;
+       s4           funcdisp;
 
        /* get required compiler data */
 
        m    = jd->m;
        code = jd->code;
        cd   = jd->cd;
-       rd   = jd->rd;
 
        /* set some variables */
 
@@ -2703,10 +2766,10 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
        M_STDU(REG_SP, REG_SP, -(cd->stackframesize * 8));
 
 #if !defined(NDEBUG)
-       if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
+       if (JITDATA_HAS_FLAG_VERBOSECALL(jd))
                emit_verbosecall_enter(jd);
-       }
 #endif
+
        /* get function address (this must happen before the stackframeinfo) */
 
        funcdisp = dseg_add_functionptr(cd, f);
@@ -2727,7 +2790,7 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
                if (IS_INT_LNG_TYPE(t)) {
                        if (!md->params[i].inmemory) {
                                s1 = md->params[i].regoff;
-                               M_LST(rd->argintregs[s1], REG_SP, LA_SIZE + PA_SIZE + 4*8 + j * 8);
+                               M_LST(s1, REG_SP, LA_SIZE + PA_SIZE + 4*8 + j * 8);
                                j++;
                        }
                }
@@ -2737,7 +2800,7 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
                if (IS_FLT_DBL_TYPE(md->paramtypes[i].type)) {
                        if (!md->params[i].inmemory) {
                                s1 = md->params[i].regoff;
-                               M_DST(rd->argfltregs[s1], REG_SP, LA_SIZE + PA_SIZE + 4*8 + j * 8);
+                               M_DST(s1, REG_SP, LA_SIZE + PA_SIZE + 4*8 + j * 8);
                                j++;
                        }
                }
@@ -2745,10 +2808,10 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
 
        /* create native stack info */
 
-       M_AADD_IMM(REG_SP, cd->stackframesize * 8, rd->argintregs[0]);
-       M_MOV(REG_PV, rd->argintregs[1]);
-       M_AADD_IMM(REG_SP, cd->stackframesize * 8, rd->argintregs[2]);
-       M_ALD(rd->argintregs[3], REG_SP, cd->stackframesize * 8 + LA_LR_OFFSET);
+       M_AADD_IMM(REG_SP, cd->stackframesize * 8, REG_A0);
+       M_MOV(REG_PV, REG_A1);
+       M_AADD_IMM(REG_SP, cd->stackframesize * 8, REG_A2);
+       M_ALD(REG_A3, REG_SP, cd->stackframesize * 8 + LA_LR_OFFSET);
        disp = dseg_add_functionptr(cd, codegen_start_native_call);
 
        M_ALD(REG_ITMP1, REG_PV, disp);
@@ -2766,7 +2829,7 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
                if (IS_INT_LNG_TYPE(t)) {
                        if (!md->params[i].inmemory) {
                                s1 = md->params[i].regoff;
-                               M_LLD(rd->argintregs[s1], REG_SP, LA_SIZE + PA_SIZE + 4 * 8 + j * 8);
+                               M_LLD(s1, REG_SP, LA_SIZE + PA_SIZE + 4 * 8 + j * 8);
                                j++;
                        }
                }
@@ -2776,7 +2839,7 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
                if (IS_FLT_DBL_TYPE(md->paramtypes[i].type)) {
                        if (!md->params[i].inmemory) {
                                s1 = md->params[i].regoff;
-                               M_DLD(rd->argfltregs[s1], REG_SP, LA_SIZE + PA_SIZE + 4 * 8 + j * 8);
+                               M_DLD(s1, REG_SP, LA_SIZE + PA_SIZE + 4 * 8 + j * 8);
                                j++;
                        }
                }
@@ -2789,39 +2852,36 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
 
                if (IS_INT_LNG_TYPE(t)) {
                        if (!md->params[i].inmemory) {
-                               s1 = rd->argintregs[md->params[i].regoff];
+                               s1 = md->params[i].regoff;
+                               s2 = nmd->params[j].regoff;
 
-                               if (!nmd->params[j].inmemory) {
-                                       s2 = rd->argintregs[nmd->params[j].regoff];
+                               if (!nmd->params[j].inmemory)
                                        M_INTMOVE(s1, s2);
-                               } else {
-                                       s2 = nmd->params[j].regoff;
-                                       M_LST(s1, REG_SP, s2 * 8);
-                               }
-
-                       } else {
-                               s1 = md->params[i].regoff + cd->stackframesize;
+                               else
+                                       M_LST(s1, REG_SP, s2);
+                       }
+                       else {
+                               s1 = md->params[i].regoff + cd->stackframesize * 8;
                                s2 = nmd->params[j].regoff;
 
-                               M_LLD(REG_ITMP1, REG_SP, s1 * 8);
-                               M_LST(REG_ITMP1, REG_SP, s2 * 8);
+                               M_LLD(REG_ITMP1, REG_SP, s1);
+                               M_LST(REG_ITMP1, REG_SP, s2);
                        }
-
-               else {
+               }
+               else {
                        /* We only copy spilled float arguments, as the float
                           argument registers keep unchanged. */
 
                        if (md->params[i].inmemory) {
-                               s1 = md->params[i].regoff + cd->stackframesize;
+                               s1 = md->params[i].regoff + cd->stackframesize * 8;
                                s2 = nmd->params[j].regoff;
 
-                               M_DLD(REG_FTMP1, REG_SP, s1 * 8);
+                               M_DLD(REG_FTMP1, REG_SP, s1);
 
-                               if (IS_2_WORD_TYPE(t)) {        
-                                       M_DST(REG_FTMP1, REG_SP, s2 * 8);
-                               } else {
-                                       M_FST(REG_FTMP1, REG_SP, s2 * 8 + 4);
-                               }
+                               if (IS_2_WORD_TYPE(t))
+                                       M_DST(REG_FTMP1, REG_SP, s2);
+                               else
+                                       M_FST(REG_FTMP1, REG_SP, s2 + 4);
                        }
                }
        }
@@ -2830,13 +2890,13 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
 
        if (m->flags & ACC_STATIC) {
                disp = dseg_add_unique_address(cd, m->class);
-               M_ALD(rd->argintregs[1], REG_PV, disp);
+               M_ALD(REG_A1, REG_PV, disp);
        }
 
        /* put env into first argument register */
 
        disp = dseg_add_unique_address(cd, _Jv_env);
-       M_ALD(rd->argintregs[0], REG_PV, disp);
+       M_ALD(REG_A0, REG_PV, disp);
 
        /* generate the actual native call */
        /* native functions have a different TOC for sure */
@@ -2860,15 +2920,17 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
                }
        }
 
+#if !defined(NDEBUG)
        /* print call trace */
-#if ! defined(NDEBGUU)
+
        if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
                emit_verbosecall_exit(jd);
        }
 #endif
+
        /* remove native stackframe info */
 
-       M_AADD_IMM(REG_SP, cd->stackframesize * 8, rd->argintregs[0]);
+       M_AADD_IMM(REG_SP, cd->stackframesize * 8, REG_A0);
        disp = dseg_add_functionptr(cd, codegen_finish_native_call);
        M_ALD(REG_ITMP1, REG_PV, disp);
        M_ALD(REG_ITMP1, REG_ITMP1, 0); /* XXX what about TOC? */
@@ -2913,89 +2975,7 @@ u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
 
        /* generate patcher stub call code */
 
-       {
-               patchref *pref;
-               u4        mcode;
-               u1       *savedmcodeptr;
-               u1       *tmpmcodeptr;
-
-               for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
-                       /* Get machine code which is patched back in later. The
-                          call is 1 instruction word long. */
-
-                       tmpmcodeptr = cd->mcodebase + pref->branchpos;
-
-                       mcode = *((u4 *) tmpmcodeptr);
-
-                       /* Patch in the call to call the following code (done at
-                          compile time). */
-
-                       savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
-                       cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
-
-                       disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
-                       M_BL(disp);
-
-                       cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
-
-                       /* create stack frame - keep stack 16-byte aligned */
-
-                       M_AADD_IMM(REG_SP, -8 * 8, REG_SP);
-
-                       /* move return address onto stack */
-
-                       M_MFLR(REG_ZERO);
-                       M_AST(REG_ZERO, REG_SP, 5 * 8);
-
-                       /* move pointer to java_objectheader onto stack */
-
-#if defined(ENABLE_THREADS)
-                       /* order reversed because of data segment layout */
-
-                       (void) dseg_add_unique_address(cd, NULL);                         /* flcword    */
-                       (void) dseg_add_unique_address(cd, lock_get_initial_lock_word()); /* monitorPtr */
-                       disp = dseg_add_unique_address(cd, NULL);                         /* vftbl      */
-
-                       M_LDA(REG_ITMP3, REG_PV, disp);
-                       M_AST(REG_ITMP3, REG_SP, 4 * 8);
-#else
-                       /* do nothing */
-#endif
-
-                       /* move machine code onto stack */
-
-                       disp = dseg_add_unique_s4(cd, mcode);
-                       M_ILD(REG_ITMP3, REG_PV, disp);
-                       M_IST(REG_ITMP3, REG_SP, 3 * 8);
-
-                       /* move class/method/field reference onto stack */
-
-                       disp = dseg_add_unique_address(cd, pref->ref);
-                       M_ALD(REG_ITMP3, REG_PV, disp);
-                       M_AST(REG_ITMP3, REG_SP, 2 * 8);
-
-                       /* move data segment displacement onto stack */
-
-                       disp = dseg_add_unique_s4(cd, pref->disp);
-                       M_ILD(REG_ITMP3, REG_PV, disp);
-                       M_IST(REG_ITMP3, REG_SP, 1 * 8);
-
-                       /* move patcher function pointer onto stack */
-
-                       disp = dseg_add_functionptr(cd, pref->patcher);
-                       M_ALD(REG_ITMP3, REG_PV, disp);
-                       M_AST(REG_ITMP3, REG_SP, 0 * 8);
-
-                       disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
-                       M_ALD(REG_ITMP3, REG_PV, disp);
-                       M_MTCTR(REG_ITMP3);
-                       M_RTS;
-               }
-       }
-
-       codegen_finish(jd);
-
-       return code->entrypoint;
+       emit_patcher_stubs(jd);
 }