Unified variables changes for common/i386.
[cacao.git] / src / vm / jit / replace.c
index e7fd3328ba9019e874efacd31f09d7ce619f2508..060203db71d5ee67c3870b24cefaa52b7ab30105 100644 (file)
 #include <assert.h>
 #include <stdlib.h>
 
+#include "arch.h"
+
 #include "mm/memory.h"
 #include "toolbox/logging.h"
 #include "vm/options.h"
+#include "vm/jit/abi.h"
 #include "vm/jit/jit.h"
 #include "vm/jit/replace.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/disass.h"
-#include "arch.h"
+
 
 /*** constants used internally ************************************************/
 
    
 *******************************************************************************/
 
-bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
+bool replace_create_replacement_points(jitdata *jd)
 {
+#if 0
+       codeinfo     *code;
+       registerdata *rd;
        basicblock *bptr;
        int count;
        methodinfo *m;
@@ -93,6 +99,11 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
        stackptr sp;
        bool indexused;
 
+       /* get required compiler data */
+
+       code = jd->code;
+       rd   = jd->rd;
+
        /* assert that we wont overwrite already allocated data */
        
        assert(code);
@@ -109,7 +120,8 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
 
        count = 0;
        alloccount = 0;
-       for (bptr = m->basicblocks; bptr; bptr = bptr->next) {
+
+       for (bptr = jd->new_basicblocks; bptr; bptr = bptr->next) {
                if (!(bptr->bitflags & BBFLAG_REPLACEMENT))
                        continue;
 
@@ -187,7 +199,8 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
        /* initialize replacement point structs */
 
        rp = rplpoints;
-       for (bptr = m->basicblocks; bptr; bptr = bptr->next) {
+
+       for (bptr = jd->new_basicblocks; bptr; bptr = bptr->next) {
                if (!(bptr->bitflags & BBFLAG_REPLACEMENT))
                        continue;
 
@@ -218,19 +231,19 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
 
        /* store the data in the codeinfo */
 
-       code->rplpoints = rplpoints;
+       code->rplpoints     = rplpoints;
        code->rplpointcount = count;
-       code->regalloc = regalloc;
+       code->regalloc      = regalloc;
        code->regalloccount = alloccount;
-       code->globalcount = globalcount;
+       code->globalcount   = globalcount;
        code->savedintcount = INT_SAV_CNT - rd->savintreguse;
        code->savedfltcount = FLT_SAV_CNT - rd->savfltreguse;
-       code->memuse = rd->memuse;
-       code->isleafmethod = m->isleafmethod; /* XXX will be moved to codeinfo */
+       code->memuse        = rd->memuse;
 
        /* everything alright */
 
        return true;
+#endif
 }
 
 /* replace_free_replacement_points *********************************************
@@ -283,7 +296,7 @@ void replace_activate_replacement_point(rplpoint *rp,rplpoint *target)
        
        rp->target = target;
        
-#if (defined(__I386__) || defined(__X86_64__)) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
        md_patch_replacement_point(rp);
 #endif
 }
@@ -311,7 +324,7 @@ void replace_deactivate_replacement_point(rplpoint *rp)
        
        rp->target = NULL;
        
-#if (defined(__I386__) || defined(__X86_64__)) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
        md_patch_replacement_point(rp);
 #endif
 }
@@ -330,6 +343,72 @@ void replace_deactivate_replacement_point(rplpoint *rp)
   
 *******************************************************************************/
 
+inline static void replace_read_value(executionstate *es,
+#ifdef HAS_4BYTE_STACKSLOT
+                                                                         u4 *sp,
+#else
+                                                                         u8 *sp,
+#endif
+                                                                         rplalloc *ra,
+                                                                         u8 *javaval)
+{
+       if (ra->flags & INMEMORY) {
+               /* XXX HAS_4BYTE_STACKSLOT may not be the right discriminant here */
+#ifdef HAS_4BYTE_STACKSLOT
+               if (IS_2_WORD_TYPE(ra->type)) {
+                       *javaval = *(u8*)(sp + ra->index);
+               }
+               else {
+#endif
+                       *javaval = sp[ra->index];
+#ifdef HAS_4BYTE_STACKSLOT
+               }
+#endif
+       }
+       else {
+               /* allocated register */
+               if (IS_FLT_DBL_TYPE(ra->type)) {
+                       *javaval = es->fltregs[ra->index];
+               }
+               else {
+                       *javaval = es->intregs[ra->index];
+               }
+       }
+}
+
+inline static void replace_write_value(executionstate *es,
+#ifdef HAS_4BYTE_STACKSLOT
+                                                                         u4 *sp,
+#else
+                                                                         u8 *sp,
+#endif
+                                                                         rplalloc *ra,
+                                                                         u8 *javaval)
+{
+       if (ra->flags & INMEMORY) {
+               /* XXX HAS_4BYTE_STACKSLOT may not be the right discriminant here */
+#ifdef HAS_4BYTE_STACKSLOT
+               if (IS_2_WORD_TYPE(ra->type)) {
+                       *(u8*)(sp + ra->index) = *javaval;
+               }
+               else {
+#endif
+                       sp[ra->index] = *javaval;
+#ifdef HAS_4BYTE_STACKSLOT
+               }
+#endif
+       }
+       else {
+               /* allocated register */
+               if (IS_FLT_DBL_TYPE(ra->type)) {
+                       es->fltregs[ra->index] = *javaval;
+               }
+               else {
+                       es->intregs[ra->index] = *javaval;
+               }
+       }
+}
+
 static void replace_read_executionstate(rplpoint *rp,executionstate *es,
                                                                         sourcestate *ss)
 {
@@ -375,7 +454,7 @@ static void replace_read_executionstate(rplpoint *rp,executionstate *es,
        /* in some cases the top stack slot is passed in REG_ITMP1 */
 
        if (  (rp->type == BBTYPE_EXH)
-#if defined(__ALPHA__)
+#if defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)
           || (rp->type == BBTYPE_SBR)
 #endif
           )
@@ -401,7 +480,7 @@ static void replace_read_executionstate(rplpoint *rp,executionstate *es,
                ss->javalocals[i] = (u8) 0x00dead0000dead00ULL;
 
        /* some entries in the intregs array are not meaningful */
-       es->intregs[REG_ITMP3] = (u8) 0x11dead1111dead11ULL;
+       /*es->intregs[REG_ITMP3] = (u8) 0x11dead1111dead11ULL;*/
        es->intregs[REG_SP   ] = (u8) 0x11dead1111dead11ULL;
 #ifdef REG_PV
        es->intregs[REG_PV   ] = (u8) 0x11dead1111dead11ULL;
@@ -418,27 +497,7 @@ static void replace_read_executionstate(rplpoint *rp,executionstate *es,
                if (t == -1)
                        continue; /* dummy rplalloc */
 
-#ifdef HAS_4BYTE_STACKSLOT
-               if (IS_2_WORD_TYPE(ra->type)) {
-                       if (ra->flags & INMEMORY) {
-                               ss->javalocals[i*5+t] = *(u8*)(sp + ra->index);
-                       }
-                       else {
-                               dolog("XXX split 2-word types in registers are not supported");
-                               assert(0);
-                       }
-               }
-               else {
-#endif
-                       if (ra->flags & INMEMORY) {
-                               ss->javalocals[i*5+t] = sp[ra->index];
-                       }
-                       else {
-                               ss->javalocals[i*5+t] = es->intregs[ra->index];
-                       }
-#ifdef HAS_4BYTE_STACKSLOT
-               }
-#endif
+               replace_read_value(es,sp,ra,ss->javalocals + (5*i+t));
        }
 
        /* read stack slots */
@@ -480,12 +539,7 @@ static void replace_read_executionstate(rplpoint *rp,executionstate *es,
        for (; count--; ra++, i++) {
                assert(ra->next);
 
-               if (ra->flags & INMEMORY) {
-                       ss->javastack[i] = sp[ra->index];
-               }
-               else {
-                       ss->javastack[i] = es->intregs[ra->index];
-               }
+               replace_read_value(es,sp,ra,ss->javastack + i);
        }
 
        /* read unused callee saved int regs */
@@ -529,7 +583,7 @@ static void replace_read_executionstate(rplpoint *rp,executionstate *es,
        ss->syncslotcount = count;
        ss->syncslots = DMNEW(u8,count);
        for (i=0; i<count; ++i) {
-               ss->syncslots[i] = *--basesp;
+               ss->syncslots[i] = sp[code->memuse + i];
        }
 }
 
@@ -571,6 +625,7 @@ static void replace_write_executionstate(rplpoint *rp,executionstate *es,
        code = rp->code;
        m = code->m;
        md = m->parseddesc;
+       topslot = TOP_IS_NORMAL;
        
        /* calculate stack pointer */
        
@@ -593,7 +648,7 @@ static void replace_write_executionstate(rplpoint *rp,executionstate *es,
        /* in some cases the top stack slot is passed in REG_ITMP1 */
 
        if (  (rp->type == BBTYPE_EXH)
-#if defined(__ALPHA__)
+#if defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)
           || (rp->type == BBTYPE_SBR) 
 #endif
           )
@@ -626,27 +681,7 @@ static void replace_write_executionstate(rplpoint *rp,executionstate *es,
                if (t == -1)
                        continue; /* dummy rplalloc */
 
-#ifdef HAS_4BYTE_STACKSLOT
-               if (IS_2_WORD_TYPE(ra->type)) {
-                       if (ra->flags & INMEMORY) {
-                               *(u8*)(sp + ra->index) = ss->javalocals[i*5+t];
-                       }
-                       else {
-                               dolog("XXX split 2-word types in registers are not supported");
-                               assert(0);
-                       }
-               }
-               else {
-#endif
-                       if (ra->flags & INMEMORY) {
-                               sp[ra->index] = ss->javalocals[i*5+t];
-                       }
-                       else {
-                               es->intregs[ra->index] = ss->javalocals[i*5+t];
-                       }
-#ifdef HAS_4BYTE_STACKSLOT
-               }
-#endif
+               replace_write_value(es,sp,ra,ss->javalocals + (5*i+t));
        }
 
        /* write stack slots */
@@ -680,12 +715,7 @@ static void replace_write_executionstate(rplpoint *rp,executionstate *es,
        for (; count--; ra++, i++) {
                assert(ra->next);
 
-               if (ra->flags & INMEMORY) {
-                       sp[ra->index] = ss->javastack[i];
-               }
-               else {
-                       es->intregs[ra->index] = ss->javastack[i];
-               }
+               replace_write_value(es,sp,ra,ss->javastack + i);
        }
        
        /* write unused callee saved int regs */
@@ -728,7 +758,7 @@ static void replace_write_executionstate(rplpoint *rp,executionstate *es,
        count = code_get_sync_slot_count(code);
        assert(count == ss->syncslotcount);
        for (i=0; i<count; ++i) {
-               *--basesp = ss->syncslots[i];
+               sp[code->memuse + i] = ss->syncslots[i];
        }
 
        /* set new pc */
@@ -764,7 +794,7 @@ void replace_me(rplpoint *rp,executionstate *es)
 
        target = rp->target;
 
-       /* XXX DEBUG turn of self-replacement */
+       /* XXX DEBUG turn off self-replacement */
        if (target == rp)
                replace_deactivate_replacement_point(rp);
        
@@ -797,7 +827,7 @@ void replace_me(rplpoint *rp,executionstate *es)
 
        /* enter new code */
 
-#if (defined(__I386__) || defined(__X86_64__)) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
        asm_replacement_in(es);
 #endif
        abort();
@@ -924,12 +954,14 @@ void replace_executionstate_println(executionstate *es,codeinfo *code)
        printf("\tpc = %p\n",(void*)es->pc);
        printf("\tsp = %p\n",(void*)es->sp);
        printf("\tpv = %p\n",(void*)es->pv);
+#if defined(ENABLE_DISASSEMBLER)
        for (i=0; i<INT_REG_CNT; ++i) {
                printf("\t%-3s = %016llx\n",regs[i],(unsigned long long)es->intregs[i]);
        }
        for (i=0; i<FLT_REG_CNT; ++i) {
                printf("\tfltregs[%2d] = %016llx\n",i,(unsigned long long)es->fltregs[i]);
        }
+#endif
 
 #ifdef HAS_4BYTE_STACKSLOT
        sp = (u4*) es->sp;
@@ -1004,7 +1036,9 @@ void replace_sourcestate_println(sourcestate *ss)
                while (nregdescint[--reg] != REG_SAV)
                        ;
                if (ss->savedintregs[i] != 0x00dead0000dead00ULL) {
+#if defined(ENABLE_DISASSEMBLER)
                        printf("\t%-3s = ",regs[reg]);
+#endif
                        printf("%016llx\n",(unsigned long long) ss->savedintregs[i]);
                }
        }