GNU header update.
[cacao.git] / src / vm / jit / mips / codegen.c
index 67c6374ac60cc990f2a1eef6206704672b9c6023..6fa28e123650d32385b3f4e896307e0f1f8f581d 100644 (file)
@@ -1,10 +1,9 @@
-/* jit/mips/codegen.c - machine code generator for mips
+/* vm/jit/mips/codegen.c - machine code generator for mips
 
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   Institut f. Computersprachen, TU Wien
-   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
-   S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
-   J. Wenninger
+   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
+   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
+   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
+   Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
@@ -32,7 +31,7 @@
    This module generates MIPS machine code for a sequence of
    intermediate code commands (ICMDs).
 
-   $Id: codegen.c 1291 2004-07-09 13:20:56Z twisti $
+   $Id: codegen.c 1735 2004-12-07 14:33:27Z twisti $
 
 */
 
 #include <signal.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include "jit/mips/types.h"
-#include "main.h"
-#include "builtin.h"
-#include "asmpart.h"
-#include "jni.h"
-#include "loader.h"
-#include "tables.h"
-#include "native.h"
-#include "jit/jit.h"
-#include "jit/reg.h"
-#include "jit/mips/codegen.h"
-
-/* include independent code generation stuff */
-#include "jit/codegen.inc"
-#include "jit/reg.inc"
+
+#include "native/native.h"
+#include "vm/builtin.h"
+#include "vm/jit/asmpart.h"
+#include "vm/jit/jit.h"
+#include "vm/jit/reg.h"
+#include "vm/jit/mips/codegen.h"
+#include "vm/jit/mips/types.h"
 
 
 /* *****************************************************************************
@@ -95,7 +87,7 @@ in the documention file: calling.doc
 
 /* #define REG_END   -1        last entry in tables */
  
-int nregdescint[] = {
+static int nregdescint[] = {
        REG_RES, REG_RES, REG_RET, REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, 
        REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_TMP, REG_TMP, REG_TMP, REG_TMP, 
        REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
@@ -104,7 +96,7 @@ int nregdescint[] = {
 
 /* for use of reserved registers, see comment above */
        
-int nregdescfloat[] = {
+static int nregdescfloat[] = {
        REG_RET, REG_RES, REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
        REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_ARG, REG_ARG, REG_ARG, REG_ARG, 
        REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
@@ -114,6 +106,16 @@ int nregdescfloat[] = {
 /* for use of reserved registers, see comment above */
 
 
+/* Include independent code generation stuff -- include after register        */
+/* descriptions to avoid extern definitions.                                  */
+
+#include "vm/jit/codegen.inc"
+#include "vm/jit/reg.inc"
+#ifdef LSRA
+#include "vm/jit/lsra.inc"
+#endif
+
+
 /* NullPointerException handlers and exception handling initialisation        */
 
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
@@ -144,8 +146,7 @@ void catch_NullPointerException(int sig, int code, struct sigcontext *sigctx)
                sigaddset(&nsig, sig);
                sigprocmask(SIG_UNBLOCK, &nsig, NULL);           /* unblock signal    */
                
-               xptr = new_exception(string_java_lang_NullPointerException);
-
+               xptr = new_nullpointerexception();
 
                sigctx->sc_regs[REG_ITMP1_XPTR] = (u8) xptr;
                sigctx->sc_regs[REG_ITMP2_XPC] = sigctx->sc_pc;
@@ -175,7 +176,7 @@ void init_exceptions(void)
 
        sigemptyset(&unblockmask);
        sa.sa_flags = 0;
-       sa.sa_sigaction = catch_NullPointerException;
+       sa.sa_sigaction = (functionptr) catch_NullPointerException;
        sigemptyset(&sa.sa_mask);
 
        if (!checknull) {
@@ -208,7 +209,7 @@ void init_exceptions(void)
 
 *******************************************************************************/
 
-void codegen(methodinfo *m)
+void codegen(methodinfo *m, codegendata *cd, registerdata *rd)
 {
        s4 len, s1, s2, s3, d;
        s4 a;
@@ -219,23 +220,19 @@ void codegen(methodinfo *m)
        basicblock     *bptr;
        instruction    *iptr;
        exceptiontable *ex;
-       registerdata   *r;
 
        {
        s4 i, p, pa, t, l;
        s4 savedregs_num;
 
-       /* keep code size smaller */
-       r = m->registerdata;
-
        savedregs_num = (m->isleafmethod) ? 0 : 1;        /* space to save the RA */
 
        /* space to save used callee saved registers */
 
-       savedregs_num += (r->savintregcnt - r->maxsavintreguse);
-       savedregs_num += (r->savfltregcnt - r->maxsavfltreguse);
+       savedregs_num += (rd->savintregcnt - rd->maxsavintreguse);
+       savedregs_num += (rd->savfltregcnt - rd->maxsavfltreguse);
 
-       parentargs_base = r->maxmemuse + savedregs_num;
+       parentargs_base = rd->maxmemuse + savedregs_num;
 
 #if defined(USE_THREADS)           /* space to save argument of monitor_enter */
 
@@ -252,10 +249,10 @@ void codegen(methodinfo *m)
        /* create method header */
 
 #if POINTERSIZE == 4
-       (void) dseg_addaddress(m);                              /* Filler         */
+       (void) dseg_addaddress(cd, m);                          /* Filler         */
 #endif
-       (void) dseg_addaddress(m);                              /* MethodPointer  */
-       (void) dseg_adds4(parentargs_base * 8);                 /* FrameSize      */
+       (void) dseg_addaddress(cd, m);                          /* MethodPointer  */
+       (void) dseg_adds4(cd, parentargs_base * 8);             /* FrameSize      */
 
 #if defined(USE_THREADS)
 
@@ -266,31 +263,31 @@ void codegen(methodinfo *m)
        */
 
        if (checksync && (m->flags & ACC_SYNCHRONIZED))
-               (void) dseg_adds4((r->maxmemuse + 1) * 8);          /* IsSync         */
+               (void) dseg_adds4(cd, (rd->maxmemuse + 1) * 8);     /* IsSync         */
        else
 
 #endif
 
-               (void) dseg_adds4(0);                               /* IsSync         */
+               (void) dseg_adds4(cd, 0);                           /* IsSync         */
                                               
-       (void) dseg_adds4(m->isleafmethod);                     /* IsLeaf         */
-       (void) dseg_adds4(r->savintregcnt - r->maxsavintreguse);/* IntSave        */
-       (void) dseg_adds4(r->savfltregcnt - r->maxsavfltreguse);/* FltSave        */
-       (void) dseg_adds4(m->exceptiontablelength);             /* ExTableSize    */
+       (void) dseg_adds4(cd, m->isleafmethod);                 /* IsLeaf         */
+       (void) dseg_adds4(cd, rd->savintregcnt - rd->maxsavintreguse);/* IntSave  */
+       (void) dseg_adds4(cd, rd->savfltregcnt - rd->maxsavfltreguse);/* FltSave  */
+       (void) dseg_adds4(cd, cd->exceptiontablelength);        /* ExTableSize    */
 
        /* create exception table */
 
-       for (ex = m->exceptiontable; ex != NULL; ex = ex->down) {
-               dseg_addtarget(ex->start);
-               dseg_addtarget(ex->end);
-               dseg_addtarget(ex->handler);
-               (void) dseg_addaddress(ex->catchtype);
+       for (ex = cd->exceptiontable; ex != NULL; ex = ex->down) {
+               dseg_addtarget(cd, ex->start);
+               dseg_addtarget(cd, ex->end);
+               dseg_addtarget(cd, ex->handler);
+               (void) dseg_addaddress(cd, ex->catchtype);
        }
        
        /* initialize mcode variables */
        
-       mcodeptr = (s4 *) mcodebase;
-       mcodeend = (s4 *) (mcodebase + mcodesize);
+       mcodeptr = (s4 *) cd->mcodebase;
+       cd->mcodeend = (s4 *) (cd->mcodebase + cd->mcodesize);
        MCODECHECK(128 + m->paramcount);
 
        /* create stack frame (if necessary) */
@@ -305,11 +302,11 @@ void codegen(methodinfo *m)
        if (!m->isleafmethod) {
                p--; M_LST(REG_RA, REG_SP, 8 * p);
        }
-       for (i = r->savintregcnt - 1; i >= r->maxsavintreguse; i--) {
-               p--; M_LST(r->savintregs[i], REG_SP, 8 * p);
+       for (i = rd->savintregcnt - 1; i >= rd->maxsavintreguse; i--) {
+               p--; M_LST(rd->savintregs[i], REG_SP, 8 * p);
        }
-       for (i = r->savfltregcnt - 1; i >= r->maxsavfltreguse; i--) {
-               p--; M_DST(r->savfltregs[i], REG_SP, 8 * p);
+       for (i = rd->savfltregcnt - 1; i >= rd->maxsavfltreguse; i--) {
+               p--; M_DST(rd->savfltregs[i], REG_SP, 8 * p);
        }
 
        /* save monitorenter argument */
@@ -317,12 +314,12 @@ void codegen(methodinfo *m)
 #if defined(USE_THREADS)
        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                if (m->flags & ACC_STATIC) {
-                       p = dseg_addaddress(m->class);
+                       p = dseg_addaddress(cd, m->class);
                        M_ALD(REG_ITMP1, REG_PV, p);
-                       M_AST(REG_ITMP1, REG_SP, r->maxmemuse * 8);
+                       M_AST(REG_ITMP1, REG_SP, rd->maxmemuse * 8);
 
                } else {
-                       M_AST(r->argintregs[0], REG_SP, r->maxmemuse * 8);
+                       M_AST(rd->argintregs[0], REG_SP, rd->maxmemuse * 8);
                }
        }                       
 #endif
@@ -337,7 +334,7 @@ void codegen(methodinfo *m)
 
                /* save integer argument registers */
                for (p = 0; p < m->paramcount && p < INT_ARG_CNT; p++) {
-                       M_LST(r->argintregs[p], REG_SP,  (2 + p) * 8);
+                       M_LST(rd->argintregs[p], REG_SP,  (2 + p) * 8);
                }
 
                /* save and copy float arguments into integer registers */
@@ -346,23 +343,23 @@ void codegen(methodinfo *m)
 
                        if (IS_FLT_DBL_TYPE(t)) {
                                if (IS_2_WORD_TYPE(t)) {
-                                       M_DST(r->argfltregs[p], REG_SP, (10 + p) * 8);
-                                       M_LLD(r->argintregs[p], REG_SP, (10 + p) * 8);
+                                       M_DST(rd->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_LLD(rd->argintregs[p], REG_SP, (10 + p) * 8);
 
                                } else {
-                                       M_FST(r->argfltregs[p], REG_SP, (10 + p) * 8);
-                                       M_ILD(r->argintregs[p], REG_SP, (10 + p) * 8);
+                                       M_FST(rd->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_ILD(rd->argintregs[p], REG_SP, (10 + p) * 8);
                                }
 
                        } else {
-                               M_DST(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                               M_DST(rd->argfltregs[p], REG_SP, (10 + p) * 8);
                        }
                }
 
-               p = dseg_addaddress(m);
+               p = dseg_addaddress(cd, m);
                M_ALD(REG_ITMP1, REG_PV, p);
                M_LST(REG_ITMP1, REG_SP, 0);
-               p = dseg_addaddress((void *) builtin_trace_args);
+               p = dseg_addaddress(cd, (void *) builtin_trace_args);
                M_ALD(REG_ITMP3, REG_PV, p);
                M_JSR(REG_RA, REG_ITMP3);
                M_NOP;
@@ -370,7 +367,7 @@ void codegen(methodinfo *m)
                M_LLD(REG_RA, REG_SP, 1 * 8);
 
                for (p = 0; p < m->paramcount && p < INT_ARG_CNT; p++) {
-                       M_LLD(r->argintregs[p], REG_SP, (2 + p) * 8);
+                       M_LLD(rd->argintregs[p], REG_SP, (2 + p) * 8);
                }
 
                for (p = 0; p < m->paramcount && p < FLT_ARG_CNT; p++) {
@@ -378,14 +375,14 @@ void codegen(methodinfo *m)
 
                        if (IS_FLT_DBL_TYPE(t)) {
                                if (IS_2_WORD_TYPE(t)) {
-                                       M_DLD(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_DLD(rd->argfltregs[p], REG_SP, (10 + p) * 8);
 
                                } else {
-                                       M_FLD(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_FLD(rd->argfltregs[p], REG_SP, (10 + p) * 8);
                                }
 
                        } else {
-                               M_DLD(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                               M_DLD(rd->argfltregs[p], REG_SP, (10 + p) * 8);
                        }
                }
 
@@ -396,7 +393,7 @@ void codegen(methodinfo *m)
 
        for (p = 0, l = 0; p < m->paramcount; p++) {
                t = m->paramtypes[p];
-               var = &(r->locals[l][t]);
+               var = &(rd->locals[l][t]);
                l++;
                if (IS_2_WORD_TYPE(t))    /* increment local counter for 2 word types */
                        l++;
@@ -405,9 +402,9 @@ void codegen(methodinfo *m)
                if (IS_INT_LNG_TYPE(t)) {                    /* integer args          */
                        if (p < INT_ARG_CNT) {                   /* register arguments    */
                                if (!(var->flags & INMEMORY)) {      /* reg arg -> register   */
-                                       M_INTMOVE(r->argintregs[p], var->regoff);
+                                       M_INTMOVE(rd->argintregs[p], var->regoff);
                                } else {                             /* reg arg -> spilled    */
-                                       M_LST(r->argintregs[p], REG_SP, 8 * var->regoff);
+                                       M_LST(rd->argintregs[p], REG_SP, 8 * var->regoff);
                                }
 
                        } else {                                 /* stack arguments       */
@@ -424,10 +421,10 @@ void codegen(methodinfo *m)
                } else {                                     /* floating args         */
                        if (p < FLT_ARG_CNT) {                   /* register arguments    */
                                if (!(var->flags & INMEMORY)) {      /* reg arg -> register   */
-                                       M_TFLTMOVE(var->type, r->argfltregs[p], var->regoff);
+                                       M_TFLTMOVE(var->type, rd->argfltregs[p], var->regoff);
 
                                } else {                                         /* reg arg -> spilled    */
-                                       M_DST(r->argfltregs[p], REG_SP, var->regoff * 8);
+                                       M_DST(rd->argfltregs[p], REG_SP, var->regoff * 8);
                                }
 
                        } else {                                 /* stack arguments       */
@@ -450,11 +447,11 @@ void codegen(methodinfo *m)
                s4 disp;
                s8 func_enter = (m->flags & ACC_STATIC) ?
                        (s8) builtin_staticmonitorenter : (s8) builtin_monitorenter;
-               p = dseg_addaddress((void *) func_enter);
+               p = dseg_addaddress(cd, (void *) func_enter);
                M_ALD(REG_ITMP3, REG_PV, p);
                M_JSR(REG_RA, REG_ITMP3);
-               M_ALD(r->argintregs[0], REG_SP, r->maxmemuse * 8);
-               disp = -(s4) ((u1 *) mcodeptr - mcodebase);
+               M_ALD(rd->argintregs[0], REG_SP, rd->maxmemuse * 8);
+               disp = -(s4) ((u1 *) mcodeptr - cd->mcodebase);
                M_LDA(REG_PV, REG_RA, disp);
        }
 #endif
@@ -465,20 +462,19 @@ void codegen(methodinfo *m)
        /* walk through all basic blocks */
        for (bptr = m->basicblocks; bptr != NULL; bptr = bptr->next) {
 
-               bptr->mpc = (s4) ((u1 *) mcodeptr - mcodebase);
+               bptr->mpc = (s4) ((u1 *) mcodeptr - cd->mcodebase);
 
                if (bptr->flags >= BBREACHED) {
 
-               /* branch resolving */
-
-               {
-               branchref *brefs;
-               for (brefs = bptr->branchrefs; brefs != NULL; brefs = brefs->next) {
-                       gen_resolvebranch((u1*) mcodebase + brefs->branchpos, 
-                                         brefs->branchpos,
-                                                         bptr->mpc);
-               }
-               }
+                       /* branch resolving */
+                       {
+                               branchref *bref;
+                               for (bref = bptr->branchrefs; bref != NULL; bref = bref->next) {
+                                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                                         bref->branchpos,
+                                                                         bptr->mpc);
+                               }
+                       }
 
                /* copy interface registers to their destination */
 
@@ -488,31 +484,31 @@ void codegen(methodinfo *m)
                while (src != NULL) {
                        len--;
                        if ((len == 0) && (bptr->type != BBTYPE_STD)) {
-                               d = reg_of_var(m, src, REG_ITMP1);
+                               d = reg_of_var(rd, src, REG_ITMP1);
                                M_INTMOVE(REG_ITMP1, d);
                                store_reg_to_var_int(src, d);
 
                        } else {
-                               d = reg_of_var(m, src, REG_IFTMP);
+                               d = reg_of_var(rd, src, REG_IFTMP);
                                if ((src->varkind != STACKVAR)) {
                                        s2 = src->type;
                                        if (IS_FLT_DBL_TYPE(s2)) {
-                                               if (!(r->interfaces[len][s2].flags & INMEMORY)) {
-                                                       s1 = r->interfaces[len][s2].regoff;
+                                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                                       s1 = rd->interfaces[len][s2].regoff;
                                                        M_TFLTMOVE(s2, s1, d);
 
                                                } else {
-                                                       M_DLD(d, REG_SP, 8 * r->interfaces[len][s2].regoff);
+                                                       M_DLD(d, REG_SP, 8 * rd->interfaces[len][s2].regoff);
                                                }
                                                store_reg_to_var_flt(src, d);
 
                                        } else {
-                                               if (!(r->interfaces[len][s2].flags & INMEMORY)) {
-                                                       s1 = r->interfaces[len][s2].regoff;
+                                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                                       s1 = rd->interfaces[len][s2].regoff;
                                                        M_INTMOVE(s1,d);
 
                                                } else {
-                                                       M_LLD(d, REG_SP, 8 * r->interfaces[len][s2].regoff);
+                                                       M_LLD(d, REG_SP, 8 * rd->interfaces[len][s2].regoff);
                                                }
                                                store_reg_to_var_int(src, d);
                                        }
@@ -537,24 +533,16 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_BEQZ(s1, 0);
-                       codegen_addxnullrefs(mcodeptr);
+                       codegen_addxnullrefs(cd, mcodeptr);
                        M_NOP;
                        break;
 
                /* constant operations ************************************************/
 
-#define ICONST(r,c) if(((c)>=-32768)&&((c)<= 32767)){M_IADD_IMM(REG_ZERO,c,r);} \
-                    else if(((c)>=0)&&((c)<=0xffff)){M_OR_IMM(REG_ZERO,c,r);} \
-                    else{a=dseg_adds4(c);M_ILD(r,REG_PV,a);}
-
-#define LCONST(r,c) if(((c)>=-32768)&&((c)<= 32767)){M_LADD_IMM(REG_ZERO,c,r);} \
-                    else if(((c)>=0)&&((c)<=0xffff)){M_OR_IMM(REG_ZERO,c,r);} \
-                    else{a=dseg_adds8(c);M_LLD(r,REG_PV,a);}
-
                case ICMD_ICONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.i = constant                    */
 
-                       d = reg_of_var(m, iptr->dst, REG_ITMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        ICONST(d, iptr->val.i);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -562,7 +550,7 @@ void codegen(methodinfo *m)
                case ICMD_LCONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.l = constant                    */
 
-                       d = reg_of_var(m, iptr->dst, REG_ITMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        LCONST(d, iptr->val.l);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -570,8 +558,8 @@ void codegen(methodinfo *m)
                case ICMD_FCONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.f = constant                    */
 
-                       d = reg_of_var(m, iptr->dst, REG_FTMP1);
-                       a = dseg_addfloat(iptr->val.f);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP1);
+                       a = dseg_addfloat(cd, iptr->val.f);
                        M_FLD(d, REG_PV, a);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -579,8 +567,8 @@ void codegen(methodinfo *m)
                case ICMD_DCONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.d = constant                    */
 
-                       d = reg_of_var(m, iptr->dst, REG_FTMP1);
-                       a = dseg_adddouble(iptr->val.d);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP1);
+                       a = dseg_adddouble(cd, iptr->val.d);
                        M_DLD(d, REG_PV, a);
                        store_reg_to_var_flt (iptr->dst, d);
                        break;
@@ -588,14 +576,13 @@ void codegen(methodinfo *m)
                case ICMD_ACONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.a = constant                    */
 
-                       d = reg_of_var(m, iptr->dst, REG_ITMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        if (iptr->val.a) {
-                               a = dseg_addaddress(iptr->val.a);
+                               a = dseg_addaddress(cd, iptr->val.a);
                                M_ALD(d, REG_PV, a);
-                               }
-                       else {
+                       } else {
                                M_INTMOVE(REG_ZERO, d);
-                               }
+                       }
                        store_reg_to_var_int(iptr->dst, d);
                        break;
 
@@ -606,11 +593,11 @@ void codegen(methodinfo *m)
                case ICMD_LLOAD:      /* op1 = local variable                         */
                case ICMD_ALOAD:
 
-                       d = reg_of_var(m, iptr->dst, REG_ITMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        if ((iptr->dst->varkind == LOCALVAR) &&
                            (iptr->dst->varnum == iptr->op1))
                                break;
-                       var = &(r->locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
                        if (var->flags & INMEMORY) {
                                M_LLD(d, REG_SP, 8 * var->regoff);
                        } else {
@@ -622,11 +609,11 @@ void codegen(methodinfo *m)
                case ICMD_FLOAD:      /* ...  ==> ..., content of local variable      */
                case ICMD_DLOAD:      /* op1 = local variable                         */
 
-                       d = reg_of_var(m, iptr->dst, REG_FTMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP1);
                        if ((iptr->dst->varkind == LOCALVAR) &&
                            (iptr->dst->varnum == iptr->op1))
                                break;
-                       var = &(r->locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
                        {
                                int t2 = ((iptr->opc == ICMD_FLOAD) ? TYPE_FLT : TYPE_DBL);
                                if (var->flags & INMEMORY) {
@@ -646,7 +633,7 @@ void codegen(methodinfo *m)
                        if ((src->varkind == LOCALVAR) &&
                            (src->varnum == iptr->op1))
                                break;
-                       var = &(r->locals[iptr->op1][iptr->opc - ICMD_ISTORE]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ISTORE]);
                        if (var->flags & INMEMORY) {
                                var_to_reg_int(s1, src, REG_ITMP1);
                                M_LST(s1, REG_SP, 8 * var->regoff);
@@ -663,7 +650,7 @@ void codegen(methodinfo *m)
                        if ((src->varkind == LOCALVAR) &&
                            (src->varnum == iptr->op1))
                                break;
-                       var = &(r->locals[iptr->op1][iptr->opc - ICMD_ISTORE]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ISTORE]);
                        {
                                int t1 = ((iptr->opc == ICMD_FSTORE) ? TYPE_FLT : TYPE_DBL);
                                if (var->flags & INMEMORY) {
@@ -686,29 +673,24 @@ void codegen(methodinfo *m)
                case ICMD_POP2:       /* ..., value, value  ==> ...                   */
                        break;
 
-#define M_COPY(from,to) \
-                       d = reg_of_var(m, to, REG_IFTMP); \
-                       if ((from->regoff != to->regoff) || \
-                           ((from->flags ^ to->flags) & INMEMORY)) { \
-                               if (IS_FLT_DBL_TYPE(from->type)) { \
-                                       var_to_reg_flt(s1, from, d); \
-                                       M_TFLTMOVE(from->type,s1,d); \
-                                       store_reg_to_var_flt(to, d); \
-                                       }\
-                               else { \
-                                       var_to_reg_int(s1, from, d); \
-                                       M_INTMOVE(s1,d); \
-                                       store_reg_to_var_int(to, d); \
-                                       }\
-                               }
-
                case ICMD_DUP:        /* ..., a ==> ..., a, a                         */
                        M_COPY(src, iptr->dst);
                        break;
 
                case ICMD_DUP_X1:     /* ..., a, b ==> ..., b, a, b                   */
 
-                       M_COPY(src,       iptr->dst->prev->prev);
+                       M_COPY(src,       iptr->dst);
+                       M_COPY(src->prev, iptr->dst->prev);
+                       M_COPY(iptr->dst, iptr->dst->prev->prev);
+                       break;
+
+               case ICMD_DUP_X2:     /* ..., a, b, c ==> ..., c, a, b, c             */
+
+                       M_COPY(src,             iptr->dst);
+                       M_COPY(src->prev,       iptr->dst->prev);
+                       M_COPY(src->prev->prev, iptr->dst->prev->prev);
+                       M_COPY(iptr->dst,       iptr->dst->prev->prev->prev);
+                       break;
 
                case ICMD_DUP2:       /* ..., a, b ==> ..., a, b, a, b                */
 
@@ -718,14 +700,11 @@ void codegen(methodinfo *m)
 
                case ICMD_DUP2_X1:    /* ..., a, b, c ==> ..., b, c, a, b, c          */
 
-                       M_COPY(src->prev,       iptr->dst->prev->prev->prev);
-
-               case ICMD_DUP_X2:     /* ..., a, b, c ==> ..., c, a, b, c             */
-
                        M_COPY(src,             iptr->dst);
                        M_COPY(src->prev,       iptr->dst->prev);
                        M_COPY(src->prev->prev, iptr->dst->prev->prev);
-                       M_COPY(src, iptr->dst->prev->prev->prev);
+                       M_COPY(iptr->dst,       iptr->dst->prev->prev->prev);
+                       M_COPY(iptr->dst->prev, iptr->dst->prev->prev->prev->prev);
                        break;
 
                case ICMD_DUP2_X2:    /* ..., a, b, c, d ==> ..., c, d, a, b, c, d    */
@@ -734,13 +713,13 @@ void codegen(methodinfo *m)
                        M_COPY(src->prev,             iptr->dst->prev);
                        M_COPY(src->prev->prev,       iptr->dst->prev->prev);
                        M_COPY(src->prev->prev->prev, iptr->dst->prev->prev->prev);
-                       M_COPY(src,       iptr->dst->prev->prev->prev->prev);
-                       M_COPY(src->prev, iptr->dst->prev->prev->prev->prev->prev);
+                       M_COPY(iptr->dst,             iptr->dst->prev->prev->prev->prev);
+                       M_COPY(iptr->dst->prev,       iptr->dst->prev->prev->prev->prev->prev);
                        break;
 
                case ICMD_SWAP:       /* ..., a, b ==> ..., b, a                      */
 
-                       M_COPY(src, iptr->dst->prev);
+                       M_COPY(src,       iptr->dst->prev);
                        M_COPY(src->prev, iptr->dst);
                        break;
 
@@ -750,7 +729,7 @@ void codegen(methodinfo *m)
                case ICMD_INEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_int(s1, src, REG_ITMP1); 
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISUB(REG_ZERO, s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -758,7 +737,7 @@ void codegen(methodinfo *m)
                case ICMD_LNEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSUB(REG_ZERO, s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -766,7 +745,7 @@ void codegen(methodinfo *m)
                case ICMD_I2L:        /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_INTMOVE(s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -774,7 +753,7 @@ void codegen(methodinfo *m)
                case ICMD_L2I:        /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISLL_IMM(s1, 0, d );
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -782,7 +761,7 @@ void codegen(methodinfo *m)
                case ICMD_INT2BYTE:   /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSLL_IMM(s1, 56, d);
                        M_LSRA_IMM( d, 56, d);
                        store_reg_to_var_int(iptr->dst, d);
@@ -791,7 +770,7 @@ void codegen(methodinfo *m)
                case ICMD_INT2CHAR:   /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
             M_CZEXT(s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -799,7 +778,7 @@ void codegen(methodinfo *m)
                case ICMD_INT2SHORT:  /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSLL_IMM(s1, 48, d);
                        M_LSRA_IMM( d, 48, d);
                        store_reg_to_var_int(iptr->dst, d);
@@ -810,7 +789,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IADD(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -819,7 +798,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= -32768) && (iptr->val.i <= 32767)) {
                                M_IADD_IMM(s1, iptr->val.i, d);
                                }
@@ -834,7 +813,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LADD(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -843,7 +822,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.l >= -32768) && (iptr->val.l <= 32767)) {
                                M_LADD_IMM(s1, iptr->val.l, d);
                                }
@@ -858,7 +837,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISUB(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -867,7 +846,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= -32767) && (iptr->val.i <= 32768)) {
                                M_IADD_IMM(s1, -iptr->val.i, d);
                                }
@@ -882,7 +861,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSUB(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -891,7 +870,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.l >= -32767) && (iptr->val.l <= 32768)) {
                                M_LADD_IMM(s1, -iptr->val.l, d);
                                }
@@ -906,7 +885,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IMUL(s1, s2);
                        M_MFLO(d);
                        M_NOP;
@@ -918,7 +897,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        ICONST(REG_ITMP2, iptr->val.i);
                        M_IMUL(s1, REG_ITMP2);
                        M_MFLO(d);
@@ -931,7 +910,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LMUL(s1, s2);
                        M_MFLO(d);
                        M_NOP;
@@ -943,7 +922,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        LCONST(REG_ITMP2, iptr->val.l);
                        M_LMUL(s1, REG_ITMP2);
                        M_MFLO(d);
@@ -956,7 +935,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IDIV(s1, s2);
                        M_MFLO(d);
                        M_NOP;
@@ -968,7 +947,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        ICONST(REG_ITMP2, iptr->val.i);
                        M_IDIV(s1, REG_ITMP2);
                        M_MFLO(d);
@@ -981,7 +960,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LDIV(s1, s2);
                        M_MFLO(d);
                        M_NOP;
@@ -993,7 +972,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        LCONST(REG_ITMP2, iptr->val.l);
                        M_LDIV(s1, REG_ITMP2);
                        M_MFLO(d);
@@ -1006,7 +985,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IDIV(s1, s2);
                        M_MFHI(d);
                        M_NOP;
@@ -1018,7 +997,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        ICONST(REG_ITMP2, iptr->val.i);
                        M_IDIV(s1, REG_ITMP2);
                        M_MFHI(d);
@@ -1031,7 +1010,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LDIV(s1, s2);
                        M_MFHI(d);
                        M_NOP;
@@ -1043,7 +1022,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        LCONST(REG_ITMP2, iptr->val.l);
                        M_LDIV(s1, REG_ITMP2);
                        M_MFHI(d);
@@ -1056,7 +1035,7 @@ void codegen(methodinfo *m)
                case ICMD_LDIVPOW2:   /* val.i = constant                             */
                                      
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSRA_IMM(s1, 63, REG_ITMP2);
                        M_LSRL_IMM(REG_ITMP2, 64 - iptr->val.i, REG_ITMP2);
                        M_LADD(s1, REG_ITMP2, REG_ITMP2);
@@ -1068,7 +1047,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISLL(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1077,7 +1056,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISLL_IMM(s1, iptr->val.i, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1086,7 +1065,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISRA(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1095,7 +1074,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISRA_IMM(s1, iptr->val.i, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1104,7 +1083,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISRL(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1113,7 +1092,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_ISRL_IMM(s1, iptr->val.i, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1122,7 +1101,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSLL(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1131,7 +1110,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSLL_IMM(s1, iptr->val.i, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1140,7 +1119,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSRA(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1149,7 +1128,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSRA_IMM(s1, iptr->val.i, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1158,7 +1137,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSRL(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1167,7 +1146,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_LSRL_IMM(s1, iptr->val.i, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1177,7 +1156,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_AND(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1186,7 +1165,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= 0) && (iptr->val.i <= 0xffff)) {
                                M_AND_IMM(s1, iptr->val.i, d);
                                }
@@ -1201,7 +1180,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (s1 == d) {
                                M_MOV(s1, REG_ITMP1);
                                s1 = REG_ITMP1;
@@ -1229,7 +1208,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.l >= 0) && (iptr->val.l <= 0xffff)) {
                                M_AND_IMM(s1, iptr->val.l, d);
                                }
@@ -1244,7 +1223,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (s1 == d) {
                                M_MOV(s1, REG_ITMP1);
                                s1 = REG_ITMP1;
@@ -1273,7 +1252,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_OR( s1,s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1282,7 +1261,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= 0) && (iptr->val.i <= 0xffff)) {
                                M_OR_IMM(s1, iptr->val.i, d);
                                }
@@ -1297,7 +1276,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.l >= 0) && (iptr->val.l <= 0xffff)) {
                                M_OR_IMM(s1, iptr->val.l, d);
                                }
@@ -1313,7 +1292,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_XOR(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1322,7 +1301,7 @@ void codegen(methodinfo *m)
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= 0) && (iptr->val.i <= 0xffff)) {
                                M_XOR_IMM(s1, iptr->val.i, d);
                                }
@@ -1337,7 +1316,7 @@ void codegen(methodinfo *m)
                                      /* val.l = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.l >= 0) && (iptr->val.l <= 0xffff)) {
                                M_XOR_IMM(s1, iptr->val.l, d);
                                }
@@ -1353,7 +1332,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_CMPLT(s1, s2, REG_ITMP3);
                        M_CMPLT(s2, s1, REG_ITMP1);
                        M_LSUB (REG_ITMP1, REG_ITMP3, d);
@@ -1364,7 +1343,7 @@ void codegen(methodinfo *m)
                case ICMD_IINC:       /* ..., value  ==> ..., value + constant        */
                                      /* op1 = variable, val.i = constant             */
 
-                       var = &(r->locals[iptr->op1][TYPE_INT]);
+                       var = &(rd->locals[iptr->op1][TYPE_INT]);
                        if (var->flags & INMEMORY) {
                                s1 = REG_ITMP1;
                                M_LLD(s1, REG_SP, 8 * var->regoff);
@@ -1382,7 +1361,7 @@ void codegen(methodinfo *m)
                case ICMD_FNEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FNEG(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1390,7 +1369,7 @@ void codegen(methodinfo *m)
                case ICMD_DNEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_DNEG(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1399,7 +1378,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FADD(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1408,7 +1387,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_DADD(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1417,7 +1396,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FSUB(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1426,7 +1405,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_DSUB(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1435,7 +1414,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FMUL(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1444,7 +1423,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_DMUL(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1453,7 +1432,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FDIV(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1462,7 +1441,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_DDIV(s1, s2, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1472,7 +1451,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FDIV(s1,s2, REG_FTMP3);
                        M_FLOORFL(REG_FTMP3, REG_FTMP3);
                        M_CVTLF(REG_FTMP3, REG_FTMP3);
@@ -1485,7 +1464,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_DDIV(s1,s2, REG_FTMP3);
                        M_FLOORDL(REG_FTMP3, REG_FTMP3);
                        M_CVTLD(REG_FTMP3, REG_FTMP3);
@@ -1497,7 +1476,7 @@ void codegen(methodinfo *m)
                case ICMD_I2F:       /* ..., value  ==> ..., (float) value            */
                case ICMD_L2F:
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_MOVLD(s1, d);
                        M_CVTLF(d, d);
                        store_reg_to_var_flt(iptr->dst, d);
@@ -1506,7 +1485,7 @@ void codegen(methodinfo *m)
                case ICMD_I2D:       /* ..., value  ==> ..., (double) value           */
                case ICMD_L2D:
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_MOVLD(s1, d);
                        M_CVTLD(d, d);
                        store_reg_to_var_flt(iptr->dst, d);
@@ -1515,7 +1494,7 @@ void codegen(methodinfo *m)
                case ICMD_F2I:       /* ..., (float) value  ==> ..., (int) value      */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_TRUNCFI(s1, REG_FTMP1);
                        M_MOVDI(REG_FTMP1, d);
                        M_NOP;
@@ -1525,7 +1504,7 @@ void codegen(methodinfo *m)
                case ICMD_D2I:       /* ..., (double) value  ==> ..., (int) value     */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_TRUNCDI(s1, REG_FTMP1);
                        M_MOVDI(REG_FTMP1, d);
                        M_NOP;
@@ -1535,7 +1514,7 @@ void codegen(methodinfo *m)
                case ICMD_F2L:       /* ..., (float) value  ==> ..., (long) value     */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_TRUNCFL(s1, REG_FTMP1);
                        M_MOVDL(REG_FTMP1, d);
                        M_NOP;
@@ -1545,7 +1524,7 @@ void codegen(methodinfo *m)
                case ICMD_D2L:       /* ..., (double) value  ==> ..., (long) value    */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_TRUNCDL(s1, REG_FTMP1);
                        M_MOVDL(REG_FTMP1, d);
                        M_NOP;
@@ -1555,7 +1534,7 @@ void codegen(methodinfo *m)
                case ICMD_F2D:       /* ..., value  ==> ..., (double) value           */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_CVTFD(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1563,7 +1542,7 @@ void codegen(methodinfo *m)
                case ICMD_D2F:       /* ..., value  ==> ..., (double) value           */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_CVTDF(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1572,7 +1551,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_FCMPULEF(s1, s2);
                        M_FBT(3);
                        M_LADD_IMM(REG_ZERO, 1, d);
@@ -1588,7 +1567,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_FCMPULED(s1, s2);
                        M_FBT(3);
                        M_LADD_IMM(REG_ZERO, 1, d);
@@ -1604,7 +1583,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_FCMPOLTF(s1, s2);
                        M_FBF(3);
                        M_LSUB_IMM(REG_ZERO, 1, d);
@@ -1620,7 +1599,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_FCMPOLTD(s1, s2);
                        M_FBF(3);
                        M_LSUB_IMM(REG_ZERO, 1, d);
@@ -1635,19 +1614,10 @@ void codegen(methodinfo *m)
 
                /* memory operations **************************************************/
 
-#define gen_bound_check \
-    if (checkbounds) { \
-        M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size)); \
-        M_CMPULT(s2, REG_ITMP3, REG_ITMP3); \
-        M_BEQZ(REG_ITMP3, 0); \
-        codegen_addxboundrefs(mcodeptr, s2); \
-        M_NOP; \
-    }
-
                case ICMD_ARRAYLENGTH: /* ..., arrayref  ==> ..., length              */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        gen_nullptr_check(s1);
                        M_ILD(d, s1, OFFSET(java_arrayheader, size));
                        store_reg_to_var_int(iptr->dst, d);
@@ -1657,7 +1627,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1672,7 +1642,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1687,7 +1657,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1702,7 +1672,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1717,7 +1687,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1732,7 +1702,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1747,7 +1717,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1762,7 +1732,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1875,18 +1845,21 @@ void codegen(methodinfo *m)
                case ICMD_PUTSTATIC:  /* ..., value  ==> ...                          */
                                      /* op1 = type, val.a = field address            */
 
-                       /* if class isn't yet initialized, do it */
+                       /* If the static fields' class is not yet initialized, we do it   */
+                       /* now. The call code is generated later.                         */
                        if (!((fieldinfo *) iptr->val.a)->class->initialized) {
-                               /* call helper function which patches this code */
-                               a = dseg_addaddress(((fieldinfo *) iptr->val.a)->class);
-                               M_ALD(REG_ITMP1, REG_PV, a);
-                               a = dseg_addaddress(asm_check_clinit);
-                               M_ALD(REG_ITMP3, REG_PV, a);
-                               M_JSR(REG_RA, REG_ITMP3);
-                               M_NOP;
+                               codegen_addclinitref(cd, mcodeptr, ((fieldinfo *) iptr->val.a)->class);
+
+                               /* This is just for debugging purposes. Is very difficult to  */
+                               /* read patched code. Here we patch the following 2 nop's     */
+                               /* so that the real code keeps untouched.                     */
+                               if (showdisassemble) {
+                                       M_NOP;
+                                       M_NOP;
+                               }
                        }
 
-                       a = dseg_addaddress(&(((fieldinfo *) iptr->val.a)->value));
+                       a = dseg_addaddress(cd, &(((fieldinfo *) iptr->val.a)->value));
                        M_ALD(REG_ITMP1, REG_PV, a);
                        switch (iptr->op1) {
                        case TYPE_INT:
@@ -1909,53 +1882,62 @@ void codegen(methodinfo *m)
                                var_to_reg_flt(s2, src, REG_FTMP2);
                                M_DST(s2, REG_ITMP1, 0);
                                break;
-                       default: panic ("internal error");
+                       default:
+                               throw_cacao_exception_exit(string_java_lang_InternalError,
+                                                                                  "Unknown PUTSTATIC operand type %d",
+                                                                                  iptr->op1);
                        }
                        break;
 
                case ICMD_GETSTATIC:  /* ...  ==> ..., value                          */
                                      /* op1 = type, val.a = field address            */
 
-                       /* if class isn't yet initialized, do it */
+                       /* If the static fields' class is not yet initialized, we do it   */
+                       /* now. The call code is generated later.                         */
                        if (!((fieldinfo *) iptr->val.a)->class->initialized) {
-                               /* call helper function which patches this code */
-                               a = dseg_addaddress(((fieldinfo *) iptr->val.a)->class);
-                               M_ALD(REG_ITMP1, REG_PV, a);
-                               a = dseg_addaddress(asm_check_clinit);
-                               M_ALD(REG_ITMP3, REG_PV, a);
-                               M_JSR(REG_RA, REG_ITMP3);
-                               M_NOP;
+                               codegen_addclinitref(cd, mcodeptr, ((fieldinfo *) iptr->val.a)->class);
+
+                               /* This is just for debugging purposes. Is very difficult to  */
+                               /* read patched code. Here we patch the following 2 nop's     */
+                               /* so that the real code keeps untouched.                     */
+                               if (showdisassemble) {
+                                       M_NOP;
+                                       M_NOP;
+                               }
                        }
 
-                       a = dseg_addaddress(&(((fieldinfo *) iptr->val.a)->value));
+                       a = dseg_addaddress(cd, &(((fieldinfo *) iptr->val.a)->value));
                        M_ALD(REG_ITMP1, REG_PV, a);
                        switch (iptr->op1) {
                        case TYPE_INT:
-                               d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                               d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                                M_ILD(d, REG_ITMP1, 0);
                                store_reg_to_var_int(iptr->dst, d);
                                break;
                        case TYPE_LNG:
-                               d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                               d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                                M_LLD(d, REG_ITMP1, 0);
                                store_reg_to_var_int(iptr->dst, d);
                                break;
                        case TYPE_ADR:
-                               d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                               d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                                M_ALD(d, REG_ITMP1, 0);
                                store_reg_to_var_int(iptr->dst, d);
                                break;
                        case TYPE_FLT:
-                               d = reg_of_var(m, iptr->dst, REG_FTMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_FTMP1);
                                M_FLD(d, REG_ITMP1, 0);
                                store_reg_to_var_flt(iptr->dst, d);
                                break;
                        case TYPE_DBL:                          
-                               d = reg_of_var(m, iptr->dst, REG_FTMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_FTMP1);
                                M_DLD(d, REG_ITMP1, 0);
                                store_reg_to_var_flt(iptr->dst, d);
                                break;
-                       default: panic ("internal error");
+                       default:
+                               throw_cacao_exception_exit(string_java_lang_InternalError,
+                                                                                  "Unknown GETSTATIC operand type %d",
+                                                                                  iptr->op1);
                        }
                        break;
 
@@ -1965,38 +1947,41 @@ void codegen(methodinfo *m)
 
                        a = ((fieldinfo *)(iptr->val.a))->offset;
                        switch (iptr->op1) {
-                               case TYPE_INT:
-                                       var_to_reg_int(s1, src->prev, REG_ITMP1);
-                                       var_to_reg_int(s2, src, REG_ITMP2);
-                                       gen_nullptr_check(s1);
-                                       M_IST(s2, s1, a);
-                                       break;
-                               case TYPE_LNG:
-                                       var_to_reg_int(s1, src->prev, REG_ITMP1);
-                                       var_to_reg_int(s2, src, REG_ITMP2);
-                                       gen_nullptr_check(s1);
-                                       M_LST(s2, s1, a);
-                                       break;
-                               case TYPE_ADR:
-                                       var_to_reg_int(s1, src->prev, REG_ITMP1);
-                                       var_to_reg_int(s2, src, REG_ITMP2);
-                                       gen_nullptr_check(s1);
-                                       M_AST(s2, s1, a);
-                                       break;
-                               case TYPE_FLT:
-                                       var_to_reg_int(s1, src->prev, REG_ITMP1);
-                                       var_to_reg_flt(s2, src, REG_FTMP2);
-                                       gen_nullptr_check(s1);
-                                       M_FST(s2, s1, a);
-                                       break;
-                               case TYPE_DBL:
-                                       var_to_reg_int(s1, src->prev, REG_ITMP1);
-                                       var_to_reg_flt(s2, src, REG_FTMP2);
-                                       gen_nullptr_check(s1);
-                                       M_DST(s2, s1, a);
-                                       break;
-                               default: panic ("internal error");
-                               }
+                       case TYPE_INT:
+                               var_to_reg_int(s1, src->prev, REG_ITMP1);
+                               var_to_reg_int(s2, src, REG_ITMP2);
+                               gen_nullptr_check(s1);
+                               M_IST(s2, s1, a);
+                               break;
+                       case TYPE_LNG:
+                               var_to_reg_int(s1, src->prev, REG_ITMP1);
+                               var_to_reg_int(s2, src, REG_ITMP2);
+                               gen_nullptr_check(s1);
+                               M_LST(s2, s1, a);
+                               break;
+                       case TYPE_ADR:
+                               var_to_reg_int(s1, src->prev, REG_ITMP1);
+                               var_to_reg_int(s2, src, REG_ITMP2);
+                               gen_nullptr_check(s1);
+                               M_AST(s2, s1, a);
+                               break;
+                       case TYPE_FLT:
+                               var_to_reg_int(s1, src->prev, REG_ITMP1);
+                               var_to_reg_flt(s2, src, REG_FTMP2);
+                               gen_nullptr_check(s1);
+                               M_FST(s2, s1, a);
+                               break;
+                       case TYPE_DBL:
+                               var_to_reg_int(s1, src->prev, REG_ITMP1);
+                               var_to_reg_flt(s2, src, REG_FTMP2);
+                               gen_nullptr_check(s1);
+                               M_DST(s2, s1, a);
+                               break;
+                       default:
+                               throw_cacao_exception_exit(string_java_lang_InternalError,
+                                                                                  "Unknown PUTFIELD operand type %d",
+                                                                                  iptr->op1);
+                       }
                        break;
 
                case ICMD_GETFIELD:   /* ...  ==> ..., value                          */
@@ -2004,55 +1989,56 @@ void codegen(methodinfo *m)
 
                        a = ((fieldinfo *)(iptr->val.a))->offset;
                        switch (iptr->op1) {
-                               case TYPE_INT:
-                                       var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
-                                       gen_nullptr_check(s1);
-                                       M_ILD(d, s1, a);
-                                       store_reg_to_var_int(iptr->dst, d);
-                                       break;
-                               case TYPE_LNG:
-                                       var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
-                                       gen_nullptr_check(s1);
-                                       M_LLD(d, s1, a);
-                                       store_reg_to_var_int(iptr->dst, d);
-                                       break;
-                               case TYPE_ADR:
-                                       var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
-                                       gen_nullptr_check(s1);
-                                       M_ALD(d, s1, a);
-                                       store_reg_to_var_int(iptr->dst, d);
-                                       break;
-                               case TYPE_FLT:
-                                       var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(m, iptr->dst, REG_FTMP1);
-                                       gen_nullptr_check(s1);
-                                       M_FLD(d, s1, a);
-                                       store_reg_to_var_flt(iptr->dst, d);
-                                       break;
-                               case TYPE_DBL:                          
-                                       var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(m, iptr->dst, REG_FTMP1);
-                                       gen_nullptr_check(s1);
-                                       M_DLD(d, s1, a);
-                                       store_reg_to_var_flt(iptr->dst, d);
-                                       break;
-                               default: panic ("internal error");
-                               }
+                       case TYPE_INT:
+                               var_to_reg_int(s1, src, REG_ITMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+                               gen_nullptr_check(s1);
+                               M_ILD(d, s1, a);
+                               store_reg_to_var_int(iptr->dst, d);
+                               break;
+                       case TYPE_LNG:
+                               var_to_reg_int(s1, src, REG_ITMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+                               gen_nullptr_check(s1);
+                               M_LLD(d, s1, a);
+                               store_reg_to_var_int(iptr->dst, d);
+                               break;
+                       case TYPE_ADR:
+                               var_to_reg_int(s1, src, REG_ITMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+                               gen_nullptr_check(s1);
+                               M_ALD(d, s1, a);
+                               store_reg_to_var_int(iptr->dst, d);
+                               break;
+                       case TYPE_FLT:
+                               var_to_reg_int(s1, src, REG_ITMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_FTMP1);
+                               gen_nullptr_check(s1);
+                               M_FLD(d, s1, a);
+                               store_reg_to_var_flt(iptr->dst, d);
+                               break;
+                       case TYPE_DBL:                          
+                               var_to_reg_int(s1, src, REG_ITMP1);
+                               d = reg_of_var(rd, iptr->dst, REG_FTMP1);
+                               gen_nullptr_check(s1);
+                               M_DLD(d, s1, a);
+                               store_reg_to_var_flt(iptr->dst, d);
+                               break;
+                       default:
+                               throw_cacao_exception_exit(string_java_lang_InternalError,
+                                                                                  "Unknown GETFIELD operand type %d",
+                                                                                  iptr->op1);
+                       }
                        break;
 
 
                /* branch operations **************************************************/
 
-#define ALIGNCODENOP {if((int)((long)mcodeptr&7)){M_NOP;}}
-
                case ICMD_ATHROW:       /* ..., objectref ==> ... (, objectref)       */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_INTMOVE(s1, REG_ITMP1_XPTR);
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP2, REG_PV, a);
                        M_JSR(REG_ITMP2_XPC, REG_ITMP2);
                        M_NOP;
@@ -2064,7 +2050,7 @@ void codegen(methodinfo *m)
                case ICMD_GOTO:         /* ... ==> ...                                */
                                        /* op1 = target JavaVM pc                     */
                        M_BR(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        ALIGNCODENOP;
                        break;
@@ -2072,15 +2058,15 @@ void codegen(methodinfo *m)
                case ICMD_JSR:          /* ... ==> ...                                */
                                        /* op1 = target JavaVM pc                     */
 
-                       dseg_addtarget(BlockPtrOfPC(iptr->op1));
-                       M_ALD(REG_ITMP1, REG_PV, -dseglen);
+                       dseg_addtarget(cd, BlockPtrOfPC(iptr->op1));
+                       M_ALD(REG_ITMP1, REG_PV, -(cd->dseglen));
                        M_JSR(REG_ITMP1, REG_ITMP1);        /* REG_ITMP1 = return address */
                        M_NOP;
                        break;
                        
                case ICMD_RET:          /* ... ==> ...                                */
                                        /* op1 = local variable                       */
-                       var = &(r->locals[iptr->op1][TYPE_ADR]);
+                       var = &(rd->locals[iptr->op1][TYPE_ADR]);
                        if (var->flags & INMEMORY) {
                                M_ALD(REG_ITMP1, REG_SP, 8 * var->regoff);
                                M_RET(REG_ITMP1);
@@ -2096,7 +2082,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_BEQZ(s1, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2105,7 +2091,7 @@ void codegen(methodinfo *m)
 
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_BNEZ(s1, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2120,7 +2106,7 @@ void codegen(methodinfo *m)
                                ICONST(REG_ITMP2, iptr->val.i);
                                M_BEQ(s1, REG_ITMP2, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2141,7 +2127,7 @@ void codegen(methodinfo *m)
                                        }
                                M_BNEZ(REG_ITMP1, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2163,7 +2149,7 @@ void codegen(methodinfo *m)
                                        M_BEQZ(REG_ITMP1, 0);
                                        }
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2178,7 +2164,7 @@ void codegen(methodinfo *m)
                                ICONST(REG_ITMP2, iptr->val.i);
                                M_BNE(s1, REG_ITMP2, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2200,7 +2186,7 @@ void codegen(methodinfo *m)
                                        M_BNEZ(REG_ITMP1, 0);
                                        }
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2221,7 +2207,7 @@ void codegen(methodinfo *m)
                                        }
                                M_BEQZ(REG_ITMP1, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2236,7 +2222,7 @@ void codegen(methodinfo *m)
                                LCONST(REG_ITMP2, iptr->val.l);
                                M_BEQ(s1, REG_ITMP2, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2257,7 +2243,7 @@ void codegen(methodinfo *m)
                                        }
                                M_BNEZ(REG_ITMP1, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2279,7 +2265,7 @@ void codegen(methodinfo *m)
                                        M_BEQZ(REG_ITMP1, 0);
                                        }
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2294,7 +2280,7 @@ void codegen(methodinfo *m)
                                LCONST(REG_ITMP2, iptr->val.l);
                                M_BNE(s1, REG_ITMP2, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2316,7 +2302,7 @@ void codegen(methodinfo *m)
                                        M_BNEZ(REG_ITMP1, 0);
                                        }
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2337,7 +2323,7 @@ void codegen(methodinfo *m)
                                        }
                                M_BEQZ(REG_ITMP1, 0);
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2348,7 +2334,7 @@ void codegen(methodinfo *m)
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_BEQ(s1, s2, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2359,7 +2345,7 @@ void codegen(methodinfo *m)
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_BNE(s1, s2, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2370,7 +2356,7 @@ void codegen(methodinfo *m)
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMPLT(s1, s2, REG_ITMP1);
                        M_BNEZ(REG_ITMP1, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2381,7 +2367,7 @@ void codegen(methodinfo *m)
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMPGT(s1, s2, REG_ITMP1);
                        M_BNEZ(REG_ITMP1, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2392,7 +2378,7 @@ void codegen(methodinfo *m)
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMPGT(s1, s2, REG_ITMP1);
                        M_BEQZ(REG_ITMP1, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2403,7 +2389,7 @@ void codegen(methodinfo *m)
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMPLT(s1, s2, REG_ITMP1);
                        M_BEQZ(REG_ITMP1, 0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        M_NOP;
                        break;
 
@@ -2417,7 +2403,7 @@ void codegen(methodinfo *m)
                                        /* val.i = constant                           */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        s3 = iptr->val.i;
                        if (iptr[1].opc == ICMD_ELSE_ICONST) {
                                if ((s3 == 1) && (iptr[1].val.i == 0)) {
@@ -2451,7 +2437,7 @@ void codegen(methodinfo *m)
                                        /* val.i = constant                           */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        s3 = iptr->val.i;
                        if (iptr[1].opc == ICMD_ELSE_ICONST) {
                                if ((s3 == 0) && (iptr[1].val.i == 1)) {
@@ -2485,7 +2471,7 @@ void codegen(methodinfo *m)
                                        /* val.i = constant                           */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        s3 = iptr->val.i;
                        if ((iptr[1].opc == ICMD_ELSE_ICONST)) {
                                if ((s3 == 1) && (iptr[1].val.i == 0)) {
@@ -2518,7 +2504,7 @@ void codegen(methodinfo *m)
                                        /* val.i = constant                           */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        s3 = iptr->val.i;
                        if ((iptr[1].opc == ICMD_ELSE_ICONST)) {
                                if ((s3 == 1) && (iptr[1].val.i == 0)) {
@@ -2551,7 +2537,7 @@ void codegen(methodinfo *m)
                                        /* val.i = constant                           */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        s3 = iptr->val.i;
                        if ((iptr[1].opc == ICMD_ELSE_ICONST)) {
                                if ((s3 == 1) && (iptr[1].val.i == 0)) {
@@ -2584,7 +2570,7 @@ void codegen(methodinfo *m)
                                        /* val.i = constant                           */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        s3 = iptr->val.i;
                        if ((iptr[1].opc == ICMD_ELSE_ICONST)) {
                                if ((s3 == 1) && (iptr[1].val.i == 0)) {
@@ -2622,11 +2608,11 @@ void codegen(methodinfo *m)
 #if defined(USE_THREADS)
                        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                                s4 disp;
-                               a = dseg_addaddress((void *) builtin_monitorexit);
+                               a = dseg_addaddress(cd, (void *) builtin_monitorexit);
                                M_ALD(REG_ITMP3, REG_PV, a);
                                M_JSR(REG_RA, REG_ITMP3);
-                               M_ALD(r->argintregs[0], REG_SP, r->maxmemuse * 8); /* delay slot */
-                               disp = -(s4) ((u1 *) mcodeptr - mcodebase);
+                               M_ALD(rd->argintregs[0], REG_SP, rd->maxmemuse * 8); /* delay slot */
+                               disp = -(s4) ((u1 *) mcodeptr - cd->mcodebase);
                                M_LDA(REG_PV, REG_RA, disp);
                        }
 #endif
@@ -2640,11 +2626,11 @@ void codegen(methodinfo *m)
 #if defined(USE_THREADS)
                        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                                s4 disp;
-                               a = dseg_addaddress((void *) builtin_monitorexit);
+                               a = dseg_addaddress(cd, (void *) builtin_monitorexit);
                                M_ALD(REG_ITMP3, REG_PV, a);
                                M_JSR(REG_RA, REG_ITMP3);
-                               M_ALD(r->argintregs[0], REG_SP, r->maxmemuse * 8); /* delay slot */
-                               disp = -(s4) ((u1 *) mcodeptr - mcodebase);
+                               M_ALD(rd->argintregs[0], REG_SP, rd->maxmemuse * 8); /* delay slot */
+                               disp = -(s4) ((u1 *) mcodeptr - cd->mcodebase);
                                M_LDA(REG_PV, REG_RA, disp);
                        }
 #endif
@@ -2660,11 +2646,11 @@ void codegen(methodinfo *m)
 #if defined(USE_THREADS)
                        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                                s4 disp;
-                               a = dseg_addaddress((void *) builtin_monitorexit);
+                               a = dseg_addaddress(cd, (void *) builtin_monitorexit);
                                M_ALD(REG_ITMP3, REG_PV, a);
                                M_JSR(REG_RA, REG_ITMP3);
-                               M_ALD(r->argintregs[0], REG_SP, r->maxmemuse * 8); /* delay slot */
-                               disp = -(s4) ((u1 *) mcodeptr - mcodebase);
+                               M_ALD(rd->argintregs[0], REG_SP, rd->maxmemuse * 8); /* delay slot */
+                               disp = -(s4) ((u1 *) mcodeptr - cd->mcodebase);
                                M_LDA(REG_PV, REG_RA, disp);
                        }
 #endif
@@ -2683,11 +2669,11 @@ nowperformreturn:
 
                        /* restore saved registers                                        */
 
-                       for (i = r->savintregcnt - 1; i >= r->maxsavintreguse; i--) {
-                               p--; M_LLD(r->savintregs[i], REG_SP, 8 * p);
+                       for (i = rd->savintregcnt - 1; i >= rd->maxsavintreguse; i--) {
+                               p--; M_LLD(rd->savintregs[i], REG_SP, 8 * p);
                        }
-                       for (i = r->savfltregcnt - 1; i >= r->maxsavfltreguse; i--) {
-                               p--; M_DLD(r->savfltregs[i], REG_SP, 8 * p);
+                       for (i = rd->savfltregcnt - 1; i >= rd->maxsavfltreguse; i--) {
+                               p--; M_DLD(rd->savfltregs[i], REG_SP, 8 * p);
                        }
 
                        /* call trace function */
@@ -2697,12 +2683,12 @@ nowperformreturn:
                                M_LST(REG_RA, REG_SP, 0);
                                M_LST(REG_RESULT, REG_SP, 8);
                                M_DST(REG_FRESULT, REG_SP,16);
-                               a = dseg_addaddress(m);
-                               M_ALD(r->argintregs[0], REG_PV, a);
-                               M_MOV(REG_RESULT, r->argintregs[1]);
-                               M_FLTMOVE(REG_FRESULT, r->argfltregs[2]);
-                               M_FMOV(REG_FRESULT, r->argfltregs[3]);
-                               a = dseg_addaddress((void *) builtin_displaymethodstop);
+                               a = dseg_addaddress(cd, m);
+                               M_ALD(rd->argintregs[0], REG_PV, a);
+                               M_MOV(REG_RESULT, rd->argintregs[1]);
+                               M_FLTMOVE(REG_FRESULT, rd->argfltregs[2]);
+                               M_FMOV(REG_FRESULT, rd->argfltregs[3]);
+                               a = dseg_addaddress(cd, (void *) builtin_displaymethodstop);
                                M_ALD(REG_ITMP3, REG_PV, a);
                                M_JSR (REG_RA, REG_ITMP3);
                                M_NOP;
@@ -2755,7 +2741,7 @@ nowperformreturn:
 
                        M_CMPULT_IMM(REG_ITMP1, i, REG_ITMP2);
                        M_BEQZ(REG_ITMP2, 0);
-                       codegen_addreference((basicblock *) tptr[0], mcodeptr);
+                       codegen_addreference(cd, (basicblock *) tptr[0], mcodeptr);
                        M_ASLL_IMM(REG_ITMP1, POINTERSHIFT, REG_ITMP1);      /* delay slot*/
 
                        /* build jump table top down and use address of lowest entry */
@@ -2764,8 +2750,8 @@ nowperformreturn:
                        tptr += i;
 
                        while (--i >= 0) {
-                               /* dseg_addtarget(BlockPtrOfPC(*--s4ptr)); */
-                               dseg_addtarget((basicblock *) tptr[0]); 
+                               /* dseg_addtarget(cd, BlockPtrOfPC(*--s4ptr)); */
+                               dseg_addtarget(cd, (basicblock *) tptr[0]); 
                                --tptr;
                                }
                        }
@@ -2773,7 +2759,7 @@ nowperformreturn:
                        /* length of dataseg after last dseg_addtarget is used by load */
 
                        M_AADD(REG_ITMP1, REG_PV, REG_ITMP2);
-                       M_ALD(REG_ITMP2, REG_ITMP2, -dseglen);
+                       M_ALD(REG_ITMP2, REG_ITMP2, -(cd->dseglen));
                        M_JMP(REG_ITMP2);
                        M_NOP;
                        ALIGNCODENOP;
@@ -2800,13 +2786,13 @@ nowperformreturn:
                                val = s4ptr[0];
                                ICONST(REG_ITMP2, val);
                                M_BEQ(s1, REG_ITMP2, 0);
-                               codegen_addreference((basicblock *) tptr[0], mcodeptr); 
+                               codegen_addreference(cd, (basicblock *) tptr[0], mcodeptr); 
                                M_NOP;
                                }
 
                        M_BR(0);
                        tptr = (void **) iptr->target;
-                       codegen_addreference((basicblock *) tptr[0], mcodeptr);
+                       codegen_addreference(cd, (basicblock *) tptr[0], mcodeptr);
                        M_NOP;
                        ALIGNCODENOP;
                        break;
@@ -2854,7 +2840,7 @@ gen_method: {
                                        continue;
                                if (IS_INT_LNG_TYPE(src->type)) {
                                        if (s3 < INT_ARG_CNT) {
-                                               s1 = r->argintregs[s3];
+                                               s1 = rd->argintregs[s3];
                                                var_to_reg_int(d, src, s1);
                                                M_INTMOVE(d, s1);
 
@@ -2865,7 +2851,7 @@ gen_method: {
 
                                } else {
                                        if (s3 < FLT_ARG_CNT) {
-                                               s1 = r->argfltregs[s3];
+                                               s1 = rd->argfltregs[s3];
                                                var_to_reg_flt(d, src, s1);
                                                M_TFLTMOVE(src->type, d, s1);
 
@@ -2881,7 +2867,7 @@ gen_method: {
                        case ICMD_BUILTIN3:
                        case ICMD_BUILTIN2:
                        case ICMD_BUILTIN1:
-                               a = dseg_addaddress((void *) lm);
+                               a = dseg_addaddress(cd, (void *) lm);
                                d = iptr->op1;                                 /* return type */
 
                                M_ALD(REG_ITMP3, REG_PV, a);     /* built-in-function pointer */
@@ -2891,7 +2877,7 @@ gen_method: {
 
                        case ICMD_INVOKESTATIC:
                        case ICMD_INVOKESPECIAL:
-                               a = dseg_addaddress(lm->stubroutine);
+                               a = dseg_addaddress(cd, lm->stubroutine);
                                d = lm->returntype;
 
                                M_ALD(REG_PV, REG_PV, a);             /* method pointer in pv */
@@ -2900,17 +2886,17 @@ gen_method: {
                        case ICMD_INVOKEVIRTUAL:
                                d = lm->returntype;
 
-                               gen_nullptr_check(r->argintregs[0]);
-                               M_ALD(REG_METHODPTR, r->argintregs[0], OFFSET(java_objectheader, vftbl));
-                               M_ALD(REG_PV, REG_METHODPTR, OFFSET(vftbl, table[0]) + sizeof(methodptr) * lm->vftblindex);
+                               gen_nullptr_check(rd->argintregs[0]);
+                               M_ALD(REG_METHODPTR, rd->argintregs[0], OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_PV, REG_METHODPTR, OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * lm->vftblindex);
                                break;
 
                        case ICMD_INVOKEINTERFACE:
                                d = lm->returntype;
                                        
-                               gen_nullptr_check(r->argintregs[0]);
-                               M_ALD(REG_METHODPTR, r->argintregs[0], OFFSET(java_objectheader, vftbl));
-                               M_ALD(REG_METHODPTR, REG_METHODPTR, OFFSET(vftbl, interfacetable[0]) - sizeof(methodptr*) * lm->class->index);
+                               gen_nullptr_check(rd->argintregs[0]);
+                               M_ALD(REG_METHODPTR, rd->argintregs[0], OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_METHODPTR, REG_METHODPTR, OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * lm->class->index);
                                M_ALD(REG_PV, REG_METHODPTR, sizeof(methodptr) * (lm - lm->class->methods));
                                break;
                        }
@@ -2922,7 +2908,7 @@ gen_method: {
 
 afteractualcall:
 
-                       s1 = (s4) ((u1 *) mcodeptr - mcodebase);
+                       s1 = (s4) ((u1 *) mcodeptr - cd->mcodebase);
                        if (s1 <= 32768) M_LDA(REG_PV, REG_RA, -s1);
                        else {
                                s4 ml = -s1, mh = 0;
@@ -2936,12 +2922,12 @@ afteractualcall:
 
                        if (d != TYPE_VOID) {
                                if (IS_INT_LNG_TYPE(iptr->dst->type)) {
-                                       s1 = reg_of_var(m, iptr->dst, REG_RESULT);
+                                       s1 = reg_of_var(rd, iptr->dst, REG_RESULT);
                                        M_INTMOVE(REG_RESULT, s1);
                                        store_reg_to_var_int(iptr->dst, s1);
 
                                } else {
-                                       s1 = reg_of_var(m, iptr->dst, REG_FRESULT);
+                                       s1 = reg_of_var(rd, iptr->dst, REG_FRESULT);
                                        M_TFLTMOVE(iptr->dst->type, REG_FRESULT, s1);
                                        store_reg_to_var_flt(iptr->dst, s1);
                                }
@@ -2972,10 +2958,10 @@ afteractualcall:
                        classinfo *super = (classinfo*) iptr->val.a;
                        
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-                       codegen_threadcritrestart((u1*) mcodeptr - mcodebase);
+                       codegen_threadcritrestart(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (s1 == d) {
                                M_MOV(s1, REG_ITMP1);
                                s1 = REG_ITMP1;
@@ -2986,12 +2972,12 @@ afteractualcall:
                                        M_BEQZ(s1, 8);
                                        M_NOP;
                                        M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                                       M_ILD(REG_ITMP2, REG_ITMP1, OFFSET(vftbl, interfacetablelength));
+                                       M_ILD(REG_ITMP2, REG_ITMP1, OFFSET(vftbl_t, interfacetablelength));
                                        M_IADD_IMM(REG_ITMP2, - super->index, REG_ITMP2);
                                        M_BLEZ(REG_ITMP2, 3);
                                        M_NOP;
                                        M_ALD(REG_ITMP1, REG_ITMP1,
-                                             OFFSET(vftbl, interfacetable[0]) -
+                                             OFFSET(vftbl_t, interfacetable[0]) -
                                              super->index * sizeof(methodptr*));
                                        M_CMPULT(REG_ZERO, REG_ITMP1, d);      /* REG_ITMP1 != 0  */
                                        }
@@ -3001,7 +2987,7 @@ afteractualcall:
                                        M_BEQZ(s1, 5);
                                        M_NOP;
                                        M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                                       M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl, baseval));
+                                       M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
                                        M_IADD_IMM(REG_ITMP1, - super->vftbl->baseval, REG_ITMP1);
                                        M_CMPULT_IMM(REG_ITMP1, s2 + 1, d);
                                        */
@@ -3009,16 +2995,16 @@ afteractualcall:
                                        M_BEQZ(s1, 9);
                                        M_NOP;
                                        M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                    a = dseg_addaddress ((void*) super->vftbl);
+                    a = dseg_addaddress(cd, (void *) super->vftbl);
                     M_ALD(REG_ITMP2, REG_PV, a);
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-                                       codegen_threadcritstart((u1*) mcodeptr - mcodebase);
+                                       codegen_threadcritstart(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
-                    M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl, baseval));
-                    M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl, baseval));
-                    M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl, diffval));
+                    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(USE_THREADS) && defined(NATIVE_THREADS)
-                                       codegen_threadcritstop((u1*) mcodeptr - mcodebase);
+                                       codegen_threadcritstop(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
                     M_ISUB(REG_ITMP1, REG_ITMP3, REG_ITMP1); 
                     M_CMPULT(REG_ITMP2, REG_ITMP1, d);
@@ -3054,26 +3040,26 @@ afteractualcall:
                        classinfo *super = (classinfo*) iptr->val.a;
                        
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-                       codegen_threadcritrestart((u1*) mcodeptr - mcodebase);
+                       codegen_threadcritrestart(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
 
-                       d = reg_of_var(m, iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        var_to_reg_int(s1, src, d);
                        if (iptr->op1) {                               /* class/interface */
                                if (super->flags & ACC_INTERFACE) {        /* interface       */
                                        M_BEQZ(s1, 9);
                                        M_NOP;
                                        M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                                       M_ILD(REG_ITMP2, REG_ITMP1, OFFSET(vftbl, interfacetablelength));
+                                       M_ILD(REG_ITMP2, REG_ITMP1, OFFSET(vftbl_t, interfacetablelength));
                                        M_IADD_IMM(REG_ITMP2, - super->index, REG_ITMP2);
                                        M_BLEZ(REG_ITMP2, 0);
-                                       codegen_addxcastrefs(mcodeptr);
+                                       codegen_addxcastrefs(cd, mcodeptr);
                                        M_NOP;
                                        M_ALD(REG_ITMP2, REG_ITMP1,
-                                             OFFSET(vftbl, interfacetable[0]) -
+                                             OFFSET(vftbl_t, interfacetable[0]) -
                                              super->index * sizeof(methodptr*));
                                        M_BEQZ(REG_ITMP2, 0);
-                                       codegen_addxcastrefs(mcodeptr);
+                                       codegen_addxcastrefs(cd, mcodeptr);
                                        M_NOP;
                                        }
                                else {                                     /* class           */
@@ -3083,7 +3069,7 @@ afteractualcall:
                                        M_BEQZ(s1, 6 + (s2 != 0));
                                        M_NOP;
                                        M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                                       M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl, baseval));
+                                       M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
                                        M_IADD_IMM(REG_ITMP1, - super->vftbl->baseval, REG_ITMP1);
                                        if (s2 == 0) {
                                                M_BNEZ(REG_ITMP1, 0);
@@ -3097,32 +3083,32 @@ afteractualcall:
                                        M_BEQZ(s1, 10 + (d == REG_ITMP3));
                                        M_NOP;
                                        M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
-                    a = dseg_addaddress ((void*) super->vftbl);
+                    a = dseg_addaddress(cd, (void *) super->vftbl);
                     M_ALD(REG_ITMP2, REG_PV, a);
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-                                       codegen_threadcritstart((u1*) mcodeptr - mcodebase);
+                                       codegen_threadcritstart(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
-                    M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl, baseval));
+                    M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
                                        if (d != REG_ITMP3) {
-                                               M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl, baseval));
-                                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl, diffval));
+                                               M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
+                                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-                                               codegen_threadcritstop((u1*) mcodeptr - mcodebase);
+                                               codegen_threadcritstop(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
                                                M_ISUB(REG_ITMP1, REG_ITMP3, REG_ITMP1); 
                                        } else {
-                                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl, baseval));
+                                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                                M_ISUB(REG_ITMP1, REG_ITMP2, REG_ITMP1); 
                                                M_ALD(REG_ITMP2, REG_PV, a);
-                                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl, diffval));
+                                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-                                               codegen_threadcritstop((u1*) mcodeptr - mcodebase);
+                                               codegen_threadcritstop(cd, (u1 *) mcodeptr - cd->mcodebase);
 #endif
                                        }
                     M_CMPULT(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                        M_BNEZ(REG_ITMP2, 0);
 
-                                       codegen_addxcastrefs(mcodeptr);
+                                       codegen_addxcastrefs(cd, mcodeptr);
                                        M_NOP;
                                        }
                                }
@@ -3137,14 +3123,14 @@ afteractualcall:
 
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_BLTZ(s1, 0);
-                       codegen_addxcheckarefs(mcodeptr);
+                       codegen_addxcheckarefs(cd, mcodeptr);
                        M_NOP;
                        break;
 
                case ICMD_CHECKEXCEPTION:  /* ... ==> ...                             */
 
                        M_BEQZ(REG_RESULT, 0);
-                       codegen_addxexceptionrefs(mcodeptr);
+                       codegen_addxexceptionrefs(cd, mcodeptr);
                        M_NOP;
                        break;
 
@@ -3158,49 +3144,47 @@ afteractualcall:
                        for (s1 = iptr->op1; --s1 >= 0; src = src->prev) {
                                var_to_reg_int(s2, src, REG_ITMP1);
                                M_BLTZ(s2, 0);
-                               codegen_addxcheckarefs(mcodeptr);
+                               codegen_addxcheckarefs(cd, mcodeptr);
                                M_NOP;
 
                                /* copy sizes to stack (argument numbers >= INT_ARG_CNT)      */
 
                                if (src->varkind != ARGVAR) {
                                        M_LST(s2, REG_SP, 8 * (s1 + INT_ARG_CNT));
-                                       }
                                }
+                       }
 
                        /* a0 = dimension count */
 
-                       ICONST(r->argintregs[0], iptr->op1);
+                       ICONST(rd->argintregs[0], iptr->op1);
 
                        /* a1 = arraydescriptor */
 
-                       a = dseg_addaddress(iptr->val.a);
-                       M_ALD(r->argintregs[1], REG_PV, a);
+                       a = dseg_addaddress(cd, iptr->val.a);
+                       M_ALD(rd->argintregs[1], REG_PV, a);
 
                        /* a2 = pointer to dimensions = stack pointer */
 
-                       M_INTMOVE(REG_SP, r->argintregs[2]);
+                       M_INTMOVE(REG_SP, rd->argintregs[2]);
 
-                       a = dseg_addaddress((void*) builtin_nmultianewarray);
+                       a = dseg_addaddress(cd, (void*) builtin_nmultianewarray);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_NOP;
-                       s1 = (int)((u1*) mcodeptr - mcodebase);
+                       s1 = (int)((u1*) mcodeptr - cd->mcodebase);
                        if (s1 <= 32768)
                                M_LDA (REG_PV, REG_RA, -s1);
                        else {
                                panic("To big");
-                           }
-                       s1 = reg_of_var(m, iptr->dst, REG_RESULT);
+                       }
+                       s1 = reg_of_var(rd, iptr->dst, REG_RESULT);
                        M_INTMOVE(REG_RESULT, s1);
                        store_reg_to_var_int(iptr->dst, s1);
                        break;
 
-
-               default: error ("Unknown pseudo command: %d", iptr->opc);
-       
-   
-
+               default:
+                       throw_cacao_exception_exit(string_java_lang_InternalError,
+                                                                          "Unknown ICMD %d", iptr->opc);
        } /* switch */
                
        } /* for instruction */
@@ -3216,20 +3200,20 @@ afteractualcall:
                        s2 = src->type;
                        if (IS_FLT_DBL_TYPE(s2)) {
                                var_to_reg_flt(s1, src, REG_FTMP1);
-                               if (!(r->interfaces[len][s2].flags & INMEMORY)) {
-                                       M_TFLTMOVE(s2, s1, r->interfaces[len][s2].regoff);
+                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                       M_TFLTMOVE(s2, s1, rd->interfaces[len][s2].regoff);
 
                                } else {
-                                       M_DST(s1, REG_SP, 8 * r->interfaces[len][s2].regoff);
+                                       M_DST(s1, REG_SP, 8 * rd->interfaces[len][s2].regoff);
                                }
 
                        } else {
                                var_to_reg_int(s1, src, REG_ITMP1);
-                               if (!(r->interfaces[len][s2].flags & INMEMORY)) {
-                                       M_INTMOVE(s1, r->interfaces[len][s2].regoff);
+                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                       M_INTMOVE(s1, rd->interfaces[len][s2].regoff);
 
                                } else {
-                                       M_LST(s1, REG_SP, 8 * r->interfaces[len][s2].regoff);
+                                       M_LST(s1, REG_SP, 8 * rd->interfaces[len][s2].regoff);
                                }
                        }
                }
@@ -3238,22 +3222,21 @@ afteractualcall:
        } /* if (bptr -> flags >= BBREACHED) */
        } /* for basic block */
 
-       /* bptr -> mpc = (int)((u1*) mcodeptr - mcodebase); */
-
        {
        /* generate bound check stubs */
 
-       s4 *xcodeptr = NULL;
-       
-       for (; xboundrefs != NULL; xboundrefs = xboundrefs->next) {
-               gen_resolvebranch((u1*) mcodebase + xboundrefs->branchpos, 
-                                 xboundrefs->branchpos,
-                                                 (u1*) mcodeptr - mcodebase);
+       s4        *xcodeptr = NULL;
+       branchref *bref;
+
+       for (bref = cd->xboundrefs; bref != NULL; bref = bref->next) {
+               gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                 bref->branchpos,
+                                                 (u1 *) mcodeptr - cd->mcodebase);
 
-               MCODECHECK(8);
+               MCODECHECK(14);
 
-               M_MOV(xboundrefs->reg, REG_ITMP1);
-               M_LADD_IMM(REG_PV, xboundrefs->branchpos - 4, REG_ITMP2_XPC);
+               M_MOV(bref->reg, REG_ITMP1);
+               M_LADD_IMM(REG_PV, bref->branchpos - 4, REG_ITMP2_XPC);
 
                if (xcodeptr != NULL) {
                        M_BR(xcodeptr - mcodeptr);
@@ -3265,11 +3248,8 @@ afteractualcall:
                        M_LSUB_IMM(REG_SP, 1 * 8, REG_SP);
                        M_LST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(string_java_lang_ArrayIndexOutOfBoundsException);
-                       M_ALD(r->argintregs[0], REG_PV, a);
-                       M_MOV(REG_ITMP1, r->argintregs[1]);
-
-                       a = dseg_addaddress(new_exception_int);
+                       M_MOV(REG_ITMP1, rd->argintregs[0]);
+                       a = dseg_addaddress(cd, new_arrayindexoutofboundsexception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_NOP;
@@ -3278,7 +3258,7 @@ afteractualcall:
                        M_LLD(REG_ITMP2_XPC, REG_SP, 0 * 8);
                        M_LADD_IMM(REG_SP, 1 * 8, REG_SP);
 
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JMP(REG_ITMP3);
                        M_NOP;
@@ -3289,21 +3269,21 @@ afteractualcall:
 
        xcodeptr = NULL;
        
-       for (; xcheckarefs != NULL; xcheckarefs = xcheckarefs->next) {
-               if ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
-                       gen_resolvebranch((u1*) mcodebase + xcheckarefs->branchpos, 
-                                                         xcheckarefs->branchpos,
-                                                         (u1*) xcodeptr - (u1*) mcodebase - 4);
+       for (bref = cd->xcheckarefs; bref != NULL; bref = bref->next) {
+               if ((cd->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - cd->mcodebase - 4);
                        continue;
                }
 
-               gen_resolvebranch((u1*) mcodebase + xcheckarefs->branchpos, 
-                                 xcheckarefs->branchpos,
-                                                 (u1*) mcodeptr - mcodebase);
+               gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                 bref->branchpos,
+                                                 (u1 *) mcodeptr - cd->mcodebase);
 
-               MCODECHECK(8);
+               MCODECHECK(12);
 
-               M_LADD_IMM(REG_PV, xcheckarefs->branchpos - 4, REG_ITMP2_XPC);
+               M_LADD_IMM(REG_PV, bref->branchpos - 4, REG_ITMP2_XPC);
 
                if (xcodeptr != NULL) {
                        M_BR(xcodeptr - mcodeptr);
@@ -3315,10 +3295,7 @@ afteractualcall:
                        M_LSUB_IMM(REG_SP, 1 * 8, REG_SP);
                        M_LST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(string_java_lang_NegativeArraySizeException);
-                       M_ALD(r->argintregs[0], REG_PV, a);
-
-                       a = dseg_addaddress(new_exception);
+                       a = dseg_addaddress(cd, new_negativearraysizeexception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_NOP;
@@ -3327,7 +3304,7 @@ afteractualcall:
                        M_LLD(REG_ITMP2_XPC, REG_SP, 0 * 8);
                        M_LADD_IMM(REG_SP, 1 * 8, REG_SP);
 
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JMP(REG_ITMP3);
                        M_NOP;
@@ -3338,21 +3315,21 @@ afteractualcall:
 
        xcodeptr = NULL;
        
-       for (; xcastrefs != NULL; xcastrefs = xcastrefs->next) {
-               if ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
-                       gen_resolvebranch((u1*) mcodebase + xcastrefs->branchpos, 
-                                                         xcastrefs->branchpos,
-                                                         (u1*) xcodeptr - (u1*) mcodebase - 4);
+       for (bref = cd->xcastrefs; bref != NULL; bref = bref->next) {
+               if ((cd->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - cd->mcodebase - 4);
                        continue;
                }
 
-               gen_resolvebranch((u1*) mcodebase + xcastrefs->branchpos, 
-                                 xcastrefs->branchpos,
-                                                 (u1*) mcodeptr - mcodebase);
+               gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                 bref->branchpos,
+                                                 (u1 *) mcodeptr - cd->mcodebase);
 
-               MCODECHECK(8);
+               MCODECHECK(12);
 
-               M_LADD_IMM(REG_PV, xcastrefs->branchpos - 4, REG_ITMP2_XPC);
+               M_LADD_IMM(REG_PV, bref->branchpos - 4, REG_ITMP2_XPC);
 
                if (xcodeptr != NULL) {
                        M_BR(xcodeptr - mcodeptr);
@@ -3364,10 +3341,7 @@ afteractualcall:
                        M_LSUB_IMM(REG_SP, 1 * 8, REG_SP);
                        M_LST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(string_java_lang_ClassCastException);
-                       M_ALD(r->argintregs[0], REG_PV, a);
-
-                       a = dseg_addaddress(new_exception);
+                       a = dseg_addaddress(cd, new_classcastexception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_NOP;
@@ -3376,7 +3350,7 @@ afteractualcall:
                        M_LLD(REG_ITMP2_XPC, REG_SP, 0 * 8);
                        M_LADD_IMM(REG_SP, 1 * 8, REG_SP);
 
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JMP(REG_ITMP3);
                        M_NOP;
@@ -3387,21 +3361,21 @@ afteractualcall:
 
        xcodeptr = NULL;
 
-       for (; xexceptionrefs != NULL; xexceptionrefs = xexceptionrefs->next) {
-               if ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
-                       gen_resolvebranch((u1*) mcodebase + xexceptionrefs->branchpos, 
-                                                         xexceptionrefs->branchpos,
-                                                         (u1*) xcodeptr - (u1*) mcodebase - 4);
+       for (bref = cd->xexceptionrefs; bref != NULL; bref = bref->next) {
+               if ((cd->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - cd->mcodebase - 4);
                        continue;
                }
 
-               gen_resolvebranch((u1*) mcodebase + xexceptionrefs->branchpos, 
-                                 xexceptionrefs->branchpos,
-                                                 (u1*) mcodeptr - mcodebase);
+               gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                 bref->branchpos,
+                                                 (u1 *) mcodeptr - cd->mcodebase);
 
-               MCODECHECK(8);
+               MCODECHECK(13);
 
-               M_LADD_IMM(REG_PV, xexceptionrefs->branchpos - 4, REG_ITMP2_XPC);
+               M_LADD_IMM(REG_PV, bref->branchpos - 4, REG_ITMP2_XPC);
 
                if (xcodeptr != NULL) {
                        M_BR(xcodeptr - mcodeptr);
@@ -3414,22 +3388,25 @@ afteractualcall:
                        M_LSUB_IMM(REG_SP, 1 * 8, REG_SP);
                        M_LST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(builtin_get_exceptionptrptr);
+                       a = dseg_addaddress(cd, builtin_get_exceptionptrptr);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_NOP;
-                       M_MOV(REG_RESULT, REG_ITMP1_XPTR);
+
+                       /* get the exceptionptr from the ptrprt and clear it */
+                       M_ALD(REG_ITMP1_XPTR, REG_RESULT, 0);
+                       M_AST(REG_ZERO, REG_RESULT, 0);
 
                        M_LLD(REG_ITMP2_XPC, REG_SP, 0 * 8);
                        M_LADD_IMM(REG_SP, 1 * 8, REG_SP);
 #else
-                       a = dseg_addaddress(&_exceptionptr);
+                       a = dseg_addaddress(cd, &_exceptionptr);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_ALD(REG_ITMP1_XPTR, REG_ITMP3, 0);
                        M_AST(REG_ZERO, REG_ITMP3, 0);
 #endif
 
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JMP(REG_ITMP3);
                        M_NOP;
@@ -3440,21 +3417,21 @@ afteractualcall:
 
        xcodeptr = NULL;
 
-       for (; xnullrefs != NULL; xnullrefs = xnullrefs->next) {
-               if ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
-                       gen_resolvebranch((u1*) mcodebase + xnullrefs->branchpos, 
-                                                         xnullrefs->branchpos,
-                                                         (u1*) xcodeptr - (u1*) mcodebase - 4);
+       for (bref = cd->xnullrefs; bref != NULL; bref = bref->next) {
+               if ((cd->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - cd->mcodebase - 4);
                        continue;
                }
 
-               gen_resolvebranch((u1*) mcodebase + xnullrefs->branchpos, 
-                                 xnullrefs->branchpos,
-                                                 (u1*) mcodeptr - mcodebase);
+               gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                 bref->branchpos,
+                                                 (u1 *) mcodeptr - cd->mcodebase);
 
-               MCODECHECK(8);
+               MCODECHECK(12);
 
-               M_LADD_IMM(REG_PV, xnullrefs->branchpos - 4, REG_ITMP2_XPC);
+               M_LADD_IMM(REG_PV, bref->branchpos - 4, REG_ITMP2_XPC);
 
                if (xcodeptr != NULL) {
                        M_BR(xcodeptr - mcodeptr);
@@ -3466,10 +3443,7 @@ afteractualcall:
                        M_LSUB_IMM(REG_SP, 1 * 8, REG_SP);
                        M_LST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(string_java_lang_NullPointerException);
-                       M_ALD(r->argintregs[0], REG_PV, a);
-
-                       a = dseg_addaddress(new_exception);
+                       a = dseg_addaddress(cd, new_nullpointerexception);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JSR(REG_RA, REG_ITMP3);
                        M_NOP;
@@ -3478,7 +3452,52 @@ afteractualcall:
                        M_LLD(REG_ITMP2_XPC, REG_SP, 0 * 8);
                        M_LADD_IMM(REG_SP, 1 * 8, REG_SP);
 
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
+                       M_ALD(REG_ITMP3, REG_PV, a);
+                       M_JMP(REG_ITMP3);
+                       M_NOP;
+               }
+       }
+
+
+       /* generate put/getstatic stub call code */
+
+       {
+               clinitref   *cref;
+               u8           mcode;
+               s4          *tmpmcodeptr;
+
+               for (cref = cd->clinitrefs; cref != NULL; cref = cref->next) {
+                       /* Get machine code which is patched back in later. The call is   */
+                       /* 2 instruction words long.                                      */
+                       xcodeptr = (s4 *) (cd->mcodebase + cref->branchpos);
+
+                       /* We need to split this, because an unaligned 8 byte read causes */
+                       /* a SIGSEGV.                                                     */
+                       mcode = ((u8) xcodeptr[1] << 32) + (u4) xcodeptr[0];
+
+                       /* patch in the call to call the following code (done at compile  */
+                       /* time)                                                          */
+
+                       tmpmcodeptr = mcodeptr;         /* save current mcodeptr          */
+                       mcodeptr = xcodeptr;            /* set mcodeptr to patch position */
+
+                       M_BRS(tmpmcodeptr - (xcodeptr + 1));
+                       M_NOP;
+
+                       mcodeptr = tmpmcodeptr;         /* restore the current mcodeptr   */
+
+                       MCODECHECK(5);
+
+                       /* move class pointer into REG_ITMP1                              */
+                       a = dseg_addaddress(cd, cref->class);
+                       M_ALD(REG_ITMP1, REG_PV, a);
+
+                       /* move machine code into REG_ITMP2                               */
+                       a = dseg_adds8(cd, mcode);
+                       M_LLD(REG_ITMP2, REG_PV, a);
+
+                       a = dseg_addaddress(cd, asm_check_clinit);
                        M_ALD(REG_ITMP3, REG_PV, a);
                        M_JMP(REG_ITMP3);
                        M_NOP;
@@ -3486,9 +3505,9 @@ afteractualcall:
        }
        }
 
-       codegen_finish(m, (s4) ((u1 *) mcodeptr - mcodebase));
+       codegen_finish(m, cd, (s4) ((u1 *) mcodeptr - cd->mcodebase));
 
-       docacheflush((void*) m->entrypoint, ((u1*) mcodeptr - mcodebase));
+       docacheflush((void*) m->entrypoint, ((u1*) mcodeptr - cd->mcodebase));
 }
 
 
@@ -3498,11 +3517,11 @@ afteractualcall:
        
 *******************************************************************************/
 
-#define COMPSTUBSIZE 4
+#define COMPSTUB_SIZE    4
 
 u1 *createcompilerstub(methodinfo *m)
 {
-       u8 *s = CNEW(u8, COMPSTUBSIZE);     /* memory to hold the stub            */
+       u8 *s = CNEW(u8, COMPSTUB_SIZE);    /* memory to hold the stub            */
        s4 *mcodeptr = (s4 *) s;            /* code generation pointer            */
        
                                            /* code for the stub                  */
@@ -3517,8 +3536,9 @@ u1 *createcompilerstub(methodinfo *m)
 
        (void) docacheflush((void*) s, (char*) mcodeptr - (char*) s);
 
-#ifdef STATISTICS
-       count_cstub_len += COMPSTUBSIZE * 8;
+#if defined(STATISTICS)
+       if (opt_stat)
+               count_cstub_len += COMPSTUB_SIZE * 8;
 #endif
 
        return (u1 *) s;
@@ -3533,7 +3553,7 @@ u1 *createcompilerstub(methodinfo *m)
 
 void removecompilerstub(u1 *stub)
 {
-       CFREE(stub, COMPSTUBSIZE * 8);
+       CFREE(stub, COMPSTUB_SIZE * 8);
 }
 
 
@@ -3544,16 +3564,18 @@ void removecompilerstub(u1 *stub)
 *******************************************************************************/
 
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-#define NATIVESTUBSTACK       2
-#define NATIVESTUBTHREADEXTRA 5
+#define NATIVESTUB_STACK           2
+#define NATIVESTUB_THREAD_EXTRA    5
 #else
-#define NATIVESTUBSTACK       1
-#define NATIVESTUBTHREADEXTRA 1
+#define NATIVESTUB_STACK           1
+#define NATIVESTUB_THREAD_EXTRA    1
 #endif
 
-#define NATIVESTUBSIZE      (54 + 4 + NATIVESTUBTHREADEXTRA - 1)
-#define NATIVEVERBOSESIZE   (50 + 17)
-#define NATIVESTUBOFFSET    9
+#define NATIVESTUB_SIZE            (54 + 4 + NATIVESTUB_THREAD_EXTRA - 1)
+#define NATIVESTUB_STATIC_SIZE     5
+#define NATIVESTUB_VERBOSE_SIZE    (50 + 17)
+#define NATIVESTUB_OFFSET          10
+
 
 u1 *createnativestub(functionptr f, methodinfo *m)
 {
@@ -3563,62 +3585,77 @@ u1 *createnativestub(functionptr f, methodinfo *m)
        s4 stackframesize = 0;              /* size of stackframe if needed       */
        s4 disp;
        s4 stubsize;
-       registerdata *r;
+       codegendata  *cd;
+       registerdata *rd;
+       t_inlining_globals *id;
+       s4 dumpsize;
 
-       /* init registers before using it */
-       reg_init(m);
+       /* mark start of dump memory area */
+
+       dumpsize = dump_size();
+
+       /* setup registers before using it */
+       
+       cd = DNEW(codegendata);
+       rd = DNEW(registerdata);
+       id = DNEW(t_inlining_globals);
 
-       /* keep code size smaller */
-       r = m->registerdata;
+       inlining_setup(m, id);
+       reg_setup(m, rd, id);
 
        descriptor2types(m);                /* set paramcount and paramtypes      */
 
-       stubsize = runverbose ? NATIVESTUBSIZE + NATIVEVERBOSESIZE : NATIVESTUBSIZE;
+       stubsize = NATIVESTUB_SIZE;         /* calculate nativestub size          */
+
+       if ((m->flags & ACC_STATIC) && !m->class->initialized)
+               stubsize += NATIVESTUB_STATIC_SIZE;
+
+       if (runverbose)
+               stubsize += NATIVESTUB_VERBOSE_SIZE;
+
        s = CNEW(u8, stubsize);             /* memory to hold the stub            */
-       cs = s + NATIVESTUBOFFSET;
+       cs = s + NATIVESTUB_OFFSET;
        mcodeptr = (s4 *) (cs);             /* code generation pointer            */
 
-       *(cs-1) = (u8) f;                   /* address of native method           */
+       /* set some required varibles which are normally set by codegen_setup     */
+       cd->mcodebase = (u1 *) mcodeptr;
+       cd->clinitrefs = NULL;
+
+       *(cs-1)  = (u8) f;                  /* address of native method           */
 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
-       *(cs-2) = (u8) &builtin_get_exceptionptrptr;
+       *(cs-2)  = (u8) &builtin_get_exceptionptrptr;
 #else
-       *(cs-2) = (u8) (&_exceptionptr);    /* address of exceptionptr            */
+       *(cs-2)  = (u8) (&_exceptionptr);   /* address of exceptionptr            */
 #endif
-       *(cs-3) = (u8) asm_handle_nat_exception;/* addr of asm exception handler  */
-       *(cs-4) = (u8) (&env);              /* addr of jni_environement           */
-       *(cs-5) = (u8) builtin_trace_args;
-       *(cs-6) = (u8) m;
-       *(cs-7) = (u8) builtin_displaymethodstop;
-       *(cs-8) = (u8) m->class;
-       *(cs-9) = (u8) asm_check_clinit;
-
-       M_LDA(REG_SP, REG_SP, -NATIVESTUBSTACK * 8); /* build up stackframe       */
+       *(cs-3)  = (u8) asm_handle_nat_exception;/* addr of asm exception handler */
+       *(cs-4)  = (u8) (&env);             /* addr of jni_environement           */
+       *(cs-5)  = (u8) builtin_trace_args;
+       *(cs-6)  = (u8) m;
+       *(cs-7)  = (u8) builtin_displaymethodstop;
+       *(cs-8)  = (u8) m->class;
+       *(cs-9)  = (u8) asm_check_clinit;
+       *(cs-10) = (u8) NULL;               /* filled with machine code           */
+
+       M_LDA(REG_SP, REG_SP, -NATIVESTUB_STACK * 8); /* build up stackframe      */
        M_LST(REG_RA, REG_SP, 0);           /* store return address               */
 
        /* if function is static, check for initialized */
 
-       if (m->flags & ACC_STATIC) {
-       /* if class isn't yet initialized, do it */
-               if (!m->class->initialized) {
-                       /* call helper function which patches this code */
-                       M_ALD(REG_ITMP1, REG_PV, -8 * 8);     /* class                    */
-                       M_ALD(REG_ITMP3, REG_PV, -9 * 8);     /* asm_check_clinit         */
-                       M_JSR(REG_RA, REG_ITMP3);
-                       M_NOP;
-               }
+       if (m->flags & ACC_STATIC && !m->class->initialized) {
+               codegen_addclinitref(cd, mcodeptr, m->class);
        }
 
        /* max. 50 instructions */
        if (runverbose) {
-               int p;
-               int t;
+               s4 p;
+               s4 t;
 
                M_LDA(REG_SP, REG_SP, -(18 * 8));
                M_AST(REG_RA, REG_SP, 1 * 8);
 
                /* save integer argument registers */
                for (p = 0; p < m->paramcount && p < INT_ARG_CNT; p++) {
-                       M_LST(r->argintregs[p], REG_SP,  (2 + p) * 8);
+                       M_LST(rd->argintregs[p], REG_SP,  (2 + p) * 8);
                }
 
                /* save and copy float arguments into integer registers */
@@ -3627,16 +3664,16 @@ u1 *createnativestub(functionptr f, methodinfo *m)
 
                        if (IS_FLT_DBL_TYPE(t)) {
                                if (IS_2_WORD_TYPE(t)) {
-                                       M_DST(r->argfltregs[p], REG_SP, (10 + p) * 8);
-                                       M_LLD(r->argintregs[p], REG_SP, (10 + p) * 8);
+                                       M_DST(rd->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_LLD(rd->argintregs[p], REG_SP, (10 + p) * 8);
 
                                } else {
-                                       M_FST(r->argfltregs[p], REG_SP, (10 + p) * 8);
-                                       M_ILD(r->argintregs[p], REG_SP, (10 + p) * 8);
+                                       M_FST(rd->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_ILD(rd->argintregs[p], REG_SP, (10 + p) * 8);
                                }
 
                        } else {
-                               M_DST(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                               M_DST(rd->argfltregs[p], REG_SP, (10 + p) * 8);
                        }
                }
 
@@ -3649,7 +3686,7 @@ u1 *createnativestub(functionptr f, methodinfo *m)
                M_LDA(REG_PV, REG_RA, disp);
 
                for (p = 0; p < m->paramcount && p < INT_ARG_CNT; p++) {
-                       M_LLD(r->argintregs[p], REG_SP,  (2 + p) * 8);
+                       M_LLD(rd->argintregs[p], REG_SP,  (2 + p) * 8);
                }
 
                for (p = 0; p < m->paramcount && p < FLT_ARG_CNT; p++) {
@@ -3657,14 +3694,14 @@ u1 *createnativestub(functionptr f, methodinfo *m)
 
                        if (IS_FLT_DBL_TYPE(t)) {
                                if (IS_2_WORD_TYPE(t)) {
-                                       M_DLD(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_DLD(rd->argfltregs[p], REG_SP, (10 + p) * 8);
 
                                } else {
-                                       M_FLD(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                                       M_FLD(rd->argfltregs[p], REG_SP, (10 + p) * 8);
                                }
 
                        } else {
-                               M_DLD(r->argfltregs[p], REG_SP, (10 + p) * 8);
+                               M_DLD(rd->argfltregs[p], REG_SP, (10 + p) * 8);
                        }
                }
 
@@ -3690,93 +3727,93 @@ u1 *createnativestub(functionptr f, methodinfo *m)
 
                if (m->flags & ACC_STATIC) {
                        if (IS_FLT_DBL_TYPE(m->paramtypes[5])) {
-                               M_DST(r->argfltregs[5], REG_SP, 1 * 8);
+                               M_DST(rd->argfltregs[5], REG_SP, 1 * 8);
                        } else {
-                               M_LST(r->argintregs[5], REG_SP, 1 * 8);
+                               M_LST(rd->argintregs[5], REG_SP, 1 * 8);
                        }
 
                        if (IS_FLT_DBL_TYPE(m->paramtypes[4])) {
-                               M_DST(r->argfltregs[4], REG_SP, 0 * 8);
+                               M_DST(rd->argfltregs[4], REG_SP, 0 * 8);
                        } else {
-                               M_LST(r->argintregs[4], REG_SP, 0 * 8);
+                               M_LST(rd->argintregs[4], REG_SP, 0 * 8);
                        }
 
                } else {
                        if (IS_FLT_DBL_TYPE(m->paramtypes[5])) {
-                               M_DST(r->argfltregs[5], REG_SP, 0 * 8);
+                               M_DST(rd->argfltregs[5], REG_SP, 0 * 8);
                        } else {
-                               M_LST(r->argintregs[5], REG_SP, 0 * 8);
+                               M_LST(rd->argintregs[5], REG_SP, 0 * 8);
                        }
                }
        }
 
        if (m->flags & ACC_STATIC) {
-               M_MOV(r->argintregs[5], r->argintregs[7]);
+               M_MOV(rd->argintregs[5], rd->argintregs[7]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[5]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[7]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[5]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[7]);
 
-               M_MOV(r->argintregs[4], r->argintregs[6]);
+               M_MOV(rd->argintregs[4], rd->argintregs[6]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[4]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[6]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[4]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[6]);
 
-               M_MOV(r->argintregs[3], r->argintregs[5]);
-               M_DMFC1(REG_ITMP1, r->argfltregs[3]);
+               M_MOV(rd->argintregs[3], rd->argintregs[5]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[3]);
 
-               M_MOV(r->argintregs[2], r->argintregs[4]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[5]);
+               M_MOV(rd->argintregs[2], rd->argintregs[4]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[5]);
 
-               M_MOV(r->argintregs[1], r->argintregs[3]);
-               M_DMFC1(REG_ITMP1, r->argfltregs[2]);
+               M_MOV(rd->argintregs[1], rd->argintregs[3]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[2]);
 
-               M_MOV(r->argintregs[0], r->argintregs[2]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[4]);
+               M_MOV(rd->argintregs[0], rd->argintregs[2]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[4]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[1]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[3]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[1]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[3]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[0]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[2]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[0]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[2]);
 
-               M_ALD(r->argintregs[1], REG_PV, -8 * 8);
+               M_ALD(rd->argintregs[1], REG_PV, -8 * 8);
 
        } else {
-               M_MOV(r->argintregs[6], r->argintregs[7]);
+               M_MOV(rd->argintregs[6], rd->argintregs[7]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[6]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[7]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[6]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[7]);
 
-               M_MOV(r->argintregs[5], r->argintregs[6]);
+               M_MOV(rd->argintregs[5], rd->argintregs[6]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[5]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[6]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[5]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[6]);
 
-               M_MOV(r->argintregs[4], r->argintregs[5]);
-               M_DMFC1(REG_ITMP1, r->argfltregs[4]);
+               M_MOV(rd->argintregs[4], rd->argintregs[5]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[4]);
 
-               M_MOV(r->argintregs[3], r->argintregs[4]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[5]);
+               M_MOV(rd->argintregs[3], rd->argintregs[4]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[5]);
 
-               M_MOV(r->argintregs[2], r->argintregs[3]);
-               M_DMFC1(REG_ITMP1, r->argfltregs[3]);
+               M_MOV(rd->argintregs[2], rd->argintregs[3]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[3]);
 
-               M_MOV(r->argintregs[1], r->argintregs[2]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[4]);
+               M_MOV(rd->argintregs[1], rd->argintregs[2]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[4]);
 
-               M_MOV(r->argintregs[0], r->argintregs[1]);
-               M_DMFC1(REG_ITMP1, r->argfltregs[2]);
+               M_MOV(rd->argintregs[0], rd->argintregs[1]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[2]);
 
-               M_DMTC1(REG_ITMP1, r->argfltregs[3]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[3]);
 
-               M_DMFC1(REG_ITMP1, r->argfltregs[1]);
-               M_DMFC1(REG_ITMP2, r->argfltregs[0]);
+               M_DMFC1(REG_ITMP1, rd->argfltregs[1]);
+               M_DMFC1(REG_ITMP2, rd->argfltregs[0]);
 
-               M_DMTC1(REG_ITMP1, r->argfltregs[2]);
-               M_DMTC1(REG_ITMP2, r->argfltregs[1]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[2]);
+               M_DMTC1(REG_ITMP2, rd->argfltregs[1]);
        }
 
-       M_ALD(r->argintregs[0], REG_PV, -4 * 8); /* jni environement              */
+       M_ALD(rd->argintregs[0], REG_PV, -4 * 8); /* jni environement              */
        M_ALD(REG_ITMP3, REG_PV, -1 * 8);   /* load adress of native method       */
        M_JSR(REG_RA, REG_ITMP3);           /* call native method                 */
        M_NOP;                              /* delay slot                         */
@@ -3792,11 +3829,11 @@ u1 *createnativestub(functionptr f, methodinfo *m)
                M_AST(REG_RA, REG_SP, 0 * 8);
                M_LST(REG_RESULT, REG_SP, 1 * 8);
                M_DST(REG_FRESULT, REG_SP, 2 * 8);
-               M_ALD(r->argintregs[0], REG_PV, -6 * 8);
-               M_MOV(REG_RESULT, r->argintregs[1]);
+               M_ALD(rd->argintregs[0], REG_PV, -6 * 8);
+               M_MOV(REG_RESULT, rd->argintregs[1]);
                M_DMFC1(REG_ITMP1, REG_FRESULT);
-               M_DMTC1(REG_ITMP1, r->argfltregs[2]);
-               M_DMTC1(REG_ITMP1, r->argfltregs[3]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[2]);
+               M_DMTC1(REG_ITMP1, rd->argfltregs[3]);
                M_ALD(REG_ITMP3, REG_PV, -7 * 8);/* builtin_displaymethodstop         */
                M_JSR(REG_RA, REG_ITMP3);
                M_NOP;
@@ -3813,15 +3850,21 @@ u1 *createnativestub(functionptr f, methodinfo *m)
        M_JSR(REG_RA, REG_ITMP3);
 
        /* delay slot */
-       if (IS_FLT_DBL_TYPE(m->returntype))
+       if (IS_FLT_DBL_TYPE(m->returntype)) {
                M_DST(REG_FRESULT, REG_SP, 1 * 8);
-       else
+
+       } else {
                M_AST(REG_RESULT, REG_SP, 1 * 8);
+       }
+
        M_MOV(REG_RESULT, REG_ITMP3);
-       if (IS_FLT_DBL_TYPE(m->returntype))
+
+       if (IS_FLT_DBL_TYPE(m->returntype)) {
                M_DLD(REG_FRESULT, REG_SP, 1 * 8);
-       else
+
+       } else {
                M_ALD(REG_RESULT, REG_SP, 1 * 8);
+       }
 #else
        M_ALD(REG_ITMP3, REG_PV, -2 * 8);   /* get address of exceptionptr        */
 #endif
@@ -3830,7 +3873,7 @@ u1 *createnativestub(functionptr f, methodinfo *m)
        M_ALD(REG_ITMP1, REG_ITMP3, 0);     /* load exception into reg. itmp1     */
 
        M_BNEZ(REG_ITMP1, 2);               /* if no exception then return        */
-       M_LDA(REG_SP, REG_SP, NATIVESTUBSTACK * 8);/*remove stackframe, delay slot*/
+       M_LDA(REG_SP, REG_SP, NATIVESTUB_STACK * 8);/*remove stackframe, delay slot*/
 
        M_RET(REG_RA);                      /* return to caller                   */
        M_NOP;                              /* delay slot                         */
@@ -3842,6 +3885,48 @@ u1 *createnativestub(functionptr f, methodinfo *m)
        M_LDA(REG_ITMP2, REG_RA, -4);       /* move fault address into reg. itmp2 */
                                            /* delay slot                         */
 
+       /* generate static stub call code                                         */
+
+       {
+               s4          *xcodeptr;
+               clinitref   *cref;
+               s4          *tmpmcodeptr;
+
+               /* there can only be one clinit ref entry                             */
+               cref = cd->clinitrefs;
+
+               if (cref) {
+                       /* Get machine code which is patched back in later. The call is   */
+                       /* 2 instruction words long.                                      */
+                       xcodeptr = (s4 *) (cd->mcodebase + cref->branchpos);
+
+                       /* We need to split this, because an unaligned 8 byte read causes */
+                       /* a SIGSEGV.                                                     */
+                       *(cs-10) = ((u8) xcodeptr[1] << 32) + (u4) xcodeptr[0];
+
+                       /* patch in the call to call the following code (done at compile  */
+                       /* time)                                                          */
+
+                       tmpmcodeptr = mcodeptr;         /* save current mcodeptr          */
+                       mcodeptr = xcodeptr;            /* set mcodeptr to patch position */
+
+                       M_BRS(tmpmcodeptr - (xcodeptr + 1));
+                       M_NOP;
+
+                       mcodeptr = tmpmcodeptr;         /* restore the current mcodeptr   */
+
+                       /* move class pointer into REG_ITMP1                              */
+                       M_ALD(REG_ITMP1, REG_PV, -8 * 8);     /* class                    */
+
+                       /* move machine code into REG_ITMP2                               */
+                       M_LLD(REG_ITMP2, REG_PV, -10 * 8);    /* machine code             */
+
+                       M_ALD(REG_ITMP3, REG_PV, -9 * 8);     /* asm_check_clinit         */
+                       M_JMP(REG_ITMP3);
+                       M_NOP;
+               }
+       }
+
        (void) docacheflush((void*) cs, (char*) mcodeptr - (char*) cs);
 
 #if 0
@@ -3849,11 +3934,16 @@ u1 *createnativestub(functionptr f, methodinfo *m)
                                (int) (mcodeptr - (s4*) s), m->paramcount);
 #endif
 
-#ifdef STATISTICS
-       count_nstub_len += NATIVESTUBSIZE * 8;
+#if defined(STATISTICS)
+       if (opt_stat)
+               count_nstub_len += NATIVESTUB_SIZE * 8;
 #endif
 
-       return (u1 *) (s + NATIVESTUBOFFSET);
+       /* release dump area */
+
+       dump_release(dumpsize);
+       
+       return (u1 *) (s + NATIVESTUB_OFFSET);
 }
 
 
@@ -3865,7 +3955,7 @@ u1 *createnativestub(functionptr f, methodinfo *m)
 
 void removenativestub(u1 *stub)
 {
-       CFREE((u8*) stub - NATIVESTUBOFFSET, NATIVESTUBSIZE * 8);
+       CFREE((u8 *) stub - NATIVESTUB_OFFSET, NATIVESTUB_SIZE * 8);
 }