* src/native/localref.c: Moved to C++.
[cacao.git] / src / vm / jit / s390 / codegen.c
index b17349e3f4e589b3cbb733e78356609d74e2d328..7db818cadc79711f99fd64de3a56a1a58bc99968 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/s390/codegen.c - machine code generator for s390
 
-   Copyright (C) 1996-2005, 2006, 2007 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
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
 #include <stdint.h>
 #include <stdio.h>
 
-#include "native/jni.h"
-#include "native/localref.h"
-#include "native/native.h"
+#include "vm/jit/s390/arch.h"
+#include "vm/jit/s390/codegen.h"
+#include "vm/jit/s390/emit.h"
+#include "vm/jit/s390/md-abi.h"
+
+#include "native/localref.hpp"
+#include "native/native.hpp"
 
 #include "mm/memory.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/lock-common.h"
-# include "threads/native/lock.h"
-#endif
+#include "threads/lock.hpp"
 
-#include "vmcore/loader.h"
-#include "vmcore/options.h"
-#include "vmcore/statistics.h"
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/exceptions.hpp"
 #include "vm/global.h"
+#include "vm/loader.hpp"
+#include "vm/options.h"
+#include "vm/statistics.h"
+#include "vm/types.h"
+#include "vm/vm.hpp"
+
 #include "vm/jit/abi.h"
 #if defined(ENABLE_LSRA)
 # include "vm/jit/allocator/lsra.h"
 #endif
 #include "vm/jit/asmpart.h"
-#include "vm/jit/codegen-common.h"
+#include "vm/jit/codegen-common.hpp"
 #include "vm/jit/dseg.h"
-#include "vm/jit/emit-common.h"
-#include "vm/jit/jit.h"
+#include "vm/jit/emit-common.hpp"
+#include "vm/jit/jit.hpp"
+#include "vm/jit/linenumbertable.hpp"
 #include "vm/jit/methodheader.h"
 #include "vm/jit/parse.h"
-#include "vm/jit/patcher-common.h"
+#include "vm/jit/patcher-common.hpp"
 #include "vm/jit/reg.h"
-#include "vm/jit/replace.h"
-#include "vm/jit/s390/arch.h"
-#include "vm/jit/s390/codegen.h"
-#include "vm/jit/s390/emit.h"
-#include "vm/jit/s390/md-abi.h"
-#include "vm/jit/stacktrace.h"
-#include "vm/types.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/jit/replace.hpp"
+#include "vm/jit/stacktrace.hpp"
+#include "vm/jit/trap.h"
+
 
 /* DO__LOG generates a call to do__log. No registers are destroyed,
  * so you may use it anywhere. regs is an array containing all general
  * purpose registers.
  */
 
+/*
 static void do__log(u4 *regs) {
-       /* insert stuff here */
 }
+*/
 
 #define DO__LOG \
        N_AHI(REG_SP, -200); \
@@ -89,7 +88,11 @@ static void do__log(u4 *regs) {
        N_LM(R0, R15, 96, REG_SP); \
        N_AHI(REG_SP, 200);
 
-#define SUPPORT_HERCULES 1
+/* If the following macro is defined, workaround code for hercules quirks
+ * is generated
+ */
+
+/* #define SUPPORT_HERCULES 1 */
 
 /* codegen *********************************************************************
 
@@ -130,11 +133,9 @@ bool codegen_emit(jitdata *jd)
        registerdata       *rd;
        s4                  len, s1, s2, s3, d, dd, disp;
        u2                  currentline;
-       ptrint              a;
-       varinfo            *var, *var1, *var2, *dst;
+       varinfo            *var;
        basicblock         *bptr;
        instruction        *iptr;
-       exception_entry    *ex;
        constant_classref  *cr;
        unresolved_class   *uc;
        methodinfo         *lm;             /* local methodinfo for ICMD_INVOKE*  */
@@ -186,7 +187,7 @@ bool codegen_emit(jitdata *jd)
           not be shared with place to save the return register
           since both values reside in R2. */
 
-       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+       if (checksync && code_is_synchronized(code)) {
                /* 1 slot space to save argument of monitor_enter */
                /* 1 slot to temporary store return value before monitor_exit */
                cd->stackframesize += 2;
@@ -197,7 +198,7 @@ bool codegen_emit(jitdata *jd)
           native code e.g. libc or jni (alignment problems with
           movaps). */
 
-       if (!jd->isleafmethod || opt_verbosecall )
+       if (!code_is_leafmethod(code) || opt_verbosecall )
                /* TODO really 16 bytes ? */
                cd->stackframesize = (cd->stackframesize + 2) & ~2;
 
@@ -206,36 +207,18 @@ bool codegen_emit(jitdata *jd)
        (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
        (void) dseg_add_unique_s4(cd, cd->stackframesize * 8); /* FrameSize       */
 
-#if defined(ENABLE_THREADS)
-       /* IsSync contains the offset relative to the stack pointer for the
-          argument of monitor_exit used in the exception handler. Since the
-          offset could be zero and give a wrong meaning of the flag it is
-          offset by one.
-       */
-
-       if (checksync && (m->flags & ACC_SYNCHRONIZED))
-               (void) dseg_add_unique_s4(cd, (rd->memuse + 1) * 8); /* IsSync        */
+       code->synchronizedoffset = rd->memuse * 8;
+
+       /* REMOVEME: We still need it for exception handling in assembler. */
+
+       if (code_is_leafmethod(code))
+               (void) dseg_add_unique_s4(cd, 1);
        else
-#endif
-               (void) dseg_add_unique_s4(cd, 0);                    /* IsSync          */
+               (void) dseg_add_unique_s4(cd, 0);
 
-       (void) dseg_add_unique_s4(cd, jd->isleafmethod);               /* IsLeaf  */
        (void) dseg_add_unique_s4(cd, INT_SAV_CNT - rd->savintreguse); /* IntSave */
        (void) dseg_add_unique_s4(cd, FLT_SAV_CNT - rd->savfltreguse); /* FltSave */
 
-       (void) dseg_addlinenumbertablesize(cd);
-
-       (void) dseg_add_unique_s4(cd, jd->exceptiontablelength); /* ExTableSize   */
-
-       /* create exception table */
-
-       for (ex = jd->exceptiontable; ex != NULL; ex = ex->down) {
-               dseg_add_target(cd, ex->start);
-               dseg_add_target(cd, ex->end);
-               dseg_add_target(cd, ex->handler);
-               (void) dseg_add_unique_address(cd, ex->catchtype.any);
-       }
-
        /* Offset PV */
 
        M_AADD_IMM(N_PV_OFFSET, REG_PV);
@@ -363,7 +346,7 @@ bool codegen_emit(jitdata *jd)
        /* save monitorenter argument */
 
 #if defined(ENABLE_THREADS)
-       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+       if (checksync && code_is_synchronized(code)) {
                /* stack offset for monitor argument */
 
                s1 = rd->memuse;
@@ -385,13 +368,13 @@ bool codegen_emit(jitdata *jd)
                /* decide which monitor enter function to call */
 
                if (m->flags & ACC_STATIC) {
-                       disp = dseg_add_address(cd, &m->class->object.header);
+                       disp = dseg_add_address(cd, &m->clazz->object.header);
                        M_ALD_DSEG(REG_A0, disp);
                }
                else {
                        M_TEST(REG_A0);
                        M_BNE(SZ_BRC + SZ_ILL);
-                       M_ILL(EXCEPTION_HARDWARE_NULLPOINTER);
+                       M_ILL(TRAP_NullPointerException);
                }
 
                disp = dseg_add_functionptr(cd, LOCK_monitor_enter);
@@ -515,7 +498,7 @@ bool codegen_emit(jitdata *jd)
 
                for (iptr = bptr->iinstr; len > 0; len--, iptr++) {
                        if (iptr->line != currentline) {
-                               dseg_addlinenumber(cd, iptr->line);
+                               linenumbertable_list_entry_add(cd, iptr->line);
                                currentline = iptr->line;
                        }
 
@@ -535,14 +518,14 @@ bool codegen_emit(jitdata *jd)
                case ICMD_INLINE_BODY:
 
                        REPLACEMENT_POINT_INLINE_BODY(cd, iptr);
-                       dseg_addlinenumber_inline_start(cd, iptr);
-                       dseg_addlinenumber(cd, iptr->line);
+                       linenumbertable_list_entry_add_inline_start(cd, iptr);
+                       linenumbertable_list_entry_add(cd, iptr->line);
                        break;
 
                case ICMD_INLINE_END:
 
-                       dseg_addlinenumber_inline_end(cd, iptr);
-                       dseg_addlinenumber(cd, iptr->line);
+                       linenumbertable_list_entry_add_inline_end(cd, iptr);
+                       linenumbertable_list_entry_add(cd, iptr->line);
                        break;
 
                case ICMD_CHECKNULL:  /* ..., objectref  ==> ..., objectref           */
@@ -1636,7 +1619,9 @@ bool codegen_emit(jitdata *jd)
 
                case ICMD_F2D:       /* ..., value  ==> ..., (double) value           */
                        {
+#ifdef SUPPORT_HERCULES
                                u1 *ref;
+#endif
                                s1 = emit_load_s1(jd, iptr, REG_FTMP1);
                                d = codegen_reg_of_dst(jd, iptr, REG_FTMP2);
 #ifdef SUPPORT_HERCULES
@@ -1808,13 +1793,13 @@ bool codegen_emit(jitdata *jd)
 
                        N_L(
                                GET_LOW_REG(d) /* maybe itmp3 */, 
-                               OFFSET(java_intarray_t, data[0]) + 4, 
+                               OFFSET(java_longarray_t, data[0]) + 4, 
                                REG_ITMP2, s1 /* maybe itmp1 */
                        );
 
                        N_L(
                                GET_HIGH_REG(d) /* maybe itmp1 */, 
-                               OFFSET(java_intarray_t, data[0]), 
+                               OFFSET(java_longarray_t, data[0]), 
                                REG_ITMP2, s1 /* maybe itmp1 */
                        );
 
@@ -1850,7 +1835,7 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s2, REG_ITMP2);
                        M_SLL_IMM(3, REG_ITMP2); /* scale index by 8 */
        
-                       N_LD(d, OFFSET(java_floatarray_t, data[0]), REG_ITMP2, s1);
+                       N_LD(d, OFFSET(java_doublearray_t, data[0]), REG_ITMP2, s1);
 
                        emit_store_dst(jd, iptr, d);
                        break;
@@ -1933,9 +1918,9 @@ bool codegen_emit(jitdata *jd)
                        M_SLL_IMM(3, REG_ITMP2);
 
                        s3 = emit_load_s3_high(jd, iptr, REG_ITMP3);
-                       N_ST(s3, OFFSET(java_intarray_t, data[0]), REG_ITMP2, s1);
+                       N_ST(s3, OFFSET(java_longarray_t, data[0]), REG_ITMP2, s1);
                        s3 = emit_load_s3_low(jd, iptr, REG_ITMP3);
-                       N_ST(s3, OFFSET(java_intarray_t, data[0]) + 4, REG_ITMP2, s1);
+                       N_ST(s3, OFFSET(java_longarray_t, data[0]) + 4, REG_ITMP2, s1);
                        break;
 
                case ICMD_FASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -2016,10 +2001,10 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = fi->type;
                                disp      = dseg_add_address(cd, fi->value);
 
-                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
+                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->clazz)) {
                                        PROFILE_CYCLE_STOP;
 
-                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class, 0);
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->clazz, 0);
 
                                        PROFILE_CYCLE_START;
                                }
@@ -2068,9 +2053,9 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = fi->type;
                                disp      = dseg_add_address(cd, fi->value);
 
-                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class)) {
+                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->clazz)) {
                                        PROFILE_CYCLE_STOP;
-                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class, disp);
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->clazz, disp);
                                        PROFILE_CYCLE_START;
                                }
                        }
@@ -2193,7 +2178,7 @@ bool codegen_emit(jitdata *jd)
                        }
 
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                               ((patchref_t *)list_first_unsynced(jd->code->patchers))->disp = (cd->mcodeptr - ref);
+                               ((patchref_t *)list_first(jd->code->patchers))->disp = (cd->mcodeptr - ref);
                        }
 
                        switch (fieldtype) {
@@ -2534,9 +2519,8 @@ bool codegen_emit(jitdata *jd)
                                        assert(0);
                                }
 
-
                                if (out_ref != NULL) {
-                                       *(u4 *)out_ref |= (u4)(cd->mcodeptr - out_ref) / 2;
+                                       N_BRC_BACK_PATCH(out_ref);
                                }
 
                        }
@@ -2599,7 +2583,7 @@ nowperformreturn:
 #endif /* !defined(NDEBUG) */
 
 #if defined(ENABLE_THREADS)
-                       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+                       if (checksync && code_is_synchronized(code)) {
                                /* we need to save the proper return value */
 
                                switch (iptr->opc) {
@@ -2840,9 +2824,13 @@ gen_method:
 
                        switch (iptr->opc) {
                        case ICMD_BUILTIN:
-                               disp = dseg_add_functionptr(cd, bte->fp);
+                               if (bte->stub == NULL) {
+                                       disp = dseg_add_functionptr(cd, bte->fp);
+                                       M_ASUB_IMM(96, REG_SP); /* register save area as required by C abi */   
+                               } else {
+                                       disp = dseg_add_functionptr(cd, bte->stub);
+                               }
 
-                               M_ASUB_IMM(96, REG_SP); /* register save area as required by C abi */   
                                if (N_VALID_DSEG_DISP(disp)) {
                                        N_L(REG_PV, N_DSEG_DISP(disp), RN, REG_PV);
                                } else {
@@ -2910,9 +2898,9 @@ gen_method:
                                }
                                else {
                                        s1 = OFFSET(vftbl_t, interfacetable[0]) -
-                                               sizeof(methodptr*) * lm->class->index;
+                                               sizeof(methodptr*) * lm->clazz->index;
 
-                                       s2 = sizeof(methodptr) * (lm - lm->class->methods);
+                                       s2 = sizeof(methodptr) * (lm - lm->clazz->methods);
                                }
 
                                /* Implicit null-pointer check */
@@ -2932,8 +2920,9 @@ gen_method:
 
                        switch (iptr->opc) {
                                case ICMD_BUILTIN:
-                                       M_AADD_IMM(96, REG_SP); /* remove C abi register save area */
-                                       emit_exception_check(cd, iptr); /* check for exception */
+                                       if (bte->stub == NULL) {
+                                               M_AADD_IMM(96, REG_SP); /* remove C abi register save area */
+                                       }
                                        break;
                        }
 
@@ -3011,9 +3000,6 @@ gen_method:
                                        supervftbl = super->vftbl;
                                }
 
-                               if ((super == NULL) || !(super->flags & ACC_INTERFACE))
-                                       CODEGEN_CRITICAL_SECTION_NEW;
-
                                s1 = emit_load_s1(jd, iptr, REG_ITMP1);
 
                                /* if class is not resolved, check which code to call */
@@ -3091,8 +3077,6 @@ gen_method:
                                        }
 
 #if 1
-                                       CODEGEN_CRITICAL_SECTION_START;
-
                                        /* REG_ITMP3 := baseval(s1) */
                                        M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
@@ -3108,8 +3092,6 @@ gen_method:
                                        M_ALD_DSEG(REG_ITMP2, disp);
                                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
 
-                                       CODEGEN_CRITICAL_SECTION_END;
-
                                        M_CMPU(REG_ITMP3, REG_ITMP2); /* Unsigned compare */
 
                                        /* M_CMPULE(REG_ITMP2, REG_ITMP3, REG_ITMP3); itmp3 = (itmp2 <= itmp3) */
@@ -3119,16 +3101,12 @@ gen_method:
                                        M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ALD_DSEG(REG_ITMP3, disp);
 
-                                       CODEGEN_CRITICAL_SECTION_START;
-
                                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                        M_ILD(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, baseval));
                                        M_ISUB(REG_ITMP3, REG_ITMP2);
                                        M_ALD_DSEG(REG_ITMP3, disp);
                                        M_ILD(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, diffval));
 
-                                       CODEGEN_CRITICAL_SECTION_END;
-                                       
                                        M_CMPU(REG_ITMP2, REG_ITMP3); /* Unsigned compare */
                                        /* M_CMPULE(REG_ITMP2, REG_ITMP3, REG_ITMP3); itmp3 = (itmp2 <= itmp3) */
                                        /* M_BEQZ(REG_ITMP3, 0); branch if (! itmp) -> branch if > */
@@ -3231,9 +3209,6 @@ gen_method:
 #                      define LABEL_EXIT_INTERFACE_DONE BRANCH_LABEL_5
 #                      define LABEL_EXIT_CLASS_NULL BRANCH_LABEL_6
 
-                       if ((super == NULL) || !(super->flags & ACC_INTERFACE))
-                               CODEGEN_CRITICAL_SECTION_NEW;
-
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        if (s1 == d) {
@@ -3335,14 +3310,10 @@ gen_method:
                                M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ALD_DSEG(REG_ITMP2, disp);
 
-                               CODEGEN_CRITICAL_SECTION_START;
-
                                M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
                                M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
 
-                               CODEGEN_CRITICAL_SECTION_END;
-
                                M_ISUB(REG_ITMP3, REG_ITMP1); /* itmp1 :=  itmp1 (sub.baseval) - itmp3 (super.baseval) */
 
                                M_CMPU(REG_ITMP1, REG_ITMP2); /* d := (uint)REG_ITMP1 <= (uint)REG_ITMP2 */
@@ -3457,8 +3428,6 @@ gen_method:
        } /* if (bptr -> flags >= BBREACHED) */
        } /* for basic block */
 
-       dseg_createlinenumbertable(cd);
-
        /* generate stubs */
 
        emit_patcher_traps(jd);
@@ -3468,36 +3437,6 @@ gen_method:
        return true;
 }
 
-
-/* codegen_emit_stub_compiler **************************************************
-
-   Emits a stub routine which calls the compiler.
-       
-*******************************************************************************/
-
-void codegen_emit_stub_compiler(jitdata *jd)
-{
-       methodinfo  *m;
-       codegendata *cd;
-
-       /* get required compiler data */
-
-       m  = jd->m;
-       cd = jd->cd;
-
-       /* code for the stub */
-
-       /* don't touch ITMP2 as it cointains the return address */
-
-       M_AADD_IMM(N_PV_OFFSET, REG_PV); /* suppress negative displacements */
-
-       /* REG_METHODPTR (REG_ITMP1) already used */
-       M_ILD_DSEG(REG_ITMP3, -2 * SIZEOF_VOID_P); /* methodinfo  */
-       M_ILD_DSEG(REG_PV, -3 * SIZEOF_VOID_P); /* compiler pointer */
-       N_BR(REG_PV);
-}
-
-
 /* codegen_emit_stub_native ****************************************************
 
    Emits a stub routine which calls a native method.
@@ -3524,315 +3463,277 @@ void codegen_emit_stub_compiler(jitdata *jd)
                                                             SP after method entry
 */
 
-void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
+void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int skipparams)
 {
-       methodinfo   *m;
-       codeinfo     *code;
-       codegendata  *cd;
-       registerdata *rd;
-       methoddesc   *md;
-       s4            nativeparams;
-       s4            i, j;                 /* count variables                    */
-       s4            t;
-       s4            s1, s2;
-       s4            disp;
+       methodinfo  *m;
+       codeinfo    *code;
+       codegendata *cd;
+       methoddesc  *md;
+       int          i, j;
+       int          t;
+       int          s1, s2;
+       int          disp;
 
        /* get required compiler data */
 
        m    = jd->m;
        code = jd->code;
        cd   = jd->cd;
-       rd   = jd->rd;
 
-       /* initialize variables */
+       /* set some variables */
 
        md = m->parseddesc;
-       nativeparams = (m->flags & ACC_STATIC) ? 2 : 1;
 
-       /* calculate stack frame size */
+       /* calculate stackframe size */
+
+       cd->stackframesize =
+               1 + /* return address */
+               sizeof(stackframeinfo_t) / 8 +
+               sizeof(localref_table) / 8 +
+               nmd->paramcount +
+               nmd->memuse +
+               (96 / 8); /* linkage area */
+
+       /* keep stack 8-byte aligned */
 
-       cd->stackframesize = 
-               1 + /* r14 - return address */ +
-               ((sizeof(stackframeinfo) + 7) / 8) +
-               ((sizeof(localref_table) + 7) / 8)  +
-               1 + /* itmp3 */
-               (INT_ARG_CNT + FLT_ARG_CNT) +
-               nmd->memuse + /* parameter passing */
-               (96 / 8)  /* required by ABI */;
+       /*ALIGN_2(cd->stackframesize);*/
 
        /* create method header */
 
        (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
        (void) dseg_add_unique_s4(cd, cd->stackframesize * 8); /* FrameSize       */
-       (void) dseg_add_unique_s4(cd, 0);                      /* IsSync          */
        (void) dseg_add_unique_s4(cd, 0);                      /* IsLeaf          */
        (void) dseg_add_unique_s4(cd, 0);                      /* IntSave         */
        (void) dseg_add_unique_s4(cd, 0);                      /* FltSave         */
-       (void) dseg_addlinenumbertablesize(cd);
-       (void) dseg_add_unique_s4(cd, 0);                      /* ExTableSize     */
 
-       /* generate stub code */
+       /* generate code */
 
        M_ASUB_IMM(cd->stackframesize * 8, REG_SP);
        M_AADD_IMM(N_PV_OFFSET, REG_PV);
 
-       /* save return address */
+       /* store return address */
 
        M_AST(REG_RA, REG_SP, (cd->stackframesize - 1) * 8);
 
-       /* generate native method profiling code */
+#if defined(ENABLE_GC_CACAO)
+       /* Save callee saved integer registers in stackframeinfo (GC may
+          need to recover them during a collection). */
 
-#if defined(ENABLE_PROFILING)
-       if (JITDATA_HAS_FLAG_INSTRUMENT(jd)) {
-               /* count frequency */
-               M_ALD_DSEG(REG_ITMP1, CodeinfoPointer);
-               ICONST(REG_ITMP2, 1);
-               N_AL(REG_ITMP2, OFFSET(codeinfo, frequency), RN, REG_ITMP1);
-               M_IST(REG_ITMP2, REG_ITMP1, OFFSET(codeinfo, frequency));
-       }
-#endif
+       disp = cd->stackframesize * 8 - sizeof(stackframeinfo_t) +
+               OFFSET(stackframeinfo_t, intregs);
 
-#if !defined(NDEBUG)
-       if (JITDATA_HAS_FLAG_VERBOSECALL(jd))
-               emit_verbosecall_enter(jd);
+       for (i = 0; i < INT_SAV_CNT; i++)
+               M_AST(abi_registers_integer_saved[i], REG_SP, disp + i * 4);
 #endif
 
-       /* get function address (this must happen before the stackframeinfo) */
-
-       disp = dseg_add_functionptr(cd, f);
-
-       if (f == NULL)
-               patcher_add_patch_ref(jd, PATCHER_resolve_native_function, m, disp);
-
-       M_ALD_DSEG(REG_ITMP1, disp);
-
-       j = 96 + (nmd->memuse * 8);
-
-       /* todo some arg registers are not volatile in C-abi terms */
-
        /* save integer and float argument registers */
 
        for (i = 0; i < md->paramcount; i++) {
-               if (! md->params[i].inmemory) {
+               if (!md->params[i].inmemory) {
                        s1 = md->params[i].regoff;
-                       t = md->paramtypes[i].type;
 
-                       if (IS_INT_LNG_TYPE(t)) {
-                               if (IS_2_WORD_TYPE(t)) {
-                                       M_LST(s1, REG_SP, j);
-                               } else {
-                                       M_IST(s1, REG_SP, j);
-                               }
-                       } else {
-                               if (IS_2_WORD_TYPE(t)) {
-                                       M_DST(s1, REG_SP, j);
-                               } else {
-                                       M_FST(s1, REG_SP, j);
-                               }
+                       switch (md->paramtypes[i].type) {
+                       case TYPE_INT:
+                       case TYPE_ADR:
+                               M_IST(s1, REG_SP, 96 + i * 8);
+                               break;
+                       case TYPE_LNG:
+                               M_LST(s1, REG_SP, 96 + i * 8);
+                               break;
+                       case TYPE_FLT:
+                       case TYPE_DBL:
+                               M_DST(s1, REG_SP, 96 + i * 8);
+                               break;
                        }
-
-                       j += 8;
                }
        }
 
-       M_AST(REG_ITMP1, REG_SP, j);
-
-       /* create dynamic stack info */
-
-       M_MOV(REG_SP, REG_A0); /* currentsp */
-       M_LDA(REG_A1, REG_PV, -N_PV_OFFSET); /* pv */
+       /* create native stack info */
 
+       M_MOV(REG_SP, REG_A0);
+       M_LDA(REG_A1, REG_PV, -N_PV_OFFSET);
        disp = dseg_add_functionptr(cd, codegen_start_native_call);
-       M_ALD_DSEG(REG_ITMP1, disp);
-
-       M_CALL(REG_ITMP1); /* call */
+       M_ALD_DSEG(REG_ITMP2, disp);
+       M_CALL(REG_ITMP2);
 
        /* remember class argument */
 
-       if (m->flags & ACC_STATIC) {
+       if (m->flags & ACC_STATIC)
                M_MOV(REG_RESULT, REG_ITMP3);
-       }
 
        /* restore integer and float argument registers */
 
-       j = 96 + (nmd->memuse * 8);
-
        for (i = 0; i < md->paramcount; i++) {
-               if (! md->params[i].inmemory) {
+               if (!md->params[i].inmemory) {
                        s1 = md->params[i].regoff;
-                       t = md->paramtypes[i].type;
 
-                       if (IS_INT_LNG_TYPE(t)) {
-                               if (IS_2_WORD_TYPE(t)) {
-                                       M_LLD(s1, REG_SP, j);
-                               } else {
-                                       M_ILD(s1, REG_SP, j);
-                               }
-                       } else {
-                               if (IS_2_WORD_TYPE(t)) {
-                                       M_DLD(s1, REG_SP, j);
-                               } else {
-                                       M_FLD(s1, REG_SP, j);
-                               }
+                       switch (md->paramtypes[i].type) {
+                       case TYPE_INT:
+                       case TYPE_ADR:
+                               M_ILD(s1, REG_SP, 96 + i * 8);
+                               break;
+                       case TYPE_LNG:
+                               M_LLD(s1, REG_SP, 96 + i * 8);
+                               break;
+                       case TYPE_FLT:
+                       case TYPE_DBL:
+                               M_DLD(s1, REG_SP, 96 + i * 8);
+                               break;
                        }
-
-                       j += 8;
                }
        }
 
-       M_ALD(REG_ITMP1, REG_SP, j);
-
        /* copy or spill arguments to new locations */
 
-       for (i = md->paramcount - 1, j = i + nativeparams; i >= 0; i--, j--) {
+       for (i = md->paramcount - 1, j = i + skipparams; i >= 0; i--, j--) {
                t = md->paramtypes[i].type;
 
                if (IS_INT_LNG_TYPE(t)) {
-
-                       if (! md->params[i].inmemory) {
-
+                       if (!md->params[i].inmemory) {
                                s1 = md->params[i].regoff;
+                               s2 = nmd->params[j].regoff;
 
-                               if (! nmd->params[j].inmemory) {
-                                       s2 = nmd->params[j].regoff;
-                                       if (IS_2_WORD_TYPE(t)) {
+                               if (!nmd->params[j].inmemory) {
+                                       if (IS_2_WORD_TYPE(t))
                                                M_LNGMOVE(s1, s2);
-                                       } else {
-                                               M_MOV(s1, s2);
-                                       }
-                               } else {
-                                       s2 = nmd->params[j].regoff;
-                                       if (IS_2_WORD_TYPE(t)) {
-                                               M_LST(s1, REG_SP, 96 + s2);
-                                       } else {
-                                               M_IST(s1, REG_SP, 96 + s2);
-                                       }
+                                       else
+                                               M_INTMOVE(s1, s2);
                                }
-
-                       } else {
-                               s1 = cd->stackframesize * 8 + md->params[i].regoff;
+                               else {
+                                       if (IS_2_WORD_TYPE(t))
+                                               M_LST(s1, REG_SP, s2);
+                                       else
+                                               M_IST(s1, REG_SP, s2);
+                               }
+                       }
+                       else {
+                               s1 = md->params[i].regoff + cd->stackframesize * 8;
                                s2 = nmd->params[j].regoff;
-                               
+
                                if (IS_2_WORD_TYPE(t)) {
-                                       N_MVC(96 + s2, 8, REG_SP, s1, REG_SP);
+                                       N_MVC(s2, 8, REG_SP, s1, REG_SP);
                                } else {
-                                       N_MVC(96 + s2, 4, REG_SP, s1, REG_SP);
+                                       N_MVC(s2, 4, REG_SP, s1, REG_SP);
                                }
                        }
-
-               else {
-                       /* We only copy spilled float arguments, as the float argument    */
-                       /* registers keep unchanged.                                      */
+               }
+               else {
+                       /* We only copy spilled float arguments, as the float
+                          argument registers keep unchanged. */
 
                        if (md->params[i].inmemory) {
-                               s1 = cd->stackframesize * 8 + md->params[i].regoff;
+                               s1 = md->params[i].regoff + cd->stackframesize * 8;
                                s2 = nmd->params[j].regoff;
 
                                if (IS_2_WORD_TYPE(t)) {
-                                       N_MVC(96 + s2, 8, REG_SP, s1, REG_SP);
+                                       N_MVC(s2, 8, REG_SP, s1, REG_SP);
                                } else {
-                                       N_MVC(96 + s2, 4, REG_SP, s1, REG_SP);
+                                       N_MVC(s2, 4, REG_SP, s1, REG_SP);
                                }
                        }
                }
        }
 
-       /* put class into second argument register */
+       /* Handle native Java methods. */
 
-       if (m->flags & ACC_STATIC) {
-               M_MOV(REG_ITMP3, REG_A1);
-       }
+       if (m->flags & ACC_NATIVE) {
+               /* put class into second argument register */
 
-       /* put env into first argument register */
+               if (m->flags & ACC_STATIC)
+                       M_MOV(REG_ITMP3, REG_A1);
 
-       disp = dseg_add_address(cd, _Jv_env);
-       M_ILD_DSEG(REG_A0, disp);
+               /* put env into first argument register */
 
-       /* do the native function call */
+               disp = dseg_add_address(cd, VM_get_jnienv());
+               M_ALD_DSEG(REG_A0, disp);
+       }
 
-       M_CALL(REG_ITMP1); /* call */
+       /* Call native function. */
 
-       /* save return value */
+       disp = dseg_add_functionptr(cd, f);
+       M_ALD_DSEG(REG_ITMP2, disp);
+       M_CALL(REG_ITMP2);
 
-       t = md->returntype.type;
+       /* save return value */
 
-       if (t != TYPE_VOID) {
-               if (IS_INT_LNG_TYPE(t)) {
-                       if (IS_2_WORD_TYPE(t)) {
-                               M_LST(REG_RESULT_PACKED, REG_SP, 96);
-                       } else {
-                               M_IST(REG_RESULT, REG_SP, 96);
-                       }
-               } else {
-                       if (IS_2_WORD_TYPE(t)) {
-                               M_DST(REG_FRESULT, REG_SP, 96);
-                       } else {
-                               M_FST(REG_FRESULT, REG_SP, 96);
-                       }
-               }
+       switch (md->returntype.type) {
+       case TYPE_INT:
+       case TYPE_ADR:
+               M_IST(REG_RESULT, REG_SP, 96);
+               break;
+       case TYPE_LNG:
+               M_LST(REG_RESULT_PACKED, REG_SP, 96);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DST(REG_FRESULT, REG_SP, 96);
+               break;
+       case TYPE_VOID:
+               break;
        }
 
-#if !defined(NDEBUG)
-       if (JITDATA_HAS_FLAG_VERBOSECALL(jd))
-               emit_verbosecall_exit(jd);
-#endif
-
        /* remove native stackframe info */
 
-       M_MOV(REG_SP, REG_A0); /* currentsp */
-       M_LDA(REG_A1, REG_PV, -N_PV_OFFSET); /* pv */
+       M_MOV(REG_SP, REG_A0);
+       M_LDA(REG_A1, REG_PV, -N_PV_OFFSET);
        disp = dseg_add_functionptr(cd, codegen_finish_native_call);
        M_ALD_DSEG(REG_ITMP1, disp);
        M_CALL(REG_ITMP1);
-       M_MOV(REG_RESULT, REG_ITMP3);
+
+       M_MOV(REG_RESULT, REG_ITMP3_XPTR);
 
        /* restore return value */
 
-       if (t != TYPE_VOID) {
-               if (IS_INT_LNG_TYPE(t)) {
-                       if (IS_2_WORD_TYPE(t)) {
-                               M_LLD(REG_RESULT_PACKED, REG_SP, 96);
-                       } else {
-                               M_ILD(REG_RESULT, REG_SP, 96);
-                       }
-               } else {
-                       if (IS_2_WORD_TYPE(t)) {
-                               M_DLD(REG_FRESULT, REG_SP, 96);
-                       } else {
-                               M_FLD(REG_FRESULT, REG_SP, 96);
-                       }
-               }
+       switch (md->returntype.type) {
+       case TYPE_INT:
+       case TYPE_ADR:
+               M_ILD(REG_RESULT, REG_SP, 96);
+               break;
+       case TYPE_LNG:
+               M_LLD(REG_RESULT_PACKED, REG_SP, 96);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DLD(REG_FRESULT, REG_SP, 96);
+               break;
+       case TYPE_VOID:
+               break;
        }
 
+#if defined(ENABLE_GC_CACAO)
+       /* Restore callee saved integer registers from stackframeinfo (GC
+          might have modified them during a collection). */
+        
+       disp = cd->stackframesize * 8 - sizeof(stackframeinfo_t) +
+               OFFSET(stackframeinfo_t, intregs);
+
+       for (i = 0; i < INT_SAV_CNT; i++)
+               M_ALD(abi_registers_integer_saved[i], REG_SP, disp + i * 4);
+#endif
+
        /* load return address */
-       
-       M_ALD(REG_ITMP2, REG_SP, (cd->stackframesize - 1) * 8);
 
-       /* remove stackframe */
+       M_ALD(REG_RA, REG_SP, (cd->stackframesize - 1) * 8);
 
-       M_AADD_IMM(cd->stackframesize * 8, REG_SP);
+       /* remove stackframe       */
 
-       /* test for exception */
+       M_AADD_IMM(cd->stackframesize * 8, REG_SP);
 
-       M_TEST(REG_ITMP3);
-       M_BNE(SZ_BRC + SZ_BCR);
+       /* check for exception */
 
-       /* return */
+       M_TEST(REG_ITMP3_XPTR);
+       M_BNE(SZ_BRC + SZ_BCR);                     /* if no exception then return        */
 
-       M_JMP(RN, REG_ITMP2);
+       M_RET;
 
        /* handle exception */
 
-       M_MOV(REG_ITMP3, REG_ITMP3_XPTR);
-       M_MOV(REG_ITMP2, REG_ITMP1_XPC); /* get return address from stack */
+       M_MOV(REG_RA, REG_ITMP1_XPC);
+       M_ASUB_IMM(2, REG_ITMP1_XPC);
 
        disp = dseg_add_functionptr(cd, asm_handle_nat_exception);
        M_ALD_DSEG(REG_ITMP2, disp);
        M_JMP(RN, REG_ITMP2);
-
-       /* generate patcher stubs */
-
-       emit_patcher_traps(jd);
 }
 
 /*