Merged trunk and subtype.
[cacao.git] / src / vm / jit / x86_64 / codegen.c
index bfccf1ea86830e44214b4e3f38ca5d0df2316e43..dc42a9a966075f1b9cb7a297edadb8102c9785d2 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/x86_64/codegen.c - machine code generator for x86_64
 
-   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 "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/primitive.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/loader.hpp"
+#include "vm/options.h"
+#include "vm/primitive.hpp"
+#include "vm/statistics.h"
+#include "vm/string.hpp"
+#include "vm/vm.hpp"
 
 #include "vm/jit/abi.h"
 #include "vm/jit/asmpart.h"
-#include "vm/jit/code.h"
-#include "vm/jit/codegen-common.h"
+#include "vm/jit/code.hpp"
+#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/methodheader.h"
 #include "vm/jit/parse.h"
-#include "vm/jit/patcher-common.h"
+#include "vm/jit/patcher-common.hpp"
 #include "vm/jit/reg.h"
-#include "vm/jit/replace.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/replace.hpp"
+#include "vm/jit/stacktrace.hpp"
 #include "vm/jit/trap.h"
 
 #if defined(ENABLE_LSRA)
 # include "vm/jit/allocator/lsra.h"
 #endif
 
-#include "vmcore/loader.h"
-#include "vmcore/options.h"
-#include "vmcore/statistics.h"
-
 
 /* codegen_emit ****************************************************************
 
@@ -1889,8 +1885,8 @@ bool codegen_emit(jitdata *jd)
                                if (IS_IMM32(iptr->sx.s23.s2.constval))
                                        M_LST_IMM32(iptr->sx.s23.s2.constval, REG_ITMP1, 0);
                                else {
-                                       M_IST_IMM(iptr->sx.s23.s2.constval, REG_ITMP1, 0);
-                                       M_IST_IMM(iptr->sx.s23.s2.constval >> 32, REG_ITMP1, 4);
+                                       M_MOV_IMM(iptr->sx.s23.s2.constval, REG_ITMP2);
+                                       M_LST(REG_ITMP2, REG_ITMP1, 0);
                                }
                                break;
                        }
@@ -2012,9 +2008,12 @@ bool codegen_emit(jitdata *jd)
                        case TYPE_LNG:
                        case TYPE_ADR:
                        case TYPE_DBL:
-                               /* XXX why no check for IS_IMM32? */
-                               M_IST32_IMM(iptr->sx.s23.s2.constval, s1, disp);
-                               M_IST32_IMM(iptr->sx.s23.s2.constval >> 32, s1, disp + 4);
+                               /* XXX why no check for IS_IMM32? -- probably because of the patcher */
+                               M_MOV_IMM(iptr->sx.s23.s2.constval, REG_ITMP2);
+                               if (disp)  /* resolved, disp can never be 0 */
+                                       M_LST(REG_ITMP2, s1, disp);
+                               else       /* unresolved */
+                                       M_LST32(REG_ITMP2, s1, disp);
                                break;
                        }
                        break;
@@ -2570,13 +2569,7 @@ gen_method:
 
                                        if (super == NULL || super->vftbl->subtype_depth >= DISPLAY_SIZE) {
                                                M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_offset));
-
-                                               *(cd->mcodeptr++) = 0x4d;
-                                               *(cd->mcodeptr++) = 0x3b;
-                                               *(cd->mcodeptr++) = 0x1c;
-                                               *(cd->mcodeptr++) = 0x02;
-                                               /* cmp (ITMP2, ITMP1, 1), ITMP3 */
-
+                                               M_LCMP_MEMINDEX(REG_ITMP2, 0, REG_ITMP1, 0, REG_ITMP3);
                                                emit_label_beq(cd, BRANCH_LABEL_6);  /* good */
 
                                                if (super == NULL) {
@@ -2585,22 +2578,11 @@ gen_method:
                                                }
 
                                                M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_depth));
-                                               M_ISUB_IMM(DISPLAY_SIZE, REG_ITMP1);
-                                               *(cd->mcodeptr++) = 0x41;
-                                               *(cd->mcodeptr++) = 0x39;
-                                               *(cd->mcodeptr++) = 0x42;
-                                               *(cd->mcodeptr++) = OFFSET(vftbl_t, subtype_overflow_length);
-                                               /* cmpl ITMP1, subtype_overflow_length(ITMP2) */
-                                               emit_label_ble(cd, BRANCH_LABEL_9);  /* throw */
+                                               M_ICMP_MEMBASE(REG_ITMP2, OFFSET(vftbl_t, subtype_depth), REG_ITMP1);
+                                               emit_label_bgt(cd, BRANCH_LABEL_9);  /* throw */
 
                                                M_ALD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, subtype_overflow));
-
-                                               *(cd->mcodeptr++) = 0x4d;
-                                               *(cd->mcodeptr++) = 0x3b;
-                                               *(cd->mcodeptr++) = 0x1c;
-                                               *(cd->mcodeptr++) = 0xc2;
-                                               /* cmp (ITMP2, ITMP1, 8), ITMP3 */
-
+                                               M_LCMP_MEMINDEX(REG_ITMP2, -8*DISPLAY_SIZE, REG_ITMP1, 3, REG_ITMP3);
                                                emit_label_beq(cd, BRANCH_LABEL_7);  /* good */
 
                                                emit_label(cd, BRANCH_LABEL_9);
@@ -2617,13 +2599,7 @@ gen_method:
                                                emit_load_s1(jd, iptr, REG_ITMP1);
                                        }
                                        else {
-                                               assert(super->vftbl->subtype_offset < 0x80);
-                                               *(cd->mcodeptr++) = 0x4d;
-                                               *(cd->mcodeptr++) = 0x3b;
-                                               *(cd->mcodeptr++) = 0x5a;
-                                               *(cd->mcodeptr++) = super->vftbl->subtype_offset;
-                                               /* cmp off(ITMP2), ITMP3 */
-
+                                               M_LCMP_MEMBASE(REG_ITMP2, super->vftbl->subtype_offset, REG_ITMP3);
                                                emit_classcast_check(cd, iptr, BRANCH_NE, REG_ITMP3, s1);
                                        }
 
@@ -2770,13 +2746,7 @@ gen_method:
 
                                if (super == NULL || super->vftbl->subtype_depth >= DISPLAY_SIZE) {
                                        M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_offset));
-
-                                       *(cd->mcodeptr++) = 0x4d;
-                                       *(cd->mcodeptr++) = 0x3b;
-                                       *(cd->mcodeptr++) = 0x1c;
-                                       *(cd->mcodeptr++) = 0x02;
-                                       /* cmp (ITMP2, ITMP1, 1), ITMP3 */
-
+                                       M_LCMP_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);
@@ -2792,22 +2762,11 @@ gen_method:
                                        }
 
                                        M_ILD(REG_ITMP1, REG_ITMP3, OFFSET(vftbl_t, subtype_depth));
-                                       M_ISUB_IMM(DISPLAY_SIZE, REG_ITMP1);
-                                       *(cd->mcodeptr++) = 0x41;
-                                       *(cd->mcodeptr++) = 0x39;
-                                       *(cd->mcodeptr++) = 0x42;
-                                       *(cd->mcodeptr++) = OFFSET(vftbl_t, subtype_overflow_length);
-                                       /* cmpl ITMP1, subtype_overflow_length(ITMP2) */
-                                       emit_label_ble(cd, BRANCH_LABEL_9);  /* false */
+                                       M_ICMP_MEMBASE(REG_ITMP2, OFFSET(vftbl_t, subtype_depth), REG_ITMP1);
+                                       emit_label_bgt(cd, BRANCH_LABEL_9);  /* false */
 
                                        M_ALD(REG_ITMP2, REG_ITMP2, OFFSET(vftbl_t, subtype_overflow));
-
-                                       *(cd->mcodeptr++) = 0x4d;
-                                       *(cd->mcodeptr++) = 0x3b;
-                                       *(cd->mcodeptr++) = 0x1c;
-                                       *(cd->mcodeptr++) = 0xc2;
-                                       /* cmp (ITMP2, ITMP1, 8), ITMP3 */
-
+                                       M_LCMP_MEMINDEX(REG_ITMP2, -8*DISPLAY_SIZE, REG_ITMP1, 3, REG_ITMP3);
                                        M_SETE(d);
                                        if (d == REG_ITMP2) {
                                                M_BSEXT(d, d);
@@ -2826,13 +2785,7 @@ gen_method:
                                        emit_label(cd, BRANCH_LABEL_6);
                                }
                                else {
-                                       assert(super->vftbl->subtype_offset < 0x80);
-                                       *(cd->mcodeptr++) = 0x4d;
-                                       *(cd->mcodeptr++) = 0x3b;
-                                       *(cd->mcodeptr++) = 0x5a;
-                                       *(cd->mcodeptr++) = super->vftbl->subtype_offset;
-                                       /* cmp off(ITMP2), ITMP3 */
-
+                                       M_LCMP_MEMBASE(REG_ITMP2, super->vftbl->subtype_offset, REG_ITMP3);
                                        M_SETE(d);
                                        if (d == REG_ITMP2)
                                                M_BSEXT(d, d);
@@ -3121,7 +3074,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
 
                /* put env into first argument register */
 
-               M_MOV_IMM(_Jv_env, REG_A0);
+               M_MOV_IMM(VM_get_jnienv(), REG_A0);
        }
 
        /* Call the native function. */
@@ -3136,7 +3089,7 @@ void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int s
        case TYPE_INT:
        case TYPE_LNG:
        case TYPE_ADR:
-               switch (md->returntype.decltype) {
+               switch (md->returntype.primitivetype) {
                case PRIMITIVETYPE_BOOLEAN:
                        M_BZEXT(REG_RESULT, REG_RESULT);
                        break;