* src/vm/jit/x86_64/emit.c (emit_trap_compiler): Fixed comment.
[cacao.git] / src / vm / jit / x86_64 / emit.c
index d59278a0145440a8173861094ae9f1b21efe9f7d..d0f940a16124e8a2f4ba84ae2474f1ab1dd7e8ea 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: emit.c 7723 2007-04-16 18:03:08Z michi $
-
 */
 
 #include "config.h"
@@ -39,9 +37,7 @@
 
 #include "mm/memory.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/lock.h"
-#endif
+#include "threads/lock-common.h"
 
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
@@ -52,6 +48,7 @@
 #include "vm/jit/codegen-common.h"
 #include "vm/jit/emit-common.h"
 #include "vm/jit/jit.h"
+#include "vm/jit/patcher-common.h"
 #include "vm/jit/replace.h"
 
 #include "vmcore/options.h"
@@ -76,7 +73,7 @@ s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
        if (IS_INMEMORY(src->flags)) {
                COUNT_SPILLS;
 
-               disp = src->vv.regoff * 8;
+               disp = src->vv.regoff;
 
                switch (src->type) {
                case TYPE_INT:
@@ -153,7 +150,7 @@ inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
        if (IS_INMEMORY(dst->flags)) {
                COUNT_SPILLS;
 
-               disp = dst->vv.regoff * 8;
+               disp = dst->vv.regoff;
 
                switch (dst->type) {
                case TYPE_INT:
@@ -180,29 +177,41 @@ inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
 
 *******************************************************************************/
 
-void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
+void emit_copy(jitdata *jd, instruction *iptr)
 {
-       codegendata  *cd;
-       s4            s1, d;
+       codegendata *cd;
+       varinfo     *src;
+       varinfo     *dst;
+       s4           s1, d;
 
        /* get required compiler data */
 
        cd = jd->cd;
 
+       /* get source and destination variables */
+
+       src = VAROP(iptr->s1);
+       dst = VAROP(iptr->dst);
+
        if ((src->vv.regoff != dst->vv.regoff) ||
                ((src->flags ^ dst->flags) & INMEMORY)) {
 
+               if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
+                       /* emit nothing, as the value won't be used anyway */
+                       return;
+               }
+
                /* If one of the variables resides in memory, we can eliminate
                   the register move from/to the temporary register with the
                   order of getting the destination register and the load. */
 
                if (IS_INMEMORY(src->flags)) {
-                       d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
+                       d  = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
                        s1 = emit_load(jd, iptr, src, d);
                }
                else {
                        s1 = emit_load(jd, iptr, src, REG_IFTMP);
-                       d = codegen_reg_of_var(iptr->opc, dst, s1);
+                       d  = codegen_reg_of_var(iptr->opc, dst, s1);
                }
 
                if (s1 != d) {
@@ -343,7 +352,7 @@ 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_ICMP(REG_ITMP3, s2);
                M_BULT(8);
                M_ALD_MEM(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
@@ -351,6 +360,22 @@ void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1,
 }
 
 
+/* 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(8);
+               M_ALD_MEM(REG_RESULT, EXCEPTION_HARDWARE_ARRAYSTORE);
+       }
+}
+
+
 /* emit_classcast_check ********************************************************
 
    Emit a ClassCastException check.
@@ -410,152 +435,40 @@ void emit_exception_check(codegendata *cd, instruction *iptr)
 }
 
 
-/* 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 size of code segment */
-
-               MCODECHECK(512);
-
-               /* Get machine code which is patched back in later. A
-                  `call rel32' is 5 bytes long (but read 8 bytes). */
-
-               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       */
-
-               /* move pointer to java_objectheader onto stack */
-
-#if defined(ENABLE_THREADS)
-               /* create a virtual java_objectheader */
-
-               (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   */
-
-               emit_lea_membase_reg(cd, RIP, -((cd->mcodeptr + 7) - cd->mcodebase) + disp, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-#else
-               M_PUSH_IMM(0);
-#endif
-
-               /* move machine code bytes and classinfo pointer into registers */
-
-               M_MOV_IMM(mcode, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-
-               M_MOV_IMM(pref->ref, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-
-               M_MOV_IMM(pref->disp, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-
-               M_MOV_IMM(pref->patcher, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-
-               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));
-               }
-       }
+       M_ALD_MEM(REG_METHODPTR, EXCEPTION_HARDWARE_COMPILER);
 }
 
 
-/* emit_replacement_stubs ******************************************************
+/* emit_trap *******************************************************************
 
-   Generates the code for the replacement stubs.
+   Emit a trap instruction and return the original machine code.
 
 *******************************************************************************/
 
-#if defined(ENABLE_REPLACEMENT)
-void emit_replacement_stubs(jitdata *jd)
+uint32_t emit_trap(codegendata *cd)
 {
-       codegendata *cd;
-       codeinfo    *code;
-       rplpoint    *rplp;
-       s4           i;
-#if !defined(NDEBUG)
-       u1          *savedmcodeptr;
-#endif
-
-       /* get required compiler data */
-
-       cd   = jd->cd;
-       code = jd->code;
+       uint16_t mcode;
 
-       rplp = code->rplpoints;
+       /* Get machine code which is patched back in later. The trap is 2
+          bytes long. */
 
-       /* store beginning of replacement stubs */
+       mcode = *((uint16_t *) cd->mcodeptr);
 
-       code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
+       /* XXX This needs to be change to INT3 when the debugging problems
+          with gdb are resolved. */
 
-       for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
-               /* do not generate stubs for non-trappable points */
+       M_UD2;
 
-               if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
-                       continue;
-
-               /* check code segment size */
-
-               MCODECHECK(512);
-
-               /* note start of stub code */
-
-#if !defined(NDEBUG)
-               savedmcodeptr = cd->mcodeptr;
-#endif
-
-               /* push address of `rplpoint` struct */
-                       
-               M_MOV_IMM(rplp, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-
-               /* jump to replacement function */
-
-               M_MOV_IMM(asm_replacement_out, REG_ITMP3);
-               M_PUSH(REG_ITMP3);
-               M_RET;
-
-               assert((cd->mcodeptr - savedmcodeptr) == REPLACEMENT_STUB_SIZE);
-       }
+       return mcode;
 }
-#endif /* defined(ENABLE_REPLACEMENT) */
 
 
 /* emit_verbosecall_enter ******************************************************
@@ -805,53 +718,53 @@ void emit_ishift(jitdata *jd, s4 shift_op, instruction *iptr)
        if (IS_INMEMORY(v_dst->flags)) {
                if (IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
                        if (s1 == d) {
-                               M_ILD(RCX, REG_SP, s2 * 8);
-                               emit_shiftl_membase(cd, shift_op, REG_SP, d * 8);
+                               M_ILD(RCX, REG_SP, s2);
+                               emit_shiftl_membase(cd, shift_op, REG_SP, d);
 
                        } else {
-                               M_ILD(RCX, REG_SP, s2 * 8);
-                               M_ILD(REG_ITMP2, REG_SP, s1 * 8);
+                               M_ILD(RCX, REG_SP, s2);
+                               M_ILD(REG_ITMP2, REG_SP, s1);
                                emit_shiftl_reg(cd, shift_op, REG_ITMP2);
-                               M_IST(REG_ITMP2, REG_SP, d * 8);
+                               M_IST(REG_ITMP2, REG_SP, d);
                        }
 
                } else if (IS_INMEMORY(v_s2->flags) && !IS_INMEMORY(v_s1->flags)) {
                        /* s1 may be equal to RCX */
                        if (s1 == RCX) {
                                if (s2 == d) {
-                                       M_ILD(REG_ITMP1, REG_SP, s2 * 8);
-                                       M_IST(s1, REG_SP, d * 8);
+                                       M_ILD(REG_ITMP1, REG_SP, s2);
+                                       M_IST(s1, REG_SP, d);
                                        M_INTMOVE(REG_ITMP1, RCX);
 
                                } else {
-                                       M_IST(s1, REG_SP, d * 8);
-                                       M_ILD(RCX, REG_SP, s2 * 8);
+                                       M_IST(s1, REG_SP, d);
+                                       M_ILD(RCX, REG_SP, s2);
                                }
 
                        } else {
-                               M_ILD(RCX, REG_SP, s2 * 8);
-                               M_IST(s1, REG_SP, d * 8);
+                               M_ILD(RCX, REG_SP, s2);
+                               M_IST(s1, REG_SP, d);
                        }
 
-                       emit_shiftl_membase(cd, shift_op, REG_SP, d * 8);
+                       emit_shiftl_membase(cd, shift_op, REG_SP, d);
 
                } else if (!IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
                        if (s1 == d) {
                                M_INTMOVE(s2, RCX);
-                               emit_shiftl_membase(cd, shift_op, REG_SP, d * 8);
+                               emit_shiftl_membase(cd, shift_op, REG_SP, d);
 
                        } else {
                                M_INTMOVE(s2, RCX);
-                               M_ILD(REG_ITMP2, REG_SP, s1 * 8);
+                               M_ILD(REG_ITMP2, REG_SP, s1);
                                emit_shiftl_reg(cd, shift_op, REG_ITMP2);
-                               M_IST(REG_ITMP2, REG_SP, d * 8);
+                               M_IST(REG_ITMP2, REG_SP, d);
                        }
 
                } else {
                        /* s1 may be equal to RCX */
-                       M_IST(s1, REG_SP, d * 8);
+                       M_IST(s1, REG_SP, d);
                        M_INTMOVE(s2, RCX);
-                       emit_shiftl_membase(cd, shift_op, REG_SP, d * 8);
+                       emit_shiftl_membase(cd, shift_op, REG_SP, d);
                }
 
                M_INTMOVE(REG_ITMP1, RCX);                             /* restore RCX */
@@ -863,19 +776,19 @@ void emit_ishift(jitdata *jd, s4 shift_op, instruction *iptr)
                }
                                        
                if (IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
-                       M_ILD(RCX, REG_SP, s2 * 8);
-                       M_ILD(d, REG_SP, s1 * 8);
+                       M_ILD(RCX, REG_SP, s2);
+                       M_ILD(d, REG_SP, s1);
                        emit_shiftl_reg(cd, shift_op, d);
 
                } else if (IS_INMEMORY(v_s2->flags) && !IS_INMEMORY(v_s1->flags)) {
                        /* s1 may be equal to RCX */
                        M_INTMOVE(s1, d);
-                       M_ILD(RCX, REG_SP, s2 * 8);
+                       M_ILD(RCX, REG_SP, s2);
                        emit_shiftl_reg(cd, shift_op, d);
 
                } else if (!IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
                        M_INTMOVE(s2, RCX);
-                       M_ILD(d, REG_SP, s1 * 8);
+                       M_ILD(d, REG_SP, s1);
                        emit_shiftl_reg(cd, shift_op, d);
 
                } else {
@@ -932,53 +845,53 @@ void emit_lshift(jitdata *jd, s4 shift_op, instruction *iptr)
        if (IS_INMEMORY(v_dst->flags)) {
                if (IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
                        if (s1 == d) {
-                               M_ILD(RCX, REG_SP, s2 * 8);
-                               emit_shift_membase(cd, shift_op, REG_SP, d * 8);
+                               M_ILD(RCX, REG_SP, s2);
+                               emit_shift_membase(cd, shift_op, REG_SP, d);
 
                        } else {
-                               M_ILD(RCX, REG_SP, s2 * 8);
-                               M_LLD(REG_ITMP2, REG_SP, s1 * 8);
+                               M_ILD(RCX, REG_SP, s2);
+                               M_LLD(REG_ITMP2, REG_SP, s1);
                                emit_shift_reg(cd, shift_op, REG_ITMP2);
-                               M_LST(REG_ITMP2, REG_SP, d * 8);
+                               M_LST(REG_ITMP2, REG_SP, d);
                        }
 
                } else if (IS_INMEMORY(v_s2->flags) && !IS_INMEMORY(v_s1->flags)) {
                        /* s1 may be equal to RCX */
                        if (s1 == RCX) {
                                if (s2 == d) {
-                                       M_ILD(REG_ITMP1, REG_SP, s2 * 8);
-                                       M_LST(s1, REG_SP, d * 8);
+                                       M_ILD(REG_ITMP1, REG_SP, s2);
+                                       M_LST(s1, REG_SP, d);
                                        M_INTMOVE(REG_ITMP1, RCX);
 
                                } else {
-                                       M_LST(s1, REG_SP, d * 8);
-                                       M_ILD(RCX, REG_SP, s2 * 8);
+                                       M_LST(s1, REG_SP, d);
+                                       M_ILD(RCX, REG_SP, s2);
                                }
 
                        } else {
-                               M_ILD(RCX, REG_SP, s2 * 8);
-                               M_LST(s1, REG_SP, d * 8);
+                               M_ILD(RCX, REG_SP, s2);
+                               M_LST(s1, REG_SP, d);
                        }
 
-                       emit_shift_membase(cd, shift_op, REG_SP, d * 8);
+                       emit_shift_membase(cd, shift_op, REG_SP, d);
 
                } else if (!IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
                        if (s1 == d) {
                                M_INTMOVE(s2, RCX);
-                               emit_shift_membase(cd, shift_op, REG_SP, d * 8);
+                               emit_shift_membase(cd, shift_op, REG_SP, d);
 
                        } else {
                                M_INTMOVE(s2, RCX);
-                               M_LLD(REG_ITMP2, REG_SP, s1 * 8);
+                               M_LLD(REG_ITMP2, REG_SP, s1);
                                emit_shift_reg(cd, shift_op, REG_ITMP2);
-                               M_LST(REG_ITMP2, REG_SP, d * 8);
+                               M_LST(REG_ITMP2, REG_SP, d);
                        }
 
                } else {
                        /* s1 may be equal to RCX */
-                       M_LST(s1, REG_SP, d * 8);
+                       M_LST(s1, REG_SP, d);
                        M_INTMOVE(s2, RCX);
-                       emit_shift_membase(cd, shift_op, REG_SP, d * 8);
+                       emit_shift_membase(cd, shift_op, REG_SP, d);
                }
 
                M_INTMOVE(REG_ITMP1, RCX);                             /* restore RCX */
@@ -990,19 +903,19 @@ void emit_lshift(jitdata *jd, s4 shift_op, instruction *iptr)
                }
 
                if (IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
-                       M_ILD(RCX, REG_SP, s2 * 8);
-                       M_LLD(d, REG_SP, s1 * 8);
+                       M_ILD(RCX, REG_SP, s2);
+                       M_LLD(d, REG_SP, s1);
                        emit_shift_reg(cd, shift_op, d);
 
                } else if (IS_INMEMORY(v_s2->flags) && !IS_INMEMORY(v_s1->flags)) {
                        /* s1 may be equal to RCX */
                        M_INTMOVE(s1, d);
-                       M_ILD(RCX, REG_SP, s2 * 8);
+                       M_ILD(RCX, REG_SP, s2);
                        emit_shift_reg(cd, shift_op, d);
 
                } else if (!IS_INMEMORY(v_s2->flags) && IS_INMEMORY(v_s1->flags)) {
                        M_INTMOVE(s2, RCX);
-                       M_LLD(d, REG_SP, s1 * 8);
+                       M_LLD(d, REG_SP, s1);
                        emit_shift_reg(cd, shift_op, d);
 
                } else {
@@ -1644,12 +1557,6 @@ void emit_idivl_reg(codegendata *cd, s8 reg) {
 
 
 
-void emit_ret(codegendata *cd) {
-    *(cd->mcodeptr++) = 0xc3;
-}
-
-
-
 /*
  * shift ops
  */
@@ -1845,11 +1752,6 @@ void emit_xchg_reg_reg(codegendata *cd, s8 reg, s8 dreg) {
 }
 
 
-void emit_nop(codegendata *cd) {
-    *(cd->mcodeptr++) = 0x90;
-}
-
-
 
 /*
  * call instructions