* Removed all Id tags.
[cacao.git] / src / vm / jit / s390 / codegen.c
index 961eb1ff5afdd512ef36adf26ece63c0b4c251cb..c9b4cfbad66c06c2f300337c72fa86741c59ee43 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: codegen.c 7845 2007-05-01 16:05:07Z pm $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 #include <stdio.h>
 
-#include "vm/types.h"
-
-#include "md-abi.h"
-
-#include "vm/jit/s390/arch.h"
-#include "vm/jit/s390/codegen.h"
-#include "vm/jit/s390/emit.h"
-
-#include "mm/memory.h"
 #include "native/jni.h"
+#include "native/localref.h"
 #include "native/native.h"
 
+#include "mm/memory.h"
+
 #if defined(ENABLE_THREADS)
+# include "threads/lock-common.h"
 # include "threads/native/lock.h"
 #endif
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
-#include "vm/global.h"
 #include "vmcore/loader.h"
 #include "vmcore/options.h"
 #include "vmcore/statistics.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/builtin.h"
+#include "vm/exceptions.h"
+#include "vm/global.h"
+#include "vm/jit/abi.h"
+#if defined(ENABLE_LSRA)
+# include "vm/jit/allocator/lsra.h"
+#endif
 #include "vm/jit/asmpart.h"
 #include "vm/jit/codegen-common.h"
 #include "vm/jit/dseg.h"
 #include "vm/jit/jit.h"
 #include "vm/jit/methodheader.h"
 #include "vm/jit/parse.h"
-#include "vm/jit/patcher.h"
+#include "vm/jit/patcher-common.h"
 #include "vm/jit/reg.h"
 #include "vm/jit/replace.h"
+#include "vm/jit/s390/arch.h"
+#include "vm/jit/s390/codegen.h"
+#include "vm/jit/s390/emit.h"
+#include "vm/jit/s390/md-abi.h"
 #include "vm/jit/stacktrace.h"
-#include "vm/jit/abi.h"
-#include "vm/jit/emit-common.h"
+#include "vm/types.h"
+#include "vm/stringlocal.h"
+#include "vm/vm.h"
 
-#if defined(ENABLE_LSRA)
-# include "vm/jit/allocator/lsra.h"
-#endif
+/* DO__LOG generates a call to do__log. No registers are destroyed,
+ * so you may use it anywhere. regs is an array containing all general
+ * purpose registers.
+ */
+
+static void do__log(u4 *regs) {
+       /* insert stuff here */
+}
 
-#define OOPS() assert(0);
+#define DO__LOG \
+       N_AHI(REG_SP, -200); \
+       N_STM(R0, R15, 96, REG_SP); \
+       M_ALD_DSEG(R14, dseg_add_address(cd, &do__log)); \
+       N_LA(R2, 96, RN, REG_SP); \
+       N_BASR(R14, R14); \
+       N_LM(R0, R15, 96, REG_SP); \
+       N_AHI(REG_SP, 200);
 
-void panic() { }
+#define SUPPORT_HERCULES 1
 
 /* codegen *********************************************************************
 
@@ -84,6 +97,30 @@ void panic() { }
 
 *******************************************************************************/
 
+/*
+
+Layout of stackframe:
+
+Meaning                                Offset
+===============================================================================
+return_address                         (stackframesize - 1) * 8 
+saved_int_reg[INT_SAV_CNT - 1]         (stackframseize - 2) * 8
+...
+saved_int_reg[rd->savintreguse]  
+saved_flt_reg[FLT_SAV_CNT - 1]
+...
+saved_flt_reg[rd->savfltreguse]        (stackframesize - 1 - savedregs_num) * 8
+
+return_value_tmp                       (rd->memuse + 1) * 8
+monitorenter_argument                  (rd->memuse) * 8 
+???
+local[rd->memuse - 1]                  (rd->memuse - 1) * 8
+....
+local[2]                               2 * 8
+local[1]                               1 * 8
+local[0]                               0 * 8
+
+*/
 
 bool codegen_emit(jitdata *jd)
 {
@@ -135,7 +172,7 @@ bool codegen_emit(jitdata *jd)
        /* space to save used callee saved registers */
 
        savedregs_num += (INT_SAV_CNT - rd->savintreguse);
-       savedregs_num += (FLT_SAV_CNT - rd->savfltreguse) * 2;
+       savedregs_num += (FLT_SAV_CNT - rd->savfltreguse);
 
        cd->stackframesize = rd->memuse + savedregs_num + 1  /* space to save RA */;
 
@@ -150,12 +187,9 @@ bool codegen_emit(jitdata *jd)
           since both values reside in R2. */
 
        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
-               /* reserve 2 slots for long/double return values for monitorexit */
-
-               if (IS_2_WORD_TYPE(m->parseddesc->returntype.type))
-                       cd->stackframesize += 3;
-               else
-                       cd->stackframesize += 2;
+               /* 1 slot space to save argument of monitor_enter */
+               /* 1 slot to temporary store return value before monitor_exit */
+               cd->stackframesize += 2;
        }
 #endif
 
@@ -165,12 +199,12 @@ bool codegen_emit(jitdata *jd)
 
        if (!jd->isleafmethod || opt_verbosecall )
                /* TODO really 16 bytes ? */
-               cd->stackframesize = (cd->stackframesize + 3) & ~3;
+               cd->stackframesize = (cd->stackframesize + 2) & ~2;
 
        /* create method header */
 
        (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
-       (void) dseg_add_unique_s4(cd, cd->stackframesize * 4); /* FrameSize       */
+       (void) dseg_add_unique_s4(cd, cd->stackframesize * 8); /* FrameSize       */
 
 #if defined(ENABLE_THREADS)
        /* IsSync contains the offset relative to the stack pointer for the
@@ -180,10 +214,9 @@ bool codegen_emit(jitdata *jd)
        */
 
        if (checksync && (m->flags & ACC_SYNCHRONIZED))
-               (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 4); /* IsSync        */
+               (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 8); /* IsSync        */
        else
 #endif
-
                (void) dseg_add_unique_s4(cd, 0);                    /* IsSync          */
 
        (void) dseg_add_unique_s4(cd, jd->isleafmethod);               /* IsLeaf  */
@@ -202,37 +235,40 @@ bool codegen_emit(jitdata *jd)
                dseg_add_target(cd, ex->handler);
                (void) dseg_add_unique_address(cd, ex->catchtype.any);
        }
-       
+
+       /* Offset PV */
+
+       M_AADD_IMM(N_PV_OFFSET, REG_PV);
+
        /* generate method profiling code */
 
 #if defined(ENABLE_PROFILING)
        if (JITDATA_HAS_FLAG_INSTRUMENT(jd)) {
                /* count frequency */
-
-               M_ALD(REG_ITMP1, REG_PV, CodeinfoPointer);
-               M_ILD(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
-               M_IADD_IMM(1, REG_ITMP2);
+               M_ALD_DSEG(REG_ITMP1, CodeinfoPointer);
+               ICONST(REG_ITMP2, 1);
+               N_AL(REG_ITMP2, OFFSET(codeinfo, frequency), RN, REG_ITMP1);
                M_IST(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
 
-/*             PROFILE_CYCLE_START; */
+               PROFILE_CYCLE_START;
        }
 #endif
 
        /* create stack frame (if necessary) */
 
        if (cd->stackframesize)
-               M_ASUB_IMM(cd->stackframesize * 4, REG_SP);
+               M_ASUB_IMM(cd->stackframesize * 8, REG_SP);
 
        /* save used callee saved registers and return address */
 
        p = cd->stackframesize;
-       p--; M_AST(REG_RA, REG_SP, p * 4);
+       p--; M_AST(REG_RA, REG_SP, p * 8);
 
        for (i = INT_SAV_CNT - 1; i >= rd->savintreguse; i--) {
-               p--; M_IST(rd->savintregs[i], REG_SP, p * 4);
+               p--; M_IST(rd->savintregs[i], REG_SP, p * 8);
        }
        for (i = FLT_SAV_CNT - 1; i >= rd->savfltreguse; i--) {
-               p -= 2; M_DST(rd->savfltregs[i], REG_SP, p * 4);
+               p--; M_DST(rd->savfltregs[i], REG_SP, p * 8);
        }
 
        /* take arguments out of register or stack frame */
@@ -244,8 +280,9 @@ bool codegen_emit(jitdata *jd)
                varindex = jd->local_map[l * 5 + t];
 
                l++;
-               if (IS_2_WORD_TYPE(t))    /* increment local counter for 2 word types */
-                       l++;
+
+               if (IS_2_WORD_TYPE(t))
+                       l++;
 
                if (varindex == UNUSED)
                        continue;
@@ -253,6 +290,7 @@ bool codegen_emit(jitdata *jd)
                var = VAR(varindex);
 
                s1 = md->params[p].regoff;
+
                if (IS_INT_LNG_TYPE(t)) {                    /* integer args          */
                        if (IS_2_WORD_TYPE(t)) {
                                s2 = PACK_REGS(
@@ -264,32 +302,28 @@ bool codegen_emit(jitdata *jd)
                        }
                        if (!md->params[p].inmemory) {           /* register arguments    */
                                if (!IS_INMEMORY(var->flags)) {      /* reg arg -> register   */
-                                       if (IS_2_WORD_TYPE(t))
+                                       if (IS_2_WORD_TYPE(t)) {
                                                M_LNGMOVE(s2, var->vv.regoff);
-                                       else
+                                       } else {
                                                M_INTMOVE(s2, var->vv.regoff);
-
+                                       }
                                } else {                             /* reg arg -> spilled    */
-                                       if (IS_2_WORD_TYPE(t))
-                                               M_LST(s2, REG_SP, var->vv.regoff * 4);
-                                       else
-                                               M_IST(s2, REG_SP, var->vv.regoff * 4);
+                                       if (IS_2_WORD_TYPE(t)) {
+                                               M_LST(s2, REG_SP, var->vv.regoff);
+                                       } else {
+                                               M_IST(s2, REG_SP, var->vv.regoff);
+                                       }
                                }
 
                        } else {                                 /* stack arguments       */
                                if (!IS_INMEMORY(var->flags)) {      /* stack arg -> register */
-                                       if (IS_2_WORD_TYPE(t))
-                                               M_LLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 4);
-                                       else
-                                               M_ILD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 4);
-
-                               } else {                             /* stack arg -> spilled  */
-                                       M_ILD(REG_ITMP1, REG_SP, (cd->stackframesize + s1) * 4);
-                                       M_IST(REG_ITMP1, REG_SP, var->vv.regoff * 4);
                                        if (IS_2_WORD_TYPE(t)) {
-                                               M_ILD(REG_ITMP1, REG_SP, (cd->stackframesize + s1) * 4 +4);
-                                               M_IST(REG_ITMP1, REG_SP, var->vv.regoff * 4 + 4);
+                                               M_LLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
+                                       } else {
+                                               M_ILD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
                                        }
+                               } else {                             /* stack arg -> spilled  */
+                                       N_MVC(var->vv.regoff, 8, REG_SP, cd->stackframesize * 8 + s1, REG_SP);
                                }
                        }
 
@@ -301,29 +335,22 @@ bool codegen_emit(jitdata *jd)
 
                                } else {                                         /* reg arg -> spilled    */
                                        if (IS_2_WORD_TYPE(t))
-                                               M_DST(s2, REG_SP, var->vv.regoff * 4);
+                                               M_DST(s2, REG_SP, var->vv.regoff);
                                        else
-                                               M_FST(s2, REG_SP, var->vv.regoff * 4);
+                                               M_FST(s2, REG_SP, var->vv.regoff);
                                }
 
                        } else {                                 /* stack arguments       */
                                if (!IS_INMEMORY(var->flags)) {      /* stack-arg -> register */
                                        if (IS_2_WORD_TYPE(t))
-                                               M_DLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 4);
+                                               M_DLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
 
                                        else
-                                               M_FLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 4);
+                                               M_FLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
 
                                } else {                             /* stack-arg -> spilled  */
-                                       if (IS_2_WORD_TYPE(t)) {
-                                               M_DLD(REG_FTMP1, REG_SP, (cd->stackframesize + s1) * 4);
-                                               M_DST(REG_FTMP1, REG_SP, var->vv.regoff * 4);
-                                               var->vv.regoff = cd->stackframesize + s1;
-
-                                       } else {
-                                               M_FLD(REG_FTMP1, REG_SP, (cd->stackframesize + s1) * 4);
-                                               M_FST(REG_FTMP1, REG_SP, var->vv.regoff * 4);
-                                       }
+                                       N_MVC(var->vv.regoff, 8, REG_SP, cd->stackframesize * 8 + s1, REG_SP);
+                                       var->vv.regoff = cd->stackframesize * 8 + s1;
                                }
                        }
                }
@@ -345,9 +372,9 @@ bool codegen_emit(jitdata *jd)
                                M_IST(abi_registers_integer_argument[p], REG_SP, p * 8);
 
                        for (p = 0; p < FLT_ARG_CNT; p++)
-                               M_FST(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
+                               M_DST(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
 
-                       s1 += ((INT_ARG_CNT + FLT_ARG_CNT) * 2);
+                       s1 += ((INT_ARG_CNT + FLT_ARG_CNT));
                }
 #endif
 
@@ -355,7 +382,7 @@ bool codegen_emit(jitdata *jd)
 
                if (m->flags & ACC_STATIC) {
                        disp = dseg_add_address(cd, &m->class->object.header);
-                       M_ALD(REG_A0, REG_PV, disp);
+                       M_ALD_DSEG(REG_A0, disp);
                }
                else {
                        M_TEST(REG_A0);
@@ -364,9 +391,9 @@ bool codegen_emit(jitdata *jd)
                }
 
                disp = dseg_add_functionptr(cd, LOCK_monitor_enter);
-               M_ALD(REG_ITMP3, REG_PV, disp);
+               M_ALD_DSEG(REG_ITMP3, disp);
 
-               M_AST(REG_A0, REG_SP, s1 * 4);
+               M_AST(REG_A0, REG_SP, s1 * 8);
 
                M_ASUB_IMM(96, REG_SP); 
                M_CALL(REG_ITMP3);
@@ -378,7 +405,7 @@ bool codegen_emit(jitdata *jd)
                                M_ILD(abi_registers_integer_argument[p], REG_SP, p * 8);
 
                        for (p = 0; p < FLT_ARG_CNT; p++)
-                               M_FLD(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
+                               M_DLD(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
 
                        M_AADD_IMM((INT_ARG_CNT + FLT_ARG_CNT) * 8, REG_SP);
                }
@@ -394,9 +421,10 @@ bool codegen_emit(jitdata *jd)
        }
 
        /* end of header generation */
-#if 0
-       replacementpoint = jd->code->rplpoints;
-#endif
+
+       /* create replacement points */
+
+       REPLACEMENT_POINTS_INIT(cd, jd);
 
        /* walk through all basic blocks */
 
@@ -412,35 +440,31 @@ bool codegen_emit(jitdata *jd)
 
                /* handle replacement points */
 
-#if 0
-               if (bptr->bitflags & BBFLAG_REPLACEMENT) {
-                       replacementpoint->pc = (u1*)(ptrint)bptr->mpc; /* will be resolved later */
-                       
-                       replacementpoint++;
-
-                       assert(cd->lastmcodeptr <= cd->mcodeptr);
-                       cd->lastmcodeptr = cd->mcodeptr + 5; /* 5 byte jmp patch */
-               }
-#endif
+               REPLACEMENT_POINT_BLOCK_START(cd, bptr);
 
                /* copy interface registers to their destination */
 
                len = bptr->indepth;
                MCODECHECK(512);
 
+#if defined(ENABLE_PROFILING)
                /* generate basicblock profiling code */
 
                if (JITDATA_HAS_FLAG_INSTRUMENT(jd)) {
                        /* count frequency */
 
-                       M_MOV_IMM(code->bbfrequency, REG_ITMP3);
-                       M_IINC_MEMBASE(REG_ITMP3, bptr->nr * 4);
+                       M_ALD_DSEG(REG_ITMP1, CodeinfoPointer);
+                       M_ALD(REG_ITMP1, REG_ITMP1, OFFSET(codeinfo, bbfrequency));
+                       ICONST(REG_ITMP2, 1);
+                       N_AL(REG_ITMP2, bptr->nr * 4, RN, REG_ITMP1);
+                       M_IST(REG_ITMP2, REG_ITMP1, bptr->nr * 4);
 
                        /* if this is an exception handler, start profiling again */
 
                        if (bptr->type == BBTYPE_EXH)
                                PROFILE_CYCLE_START;
                }
+#endif
 
 #if defined(ENABLE_LSRA)
                if (opt_lsra) {
@@ -497,8 +521,24 @@ bool codegen_emit(jitdata *jd)
                case ICMD_NOP:        /* ...  ==> ...                                 */
                case ICMD_POP:        /* ..., value  ==> ...                          */
                case ICMD_POP2:       /* ..., value, value  ==> ...                   */
-               case ICMD_INLINE_START: /* internal ICMDs                         */
+                       break;
+
+               case ICMD_INLINE_START:
+
+                       REPLACEMENT_POINT_INLINE_START(cd, iptr);
+                       break;
+
+               case ICMD_INLINE_BODY:
+
+                       REPLACEMENT_POINT_INLINE_BODY(cd, iptr);
+                       dseg_addlinenumber_inline_start(cd, iptr);
+                       dseg_addlinenumber(cd, iptr->line);
+                       break;
+
                case ICMD_INLINE_END:
+
+                       dseg_addlinenumber_inline_end(cd, iptr);
+                       dseg_addlinenumber(cd, iptr->line);
                        break;
 
                case ICMD_CHECKNULL:  /* ..., objectref  ==> ..., objectref           */
@@ -524,14 +564,14 @@ bool codegen_emit(jitdata *jd)
                case ICMD_FCONST:     /* ...  ==> ..., constant                       */
                        d = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
                        disp = dseg_add_float(cd, iptr->sx.val.f);
-                       M_FLDN(d, REG_PV, disp, REG_ITMP1);
+                       M_FLD_DSEG(d, disp, REG_ITMP1);
                        emit_store_dst(jd, iptr, d);
                        break;
                
                case ICMD_DCONST:     /* ...  ==> ..., constant                       */
                        d = codegen_reg_of_dst(jd, iptr, REG_FTMP1);
                        disp = dseg_add_double(cd, iptr->sx.val.d);
-                       M_DLDN(d, REG_PV, disp, REG_ITMP1);
+                       M_DLD_DSEG(d, disp, REG_ITMP1);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -544,18 +584,28 @@ bool codegen_emit(jitdata *jd)
 
 /*                             PROFILE_CYCLE_STOP; */
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_classinfo,
+                               patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_classinfo,
                                                                          cr, disp);
 
 /*                             PROFILE_CYCLE_START; */
 
-                               M_ALD(d, REG_PV, disp);
+                               M_ALD_DSEG(d, disp);
                        } else {
                                if (iptr->sx.val.anyptr == 0) {
                                        M_CLR(d);
                                } else {
                                        disp = dseg_add_unique_address(cd, iptr->sx.val.anyptr);
-                                       M_ALD(d, REG_PV, disp);
+                                       M_ALD_DSEG(d, disp);
+                                       /*
+                                       if (((u4)(iptr->sx.val.anyptr) & 0x00008000) == 0) {
+                                               N_LHI(d, ((u4)(iptr->sx.val.anyptr) >> 16) & 0xFFFF);
+                                               M_SLL_IMM(16, d);
+                                               N_AHI(d, (u4)(iptr->sx.val.anyptr) & 0xFFFF);
+                                       } else {
+                                               disp = dseg_add_unique_address(cd, iptr->sx.val.anyptr);
+                                               M_ALD_DSEG(d, disp);
+                                       }
+                                       */
                                }
                        }
                        emit_store_dst(jd, iptr, d);
@@ -879,7 +929,7 @@ bool codegen_emit(jitdata *jd)
                                M_IMUL_IMM(iptr->sx.val.i, d);
                        } else {
                                disp = dseg_add_s4(cd, iptr->sx.val.i);
-                               M_ILD(REG_ITMP2, REG_PV, disp);
+                               M_ILD_DSEG(REG_ITMP2, disp);
                                M_IMUL(REG_ITMP2, d);   
                        }
                        emit_store_dst(jd, iptr, d);
@@ -958,7 +1008,7 @@ bool codegen_emit(jitdata *jd)
                        /* call builtin */
 
                        M_ASUB_IMM(96, REG_SP);
-                       M_ALD(REG_ITMP3, REG_PV, disp);
+                       M_ALD_DSEG(REG_ITMP3, disp);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_AADD_IMM(96, REG_SP);
 
@@ -1028,7 +1078,7 @@ bool codegen_emit(jitdata *jd)
                                        M_IADD(REG_ITMP1, d);
                                }
 
-                               *(u4 *)ref |= (u4)(cd->mcodeptr - ref) / 2;
+                               N_BRC_BACK_PATCH(ref);
 
                                M_SRA_IMM(iptr->sx.val.i, d);
 
@@ -1511,28 +1561,99 @@ bool codegen_emit(jitdata *jd)
                        break;
 
                case ICMD_F2I:       /* ..., value  ==> ..., (int) value              */
-                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
-                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
-                       M_CVTFI(s1, d);
-                       emit_store_dst(jd, iptr, d);
-                       /* TODO: corner cases ? */
-                       break;
+               case ICMD_D2I:
+                       {
+                               u1 *ref1;
+#ifdef SUPPORT_HERCULES
+                               u1 *ref2, *ref3;
+#endif
 
-               case ICMD_D2I:       /* ..., value  ==> ..., (int) value              */
-                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
-                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
-                       M_CVTDI(s1, d);
-                       emit_store_dst(jd, iptr, d);
-                       /* TODO: corner cases ? */
-                       break;
+                               s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                               d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
+
+                               /* Test if NAN */
+
+                               switch (iptr->opc) {
+                                       case ICMD_F2I:
+                                               N_LTEBR(s1, s1); 
+                                               break;
+                                       case ICMD_D2I:
+                                               N_LTDBR(s1, s1);
+                                               break;
+                               }
+
+                               N_BRC(DD_0 | DD_1 | DD_2, SZ_BRC + SZ_LHI + SZ_BRC); /* Non a NaN */
+                               N_LHI(d, 0); /* Load 0 */
+                               ref1 = cd->mcodeptr;
+                               N_BRC(DD_ANY, 0); /* Exit */
 
+                               /* Convert */
+
+                               switch (iptr->opc) {
+                                       case ICMD_F2I:
+                                               M_CVTFI(s1, d); 
+                                               break;
+                                       case ICMD_D2I:
+                                               M_CVTDI(s1, d); 
+                                               break;
+                               }
+
+#ifdef SUPPORT_HERCULES
+                               /* Hercules does the conversion using a plain C conversion.
+                                * According to manual, real hardware should *NOT* require this.
+                                *
+                                * Corner case: Positive float leads to INT_MIN (overflow).
+                                */
+
+                               switch (iptr->opc) {
+                                       case ICMD_F2I:
+                                               N_LTEBR(s1, s1); 
+                                               break;
+                                       case ICMD_D2I:
+                                               N_LTDBR(s1, s1);
+                                               break;
+                               }
+
+                               ref2 = cd->mcodeptr;
+                               N_BRC(DD_0 | DD_1 | DD_3, 0); /* If operand is positive, continue */
+
+                               M_TEST(d);
+
+                               ref3 = cd->mcodeptr;
+                               M_BGE(0); /* If integer result is negative, continue */
+
+                               disp = dseg_add_s4(cd, 0x7fffffff); /* Load INT_MAX */
+                               M_ILD_DSEG(d, disp);
+#endif
+                               N_BRC_BACK_PATCH(ref1);
+#ifdef SUPPORT_HERCULES
+                               N_BRC_BACK_PATCH(ref2);
+                               N_BRC_BACK_PATCH(ref3);
+#endif
+                               emit_store_dst(jd, iptr, d);
+                       }
                        break;
 
                case ICMD_F2D:       /* ..., value  ==> ..., (double) value           */
-                       s1 = emit_load_s1(jd, iptr, REG_FTMP1);
-                       d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
-                       M_CVTFD(s1, d);
-                       emit_store_dst(jd, iptr, d);
+                       {
+                               u1 *ref;
+                               s1 = emit_load_s1(jd, iptr, REG_FTMP1);
+                               d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
+#ifdef SUPPORT_HERCULES
+                               N_LTEBR(s1, s1);
+                               ref = cd->mcodeptr;
+                               N_BRC(DD_0 | DD_1 | DD_2, 0); /* Non a NaN */
+                               disp = dseg_add_double(cd, 0.0 / 0.0);
+                               M_DLD_DSEG(d, disp, REG_ITMP1);
+                               emit_label_br(cd, BRANCH_LABEL_1);
+                               N_BRC_BACK_PATCH(ref);
+#endif
+                               M_CVTFD(s1, d);
+#ifdef SUPPORT_HERCULES
+                               emit_label(cd, BRANCH_LABEL_1);
+#endif
+                               emit_store_dst(jd, iptr, d);
+                       }
                        break;
 
                case ICMD_D2F:       /* ..., value  ==> ..., (float) value            */
@@ -1600,7 +1721,7 @@ bool codegen_emit(jitdata *jd)
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP3);
                        /* TODO softnull */
                        /* 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;
@@ -1613,7 +1734,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
 
-                       N_IC(d, OFFSET(java_bytearray, data[0]), s2, s1);
+                       N_IC(d, OFFSET(java_bytearray_t, data[0]), s2, s1);
 
                        M_SLL_IMM(24, d);
                        M_SRA_IMM(24, d);
@@ -1633,7 +1754,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(1, REG_ITMP2);
 
-                       N_LH(d, OFFSET(java_chararray, data[0]), REG_ITMP2, s1);
+                       N_LH(d, OFFSET(java_chararray_t, data[0]), REG_ITMP2, s1);
 
                        /* N_LH does sign extends, undo ! */
 
@@ -1654,7 +1775,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(1, REG_ITMP2);
 
-                       N_LH(d, OFFSET(java_shortarray, data[0]), REG_ITMP2, s1);
+                       N_LH(d, OFFSET(java_shortarray_t, data[0]), REG_ITMP2, s1);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1668,7 +1789,7 @@ bool codegen_emit(jitdata *jd)
                        
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(2, REG_ITMP2); /* scale index by 4 */
-                       N_L(d, OFFSET(java_intarray, data[0]), REG_ITMP2, s1);
+                       N_L(d, OFFSET(java_intarray_t, data[0]), REG_ITMP2, s1);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1685,8 +1806,15 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(3, REG_ITMP2); /* scale index by 8 */
 
-                       N_L(GET_HIGH_REG(d) /* evntl. itmp1 */, OFFSET(java_intarray, data[0]), REG_ITMP2, s1);
-                       N_L(GET_LOW_REG(d) /* evntl. itmp2 */, OFFSET(java_intarray, data[0]) + 4, REG_ITMP2, s1);
+                       /* We need to preserve the array address after the first load */
+
+                       if (GET_HIGH_REG(d) == s1) {
+                               M_INTMOVE(s1, REG_ITMP3);
+                               s1 = REG_ITMP3;
+                       }
+
+                       N_L(GET_HIGH_REG(d) /* evntl. itmp1 */, OFFSET(java_intarray_t, data[0]), REG_ITMP2, s1);
+                       N_L(GET_LOW_REG(d) /* evntl. itmp2 */, OFFSET(java_intarray_t, data[0]) + 4, REG_ITMP2, s1);
                        emit_store_dst(jd, iptr, d);
 
                        break;
@@ -1703,7 +1831,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(2, REG_ITMP2); /* scale index by 4 */
        
-                       N_LE(d, OFFSET(java_floatarray, data[0]), REG_ITMP2, s1);
+                       N_LE(d, OFFSET(java_floatarray_t, data[0]), REG_ITMP2, s1);
 
                        emit_store_dst(jd, iptr, d);
                        break;
@@ -1719,7 +1847,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(3, REG_ITMP2); /* scale index by 8 */
        
-                       N_LD(d, OFFSET(java_floatarray, data[0]), REG_ITMP2, s1);
+                       N_LD(d, OFFSET(java_floatarray_t, data[0]), REG_ITMP2, s1);
 
                        emit_store_dst(jd, iptr, d);
                        break;
@@ -1734,7 +1862,7 @@ bool codegen_emit(jitdata *jd)
                        
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(2, REG_ITMP2); /* scale index by 4 */
-                       N_L(d, OFFSET(java_objectarray, data[0]), REG_ITMP2, s1);
+                       N_L(d, OFFSET(java_objectarray_t, data[0]), REG_ITMP2, s1);
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1745,7 +1873,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
 
-                       N_STC(s3, OFFSET(java_bytearray, data[0]), s2, s1);
+                       N_STC(s3, OFFSET(java_bytearray_t, data[0]), s2, s1);
                        break;
 
                case ICMD_CASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1759,7 +1887,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(1, REG_ITMP2);
 
-                       N_STH(s3, OFFSET(java_chararray, data[0]), REG_ITMP2, s1);
+                       N_STH(s3, OFFSET(java_chararray_t, data[0]), REG_ITMP2, s1);
 
                        break;
 
@@ -1773,7 +1901,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(1, REG_ITMP2);
 
-                       N_STH(s3, OFFSET(java_shortarray, data[0]), REG_ITMP2, s1);
+                       N_STH(s3, OFFSET(java_shortarray_t, data[0]), REG_ITMP2, s1);
                        break;
 
                case ICMD_IASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1788,7 +1916,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(2, REG_ITMP2);
 
-                       N_ST(s3, OFFSET(java_intarray, data[0]), REG_ITMP2, s1);
+                       N_ST(s3, OFFSET(java_intarray_t, data[0]), REG_ITMP2, s1);
                        break;
 
                case ICMD_LASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1802,9 +1930,9 @@ bool codegen_emit(jitdata *jd)
                        M_SLL_IMM(3, REG_ITMP2);
 
                        s3 = emit_load_s3_high(jd, iptr, REG_ITMP3);
-                       N_ST(s3, OFFSET(java_intarray, data[0]), REG_ITMP2, s1);
+                       N_ST(s3, OFFSET(java_intarray_t, data[0]), REG_ITMP2, s1);
                        s3 = emit_load_s3_low(jd, iptr, REG_ITMP3);
-                       N_ST(s3, OFFSET(java_intarray, data[0]) + 4, REG_ITMP2, s1);
+                       N_ST(s3, OFFSET(java_intarray_t, data[0]) + 4, REG_ITMP2, s1);
                        break;
 
                case ICMD_FASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1817,7 +1945,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(2, REG_ITMP2);
 
-                       N_STE(s3, OFFSET(java_floatarray, data[0]), REG_ITMP2, s1);
+                       N_STE(s3, OFFSET(java_floatarray_t, data[0]), REG_ITMP2, s1);
                        break;
 
                case ICMD_DASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1830,7 +1958,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(3, REG_ITMP2);
 
-                       N_STD(s3, OFFSET(java_doublearray, data[0]), REG_ITMP2, s1);
+                       N_STD(s3, OFFSET(java_doublearray_t, data[0]), REG_ITMP2, s1);
                        break;
 
                case ICMD_AASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1845,7 +1973,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s3, REG_A1);
 
                        disp = dseg_add_functionptr(cd, BUILTIN_canstore);
-                       M_ALD(REG_ITMP3, REG_PV, disp);
+                       M_ALD_DSEG(REG_ITMP3, disp);
                        M_ASUB_IMM(96, REG_SP);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_AADD_IMM(96, REG_SP);
@@ -1858,7 +1986,7 @@ bool codegen_emit(jitdata *jd)
 
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(2, REG_ITMP2);
-                       N_ST(s3, OFFSET(java_objectarray, data[0]), REG_ITMP2, s1);
+                       N_ST(s3, OFFSET(java_objectarray_t, data[0]), REG_ITMP2, s1);
 
                        /*
                        M_SAADDQ(s2, s1, REG_ITMP1); itmp1 := 4 * s2 + s1
@@ -1876,25 +2004,25 @@ bool codegen_emit(jitdata *jd)
 
 /*                             PROFILE_CYCLE_STOP; */
 
-                               codegen_add_patch_ref(cd, PATCHER_get_putstatic, uf, disp);
+                               patcher_add_patch_ref(jd, PATCHER_get_putstatic, uf, disp);
 
 /*                             PROFILE_CYCLE_START; */
                        }
                        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)) {
                                        PROFILE_CYCLE_STOP;
 
-                                       codegen_add_patch_ref(cd, PATCHER_clinit, fi->class, 0);
+                                       patcher_add_patch_ref(jd, PATCHER_clinit, fi->class, 0);
 
                                        PROFILE_CYCLE_START;
                                }
                        }
 
-                       M_ALD(REG_ITMP1, REG_PV, disp);
+                       M_ALD_DSEG(REG_ITMP1, disp);
 
                        switch (fieldtype) {
                        case TYPE_INT:
@@ -1902,7 +2030,7 @@ bool codegen_emit(jitdata *jd)
                                M_ILD(d, REG_ITMP1, 0);
                                break;
                        case TYPE_LNG:
-                               d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                               d = codegen_reg_of_dst(jd, iptr, REG_ITMP23_PACKED);
                                M_LLD(d, REG_ITMP1, 0);
                                break;
                        case TYPE_ADR:
@@ -1930,19 +2058,21 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = dseg_add_unique_address(cd, uf);
 
-                               codegen_addpatchref(cd, PATCHER_get_putstatic, uf, disp);
+                               patcher_add_patch_ref(jd, PATCHER_get_putstatic, uf, disp);
                        }
                        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);
+                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
+                                       PROFILE_CYCLE_STOP;
+                                       patcher_add_patch_ref(jd, PATCHER_clinit, fi->class, disp);
+                                       PROFILE_CYCLE_START;
+                               }
                        }
 
-                       M_ALD(REG_ITMP1, REG_PV, disp);
+                       M_ALD_DSEG(REG_ITMP1, disp);
                        switch (fieldtype) {
                        case TYPE_INT:
                                s1 = emit_load_s1(jd, iptr, REG_ITMP2);
@@ -1977,7 +2107,7 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = 0;
 
-                               codegen_addpatchref(cd, PATCHER_get_putfield, uf, 0);
+                               patcher_add_patch_ref(jd, PATCHER_get_putfield, uf, 0);
                        }
                        else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
@@ -1991,7 +2121,7 @@ bool codegen_emit(jitdata *jd)
                                M_ILD(d, s1, disp);
                                break;
                        case TYPE_LNG:
-                               d = codegen_reg_of_dst(jd, iptr, REG_ITMP12_PACKED);
+                               d = codegen_reg_of_dst(jd, iptr, REG_ITMP23_PACKED);
                                if (GET_HIGH_REG(d) == s1) {
                                        M_ILD(GET_LOW_REG(d), s1, disp + 4);
                                        M_ILD(GET_HIGH_REG(d), s1, disp);
@@ -2018,6 +2148,8 @@ bool codegen_emit(jitdata *jd)
                        break;
 
                case ICMD_PUTFIELD:   /* ..., objectref, value  ==> ...               */
+                       {
+                       u1 *ref;
 
                        s1 = emit_load_s1_notzero(jd, iptr, REG_ITMP1);
                        emit_nullpointer_check(cd, iptr, s1);
@@ -2033,36 +2165,53 @@ bool codegen_emit(jitdata *jd)
                                disp      = fi->offset;
                        }
 
+                       /* We can't add a patcher ref behind this load,
+                        * because the patcher would destroy REG_ITMP3.
+                        *
+                        * We pass in the disp parameter, how many bytes
+                        * to skip to the to the actual store.
+                        *
+                        * XXX this relies on patcher_add_patch_ref internals
+                        */
+
+                       if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
+                               patcher_add_patch_ref(jd, PATCHER_get_putfield, uf, 0);
+                               ref = cd->mcodeptr;
+                       }
+
+
                        if (IS_INT_LNG_TYPE(fieldtype)) {
                                if (IS_2_WORD_TYPE(fieldtype))
                                        s2 = emit_load_s2(jd, iptr, REG_ITMP23_PACKED);
                                else
                                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
-                       }
-                       else
+                       } else {
                                s2 = emit_load_s2(jd, iptr, REG_FTMP2);
+                       }
 
-                       if (INSTRUCTION_IS_UNRESOLVED(iptr))
-                               codegen_addpatchref(cd, PATCHER_get_putfield, uf, 0);
+                       if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
+                               ((patchref_t *)list_first_unsynced(jd->code->patchers))->disp = (cd->mcodeptr - ref);
+                       }
 
                        switch (fieldtype) {
-                       case TYPE_INT:
-                               M_IST(s2, s1, disp);
-                               break;
-                       case TYPE_LNG:
-                               /* TODO really order */
-                               M_IST(GET_LOW_REG(s2), s1, disp + 4);      /* keep this order */
-                               M_IST(GET_HIGH_REG(s2), s1, disp);         /* keep this order */
-                               break;
-                       case TYPE_ADR:
-                               M_AST(s2, s1, disp);
-                               break;
-                       case TYPE_FLT:
-                               M_FST(s2, s1, disp);
-                               break;
-                       case TYPE_DBL:
-                               M_DST(s2, s1, disp);
-                               break;
+                               case TYPE_INT:
+                                       M_IST(s2, s1, disp);
+                                       break;
+                               case TYPE_LNG:
+                                       M_IST(GET_LOW_REG(s2), s1, disp + 4);      /* keep this order */
+                                       M_IST(GET_HIGH_REG(s2), s1, disp);         /* keep this order */
+                                       break;
+                               case TYPE_ADR:
+                                       M_AST(s2, s1, disp);
+                                       break;
+                               case TYPE_FLT:
+                                       M_FST(s2, s1, disp);
+                                       break;
+                               case TYPE_DBL:
+                                       M_DST(s2, s1, disp);
+                                       break;
+                       }
+
                        }
                        break;
 
@@ -2070,21 +2219,21 @@ bool codegen_emit(jitdata *jd)
 
                case ICMD_ATHROW:       /* ..., objectref ==> ... (, objectref)       */
 
+                       /* PROFILE_CYCLE_STOP; */
+               
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        M_INTMOVE(s1, REG_ITMP1_XPTR);
 
-                       PROFILE_CYCLE_STOP;
-
 #ifdef ENABLE_VERIFIER
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uc = iptr->sx.s23.s2.uc;
 
-                               codegen_add_patch_ref(cd, PATCHER_athrow_areturn, uc, 0);
+                               patcher_add_patch_ref(jd, PATCHER_athrow_areturn, uc, 0);
                        }
 #endif /* ENABLE_VERIFIER */
 
                        disp = dseg_add_functionptr(cd, asm_handle_exception);
-                       M_ALD(REG_ITMP2, REG_PV, disp);
+                       M_ALD_DSEG(REG_ITMP2, disp);
                        M_JMP(REG_ITMP2_XPC, REG_ITMP2);
                        M_NOP;
 
@@ -2130,8 +2279,12 @@ bool codegen_emit(jitdata *jd)
                                M_ICMP_IMM(s1, iptr->sx.val.i);
                        else {
                                disp = dseg_add_s4(cd, iptr->sx.val.i);
-                               ICONST(REG_ITMP2, disp);
-                               N_C(s1, 0, REG_ITMP2, REG_PV);
+                               if (N_VALID_DSEG_DISP(disp)) {
+                                       N_C(s1, N_DSEG_DISP(disp), RN, REG_PV);
+                               } else {
+                                       ICONST(REG_ITMP2, disp);
+                                       N_C(s1, -N_PV_OFFSET, REG_ITMP2, REG_PV);
+                               }
                        }
 
                        switch (iptr->opc) {
@@ -2174,8 +2327,12 @@ bool codegen_emit(jitdata *jd)
                                M_ICMP_IMM(s1, iptr->sx.val.l >> 32);
                        else {
                                disp = dseg_add_s4(cd, iptr->sx.val.l >> 32);
-                               ICONST(REG_ITMP2, disp);
-                               N_C(s1, 0, REG_ITMP2, REG_PV);
+                               if (N_VALID_DSEG_DISP(disp)) {
+                                       N_C(s1, N_DSEG_DISP(disp), RN, REG_PV);
+                               } else {
+                                       ICONST(REG_ITMP2, disp);
+                                       N_C(s1, -N_PV_OFFSET, REG_ITMP2, REG_PV);
+                               }
                        }
 
                        switch(iptr->opc) {
@@ -2206,8 +2363,12 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1_low(jd, iptr, REG_ITMP1);
 
                        disp = dseg_add_s4(cd, (s4)(iptr->sx.val.l & 0xffffffff));
-                       ICONST(REG_ITMP2, disp);
-                       N_CL(s1, 0, REG_ITMP2, REG_PV);
+                       if (N_VALID_DSEG_DISP(disp)) {
+                               N_CL(s1, N_DSEG_DISP(disp), RN, REG_PV);
+                       } else {
+                               ICONST(REG_ITMP2, disp);
+                               N_CL(s1, -N_PV_OFFSET, REG_ITMP2, REG_PV);
+                       }
 
                        switch(iptr->opc) {
                                case ICMD_IF_LLT:
@@ -2395,7 +2556,9 @@ bool codegen_emit(jitdata *jd)
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                unresolved_class *uc = iptr->sx.s23.s2.uc;
 
-                               codegen_addpatchref(cd, PATCHER_athrow_areturn, uc, 0);
+                               PROFILE_CYCLE_STOP;
+                               patcher_add_patch_ref(jd, PATCHER_athrow_areturn, uc, 0);
+                               PROFILE_CYCLE_START;
                        }
 #endif /* ENABLE_VERIFIER */
                        goto nowperformreturn;
@@ -2434,29 +2597,28 @@ nowperformreturn:
 
 #if defined(ENABLE_THREADS)
                        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
-
                                /* we need to save the proper return value */
 
                                switch (iptr->opc) {
                                case ICMD_LRETURN:
-                                       M_IST(REG_RESULT2, REG_SP, rd->memuse * 4 + 8);
+                                       M_IST(REG_RESULT2, REG_SP, ((rd->memuse + 1) * 8) + 4);
                                        /* fall through */
                                case ICMD_IRETURN:
                                case ICMD_ARETURN:
-                                       M_IST(REG_RESULT , REG_SP, rd->memuse * 4 + 4);
+                                       M_IST(REG_RESULT , REG_SP, (rd->memuse + 1) * 8);
                                        break;
                                case ICMD_FRETURN:
-                                       M_FST(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_FST(REG_FRESULT, REG_SP, (rd->memuse + 1) * 8);
                                        break;
                                case ICMD_DRETURN:
-                                       M_DST(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_DST(REG_FRESULT, REG_SP, (rd->memuse + 1) * 8);
                                        break;
                                }
 
-                               M_ALD(REG_A0, REG_SP, rd->memuse * 4);
+                               M_ALD(REG_A0, REG_SP, rd->memuse * 8);
 
                                disp = dseg_add_functionptr(cd, LOCK_monitor_exit);
-                               M_ALD(REG_ITMP3, REG_PV, disp);
+                               M_ALD_DSEG(REG_ITMP3, disp);
 
                                M_ASUB_IMM(96, REG_SP);
                                M_CALL(REG_ITMP3);
@@ -2466,17 +2628,17 @@ nowperformreturn:
 
                                switch (iptr->opc) {
                                case ICMD_LRETURN:
-                                       M_ILD(REG_RESULT2, REG_SP, rd->memuse * 4 + 8);
+                                       M_ILD(REG_RESULT2, REG_SP, ((rd->memuse + 1) * 8) + 4);
                                        /* fall through */
                                case ICMD_IRETURN:
                                case ICMD_ARETURN:
-                                       M_ILD(REG_RESULT , REG_SP, rd->memuse * 4 + 4);
+                                       M_ILD(REG_RESULT , REG_SP, (rd->memuse + 1) * 8);
                                        break;
                                case ICMD_FRETURN:
-                                       M_FLD(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_FLD(REG_FRESULT, REG_SP, (rd->memuse + 1) * 8);
                                        break;
                                case ICMD_DRETURN:
-                                       M_DLD(REG_FRESULT, REG_SP, rd->memuse * 4 + 4);
+                                       M_DLD(REG_FRESULT, REG_SP, (rd->memuse + 1) * 8);
                                        break;
                                }
                        }
@@ -2484,24 +2646,27 @@ nowperformreturn:
 
                        /* restore return address                                         */
 
-                       p--; M_ALD(REG_RA, REG_SP, p * 4);
+                       p--; M_ALD(REG_RA, REG_SP, p * 8);
 
                        /* restore saved registers                                        */
 
                        for (i = INT_SAV_CNT - 1; i >= rd->savintreguse; i--) {
-                               p--; M_ILD(rd->savintregs[i], REG_SP, p * 4);
+                               p--; M_ILD(rd->savintregs[i], REG_SP, p * 8);
                        }
                        for (i = FLT_SAV_CNT - 1; i >= rd->savfltreguse; i--) {
-                               p -= 2; M_DLD(rd->savfltregs[i], REG_SP, p * 4);
+                               p--; M_DLD(rd->savfltregs[i], REG_SP, p * 8);
                        }
 
                        /* deallocate stack                                               */
 
                        if (cd->stackframesize)
-                               M_AADD_IMM(cd->stackframesize * 4, REG_SP);
+                               M_AADD_IMM(cd->stackframesize * 8, REG_SP);
+
+                       /* generate method profiling code */
+
+                       PROFILE_CYCLE_STOP;
 
                        M_RET;
-                       ALIGNCODENOP;
                        }
                        break;
 
@@ -2551,7 +2716,7 @@ nowperformreturn:
 
                        M_SLL_IMM(2, REG_ITMP1); /* scale by 4 */
                        M_ASUB_IMM(cd->dseglen, REG_ITMP1);
-                       N_L(REG_ITMP1, 0, REG_ITMP1, REG_PV);
+                       N_L(REG_ITMP1, -N_PV_OFFSET, REG_ITMP1, REG_PV);
                        M_JMP(RN, REG_ITMP1);
 
                        break;
@@ -2642,11 +2807,11 @@ gen_method:
                                        else {
                                                if (IS_2_WORD_TYPE(var->type)) {
                                                        d = emit_load(jd, iptr, var, REG_ITMP12_PACKED);
-                                                       M_LST(d, REG_SP, md->params[s3].regoff * 4);
+                                                       M_LST(d, REG_SP, md->params[s3].regoff);
                                                }
                                                else {
                                                        d = emit_load(jd, iptr, var, REG_ITMP1);
-                                                       M_IST(d, REG_SP, md->params[s3].regoff * 4);
+                                                       M_IST(d, REG_SP, md->params[s3].regoff);
                                                }
                                        }
                                }
@@ -2659,20 +2824,28 @@ gen_method:
                                        else {
                                                d = emit_load(jd, iptr, var, REG_FTMP1);
                                                if (IS_2_WORD_TYPE(var->type))
-                                                       M_DST(d, REG_SP, md->params[s3].regoff * 4);
+                                                       M_DST(d, REG_SP, md->params[s3].regoff);
                                                else
-                                                       M_FST(d, REG_SP, md->params[s3].regoff * 4);
+                                                       M_FST(d, REG_SP, md->params[s3].regoff);
                                        }
                                }
                        }
 
+                       /* generate method profiling code */
+
+                       PROFILE_CYCLE_STOP;
+
                        switch (iptr->opc) {
                        case ICMD_BUILTIN:
                                disp = dseg_add_functionptr(cd, bte->fp);
 
                                M_ASUB_IMM(96, REG_SP); /* register save area as required by C abi */   
-                               N_LHI(REG_ITMP1, disp);
-                               N_L(REG_PV, 0, REG_ITMP1, REG_PV);
+                               if (N_VALID_DSEG_DISP(disp)) {
+                                       N_L(REG_PV, N_DSEG_DISP(disp), RN, REG_PV);
+                               } else {
+                                       N_LHI(REG_ITMP1, disp);
+                                       N_L(REG_PV, -N_PV_OFFSET, REG_ITMP1, REG_PV);
+                               }
                                break;
 
                        case ICMD_INVOKESPECIAL:
@@ -2686,21 +2859,25 @@ gen_method:
                                if (lm == NULL) {
                                        disp = dseg_add_unique_address(cd, um);
 
-                                       codegen_addpatchref(cd, PATCHER_invokestatic_special,
+                                       patcher_add_patch_ref(jd, PATCHER_invokestatic_special,
                                                                                um, disp);
                                }
                                else
                                        disp = dseg_add_address(cd, lm->stubroutine);
 
-                               N_LHI(REG_ITMP1, disp);
-                               N_L(REG_PV, 0, REG_ITMP1, REG_PV);
+                               if (N_VALID_DSEG_DISP(disp)) {
+                                       N_L(REG_PV, N_DSEG_DISP(disp), RN, REG_PV);
+                               } else {
+                                       N_LHI(REG_ITMP1, disp);
+                                       N_L(REG_PV, -N_PV_OFFSET, REG_ITMP1, REG_PV);
+                               }
                                break;
 
                        case ICMD_INVOKEVIRTUAL:
                                /* TODO softnull REG_A0 */
 
                                if (lm == NULL) {
-                                       codegen_addpatchref(cd, PATCHER_invokevirtual, um, 0);
+                                       patcher_add_patch_ref(jd, PATCHER_invokevirtual, um, 0);
 
                                        s1 = 0;
                                }
@@ -2711,7 +2888,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);
                                break;
 
@@ -2722,25 +2899,23 @@ gen_method:
                                 * and -0xFFF in index register (itmp1)
                                 */
 
-                               N_LHI(REG_ITMP1, -N_DISP_MAX);
-
                                if (lm == NULL) {
-                                       codegen_addpatchref(cd, PATCHER_invokeinterface, um, 0);
+                                       patcher_add_patch_ref(jd, PATCHER_invokeinterface, um, 0);
 
                                        s1 = 0;
                                        s2 = 0;
                                }
                                else {
                                        s1 = OFFSET(vftbl_t, interfacetable[0]) -
-                                               sizeof(methodptr*) * lm->class->index +
-                                               N_DISP_MAX;
+                                               sizeof(methodptr*) * lm->class->index;
 
                                        s2 = sizeof(methodptr) * (lm - lm->class->methods);
                                }
 
                                /* Implicit null-pointer check */
-                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_objectheader, vftbl));
-                               N_L(REG_METHODPTR, s1, REG_ITMP1, REG_METHODPTR);
+                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_object_t, vftbl));
+                               N_LHI(REG_ITMP1, s1);
+                               N_L(REG_METHODPTR, 0, REG_ITMP1, REG_METHODPTR);
                                M_ALD(REG_PV, REG_METHODPTR, s2);
                                break;
                        }
@@ -2748,11 +2923,8 @@ gen_method:
                        /* generate the actual call */
 
                        M_CALL(REG_PV);
-                       REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr);
-                       N_BASR(REG_PV, RN);
-                       disp = (s4) (cd->mcodeptr - cd->mcodebase);
-                       M_ASUB_IMM32(disp, REG_ITMP1, REG_PV);
-       
+                       emit_restore_pv(cd);
+
                        /* post call finalization */
 
                        switch (iptr->opc) {
@@ -2762,6 +2934,14 @@ gen_method:
                                        break;
                        }
 
+                       /* generate method profiling code */
+
+                       PROFILE_CYCLE_START;
+
+                       /* store size of call code in replacement point */
+
+                       REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr);
+       
                        /* store return value */
 
                        d = md->returntype.type;
@@ -2828,9 +3008,9 @@ gen_method:
                                        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_notzero(jd, iptr, REG_ITMP1);
 
                                /* if class is not resolved, check which code to call */
@@ -2841,13 +3021,17 @@ gen_method:
 
                                        disp = dseg_add_unique_s4(cd, 0);         /* super->flags */
 
-                                       codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_flags,
+                                       patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_flags,
                                                                                  iptr->sx.s23.s3.c.ref,
                                                                                  disp);
 
                                        ICONST(REG_ITMP2, ACC_INTERFACE);
-                                       ICONST(REG_ITMP3, disp); /* TODO negative displacement */
-                                       N_N(REG_ITMP2, 0, REG_ITMP3, REG_PV);
+                                       if (N_VALID_DSEG_DISP(disp)) {
+                                               N_N(REG_ITMP2, N_DSEG_DISP(disp), RN, REG_PV);
+                                       } else {
+                                               ICONST(REG_ITMP3, disp);
+                                               N_N(REG_ITMP2, -N_PV_OFFSET, REG_ITMP3, REG_PV);
+                                       }
                                        emit_label_beq(cd, LABEL_CLASS);
                                }
 
@@ -2855,7 +3039,7 @@ gen_method:
 
                                if ((super == NULL) || (super->flags & ACC_INTERFACE)) {
                                        if (super == NULL) {
-                                               codegen_add_patch_ref(cd,
+                                               patcher_add_patch_ref(jd,
                                                                                          PATCHER_checkcast_instanceof_interface,
                                                                                          iptr->sx.s23.s3.c.ref,
                                                                                          0);
@@ -2864,7 +3048,7 @@ gen_method:
                                                emit_label_beq(cd, LABEL_EXIT_INTERFACE_NULL);
                                        }
 
-                                       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_ISUB_IMM(superindex, REG_ITMP3);
                                        emit_classcast_check(cd, iptr, BRANCH_LE, RN, s1);
@@ -2891,7 +3075,7 @@ gen_method:
                                        if (super == NULL) {
                                                disp = dseg_add_unique_address(cd, NULL);
 
-                                               codegen_add_patch_ref(cd,
+                                               patcher_add_patch_ref(jd,
                                                                                          PATCHER_resolve_classref_to_vftbl,
                                                                                          iptr->sx.s23.s3.c.ref,
                                                                                          disp);
@@ -2902,19 +3086,19 @@ gen_method:
                                                emit_label_beq(cd, LABEL_EXIT_CLASS_NULL);
                                        }
 
-                                       M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
-                                       M_ALD(REG_ITMP3, REG_PV, disp);
-#if defined(ENABLE_THREADS)
-                                       codegen_threadcritstart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+                                       M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
+                                       M_ALD_DSEG(REG_ITMP3, disp);
+
+                                       CODEGEN_CRITICAL_SECTION_START;
+
                                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                        M_ILD(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, baseval));
                                        M_ISUB(REG_ITMP3, REG_ITMP2);
-                                       M_ALD(REG_ITMP3, REG_PV, disp);
+                                       M_ALD_DSEG(REG_ITMP3, disp);
                                        M_ILD(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, diffval));
-#if defined(ENABLE_THREADS)
-                                       codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                                       CODEGEN_CRITICAL_SECTION_END;
+                                       
                                        M_CMPU(REG_ITMP2, REG_ITMP3); /* Unsigned compare */
                                        /* M_CMPULE(REG_ITMP2, REG_ITMP3, REG_ITMP3); itmp3 = (itmp2 <= itmp3) */
                                        /* M_BEQZ(REG_ITMP3, 0); branch if (! itmp) -> branch if > */
@@ -2948,7 +3132,7 @@ gen_method:
                                if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                        disp = dseg_add_unique_address(cd, NULL);
 
-                                       codegen_add_patch_ref(cd,
+                                       patcher_add_patch_ref(jd,
                                                                                  PATCHER_resolve_classref_to_classinfo,
                                                                                  iptr->sx.s23.s3.c.ref,
                                                                                  disp);
@@ -2956,9 +3140,9 @@ gen_method:
                                else
                                        disp = dseg_add_address(cd, iptr->sx.s23.s3.c.cls);
 
-                               M_ALD(REG_A1, REG_PV, disp);
+                               M_ALD_DSEG(REG_A1, disp);
                                disp = dseg_add_functionptr(cd, BUILTIN_arraycheckcast);
-                               M_ALD(REG_ITMP1, REG_PV, disp);
+                               M_ALD_DSEG(REG_ITMP1, disp);
                                M_ASUB_IMM(96, REG_SP);
                                M_JSR(REG_RA, REG_ITMP1);
                                M_AADD_IMM(96, REG_SP);
@@ -3016,9 +3200,9 @@ gen_method:
 #                      define LABEL_EXIT_INTERFACE_DONE BRANCH_LABEL_5
 #                      define LABEL_EXIT_CLASS_NULL BRANCH_LABEL_6
 
-#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_notzero(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        if (s1 == d) {
@@ -3036,12 +3220,18 @@ gen_method:
 
                                disp = dseg_add_unique_s4(cd, 0);             /* super->flags */
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_flags,
+                               patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_flags,
                                                                          iptr->sx.s23.s3.c.ref, disp);
 
                                ICONST(REG_ITMP2, ACC_INTERFACE);
-                               ICONST(REG_ITMP3, disp); /* TODO negative displacement */
-                               N_N(REG_ITMP2, 0, REG_ITMP3, REG_PV);
+
+                               if (N_VALID_DSEG_DISP(disp)) {
+                                       N_N(REG_ITMP2, N_DSEG_DISP(disp), RN, REG_PV);
+                               } else {
+                                       ICONST(REG_ITMP3, disp);
+                                       N_N(REG_ITMP2, -N_PV_OFFSET, REG_ITMP3, REG_PV);
+                               }
+
                                emit_label_beq(cd, LABEL_CLASS);
                        }
 
@@ -3054,7 +3244,7 @@ gen_method:
                                        if (d == REG_ITMP2)
                                                M_CLR(d);
 
-                                       codegen_add_patch_ref(cd,
+                                       patcher_add_patch_ref(jd,
                                                                                  PATCHER_checkcast_instanceof_interface,
                                                                                  iptr->sx.s23.s3.c.ref, 0);
                                }
@@ -3064,7 +3254,7 @@ gen_method:
                                        emit_label_beq(cd, LABEL_EXIT_INTERFACE_NULL);
                                }
 
-                               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_ISUB_IMM(superindex, REG_ITMP3);
 
@@ -3098,7 +3288,7 @@ gen_method:
                                if (super == NULL) {
                                        disp = dseg_add_unique_address(cd, NULL);
 
-                                       codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_vftbl,
+                                       patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_vftbl,
                                                                                  iptr->sx.s23.s3.c.ref,
                                                                                  disp);
                                }
@@ -3111,17 +3301,17 @@ gen_method:
                                        emit_label_beq(cd, LABEL_EXIT_CLASS_NULL);
                                }
 
-                               M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                               M_ALD(REG_ITMP2, REG_PV, disp);
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritstart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+                               M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
+                               M_ALD_DSEG(REG_ITMP2, disp);
+
+                               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_ISUB(REG_ITMP3, REG_ITMP1); /* itmp1 :=  itmp1 (sub.baseval) - itmp3 (super.baseval) */
 
                                M_CMPU(REG_ITMP1, REG_ITMP2); /* d := (uint)REG_ITMP1 <= (uint)REG_ITMP2 */
@@ -3178,7 +3368,7 @@ gen_method:
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                disp = dseg_add_unique_address(cd, 0);
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_classinfo,
+                               patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_classinfo,
                                                                          iptr->sx.s23.s3.c.ref,
                                                                          disp);
                        }
@@ -3191,14 +3381,14 @@ gen_method:
 
                        /* a1 = classinfo */
 
-                       M_ALD(REG_A1, REG_PV, disp);
+                       M_ALD_DSEG(REG_A1, disp);
 
                        /* a2 = pointer to dimensions = stack pointer */
 
                        M_MOV(REG_SP, REG_A2);
 
                        disp = dseg_add_functionptr(cd, BUILTIN_multianewarray);
-                       M_ALD(REG_ITMP1, REG_PV, disp);
+                       M_ALD_DSEG(REG_ITMP1, disp);
                        M_ASUB_IMM(96, REG_SP);
                        M_JSR(REG_RA, REG_ITMP1);
                        M_AADD_IMM(96, REG_SP);
@@ -3240,10 +3430,7 @@ gen_method:
 
        /* generate stubs */
 
-       emit_patcher_stubs(jd);
-#if 0
-       emit_replacement_stubs(jd);
-#endif
+       emit_patcher_traps(jd);
 
        /* everything's ok */
 
@@ -3271,11 +3458,11 @@ void codegen_emit_stub_compiler(jitdata *jd)
 
        /* don't touch ITMP3 as it cointains the return address */
 
-       M_ISUB_IMM((3 * 4), REG_PV); /* suppress negative displacements */
+       M_AADD_IMM(N_PV_OFFSET, REG_PV); /* suppress negative displacements */
 
-       M_ILD(REG_ITMP1, REG_PV, 1 * 4); /* methodinfo  */
+       M_ILD_DSEG(REG_ITMP1, -2 * SIZEOF_VOID_P); /* methodinfo  */
        /* TODO where is methodpointer loaded into itmp2? is it already inside? */
-       M_ILD(REG_PV, REG_PV, 0 * 4); /* compiler pointer */
+       M_ILD_DSEG(REG_PV, -3 * SIZEOF_VOID_P); /* compiler pointer */
        N_BR(REG_PV);
 }
 
@@ -3335,20 +3522,17 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        cd->stackframesize = 
                1 + /* r14 - return address */ +
-               sizeof(stackframeinfo) / SIZEOF_VOID_P +
-               sizeof(localref_table) / SIZEOF_VOID_P +
+               ((sizeof(stackframeinfo) + 7) / 8) +
+               ((sizeof(localref_table) + 7) / 8)  +
                1 + /* itmp3 */
-               (INT_ARG_CNT + FLT_ARG_CNT) * 2 +
+               (INT_ARG_CNT + FLT_ARG_CNT) +
                nmd->memuse + /* parameter passing */
-               96 / SIZEOF_VOID_P /* required by ABI */;
-
-       cd->stackframesize |= 0x1;                  /* keep stack 8-byte aligned */
-
+               (96 / 8)  /* required by ABI */;
 
        /* create method header */
 
        (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
-       (void) dseg_add_unique_s4(cd, cd->stackframesize * 4); /* FrameSize       */
+       (void) dseg_add_unique_s4(cd, cd->stackframesize * 8); /* FrameSize       */
        (void) dseg_add_unique_s4(cd, 0);                      /* IsSync          */
        (void) dseg_add_unique_s4(cd, 0);                      /* IsLeaf          */
        (void) dseg_add_unique_s4(cd, 0);                      /* IntSave         */
@@ -3356,23 +3540,26 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        (void) dseg_addlinenumbertablesize(cd);
        (void) dseg_add_unique_s4(cd, 0);                      /* ExTableSize     */
 
+       /* generate stub code */
+
+       N_AHI(REG_SP, -(cd->stackframesize * 8));
+       N_AHI(REG_PV, N_PV_OFFSET);
+
        /* generate native method profiling code */
-#if 0
+
+#if defined(ENABLE_PROFILING)
        if (JITDATA_HAS_FLAG_INSTRUMENT(jd)) {
                /* count frequency */
-
-               M_MOV_IMM(code, REG_ITMP3);
-               M_IINC_MEMBASE(REG_ITMP3, OFFSET(codeinfo, frequency));
+               M_ALD_DSEG(REG_ITMP1, CodeinfoPointer);
+               ICONST(REG_ITMP2, 1);
+               N_AL(REG_ITMP2, OFFSET(codeinfo, frequency), RN, REG_ITMP1);
+               M_IST(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
        }
 #endif
 
-       /* generate stub code */
-
-       N_AHI(REG_SP, -(cd->stackframesize * SIZEOF_VOID_P));
-
        /* save return address */
 
-       N_ST(R14, (cd->stackframesize - 1) * SIZEOF_VOID_P, RN, REG_SP);
+       N_ST(R14, (cd->stackframesize - 1) * 8, RN, REG_SP);
 
 #if !defined(NDEBUG)
        if (JITDATA_HAS_FLAG_VERBOSECALL(jd))
@@ -3385,12 +3572,12 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
 #if !defined(WITH_STATIC_CLASSPATH)
        if (f == NULL)
-               codegen_add_patch_ref(cd, PATCHER_resolve_native, m, disp);
+               patcher_add_patch_ref(jd, PATCHER_resolve_native_function, m, disp);
 #endif
 
-       M_ILD(REG_ITMP1, REG_PV, disp);
+       M_ILD_DSEG(REG_ITMP1, disp);
 
-       j = 96 + (nmd->memuse * 4);
+       j = 96 + (nmd->memuse * 8);
 
        /* todo some arg registers are not volatile in C-abi terms */
 
@@ -3425,19 +3612,19 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        /* create dynamic stack info */
 
-       N_LAE(REG_A0, (cd->stackframesize - 1) * 4, RN, REG_SP); /* datasp */
-       N_LR(REG_A1, REG_PV); /* pv */
-       N_LAE(REG_A2, cd->stackframesize * 4, RN, REG_SP); /* old SP */
-       N_L(REG_A3, (cd->stackframesize - 1) * 4, RN, REG_SP); /* return address */
+       N_LAE(REG_A0, (cd->stackframesize - 1) * , RN, REG_SP); /* datasp */
+       N_LA(REG_A1, -N_PV_OFFSET, RN, REG_PV); /* pv */
+       N_LAE(REG_A2, cd->stackframesize * 8, RN, REG_SP); /* old SP */
+       N_L(REG_A3, (cd->stackframesize - 1) * 8, RN, REG_SP); /* return address */
 
        disp = dseg_add_functionptr(cd, codegen_start_native_call);
-       M_ILD(REG_ITMP1, REG_PV, disp);
+       M_ILD_DSEG(REG_ITMP1, disp);
 
        M_CALL(REG_ITMP1); /* call */
 
        /* restore integer and float argument registers */
 
-       j = 96 + (nmd->memuse * 4);
+       j = 96 + (nmd->memuse * 8);
 
        for (i = 0; i < md->paramcount; i++) {
                if (! md->params[i].inmemory) {
@@ -3500,24 +3687,24 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                                                N_STM(
                                                        GET_HIGH_REG(s1), 
                                                        GET_LOW_REG(s1), 
-                                                       96 + (s2 * 4), REG_SP
+                                                       96 + s2, REG_SP
                                                );
                                        } else {
                                                N_ST(
                                                        s1, 
-                                                       96 + (s2 * 4), RN, REG_SP
+                                                       96 + s2, RN, REG_SP
                                                );
                                        }
                                }
 
                        } else {
-                               s1 = cd->stackframesize + md->params[i].regoff;
+                               s1 = cd->stackframesize * 8 + md->params[i].regoff;
                                s2 = nmd->params[j].regoff;
                                
                                if (IS_2_WORD_TYPE(t)) {
-                                       N_MVC(96 + (s2 * 4), 8, REG_SP, (s1 * 4), REG_SP);
+                                       N_MVC(96 + s2, 8, REG_SP, s1, REG_SP);
                                } else {
-                                       N_MVC(96 + (s2 * 4), 4, REG_SP, (s1 * 4), REG_SP);
+                                       N_MVC(96 + s2, 4, REG_SP, s1, REG_SP);
                                }
                        }
 
@@ -3526,13 +3713,13 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                        /* registers keep unchanged.                                      */
 
                        if (md->params[i].inmemory) {
-                               s1 = cd->stackframesize + md->params[i].regoff;
+                               s1 = cd->stackframesize * 8 + md->params[i].regoff;
                                s2 = nmd->params[j].regoff;
 
                                if (IS_2_WORD_TYPE(t)) {
-                                       N_MVC(96 + (s2 * 4), 8, REG_SP, (s1 * 4), REG_SP);
+                                       N_MVC(96 + s2, 8, REG_SP, s1, REG_SP);
                                } else {
-                                       N_MVC(96 + (s2 * 4), 4, REG_SP, (s1 * 4), REG_SP);
+                                       N_MVC(96 + s2, 4, REG_SP, s1, REG_SP);
                                }
                        }
                }
@@ -3542,13 +3729,13 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        if (m->flags & ACC_STATIC) {
                disp = dseg_add_address(cd, m->class);
-               M_ILD(REG_A1, REG_PV, disp);
+               M_ILD_DSEG(REG_A1, disp);
        }
 
        /* put env into first argument register */
 
        disp = dseg_add_address(cd, _Jv_env);
-       M_ILD(REG_A0, REG_PV, disp);
+       M_ILD_DSEG(REG_A0, disp);
 
        /* do the native function call */
 
@@ -3581,9 +3768,9 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        /* remove native stackframe info */
 
-       N_LAE(REG_A0, (cd->stackframesize - 1) * 4, RN, REG_SP); /* datasp */
+       N_LAE(REG_A0, (cd->stackframesize - 1) * 8, RN, REG_SP); /* datasp */
        disp = dseg_add_functionptr(cd, codegen_finish_native_call);
-       M_ILD(REG_ITMP1, REG_PV, disp);
+       M_ALD_DSEG(REG_ITMP1, disp);
        M_CALL(REG_ITMP1);
        N_LR(REG_ITMP3, REG_RESULT);
 
@@ -3607,11 +3794,11 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
        /* load return address */
        
-       N_L(REG_ITMP2, (cd->stackframesize - 1) * 4, RN, REG_SP);
+       N_L(REG_ITMP2, (cd->stackframesize - 1) * 8, RN, REG_SP);
 
        /* remove stackframe */
 
-       N_AHI(REG_SP, cd->stackframesize * 4);
+       N_AHI(REG_SP, cd->stackframesize * 8);
 
        /* test for exception */
 
@@ -3633,12 +3820,12 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 #endif
 
        disp = dseg_add_functionptr(cd, asm_handle_nat_exception);
-       M_ALD(REG_ITMP3, REG_PV, disp);
+       M_ALD_DSEG(REG_ITMP3, disp);
        M_JMP(RN, REG_ITMP3);
 
        /* generate patcher stubs */
 
-       emit_patcher_stubs(jd);
+       emit_patcher_traps(jd);
 }
 
 s4 codegen_reg_of_dst_notzero(jitdata *jd, instruction *iptr, s4 tempregnum) {