GNU header update.
[cacao.git] / src / vm / jit / powerpc / codegen.c
index b9742b5dd06a6bedacefb1f028f89adfa5d11037..b4a05341a478d8051713efc318c4f9a5aa7da2f9 100644 (file)
@@ -1,10 +1,9 @@
-/* jit/powerpc/codegen.c - machine code generator for 32-bit powerpc
+/* vm/jit/powerpc/codegen.c - machine code generator for 32-bit powerpc
 
-   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.
 
    Authors: Andreas Krall
             Stefan Ring
 
-   $Id: codegen.c 609 2003-11-12 01:18:53Z twisti $
+   $Id: codegen.c 1735 2004-12-07 14:33:27Z twisti $
 
 */
 
-
 #include <stdio.h>
 #include <signal.h>
-#include "types.h"
-#include "codegen.h"
-#include "jit.h"
-#include "reg.h"
-#include "builtin.h"
-#include "asmpart.h"
-#include "jni.h"
-#include "loader.h"
-#include "tables.h"
-#include "native.h"
-
-/* include independent code generation stuff */
-#include "codegen.inc"
-#include "reg.inc"
+
+#include "config.h"
+#include "native/native.h"
+#include "vm/builtin.h"
+#include "vm/global.h"
+#include "vm/loader.h"
+#include "vm/tables.h"
+#include "vm/jit/asmpart.h"
+#include "vm/jit/jit.h"
+#include "vm/jit/parse.h"
+#include "vm/jit/reg.h"
+#include "vm/jit/powerpc/arch.h"
+#include "vm/jit/powerpc/codegen.h"
+#include "vm/jit/powerpc/types.h"
 
 
 /* register descripton - array ************************************************/
@@ -62,7 +60,7 @@
 
 /* #define REG_END   -1        last entry in tables */
  
-int nregdescint[] = {
+static int nregdescint[] = {
        REG_RES, REG_RES, REG_TMP, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, 
        REG_ARG, REG_ARG, REG_ARG, REG_RES, REG_RES, REG_RES, REG_SAV, REG_SAV, 
        REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
@@ -71,256 +69,87 @@ int nregdescint[] = {
 
 /* for use of reserved registers, see comment above */
        
-int nregdescfloat[] = {
+static int nregdescfloat[] = {
        REG_RES, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG,
        REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_SAV, REG_SAV, 
        REG_RES, REG_RES, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP,
        REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
        REG_END };
 
-/* for use of reserved registers, see comment above */
-
-
-/* stackframe-infos ***********************************************************/
-
-int parentargs_base; /* offset in stackframe for the parameter from the caller*/
-
-/* -> see file 'calling.doc' */
-
 
-/* additional functions and macros to generate code ***************************/
+/* Include independent code generation stuff -- include after register        */
+/* descriptions to avoid extern definitions.                                  */
 
-#define BlockPtrOfPC(pc)  ((basicblock *) iptr->target)
-
-
-#ifdef STATISTICS
-#define COUNT_SPILLS count_spills++
-#else
-#define COUNT_SPILLS
+#include "vm/jit/codegen.inc"
+#include "vm/jit/reg.inc"
+#ifdef LSRA
+#include "vm/jit/lsra.inc"
 #endif
 
 
-/* gen_nullptr_check(objreg) */
-
-#ifdef SOFTNULLPTRCHECK
-#define gen_nullptr_check(objreg) \
-       if (checknull) {\
-               M_TST((objreg));\
-               M_BEQ(0);\
-               codegen_addxnullrefs(mcodeptr);\
-       }
-#else
-#define gen_nullptr_check(objreg)
-#endif
+void asm_cacheflush(void *, long);
 
+/* #include <architecture/ppc/cframe.h> */
 
-/* MCODECHECK(icnt) */
-
-#define MCODECHECK(icnt) \
-       if((mcodeptr+(icnt))>mcodeend)mcodeptr=codegen_increase((u1*)mcodeptr)
-
-/* M_INTMOVE:
-     generates an integer-move from register a to b.
-     if a and b are the same int-register, no code will be generated.
-*/ 
-
-#define M_INTMOVE(a,b) if((a)!=(b)){M_MOV(a,b);}
-
-#define M_TINTMOVE(t,a,b) \
-       if ((t)==TYPE_LNG) \
-               {if (a<=b)M_INTMOVE(secondregs[a],secondregs[b]);\
-               M_INTMOVE(a,b); if (a>b)M_INTMOVE(secondregs[a],secondregs[b]);} \
-       else \
-               M_INTMOVE(a,b);
-
-
-/* M_FLTMOVE:
-    generates a floating-point-move from register a to b.
-    if a and b are the same float-register, no code will be generated
-*/ 
-
-#define M_FLTMOVE(a,b) if((a)!=(b)){M_FMOV(a,b);}
-
-
-/* var_to_reg_xxx:
-    this function generates code to fetch data from a pseudo-register
-    into a real register. 
-    If the pseudo-register has actually been assigned to a real 
-    register, no code will be emitted, since following operations
-    can use this register directly.
-    
-    v: pseudoregister to be fetched from
-    tempregnum: temporary register to be used if v is actually spilled to ram
-
-    return: the register number, where the operand can be found after 
-            fetching (this wil be either tempregnum or the register
-            number allready given to v)
-*/
-
-#define var_to_reg_int0(regnr,v,tempnr,a,b) { \
-       if ((v)->flags & INMEMORY) \
-               {COUNT_SPILLS;if (a) M_ILD(tempnr,REG_SP,4*(v)->regoff); \
-               regnr=tempnr; \
-               if ((b) && IS_2_WORD_TYPE((v)->type)) \
-                       M_ILD((a)?secondregs[tempnr]:tempnr,REG_SP,4*(v)->regoff+4);} \
-       else regnr=(!(a)&&(b)) ? secondregs[(v)->regoff] : (v)->regoff; \
-}
-#define var_to_reg_int(regnr,v,tempnr) var_to_reg_int0(regnr,v,tempnr,1,1)
-
-
-#define var_to_reg_flt(regnr,v,tempnr) { \
-       if ((v)->flags & INMEMORY) { \
-               COUNT_SPILLS; \
-               if ((v)->type==TYPE_DBL) \
-                       M_DLD(tempnr,REG_SP,4*(v)->regoff); \
-               else \
-                       M_FLD(tempnr,REG_SP,4*(v)->regoff); \
-               regnr=tempnr; \
-       } else regnr=(v)->regoff; \
-}
-
-
-/* reg_of_var:
-    This function determines a register, to which the result of an operation
-    should go, when it is ultimatively intended to store the result in
-    pseudoregister v.
-    If v is assigned to an actual register, this register will be returned.
-    Otherwise (when v is spilled) this function returns tempregnum.
-    If not already done, regoff and flags are set in the stack location.
-*/        
-
-static int reg_of_var(stackptr v, int tempregnum)
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+void thread_restartcriticalsection(void *u)
 {
-       varinfo      *var;
-
-       switch (v->varkind) {
-               case TEMPVAR:
-                       if (!(v->flags & INMEMORY))
-                               return(v->regoff);
-                       break;
-               case STACKVAR:
-                       var = &(interfaces[v->varnum][v->type]);
-                       v->regoff = var->regoff;
-                       if (!(var->flags & INMEMORY))
-                               return(var->regoff);
-                       break;
-               case LOCALVAR:
-                       var = &(locals[v->varnum][v->type]);
-                       v->regoff = var->regoff;
-                       if (!(var->flags & INMEMORY))
-                               return(var->regoff);
-                       break;
-               case ARGVAR:
-                       v->regoff = v->varnum;
-                       if (IS_FLT_DBL_TYPE(v->type)) {
-                               if (v->varnum < fltreg_argnum) {
-                                       v->regoff = argfltregs[v->varnum];
-                                       return(argfltregs[v->varnum]);
-                                       }
-                               }
-                       else
-                               if (v->varnum < intreg_argnum - (IS_2_WORD_TYPE(v->type)!=0)) {
-                                       v->regoff = argintregs[v->varnum];
-                                       return(argintregs[v->varnum]);
-                                       }
-                       v->regoff += 6;
-                       break;
-               }
-       v->flags |= INMEMORY;
-       return tempregnum;
+       /* XXX set pc to restart address */
 }
+#endif
 
+#include <mach/message.h>
 
-/* store_reg_to_var_xxx:
-    This function generates the code to store the result of an operation
-    back into a spilled pseudo-variable.
-    If the pseudo-variable has not been spilled in the first place, this 
-    function will generate nothing.
-    
-    v ............ Pseudovariable
-    tempregnum ... Number of the temporary registers as returned by
-                   reg_of_var.
-*/     
-
-#define store_reg_to_var_int0(sptr, tempregnum, a, b) {       \
-       if ((sptr)->flags & INMEMORY) {                    \
-               COUNT_SPILLS;                                  \
-               if (a) M_IST(tempregnum, REG_SP, 4 * (sptr)->regoff); \
-               if ((b) && IS_2_WORD_TYPE((sptr)->type)) \
-                       M_IST(secondregs[tempregnum], REG_SP, 4 * (sptr)->regoff + 4); \
-               }                                              \
-       }
-
-#define store_reg_to_var_int(sptr, tempregnum) \
-       store_reg_to_var_int0(sptr, tempregnum, 1, 1)
-
-#define store_reg_to_var_flt(sptr, tempregnum) {       \
-       if ((sptr)->flags & INMEMORY) {                    \
-               COUNT_SPILLS;                                  \
-               if ((sptr)->type==TYPE_DBL) \
-                       M_DST(tempregnum, REG_SP, 4 * (sptr)->regoff); \
-               else \
-                       M_FST(tempregnum, REG_SP, 4 * (sptr)->regoff); \
-               }                                              \
-       }
-
-
-//#include <architecture/ppc/cframe.h>
-
-/* NullPointerException signal handler for hardware null pointer check */
-
-void asm_sighandler();
+int cacao_catch_Handler(mach_port_t thread)
+{
+       unsigned int *regs;
+       unsigned int crashpc;
+       s4 instr, reg;
+       java_objectheader *xptr;
+
+       /* Mach stuff */
+       thread_state_flavor_t flavor = PPC_THREAD_STATE;
+       mach_msg_type_number_t thread_state_count = PPC_THREAD_STATE_COUNT;
+       ppc_thread_state_t thread_state;
+       kern_return_t r;
+       
+       if (checknull)
+               return 0;
 
-int lastsig;
-int crashpc;
+       r = thread_get_state(thread, flavor,
+               (natural_t*)&thread_state, &thread_state_count);
+       if (r != KERN_SUCCESS)
+               panic("thread_get_state failed");
 
-int catch_Handler(int *regs)
-{
-       sigset_t nsig;
-       int instr, reg;
+       regs = &thread_state.r0;
+       crashpc = thread_state.srr0;
 
-       instr = *(int *) crashpc;
-       reg = (instr>>16) & 31;
+       instr = *(s4*) crashpc;
+       reg = (instr >> 16) & 31;
 
        if (!regs[reg]) {
-               sigemptyset(&nsig);
-               sigaddset(&nsig, lastsig);
-               sigprocmask(SIG_UNBLOCK, &nsig, NULL);           /* unblock signal    */
+               xptr = new_nullpointerexception();
 
                regs[REG_ITMP2_XPC] = crashpc;
-               regs[REG_ITMP1_XPTR] = (int) proto_java_lang_NullPointerException;
+               regs[REG_ITMP1_XPTR] = (u4) xptr;
+               thread_state.srr0 = (u4) asm_handle_exception;
 
-               return 0;
-       }
+               r = thread_set_state(thread, flavor,
+                       (natural_t*)&thread_state, thread_state_count);
+               if (r != KERN_SUCCESS)
+                       panic("thread_set_state failed");
 
-       panic("segfault");
-}
+               return 1;
+       }
 
-void catch_NullPointerException(int sig, void *code, struct sigcontext *ctx)
-{
-       int oldsp = ctx->sc_sp;
+       throw_cacao_exception_exit(string_java_lang_InternalError, "Segmentation fault at %p", regs[reg]);
 
-       lastsig = sig;
-       crashpc = ctx->sc_ir;
-       ctx->sc_ir = (int) asm_sighandler;
+       return 0;
 }
 
-
 void init_exceptions(void)
 {
-       /* install signal handlers we need to convert to exceptions */
-
-       if (!checknull) {
-
-#if defined(SIGSEGV)
-               signal(SIGSEGV, (void*) catch_NullPointerException);
-#endif
-
-#if defined(SIGBUS)
-               signal(SIGBUS, (void*) catch_NullPointerException);
-#endif
-
-               }
+       GC_dirty_init(1);
 }
 
 void adjust_argvars(stackptr s, int d, int *fa, int *ia)
@@ -336,16 +165,10 @@ void adjust_argvars(stackptr s, int d, int *fa, int *ia)
        *ia += (IS_2_WORD_TYPE(s->type)) ? 2 : 1;
 }
 
-void nocode()
-{
-       printf("NOCODE\n");
-}
-
-
 
 #define intmaxf(a,b) (((a)<(b)) ? (b) : (a))
 
-void preregpass()
+void preregpass(methodinfo *m, registerdata *rd)
 {
        int paramsize;
        stackptr    src;
@@ -353,8 +176,9 @@ void preregpass()
        instruction *iptr;
        int s3, len;
 
-       ifmemuse = 0;
-       for (bptr = block; bptr != NULL; bptr = bptr->next) {
+       rd->ifmemuse = 0;
+
+       for (bptr = m->basicblocks; bptr != NULL; bptr = bptr->next) {
                len = bptr->icount;
                for (iptr = bptr->iinstr, src = bptr->instack;
                    len > 0;
@@ -386,84 +210,71 @@ countparams:
                                        paramsize = 0;
                                        for (; --s3 >= 0; src = src->prev) {
                                                paramsize += IS_2_WORD_TYPE(src->type) ? 2 : 1;
-                                               }
-                                       ifmemuse = intmaxf(ifmemuse, paramsize);
+                                       }
+                                       rd->ifmemuse = intmaxf(rd->ifmemuse, paramsize);
                                        break;
 
                                case ICMD_MULTIANEWARRAY:
                                        s3 = iptr->op1;
-                                       paramsize = intreg_argnum + s3;
-                                       ifmemuse = intmaxf(ifmemuse, paramsize);
+                                       paramsize = rd->intreg_argnum + s3;
+                                       rd->ifmemuse = intmaxf(rd->ifmemuse, paramsize);
                                        break;
                        }
                }
        }
 
-       ifmemuse += 6;
-       maxmemuse = ifmemuse;
+       rd->ifmemuse += 6;
+       rd->maxmemuse = rd->ifmemuse;
 }
 
+
 /* function gen_mcode **********************************************************
 
        generates machine code
 
 *******************************************************************************/
 
-#define        MethodPointer   -8
-#define        FrameSize       -12
-#define     IsSync          -16
-#define     IsLeaf          -20
-#define     IntSave         -24
-#define     FltSave         -28
-#define     ExTableSize     -32
-#define     ExTableStart    -32
-
-#define     ExEntrySize     -16
-#define     ExStartPC       -4
-#define     ExEndPC         -8
-#define     ExHandlerPC     -12
-#define     ExCatchType     -16
-
-
-void codegen()
+void codegen(methodinfo *m, codegendata *cd, registerdata *rd)
 {
-       int  len, s1, s2, s3, d;
-       s4   a;
+       s4 len, s1, s2, s3, d;
+       s4 a;
+       s4 parentargs_base;
        s4          *mcodeptr;
        stackptr    src;
        varinfo     *var;
        basicblock  *bptr;
        instruction *iptr;
-       xtable *ex;
+       exceptiontable *ex;
 
        {
-       int p, pa, t, l, r;
+       s4 i, p, pa, t, l;
+       s4 savedregs_num;
 
        savedregs_num = 0;
 
        /* space to save used callee saved registers */
 
-       savedregs_num += (savintregcnt - maxsavintreguse);
-       savedregs_num += 2*(savfltregcnt - maxsavfltreguse);
+       savedregs_num += (rd->savintregcnt - rd->maxsavintreguse);
+       savedregs_num += 2 * (rd->savfltregcnt - rd->maxsavfltreguse);
 
-       parentargs_base = maxmemuse + savedregs_num;
+       parentargs_base = rd->maxmemuse + savedregs_num;
 
 #ifdef USE_THREADS                 /* space to save argument of monitor_enter */
 
-       if (checksync && (method->flags & ACC_SYNCHRONIZED))
+       if (checksync && (m->flags & ACC_SYNCHRONIZED))
                parentargs_base++;
 
 #endif
 
        /* create method header */
 
-       parentargs_base = (parentargs_base+3) & ~3;
+       parentargs_base = (parentargs_base + 3) & ~3;
 
-#if POINTERSIZE==4
-       (void) dseg_addaddress(method);                         /* Filler         */
+#if POINTERSIZE == 4
+       (void) dseg_addaddress(cd, m);                          /* Filler         */
 #endif
-       (void) dseg_addaddress(method);                         /* MethodPointer  */
-       (void) dseg_adds4(parentargs_base * 4);                 /* FrameSize      */
+       (void) dseg_addaddress(cd, m);                          /* MethodPointer  */
+       (void) dseg_adds4(cd, parentargs_base * 4);             /* FrameSize      */
 
 #ifdef USE_THREADS
 
@@ -473,258 +284,292 @@ void codegen()
           offset by one.
        */
 
-       if (checksync && (method->flags & ACC_SYNCHRONIZED))
-               (void) dseg_adds4((maxmemuse + 1) * 4);             /* IsSync         */
+       if (checksync && (m->flags & ACC_SYNCHRONIZED))
+               (void) dseg_adds4(cd, (rd->maxmemuse + 1) * 4);     /* IsSync         */
        else
 
 #endif
 
-       (void) dseg_adds4(0);                                   /* IsSync         */
+               (void) dseg_adds4(cd, 0);                           /* IsSync         */
                                               
-       (void) dseg_adds4(isleafmethod);                        /* IsLeaf         */
-       (void) dseg_adds4(savintregcnt - maxsavintreguse);      /* IntSave        */
-       (void) dseg_adds4(savfltregcnt - maxsavfltreguse);      /* FltSave        */
-       (void) dseg_adds4(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 = extable; ex != NULL; ex = ex->down) {
-
-#ifdef LOOP_DEBUG      
-               if (ex->start != NULL)
-                       printf("adding start - %d - ", ex->start->debug_nr);
-               else {
-                       printf("PANIC - start is NULL");
-                       exit(-1);
-               }
-#endif
-
-               dseg_addtarget(ex->start);
-
-#ifdef LOOP_DEBUG                      
-               if (ex->end != NULL)
-                       printf("adding end - %d - ", ex->end->debug_nr);
-               else {
-                       printf("PANIC - end is NULL");
-                       exit(-1);
-               }
-#endif
-
-               dseg_addtarget(ex->end);
-
-#ifdef LOOP_DEBUG              
-               if (ex->handler != NULL)
-                       printf("adding handler - %d\n", ex->handler->debug_nr);
-               else {
-                       printf("PANIC - handler is NULL");
-                       exit(-1);
-               }
-#endif
-
-               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);
-       MCODECHECK(128 + mparamcount);
+       mcodeptr = (s4 *) cd->mcodebase;
+       cd->mcodeend = (s4 *) (cd->mcodebase + cd->mcodesize);
+       MCODECHECK(128 + m->paramcount);
 
        /* create stack frame (if necessary) */
 
-       if (!isleafmethod) {
+       if (!m->isleafmethod) {
                M_MFLR(REG_ITMP3);
                M_AST(REG_ITMP3, REG_SP, 8);
        }
-       if (parentargs_base)
-               {M_LDA (REG_SP, REG_SP, -parentargs_base * 4);}
+
+       if (parentargs_base) {
+               M_STWU(REG_SP, REG_SP, -parentargs_base * 4);
+       }
 
        /* save return address and used callee saved registers */
 
        p = parentargs_base;
-       for (r = savintregcnt - 1; r >= maxsavintreguse; r--)
-               {p--; M_IST (savintregs[r], REG_SP, 4 * p);}
-       for (r = savfltregcnt - 1; r >= maxsavfltreguse; r--)
-               {p-=2; M_DST (savfltregs[r], REG_SP, 4 * p);}
+       for (i = rd->savintregcnt - 1; i >= rd->maxsavintreguse; i--) {
+               p--; M_IST(rd->savintregs[i], REG_SP, 4 * p);
+       }
+       for (i = rd->savfltregcnt - 1; i >= rd->maxsavfltreguse; i--) {
+               p-=2; M_DST(rd->savfltregs[i], REG_SP, 4 * p);
+       }
 
        /* save monitorenter argument */
 
-#ifdef USE_THREADS
-       if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
-               if (method->flags & ACC_STATIC) {
-                       p = dseg_addaddress (class);
+#if defined(USE_THREADS)
+       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+               if (m->flags & ACC_STATIC) {
+                       p = dseg_addaddress(cd, m->class);
                        M_ALD(REG_ITMP1, REG_PV, p);
-                       M_AST(REG_ITMP1, REG_SP, 4 * maxmemuse);
-                       } 
-               else {
-                       M_AST (argintregs[0], REG_SP, 4 * maxmemuse);
-                       }
-               }                       
+                       M_AST(REG_ITMP1, REG_SP, 4 * rd->maxmemuse);
+
+               else {
+                       M_AST(rd->argintregs[0], REG_SP, 4 * rd->maxmemuse);
+               }
+       }                       
 #endif
 
        /* copy argument registers to stack and call trace function with pointer
-          to arguments on stack. ToDo: save floating point registers !!!!!!!!!
+          to arguments on stack.
        */
 
-#if 1
        if (runverbose) {
+               s4 longargs = 0;
+               s4 fltargs = 0;
+               s4 dblargs = 0;
+
                M_MFLR(REG_ITMP3);
-               M_LDA (REG_SP, REG_SP, -208);
-
-               M_IST(REG_ITMP3, REG_SP, 13*8 + 96);
-               M_IST(argintregs[0], REG_SP, 0*4 + 64);
-               M_IST(argintregs[1], REG_SP, 1*4 + 64);
-               M_IST(argintregs[2], REG_SP, 2*4 + 64);
-               M_IST(argintregs[3], REG_SP, 3*4 + 64);
-               M_IST(argintregs[4], REG_SP, 4*4 + 64);
-               M_IST(argintregs[5], REG_SP, 5*4 + 64);
-               M_IST(argintregs[6], REG_SP, 6*4 + 64);
-               M_IST(argintregs[7], REG_SP, 7*4 + 64);
-
-               M_DST(argfltregs[0], REG_SP, 0*8 + 96);
-               M_DST(argfltregs[1], REG_SP, 1*8 + 96);
-               M_DST(argfltregs[2], REG_SP, 2*8 + 96);
-               M_DST(argfltregs[3], REG_SP, 3*8 + 96);
-               M_DST(argfltregs[4], REG_SP, 4*8 + 96);
-               M_DST(argfltregs[5], REG_SP, 5*8 + 96);
-               M_DST(argfltregs[6], REG_SP, 6*8 + 96);
-               M_DST(argfltregs[7], REG_SP, 7*8 + 96);
-               M_DST(argfltregs[8], REG_SP, 8*8 + 96);
-               M_DST(argfltregs[9], REG_SP, 9*8 + 96);
-               M_DST(argfltregs[10], REG_SP, 10*8 + 96);
-               M_DST(argfltregs[11], REG_SP, 11*8 + 96);
-               M_DST(argfltregs[12], REG_SP, 12*8 + 96);
-
-               p = dseg_addaddress (method);
+               /* XXX must be a multiple of 16 */
+               M_LDA(REG_SP, REG_SP, -(24 + (INT_ARG_CNT + FLT_ARG_CNT + 1) * 8));
+
+               M_IST(REG_ITMP3, REG_SP, 24 + (INT_ARG_CNT + FLT_ARG_CNT) * 8);
+
+               M_CLR(REG_ITMP1);    /* clear help register */
+
+               /* save all arguments into the reserved stack space */
+               for (p = 0; p < m->paramcount && p < TRACE_ARGS_NUM; p++) {
+                       t = m->paramtypes[p];
+
+                       if (IS_INT_LNG_TYPE(t)) {
+                               /* overlapping u8's are on the stack */
+                               if ((p + longargs + dblargs) < (INT_ARG_CNT - IS_2_WORD_TYPE(t))) {
+                                       s1 = rd->argintregs[p + longargs + dblargs];
+
+                                       if (!IS_2_WORD_TYPE(t)) {
+                                               M_IST(REG_ITMP1, REG_SP, 24 + p * 8);
+                                               M_IST(s1, REG_SP, 24 + p * 8 + 4);
+
+                                       } else {
+                                               M_IST(s1, REG_SP, 24 + p  * 8);
+                                               M_IST(rd->secondregs[s1], REG_SP, 24 + p * 8 + 4);
+                                               longargs++;
+                                       }
+
+                               } else {
+                    a = dseg_adds4(cd, 0xdeadbeef);
+                    M_ILD(REG_ITMP1, REG_PV, a);
+                    M_IST(REG_ITMP1, REG_SP, 24 + p * 8);
+                    M_IST(REG_ITMP1, REG_SP, 24 + p * 8 + 4);
+                               }
+
+                       } else {
+                               if ((fltargs + dblargs) < FLT_ARG_CNT) {
+                                       s1 = rd->argfltregs[fltargs + dblargs];
+
+                                       if (!IS_2_WORD_TYPE(t)) {
+                                               M_IST(REG_ITMP1, REG_SP, 24 + p * 8);
+                                               M_FST(s1, REG_SP, 24 + p * 8 + 4);
+                                               fltargs++;
+                                               
+                                       } else {
+                                               M_DST(s1, REG_SP, 24 + p * 8);
+                                               dblargs++;
+                                       }
+
+                               } else {
+                                       /* this should not happen */
+                               }
+                       }
+               }
+
+               /* TODO: save remaining integer and flaot argument registers */
+
+               /* load first 4 arguments into integer argument registers */
+               for (p = 0; p < 8; p++) {
+                       d = rd->argintregs[p];
+                       M_ILD(d, REG_SP, 24 + p * 4);
+               }
+
+               p = dseg_addaddress(cd, m);
                M_ALD(REG_ITMP1, REG_PV, p);
-               M_AST(REG_ITMP1, REG_SP, 56);
-               p = dseg_addaddress ((void*) (builtin_trace_args));
+               M_AST(REG_ITMP1, REG_SP, 11 * 8);    /* 24 (linkage area) + 32 (4 * s8 parameter area regs) + 32 (4 * s8 parameter area stack) = 88 */
+               p = dseg_addaddress(cd, (void *) builtin_trace_args);
                M_ALD(REG_ITMP2, REG_PV, p);
                M_MTCTR(REG_ITMP2);
                M_JSR;
 
-               M_ILD(REG_ITMP3, REG_SP, 13*8 + 96);
-               M_ILD(argintregs[0], REG_SP, 0*4 + 64);
-               M_ILD(argintregs[1], REG_SP, 1*4 + 64);
-               M_ILD(argintregs[2], REG_SP, 2*4 + 64);
-               M_ILD(argintregs[3], REG_SP, 3*4 + 64);
-               M_ILD(argintregs[4], REG_SP, 4*4 + 64);
-               M_ILD(argintregs[5], REG_SP, 5*4 + 64);
-               M_ILD(argintregs[6], REG_SP, 6*4 + 64);
-               M_ILD(argintregs[7], REG_SP, 7*4 + 64);
-
-               M_DLD(argfltregs[0], REG_SP, 0*8 + 96);
-               M_DLD(argfltregs[1], REG_SP, 1*8 + 96);
-               M_DLD(argfltregs[2], REG_SP, 2*8 + 96);
-               M_DLD(argfltregs[3], REG_SP, 3*8 + 96);
-               M_DLD(argfltregs[4], REG_SP, 4*8 + 96);
-               M_DLD(argfltregs[5], REG_SP, 5*8 + 96);
-               M_DLD(argfltregs[6], REG_SP, 6*8 + 96);
-               M_DLD(argfltregs[7], REG_SP, 7*8 + 96);
-               M_DLD(argfltregs[8], REG_SP, 8*8 + 96);
-               M_DLD(argfltregs[9], REG_SP, 9*8 + 96);
-               M_DLD(argfltregs[10], REG_SP, 10*8 + 96);
-               M_DLD(argfltregs[11], REG_SP, 11*8 + 96);
-               M_DLD(argfltregs[12], REG_SP, 12*8 + 96);
-
-               M_LDA (REG_SP, REG_SP, 208);
+               longargs = 0;
+               fltargs = 0;
+               dblargs = 0;
+
+        /* restore arguments into the reserved stack space */
+        for (p = 0; p < m->paramcount && p < TRACE_ARGS_NUM; p++) {
+            t = m->paramtypes[p];
+
+            if (IS_INT_LNG_TYPE(t)) {
+                if ((p + longargs + dblargs) < INT_ARG_CNT) {
+                    s1 = rd->argintregs[p + longargs + dblargs];
+
+                                       if (!IS_2_WORD_TYPE(t)) {
+                                               M_ILD(s1, REG_SP, 24 + p * 8 + 4);
+
+                                       } else {
+                                               M_ILD(s1, REG_SP, 24 + p  * 8);
+                                               M_ILD(rd->secondregs[s1], REG_SP, 24 + p * 8 + 4);
+                                               longargs++;
+                                       }
+                               }
+
+            } else {
+                if ((fltargs + dblargs) < FLT_ARG_CNT) {
+                    s1 = rd->argfltregs[fltargs + dblargs];
+
+                                       if (!IS_2_WORD_TYPE(t)) {
+                                               M_FLD(s1, REG_SP, 24 + p * 8 + 4);
+                                               fltargs++;
+
+                                       } else {
+                                               M_DLD(s1, REG_SP, 24 + p * 8);
+                                               dblargs++;
+                                       }
+                               }
+            }
+        }
+
+               M_ILD(REG_ITMP3, REG_SP, 24 + (INT_ARG_CNT + FLT_ARG_CNT) * 8);
+
+               M_LDA(REG_SP, REG_SP, 24 + (INT_ARG_CNT + FLT_ARG_CNT + 1) * 8);
                M_MTLR(REG_ITMP3);
-               }
-#endif
+       }
 
        /* take arguments out of register or stack frame */
 
        {
                int narg=0, niarg=0;
                int arg, iarg;
-       for (p = 0, l = 0; p < mparamcount; p++) {
+
+       for (p = 0, l = 0; p < m->paramcount; p++) {
                arg = narg; iarg = niarg;
-               t = mparamtypes[p];
-               var = &(locals[l][t]);
+               t = m->paramtypes[p];
+               var = &(rd->locals[l][t]);
                l++, niarg++;
                if (IS_2_WORD_TYPE(t))    /* increment local counter for 2 word types */
                        l++, niarg++;
                if (var->type < 0)
                        continue;
-               r = var->regoff; 
                if (IS_INT_LNG_TYPE(t)) {                    /* integer args          */
                        if (iarg < INT_ARG_CNT -
                                        (IS_2_WORD_TYPE(t)!=0)) {        /* register arguments    */
-                               if (!(var->flags & INMEMORY))        /* reg arg -> register   */
-                                       {M_TINTMOVE (t, argintregs[iarg], r);}
-                               else                                 /* reg arg -> spilled    */
-                                       {
-                                               M_IST (argintregs[iarg], REG_SP, 4 * r);
-                                               if (IS_2_WORD_TYPE(t))
-                                                       M_IST (secondregs[argintregs[iarg]], REG_SP, 4 * r + 4);
-                                       }
-                               }
-                       else {                                   /* stack arguments       */
+                               if (!(var->flags & INMEMORY)) {      /* reg arg -> register   */
+                                       M_TINTMOVE(t, rd->argintregs[iarg], var->regoff);
+
+                               } else {                             /* reg arg -> spilled    */
+                                       M_IST(rd->argintregs[iarg], REG_SP, 4 * var->regoff);
+                                       if (IS_2_WORD_TYPE(t))
+                                               M_IST(rd->secondregs[rd->argintregs[iarg]], REG_SP, 4 * var->regoff + 4);
+                               }
+
+                       } else {                                 /* stack arguments       */
                                pa = iarg + 6;
-                               if (!(var->flags & INMEMORY))        /* stack arg -> register */ 
-                                       {
-                                               M_ILD (r, REG_SP, 4 * (parentargs_base + pa));
-                                               if (IS_2_WORD_TYPE(t))
-                                                       M_ILD (secondregs[r], REG_SP, 4 * (parentargs_base + pa) + 4);
-                                       }
-                               else {                               /* stack arg -> spilled  */
-                                       M_ILD (REG_ITMP1, REG_SP, 4 * (parentargs_base + pa));
-                                       M_IST (REG_ITMP1, REG_SP, 4 * r);
+                               if (!(var->flags & INMEMORY)) {      /* stack arg -> register */ 
+                                       M_ILD(var->regoff, REG_SP, 4 * (parentargs_base + pa));
+                                       if (IS_2_WORD_TYPE(t))
+                                               M_ILD(rd->secondregs[var->regoff], REG_SP, 4 * (parentargs_base + pa) + 4);
+
+                               } else {                              /* stack arg -> spilled  */
+                                       M_ILD(REG_ITMP1, REG_SP, 4 * (parentargs_base + pa));
+                                       M_IST(REG_ITMP1, REG_SP, 4 * var->regoff);
                                        if (IS_2_WORD_TYPE(t)) {
-                                               M_ILD (REG_ITMP1, REG_SP, 4 * (parentargs_base + pa) + 4);
-                                               M_IST (REG_ITMP1, REG_SP, 4 * r + 4);
-                                               }
-                                       }
-                               }
-                       }
-               else {                                       /* floating args         */   
+                                               M_ILD(REG_ITMP1, REG_SP, 4 * (parentargs_base + pa) + 4);
+                                               M_IST(REG_ITMP1, REG_SP, 4 * var->regoff + 4);
+                                       }
+                               }
+                       }
+
+               } else {                                     /* floating args         */   
                        ++narg;
                        if (arg < FLT_ARG_CNT) {                 /* register arguments    */
-                               if (!(var->flags & INMEMORY))        /* reg arg -> register   */
-                                       {M_FLTMOVE (argfltregs[arg], r);}
-                               else                                             /* reg arg -> spilled    */
-                                       M_DST (argfltregs[arg], REG_SP, 4 * r);
+                               if (!(var->flags & INMEMORY)) {      /* reg arg -> register   */
+                                       M_FLTMOVE(rd->argfltregs[arg], var->regoff);
+
+                               } else {                                         /* reg arg -> spilled    */
+                                       M_DST(rd->argfltregs[arg], REG_SP, 4 * var->regoff);
                                }
-                       else {                                   /* stack arguments       */
+
+                       } else {                                 /* stack arguments       */
                                pa = iarg + 6;
-                               if (!(var->flags & INMEMORY))        /* stack-arg -> register */
-                                       if (IS_2_WORD_TYPE(t))
-                                               M_DLD (r, REG_SP, 4 * (parentargs_base + pa) );
-                                       else
-                                               M_FLD (r, REG_SP, 4 * (parentargs_base + pa) );
-                               else {                               /* stack-arg -> spilled  */
-                                       if (IS_2_WORD_TYPE(t))
-                                               M_DLD (REG_FTMP1, REG_SP, 4 * (parentargs_base + pa));
-                                       else
-                                               M_FLD (REG_FTMP1, REG_SP, 4 * (parentargs_base + pa));
-                                       M_DST (REG_FTMP1, REG_SP, 4 * r);
-                                       }
-                               }
-                       }
-               }  /* end for */
+                               if (!(var->flags & INMEMORY)) {      /* stack-arg -> register */
+                                       if (IS_2_WORD_TYPE(t)) {
+                                               M_DLD(var->regoff, REG_SP, 4 * (parentargs_base + pa));
+
+                                       } else {
+                                               M_FLD(var->regoff, REG_SP, 4 * (parentargs_base + pa));
+                                       }
+
+                               } else {                             /* stack-arg -> spilled  */
+                                       if (IS_2_WORD_TYPE(t)) {
+                                               M_DLD(REG_FTMP1, REG_SP, 4 * (parentargs_base + pa));
+
+                                       } else {
+                                               M_FLD(REG_FTMP1, REG_SP, 4 * (parentargs_base + pa));
+                                       }
+                                       M_DST(REG_FTMP1, REG_SP, 4 * var->regoff);
+                               }
+                       }
+               }
+       } /* end for */
        }
 
        /* call monitorenter function */
 
-#ifdef USE_THREADS
-       if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
-               p = dseg_addaddress ((void*) (builtin_monitorenter));
+#if defined(USE_THREADS)
+       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+               s4 func_enter = (m->flags & ACC_STATIC) ?
+                       (s4) builtin_staticmonitorenter : (s4) builtin_monitorenter;
+               p = dseg_addaddress(cd, (void *) func_enter);
                M_ALD(REG_ITMP3, REG_PV, p);
                M_MTCTR(REG_ITMP3);
-               M_ALD(argintregs[0], REG_SP, 4 * maxmemuse);
+               M_ALD(rd->argintregs[0], REG_SP, rd->maxmemuse * 4);
                M_JSR;
-               }                       
+       }
 #endif
        }
 
        /* end of header generation */
 
        /* walk through all basic blocks */
-       for (/* bbs = block_count, */ bptr = block; /* --bbs >= 0 */ bptr != NULL; bptr = bptr->next) {
+       for (bptr = m->basicblocks; bptr != NULL; bptr = bptr->next) {
 
-               bptr -> mpc = (int)((u1*) mcodeptr - mcodebase);
+               bptr->mpc = (s4) ((u1 *) mcodeptr - cd->mcodebase);
 
                if (bptr->flags >= BBREACHED) {
 
@@ -733,8 +578,9 @@ void codegen()
                {
                branchref *brefs;
                for (brefs = bptr->branchrefs; brefs != NULL; brefs = brefs->next) {
-                       gen_resolvebranch((u1*) mcodebase + brefs->branchpos, 
-                                         brefs->branchpos, bptr->mpc);
+                       gen_resolvebranch((u1*) cd->mcodebase + brefs->branchpos, 
+                                         brefs->branchpos,
+                                                         bptr->mpc);
                        }
                }
 
@@ -746,51 +592,51 @@ void codegen()
                while (src != NULL) {
                        len--;
                        if ((len == 0) && (bptr->type != BBTYPE_STD)) {
-                               d = reg_of_var(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(src, REG_IFTMP);
+
+                       else {
+                               d = reg_of_var(rd, src, REG_IFTMP);
                                if ((src->varkind != STACKVAR)) {
                                        s2 = src->type;
                                        if (IS_FLT_DBL_TYPE(s2)) {
-                                               if (!(interfaces[len][s2].flags & INMEMORY)) {
-                                                       s1 = interfaces[len][s2].regoff;
+                                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                                       s1 = rd->interfaces[len][s2].regoff;
                                                        M_FLTMOVE(s1,d);
+
+                                               } else {
+                                                       if (IS_2_WORD_TYPE(s2)) {
+                                                               M_DLD(d, REG_SP, 4 * rd->interfaces[len][s2].regoff);
+
+                                                       } else {
+                                                               M_FLD(d, REG_SP, 4 * rd->interfaces[len][s2].regoff);
                                                        }
-                                               else {
-                                                       if (IS_2_WORD_TYPE(s2))
-                                                               M_DLD(d, REG_SP, 4 * interfaces[len][s2].regoff);
-                                                       else
-                                                               M_FLD(d, REG_SP, 4 * interfaces[len][s2].regoff);
-                                                       }
-                                               store_reg_to_var_flt(src, d);
                                                }
-                                       else {
-                                               if (!(interfaces[len][s2].flags & INMEMORY)) {
-                                                       s1 = interfaces[len][s2].regoff;
+                                               store_reg_to_var_flt(src, d);
+
+                                       } else {
+                                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                                       s1 = rd->interfaces[len][s2].regoff;
                                                        M_TINTMOVE(s2,s1,d);
-                                                       }
-                                               else {
-                                                       M_ILD(d, REG_SP, 4 * interfaces[len][s2].regoff);
+
+                                               else {
+                                                       M_ILD(d, REG_SP, 4 * rd->interfaces[len][s2].regoff);
                                                        if (IS_2_WORD_TYPE(s2))
-                                                               M_ILD(secondregs[d], REG_SP, 4 * interfaces[len][s2].regoff + 4);
-                                                       }
-                                               store_reg_to_var_int(src, d);
+                                                               M_ILD(rd->secondregs[d], REG_SP, 4 * rd->interfaces[len][s2].regoff + 4);
                                                }
+                                               store_reg_to_var_int(src, d);
                                        }
                                }
-                       src = src->prev;
                        }
+                       src = src->prev;
+               }
 
                /* walk through all instructions */
                
                src = bptr->instack;
                len = bptr->icount;
-               for (iptr = bptr->iinstr;
-                   len > 0;
-                   src = iptr->dst, len--, iptr++) {
+               for (iptr = bptr->iinstr; len > 0; src = iptr->dst, len--, iptr++) {
 
        MCODECHECK(64);           /* an instruction usually needs < 64 words      */
        switch (iptr->opc) {
@@ -803,20 +649,15 @@ void codegen()
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_TST(s1);
                        M_BEQ(0);
-                       codegen_addxnullrefs(mcodeptr);
+                       codegen_addxnullrefs(cd, mcodeptr);
                        break;
 
                /* constant operations ************************************************/
 
-#define ICONST(r,c) if(((c)>=0&&(c)<=32767)||((c)>=-32768&&(c)<0)){M_LDA(r,REG_ZERO,c);} \
-                    else{a=dseg_adds4(c);M_ILD(r,REG_PV,a);}
-
-#define LCONST(r,c) ICONST(r,(s4)((s8)(c)>>32)); ICONST(secondregs[r],(s4)((s8)(c)));
-
                case ICMD_ICONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.i = constant                    */
 
-                       d = reg_of_var(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;
@@ -824,7 +665,7 @@ void codegen()
                case ICMD_LCONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.l = constant                    */
 
-                       d = reg_of_var(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;
@@ -832,25 +673,25 @@ void codegen()
                case ICMD_FCONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.f = constant                    */
 
-                       d = reg_of_var (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);
+                       store_reg_to_var_flt(iptr->dst, d);
                        break;
                        
                case ICMD_DCONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.d = constant                    */
 
-                       d = reg_of_var (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);
+                       store_reg_to_var_flt(iptr->dst, d);
                        break;
 
                case ICMD_ACONST:     /* ...  ==> ..., constant                       */
                                      /* op1 = 0, val.a = constant                    */
 
-                       d = reg_of_var(iptr->dst, REG_ITMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        ICONST(d, (u4) iptr->val.a);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -862,35 +703,37 @@ void codegen()
                case ICMD_LLOAD:      /* op1 = local variable                         */
                case ICMD_ALOAD:
 
-                       d = reg_of_var(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 = &(locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
                        if (var->flags & INMEMORY) {
                                M_ILD(d, REG_SP, 4 * var->regoff);
                                if (IS_2_WORD_TYPE(var->type))
-                                       M_ILD(secondregs[d], REG_SP, 4 * var->regoff + 4);
-                       } else
-                               {M_TINTMOVE(var->type,var->regoff,d);}
+                                       M_ILD(rd->secondregs[d], REG_SP, 4 * var->regoff + 4);
+                       } else {
+                               M_TINTMOVE(var->type, var->regoff, d);
+                       }
                        store_reg_to_var_int(iptr->dst, d);
                        break;
 
                case ICMD_FLOAD:      /* ...  ==> ..., content of local variable      */
                case ICMD_DLOAD:      /* op1 = local variable                         */
 
-                       d = reg_of_var(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 = &(locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ILOAD]);
                        if (var->flags & INMEMORY)
                                if (IS_2_WORD_TYPE(var->type))
                                        M_DLD(d, REG_SP, 4 * var->regoff);
                                else
                                        M_FLD(d, REG_SP, 4 * var->regoff);
-                       else
-                               {M_FLTMOVE(var->regoff,d);}
+                       else {
+                               M_FLTMOVE(var->regoff, d);
+                       }
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
 
@@ -902,17 +745,16 @@ void codegen()
                        if ((src->varkind == LOCALVAR) &&
                            (src->varnum == iptr->op1))
                                break;
-                       var = &(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_IST(s1, REG_SP, 4 * var->regoff);
                                if (IS_2_WORD_TYPE(var->type))
-                                       M_IST(secondregs[s1], REG_SP, 4 * var->regoff + 4);
-                               }
-                       else {
+                                       M_IST(rd->secondregs[s1], REG_SP, 4 * var->regoff + 4);
+                       } else {
                                var_to_reg_int(s1, src, var->regoff);
                                M_TINTMOVE(var->type, s1, var->regoff);
-                               }
+                       }
                        break;
 
                case ICMD_FSTORE:     /* ..., value  ==> ...                          */
@@ -921,18 +763,17 @@ void codegen()
                        if ((src->varkind == LOCALVAR) &&
                            (src->varnum == iptr->op1))
                                break;
-                       var = &(locals[iptr->op1][iptr->opc - ICMD_ISTORE]);
+                       var = &(rd->locals[iptr->op1][iptr->opc - ICMD_ISTORE]);
                        if (var->flags & INMEMORY) {
                                var_to_reg_flt(s1, src, REG_FTMP1);
-                               if (var->type==TYPE_DBL)
+                               if (var->type == TYPE_DBL)
                                        M_DST(s1, REG_SP, 4 * var->regoff);
                                else
                                        M_FST(s1, REG_SP, 4 * var->regoff);
-                               }
-                       else {
+                       } else {
                                var_to_reg_flt(s1, src, var->regoff);
                                M_FLTMOVE(s1, var->regoff);
-                               }
+                       }
                        break;
 
 
@@ -944,29 +785,24 @@ void codegen()
                case ICMD_POP2:       /* ..., value, value  ==> ...                   */
                        break;
 
-#define M_COPY(from,to) \
-                       d = reg_of_var(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_FLTMOVE(s1,d); \
-                                       store_reg_to_var_flt(to, d); \
-                                       }\
-                               else { \
-                                       var_to_reg_int(s1, from, d); \
-                                       M_TINTMOVE(from->type,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                */
 
@@ -976,14 +812,11 @@ void codegen()
 
                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    */
@@ -992,13 +825,13 @@ void codegen()
                        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;
 
@@ -1008,7 +841,7 @@ void codegen()
                case ICMD_INEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_int(s1, src, REG_ITMP1); 
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_NEG(s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1016,8 +849,8 @@ void codegen()
         case ICMD_LNEG:       /* ..., value  ==> ..., - value                 */
 
             var_to_reg_int(s1, src, REG_ITMP1);
-            d = reg_of_var(iptr->dst, REG_ITMP3);
-            M_SUBFIC(secondregs[s1], 0, secondregs[d]);
+            d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+            M_SUBFIC(rd->secondregs[s1], 0, rd->secondregs[d]);
             M_SUBFZE(s1, d);
             store_reg_to_var_int(iptr->dst, d);
             break;
@@ -1025,16 +858,16 @@ void codegen()
                case ICMD_I2L:        /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
-                       M_INTMOVE(s1, secondregs[d]);
-                       M_SRA_IMM(secondregs[d], 31, d);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+                       M_INTMOVE(s1, rd->secondregs[d]);
+                       M_SRA_IMM(rd->secondregs[d], 31, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
 
                case ICMD_L2I:        /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int0(s1, src, REG_ITMP2, 0, 1);
-                       d = reg_of_var(iptr->dst, REG_ITMP2);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP2);
                        M_INTMOVE(s1, d );
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1042,7 +875,7 @@ void codegen()
                case ICMD_INT2BYTE:   /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_BSEXT(s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1050,7 +883,7 @@ void codegen()
                case ICMD_INT2CHAR:   /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(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;
@@ -1058,7 +891,7 @@ void codegen()
                case ICMD_INT2SHORT:  /* ..., value  ==> ..., value                   */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_SSEXT(s1, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1068,7 +901,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(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;
@@ -1077,7 +910,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(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);
                                }
@@ -1092,8 +925,8 @@ void codegen()
 
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 0, 1);
             var_to_reg_int0(s2, src, REG_ITMP2, 0, 1);
-            d = reg_of_var(iptr->dst, REG_ITMP3);
-            M_ADDC(s1, s2, secondregs[d]);
+            d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+            M_ADDC(s1, s2, rd->secondregs[d]);
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 1, 0);
             var_to_reg_int0(s2, src, REG_ITMP3, 1, 0);
             M_ADDE(s1, s2, d);
@@ -1105,13 +938,13 @@ void codegen()
 
                        s3 = iptr->val.l & 0xffffffff;
                        var_to_reg_int0(s1, src, REG_ITMP1, 0, 1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((s3 >= -32768) && (s3 <= 32767)) {
-                               M_ADDIC(s1, s3, secondregs[d]);
+                               M_ADDIC(s1, s3, rd->secondregs[d]);
                                }
                        else {
                                ICONST(REG_ITMP2, s3);
-                               M_ADDC(s1, REG_ITMP2, secondregs[d]);
+                               M_ADDC(s1, REG_ITMP2, rd->secondregs[d]);
                                }
                        var_to_reg_int0(s1, src, REG_ITMP1, 1, 0);
                        s3 = iptr->val.l >> 32;
@@ -1130,7 +963,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(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;
@@ -1139,7 +972,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(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);
                                }
@@ -1154,8 +987,8 @@ void codegen()
 
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 0, 1);
             var_to_reg_int0(s2, src, REG_ITMP2, 0, 1);
-            d = reg_of_var(iptr->dst, REG_ITMP3);
-            M_SUBC(s1, s2, secondregs[d]);
+            d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+            M_SUBC(s1, s2, rd->secondregs[d]);
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 1, 0);
             var_to_reg_int0(s2, src, REG_ITMP3, 1, 0);
             M_SUBE(s1, s2, d);
@@ -1167,13 +1000,13 @@ void codegen()
 
                        s3 = (-iptr->val.l) & 0xffffffff;
                        var_to_reg_int0(s1, src, REG_ITMP1, 0, 1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((s3 >= -32768) && (s3 <= 32767)) {
-                               M_ADDIC(s1, s3, secondregs[d]);
+                               M_ADDIC(s1, s3, rd->secondregs[d]);
                                }
                        else {
                                ICONST(REG_ITMP2, s3);
-                               M_ADDC(s1, REG_ITMP2, secondregs[d]);
+                               M_ADDC(s1, REG_ITMP2, rd->secondregs[d]);
                                }
                        var_to_reg_int0(s1, src, REG_ITMP1, 1, 0);
                        s3 = (-iptr->val.l) >> 32;
@@ -1191,7 +1024,7 @@ void codegen()
                case ICMD_IDIV:       /* ..., val1, val2  ==> ..., val1 / val2        */
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IDIV(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1199,7 +1032,7 @@ void codegen()
                case ICMD_IREM:       /* ..., val1, val2  ==> ..., val1 % val2        */
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IDIV(s1, s2, d);
                        M_IMUL(d, s2, d);
                        M_ISUB(s1, d, d);
@@ -1210,7 +1043,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_IMUL(s1, s2, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1219,7 +1052,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= -32768) && (iptr->val.i <= 32767)) {
                                M_IMUL_IMM(s1, iptr->val.i, d);
                                }
@@ -1233,7 +1066,7 @@ void codegen()
                case ICMD_IDIVPOW2:   /* ..., value  ==> ..., value << constant       */
                                      
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_SRA_IMM(s1, iptr->val.i, d);
                        M_ADDZE(d, d);
                        store_reg_to_var_int(iptr->dst, d);
@@ -1243,7 +1076,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_AND_IMM(s2, 0x1f, REG_ITMP3);
                        M_SLL(s1, REG_ITMP3, d);
                        store_reg_to_var_int(iptr->dst, d);
@@ -1253,7 +1086,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_SLL_IMM(s1, iptr->val.i & 0x1f, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1262,7 +1095,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_AND_IMM(s2, 0x1f, REG_ITMP3);
                        M_SRA(s1, REG_ITMP3, d);
                        store_reg_to_var_int(iptr->dst, d);
@@ -1272,7 +1105,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_SRA_IMM(s1, iptr->val.i & 0x1f, d);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
@@ -1281,7 +1114,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_AND_IMM(s2, 0x1f, REG_ITMP2);
                        M_SRL(s1, REG_ITMP2, d);
                        store_reg_to_var_int(iptr->dst, d);
@@ -1291,7 +1124,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->val.i & 0x1f)
                                M_SRL_IMM(s1, iptr->val.i & 0x1f, d);
                        else
@@ -1303,7 +1136,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(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;
@@ -1312,7 +1145,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= 0) && (iptr->val.i <= 65535)) {
                                M_AND_IMM(s1, iptr->val.i, d);
                                }
@@ -1332,8 +1165,8 @@ void codegen()
 
                        var_to_reg_int0(s1, src->prev, REG_ITMP1, 0, 1);
             var_to_reg_int0(s2, src, REG_ITMP2, 0, 1);
-            d = reg_of_var(iptr->dst, REG_ITMP3);
-            M_AND(s1, s2, secondregs[d]);
+            d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+            M_AND(s1, s2, rd->secondregs[d]);
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 1, 0);
             var_to_reg_int0(s2, src, REG_ITMP3, 1, 0);
             M_AND(s1, s2, d);
@@ -1345,13 +1178,13 @@ void codegen()
 
                        s3 = iptr->val.l & 0xffffffff;
                        var_to_reg_int0(s1, src, REG_ITMP1, 0, 1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((s3 >= 0) && (s3 <= 65535)) {
-                               M_AND_IMM(s1, s3, secondregs[d]);
+                               M_AND_IMM(s1, s3, rd->secondregs[d]);
                                }
                        else {
                                ICONST(REG_ITMP2, s3);
-                               M_AND(s1, REG_ITMP2, secondregs[d]);
+                               M_AND(s1, REG_ITMP2, rd->secondregs[d]);
                                }
                        var_to_reg_int0(s1, src, REG_ITMP1, 1, 0);
                        s3 = iptr->val.l >> 32;
@@ -1369,7 +1202,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_MOV(s1, REG_ITMP2);
                        M_CMPI(s1, 0);
                        M_BGE(1 + 2*(iptr->val.i >= 32768));
@@ -1394,7 +1227,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(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;
@@ -1403,7 +1236,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= 0) && (iptr->val.i <= 65535)) {
                                M_OR_IMM(s1, iptr->val.i, d);
                                }
@@ -1418,8 +1251,8 @@ void codegen()
 
                        var_to_reg_int0(s1, src->prev, REG_ITMP1, 0, 1);
             var_to_reg_int0(s2, src, REG_ITMP2, 0, 1);
-            d = reg_of_var(iptr->dst, REG_ITMP3);
-            M_OR(s1, s2, secondregs[d]);
+            d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+            M_OR(s1, s2, rd->secondregs[d]);
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 1, 0);
             var_to_reg_int0(s2, src, REG_ITMP3, 1, 0);
             M_OR(s1, s2, d);
@@ -1431,13 +1264,13 @@ void codegen()
 
                        s3 = iptr->val.l & 0xffffffff;
                        var_to_reg_int0(s1, src, REG_ITMP1, 0, 1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((s3 >= 0) && (s3 <= 65535)) {
-                               M_OR_IMM(s1, s3, secondregs[d]);
+                               M_OR_IMM(s1, s3, rd->secondregs[d]);
                                }
                        else {
                                ICONST(REG_ITMP2, s3);
-                               M_OR(s1, REG_ITMP2, secondregs[d]);
+                               M_OR(s1, REG_ITMP2, rd->secondregs[d]);
                                }
                        var_to_reg_int0(s1, src, REG_ITMP1, 1, 0);
                        s3 = iptr->val.l >> 32;
@@ -1455,7 +1288,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(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;
@@ -1464,7 +1297,7 @@ void codegen()
                                      /* val.i = constant                             */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((iptr->val.i >= 0) && (iptr->val.i <= 65535)) {
                                M_XOR_IMM(s1, iptr->val.i, d);
                                }
@@ -1479,8 +1312,8 @@ void codegen()
 
                        var_to_reg_int0(s1, src->prev, REG_ITMP1, 0, 1);
             var_to_reg_int0(s2, src, REG_ITMP2, 0, 1);
-            d = reg_of_var(iptr->dst, REG_ITMP3);
-            M_XOR(s1, s2, secondregs[d]);
+            d = reg_of_var(rd, iptr->dst, REG_ITMP3);
+            M_XOR(s1, s2, rd->secondregs[d]);
             var_to_reg_int0(s1, src->prev, REG_ITMP1, 1, 0);
             var_to_reg_int0(s2, src, REG_ITMP3, 1, 0);
             M_XOR(s1, s2, d);
@@ -1492,13 +1325,13 @@ void codegen()
 
                        s3 = iptr->val.l & 0xffffffff;
                        var_to_reg_int0(s1, src, REG_ITMP1, 0, 1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if ((s3 >= 0) && (s3 <= 65535)) {
-                               M_XOR_IMM(s1, s3, secondregs[d]);
+                               M_XOR_IMM(s1, s3, rd->secondregs[d]);
                                }
                        else {
                                ICONST(REG_ITMP2, s3);
-                               M_XOR(s1, REG_ITMP2, secondregs[d]);
+                               M_XOR(s1, REG_ITMP2, rd->secondregs[d]);
                                }
                        var_to_reg_int0(s1, src, REG_ITMP1, 1, 0);
                        s3 = iptr->val.l >> 32;
@@ -1516,10 +1349,10 @@ void codegen()
 
                        var_to_reg_int0(s1, src->prev, REG_ITMP3, 1, 0);
                        var_to_reg_int0(s2, src, REG_ITMP2, 1, 0);
-                       d = reg_of_var(iptr->dst, REG_ITMP1);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        {
                                int tempreg =
-                                       (d==s1 || d==s2 || d==secondregs[s1] || d==secondregs[s2]);
+                                       (d==s1 || d==s2 || d==rd->secondregs[s1] || d==rd->secondregs[s2]);
                                int dreg = tempreg ? REG_ITMP1 : d;
                                s4 *br1;
                                M_IADD_IMM(REG_ZERO, 1, dreg);
@@ -1544,7 +1377,7 @@ void codegen()
                case ICMD_IINC:       /* ..., value  ==> ..., value + constant        */
                                      /* op1 = variable, val.i = constant             */
 
-            var = &(locals[iptr->op1][TYPE_INT]);
+            var = &(rd->locals[iptr->op1][TYPE_INT]);
             if (var->flags & INMEMORY) {
                 s1 = REG_ITMP1;
                 M_ILD(s1, REG_SP, 4 * var->regoff);
@@ -1570,7 +1403,7 @@ void codegen()
                case ICMD_FNEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FMOVN(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1578,7 +1411,7 @@ void codegen()
                case ICMD_DNEG:       /* ..., value  ==> ..., - value                 */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FMOVN(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1587,7 +1420,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1596,7 +1429,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1605,7 +1438,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1614,7 +1447,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1623,7 +1456,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1632,7 +1465,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1641,7 +1474,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1650,7 +1483,7 @@ void codegen()
 
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(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;
@@ -1658,13 +1491,13 @@ void codegen()
                case ICMD_F2I:       /* ..., value  ==> ..., (int) value              */
                case ICMD_D2I:
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_CLR(d);
-                       a = dseg_addfloat(0.0);
+                       a = dseg_addfloat(cd, 0.0);
                        M_FLD(REG_FTMP2, REG_PV, a);
                        M_FCMPU(s1, REG_FTMP2);
                        M_BNAN(4);
-                       a = dseg_adds4(0);
+                       a = dseg_adds4(cd, 0);
                        M_CVTDL_C(s1, REG_FTMP1);
                        M_LDA (REG_ITMP1, REG_PV, a);
                        M_STFIWX(REG_FTMP1, 0, REG_ITMP1);
@@ -1675,7 +1508,7 @@ void codegen()
                case ICMD_F2D:       /* ..., value  ==> ..., (double) value           */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        M_FLTMOVE(s1, d);
                        store_reg_to_var_flt(iptr->dst, d);
                        break;
@@ -1683,7 +1516,7 @@ void codegen()
                case ICMD_D2F:       /* ..., value  ==> ..., (double) value           */
 
                        var_to_reg_flt(s1, src, REG_FTMP1);
-                       d = reg_of_var(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;
@@ -1692,7 +1525,7 @@ void codegen()
                case ICMD_DCMPL:
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_FCMPU(s2, s1);
                        M_IADD_IMM(0, -1, d);
                        M_BNAN(4);
@@ -1707,7 +1540,7 @@ void codegen()
                case ICMD_DCMPG:
                        var_to_reg_flt(s1, src->prev, REG_FTMP1);
                        var_to_reg_flt(s2, src, REG_FTMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        M_FCMPU(s1, s2);
                        M_IADD_IMM(0, 1, d);
                        M_BNAN(4);
@@ -1721,18 +1554,10 @@ void codegen()
 
                /* memory operations **************************************************/
 
-#define gen_bound_check \
-            if (checkbounds) { \
-                               M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));\
-                               M_CMPU(s2, REG_ITMP3);\
-                               M_BGE(0);\
-                               codegen_addxboundrefs(mcodeptr); \
-                }
-
                case ICMD_ARRAYLENGTH: /* ..., arrayref  ==> ..., length              */
 
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(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);
@@ -1742,7 +1567,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1757,7 +1582,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1765,7 +1590,7 @@ void codegen()
                        M_SLL_IMM(s2, 3, REG_ITMP2);
                        M_IADD(s1, REG_ITMP2, REG_ITMP2);
                        M_ILD(d, REG_ITMP2, OFFSET(java_longarray, data[0]));
-                       M_ILD(secondregs[d], REG_ITMP2, OFFSET(java_longarray, data[0])+4);
+                       M_ILD(rd->secondregs[d], REG_ITMP2, OFFSET(java_longarray, data[0])+4);
                        store_reg_to_var_int(iptr->dst, d);
                        break;
 
@@ -1773,7 +1598,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1788,7 +1613,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1803,7 +1628,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_FTMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_FTMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1818,7 +1643,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1833,7 +1658,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1848,7 +1673,7 @@ void codegen()
 
                        var_to_reg_int(s1, src->prev, REG_ITMP1);
                        var_to_reg_int(s2, src, REG_ITMP2);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (iptr->op1 == 0) {
                                gen_nullptr_check(s1);
                                gen_bound_check;
@@ -1962,7 +1787,29 @@ void codegen()
                case ICMD_PUTSTATIC:  /* ..., value  ==> ...                          */
                                      /* op1 = type, val.a = field address            */
 
-                       a = dseg_addaddress (&(((fieldinfo *)(iptr->val.a))->value));
+                       /* if class isn't yet initialized, do it */
+                       if (!((fieldinfo *) iptr->val.a)->class->initialized) {
+                               /* call helper function which patches this code */
+                               a = dseg_addaddress(cd, ((fieldinfo *) iptr->val.a)->class);
+                               M_ALD(REG_ITMP1, REG_PV, a);
+                               a = dseg_addaddress(cd, asm_check_clinit);
+                               M_ALD(REG_PV, REG_PV, a);
+                               M_MTCTR(REG_PV);
+                               M_JSR;
+
+                               /* recompute pv */
+                               s1 = (s4) ((u1 *) mcodeptr - cd->mcodebase);
+                               M_MFLR(REG_ITMP1);
+                               if (s1 <= 32768) M_LDA(REG_PV, REG_ITMP1, -s1);
+                               else {
+                                       s4 ml = -s1, mh = 0;
+                                       while (ml < -32768) { ml += 65536; mh--; }
+                                       M_LDA(REG_PV, REG_ITMP1, ml);
+                                       M_LDAH(REG_PV, REG_PV, mh);
+                               }
+                       }
+
+                       a = dseg_addaddress(cd, &(((fieldinfo *) iptr->val.a)->value));
                        M_ALD(REG_ITMP1, REG_PV, a);
                        switch (iptr->op1) {
                                case TYPE_INT:
@@ -1972,7 +1819,7 @@ void codegen()
                                case TYPE_LNG:
                                        var_to_reg_int(s2, src, REG_ITMP3);
                                        M_IST(s2, REG_ITMP1, 0);
-                                       M_IST(secondregs[s2], REG_ITMP1, 4);
+                                       M_IST(rd->secondregs[s2], REG_ITMP1, 4);
                                        break;
                                case TYPE_ADR:
                                        var_to_reg_int(s2, src, REG_ITMP2);
@@ -1993,32 +1840,54 @@ void codegen()
                case ICMD_GETSTATIC:  /* ...  ==> ..., value                          */
                                      /* op1 = type, val.a = field address            */
 
-                       a = dseg_addaddress (&(((fieldinfo *)(iptr->val.a))->value));
+            /* if class isn't yet initialized, do it */
+            if (!((fieldinfo *) iptr->val.a)->class->initialized) {
+                /* call helper function which patches this code */
+                a = dseg_addaddress(cd, ((fieldinfo *) iptr->val.a)->class);
+                M_ALD(REG_ITMP1, REG_PV, a);
+                a = dseg_addaddress(cd, asm_check_clinit);
+                M_ALD(REG_PV, REG_PV, a);
+                M_MTCTR(REG_PV);
+                M_JSR;
+
+                /* recompute pv */
+                s1 = (s4) ((u1 *) mcodeptr - cd->mcodebase);
+                M_MFLR(REG_ITMP1);
+                if (s1 <= 32768) M_LDA(REG_PV, REG_ITMP1, -s1);
+                else {
+                    s4 ml = -s1, mh = 0;
+                    while (ml < -32768) { ml += 65536; mh--; }
+                    M_LDA(REG_PV, REG_ITMP1, ml);
+                    M_LDAH(REG_PV, REG_PV, mh);
+                }
+            }
+
+                       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(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(iptr->dst, REG_ITMP3);
+                                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                                        M_ILD(d, REG_ITMP1, 0);
-                                       M_ILD(secondregs[d], REG_ITMP1, 4);
+                                       M_ILD(rd->secondregs[d], REG_ITMP1, 4);
                                        store_reg_to_var_int(iptr->dst, d);
                                        break;
                                case TYPE_ADR:
-                                       d = reg_of_var(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(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(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;
@@ -2043,7 +1912,7 @@ void codegen()
                                        var_to_reg_int(s2, src, REG_ITMP3);
                                        gen_nullptr_check(s1);
                                        M_IST(s2, s1, a);
-                                       M_IST(secondregs[s2], s1, a+4);
+                                       M_IST(rd->secondregs[s2], s1, a+4);
                                        break;
                                case TYPE_ADR:
                                        var_to_reg_int(s1, src->prev, REG_ITMP1);
@@ -2074,36 +1943,36 @@ void codegen()
                        switch (iptr->op1) {
                                case TYPE_INT:
                                        var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                                       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(iptr->dst, REG_ITMP3);
+                                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                                        gen_nullptr_check(s1);
                                        M_ILD(d, s1, a);
-                                       M_ILD(secondregs[d], s1, a+4);
+                                       M_ILD(rd->secondregs[d], s1, a+4);
                                        store_reg_to_var_int(iptr->dst, d);
                                        break;
                                case TYPE_ADR:
                                        var_to_reg_int(s1, src, REG_ITMP1);
-                                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                                       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(iptr->dst, REG_FTMP1);
+                                       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(iptr->dst, REG_FTMP1);
+                                       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);
@@ -2115,20 +1984,18 @@ void codegen()
 
                /* branch operations **************************************************/
 
-#define ALIGNCODENOP {if((int)((long)mcodeptr&7)){M_NOP;}}
-
                case ICMD_ATHROW:       /* ..., objectref ==> ... (, objectref)       */
 
-                       a = dseg_addaddress(asm_handle_exception);
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP2, REG_PV, a);
                        M_MTCTR(REG_ITMP2);
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_INTMOVE(s1, REG_ITMP1_XPTR);
 
-                       if (isleafmethod) M_MFLR(REG_ITMP3);  /* save LR */
+                       if (m->isleafmethod) M_MFLR(REG_ITMP3);  /* save LR */
                        M_BL(0);            /* get current PC */
                        M_MFLR(REG_ITMP2_XPC);
-                       if (isleafmethod) M_MTLR(REG_ITMP3);  /* restore LR */
+                       if (m->isleafmethod) M_MTLR(REG_ITMP3);  /* restore LR */
                        M_RTS;              /* jump to CTR */
 
                        ALIGNCODENOP;
@@ -2137,26 +2004,26 @@ void codegen()
                case ICMD_GOTO:         /* ... ==> ...                                */
                                        /* op1 = target JavaVM pc                     */
                        M_BR(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        ALIGNCODENOP;
                        break;
 
                case ICMD_JSR:          /* ... ==> ...                                */
                                        /* op1 = target JavaVM pc                     */
 
-                       if (isleafmethod) M_MFLR(REG_ITMP2);
+                       if (m->isleafmethod) M_MFLR(REG_ITMP2);
                        M_BL(0);
                        M_MFLR(REG_ITMP1);
-                       M_IADD_IMM(REG_ITMP1, isleafmethod?16:12, REG_ITMP1);
-                       if (isleafmethod) M_MTLR(REG_ITMP2);
+                       M_IADD_IMM(REG_ITMP1, m->isleafmethod ? 16 : 12, REG_ITMP1);
+                       if (m->isleafmethod) M_MTLR(REG_ITMP2);
                        M_BR(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
                        
                case ICMD_RET:          /* ... ==> ...                                */
                                        /* op1 = local variable                       */
 
-                       var = &(locals[iptr->op1][TYPE_ADR]);
+                       var = &(rd->locals[iptr->op1][TYPE_ADR]);
                        if (var->flags & INMEMORY) {
                                M_ALD(REG_ITMP1, REG_SP, 4 * var->regoff);
                                M_MTCTR(REG_ITMP1);
@@ -2173,7 +2040,7 @@ void codegen()
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_TST(s1);
                        M_BEQ(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IFNONNULL:    /* ..., value ==> ...                         */
@@ -2182,7 +2049,7 @@ void codegen()
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_TST(s1);
                        M_BNE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IFLT:
@@ -2222,7 +2089,7 @@ void codegen()
                                        M_BEQ(0);
                                        break;
                                }
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
 
@@ -2248,7 +2115,7 @@ void codegen()
                                M_CMP(s1, REG_ITMP3)
                        }
                        M_BEQ(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
                        
                case ICMD_IF_LLT:       /* ..., value ==> ...                         */
@@ -2263,7 +2130,7 @@ void codegen()
                        if ((iptr->val.l >= -32768) && (iptr->val.l <= 32767)) {
                                M_CMPI(s2, (u4) (iptr->val.l >> 32));
                                M_BLT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BGT(2);
                                M_CMPI(s1, (u4) (iptr->val.l & 0xffffffff));
 
@@ -2271,13 +2138,13 @@ void codegen()
                                ICONST(REG_ITMP3, (u4) (iptr->val.l >> 32));
                                M_CMP(s2, REG_ITMP3);
                                M_BLT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BGT(3);
                                ICONST(REG_ITMP3, (u4) (iptr->val.l & 0xffffffff));
                                M_CMP(s1, REG_ITMP3)
                        }
                        M_BLT(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
                        
                case ICMD_IF_LLE:       /* ..., value ==> ...                         */
@@ -2293,7 +2160,7 @@ void codegen()
                        if ((iptr->val.l >= -32768) && (iptr->val.l <= 32767)) {
                                M_CMPI(s2, (u4) (iptr->val.l >> 32));
                                M_BLT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BGT(2);
                                M_CMPI(s1, (u4) (iptr->val.l & 0xffffffff));
 
@@ -2301,13 +2168,13 @@ void codegen()
                                ICONST(REG_ITMP3, (u4) (iptr->val.l >> 32));
                                M_CMP(s2, REG_ITMP3);
                                M_BLT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BGT(3);
                                ICONST(REG_ITMP3, (u4) (iptr->val.l & 0xffffffff));
                                M_CMP(s1, REG_ITMP3)
                        }
                        M_BLE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
                        
                case ICMD_IF_LNE:       /* ..., value ==> ...                         */
@@ -2332,7 +2199,7 @@ void codegen()
                                M_CMP(s1, REG_ITMP3)
                        }
                        M_BNE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
                        
                case ICMD_IF_LGT:       /* ..., value ==> ...                         */
@@ -2348,7 +2215,7 @@ void codegen()
                        if ((iptr->val.l >= -32768) && (iptr->val.l <= 32767)) {
                                M_CMPI(s2, (u4) (iptr->val.l >> 32));
                                M_BGT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BLT(2);
                                M_CMPI(s1, (u4) (iptr->val.l & 0xffffffff));
 
@@ -2356,13 +2223,13 @@ void codegen()
                                ICONST(REG_ITMP3, (u4) (iptr->val.l >> 32));
                                M_CMP(s2, REG_ITMP3);
                                M_BGT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BLT(3);
                                ICONST(REG_ITMP3, (u4) (iptr->val.l & 0xffffffff));
                                M_CMP(s1, REG_ITMP3)
                        }
                        M_BGT(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
                        
                case ICMD_IF_LGE:       /* ..., value ==> ...                         */
@@ -2377,7 +2244,7 @@ void codegen()
                        if ((iptr->val.l >= -32768) && (iptr->val.l <= 32767)) {
                                M_CMPI(s2, (u4) (iptr->val.l >> 32));
                                M_BGT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BLT(2);
                                M_CMPI(s1, (u4) (iptr->val.l & 0xffffffff));
 
@@ -2385,16 +2252,16 @@ void codegen()
                                ICONST(REG_ITMP3, (u4) (iptr->val.l >> 32));
                                M_CMP(s2, REG_ITMP3);
                                M_BGT(0);
-                               codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                               codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                                M_BLT(3);
                                ICONST(REG_ITMP3, (u4) (iptr->val.l & 0xffffffff));
                                M_CMP(s1, REG_ITMP3)
                        }
                        M_BGE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
-                       //CUT: alle _L
+                       /* CUT: alle _L */
                case ICMD_IF_ICMPEQ:    /* ..., value, value ==> ...                  */
                case ICMD_IF_LCMPEQ:    /* op1 = target JavaVM pc                     */
                case ICMD_IF_ACMPEQ:
@@ -2403,7 +2270,7 @@ void codegen()
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMP(s1, s2);
                        M_BEQ(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IF_ICMPNE:    /* ..., value, value ==> ...                  */
@@ -2414,7 +2281,7 @@ void codegen()
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMP(s1, s2);
                        M_BNE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IF_ICMPLT:    /* ..., value, value ==> ...                  */
@@ -2424,7 +2291,7 @@ void codegen()
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMP(s1, s2);
                        M_BLT(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IF_ICMPGT:    /* ..., value, value ==> ...                  */
@@ -2434,7 +2301,7 @@ void codegen()
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMP(s1, s2);
                        M_BGT(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IF_ICMPLE:    /* ..., value, value ==> ...                  */
@@ -2444,7 +2311,7 @@ void codegen()
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMP(s1, s2);
                        M_BLE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IF_ICMPGE:    /* ..., value, value ==> ...                  */
@@ -2454,7 +2321,7 @@ void codegen()
                        var_to_reg_int(s2, src, REG_ITMP2);
                        M_CMP(s1, s2);
                        M_BGE(0);
-                       codegen_addreference(BlockPtrOfPC(iptr->op1), mcodeptr);
+                       codegen_addreference(cd, BlockPtrOfPC(iptr->op1), mcodeptr);
                        break;
 
                case ICMD_IRETURN:      /* ..., retvalue ==> ...                      */
@@ -2466,14 +2333,14 @@ void codegen()
 
                case ICMD_RETURN:      /* ...  ==> ...                                */
 
-#ifdef USE_THREADS
-                       if (checksync && (method->flags & ACC_SYNCHRONIZED)) {
-                               a = dseg_addaddress ((void*) (builtin_monitorexit));
+#if defined(USE_THREADS)
+                       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+                               a = dseg_addaddress(cd, (void *) (builtin_monitorexit));
                                M_ALD(REG_ITMP3, REG_PV, a);
                                M_MTCTR(REG_ITMP3);
-                               M_ALD(argintregs[0], REG_SP, 4 * maxmemuse);
+                               M_ALD(rd->argintregs[0], REG_SP, rd->maxmemuse * 4);
                                M_JSR;
-                               }                       
+                       }
 #endif
                        switch (iptr->opc) {
                        case ICMD_IRETURN:
@@ -2492,57 +2359,71 @@ void codegen()
 
 nowperformreturn:
                        {
-                       int r, p;
+                       s4 i, p;
                        
                        p = parentargs_base;
                        
                        /* restore return address                                         */
 
-                       if (!isleafmethod) {
-                               M_ALD (REG_ITMP3, REG_SP, 4 * p + 8);
-                               M_MTLR (REG_ITMP3);
-                               }
+                       if (!m->isleafmethod) {
+                               M_ALD(REG_ITMP3, REG_SP, 4 * p + 8);
+                               M_MTLR(REG_ITMP3);
+                       }
 
                        /* restore saved registers                                        */
 
-                       for (r = savintregcnt - 1; r >= maxsavintreguse; r--)
-                                       {p--; M_ILD(savintregs[r], REG_SP, 4 * p);}
-                       for (r = savfltregcnt - 1; r >= maxsavfltreguse; r--)
-                                       {p-=2; M_DLD(savfltregs[r], REG_SP, 4 * p);}
+                       for (i = rd->savintregcnt - 1; i >= rd->maxsavintreguse; i--) {
+                               p--; M_ILD(rd->savintregs[i], REG_SP, 4 * p);
+                       }
+                       for (i = rd->savfltregcnt - 1; i >= rd->maxsavfltreguse; i--) {
+                               p -= 2; M_DLD(rd->savfltregs[i], REG_SP, 4 * p);
+                       }
 
                        /* deallocate stack                                               */
 
-                       if (parentargs_base)
-                               {M_LDA(REG_SP, REG_SP, parentargs_base*4);}
+                       if (parentargs_base) {
+                               M_LDA(REG_SP, REG_SP, parentargs_base*4);
+                       }
 
                        /* call trace function */
 
-#if 1
                        if (runverbose) {
                                M_MFLR(REG_ITMP3);
-                               M_LDA (REG_SP, REG_SP, -80);
+                               M_LDA(REG_SP, REG_SP, -10 * 8);
                                M_DST(REG_FRESULT, REG_SP, 48+0);
                                M_IST(REG_RESULT, REG_SP, 48+8);
                                M_AST(REG_ITMP3, REG_SP, 48+12);
-                               M_IST(REG_RESULT+1, REG_SP, 48+16);
-                               a = dseg_addaddress (method);
-                               M_MOV(REG_RESULT+1, argintregs[2]);
-                               M_MOV(REG_RESULT, argintregs[1]);
-                               M_ALD(argintregs[0], REG_PV, a);
-                               M_FLTMOVE(REG_FRESULT, argfltregs[0]);
-                               M_FLTMOVE(REG_FRESULT, argfltregs[1]);
-                               a = dseg_addaddress ((void*) (builtin_displaymethodstop));
+                               M_IST(REG_RESULT2, REG_SP, 48+16);
+                               a = dseg_addaddress(cd, m);
+
+                               /* keep this order */
+                               switch (iptr->opc) {
+                               case ICMD_IRETURN:
+                               case ICMD_ARETURN:
+                                       M_MOV(REG_RESULT, rd->argintregs[2]);
+                                       M_CLR(rd->argintregs[1]);
+                                       break;
+
+                               case ICMD_LRETURN:
+                                       M_MOV(REG_RESULT2, rd->argintregs[2]);
+                                       M_MOV(REG_RESULT, rd->argintregs[1]);
+                                       break;
+                               }
+                               M_ALD(rd->argintregs[0], REG_PV, a);
+
+                               M_FLTMOVE(REG_FRESULT, rd->argfltregs[0]);
+                               M_FLTMOVE(REG_FRESULT, rd->argfltregs[1]);
+                               a = dseg_addaddress(cd, (void *) builtin_displaymethodstop);
                                M_ALD(REG_ITMP2, REG_PV, a);
                                M_MTCTR(REG_ITMP2);
                                M_JSR;
                                M_DLD(REG_FRESULT, REG_SP, 48+0);
                                M_ILD(REG_RESULT, REG_SP, 48+8);
                                M_ALD(REG_ITMP3, REG_SP, 48+12);
-                               M_ILD(REG_RESULT+1, REG_SP, 48+16);
-                               M_LDA (REG_SP, REG_SP, 80);
+                               M_ILD(REG_RESULT2, REG_SP, 48+16);
+                               M_LDA(REG_SP, REG_SP, 10 * 8);
                                M_MTLR(REG_ITMP3);
-                               }
-#endif
+                       }
 
                        M_RET;
                        ALIGNCODENOP;
@@ -2562,15 +2443,14 @@ nowperformreturn:
                        i = s4ptr[2];                          /* high    */
                        
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       if (l == 0)
-                               {M_INTMOVE(s1, REG_ITMP1);}
-                       else if (l <= 32768) {
+                       if (l == 0) {
+                               M_INTMOVE(s1, REG_ITMP1);
+                       else if (l <= 32768) {
                                M_LDA(REG_ITMP1, s1, -l);
-                               }
-                       else {
+                       } else {
                                ICONST(REG_ITMP2, l);
                                M_ISUB(s1, REG_ITMP2, REG_ITMP1);
-                               }
+                       }
                        i = i - l + 1;
 
                        /* range check */
@@ -2578,9 +2458,8 @@ nowperformreturn:
                        M_CMPUI(REG_ITMP1, i - 1);
                        M_BGT(0);
 
-
-                       /* codegen_addreference(BlockPtrOfPC(s4ptr[0]), mcodeptr); */
-                       codegen_addreference((basicblock *) tptr[0], mcodeptr);
+                       /* codegen_addreference(cd, BlockPtrOfPC(s4ptr[0]), mcodeptr); */
+                       codegen_addreference(cd, (basicblock *) tptr[0], mcodeptr);
 
                        /* build jump table top down and use address of lowest entry */
 
@@ -2588,8 +2467,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;
                                }
                        }
@@ -2598,7 +2477,7 @@ nowperformreturn:
 
                        M_SLL_IMM(REG_ITMP1, 2, REG_ITMP1);
                        M_IADD(REG_ITMP1, REG_PV, REG_ITMP2);
-                       M_ALD(REG_ITMP2, REG_ITMP2, -dseglen);
+                       M_ALD(REG_ITMP2, REG_ITMP2, -(cd->dseglen));
                        M_MTCTR(REG_ITMP2);
                        M_RTS;
                        ALIGNCODENOP;
@@ -2627,20 +2506,20 @@ nowperformreturn:
                                        M_CMPI(s1, val);
                                        } 
                                else {
-                                       a = dseg_adds4 (val);
+                                       a = dseg_adds4(cd, val);
                                        M_ILD(REG_ITMP2, REG_PV, a);
                                        M_CMP(s1, REG_ITMP2);
                                        }
                                M_BEQ(0);
-                               /* codegen_addreference(BlockPtrOfPC(s4ptr[1]), mcodeptr); */
-                               codegen_addreference((basicblock *) tptr[0], mcodeptr); 
+                               /* codegen_addreference(cd, BlockPtrOfPC(s4ptr[1]), mcodeptr); */
+                               codegen_addreference(cd, (basicblock *) tptr[0], mcodeptr); 
                                }
 
                        M_BR(0);
-                       /* codegen_addreference(BlockPtrOfPC(l), mcodeptr); */
+                       /* codegen_addreference(cd, BlockPtrOfPC(l), mcodeptr); */
                        
                        tptr = (void **) iptr->target;
-                       codegen_addreference((basicblock *) tptr[0], mcodeptr);
+                       codegen_addreference(cd, (basicblock *) tptr[0], mcodeptr);
 
                        ALIGNCODENOP;
                        break;
@@ -2677,8 +2556,8 @@ nowperformreturn:
                        s3 = iptr->op1;
 
 gen_method: {
-                       methodinfo   *m;
-                       classinfo    *ci;
+                       methodinfo *lm;
+                       classinfo  *ci;
 
                        MCODECHECK((s3 << 1) + 64);
 
@@ -2695,7 +2574,7 @@ gen_method: {
                                        ++fltcnt;
                                }
 
-                       for (s3=s3save, src=srcsave; --s3 >= 0; src = src->prev) {
+                       for (s3 = s3save, src = srcsave; --s3 >= 0; src = src->prev) {
                                argsize -= IS_2_WORD_TYPE(src->type) ? 2 : 1;
                                if (IS_FLT_DBL_TYPE(src->type))
                                        --fltcnt;
@@ -2703,45 +2582,47 @@ gen_method: {
                                        continue;
                                if (IS_INT_LNG_TYPE(src->type)) {
                                        if (argsize < INT_ARG_CNT) {
-                                               s1 = argintregs[argsize];
+                                               s1 = rd->argintregs[argsize];
                                                var_to_reg_int(d, src, s1);
-                                               if (argsize < INT_ARG_CNT-1)
-                                                       {M_TINTMOVE(src->type, d, s1);}
-                                               else {
+                                               if (argsize < INT_ARG_CNT-1) {
+                                                       M_TINTMOVE(src->type, d, s1);
+
+                                               } else {
                                                        M_INTMOVE(d, s1);
                                                        if (IS_2_WORD_TYPE(src->type))
-                                                               M_IST(secondregs[d], REG_SP, 4 * (argsize + 6) + 4);
-                                                       }
+                                                               M_IST(rd->secondregs[d], REG_SP, 4 * (argsize + 6) + 4);
                                                }
-                                       else  {
+
+                                       } else {
                                                var_to_reg_int(d, src, REG_ITMP1);
                                                M_IST(d, REG_SP, 4 * (argsize + 6));
                                                if (IS_2_WORD_TYPE(src->type))
-                                                       M_IST(secondregs[d], REG_SP, 4 * (argsize + 6) + 4);
-                                               }
+                                                       M_IST(rd->secondregs[d], REG_SP, 4 * (argsize + 6) + 4);
                                        }
-                               else
+
+                               } else {
                                        if (fltcnt < FLT_ARG_CNT) {
-                                               s1 = argfltregs[fltcnt];
+                                               s1 = rd->argfltregs[fltcnt];
                                                var_to_reg_flt(d, src, s1);
                                                M_FLTMOVE(d, s1);
-                                               }
-                                       else {
+
+                                       else {
                                                var_to_reg_flt(d, src, REG_FTMP1);
                                                if (IS_2_WORD_TYPE(src->type))
                                                        M_DST(d, REG_SP, 4 * (argsize + 6));
                                                else
                                                        M_FST(d, REG_SP, 4 * (argsize + 6));
-                                               }
-                               } /* end of for */
+                                       }
+                               }
+                       } /* end of for */
                        }
 
-                       m = iptr->val.a;
+                       lm = iptr->val.a;
                        switch (iptr->opc) {
                                case ICMD_BUILTIN3:
                                case ICMD_BUILTIN2:
                                case ICMD_BUILTIN1:
-                                       a = dseg_addaddress ((void*) (m));
+                                       a = dseg_addaddress(cd, (void *) lm);
 
                                        M_ALD(REG_PV, REG_PV, a); /* Pointer to built-in-function */
                                        d = iptr->op1;
@@ -2749,77 +2630,75 @@ gen_method: {
 
                                case ICMD_INVOKESTATIC:
                                case ICMD_INVOKESPECIAL:
-                                       a = dseg_addaddress (m->stubroutine);
+                                       a = dseg_addaddress(cd, lm->stubroutine);
 
-                                       M_ALD(REG_PV, REG_PV, a );       /* method pointer in r27 */
+                                       M_ALD(REG_PV, REG_PV, a);       /* method pointer in r27 */
 
-                                       d = m->returntype;
+                                       d = lm->returntype;
                                        goto makeactualcall;
 
                                case ICMD_INVOKEVIRTUAL:
 
-                                       gen_nullptr_check(argintregs[0]);
-                                       M_ALD(REG_METHODPTR, argintregs[0],
-                                                                OFFSET(java_objectheader, vftbl));
-                                       M_ALD(REG_PV, REG_METHODPTR, OFFSET(vftbl, table[0]) +
-                                                               sizeof(methodptr) * m->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);
 
-                                       d = m->returntype;
+                                       d = lm->returntype;
                                        goto makeactualcall;
 
                                case ICMD_INVOKEINTERFACE:
-                                       ci = m->class;
+                                       ci = lm->class;
                                        
-                                       gen_nullptr_check(argintregs[0]);
-                                       M_ALD(REG_METHODPTR, argintregs[0],
-                                                                OFFSET(java_objectheader, vftbl));    
+                                       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, interfacetable[0]) -
+                                             OFFSET(vftbl_t, interfacetable[0]) -
                                              sizeof(methodptr*) * ci->index);
                                        M_ALD(REG_PV, REG_METHODPTR,
-                                                           sizeof(methodptr) * (m - ci->methods));
+                                                 sizeof(methodptr) * (lm - ci->methods));
 
-                                       d = m->returntype;
+                                       d = lm->returntype;
                                        goto makeactualcall;
 
                                default:
                                        d = 0;
-                                       sprintf (logtext, "Unkown ICMD-Command: %d", iptr->opc);
-                                       error ();
+                                       error ("Unkown ICMD-Command: %d", iptr->opc);
                                }
 
 makeactualcall:
-
                        M_MTCTR(REG_PV);
                        M_JSR;
 
                        /* recompute pv */
 
-                       s1 = (int)((u1*) mcodeptr - mcodebase);
+                       s1 = (s4) ((u1 *) mcodeptr - cd->mcodebase);
                        M_MFLR(REG_ITMP1);
-                       if (s1<=32768) M_LDA (REG_PV, REG_ITMP1, -s1);
+                       if (s1 <= 32768) M_LDA(REG_PV, REG_ITMP1, -s1);
                        else {
-                               s4 ml=-s1, mh=0;
-                               while (ml<-32768) { ml+=65536; mh--; }
-                               M_LDA (REG_PV, REG_ITMP1, ml );
-                               M_LDAH (REG_PV, REG_PV, mh );
-                               }
+                               s4 ml = -s1, mh = 0;
+                               while (ml < -32768) { ml += 65536; mh--; }
+                               M_LDA(REG_PV, REG_ITMP1, ml);
+                               M_LDAH(REG_PV, REG_PV, mh);
+                       }
 
                        /* d contains return type */
 
                        if (d != TYPE_VOID) {
                                if (IS_INT_LNG_TYPE(iptr->dst->type)) {
-                                       s1 = reg_of_var(iptr->dst, REG_RESULT);
+                                       s1 = reg_of_var(rd, iptr->dst, REG_RESULT);
                                        M_TINTMOVE(iptr->dst->type, REG_RESULT, s1);
                                        store_reg_to_var_int(iptr->dst, s1);
-                                       }
-                               else {
-                                       s1 = reg_of_var(iptr->dst, REG_FRESULT);
+
+                               else {
+                                       s1 = reg_of_var(rd, iptr->dst, REG_FRESULT);
                                        M_FLTMOVE(REG_FRESULT, s1);
                                        store_reg_to_var_flt(iptr->dst, s1);
-                                       }
                                }
                        }
+                       }
                        break;
 
 
@@ -2844,8 +2723,11 @@ makeactualcall:
                        {
                        classinfo *super = (classinfo*) iptr->val.a;
                        
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+            codegen_threadcritrestart(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
                        var_to_reg_int(s1, src, REG_ITMP1);
-                       d = reg_of_var(iptr->dst, REG_ITMP3);
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP3);
                        if (s1 == d) {
                                M_MOV(s1, REG_ITMP1);
                                s1 = REG_ITMP1;
@@ -2856,11 +2738,11 @@ makeactualcall:
                                if (super->flags & ACC_INTERFACE) {        /* interface       */
                                        M_BEQ(9);
                                        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_LDATST(REG_ITMP2, REG_ITMP2, - super->index);
                                        M_BLE(5);
                                        M_ALD(REG_ITMP1, REG_ITMP1,
-                                             OFFSET(vftbl, interfacetable[0]) -
+                                             OFFSET(vftbl_t, interfacetable[0]) -
                                              super->index * sizeof(methodptr*));
                                        M_TST(REG_ITMP1);
                                        M_CLR(d);
@@ -2870,11 +2752,17 @@ makeactualcall:
                                else {                                     /* class           */
                                        M_BEQ(10);
                                        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);
-                                       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));
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+                                       codegen_threadcritstart(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
+                                       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(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
                                        M_ISUB(REG_ITMP1, REG_ITMP3, REG_ITMP1);
                                        M_CMPU(REG_ITMP1, REG_ITMP2);
                                        M_CLR(d);
@@ -2909,44 +2797,56 @@ makeactualcall:
                        {
                        classinfo *super = (classinfo*) iptr->val.a;
                        
-                       d = reg_of_var(iptr->dst, REG_ITMP1);
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+                       codegen_threadcritrestart(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
+                       d = reg_of_var(rd, iptr->dst, REG_ITMP1);
                        var_to_reg_int(s1, src, d);
                        if (iptr->op1) {                               /* class/interface */
                                M_TST(s1);
                                if (super->flags & ACC_INTERFACE) {        /* interface       */
                                        M_BEQ(7);
                                        M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
-                                       M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl, interfacetablelength));
+                                       M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, interfacetablelength));
                                        M_LDATST(REG_ITMP3, REG_ITMP3, - super->index);
                                        M_BLE(0);
-                                       codegen_addxcastrefs(mcodeptr);
+                                       codegen_addxcastrefs(cd, mcodeptr);
                                        M_ALD(REG_ITMP3, REG_ITMP2,
-                                             OFFSET(vftbl, interfacetable[0]) -
+                                             OFFSET(vftbl_t, interfacetable[0]) -
                                              super->index * sizeof(methodptr*));
                                        M_TST(REG_ITMP3);
                                        M_BEQ(0);
-                                       codegen_addxcastrefs(mcodeptr);
+                                       codegen_addxcastrefs(cd, mcodeptr);
                                        }
                                else {                                     /* class           */
                     M_BEQ(8 + (s1 == REG_ITMP1));
                     M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
-                    M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl, baseval));
-                    a = dseg_addaddress ((void*) super->vftbl);
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+                                       codegen_threadcritstart(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
+                    M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
+                    a = dseg_addaddress(cd, (void*) super->vftbl);
                     M_ALD(REG_ITMP2, REG_PV, a);
                     if (d != REG_ITMP1) {
-                        M_ILD(REG_ITMP1, REG_ITMP2, OFFSET(vftbl, baseval));
-                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl, diffval));
+                        M_ILD(REG_ITMP1, 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(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
                         M_ISUB(REG_ITMP3, REG_ITMP1, REG_ITMP3);
                         }
                     else {
-                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl, baseval));
+                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
                         M_ISUB(REG_ITMP3, REG_ITMP2, REG_ITMP3);
                         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(cd, (u1*) mcodeptr - cd->mcodebase);
+#endif
                         }
                     M_CMPU(REG_ITMP3, REG_ITMP2);
                     M_BGT(0);
-                    codegen_addxcastrefs(mcodeptr);
+                    codegen_addxcastrefs(cd, mcodeptr);
                     }
                                }
                        else
@@ -2961,7 +2861,14 @@ makeactualcall:
                        var_to_reg_int(s1, src, REG_ITMP1);
                        M_CMPI(s1, 0);
                        M_BLT(0);
-                       codegen_addxcheckarefs(mcodeptr);
+                       codegen_addxcheckarefs(cd, mcodeptr);
+                       break;
+
+               case ICMD_CHECKEXCEPTION:   /* ... ==> ...                            */
+
+                       M_CMPI(REG_RESULT, 0);
+                       M_BEQ(0);
+                       codegen_addxexceptionrefs(cd, mcodeptr);
                        break;
 
                case ICMD_MULTIANEWARRAY:/* ..., cnt1, [cnt2, ...] ==> ..., arrayref  */
@@ -2975,7 +2882,7 @@ makeactualcall:
                                var_to_reg_int(s2, src, REG_ITMP1);
                                M_CMPI(s2, 0);
                                M_BLT(0);
-                               codegen_addxcheckarefs(mcodeptr);
+                               codegen_addxcheckarefs(cd, mcodeptr);
 
                                /* copy sizes to stack (argument numbers >= INT_ARG_CNT)      */
 
@@ -2986,42 +2893,39 @@ makeactualcall:
 
                        /* a0 = dimension count */
 
-                       ICONST(argintregs[0], iptr->op1);
+                       ICONST(rd->argintregs[0], iptr->op1);
 
                        /* a1 = arraydescriptor */
 
-                       a = dseg_addaddress(iptr->val.a);
-                       M_ALD(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_LDA(argintregs[2], REG_SP, (INT_ARG_CNT + 6)*4);
+                       M_LDA(rd->argintregs[2], REG_SP, (INT_ARG_CNT + 6)*4);
 
-                       a = dseg_addaddress((void*) (builtin_nmultianewarray));
+                       a = dseg_addaddress(cd, (void *) builtin_nmultianewarray);
                        M_ALD(REG_PV, REG_PV, a);
                        M_MTCTR(REG_PV);
                        M_JSR;
-                       s1 = (int)((u1*) mcodeptr - mcodebase);
+                       s1 = (s4) ((u1 *) mcodeptr - cd->mcodebase);
                        M_MFLR(REG_ITMP1);
                        if (s1 <= 32768)
                                M_LDA (REG_PV, REG_ITMP1, -s1);
                        else {
                                s4 ml = -s1, mh = 0;
-                       while (ml < -32768) {ml += 65536; mh--;}
+                               while (ml < -32768) {ml += 65536; mh--;}
                                M_LDA(REG_PV, REG_ITMP1, ml);
                                M_LDAH(REG_PV, REG_PV, mh);
                            }
-                       s1 = reg_of_var(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: sprintf (logtext, "Unknown pseudo command: %d", iptr->opc);
-                        error();
+               default: error ("Unknown pseudo command: %d", iptr->opc);
        
-   
-
        } /* switch */
                
        } /* for instruction */
@@ -3030,34 +2934,34 @@ makeactualcall:
 
        src = bptr->outstack;
        len = bptr->outdepth;
-       MCODECHECK(64+len);
+       MCODECHECK(64 + len);
        while (src) {
                len--;
                if ((src->varkind != STACKVAR)) {
                        s2 = src->type;
                        if (IS_FLT_DBL_TYPE(s2)) {
                                var_to_reg_flt(s1, src, REG_FTMP1);
-                               if (!(interfaces[len][s2].flags & INMEMORY)) {
-                                       M_FLTMOVE(s1,interfaces[len][s2].regoff);
-                                       }
-                               else {
-                                       M_DST(s1, REG_SP, 4 * interfaces[len][s2].regoff);
-                                       }
+                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                       M_FLTMOVE(s1, rd->interfaces[len][s2].regoff);
+
+                               } else {
+                                       M_DST(s1, REG_SP, rd->interfaces[len][s2].regoff * 4);
                                }
-                       else {
+
+                       } else {
                                var_to_reg_int(s1, src, REG_ITMP1);
-                               if (!(interfaces[len][s2].flags & INMEMORY)) {
-                                       M_TINTMOVE(s2,s1,interfaces[len][s2].regoff);
-                                       }
-                               else {
-                                       M_IST(s1, REG_SP, 4 * interfaces[len][s2].regoff);
+                               if (!(rd->interfaces[len][s2].flags & INMEMORY)) {
+                                       M_TINTMOVE(s2, s1, rd->interfaces[len][s2].regoff);
+
+                               else {
+                                       M_IST(s1, REG_SP, rd->interfaces[len][s2].regoff * 4);
                                        if (IS_2_WORD_TYPE(s2))
-                                               M_IST(secondregs[s1], REG_SP, 4 * interfaces[len][s2].regoff + 4);
-                                       }
+                                               M_IST(rd->secondregs[s1], REG_SP, rd->interfaces[len][s2].regoff * 4 + 4);
                                }
                        }
-               src = src->prev;
                }
+               src = src->prev;
+       }
        } /* if (bptr -> flags >= BBREACHED) */
        } /* for basic block */
 
@@ -3067,153 +2971,247 @@ makeactualcall:
        /* generate bound check stubs */
 
        s4 *xcodeptr = NULL;
-       
-       for (; xboundrefs != NULL; xboundrefs = xboundrefs->next) {
-               if ((exceptiontablelength == 0) && (xcodeptr != NULL)) {
-                       gen_resolvebranch((u1*) mcodebase + xboundrefs->branchpos, 
-                               xboundrefs->branchpos, (u1*) xcodeptr - (u1*) mcodebase - 4);
-                       continue;
-                       }
-
+       branchref *bref;
 
-               gen_resolvebranch((u1*) mcodebase + xboundrefs->branchpos, 
-                                 xboundrefs->branchpos, (u1*) mcodeptr - mcodebase);
+       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_LDA(REG_ITMP2_XPC, REG_PV, xboundrefs->branchpos - 4);
+               /* move index register into REG_ITMP1 */
+               M_MOV(bref->reg, REG_ITMP1);
+               M_LDA(REG_ITMP2_XPC, REG_PV, bref->branchpos - 4);
 
                if (xcodeptr != NULL) {
-                       M_BR((xcodeptr-mcodeptr)-1);
-                       }
-               else {
+                       M_BR(xcodeptr - mcodeptr - 1);
+
+               else {
                        xcodeptr = mcodeptr;
 
-                       a = dseg_addaddress(proto_java_lang_ArrayIndexOutOfBoundsException);
-                       M_ALD(REG_ITMP1_XPTR, REG_PV, a);
+                       M_IADD_IMM(REG_SP, -1 * 8, REG_SP);
+                       M_IST(REG_ITMP2_XPC, REG_SP, 0 * 8);
+
+                       M_MOV(REG_ITMP1, rd->argintregs[0]);
+
+                       a = dseg_addaddress(cd, new_arrayindexoutofboundsexception);
+                       M_ALD(REG_ITMP2, REG_PV, a);
+                       M_MTCTR(REG_ITMP2);
+                       M_JSR;
+                       M_MOV(REG_RESULT, REG_ITMP1_XPTR);
+
+                       M_ILD(REG_ITMP2_XPC, REG_SP, 0 * 8);
+                       M_IADD_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_MTCTR(REG_ITMP3);
                        M_RTS;
-                       }
                }
+       }
 
        /* generate negative array size check stubs */
 
        xcodeptr = NULL;
        
-       for (; xcheckarefs != NULL; xcheckarefs = xcheckarefs->next) {
-               if ((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 ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - (u1 *) 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_LDA(REG_ITMP2_XPC, REG_PV, xcheckarefs->branchpos - 4);
+               M_LDA(REG_ITMP2_XPC, REG_PV, bref->branchpos - 4);
 
                if (xcodeptr != NULL) {
-                       M_BR((xcodeptr-mcodeptr)-1);
-                       }
-               else {
+                       M_BR(xcodeptr - mcodeptr - 1);
+
+               else {
                        xcodeptr = mcodeptr;
 
-                       a = dseg_addaddress(proto_java_lang_NegativeArraySizeException);
-                       M_ALD(REG_ITMP1_XPTR, REG_PV, a);
+            M_IADD_IMM(REG_SP, -1 * 8, REG_SP);
+            M_IST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(asm_handle_exception);
+            a = dseg_addaddress(cd, new_negativearraysizeexception);
+            M_ALD(REG_ITMP2, REG_PV, a);
+            M_MTCTR(REG_ITMP2);
+            M_JSR;
+            M_MOV(REG_RESULT, REG_ITMP1_XPTR);
+
+            M_ILD(REG_ITMP2_XPC, REG_SP, 0 * 8);
+            M_IADD_IMM(REG_SP, 1 * 8, REG_SP);
+
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP3, REG_PV, a);
 
                        M_MTCTR(REG_ITMP3);
                        M_RTS;
-                       }
                }
+       }
 
        /* generate cast check stubs */
 
        xcodeptr = NULL;
        
-       for (; xcastrefs != NULL; xcastrefs = xcastrefs->next) {
-               if ((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 ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - (u1 *) 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_LDA(REG_ITMP2_XPC, REG_PV, xcastrefs->branchpos - 4);
+               M_LDA(REG_ITMP2_XPC, REG_PV, bref->branchpos - 4);
 
                if (xcodeptr != NULL) {
-                       M_BR((xcodeptr-mcodeptr)-1);
-                       }
-               else {
+                       M_BR(xcodeptr - mcodeptr - 1);
+
+               else {
                        xcodeptr = mcodeptr;
 
-                       a = dseg_addaddress(proto_java_lang_ClassCastException);
-                       M_ALD(REG_ITMP1_XPTR, REG_PV, a);
+            M_IADD_IMM(REG_SP, -1 * 8, REG_SP);
+            M_IST(REG_ITMP2_XPC, REG_SP, 0 * 8);
+
+            a = dseg_addaddress(cd, new_classcastexception);
+            M_ALD(REG_ITMP2, REG_PV, a);
+            M_MTCTR(REG_ITMP2);
+            M_JSR;
+            M_MOV(REG_RESULT, REG_ITMP1_XPTR);
 
-                       a = dseg_addaddress(asm_handle_exception);
+            M_ILD(REG_ITMP2_XPC, REG_SP, 0 * 8);
+            M_IADD_IMM(REG_SP, 1 * 8, REG_SP);
+
+                       a = dseg_addaddress(cd, asm_handle_exception);
                        M_ALD(REG_ITMP3, REG_PV, a);
 
                        M_MTCTR(REG_ITMP3);
                        M_RTS;
-                       }
                }
+       }
+
+       /* generate exception check stubs */
+
+       xcodeptr = NULL;
+
+       for (bref = cd->xexceptionrefs; bref != NULL; bref = bref->next) {
+               if ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - (u1 *) cd->mcodebase - 4);
+                       continue;
+               }
+
+               gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                 bref->branchpos,
+                                                 (u1 *) mcodeptr - cd->mcodebase);
+
+               MCODECHECK(14);
+
+               M_LDA(REG_ITMP2_XPC, REG_PV, bref->branchpos - 4);
 
+               if (xcodeptr != NULL) {
+                       M_BR(xcodeptr - mcodeptr - 1);
+
+               } else {
+                       xcodeptr = mcodeptr;
+
+                       /* XXX this cannot work - there is no link area */
+            M_IADD_IMM(REG_SP, -1 * 8, REG_SP);
+            M_IST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-#ifdef SOFTNULLPTRCHECK
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+            a = dseg_addaddress(cd, builtin_get_exceptionptrptr);
+            M_ALD(REG_ITMP2, REG_PV, a);
+            M_MTCTR(REG_ITMP2);
+            M_JSR;
+
+                       /* get the exceptionptr from the ptrprt and clear it */
+            M_ALD(REG_ITMP1_XPTR, REG_RESULT, 0);
+                       M_CLR(REG_ITMP3);
+                       M_AST(REG_ITMP3, REG_RESULT, 0);
+#else
+                       a = dseg_addaddress(cd, &_exceptionptr);
+                       M_ALD(REG_ITMP2, REG_PV, a);
+
+                       M_ALD(REG_ITMP1_XPTR, REG_ITMP2, 0);
+                       M_CLR(REG_ITMP3);
+                       M_AST(REG_ITMP3, REG_ITMP2, 0);
+#endif
+
+            M_ILD(REG_ITMP2_XPC, REG_SP, 0 * 8);
+            M_IADD_IMM(REG_SP, 1 * 8, REG_SP);
+
+                       a = dseg_addaddress(cd, asm_handle_exception);
+                       M_ALD(REG_ITMP3, REG_PV, a);
+                       M_MTCTR(REG_ITMP3);
+                       M_RTS;
+               }
+       }
 
        /* generate null pointer check stubs */
 
        xcodeptr = NULL;
 
-       for (; xnullrefs != NULL; xnullrefs = xnullrefs->next) {
-               if ((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 ((m->exceptiontablelength == 0) && (xcodeptr != NULL)) {
+                       gen_resolvebranch((u1 *) cd->mcodebase + bref->branchpos, 
+                                                         bref->branchpos,
+                                                         (u1 *) xcodeptr - (u1 *) 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_LDA(REG_ITMP2_XPC, REG_PV, xnullrefs->branchpos - 4);
+               M_LDA(REG_ITMP2_XPC, REG_PV, bref->branchpos - 4);
 
                if (xcodeptr != NULL) {
-                       M_BR((xcodeptr-mcodeptr)-1);
-                       }
-               else {
+                       M_BR(xcodeptr - mcodeptr - 1);
+
+               else {
                        xcodeptr = mcodeptr;
 
-                       a = dseg_addaddress(proto_java_lang_NullPointerException);
-                       M_ALD(REG_ITMP1_XPTR, REG_PV, a);
+            M_IADD_IMM(REG_SP, -1 * 8, REG_SP);
+            M_IST(REG_ITMP2_XPC, REG_SP, 0 * 8);
 
-                       a = dseg_addaddress(asm_handle_exception);
-                       M_ALD(REG_ITMP3, REG_PV, a);
+            a = dseg_addaddress(cd, new_nullpointerexception);
+            M_ALD(REG_ITMP2, REG_PV, a);
+            M_MTCTR(REG_ITMP2);
+            M_JSR;
+            M_MOV(REG_RESULT, REG_ITMP1_XPTR);
+
+            M_ILD(REG_ITMP2_XPC, REG_SP, 0 * 8);
+            M_IADD_IMM(REG_SP, 1 * 8, REG_SP);
 
+                       a = dseg_addaddress(cd, asm_handle_exception);
+                       M_ALD(REG_ITMP3, REG_PV, a);
                        M_MTCTR(REG_ITMP3);
                        M_RTS;
-                       }
                }
-
-#endif
+       }
        }
 
-       codegen_finish((int)((u1*) mcodeptr - mcodebase));
+       codegen_finish(m, cd, (s4) ((u1 *) mcodeptr - cd->mcodebase));
 
-    docacheflush((void*) method->entrypoint, ((u1*) mcodeptr - mcodebase));
+    asm_cacheflush((void *) m->entrypoint, ((u1 *) mcodeptr - cd->mcodebase));
 }
 
 
@@ -3225,7 +3223,7 @@ makeactualcall:
 
 #define COMPSTUBSIZE 6
 
-u1 *createcompilerstub (methodinfo *m)
+u1 *createcompilerstub(methodinfo *m)
 {
        s4 *s = CNEW (s4, COMPSTUBSIZE);    /* memory to hold the stub            */
        s4 *mcodeptr = s;                   /* code generation pointer            */
@@ -3238,10 +3236,11 @@ u1 *createcompilerstub (methodinfo *m)
        s[4] = (s4) m;                      /* literals to be adressed            */
        s[5] = (s4) asm_call_jit_compiler;  /* jump directly via PV from above    */
 
-    docacheflush((void*) s, (char*) mcodeptr - (char*) s);
+    asm_cacheflush((void*) s, (u1*) mcodeptr - (u1*) s);
 
-#ifdef STATISTICS
-       count_cstub_len += COMPSTUBSIZE * 4;
+#if defined(STATISTICS)
+       if (opt_stat)
+               count_cstub_len += COMPSTUBSIZE * 4;
 #endif
 
        return (u1*) s;
@@ -3254,149 +3253,366 @@ u1 *createcompilerstub (methodinfo *m)
 
 *******************************************************************************/
 
-void removecompilerstub (u1 *stub) 
+void removecompilerstub(u1 *stub) 
 {
-       CFREE (stub, COMPSTUBSIZE * 4);
+       CFREE(stub, COMPSTUBSIZE * 4);
 }
 
+
 /* function: createnativestub **************************************************
 
        creates a stub routine which calls a native method
 
 *******************************************************************************/
 
-#define NATIVESTUBSIZE 80
-#define NATIVESTUBOFFSET 8
+#define NATIVESTUBSIZE      200
+#define NATIVESTUBOFFSET    9
 
-u1 *createnativestub (functionptr f, methodinfo *m)
+u1 *createnativestub(functionptr f, methodinfo *m)
 {
-       int disp;
-       s4 *s = CNEW (s4, NATIVESTUBSIZE);  /* memory to hold the stub            */
-       s4 *cs = s + NATIVESTUBOFFSET;
-       s4 *mcodeptr = cs;                  /* code generation pointer            */
-
-       *(cs-1) = (s4) f;                   /* address of native method           */
-       *(cs-2) = (s4) (&exceptionptr);     /* address of exceptionptr            */
-       *(cs-3) = (s4) asm_handle_nat_exception; /* addr of asm exception handler */
-       *(cs-4) = (s4) (&env);              /* addr of jni_environement           */
-//     *(cs-5) = (s4) asm_builtin_trace;
-       *(cs-6) = (s4) m;
-//     *(cs-7) = (s4) asm_builtin_exittrace;
-       *(cs-8) = (s4) builtin_trace_exception;
+       s4 *s;                              /* memory to hold the stub            */
+       s4 *cs;
+       s4 *mcodeptr;                       /* code generation pointer            */
+       s4 stackframesize = 0;              /* size of stackframe if needed       */
+       s4 disp;
+       registerdata *rd;
+       t_inlining_globals *id;
+       s4 dumpsize;
 
-#if 0
-       printf("stub: ");
-       utf_display(m->class->name);
-       printf(".");
-       utf_display(m->name);
-       printf(" 0x%p\n", cs);
-#endif
+       /* mark start of dump memory area */
 
-       M_MFLR (REG_ITMP1);
-       M_AST  (REG_ITMP1, REG_SP, 8);      /* store return address               */
-       M_LDA  (REG_SP, REG_SP, -64);       /* build up stackframe                */
+       dumpsize = dump_size();
+
+       /* setup registers before using it */
+       
+       rd = DNEW(registerdata);
+       id = DNEW(t_inlining_globals);
+
+       inlining_setup(m, id);
+       reg_setup(m, rd, id);
+
+       descriptor2types(m);                /* set paramcount and paramtypes      */
+
+       s = CNEW(s4, NATIVESTUBSIZE);
+       cs = s + NATIVESTUBOFFSET;
+       mcodeptr = cs;
+
+       *(cs-1) = (u4) f;                   /* address of native method           */
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       *(cs-2) = (u4) builtin_get_exceptionptrptr;
+#else
+       *(cs-2) = (u4) (&_exceptionptr);    /* address of exceptionptr            */
+#endif
+       *(cs-3) = (u4) asm_handle_nat_exception; /* addr of asm exception handler */
+       *(cs-4) = (u4) (&env);              /* addr of jni_environement           */
+       *(cs-5) = (u4) builtin_trace_args;
+       *(cs-6) = (u4) m;
+       *(cs-7) = (u4) builtin_displaymethodstop;
+       *(cs-8) = (u4) m->class;
+       *(cs-9) = (u4) asm_check_clinit;
+
+       M_MFLR(REG_ITMP1);
+       M_AST(REG_ITMP1, REG_SP, 8);        /* store return address               */
+       M_LDA(REG_SP, REG_SP, -64);         /* build up stackframe                */
+
+       /* 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 * 4);     /* class                    */
+                       M_ALD(REG_PV, REG_PV, -9 * 4);        /* asm_check_clinit         */
+                       M_MTCTR(REG_PV);
+                       M_JSR;
+                       disp = -(s4) (mcodeptr - cs) * 4;
+                       M_MFLR(REG_ITMP1);
+                       M_LDA(REG_PV, REG_ITMP1, disp);       /* recompute pv from ra     */
+               }
+       }
 
-#if 0
        if (runverbose) {
-               M_ALD(REG_ITMP1, REG_PV, -6*4);
-               M_ALD(REG_PV, REG_PV, -5*4);
+               s4 p, t, s1, d, a;
+        s4 longargs = 0;
+        s4 fltargs = 0;
+               s4 dblargs = 0;
+
+/*          M_MFLR(REG_ITMP3); */
+               /* XXX must be a multiple of 16 */
+        M_LDA(REG_SP, REG_SP, -(24 + (INT_ARG_CNT + FLT_ARG_CNT + 1) * 8));
+
+/*          M_IST(REG_ITMP3, REG_SP, 24 + (INT_ARG_CNT + FLT_ARG_CNT) * 8); */
+
+               M_CLR(REG_ITMP1);
+
+        /* save all arguments into the reserved stack space */
+        for (p = 0; p < m->paramcount && p < TRACE_ARGS_NUM; p++) {
+            t = m->paramtypes[p];
+
+            if (IS_INT_LNG_TYPE(t)) {
+                /* overlapping u8's are on the stack */
+                if ((p + longargs + dblargs) < (INT_ARG_CNT - IS_2_WORD_TYPE(t))) {
+                    s1 = rd->argintregs[p + longargs + dblargs];
+
+                    if (!IS_2_WORD_TYPE(t)) {
+                        M_IST(REG_ITMP1, REG_SP, 24 + p * 8);
+                        M_IST(s1, REG_SP, 24 + p * 8 + 4);
+
+                    } else {
+                        M_IST(s1, REG_SP, 24 + p  * 8);
+                        M_IST(rd->secondregs[s1], REG_SP, 24 + p * 8 + 4);
+                        longargs++;
+                    }
+
+                } else {
+                                       /* we do not have a data segment here */
+                    /* a = dseg_adds4(cd, 0xdeadbeef);
+                                          M_ILD(REG_ITMP1, REG_PV, a); */
+                                       M_LDA(REG_ITMP1, REG_ZERO, -1);
+                    M_IST(REG_ITMP1, REG_SP, 24 + p * 8);
+                    M_IST(REG_ITMP1, REG_SP, 24 + p * 8 + 4);
+                }
+
+            } else {
+                if ((fltargs + dblargs) < FLT_ARG_CNT) {
+                    s1 = rd->argfltregs[fltargs + dblargs];
+
+                    if (!IS_2_WORD_TYPE(t)) {
+                        M_IST(REG_ITMP1, REG_SP, 24 + p * 8);
+                        M_FST(s1, REG_SP, 24 + p * 8 + 4);
+                        fltargs++;
+
+                    } else {
+                        M_DST(s1, REG_SP, 24 + p * 8);
+                        dblargs++;
+                    }
+
+                } else {
+                    /* this should not happen */
+                }
+            }
+        }
+
+        /* TODO: save remaining integer and flaot argument registers */
+
+        /* load first 4 arguments into integer argument registers */
+        for (p = 0; p < 8; p++) {
+            d = rd->argintregs[p];
+            M_ILD(d, REG_SP, 24 + p * 4);
+        }
+
+        M_ALD(REG_ITMP1, REG_PV, -6 * 4);
+        M_AST(REG_ITMP1, REG_SP, 11 * 8);    /* 24 (linkage area) + 32 (4 * s8 parameter area regs) + 32 (4 * s8 parameter area stack) = 88 */
+               M_ALD(REG_ITMP2, REG_PV, -5 * 4);
+        M_MTCTR(REG_ITMP2);
+        M_JSR;
+
+        longargs = 0;
+        fltargs = 0;
+        dblargs = 0;
+
+        /* restore arguments into the reserved stack space */
+        for (p = 0; p < m->paramcount && p < TRACE_ARGS_NUM; p++) {
+            t = m->paramtypes[p];
+
+            if (IS_INT_LNG_TYPE(t)) {
+                if ((p + longargs + dblargs) < INT_ARG_CNT) {
+                    s1 = rd->argintregs[p + longargs + dblargs];
+
+                    if (!IS_2_WORD_TYPE(t)) {
+                        M_ILD(s1, REG_SP, 24 + p * 8 + 4);
+
+                    } else {
+                        M_ILD(s1, REG_SP, 24 + p  * 8);
+                        M_ILD(rd->secondregs[s1], REG_SP, 24 + p * 8 + 4);
+                        longargs++;
+                    }
+                }
+
+            } else {
+                if ((fltargs + dblargs) < FLT_ARG_CNT) {
+                    s1 = rd->argfltregs[fltargs + dblargs];
+
+                    if (!IS_2_WORD_TYPE(t)) {
+                        M_FLD(s1, REG_SP, 24 + p * 8 + 4);
+                        fltargs++;
 
-//             M_JSR(REG_RA, REG_PV);
-               disp = -(int) (mcodeptr - (s4*) cs)*4;
-               M_LDA(REG_PV, REG_ITMP1, disp);
+                    } else {
+                        M_DLD(s1, REG_SP, 24 + p * 8);
+                        dblargs++;
+                    }
+                }
+            }
+        }
+
+/*          M_ILD(REG_ITMP3, REG_SP, 24 + (INT_ARG_CNT + FLT_ARG_CNT) * 8); */
+
+        M_LDA(REG_SP, REG_SP, 24 + (INT_ARG_CNT + FLT_ARG_CNT + 1) * 8);
+/*          M_MTLR(REG_ITMP3); */
        }
-#endif
 
-       reg_init(m);
+       /* save argument registers on stack -- if we have to */
+       if ((m->flags & ACC_STATIC && m->paramcount > (INT_ARG_CNT - 2)) ||
+               m->paramcount > (INT_ARG_CNT - 1)) {
+               s4 i;
+               s4 paramshiftcnt = (m->flags & ACC_STATIC) ? 2 : 1;
+               s4 stackparamcnt = (m->paramcount > INT_ARG_CNT) ? m->paramcount - INT_ARG_CNT : 0;
 
-       M_MOV  (argintregs[6],argintregs[7]); 
-       M_MOV  (argintregs[5],argintregs[6]); 
-       M_MOV  (argintregs[4],argintregs[5]); 
-       M_MOV  (argintregs[3],argintregs[4]);
-       M_MOV  (argintregs[2],argintregs[3]);
-       M_MOV  (argintregs[1],argintregs[2]);
-       M_MOV  (argintregs[0],argintregs[1]);
-       
-       M_ALD  (argintregs[0], REG_PV, -4*4);/* load adress of jni_environement   */
+               stackframesize = stackparamcnt + paramshiftcnt;
+
+               M_LDA(REG_SP, REG_SP, -stackframesize * 8);
 
-       M_ALD  (REG_PV, REG_PV, -1*4);      /* load adress of native method       */
+               panic("nativestub");
+       }
+
+       if (m->flags & ACC_STATIC) {
+        M_MOV(rd->argintregs[5], rd->argintregs[7]);
+        M_MOV(rd->argintregs[4], rd->argintregs[6]);
+        M_MOV(rd->argintregs[3], rd->argintregs[5]);
+        M_MOV(rd->argintregs[2], rd->argintregs[4]);
+        M_MOV(rd->argintregs[1], rd->argintregs[3]);
+        M_MOV(rd->argintregs[0], rd->argintregs[2]);
+
+               /* put class into second argument register */
+               M_ALD(rd->argintregs[1], REG_PV, -8 * 4);
+
+       } else {
+               M_MOV(rd->argintregs[6], rd->argintregs[7]); 
+               M_MOV(rd->argintregs[5], rd->argintregs[6]); 
+               M_MOV(rd->argintregs[4], rd->argintregs[5]); 
+               M_MOV(rd->argintregs[3], rd->argintregs[4]);
+               M_MOV(rd->argintregs[2], rd->argintregs[3]);
+               M_MOV(rd->argintregs[1], rd->argintregs[2]);
+               M_MOV(rd->argintregs[0], rd->argintregs[1]);
+       }
+
+       /* put env into first argument register */
+       M_ALD(rd->argintregs[0], REG_PV, -4 * 4);
+
+       M_ALD(REG_PV, REG_PV, -1 * 4);      /* load adress of native method       */
        M_MTCTR(REG_PV);
        M_JSR;
+       disp = -(s4) (mcodeptr - cs) * 4;
+       M_MFLR(REG_ITMP1);
+       M_LDA(REG_PV, REG_ITMP1, disp);     /* recompute pv from ra               */
 
-       disp = -(int) (mcodeptr - cs)*4;
-       M_MFLR (REG_ITMP1);
-       M_LDA  (REG_PV, REG_ITMP1, disp);   /* recompute pv from ra               */
-       M_ALD  (REG_ITMP2, REG_PV, -2*4);   /* get address of exceptionptr        */
-
-       M_ALD  (REG_ITMP1, REG_ITMP2, 0);   /* load exception into reg. itmp1     */
-       M_TST  (REG_ITMP1);
-       M_BNE  (4 + (runverbose ? 0 : 0));  /* if no exception then return        */
+       /* remove stackframe if there is one */
+       if (stackframesize) {
+               M_LDA(REG_SP, REG_SP, stackframesize * 8);
+       }
 
-#if 0
+       /* 20 instructions */
        if (runverbose) {
-               M_ALD(argintregs[0], REG_PV, -6*4);
-               M_MOV(REG_RESULT, argintregs[1]);
-               M_FMOV(REG_FRESULT, argfltregs[2]);
-               M_FMOV(REG_FRESULT, argfltregs[3]);
-               M_ALD(REG_PV, REG_PV, -7*4);
-//             M_JSR(REG_RA, REG_PV);
+               M_MFLR(REG_ITMP3);
+               M_LDA(REG_SP, REG_SP, -10 * 8);
+               M_DST(REG_FRESULT, REG_SP, 48+0);
+               M_IST(REG_RESULT, REG_SP, 48+8);
+               M_AST(REG_ITMP3, REG_SP, 48+12);
+               M_IST(REG_RESULT2, REG_SP, 48+16);
+
+               /* keep this order */
+               switch (m->returntype) {
+               case TYPE_INT:
+               case TYPE_ADDRESS:
+                       M_MOV(REG_RESULT, rd->argintregs[2]);
+                       M_CLR(rd->argintregs[1]);
+                       break;
+
+               case TYPE_LONG:
+                       M_MOV(REG_RESULT2, rd->argintregs[2]);
+                       M_MOV(REG_RESULT, rd->argintregs[1]);
+                       break;
+               }
+               M_ALD(rd->argintregs[0], REG_PV, -6 * 4);
+
+               M_FLTMOVE(REG_FRESULT, rd->argfltregs[0]);
+               M_FLTMOVE(REG_FRESULT, rd->argfltregs[1]);
+               M_ALD(REG_ITMP2, REG_PV, -7 * 4);/* builtin_displaymethodstop         */
+               M_MTCTR(REG_ITMP2);
+               M_JSR;
+               M_DLD(REG_FRESULT, REG_SP, 48+0);
+               M_ILD(REG_RESULT, REG_SP, 48+8);
+               M_ALD(REG_ITMP3, REG_SP, 48+12);
+               M_ILD(REG_RESULT2, REG_SP, 48+16);
+               M_LDA(REG_SP, REG_SP, 10 * 8);
+               M_MTLR(REG_ITMP3);
+       }
+
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       if (IS_FLT_DBL_TYPE(m->returntype))
+               if (IS_2_WORD_TYPE(m->returntype))
+                       M_DST(REG_FRESULT, REG_SP, 56);
+               else
+                       M_FST(REG_FRESULT, REG_SP, 56);
+       else {
+               M_IST(REG_RESULT, REG_SP, 56);
+               if (IS_2_WORD_TYPE(m->returntype))
+                       M_IST(REG_RESULT2, REG_SP, 60);
+       }
+
+       M_ALD(REG_ITMP2, REG_PV, -2 * 4);   /* builtin_get_exceptionptrptr        */
+       M_MTCTR(REG_ITMP2);
+       M_JSR;
+       disp = -(s4) (mcodeptr - (s4 *) cs) * 4;
+       M_MFLR(REG_ITMP1);
+       M_LDA(REG_PV, REG_ITMP1, disp);
+       M_MOV(REG_RESULT, REG_ITMP2);
+
+       if (IS_FLT_DBL_TYPE(m->returntype))
+               if (IS_2_WORD_TYPE(m->returntype))
+                       M_DLD(REG_FRESULT, REG_SP, 56);
+               else
+                       M_FLD(REG_FRESULT, REG_SP, 56);
+       else {
+               M_ILD(REG_RESULT, REG_SP, 56);
+               if (IS_2_WORD_TYPE(m->returntype))
+                       M_ILD(REG_RESULT2, REG_SP, 60);
        }
+#else
+       M_ALD(REG_ITMP2, REG_PV, -2 * 4);   /* get address of exceptionptr        */
 #endif
+       M_ALD(REG_ITMP1, REG_ITMP2, 0);     /* load exception into reg. itmp1     */
+       M_TST(REG_ITMP1);
+       M_BNE(4);                           /* if no exception then return        */
 
-       M_ALD  (REG_ITMP1, REG_SP, 64+8);         /* load return address                */
-       M_MTLR (REG_ITMP1);
-       M_LDA  (REG_SP, REG_SP, 64);         /* remove stackframe                  */
+       M_ALD(REG_ITMP1, REG_SP, 64 + 8);   /* load return address                */
+       M_MTLR(REG_ITMP1);
+       M_LDA(REG_SP, REG_SP, 64);          /* remove stackframe                  */
 
        M_RET;
        
-       M_CLR  (REG_ITMP3);
-       M_AST  (REG_ITMP3, REG_ITMP2, 0);    /* store NULL into exceptionptr       */
-
-#if 0
-       if (runverbose) {
-               M_LDA(REG_SP, REG_SP, -8);
-               M_AST(REG_ITMP1, REG_SP, 0);
-               M_MOV(REG_ITMP1, argintregs[0]);
-               M_ALD(argintregs[1], REG_PV, -6*4);
-               M_ALD(argintregs[2], REG_SP, 0);
-               M_CLR(argintregs[3]);
-               M_ALD(REG_PV, REG_PV, -8*4);
-//             M_JSR(REG_RA, REG_PV);
-               disp = -(int) (mcodeptr - cs)*4;
-               M_LDA  (REG_PV, REG_ITMP1, disp);
-               M_ALD(REG_ITMP1, REG_SP, 0);
-               M_LDA(REG_SP, REG_SP, 8);
-       }
-#endif
+       M_CLR(REG_ITMP3);
+       M_AST(REG_ITMP3, REG_ITMP2, 0);     /* store NULL into exceptionptr       */
 
-       M_ALD  (REG_ITMP3, REG_SP, 64+8);         /* load return address                */
-       M_MTLR (REG_ITMP3);
-       M_LDA  (REG_SP, REG_SP, 64);         /* remove stackframe                  */
+       M_ALD(REG_ITMP3, REG_SP, 64 + 8);   /* load return address                */
+       M_MTLR(REG_ITMP3);
+       M_LDA(REG_SP, REG_SP, 64);          /* remove stackframe                  */
 
-       M_LDA  (REG_ITMP2, REG_ITMP1, -4);     /* move fault address into reg. itmp2 */
+       M_LDA(REG_ITMP2, REG_ITMP1, -4);    /* move fault address into reg. itmp2 */
 
-       M_ALD  (REG_ITMP3, REG_PV, -3*4);   /* load asm exception handler address */
+       M_ALD(REG_ITMP3, REG_PV, -3 * 4);   /* load asm exception handler address */
        M_MTCTR(REG_ITMP3);
        M_RTS;
        
 #if 0
-       {
-               static int stubprinted;
-               if (!stubprinted)
-                       printf("stubsize: %d/2\n", (int) (p - (s4*) s));
-               stubprinted = 1;
-       }
+       dolog_plain("stubsize: %d (for %d params)\n", (int) (mcodeptr - (s4 *) s), m->paramcount);
 #endif
 
-    docacheflush((void*) s, (char*) mcodeptr - (char*) s);
+    asm_cacheflush((void *) s, (u1*) mcodeptr - (u1*) s);
 
-#ifdef STATISTICS
-       count_nstub_len += NATIVESTUBSIZE * 4;
+#if defined(STATISTICS)
+       if (opt_stat)
+               count_nstub_len += NATIVESTUBSIZE * 4;
 #endif
 
+       /* release dump area */
+
+       dump_release(dumpsize);
+
        return (u1*) (s + NATIVESTUBOFFSET);
 }
 
+
 /* function: removenativestub **************************************************
 
     removes a previously created native-stub from memory
@@ -3405,34 +3621,7 @@ u1 *createnativestub (functionptr f, methodinfo *m)
 
 void removenativestub(u1 *stub)
 {
-       CFREE ((s4*) stub - NATIVESTUBOFFSET, NATIVESTUBSIZE * 4);
-}
-
-
-java_objectheader *asm_docalljavamethod(methodinfo *m, void *arg1, void *arg2,
-                                                                               void *arg3, void *arg4);
-
-java_objectheader *asm_calljavamethod(methodinfo *m, void *arg1, void *arg2,
-                                                                         void *arg3, void *arg4)
-{
-       java_objectheader *r = asm_docalljavamethod(m, arg1, arg2, arg3, arg4);
-       return (exceptionptr ? r : NULL);
-}
-
-
-java_objectheader *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
-                                                                               void *arg3, void *arg4)
-{
-       return asm_docalljavamethod(m, arg1, arg2, arg3, arg4);
-}
-
-
-void asm_cacheflush(u1 *p, long bytelen);
-
-
-void docacheflush(u1 *p, long bytelen)
-{
-       asm_cacheflush(p, bytelen);
+       CFREE((s4 *) stub - NATIVESTUBOFFSET, NATIVESTUBSIZE * 4);
 }