* src/vm/jit/codegen-common.cpp (codegen_emit): New generic version of the
[cacao.git] / src / vm / jit / alpha / emit.c
index b6883c8247820a8bbd688f1ca4438946eb2101db..dd5d345c711f3d09d498ff039dd49cec2102416a 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/alpha/emit.c - Alpha 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.
 
 
 #include "vm/jit/alpha/codegen.h"
 
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
-#include "threads/lock-common.h"
+#include "threads/lock.hpp"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/options.h"
 
 #include "vm/jit/abi.h"
 #include "vm/jit/abi-asm.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/patcher-common.h"
-#include "vm/jit/replace.h"
-
-#include "vmcore/options.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.hpp"
 
 
 /* emit_load *******************************************************************
@@ -237,6 +234,28 @@ void emit_lconst(codegendata *cd, s4 d, s8 value)
 }
 
 
+/**
+ * Emits code comparing one integer register to an immediate value.
+ */
+void emit_icmpeq_imm(codegendata* cd, int reg, int32_t value, int d)
+{
+       int32_t disp;
+
+       if ((value >= 0) && (value <= 255)) {
+               M_CMPEQ_IMM(reg, value, d);
+       } else {
+               assert(reg != REG_ITMP2);
+               if ((value >= -32768) && (value <= 32767)) {
+                       M_LDA(REG_ITMP2, REG_ZERO, value);
+               } else {
+                       disp = dseg_add_s4(cd, value);
+                       M_ILD(REG_ITMP2, REG_PV, disp);
+               }
+               M_CMPEQ(reg, REG_ITMP2, d);
+       }
+}
+
+
 /* emit_branch *****************************************************************
 
    Emits the code for conditional and unconditional branchs.
@@ -327,7 +346,7 @@ void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
                M_BNEZ(reg, 1);
                /* Destination register must not be REG_ZERO, because then no
                   SIGSEGV is thrown. */
-               M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
+               M_ALD_INTERN(reg, REG_ZERO, TRAP_ArithmeticException);
        }
 }
 
@@ -344,7 +363,7 @@ void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1,
                M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
                M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
                M_BNEZ(REG_ITMP3, 1);
-               M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
+               M_ALD_INTERN(s2, REG_ZERO, TRAP_ArrayIndexOutOfBoundsException);
        }
 }
 
@@ -361,7 +380,7 @@ void emit_arraystore_check(codegendata *cd, instruction *iptr)
                M_BNEZ(REG_RESULT, 1);
                /* Destination register must not be REG_ZERO, because then no
                   SIGSEGV is thrown. */
-               M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);
+               M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_ArrayStoreException);
        }
 }
 
@@ -385,7 +404,7 @@ void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 r
                default:
                        vm_abort("emit_classcast_check: unknown condition %d", condition);
                }
-               M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
+               M_ALD_INTERN(s1, REG_ZERO, TRAP_ClassCastException);
        }
 }
 
@@ -402,7 +421,7 @@ void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
                M_BNEZ(reg, 1);
                /* Destination register must not be REG_ZERO, because then no
                   SIGSEGV is thrown. */
-               M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
+               M_ALD_INTERN(reg, REG_ZERO, TRAP_NullPointerException);
        }
 }
 
@@ -419,11 +438,23 @@ void emit_exception_check(codegendata *cd, instruction *iptr)
                M_BNEZ(REG_RESULT, 1);
                /* Destination register must not be REG_ZERO, because then no
                   SIGSEGV is thrown. */
-               M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
+               M_ALD_INTERN(REG_RESULT, REG_ZERO, TRAP_CHECK_EXCEPTION);
        }
 }
 
 
+/* emit_trap_compiler **********************************************************
+
+   Emit a trap instruction which calls the JIT compiler.
+
+*******************************************************************************/
+
+void emit_trap_compiler(codegendata *cd)
+{
+       M_ALD_INTERN(REG_METHODPTR, REG_ZERO, TRAP_COMPILER);
+}
+
+
 /* emit_trap *******************************************************************
 
    Emit a trap instruction and return the original machine code.
@@ -437,16 +468,133 @@ uint32_t emit_trap(codegendata *cd)
        /* Get machine code which is patched back in later. The
           trap is 1 instruction word long. */
 
-       mcode = *((u*) cd->mcodeptr);
+       mcode = *((uint32_t*) cd->mcodeptr);
 
-       /* Destination register must not be REG_ZERO, because then no
-          SIGSEGV is thrown. */
-       M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
+       // Generate a SIGILL.
+       M_UNDEFINED;
 
        return mcode;
 }
 
 
+/**
+ * Emit code to recompute the procedure vector.
+ */
+void emit_recompute_pv(codegendata *cd)
+{
+       int32_t disp = (int32_t) (cd->mcodeptr - cd->mcodebase);
+
+       M_LDA(REG_PV, REG_RA, -disp);
+}
+
+
+/**
+ * Generates synchronization code to enter a monitor.
+ */
+#if defined(ENABLE_THREADS)
+void emit_monitor_enter(jitdata* jd, int32_t syncslot_offset)
+{
+       int32_t p;
+       int32_t disp;
+
+       // Get required compiler data.
+       methodinfo*  m  = jd->m;
+       codegendata* cd = jd->cd;
+
+#if !defined(NDEBUG)
+       if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
+               M_LDA(REG_SP, REG_SP, -(INT_ARG_CNT + FLT_ARG_CNT) * 8);
+
+               for (p = 0; p < INT_ARG_CNT; p++)
+                       M_LST(abi_registers_integer_argument[p], REG_SP, p * 8);
+
+               for (p = 0; p < FLT_ARG_CNT; p++)
+                       M_DST(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
+
+               syncslot_offset += (INT_ARG_CNT + FLT_ARG_CNT) * 8;
+       }
+#endif /* !defined(NDEBUG) */
+
+       /* decide which monitor enter function to call */
+
+       if (m->flags & ACC_STATIC) {
+               disp = dseg_add_address(cd, &m->clazz->object.header);
+               M_ALD(REG_A0, REG_PV, disp);
+       }
+       else {
+               M_BNEZ(REG_A0, 1);
+               M_ALD_INTERN(REG_ZERO, REG_ZERO, TRAP_NullPointerException);
+       }
+
+       M_AST(REG_A0, REG_SP, syncslot_offset);
+       disp = dseg_add_functionptr(cd, LOCK_monitor_enter);
+       M_ALD(REG_PV, REG_PV, disp);
+       M_JSR(REG_RA, REG_PV);
+       emit_recompute_pv(cd);
+
+#if !defined(NDEBUG)
+       if (JITDATA_HAS_FLAG_VERBOSECALL(jd)) {
+               for (p = 0; p < INT_ARG_CNT; p++)
+                       M_LLD(abi_registers_integer_argument[p], REG_SP, p * 8);
+
+               for (p = 0; p < FLT_ARG_CNT; p++)
+                       M_DLD(abi_registers_float_argument[p], REG_SP, (INT_ARG_CNT + p) * 8);
+
+               M_LDA(REG_SP, REG_SP, (INT_ARG_CNT + FLT_ARG_CNT) * 8);
+       }
+#endif
+}
+#endif
+
+
+/**
+ * Generates synchronization code to leave a monitor.
+ */
+#if defined(ENABLE_THREADS)
+void emit_monitor_exit(jitdata* jd, int32_t syncslot_offset)
+{
+       int32_t disp;
+
+       // Get required compiler data.
+       methodinfo*  m  = jd->m;
+       codegendata* cd = jd->cd;
+
+       M_ALD(REG_A0, REG_SP, syncslot_offset);
+
+       methoddesc* md = m->parseddesc;
+
+       switch (md->returntype.type) {
+       case TYPE_INT:
+       case TYPE_LNG:
+       case TYPE_ADR:
+               M_LST(REG_RESULT, REG_SP, syncslot_offset);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DST(REG_FRESULT, REG_SP, syncslot_offset);
+               break;
+       }
+
+       disp = dseg_add_functionptr(cd, LOCK_monitor_exit);
+       M_ALD(REG_PV, REG_PV, disp);
+       M_JSR(REG_RA, REG_PV);
+       emit_recompute_pv(cd);
+
+       switch (md->returntype.type) {
+       case TYPE_INT:
+       case TYPE_LNG:
+       case TYPE_ADR:
+               M_LLD(REG_RESULT, REG_SP, syncslot_offset);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DLD(REG_FRESULT, REG_SP, syncslot_offset);
+               break;
+       }
+}
+#endif
+
+
 /* emit_verbosecall_enter ******************************************************
 
    Generates the code for the call trace.
@@ -457,17 +605,20 @@ uint32_t emit_trap(codegendata *cd)
 void emit_verbosecall_enter(jitdata *jd)
 {
        methodinfo   *m;
+       codeinfo     *code;
        codegendata  *cd;
        registerdata *rd;
        methoddesc   *md;
+       int32_t       stackframesize;
        s4            disp;
-       s4            i, t;
+       s4            i, j, s;
 
        /* 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;
 
@@ -475,73 +626,100 @@ void emit_verbosecall_enter(jitdata *jd)
 
        M_NOP;
 
-       M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
-       M_AST(REG_RA, REG_SP, 1 * 8);
+       stackframesize = ARG_CNT + TMP_CNT + md->paramcount + 1;
 
-       /* save argument registers */
+       M_LDA(REG_SP, REG_SP, -(stackframesize * 8));
+       M_AST(REG_RA, REG_SP, 0 * 8);
+
+       /* save all argument and temporary registers for leaf methods */
 
-       for (i = 0; i < INT_ARG_CNT; i++)
-               M_LST(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
+       if (code_is_leafmethod(code)) {
+               j = 1 + md->paramcount;
 
-       for (i = 0; i < FLT_ARG_CNT; i++)
-               M_DST(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
+               for (i = 0; i < INT_ARG_CNT; i++, j++)
+                       M_LST(abi_registers_integer_argument[i], REG_SP, j * 8);
 
-       /* save temporary registers for leaf methods */
+               for (i = 0; i < FLT_ARG_CNT; i++, j++)
+                       M_DST(abi_registers_float_argument[i], REG_SP, j * 8);
 
-       if (jd->isleafmethod) {
-               for (i = 0; i < INT_TMP_CNT; i++)
-                       M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
+               for (i = 0; i < INT_TMP_CNT; i++, j++)
+                       M_LST(rd->tmpintregs[i], REG_SP, j * 8);
 
-               for (i = 0; i < FLT_TMP_CNT; i++)
-                       M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
+               for (i = 0; i < FLT_TMP_CNT; i++, j++)
+                       M_DST(rd->tmpfltregs[i], REG_SP, j * 8);
        }
 
-       /* load float arguments into integer registers */
+       /* save argument registers */
 
-       for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
-               t = md->paramtypes[i].type;
+       for (i = 0; i < md->paramcount; i++) {
+               if (!md->params[i].inmemory) {
+                       s = md->params[i].regoff;
 
-               if (IS_FLT_DBL_TYPE(t)) {
-                       if (IS_2_WORD_TYPE(t)) {
-                               M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
-                               M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
-                       }
-                       else {
-                               M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
-                               M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
+                       switch (md->paramtypes[i].type) {
+                       case TYPE_ADR:
+                       case TYPE_INT:
+                       case TYPE_LNG:
+                               M_LST(s, REG_SP, (1 + i) * 8);
+                               break;
+                       case TYPE_FLT:
+                       case TYPE_DBL:
+                               M_DST(s, REG_SP, (1 + i) * 8);
+                               break;
                        }
                }
        }
 
        disp = dseg_add_address(cd, m);
-       M_ALD(REG_ITMP1, REG_PV, disp);
-       M_AST(REG_ITMP1, REG_SP, 0 * 8);
-       disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
+       M_ALD(REG_A0, REG_PV, disp);
+       M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
+       M_LDA(REG_A2, REG_SP, stackframesize * 8 + cd->stackframesize * 8);
+
+       disp = dseg_add_functionptr(cd, trace_java_call_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);
-       M_ALD(REG_RA, REG_SP, 1 * 8);
+       M_ALD(REG_RA, REG_SP, 0 * 8);
 
        /* restore argument registers */
 
-       for (i = 0; i < INT_ARG_CNT; i++)
-               M_LLD(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
+       for (i = 0; i < md->paramcount; i++) {
+               if (!md->params[i].inmemory) {
+                       s = md->params[i].regoff;
+
+                       switch (md->paramtypes[i].type) {
+                       case TYPE_ADR:
+                       case TYPE_INT:
+                       case TYPE_LNG:
+                               M_LLD(s, REG_SP, (1 + i) * 8);
+                               break;
+                       case TYPE_FLT:
+                       case TYPE_DBL:
+                               M_DLD(s, REG_SP, (1 + i) * 8);
+                               break;
+                       }
+               }
+       }
+
+       /* restore all argument and temporary registers for leaf methods */
+
+       if (code_is_leafmethod(code)) {
+               j = 1 + md->paramcount;
 
-       for (i = 0; i < FLT_ARG_CNT; i++)
-               M_DLD(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
+               for (i = 0; i < INT_ARG_CNT; i++, j++)
+                       M_LLD(abi_registers_integer_argument[i], REG_SP, j * 8);
 
-       /* restore temporary registers for leaf methods */
+               for (i = 0; i < FLT_ARG_CNT; i++, j++)
+                       M_DLD(abi_registers_float_argument[i], REG_SP, j * 8);
 
-       if (jd->isleafmethod) {
-               for (i = 0; i < INT_TMP_CNT; i++)
-                       M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
+               for (i = 0; i < INT_TMP_CNT; i++, j++)
+                       M_LLD(rd->tmpintregs[i], REG_SP, j * 8);
 
-               for (i = 0; i < FLT_TMP_CNT; i++)
-                       M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
+               for (i = 0; i < FLT_TMP_CNT; i++, j++)
+                       M_DLD(rd->tmpfltregs[i], REG_SP, j * 8);
        }
 
-       M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
+       M_LDA(REG_SP, REG_SP, stackframesize * 8);
 
        /* mark trace code */
 
@@ -554,8 +732,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)
@@ -564,6 +740,7 @@ void emit_verbosecall_exit(jitdata *jd)
        methodinfo   *m;
        codegendata  *cd;
        registerdata *rd;
+       methoddesc   *md;
        s4            disp;
 
        /* get required compiler data */
@@ -572,34 +749,55 @@ void emit_verbosecall_exit(jitdata *jd)
        cd = jd->cd;
        rd = jd->rd;
 
+       md = m->parseddesc;
+
        /* mark trace code */
 
        M_NOP;
 
-       M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
+       M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
        M_AST(REG_RA, REG_SP, 0 * 8);
 
-       M_LST(REG_RESULT, REG_SP, 1 * 8);
-       M_DST(REG_FRESULT, REG_SP, 2 * 8);
-
-       M_MOV(REG_RESULT, REG_A0);
-       M_FMOV(REG_FRESULT, REG_FA1);
-       M_FMOV(REG_FRESULT, REG_FA2);
+       /* save return value */
+
+       switch (md->returntype.type) {
+       case TYPE_ADR:
+       case TYPE_INT:
+       case TYPE_LNG:
+               M_LST(REG_RESULT, REG_SP, 1 * 8);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DST(REG_FRESULT, REG_SP, 1 * 8);
+               break;
+       }
 
        disp = dseg_add_address(cd, m);
-       M_ALD(REG_A3, REG_PV, disp);
+       M_ALD(REG_A0, REG_PV, disp);
+       M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
 
-       disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
+       disp = dseg_add_functionptr(cd, trace_java_call_exit);
        M_ALD(REG_PV, REG_PV, disp);
        M_JSR(REG_RA, REG_PV);
        disp = (cd->mcodeptr - cd->mcodebase);
        M_LDA(REG_PV, REG_RA, -disp);
 
-       M_DLD(REG_FRESULT, REG_SP, 2 * 8);
-       M_LLD(REG_RESULT, REG_SP, 1 * 8);
+       /* restore return value */
+
+       switch (md->returntype.type) {
+       case TYPE_ADR:
+       case TYPE_INT:
+       case TYPE_LNG:
+               M_LLD(REG_RESULT, REG_SP, 1 * 8);
+               break;
+       case TYPE_FLT:
+       case TYPE_DBL:
+               M_DLD(REG_FRESULT, REG_SP, 1 * 8);
+               break;
+       }
 
        M_ALD(REG_RA, REG_SP, 0 * 8);
-       M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
+       M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
 
        /* mark trace code */