Merged trunk and subtype.
[cacao.git] / src / vm / jit / i386 / emit.c
index 9460875e44a051617d3129dfc383b8d865b41dd2..66fba15ffcc36d30922e8c46422529126a58388c 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/i386/emit.c - i386 code emitter functions
 
-   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.
 
@@ -22,8 +20,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: emit.c 8267 2007-08-07 11:07:48Z twisti $
-
 */
 
 
 
 #include "threads/lock-common.h"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/options.h"
+#include "vm/statistics.h"
 
 #include "vm/jit/abi.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/dseg.h"
-#include "vm/jit/emit-common.h"
-#include "vm/jit/jit.h"
-#include "vm/jit/replace.h"
-
-#include "vmcore/options.h"
-#include "vmcore/statistics.h"
+#include "vm/jit/emit-common.hpp"
+#include "vm/jit/jit.hpp"
+#include "vm/jit/patcher-common.hpp"
+#include "vm/jit/replace.hpp"
+#include "vm/jit/trace.hpp"
+#include "vm/jit/trap.h"
 
 
 /* emit_load ******************************************************************
@@ -411,7 +407,7 @@ void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
        if (INSTRUCTION_MUST_CHECK(iptr)) {
                M_TEST(reg);
                M_BNE(6);
-               M_ALD_MEM(reg, EXCEPTION_HARDWARE_ARITHMETIC);
+               M_ALD_MEM(reg, TRAP_ArithmeticException);
        }
 }
 
@@ -425,10 +421,26 @@ void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
 {
        if (INSTRUCTION_MUST_CHECK(iptr)) {
-        M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
+        M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
         M_CMP(REG_ITMP3, s2);
         M_BB(6);
-               M_ALD_MEM(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
+               M_ALD_MEM(s2, TRAP_ArrayIndexOutOfBoundsException);
+       }
+}
+
+
+/* emit_arraystore_check *******************************************************
+
+   Emit an ArrayStoreException check.
+
+*******************************************************************************/
+
+void emit_arraystore_check(codegendata *cd, instruction *iptr)
+{
+       if (INSTRUCTION_MUST_CHECK(iptr)) {
+               M_TEST(REG_RESULT);
+               M_BNE(6);
+               M_ALD_MEM(REG_RESULT, TRAP_ArrayStoreException);
        }
 }
 
@@ -446,16 +458,22 @@ void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 r
                case BRANCH_LE:
                        M_BGT(6);
                        break;
+               case BRANCH_GE:
+                       M_BLT(6);
+                       break;
                case BRANCH_EQ:
                        M_BNE(6);
                        break;
+               case BRANCH_NE:
+                       M_BEQ(6);
+                       break;
                case BRANCH_ULE:
                        M_BBE(6);
                        break;
                default:
                        vm_abort("emit_classcast_check: unknown condition %d", condition);
                }
-               M_ALD_MEM(s1, EXCEPTION_HARDWARE_CLASSCAST);
+               M_ALD_MEM(s1, TRAP_ClassCastException);
        }
 }
 
@@ -471,7 +489,7 @@ void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
        if (INSTRUCTION_MUST_CHECK(iptr)) {
                M_TEST(reg);
                M_BNE(6);
-               M_ALD_MEM(reg, EXCEPTION_HARDWARE_NULLPOINTER);
+               M_ALD_MEM(reg, TRAP_NullPointerException);
        }
 }
 
@@ -487,95 +505,37 @@ void emit_exception_check(codegendata *cd, instruction *iptr)
        if (INSTRUCTION_MUST_CHECK(iptr)) {
                M_TEST(REG_RESULT);
                M_BNE(6);
-               M_ALD_MEM(REG_RESULT, EXCEPTION_HARDWARE_EXCEPTION);
+               M_ALD_MEM(REG_RESULT, TRAP_CHECK_EXCEPTION);
        }
 }
 
 
-/* emit_patcher_stubs **********************************************************
+/* emit_trap_compiler **********************************************************
 
-   Generates the code for the patcher stubs.
+   Emit a trap instruction which calls the JIT compiler.
 
 *******************************************************************************/
 
-void emit_patcher_stubs(jitdata *jd)
+void emit_trap_compiler(codegendata *cd)
 {
-       codegendata *cd;
-       patchref    *pref;
-       u8           mcode;
-       u1          *savedmcodeptr;
-       u1          *tmpmcodeptr;
-       s4           targetdisp;
-       s4           disp;
-
-       /* get required compiler data */
-
-       cd = jd->cd;
-
-       /* generate code patching stub call code */
-
-       targetdisp = 0;
-
-       for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
-               /* check code segment size */
-
-               MCODECHECK(512);
-
-               /* Get machine code which is patched back in later. A
-                  `call rel32' is 5 bytes long. */
-
-               savedmcodeptr = cd->mcodebase + pref->branchpos;
-               mcode = *((u8 *) savedmcodeptr);
-
-               /* patch in `call rel32' to call the following code */
-
-               tmpmcodeptr  = cd->mcodeptr;    /* save current mcodeptr              */
-               cd->mcodeptr = savedmcodeptr;   /* set mcodeptr to patch position     */
-
-               M_CALL_IMM(tmpmcodeptr - (savedmcodeptr + PATCHER_CALL_SIZE));
-
-               cd->mcodeptr = tmpmcodeptr;     /* restore the current mcodeptr       */
-
-               /* save REG_ITMP3 */
-
-               M_PUSH(REG_ITMP3);
-
-               /* move pointer to java_objectheader onto stack */
-
-#if defined(ENABLE_THREADS)
-               (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
-               (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
-               disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
+       M_ALD_MEM(REG_METHODPTR, TRAP_COMPILER);
+}
 
-               M_MOV_IMM(0, REG_ITMP3);
-               dseg_adddata(cd);
-               M_AADD_IMM(disp, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-#else
-               M_PUSH_IMM(0);
-#endif
+/* emit_trap_countdown *********************************************************
 
-               /* move machine code bytes and classinfo pointer into registers */
+   Emit a countdown trap.
 
-               M_PUSH_IMM(mcode >> 32);
-               M_PUSH_IMM(mcode);
-               M_PUSH_IMM(pref->ref);
-               M_PUSH_IMM(pref->patcher);
+   counter....absolute address of the counter variable
 
-               if (targetdisp == 0) {
-                       targetdisp = cd->mcodeptr - cd->mcodebase;
+*******************************************************************************/
 
-                       M_MOV_IMM(asm_patcher_wrapper, REG_ITMP3);
-                       M_JMP(REG_ITMP3);
-               }
-               else {
-                       M_JMP_IMM((cd->mcodebase + targetdisp) -
-                                         (cd->mcodeptr + PATCHER_CALL_SIZE));
-               }
-       }
+void emit_trap_countdown(codegendata *cd, s4 *counter)
+{
+       M_ISUB_IMM_MEMABS(1, (s4) counter);
+       M_BNS(6);
+       M_ALD_MEM(REG_METHODPTR, TRAP_COUNTDOWN);
 }
 
-
 /* emit_trap *******************************************************************
 
    Emit a trap instruction and return the original machine code.
@@ -584,16 +544,22 @@ void emit_patcher_stubs(jitdata *jd)
 
 uint32_t emit_trap(codegendata *cd)
 {
-       uint32_t mcode;
+       uint16_t mcode;
 
        /* Get machine code which is patched back in later. The
-          trap is 1 instruction word long. */
+          trap is 2 bytes long. */
 
-       mcode = *((uint32_t *) cd->mcodeptr);
+       mcode = *((uint16_t *) cd->mcodeptr);
 
-       M_NOP;
+#if 0
+       /* XXX this breaks GDB, so we disable it for now */
+       *(cd->mcodeptr++) = 0xcc;
+       M_INT3;
+#else
+       M_UD2;
+#endif
 
-       return mcode;
+       return (uint32_t) mcode;
 }
 
 
@@ -607,21 +573,23 @@ uint32_t emit_trap(codegendata *cd)
 void emit_verbosecall_enter(jitdata *jd)
 {
        methodinfo   *m;
+       codeinfo     *code;
        codegendata  *cd;
        registerdata *rd;
        methoddesc   *md;
-       int32_t       disp;
+       int32_t       stackframesize;
        int           i;
-       int           d;
+       int           align_off;             /* offset for alignment compensation */
 
        if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
                return;
 
        /* get required compiler data */
 
-       m  = jd->m;
-       cd = jd->cd;
-       rd = jd->rd;
+       m    = jd->m;
+       code = jd->code;
+       cd   = jd->cd;
+       rd   = jd->rd;
 
        md = m->parseddesc;
 
@@ -629,62 +597,40 @@ void emit_verbosecall_enter(jitdata *jd)
 
        M_NOP;
 
-       /* methodinfo* + arguments + return address */
+       /* keep stack 16-byte aligned */
 
-       disp = (TRACE_ARGS_NUM + 1 + TMP_CNT) * 8 + cd->stackframesize * 8 + 4;
+       stackframesize = 2 + TMP_CNT;
+       ALIGN_2(stackframesize);
 
-       M_ASUB_IMM((TRACE_ARGS_NUM + 1 + TMP_CNT) * 8, REG_SP);
+       M_ASUB_IMM(stackframesize * 8, REG_SP);
 
        /* save temporary registers for leaf methods */
 
-       for (i = 0; i < INT_TMP_CNT; i++)
-               M_IST(rd->tmpintregs[i], REG_SP, (TRACE_ARGS_NUM + 1 + i) * 8);
-
-       /* save argument registers */
-
-       for (i = 0; i < md->paramcount; i++) {
-               d = i * 8;
-
-               switch (md->paramtypes[i].type) {
-               case TYPE_INT:
-                       M_ILD(EAX, REG_SP, disp);
-                       emit_cltd(cd);
-                       M_LST(EAX_EDX_PACKED, REG_SP, d);
-                       break;
-               case TYPE_LNG:
-                       M_LLD(REG_ITMP12_PACKED, REG_SP, disp);
-                       M_LST(REG_ITMP12_PACKED, REG_SP, d);
-                       break;
-               case TYPE_ADR:
-                       M_ALD(REG_ITMP1, REG_SP, disp);
-                       M_AST(REG_ITMP1, REG_SP, d);
-                       M_IST_IMM(0, REG_SP, d + 4);                /* high-bits are zero */
-                       break;
-               case TYPE_FLT:
-                       M_FLD(REG_NULL, REG_SP, disp);
-                       M_FST(REG_NULL, REG_SP, d);
-                       M_IST_IMM(0, REG_SP, d + 4);                /* high-bits are zero */
-                       break;
-               case TYPE_DBL:
-                       M_DLD(REG_NULL, REG_SP, disp);
-                       M_DST(REG_NULL, REG_SP, d);
-                       break;
-               }
-
-               disp += 8;
+       if (code_is_leafmethod(code)) {
+               for (i = 0; i < INT_TMP_CNT; i++)
+                       M_IST(rd->tmpintregs[i], REG_SP, (2 + i) * 8);
        }
 
-       M_AST_IMM(m, REG_SP, TRACE_ARGS_NUM * 8);
+       /* no argument registers to save */
 
-       M_MOV_IMM(builtin_verbosecall_enter, REG_ITMP1);
+       align_off = cd->stackframesize ? 4 : 0;
+       M_AST_IMM(m, REG_SP, 0 * 4);
+       M_AST_IMM(0, REG_SP, 1 * 4);
+       M_AST(REG_SP, REG_SP, 2 * 4);
+       M_IADD_IMM_MEMBASE(stackframesize * 8 + cd->stackframesize * 8 + 4 + align_off, REG_SP, 2 * 4);
+       M_MOV_IMM(trace_java_call_enter, REG_ITMP1);
        M_CALL(REG_ITMP1);
 
+       /* no argument registers to restore */
+
        /* restore temporary registers for leaf methods */
 
-       for (i = 0; i < INT_TMP_CNT; i++)
-               M_ILD(rd->tmpintregs[i], REG_SP, (TRACE_ARGS_NUM + 1 + i) * 8);
+       if (code_is_leafmethod(code)) {
+               for (i = 0; i < INT_TMP_CNT; i++)
+                       M_ILD(rd->tmpintregs[i], REG_SP, (2 + i) * 8);
+       }
 
-       M_AADD_IMM((TRACE_ARGS_NUM + 1 + TMP_CNT) * 8, REG_SP);
+       M_AADD_IMM(stackframesize * 8, REG_SP);
 
        /* mark trace code */
 
@@ -697,8 +643,6 @@ void emit_verbosecall_enter(jitdata *jd)
 
    Generates the code for the call trace.
 
-   void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
-
 *******************************************************************************/
 
 #if !defined(NDEBUG)
@@ -707,6 +651,7 @@ void emit_verbosecall_exit(jitdata *jd)
        methodinfo   *m;
        codegendata  *cd;
        registerdata *rd;
+       methoddesc   *md;
 
        if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
                return;
@@ -717,25 +662,53 @@ void emit_verbosecall_exit(jitdata *jd)
        cd = jd->cd;
        rd = jd->rd;
 
+       md = m->parseddesc;
+
        /* mark trace code */
 
        M_NOP;
 
-       M_ASUB_IMM(8 + 8 + 4 + 4 + 8, REG_SP);  /* +8: keep stack 16-byte aligned */
-
-       M_LST(REG_RESULT_PACKED, REG_SP, 0 * 8);
-
-       M_DSTNP(REG_NULL, REG_SP, 1 * 8);
-       M_FSTNP(REG_NULL, REG_SP, 2 * 8);
-
-       M_AST_IMM(m, REG_SP, 2 * 8 + 1 * 4);
+       /* keep stack 16-byte aligned */
+
+       M_ASUB_IMM(4 + 4 + 8, REG_SP);
+
+       /* save return value */
+
+       switch (md->returntype.type) {
+       case TYPE_ADR:
+       case TYPE_INT:
+               M_IST(REG_RESULT, REG_SP, 2 * 4);
+               break;
+       case TYPE_LNG:
+               M_LST(REG_RESULT_PACKED, REG_SP, 2 * 4);
+               break;
+       case TYPE_FLT:
+               M_FSTNP(REG_NULL, REG_SP, 2 * 4);
+               break;
+       case TYPE_DBL:
+               M_DSTNP(REG_NULL, REG_SP, 2 * 4);
+               break;
+       }
 
-       M_MOV_IMM(builtin_verbosecall_exit, REG_ITMP1);
+       M_AST_IMM(m, REG_SP, 0 * 4);
+       M_AST(REG_SP, REG_SP, 1 * 4);
+       M_IADD_IMM_MEMBASE(2 * 4, REG_SP, 1 * 4);
+       M_MOV_IMM(trace_java_call_exit, REG_ITMP1);
        M_CALL(REG_ITMP1);
 
-       M_LLD(REG_RESULT_PACKED, REG_SP, 0 * 4);
+       /* restore return value */
+
+       switch (md->returntype.type) {
+       case TYPE_ADR:
+       case TYPE_INT:
+               M_ILD(REG_RESULT, REG_SP, 2 * 4);
+               break;
+       case TYPE_LNG:
+               M_LLD(REG_RESULT_PACKED, REG_SP, 2 * 4);
+               break;
+       }
 
-       M_AADD_IMM(8 + 8 + 4 + 4 + 8, REG_SP);
+       M_AADD_IMM(4 + 4 + 8, REG_SP);
 
        /* mark trace code */
 
@@ -958,6 +931,15 @@ void emit_movb_imm_membase(codegendata *cd, s4 imm, s4 basereg, s4 disp)
 }
 
 
+void emit_movsbl_reg_reg(codegendata *cd, s4 a, s4 b)
+{
+       assert(a < 4);                     /* Can only operate on al, bl, cl, dl. */
+       *(cd->mcodeptr++) = 0x0f;
+       *(cd->mcodeptr++) = 0xbe;
+       emit_reg((b),(a));
+}
+
+
 void emit_movsbl_memindex_reg(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale, s4 reg)
 {
        COUNT(count_mov_mem_reg);
@@ -984,6 +966,15 @@ void emit_movswl_memindex_reg(codegendata *cd, s4 disp, s4 basereg, s4 indexreg,
 }
 
 
+void emit_movzbl_reg_reg(codegendata *cd, s4 a, s4 b)
+{
+       assert(a < 4);                     /* Can only operate on al, bl, cl, dl. */
+       *(cd->mcodeptr++) = 0x0f;
+       *(cd->mcodeptr++) = 0xb6;
+       emit_reg((b),(a));
+}
+
+
 void emit_movzwl_reg_reg(codegendata *cd, s4 a, s4 b)
 {
        *(cd->mcodeptr++) = 0x0f;
@@ -1099,6 +1090,11 @@ void emit_alu_imm_memabs(codegendata *cd, s4 opc, s4 imm, s4 disp)
        }
 }
 
+void emit_alu_memindex_reg(codegendata *cd, s4 opc, s4 disp, s4 basereg, s4 indexreg, s4 scale, s4 reg)
+{
+       *(cd->mcodeptr++) = (((u1) (opc)) << 3) + 3;
+       emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
+}
 
 void emit_test_reg_reg(codegendata *cd, s4 reg, s4 dreg)
 {
@@ -1119,16 +1115,16 @@ void emit_test_imm_reg(codegendata *cd, s4 imm, s4 reg)
 /*
  * inc, dec operations
  */
-void emit_dec_mem(codegendata *cd, s4 mem)
+void emit_inc_reg(codegendata *cd, s4 reg)
 {
        *(cd->mcodeptr++) = 0xff;
-       emit_mem(1,(mem));
+       emit_reg(0,(reg));
 }
 
-
-void emit_cltd(codegendata *cd)
+void emit_dec_mem(codegendata *cd, s4 mem)
 {
-       *(cd->mcodeptr++) = 0x99;
+       *(cd->mcodeptr++) = 0xff;
+       emit_mem(1,(mem));
 }
 
 
@@ -1211,12 +1207,6 @@ void emit_idiv_reg(codegendata *cd, s4 reg)
 }
 
 
-void emit_ret(codegendata *cd)
-{
-       *(cd->mcodeptr++) = 0xc3;
-}
-
-
 
 /*
  * shift ops
@@ -1372,12 +1362,6 @@ void emit_push_reg(codegendata *cd, s4 reg)
 }
 
 
-void emit_nop(codegendata *cd)
-{
-       *(cd->mcodeptr++) = 0x90;
-}
-
-
 void emit_lock(codegendata *cd)
 {
        *(cd->mcodeptr++) = 0xf0;
@@ -1907,6 +1891,14 @@ void emit_fincstp(codegendata *cd)
        *(cd->mcodeptr++) = 0xf7;
 }
 
+#if defined(ENABLE_ESCAPE_CHECK)
+void emit_escape_check(codegendata *cd, s4 reg) {
+       M_PUSH(reg);
+       M_MOV_IMM(asm_escape_check, REG_ITMP3);
+       M_CALL(REG_ITMP3);
+       M_IADD_IMM(4, REG_SP);
+}
+#endif
 
 /*
  * These are local overrides for various environment variables in Emacs.