* src/vm/builtin.c (builtin_canstore): Throw an ArrayStoreException.
[cacao.git] / src / vm / jit / alpha / codegen.c
index ab57c9d0a15f7a121d873ad3060cf453699b4995..0a6ff69e09eba50e0147bd389d0f1da730fca3d0 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: codegen.c 7820 2007-04-25 19:55:34Z twisti $
-
 */
 
 
@@ -43,6 +41,7 @@
 #include "mm/memory.h"
 
 #include "native/jni.h"
+#include "native/localref.h"
 #include "native/native.h"
 
 #include "threads/lock-common.h"
@@ -59,7 +58,7 @@
 #include "vm/jit/emit-common.h"
 #include "vm/jit/jit.h"
 #include "vm/jit/parse.h"
-#include "vm/jit/patcher.h"
+#include "vm/jit/patcher-common.h"
 #include "vm/jit/reg.h"
 #include "vm/jit/replace.h"
 #include "vm/jit/stacktrace.h"
@@ -215,13 +214,13 @@ bool codegen_emit(jitdata *jd)
                                if (!IS_INMEMORY(var->flags))
                                        M_INTMOVE(s1, var->vv.regoff);
                                else
-                                       M_LST(s1, REG_SP, var->vv.regoff * 8);
+                                       M_LST(s1, REG_SP, var->vv.regoff);
                        }
                        else {                                   /* stack arguments       */
                                if (!IS_INMEMORY(var->flags))
-                                       M_LLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) *8);
+                                       M_LLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
                                else
-                                       var->vv.regoff = cd->stackframesize + s1;
+                                       var->vv.regoff = cd->stackframesize * 8 + s1;
                        }
                }
                else {                                       /* floating args         */
@@ -233,9 +232,9 @@ bool codegen_emit(jitdata *jd)
                        }
                        else {                                   /* stack arguments       */
                                if (!(var->flags & INMEMORY))
-                                       M_DLD(var->vv.regoff, REG_SP, (cd->stackframesize + s1) * 8);
+                                       M_DLD(var->vv.regoff, REG_SP, cd->stackframesize * 8 + s1);
                                else
-                                       var->vv.regoff = cd->stackframesize + s1;
+                                       var->vv.regoff = cd->stackframesize * 8 + s1;
                        }
                }
        }
@@ -450,7 +449,7 @@ bool codegen_emit(jitdata *jd)
                                   resolved the same class, the returned displacement
                                   of dseg_add_address is ok to use. */
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_classinfo,
+                               patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_classinfo,
                                                                          cr, disp);
 
                                M_ALD(d, REG_PV, disp);
@@ -1418,7 +1417,7 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
                        /* implicit null-pointer check */
-                       M_ILD(d, s1, OFFSET(java_arrayheader, size));
+                       M_ILD(d, s1, OFFSET(java_array_t, size));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1431,13 +1430,13 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
-                               M_BLDU(d, REG_ITMP1, OFFSET (java_bytearray, data[0]));
+                               M_BLDU(d, REG_ITMP1, OFFSET (java_bytearray_t, data[0]));
                                M_BSEXT(d, d);
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_bytearray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_bytearray, data[0])+1);
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_bytearray_t, data[0])+1);
                                M_EXTQH(REG_ITMP2, REG_ITMP1, d);
                                M_SRA_IMM(d, 56, d);
                        }
@@ -1454,13 +1453,13 @@ bool codegen_emit(jitdata *jd)
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_SLDU(d, REG_ITMP1, OFFSET(java_chararray, data[0]));
+                               M_SLDU(d, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
                        }
                        else {
                                M_LADD (s2, s1, REG_ITMP1);
                                M_LADD (s2, REG_ITMP1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_chararray, data[0]));
-                               M_LDA  (REG_ITMP1, REG_ITMP1, OFFSET(java_chararray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
+                               M_LDA  (REG_ITMP1, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
                                M_EXTWL(REG_ITMP2, REG_ITMP1, d);
                        }
                        emit_store_dst(jd, iptr, d);
@@ -1476,13 +1475,13 @@ bool codegen_emit(jitdata *jd)
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_SLDU( d, REG_ITMP1, OFFSET (java_shortarray, data[0]));
+                               M_SLDU( d, REG_ITMP1, OFFSET (java_shortarray_t, data[0]));
                                M_SSEXT(d, d);
                        } else {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_shortarray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_shortarray, data[0])+2);
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_shortarray_t, data[0])+2);
                                M_EXTQH(REG_ITMP2, REG_ITMP1, d);
                                M_SRA_IMM(d, 48, d);
                        }
@@ -1497,7 +1496,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_S4ADDQ(s2, s1, REG_ITMP1);
-                       M_ILD(d, REG_ITMP1, OFFSET(java_intarray, data[0]));
+                       M_ILD(d, REG_ITMP1, OFFSET(java_intarray_t, data[0]));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1509,7 +1508,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_S8ADDQ(s2, s1, REG_ITMP1);
-                       M_LLD(d, REG_ITMP1, OFFSET(java_longarray, data[0]));
+                       M_LLD(d, REG_ITMP1, OFFSET(java_longarray_t, data[0]));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1521,7 +1520,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_S4ADDQ(s2, s1, REG_ITMP1);
-                       M_FLD(d, REG_ITMP1, OFFSET(java_floatarray, data[0]));
+                       M_FLD(d, REG_ITMP1, OFFSET(java_floatarray_t, data[0]));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1533,7 +1532,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_S8ADDQ(s2, s1, REG_ITMP1);
-                       M_DLD(d, REG_ITMP1, OFFSET(java_doublearray, data[0]));
+                       M_DLD(d, REG_ITMP1, OFFSET(java_doublearray_t, data[0]));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1545,7 +1544,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SAADDQ(s2, s1, REG_ITMP1);
-                       M_ALD(d, REG_ITMP1, OFFSET(java_objectarray, data[0]));
+                       M_ALD(d, REG_ITMP1, OFFSET(java_objectarray_t, data[0]));
                        emit_store_dst(jd, iptr, d);
                        break;
 
@@ -1559,12 +1558,12 @@ bool codegen_emit(jitdata *jd)
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
-                               M_BST(s3, REG_ITMP1, OFFSET(java_bytearray, data[0]));
+                               M_BST(s3, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_bytearray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_bytearray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
                                M_INSBL(s3, REG_ITMP1, REG_ITMP3);
                                M_MSKBL(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                M_OR(REG_ITMP2, REG_ITMP3, REG_ITMP2);
@@ -1582,13 +1581,13 @@ bool codegen_emit(jitdata *jd)
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_SST(s3, REG_ITMP1, OFFSET(java_chararray, data[0]));
+                               M_SST(s3, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_chararray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_chararray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
                                M_INSWL(s3, REG_ITMP1, REG_ITMP3);
                                M_MSKWL(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                M_OR(REG_ITMP2, REG_ITMP3, REG_ITMP2);
@@ -1606,13 +1605,13 @@ bool codegen_emit(jitdata *jd)
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_SST(s3, REG_ITMP1, OFFSET(java_shortarray, data[0]));
+                               M_SST(s3, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_shortarray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_shortarray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
                                M_INSWL(s3, REG_ITMP1, REG_ITMP3);
                                M_MSKWL(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                M_OR(REG_ITMP2, REG_ITMP3, REG_ITMP2);
@@ -1628,7 +1627,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_S4ADDQ(s2, s1, REG_ITMP1);
-                       M_IST(s3, REG_ITMP1, OFFSET(java_intarray, data[0]));
+                       M_IST(s3, REG_ITMP1, OFFSET(java_intarray_t, data[0]));
                        break;
 
                case ICMD_LASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1639,7 +1638,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_S8ADDQ(s2, s1, REG_ITMP1);
-                       M_LST(s3, REG_ITMP1, OFFSET(java_longarray, data[0]));
+                       M_LST(s3, REG_ITMP1, OFFSET(java_longarray_t, data[0]));
                        break;
 
                case ICMD_FASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1650,7 +1649,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_FTMP3);
                        M_S4ADDQ(s2, s1, REG_ITMP1);
-                       M_FST(s3, REG_ITMP1, OFFSET(java_floatarray, data[0]));
+                       M_FST(s3, REG_ITMP1, OFFSET(java_floatarray_t, data[0]));
                        break;
 
                case ICMD_DASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1661,7 +1660,7 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        s3 = emit_load_s3(jd, iptr, REG_FTMP3);
                        M_S8ADDQ(s2, s1, REG_ITMP1);
-                       M_DST(s3, REG_ITMP1, OFFSET(java_doublearray, data[0]));
+                       M_DST(s3, REG_ITMP1, OFFSET(java_doublearray_t, data[0]));
                        break;
 
                case ICMD_AASTORE:    /* ..., arrayref, index, value  ==> ...         */
@@ -1675,18 +1674,18 @@ bool codegen_emit(jitdata *jd)
                        M_INTMOVE(s1, REG_A0);
                        M_INTMOVE(s3, REG_A1);
 
-                       disp = dseg_add_functionptr(cd, BUILTIN_canstore);
+                       disp = dseg_add_functionptr(cd, BUILTIN_FAST_canstore);
                        M_ALD(REG_PV, REG_PV, disp);
                        M_JSR(REG_RA, REG_PV);
                        disp = (s4) (cd->mcodeptr - cd->mcodebase);
                        M_LDA(REG_PV, REG_RA, -disp);
-                       emit_exception_check(cd, iptr);
+                       emit_arraystore_check(cd, iptr);
 
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        s2 = emit_load_s2(jd, iptr, REG_ITMP2);
                        s3 = emit_load_s3(jd, iptr, REG_ITMP3);
                        M_SAADDQ(s2, s1, REG_ITMP1);
-                       M_AST(s3, REG_ITMP1, OFFSET(java_objectarray, data[0]));
+                       M_AST(s3, REG_ITMP1, OFFSET(java_objectarray_t, data[0]));
                        break;
 
 
@@ -1698,12 +1697,12 @@ bool codegen_emit(jitdata *jd)
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
-                               M_BST(REG_ZERO, REG_ITMP1, OFFSET(java_bytearray, data[0]));
+                               M_BST(REG_ZERO, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_bytearray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_bytearray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_bytearray_t, data[0]));
                                M_INSBL(REG_ZERO, REG_ITMP1, REG_ITMP3);
                                M_MSKBL(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                M_OR(REG_ITMP2, REG_ITMP3, REG_ITMP2);
@@ -1720,13 +1719,13 @@ bool codegen_emit(jitdata *jd)
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_SST(REG_ZERO, REG_ITMP1, OFFSET(java_chararray, data[0]));
+                               M_SST(REG_ZERO, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_chararray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_chararray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_chararray_t, data[0]));
                                M_INSWL(REG_ZERO, REG_ITMP1, REG_ITMP3);
                                M_MSKWL(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                M_OR(REG_ITMP2, REG_ITMP3, REG_ITMP2);
@@ -1743,13 +1742,13 @@ bool codegen_emit(jitdata *jd)
                        if (has_ext_instr_set) {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_SST(REG_ZERO, REG_ITMP1, OFFSET(java_shortarray, data[0]));
+                               M_SST(REG_ZERO, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
                        }
                        else {
                                M_LADD(s2, s1, REG_ITMP1);
                                M_LADD(s2, REG_ITMP1, REG_ITMP1);
-                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_shortarray, data[0]));
-                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_shortarray, data[0]));
+                               M_LLD_U(REG_ITMP2, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
+                               M_LDA(REG_ITMP1, REG_ITMP1, OFFSET(java_shortarray_t, data[0]));
                                M_INSWL(REG_ZERO, REG_ITMP1, REG_ITMP3);
                                M_MSKWL(REG_ITMP2, REG_ITMP1, REG_ITMP2);
                                M_OR(REG_ITMP2, REG_ITMP3, REG_ITMP2);
@@ -1764,7 +1763,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_S4ADDQ(s2, s1, REG_ITMP1);
-                       M_IST(REG_ZERO, REG_ITMP1, OFFSET(java_intarray, data[0]));
+                       M_IST(REG_ZERO, REG_ITMP1, OFFSET(java_intarray_t, data[0]));
                        break;
 
                case ICMD_LASTORECONST:   /* ..., arrayref, index  ==> ...            */
@@ -1774,7 +1773,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_S8ADDQ(s2, s1, REG_ITMP1);
-                       M_LST(REG_ZERO, REG_ITMP1, OFFSET(java_longarray, data[0]));
+                       M_LST(REG_ZERO, REG_ITMP1, OFFSET(java_longarray_t, data[0]));
                        break;
 
                case ICMD_AASTORECONST:   /* ..., arrayref, index  ==> ...            */
@@ -1784,7 +1783,7 @@ bool codegen_emit(jitdata *jd)
                        /* implicit null-pointer check */
                        emit_arrayindexoutofbounds_check(cd, iptr, s1, s2);
                        M_SAADDQ(s2, s1, REG_ITMP1);
-                       M_AST(REG_ZERO, REG_ITMP1, OFFSET(java_objectarray, data[0]));
+                       M_AST(REG_ZERO, REG_ITMP1, OFFSET(java_objectarray_t, data[0]));
                        break;
 
 
@@ -1795,15 +1794,15 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = dseg_add_unique_address(cd, uf);
 
-                               codegen_add_patch_ref(cd, PATCHER_get_putstatic, uf, disp);
+                               patcher_add_patch_ref(jd, PATCHER_get_putstatic, uf, disp);
                        }
                        else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp      = dseg_add_address(cd, &(fi->value));
+                               disp      = dseg_add_address(cd, fi->value);
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       codegen_add_patch_ref(cd, PATCHER_initialize_class, fi->class,
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class,
                                                                                  0);
                        }
 
@@ -1840,15 +1839,15 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = dseg_add_unique_address(cd, uf);
 
-                               codegen_add_patch_ref(cd, PATCHER_get_putstatic, uf, disp);
+                               patcher_add_patch_ref(jd, PATCHER_get_putstatic, uf, disp);
                        }
                        else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp      = dseg_add_address(cd, &(fi->value));
+                               disp      = dseg_add_address(cd, fi->value);
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       codegen_add_patch_ref(cd, PATCHER_initialize_class, fi->class,
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class,
                                                                                  0);
                        }
 
@@ -1886,15 +1885,15 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = dseg_add_unique_address(cd, uf);
 
-                               codegen_add_patch_ref(cd, PATCHER_get_putstatic, uf, disp);
+                               patcher_add_patch_ref(jd, PATCHER_get_putstatic, uf, disp);
                        }
                        else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
                                fieldtype = fi->type;
-                               disp      = dseg_add_address(cd, &(fi->value));
+                               disp      = dseg_add_address(cd, fi->value);
 
                                if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       codegen_add_patch_ref(cd, PATCHER_initialize_class, fi->class,
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class,
                                                                                  0);
                        }
                        
@@ -1928,7 +1927,7 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = 0;
 
-                               codegen_add_patch_ref(cd, PATCHER_get_putfield, uf, 0);
+                               patcher_add_patch_ref(jd, PATCHER_get_putfield, uf, 0);
                        }
                        else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
@@ -1984,7 +1983,7 @@ bool codegen_emit(jitdata *jd)
                                s2 = emit_load_s2(jd, iptr, REG_FTMP2);
 
                        if (INSTRUCTION_IS_UNRESOLVED(iptr))
-                               codegen_add_patch_ref(cd, PATCHER_get_putfield, uf, 0);
+                               patcher_add_patch_ref(jd, PATCHER_get_putfield, uf, 0);
 
                        /* implicit null-pointer check */
                        switch (fieldtype) {
@@ -2017,7 +2016,7 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = uf->fieldref->parseddesc.fd->type;
                                disp      = 0;
 
-                               codegen_add_patch_ref(cd, PATCHER_get_putfield, uf, 0);
+                               patcher_add_patch_ref(jd, PATCHER_get_putfield, uf, 0);
                        }
                        else {
                                fi        = iptr->sx.s23.s3.fmiref->p.field;
@@ -2057,7 +2056,7 @@ bool codegen_emit(jitdata *jd)
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                unresolved_class *uc = iptr->sx.s23.s2.uc;
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_class, uc, 0);
+                               patcher_add_patch_ref(jd, PATCHER_resolve_class, uc, 0);
                        }
 #endif /* ENABLE_VERIFIER */
 
@@ -2356,7 +2355,7 @@ bool codegen_emit(jitdata *jd)
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                unresolved_class *uc = iptr->sx.s23.s2.uc;
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_class, uc, 0);
+                               patcher_add_patch_ref(jd, PATCHER_resolve_class, uc, 0);
                        }
 #endif /* ENABLE_VERIFIER */
                        goto nowperformreturn;
@@ -2537,6 +2536,8 @@ nowperformreturn:
 
                case ICMD_BUILTIN:      /* ..., arg1, arg2, arg3 ==> ...              */
 
+                       REPLACEMENT_POINT_FORGC_BUILTIN(cd, iptr);
+
                        bte = iptr->sx.s23.s3.bte;
                        md  = bte->md;
                        goto gen_method;
@@ -2583,7 +2584,7 @@ gen_method:
                                        }
                                        else {
                                                s1 = emit_load(jd, iptr, var, REG_ITMP1);
-                                               M_LST(s1, REG_SP, d * 8);
+                                               M_LST(s1, REG_SP, d);
                                        }
                                }
                                else {
@@ -2593,21 +2594,24 @@ gen_method:
                                        }
                                        else {
                                                s1 = emit_load(jd, iptr, var, REG_FTMP1);
-                                               M_DST(s1, REG_SP, d * 8);
+                                               M_DST(s1, REG_SP, d);
                                        }
                                }
                        }
 
                        switch (iptr->opc) {
                        case ICMD_BUILTIN:
-                               disp = dseg_add_functionptr(cd, bte->fp);
+                               if (bte->stub == NULL)
+                                       disp = dseg_add_functionptr(cd, bte->fp);
+                               else
+                                       disp = dseg_add_functionptr(cd, bte->stub);
 
                                M_ALD(REG_PV, REG_PV, disp);  /* Pointer to built-in-function */
 
                                /* generate the actual call */
 
                                M_JSR(REG_RA, REG_PV);
-                               REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr);
+                               REPLACEMENT_POINT_FORGC_BUILTIN_RETURN(cd, iptr);
                                disp = (s4) (cd->mcodeptr - cd->mcodebase);
                                M_LDA(REG_PV, REG_RA, -disp);
 
@@ -2622,7 +2626,7 @@ gen_method:
                                if (lm == NULL) {
                                        disp = dseg_add_unique_address(cd, um);
 
-                                       codegen_add_patch_ref(cd, PATCHER_invokestatic_special,
+                                       patcher_add_patch_ref(jd, PATCHER_invokestatic_special,
                                                                                  um, disp);
                                }
                                else
@@ -2640,7 +2644,7 @@ gen_method:
 
                        case ICMD_INVOKEVIRTUAL:
                                if (lm == NULL) {
-                                       codegen_add_patch_ref(cd, PATCHER_invokevirtual, um, 0);
+                                       patcher_add_patch_ref(jd, PATCHER_invokevirtual, um, 0);
 
                                        s1 = 0;
                                }
@@ -2649,7 +2653,7 @@ gen_method:
                                                sizeof(methodptr) * lm->vftblindex;
 
                                /* implicit null-pointer check */
-                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_object_t, vftbl));
                                M_ALD(REG_PV, REG_METHODPTR, s1);
 
                                /* generate the actual call */
@@ -2662,7 +2666,7 @@ gen_method:
 
                        case ICMD_INVOKEINTERFACE:
                                if (lm == NULL) {
-                                       codegen_add_patch_ref(cd, PATCHER_invokeinterface, um, 0);
+                                       patcher_add_patch_ref(jd, PATCHER_invokeinterface, um, 0);
 
                                        s1 = 0;
                                        s2 = 0;
@@ -2675,7 +2679,7 @@ gen_method:
                                }
                                        
                                /* implicit null-pointer check */
-                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_METHODPTR, REG_A0, OFFSET(java_object_t, vftbl));
                                M_ALD(REG_METHODPTR, REG_METHODPTR, s1);
                                M_ALD(REG_PV, REG_METHODPTR, s2);
 
@@ -2708,42 +2712,24 @@ gen_method:
 
                case ICMD_CHECKCAST:  /* ..., objectref ==> ..., objectref            */
 
-                                     /* val.a: (classinfo*) superclass               */
-
-                       /*  superclass is an interface:
-                        *      
-                        *  OK if ((sub == NULL) ||
-                        *         (sub->vftbl->interfacetablelength > super->index) &&
-                        *         (sub->vftbl->interfacetable[-super->index] != NULL));
-                        *      
-                        *  superclass is a class:
-                        *      
-                        *  OK if ((sub == NULL) || (0
-                        *         <= (sub->vftbl->baseval - super->vftbl->baseval) <=
-                        *         super->vftbl->diffval));
-                        */
-
                        if (!(iptr->flags.bits & INS_FLAG_ARRAY)) {
                                /* object type cast-check */
 
                                classinfo *super;
-                               vftbl_t   *supervftbl;
                                s4         superindex;
 
                                if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
-                                       super = NULL;
+                                       super      = NULL;
                                        superindex = 0;
-                                       supervftbl = NULL;
                                }
                                else {
-                                       super = iptr->sx.s23.s3.c.cls;
+                                       super      = iptr->sx.s23.s3.c.cls;
                                        superindex = super->index;
-                                       supervftbl = super->vftbl;
                                }
 
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritrestart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+                               if ((super == NULL) || !(super->flags & ACC_INTERFACE))
+                                       CODEGEN_CRITICAL_SECTION_NEW;
+
                                s1 = emit_load_s1(jd, iptr, REG_ITMP1);
 
                                /* if class is not resolved, check which code to call */
@@ -2753,7 +2739,7 @@ gen_method:
 
                                        disp = dseg_add_unique_s4(cd, 0);         /* super->flags */
 
-                                       codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_flags,
+                                       patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_flags,
                                                                                  iptr->sx.s23.s3.c.ref,
                                                                                  disp);
 
@@ -2768,7 +2754,7 @@ gen_method:
 
                                if ((super == NULL) || (super->flags & ACC_INTERFACE)) {
                                        if (super == NULL) {
-                                               codegen_add_patch_ref(cd,
+                                               patcher_add_patch_ref(jd,
                                                                                          PATCHER_checkcast_interface,
                                                                                          iptr->sx.s23.s3.c.ref,
                                                                                          0);
@@ -2776,7 +2762,7 @@ gen_method:
                                        else
                                                emit_label_beqz(cd, BRANCH_LABEL_3, s1);
 
-                                       M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ILD(REG_ITMP3, REG_ITMP2,
                                                  OFFSET(vftbl_t, interfacetablelength));
                                        M_LDA(REG_ITMP3, REG_ITMP3, -superindex);
@@ -2801,22 +2787,22 @@ gen_method:
 
                                                disp = dseg_add_unique_address(cd, NULL);
 
-                                               codegen_add_patch_ref(cd,
+                                               patcher_add_patch_ref(jd,
                                                                                          PATCHER_resolve_classref_to_vftbl,
                                                                                          iptr->sx.s23.s3.c.ref,
                                                                                          disp);
                                        }
                                        else {
-                                               disp = dseg_add_address(cd, supervftbl);
+                                               disp = dseg_add_address(cd, super->vftbl);
 
                                                emit_label_beqz(cd, BRANCH_LABEL_5, s1);
                                        }
 
-                                       M_ALD(REG_ITMP2, s1, OFFSET(java_objectheader, vftbl));
+                                       M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
                                        M_ALD(REG_ITMP3, REG_PV, disp);
-#if defined(ENABLE_THREADS)
-                                       codegen_threadcritstart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                                       CODEGEN_CRITICAL_SECTION_START;
+
                                        M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                        /*                              if (s1 != REG_ITMP1) { */
                                        /*                                      M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, baseval)); */
@@ -2831,9 +2817,9 @@ gen_method:
                                        M_ISUB(REG_ITMP2, REG_ITMP3, REG_ITMP2);
                                        M_ALD(REG_ITMP3, REG_PV, disp);
                                        M_ILD(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, diffval));
-#if defined(ENABLE_THREADS)
-                                       codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                                       CODEGEN_CRITICAL_SECTION_END;
+
                                        /*                              } */
                                        M_CMPULE(REG_ITMP2, REG_ITMP3, REG_ITMP3);
                                        emit_classcast_check(cd, iptr, BRANCH_EQ, REG_ITMP3, s1);
@@ -2858,7 +2844,7 @@ gen_method:
                                if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                        disp = dseg_add_unique_address(cd, NULL);
 
-                                       codegen_add_patch_ref(cd,
+                                       patcher_add_patch_ref(jd,
                                                                                  PATCHER_resolve_classref_to_classinfo,
                                                                                  iptr->sx.s23.s3.c.ref,
                                                                                  disp);
@@ -2885,21 +2871,6 @@ gen_method:
 
                case ICMD_INSTANCEOF: /* ..., objectref ==> ..., intresult            */
 
-                                     /* val.a: (classinfo*) superclass               */
-
-                       /*  superclass is an interface:
-                        *      
-                        *  return (sub != NULL) &&
-                        *         (sub->vftbl->interfacetablelength > super->index) &&
-                        *         (sub->vftbl->interfacetable[-super->index] != NULL);
-                        *      
-                        *  superclass is a class:
-                        *      
-                        *  return ((sub != NULL) && (0
-                        *          <= (sub->vftbl->baseval - super->vftbl->baseval) <=
-                        *          super->vftbl->diffvall));
-                        */
-
                        {
                        classinfo *super;
                        vftbl_t   *supervftbl;
@@ -2916,9 +2887,9 @@ gen_method:
                                supervftbl = super->vftbl;
                        }
 
-#if defined(ENABLE_THREADS)
-                       codegen_threadcritrestart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+                       if ((super == NULL) || !(super->flags & ACC_INTERFACE))
+                               CODEGEN_CRITICAL_SECTION_NEW;
+
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
 
@@ -2935,7 +2906,7 @@ gen_method:
 
                                disp = dseg_add_unique_s4(cd, 0);             /* super->flags */
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_flags,
+                               patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_flags,
                                                                          iptr->sx.s23.s3.c.ref, disp);
 
                                M_ILD(REG_ITMP3, REG_PV, disp);
@@ -2955,7 +2926,7 @@ gen_method:
                                        if (d == REG_ITMP2)
                                                M_CLR(d);
 
-                                       codegen_add_patch_ref(cd,
+                                       patcher_add_patch_ref(jd,
                                                                                  PATCHER_instanceof_interface,
                                                                                  iptr->sx.s23.s3.c.ref, 0);
                                }
@@ -2964,7 +2935,7 @@ gen_method:
                                        emit_label_beqz(cd, BRANCH_LABEL_3, s1);
                                }
 
-                               M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ILD(REG_ITMP3, REG_ITMP1, OFFSET(vftbl_t, interfacetablelength));
                                M_LDA(REG_ITMP3, REG_ITMP3, -superindex);
                                M_BLEZ(REG_ITMP3, 2);
@@ -2987,7 +2958,7 @@ gen_method:
 
                                        disp = dseg_add_unique_address(cd, NULL);
 
-                                       codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_vftbl,
+                                       patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_vftbl,
                                                                                  iptr->sx.s23.s3.c.ref,
                                                                                  disp);
                                }
@@ -2998,17 +2969,17 @@ gen_method:
                                        emit_label_beqz(cd, BRANCH_LABEL_5, s1);
                                }
 
-                               M_ALD(REG_ITMP1, s1, OFFSET(java_objectheader, vftbl));
+                               M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
                                M_ALD(REG_ITMP2, REG_PV, disp);
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritstart(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                               CODEGEN_CRITICAL_SECTION_START;
+
                                M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
                                M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, baseval));
                                M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, diffval));
-#if defined(ENABLE_THREADS)
-                               codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase);
-#endif
+
+                               CODEGEN_CRITICAL_SECTION_END;
+
                                M_ISUB(REG_ITMP1, REG_ITMP3, REG_ITMP1);
                                M_CMPULE(REG_ITMP1, REG_ITMP2, d);
 
@@ -3054,7 +3025,7 @@ gen_method:
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                disp = dseg_add_unique_address(cd, 0);
 
-                               codegen_add_patch_ref(cd, PATCHER_resolve_classref_to_classinfo,
+                               patcher_add_patch_ref(jd, PATCHER_resolve_classref_to_classinfo,
                                                                          iptr->sx.s23.s3.c.ref,
                                                                          disp);
                        }
@@ -3097,10 +3068,9 @@ gen_method:
 
        dseg_createlinenumbertable(cd);
 
-       /* generate stubs */
+       /* generate traps */
 
-       emit_patcher_stubs(jd);
-       REPLACEMENT_EMIT_STUBS(jd);
+       emit_patcher_traps(jd);
 
        /* everything's ok */
 
@@ -3132,6 +3102,185 @@ void codegen_emit_stub_compiler(jitdata *jd)
 }
 
 
+/* codegen_emit_stub_builtin ***************************************************
+
+   Emits a stub routine which calls a builtin function.
+
+*******************************************************************************/
+
+void codegen_emit_stub_builtin(jitdata *jd, builtintable_entry *bte)
+{
+       codeinfo    *code;
+       codegendata *cd;
+       methoddesc  *md;
+       s4           i;
+       s4           disp;
+       s4           s1;
+
+       /* get required compiler data */
+
+       code = jd->code;
+       cd   = jd->cd;
+
+       /* set some variables */
+
+       md = bte->md;
+
+       /* calculate stack frame size */
+
+       cd->stackframesize =
+               1 +                             /* return address                     */
+               sizeof(stackframeinfo) / SIZEOF_VOID_P +
+               md->paramcount;
+
+       /* 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 */
+
+       M_LDA(REG_SP, REG_SP, -(cd->stackframesize * 8));
+       M_AST(REG_RA, REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
+
+
+#if defined(ENABLE_GC_CACAO)
+       /* Save callee saved integer registers in stackframeinfo (GC may
+          need to recover them during a collection). */
+
+       disp = cd->stackframesize * 8 - SIZEOF_VOID_P - sizeof(stackframeinfo) +
+               OFFSET(stackframeinfo, intregs);
+
+       for (i = 0; i < INT_SAV_CNT; i++)
+               M_AST(abi_registers_integer_saved[i], REG_SP, disp + i * 8);
+#endif
+
+       /* save integer and float argument registers */
+
+       for (i = 0; i < md->paramcount; i++) {
+               if (!md->params[i].inmemory) {
+                       s1 = md->params[i].regoff;
+
+                       switch (md->paramtypes[i].type) {
+                       case TYPE_INT:
+                       case TYPE_LNG:
+                       case TYPE_ADR:
+                               M_LST(s1, REG_SP, i * 8);
+                               break;
+                       case TYPE_FLT:
+                       case TYPE_DBL:
+                               M_DST(s1, REG_SP, i * 8);
+                               break;
+                       }
+               }
+       }
+
+       /* prepare data structures for native function call */
+
+       M_LDA(REG_A0, REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
+       M_MOV(REG_PV, REG_A1);
+       M_LDA(REG_A2, REG_SP, cd->stackframesize * 8);
+       M_ALD(REG_A3, REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
+       disp = dseg_add_functionptr(cd, codegen_stub_builtin_enter);
+       M_ALD(REG_PV, REG_PV, disp);
+       M_JSR(REG_RA, REG_PV);
+       disp = (s4) (cd->mcodeptr - cd->mcodebase);
+       M_LDA(REG_PV, REG_RA, -disp);
+
+       /* restore integer and float argument registers */
+
+       for (i = 0; i < md->paramcount; i++) {
+               if (!md->params[i].inmemory) {
+                       s1 = md->params[i].regoff;
+
+                       switch (md->paramtypes[i].type) {
+                       case TYPE_INT:
+                       case TYPE_LNG:
+                       case TYPE_ADR:
+                               M_LLD(s1, REG_SP, i * 8);
+                               break;
+                       case TYPE_FLT:
+                       case TYPE_DBL:
+                               M_DLD(s1, REG_SP, i * 8);
+                               break;
+                       }
+               }
+       }
+
+       /* do the builtin function call */
+
+       disp = dseg_add_functionptr(cd, bte->fp);
+       M_ALD(REG_PV, REG_PV, disp);
+       M_JSR(REG_RA, REG_PV);
+       disp = (s4) (cd->mcodeptr - cd->mcodebase);
+       M_LDA(REG_PV, REG_RA, -disp);       /* recompute pv from ra               */
+
+       /* save return value */
+
+       switch (md->returntype.type) {
+       case TYPE_INT:
+       case TYPE_LNG:
+       case TYPE_ADR:
+               M_LST(REG_RESULT, REG_SP, 0 * 8);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DST(REG_FRESULT, REG_SP, 0 * 8);
+               break;
+       case TYPE_VOID:
+               break;
+       }
+
+       /* remove native stackframe info */
+
+       M_LDA(REG_A0, REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
+       disp = dseg_add_functionptr(cd, codegen_stub_builtin_exit);
+       M_ALD(REG_PV, REG_PV, disp);
+       M_JSR(REG_RA, REG_PV);
+       disp = (s4) (cd->mcodeptr - cd->mcodebase);
+       M_LDA(REG_PV, REG_RA, -disp);
+       M_MOV(REG_RESULT, REG_ITMP1_XPTR);
+
+       /* restore return value */
+
+       switch (md->returntype.type) {
+       case TYPE_INT:
+       case TYPE_LNG:
+       case TYPE_ADR:
+               M_LLD(REG_RESULT, REG_SP, 0 * 8);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DLD(REG_FRESULT, REG_SP, 0 * 8);
+               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_VOID_P - sizeof(stackframeinfo) +
+               OFFSET(stackframeinfo, intregs);
+
+       for (i = 0; i < INT_SAV_CNT; i++)
+               M_ALD(abi_registers_integer_saved[i], REG_SP, disp + i * 8);
+#endif
+
+       M_ALD(REG_RA, REG_SP, (cd->stackframesize - 1) * 8); /* get RA            */
+       M_LDA(REG_SP, REG_SP, cd->stackframesize * 8);
+
+       M_RET(REG_ZERO, REG_RA);            /* return to caller                   */
+}
+
+
 /* codegen_emit_stub_native ****************************************************
 
    Emits a stub routine which calls a native method.
@@ -3200,7 +3349,18 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
 
 #if !defined(WITH_STATIC_CLASSPATH)
        if (f == NULL)
-               codegen_add_patch_ref(cd, PATCHER_resolve_native_function, m, funcdisp);
+               patcher_add_patch_ref(jd, PATCHER_resolve_native_function, m, funcdisp);
+#endif
+
+#if defined(ENABLE_GC_CACAO)
+       /* Save callee saved integer registers in stackframeinfo (GC may
+          need to recover them during a collection). */
+
+       disp = cd->stackframesize * 8 - SIZEOF_VOID_P - sizeof(stackframeinfo) +
+               OFFSET(stackframeinfo, intregs);
+
+       for (i = 0; i < INT_SAV_CNT; i++)
+               M_AST(abi_registers_integer_saved[i], REG_SP, disp + i * 8);
 #endif
 
        /* save integer and float argument registers */
@@ -3268,13 +3428,13 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                                if (!nmd->params[j].inmemory)
                                        M_INTMOVE(s1, s2);
                                else
-                                       M_LST(s1, REG_SP, s2 * 8);
+                                       M_LST(s1, REG_SP, s2);
                        }
                        else {
-                               s1 = md->params[i].regoff + cd->stackframesize;
+                               s1 = md->params[i].regoff + cd->stackframesize * 8;
                                s2 = nmd->params[j].regoff;
-                               M_LLD(REG_ITMP1, REG_SP, s1 * 8);
-                               M_LST(REG_ITMP1, REG_SP, s2 * 8);
+                               M_LLD(REG_ITMP1, REG_SP, s1);
+                               M_LST(REG_ITMP1, REG_SP, s2);
                        }
                }
                else {
@@ -3286,19 +3446,19 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                                        M_FLTMOVE(s1, s2);
                                else {
                                        if (IS_2_WORD_TYPE(t))
-                                               M_DST(s1, REG_SP, s2 * 8);
+                                               M_DST(s1, REG_SP, s2);
                                        else
-                                               M_FST(s1, REG_SP, s2 * 8);
+                                               M_FST(s1, REG_SP, s2);
                                }
                        }
                        else {
-                               s1 = md->params[i].regoff + cd->stackframesize;
+                               s1 = md->params[i].regoff + cd->stackframesize * 8;
                                s2 = nmd->params[j].regoff;
-                               M_DLD(REG_FTMP1, REG_SP, s1 * 8);
+                               M_DLD(REG_FTMP1, REG_SP, s1);
                                if (IS_2_WORD_TYPE(t))
-                                       M_DST(REG_FTMP1, REG_SP, s2 * 8);
+                                       M_DST(REG_FTMP1, REG_SP, s2);
                                else
-                                       M_FST(REG_FTMP1, REG_SP, s2 * 8);
+                                       M_FST(REG_FTMP1, REG_SP, s2);
                        }
                }
        }
@@ -3371,6 +3531,17 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
                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_VOID_P - sizeof(stackframeinfo) +
+               OFFSET(stackframeinfo, intregs);
+
+       for (i = 0; i < INT_SAV_CNT; i++)
+               M_ALD(abi_registers_integer_saved[i], REG_SP, disp + i * 8);
+#endif
+
        M_ALD(REG_RA, REG_SP, (cd->stackframesize - 1) * 8); /* get RA            */
        M_LDA(REG_SP, REG_SP, cd->stackframesize * 8);
 
@@ -3389,7 +3560,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f)
        
        /* generate patcher stubs */
 
-       emit_patcher_stubs(jd);
+       emit_patcher_traps(jd);
 }