Merged trunk and subtype.
[cacao.git] / src / vm / jit / i386 / codegen.c
index 57a79435b113a92ed8f3f01f5240419229e44f11..a5f09828d93868345d8466f24a8e3feb62a1709a 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/i386/codegen.c - machine code generator for i386
 
-   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 "vm/jit/i386/emit.h"
 
 #include "mm/memory.h"
-#include "native/jni.h"
+
 #include "native/localref.h"
 #include "native/native.h"
 
 #include "threads/lock-common.h"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/exceptions.hpp"
 #include "vm/global.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/loader.hpp"
+#include "vm/options.h"
+#include "vm/primitive.hpp"
+#include "vm/utf8.h"
+#include "vm/vm.hpp"
 
 #include "vm/jit/abi.h"
 #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.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/stacktrace.h"
+#include "vm/jit/replace.hpp"
+#include "vm/jit/stacktrace.hpp"
+#include "vm/jit/trap.h"
 
 #if defined(ENABLE_SSA)
 # include "vm/jit/optimizing/lsra.h"
 # include "vm/jit/allocator/lsra.h"
 #endif
 
-#include "vmcore/loader.h"
-#include "vmcore/options.h"
-#include "vmcore/utf8.h"
-
 
 /* codegen_emit ****************************************************************
 
@@ -88,10 +87,10 @@ bool codegen_emit(jitdata *jd)
        codegendata        *cd;
        registerdata       *rd;
        s4                  len, s1, s2, s3, d, disp;
+       int                 align_off;      /* offset for alignment compensation  */
        varinfo            *var, *var1;
        basicblock         *bptr;
        instruction        *iptr;
-       exception_entry    *ex;
        u2                  currentline;
        methodinfo         *lm;             /* local methodinfo for ICMD_INVOKE*  */
        builtintable_entry *bte;
@@ -140,7 +139,7 @@ bool codegen_emit(jitdata *jd)
 #if defined(ENABLE_THREADS)
        /* space to save argument of monitor_enter */
 
-       if (checksync && (m->flags & ACC_SYNCHRONIZED))
+       if (checksync && code_is_synchronized(code))
                cd->stackframesize++;
 #endif
 
@@ -149,25 +148,19 @@ bool codegen_emit(jitdata *jd)
     /* Keep stack of non-leaf functions 16-byte aligned. */
 
        if (!code_is_leafmethod(code)) {
-               ALIGN_ODD(cd->stackframesize);    /* XXX this is wrong, +4 is missing */
+               ALIGN_ODD(cd->stackframesize);
        }
 
+       align_off = cd->stackframesize ? 4 : 0;
+
        (void) dseg_add_unique_address(cd, code);              /* CodeinfoPointer */
-       (void) dseg_add_unique_s4(cd, cd->stackframesize * 8); /* FrameSize       */
+       (void) dseg_add_unique_s4(
+               cd, cd->stackframesize * 8 + align_off);           /* FrameSize       */
+
+       code->synchronizedoffset = rd->memuse * 8;
+
+       /* REMOVEME: We still need it for exception handling in assembler. */
 
-#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        */
-       else
-#endif
-               (void) dseg_add_unique_s4(cd, 0);                  /* IsSync          */
-                                              
        if (code_is_leafmethod(code))
                (void) dseg_add_unique_s4(cd, 1);                  /* IsLeaf          */
        else
@@ -176,24 +169,6 @@ bool codegen_emit(jitdata *jd)
        (void) dseg_add_unique_s4(cd, INT_SAV_CNT - rd->savintreguse); /* IntSave */
        (void) dseg_add_unique_s4(cd, FLT_SAV_CNT - rd->savfltreguse); /* FltSave */
 
-       /* adds a reference for the length of the line number counter. We don't
-          know the size yet, since we evaluate the information during code
-          generation, to save one additional iteration over the whole
-          instructions. During code optimization the position could have changed
-          to the information gotten from the class file */
-       (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);
-       }
-
 #if defined(ENABLE_PROFILING)
        /* generate method profiling code */
 
@@ -208,7 +183,8 @@ bool codegen_emit(jitdata *jd)
        /* create stack frame (if necessary) */
 
        if (cd->stackframesize)
-               M_ASUB_IMM(cd->stackframesize * 8, REG_SP);
+               /* align_off == 4 */
+               M_ASUB_IMM(cd->stackframesize * 8 + 4, REG_SP);
 
        /* save return address and used callee saved registers */
 
@@ -261,7 +237,8 @@ bool codegen_emit(jitdata *jd)
                        } 
                        else {
                                if (!(var->flags & INMEMORY)) {
-                                       M_ILD(d, REG_SP, cd->stackframesize * 8 + 4 + s1);
+                                       M_ILD(d, REG_SP,
+                                                 cd->stackframesize * 8 + 4 + align_off + s1);
                                } 
                                else {
                                        if (!IS_2_WORD_TYPE(t)) {
@@ -269,15 +246,17 @@ bool codegen_emit(jitdata *jd)
                                                /* no copy avoiding by now possible with SSA */
                                                if (ls != NULL) {
                                                        emit_mov_membase_reg(   /* + 4 for return address */
-                                                                cd, REG_SP, cd->stackframesize * 8 + s1 + 4,
-                                                                REG_ITMP1);    
+                                                               cd, REG_SP,
+                                                               cd->stackframesize * 8 + s1 + 4 + align_off,
+                                                               REG_ITMP1);    
                                                        emit_mov_reg_membase(
-                                                                cd, REG_ITMP1, REG_SP, var->vv.regoff);
+                                                               cd, REG_ITMP1, REG_SP, var->vv.regoff);
                                                }
                                                else 
 #endif /*defined(ENABLE_SSA)*/
                                                        /* reuse stackslot */
-                                                       var->vv.regoff = cd->stackframesize * 8 + 4 + s1;
+                                                       var->vv.regoff = cd->stackframesize * 8 + 4 +
+                                                               align_off + s1;
 
                                        } 
                                        else {
@@ -285,20 +264,22 @@ bool codegen_emit(jitdata *jd)
                                                /* no copy avoiding by now possible with SSA */
                                                if (ls != NULL) {
                                                        emit_mov_membase_reg(  /* + 4 for return address */
-                                                                cd, REG_SP, cd->stackframesize * 8 + s1 + 4,
-                                                                REG_ITMP1);
+                                                               cd, REG_SP,
+                                                               cd->stackframesize * 8 + s1 + 4 + align_off,
+                                                               REG_ITMP1);
                                                        emit_mov_reg_membase(
-                                                                cd, REG_ITMP1, REG_SP, var->vv.regoff);
+                                                               cd, REG_ITMP1, REG_SP, var->vv.regoff);
                                                        emit_mov_membase_reg(   /* + 4 for return address */
-                                                                 cd, REG_SP, cd->stackframesize * 8 + s1 + 4 + 4,
-                                                                 REG_ITMP1);             
+                                                               cd, REG_SP,
+                                                               cd->stackframesize * 8 + s1 + 4 + 4 + align_off,
+                                                               REG_ITMP1);             
                                                        emit_mov_reg_membase(
-                                                                cd, REG_ITMP1, REG_SP, var->vv.regoff + 4);
+                                                               cd, REG_ITMP1, REG_SP, var->vv.regoff + 4);
                                                }
                                                else
 #endif /*defined(ENABLE_SSA)*/
                                                        /* reuse stackslot */
-                                                       var->vv.regoff = cd->stackframesize * 8 + 4 + s1;
+                                                       var->vv.regoff = cd->stackframesize * 8 + 8 + s1;
                                        }
                                }
                        }
@@ -318,14 +299,16 @@ bool codegen_emit(jitdata *jd)
                                if (!(var->flags & INMEMORY)) {      /* stack-arg -> register */
                                        if (t == TYPE_FLT) {
                                                emit_flds_membase(
-                            cd, REG_SP, cd->stackframesize * 8 + s1 + 4);
+                            cd, REG_SP,
+                                                       cd->stackframesize * 8 + s1 + 4 + align_off);
                                                assert(0);
 /*                                             emit_fstp_reg(cd, var->vv.regoff + fpu_st_offset); */
 
                                        } 
                                        else {
                                                emit_fldl_membase(
-                            cd, REG_SP, cd->stackframesize * 8 + s1 + 4);
+                            cd, REG_SP,
+                                                       cd->stackframesize * 8 + s1 + 4 + align_off);
                                                assert(0);
 /*                                             emit_fstp_reg(cd, var->vv.regoff + fpu_st_offset); */
                                        }
@@ -335,24 +318,29 @@ bool codegen_emit(jitdata *jd)
                                        /* no copy avoiding by now possible with SSA */
                                        if (ls != NULL) {
                                                emit_mov_membase_reg(
-                                                cd, REG_SP, cd->stackframesize * 8 + s1 + 4, REG_ITMP1);
+                                                       cd, REG_SP,
+                                                       cd->stackframesize * 8 + s1 + 4 + align_off,
+                                                       REG_ITMP1);
                                                emit_mov_reg_membase(
-                                                                        cd, REG_ITMP1, REG_SP, var->vv.regoff);
+                                                       cd, REG_ITMP1, REG_SP, var->vv.regoff);
                                                if (t == TYPE_FLT) {
                                                        emit_flds_membase(
-                                                                 cd, REG_SP, cd->stackframesize * 8 + s1 + 4);
+                                                               cd, REG_SP,
+                                                               cd->stackframesize * 8 + s1 + 4 + align_off);
                                                        emit_fstps_membase(cd, REG_SP, var->vv.regoff);
                                                } 
                                                else {
                                                        emit_fldl_membase(
-                                                                 cd, REG_SP, cd->stackframesize * 8 + s1 + 4);
+                                                               cd, REG_SP,
+                                                               cd->stackframesize * 8 + s1 + 4 + align_off);
                                                        emit_fstpl_membase(cd, REG_SP, var->vv.regoff);
                                                }
                                        }
                                        else
 #endif /*defined(ENABLE_SSA)*/
                                                /* reuse stackslot */
-                                               var->vv.regoff = cd->stackframesize * 8 + 4 + s1;
+                                               var->vv.regoff = cd->stackframesize * 8 + 4 +
+                                                       align_off + s1;
                                }
                        }
                }
@@ -361,17 +349,17 @@ bool codegen_emit(jitdata *jd)
        /* call monitorenter function */
 
 #if defined(ENABLE_THREADS)
-       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+       if (checksync && code_is_synchronized(code)) {
                s1 = rd->memuse;
 
                if (m->flags & ACC_STATIC) {
-                       M_MOV_IMM(&m->class->object.header, REG_ITMP1);
+                       M_MOV_IMM(&m->clazz->object.header, REG_ITMP1);
                }
                else {
-                       M_ALD(REG_ITMP1, REG_SP, cd->stackframesize * 8 + 4);
+                       M_ALD(REG_ITMP1, REG_SP, cd->stackframesize * 8 + 4 + align_off);
                        M_TEST(REG_ITMP1);
                        M_BNE(6);
-                       M_ALD_MEM(REG_ITMP1, EXCEPTION_HARDWARE_NULLPOINTER);
+                       M_ALD_MEM(REG_ITMP1, TRAP_NullPointerException);
                }
 
                M_AST(REG_ITMP1, REG_SP, s1 * 8);
@@ -418,9 +406,7 @@ bool codegen_emit(jitdata *jd)
                if (bptr->bitflags & BBFLAG_REPLACEMENT) {
                        if (cd->replacementpoint[-1].flags & RPLPOINT_FLAG_COUNTDOWN) {
                                MCODECHECK(32);
-                               disp = (s4) &(m->hitcountdown);
-                               M_ISUB_IMM_MEMABS(1, disp);
-                               M_BS(0);
+                               emit_trap_countdown(cd, &(m->hitcountdown));
                        }
                }
 #endif
@@ -454,11 +440,13 @@ bool codegen_emit(jitdata *jd)
                                var = VAR(bptr->invars[len]);
                                if (bptr->type != BBTYPE_STD) {
                                        if (!IS_2_WORD_TYPE(var->type)) {
+#if !defined(ENABLE_SSA)
                                                if (bptr->type == BBTYPE_EXH) {
                                                        d = codegen_reg_of_var(0, var, REG_ITMP1);
                                                        M_INTMOVE(REG_ITMP1, d);
                                                        emit_store(jd, NULL, var, d);
                                                }
+#endif
                                        } 
                                        else {
                                                log_text("copy interface registers(EXH, SBR): longs \
@@ -503,7 +491,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;
                        }
 
@@ -523,14 +511,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           */
@@ -2205,8 +2193,8 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = fi->type;
                                disp      = (intptr_t) fi->value;
 
-                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class, 0);
+                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->clazz))
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->clazz, 0);
                        }
 
                        M_MOV_IMM(disp, REG_ITMP1);
@@ -2233,7 +2221,7 @@ bool codegen_emit(jitdata *jd)
                        break;
 
                case ICMD_PUTSTATIC:  /* ..., value  ==> ...                          */
-
+                       
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
@@ -2246,8 +2234,8 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = fi->type;
                                disp      = (intptr_t) fi->value;
 
-                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class, 0);
+                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->clazz))
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->clazz, 0);
                        }
 
                        M_MOV_IMM(disp, REG_ITMP1);
@@ -2288,8 +2276,8 @@ bool codegen_emit(jitdata *jd)
                                fieldtype = fi->type;
                                disp      = (intptr_t) fi->value;
 
-                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
-                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->class, 0);
+                               if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->clazz))
+                                       patcher_add_patch_ref(jd, PATCHER_initialize_class, fi->clazz, 0);
                        }
 
                        M_MOV_IMM(disp, REG_ITMP1);
@@ -2312,6 +2300,10 @@ bool codegen_emit(jitdata *jd)
                        s1 = emit_load_s1(jd, iptr, REG_ITMP1);
                        emit_nullpointer_check(cd, iptr, s1);
 
+#if defined(ENABLE_ESCAPE_CHECK)
+                       /*emit_escape_check(cd, s1);*/
+#endif
+
                        if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
                                uf        = iptr->sx.s23.s3.uf;
                                fieldtype = uf->fieldref->parseddesc.fd->type;
@@ -2746,7 +2738,7 @@ nowperformreturn:
 #endif
 
 #if defined(ENABLE_THREADS)
-                       if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
+                       if (checksync && code_is_synchronized(code)) {
                                M_ALD(REG_ITMP2, REG_SP, rd->memuse * 8);
 
                                /* we need to save the proper return value */
@@ -2816,7 +2808,7 @@ nowperformreturn:
                        /* deallocate stack */
 
                        if (cd->stackframesize)
-                               M_AADD_IMM(cd->stackframesize * 8, REG_SP);
+                               M_AADD_IMM(cd->stackframesize * 8 + 4, REG_SP);
 
                        M_RET;
                        }
@@ -2895,6 +2887,21 @@ nowperformreturn:
 
                        bte = iptr->sx.s23.s3.bte;
                        md = bte->md;
+
+#if defined(ENABLE_ESCAPE_REASON)
+                       if (bte->fp == BUILTIN_escape_reason_new) {
+                               void set_escape_reasons(void *);
+                               M_ASUB_IMM(8, REG_SP);
+                               M_MOV_IMM(iptr->escape_reasons, REG_ITMP1);
+                               M_AST(EDX, REG_SP, 4);
+                               M_AST(REG_ITMP1, REG_SP, 0);
+                               M_MOV_IMM(set_escape_reasons, REG_ITMP1);
+                               M_CALL(REG_ITMP1);
+                               M_ALD(EDX, REG_SP, 4);
+                               M_AADD_IMM(8, REG_SP);
+                       }
+#endif
+
                        goto gen_method;
 
                case ICMD_INVOKESTATIC: /* ..., [arg1, [arg2 ...]] ==> ...            */
@@ -2969,6 +2976,12 @@ gen_method:
                                        M_MOV_IMM(bte->stub, REG_ITMP1);
                                }
                                M_CALL(REG_ITMP1);
+
+#if defined(ENABLE_ESCAPE_CHECK)
+                               if (bte->opcode == ICMD_NEW || bte->opcode == ICMD_NEWARRAY) {
+                                       /*emit_escape_annotate_object(cd, m);*/
+                               }
+#endif
                                break;
 
                        case ICMD_INVOKESPECIAL:
@@ -3034,9 +3047,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);
 
                                        d = md->returntype.type;
                                }
@@ -3102,9 +3115,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 */
@@ -3168,39 +3178,49 @@ gen_method:
                                        }
 
                                        M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
-
                                        if (super == NULL) {
                                                patcher_add_patch_ref(jd, PATCHER_checkcast_class,
                                                                                        iptr->sx.s23.s3.c.ref,
                                                                                        0);
                                        }
-
                                        M_MOV_IMM(supervftbl, REG_ITMP3);
 
-                                       CODEGEN_CRITICAL_SECTION_START;
+                                       if (super == NULL || super->vftbl->subtype_depth >= DISPLAY_SIZE) {
+                                               M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_offset));
+                                               M_CMP_MEMINDEX(REG_ITMP2, 0, REG_ITMP1, 0, REG_ITMP3);
+                                               emit_label_beq(cd, BRANCH_LABEL_6);  /* good */
+
+                                               if (super == NULL) {
+                                                       M_ICMP_IMM(OFFSET(vftbl_t, subtype_display[DISPLAY_SIZE]), REG_ITMP1);
+                                                       emit_label_bne(cd, BRANCH_LABEL_10);  /* throw */
+                                               }
 
-                                       M_ILD32(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
+                                               M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_depth));
+                                               M_CMP_MEMBASE(REG_ITMP2, OFFSET(vftbl_t, subtype_depth), REG_ITMP1);
+                                               emit_label_bgt(cd, BRANCH_LABEL_9);  /* throw */
 
-                                       /*                              if (s1 != REG_ITMP1) { */
-                                       /*                                      emit_mov_membase_reg(cd, REG_ITMP3, OFFSET(vftbl_t, baseval), REG_ITMP1); */
-                                       /*                                      emit_mov_membase_reg(cd, REG_ITMP3, OFFSET(vftbl_t, diffval), REG_ITMP3); */
-                                       /* #if defined(ENABLE_THREADS) */
-                                       /*                                      codegen_threadcritstop(cd, cd->mcodeptr - cd->mcodebase); */
-                                       /* #endif */
-                                       /*                                      emit_alu_reg_reg(cd, ALU_SUB, REG_ITMP1, REG_ITMP2); */
+                                               M_ALD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, subtype_overflow));
+                                               M_CMP_MEMINDEX(REG_ITMP2, -4*DISPLAY_SIZE, REG_ITMP1, 2, REG_ITMP3);
+                                               emit_label_beq(cd, BRANCH_LABEL_7);  /* good */
 
-                                       /*                              } else { */
-                                       M_ILD32(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, baseval));
-                                       M_ISUB(REG_ITMP3, REG_ITMP2);
-                                       M_MOV_IMM(supervftbl, REG_ITMP3);
-                                       M_ILD(REG_ITMP3, REG_ITMP3, OFFSET(vftbl_t, diffval));
+                                               emit_label(cd, BRANCH_LABEL_9);
+                                               if (super == NULL)
+                                                       emit_label(cd, BRANCH_LABEL_10);
 
-                                       CODEGEN_CRITICAL_SECTION_END;
+                                               /* reload s1, might have been destroyed */
+                                               emit_load_s1(jd, iptr, REG_ITMP1);
+                                               M_ALD_MEM(s1, TRAP_ClassCastException);
 
-                                       /*                              } */
+                                               emit_label(cd, BRANCH_LABEL_7);
+                                               emit_label(cd, BRANCH_LABEL_6);
+                                               /* reload s1, might have been destroyed */
+                                               emit_load_s1(jd, iptr, REG_ITMP1);
+                                       }
+                                       else {
+                                               M_CMP_MEMBASE(REG_ITMP2, super->vftbl->subtype_offset, REG_ITMP3);
 
-                                       M_CMP(REG_ITMP3, REG_ITMP2);
-                                       emit_classcast_check(cd, iptr, BRANCH_ULE, REG_ITMP3, s1);
+                                               emit_classcast_check(cd, iptr, BRANCH_NE, REG_ITMP3, s1);
+                                       }
 
                                        if (super != NULL)
                                                emit_label(cd, BRANCH_LABEL_5);
@@ -3257,9 +3277,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);
                        d = codegen_reg_of_dst(jd, iptr, REG_ITMP2);
 
@@ -3334,28 +3351,60 @@ gen_method:
                                        emit_label_beq(cd, BRANCH_LABEL_5);
                                }
 
-                               M_ALD(REG_ITMP1, s1, OFFSET(java_object_t, vftbl));
-
+                               M_ALD(REG_ITMP2, s1, OFFSET(java_object_t, vftbl));
                                if (super == NULL) {
                                        patcher_add_patch_ref(jd, PATCHER_instanceof_class,
                                                                                iptr->sx.s23.s3.c.ref, 0);
                                }
+                               M_MOV_IMM(supervftbl, REG_ITMP3);
+
+                               if (super == NULL || super->vftbl->subtype_depth >= DISPLAY_SIZE) {
+                                       M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_offset));
+                                       M_CMP_MEMINDEX(REG_ITMP2, 0, REG_ITMP1, 0, REG_ITMP3);
+                                       emit_label_bne(cd, BRANCH_LABEL_8); /* jump over INC/SETE */
+                                       if (d == REG_ITMP2) {
+                                               M_SETE(d);
+                                               M_BSEXT(d, d);
+                                       } else
+                                               M_IINC(d);
+                                       emit_label_br(cd, BRANCH_LABEL_6);  /* true */
+                                       emit_label(cd, BRANCH_LABEL_8);
+
+                                       if (super == NULL) {
+                                               M_ICMP_IMM(OFFSET(vftbl_t, subtype_display[DISPLAY_SIZE]), REG_ITMP1);
+                                               emit_label_bne(cd, BRANCH_LABEL_10);  /* false */
+                                       }
 
-                               M_MOV_IMM(supervftbl, REG_ITMP2);
+                                       M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_depth));
+                                       M_CMP_MEMBASE(REG_ITMP2, OFFSET(vftbl_t, subtype_depth), REG_ITMP1);
+                                       emit_label_bgt(cd, BRANCH_LABEL_9);  /* false */
 
-                               CODEGEN_CRITICAL_SECTION_START;
+                                       M_ALD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, subtype_overflow));
+                                       M_CMP_MEMINDEX(REG_ITMP2, -4*DISPLAY_SIZE, REG_ITMP1, 2, REG_ITMP3);
+                                       M_SETE(d);
+                                       if (d == REG_ITMP2) {
+                                               M_BSEXT(d, d);
 
-                               M_ILD(REG_ITMP1, REG_ITMP1, OFFSET(vftbl_t, baseval));
-                               M_ILD(REG_ITMP3, REG_ITMP2, OFFSET(vftbl_t, diffval));
-                               M_ILD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, baseval));
+                                               emit_label_br(cd, BRANCH_LABEL_7); /* jump over M_CLR */
+                                       }
 
-                               CODEGEN_CRITICAL_SECTION_END;
+                                       emit_label(cd, BRANCH_LABEL_9);
+                                       if (super == NULL)
+                                               emit_label(cd, BRANCH_LABEL_10);
+                                       if (d == REG_ITMP2) {
+                                               M_CLR(d);
 
-                               M_ISUB(REG_ITMP2, REG_ITMP1);
-                               M_CLR(d);                                 /* may be REG_ITMP2 */
-                               M_CMP(REG_ITMP3, REG_ITMP1);
-                               M_BA(5);
-                               M_MOV_IMM(1, d);
+                                               emit_label(cd, BRANCH_LABEL_7);
+                                       }
+                                       emit_label(cd, BRANCH_LABEL_6);
+                               }
+                               else {
+                                       M_CMP_MEMBASE(REG_ITMP2, super->vftbl->subtype_offset, REG_ITMP3);
+
+                                       M_SETE(d);
+                                       if (d == REG_ITMP2)
+                                               M_BSEXT(d, d);
+                               }
 
                                if (super != NULL)
                                        emit_label(cd, BRANCH_LABEL_5);
@@ -3429,6 +3478,13 @@ gen_method:
                        emit_store_dst(jd, iptr, s1);
                        break;
 
+#if defined(ENABLE_SSA)
+               case ICMD_GETEXCEPTION:
+                       d = codegen_reg_of_dst(jd, iptr, REG_ITMP1);
+                       M_INTMOVE(REG_ITMP1, d);
+                       emit_store_dst(jd, iptr, d);
+                       break;
+#endif
                default:
                        exceptions_throw_internalerror("Unknown ICMD %d during code generation",
                                                                                   iptr->opc);
@@ -3468,8 +3524,6 @@ gen_method:
        } /* if (bptr -> flags >= BBREACHED) */
        } /* for basic block */
 
-       dseg_createlinenumbertable(cd);
-
        /* generate stubs */
 
        emit_patcher_traps(jd);
@@ -3494,10 +3548,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
        methoddesc  *md;
        int          i, j;                 /* count variables                    */
        int          s1, s2;
-       int          funcdisp;
-#if defined(ENABLE_GC_CACAO)
        int          disp;
-#endif
 
        /* get required compiler data */
 
@@ -3519,18 +3570,15 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
 
     /* keep stack 16-byte aligned */
 
-       ALIGN_ODD(cd->stackframesize);        /* XXX this is wrong, +4 is missing */
+       ALIGN_ODD(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, cd->stackframesize * 8 + 4); /* FrameSize       */
        (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     */
 
 #if defined(ENABLE_PROFILING)
        /* generate native method profiling code */
@@ -3545,14 +3593,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
 
        /* calculate stackframe size for native function */
 
-       M_ASUB_IMM(cd->stackframesize * 8, REG_SP);
-
-       /* get function address (this must happen before the stackframeinfo) */
-
-       funcdisp = dseg_add_functionptr(cd, f);
-
-       if (f == NULL)
-               patcher_add_patch_ref(jd, PATCHER_resolve_native_function, m, funcdisp);
+       M_ASUB_IMM(cd->stackframesize * 8 + 4, REG_SP);
 
        /* Mark the whole fpu stack as free for native functions (only for saved  */
        /* register count == 0).                                                  */
@@ -3598,7 +3639,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
                if (!md->params[i].inmemory)
                        assert(0);
 
-               s1 = md->params[i].regoff + cd->stackframesize * 8 + 4;
+               s1 = md->params[i].regoff + cd->stackframesize * 8 + 8;
                s2 = nmd->params[j].regoff;
 
                /* float/double in memory can be copied like int/longs */
@@ -3628,14 +3669,15 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
 
                /* put env into first argument */
 
-               M_AST_IMM(_Jv_env, REG_SP, 0 * 4);
+               M_AST_IMM(VM_get_jnienv(), REG_SP, 0 * 4);
        }
 
-       /* call the native function */
+       /* Call the native function. */
 
+       disp = dseg_add_functionptr(cd, f);
        emit_mov_imm_reg(cd, 0, REG_ITMP3);
        dseg_adddata(cd);
-       M_ALD(REG_ITMP1, REG_ITMP3, funcdisp);
+       M_ALD(REG_ITMP1, REG_ITMP3, disp);
        M_CALL(REG_ITMP1);
 
        /* save return value */
@@ -3643,6 +3685,20 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
        switch (md->returntype.type) {
        case TYPE_INT:
        case TYPE_ADR:
+               switch (md->returntype.primitivetype) {
+               case PRIMITIVETYPE_BOOLEAN:
+                       M_BZEXT(REG_RESULT, REG_RESULT);
+                       break;
+               case PRIMITIVETYPE_BYTE:
+                       M_BSEXT(REG_RESULT, REG_RESULT);
+                       break;
+               case PRIMITIVETYPE_CHAR:
+                       M_CZEXT(REG_RESULT, REG_RESULT);
+                       break;
+               case PRIMITIVETYPE_SHORT:
+                       M_SSEXT(REG_RESULT, REG_RESULT);
+                       break;
+               }
                M_IST(REG_RESULT, REG_SP, 1 * 8);
                break;
        case TYPE_LNG:
@@ -3700,7 +3756,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
                M_ALD(abi_registers_integer_saved[i], REG_SP, disp + i * 4);
 #endif
 
-       M_AADD_IMM(cd->stackframesize * 8, REG_SP);
+       M_AADD_IMM(cd->stackframesize * 8 + 4, REG_SP);
 
        /* check for exception */
 
@@ -3717,10 +3773,6 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
 
        M_MOV_IMM(asm_handle_nat_exception, REG_ITMP3);
        M_JMP(REG_ITMP3);
-
-       /* generate patcher stubs */
-
-       emit_patcher_traps(jd);
 }