* src/vm/jit/trace.c (trace_java_call_enter): Removed unused variable.
[cacao.git] / src / vm / jit / replace.c
index f0cbf3c0469d40e4069fd584adb181d9ec579837..c0b170231bfd8be55c1e4c2b871644bebbf7fc5f 100644 (file)
@@ -114,7 +114,7 @@ typedef u8 stackslot_t;
 
 #if !defined(NDEBUG)
 static void java_value_print(s4 type, replace_val_t value);
-static void replace_stackframeinfo_println(stackframeinfo *sfi);
+static void replace_stackframeinfo_println(stackframeinfo_t *sfi);
 #endif
 
 #if !defined(NDEBUG)
@@ -429,11 +429,6 @@ bool replace_create_replacement_points(jitdata *jd)
 
        m = code->m;
 
-       /* set codeinfo flags */
-
-       if (jd->isleafmethod)
-               CODE_SETFLAG_LEAFMETHOD(code);
-
        /* in instance methods, we may need a rplpoint at the method entry */
 
 #if defined(REPLACE_PATCH_DYNAMIC_CALL)
@@ -851,7 +846,17 @@ void replace_activate_replacement_points(codeinfo *code, bool mappable)
                savedmcode -= REPLACEMENT_PATCH_SIZE;
 
 #if defined(ENABLE_JIT)
+# if defined(ENABLE_DISASSEMBLER)
+               DOLOG( printf("\tinstruction before: ");
+                          disassinstr(rp->pc); fflush(stdout); );
+# endif
+
                md_patch_replacement_point(rp->pc, savedmcode, false);
+
+# if defined(ENABLE_DISASSEMBLER)
+               DOLOG( printf("\tinstruction after : ");
+                          disassinstr(rp->pc); fflush(stdout); );
+# endif
 #endif
 
                rp->flags |= RPLPOINT_FLAG_ACTIVE;
@@ -914,7 +919,17 @@ void replace_deactivate_replacement_points(codeinfo *code)
                           replace_replacement_point_println(rp, 1); fflush(stdout); );
 
 #if defined(ENABLE_JIT)
+# if defined(ENABLE_DISASSEMBLER)
+               DOLOG( printf("\tinstruction before: ");
+                          disassinstr(rp->pc); fflush(stdout); );
+# endif
+
                md_patch_replacement_point(rp->pc, savedmcode, true);
+
+# if defined(ENABLE_DISASSEMBLER)
+               DOLOG( printf("\tinstruction before: ");
+                          disassinstr(rp->pc); fflush(stdout); );
+# endif
 #endif
 
                rp->flags &= ~RPLPOINT_FLAG_ACTIVE;
@@ -1503,14 +1518,14 @@ u1* replace_pop_activation_record(executionstate_t *es,
        /* read the return address */
 
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
-       if (CODE_IS_LEAFMETHOD(es->code))
+       if (code_is_leafmethod(es->code))
                ra = (u1*) (ptrint) es->intregs[REPLACE_REG_RA];
        else
 #endif
                ra = md_stacktrace_get_returnaddress(es->sp,
                                SIZE_OF_STACKSLOT * es->code->stackframesize);
 
-       DOLOG( printf("return address: %p\n", (void*)ra); );
+       DOLOG( printf("RA = %p\n", (void*)ra); );
 
        assert(ra);
 
@@ -1534,14 +1549,14 @@ u1* replace_pop_activation_record(executionstate_t *es,
 
 #if defined(REPLACE_RA_TOP_OF_FRAME)
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
-       if (!CODE_IS_LEAFMETHOD(es->code))
+       if (!code_is_leafmethod(es->code))
 #endif
                es->intregs[REPLACE_REG_RA] = *--basesp;
 #endif /* REPLACE_RA_TOP_OF_FRAME */
 
 #if defined(REPLACE_RA_LINKAGE_AREA)
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
-       if (!CODE_IS_LEAFMETHOD(es->code))
+       if (!code_is_leafmethod(es->code))
 #endif
                es->intregs[REPLACE_REG_RA] = basesp[LA_LR_OFFSET / sizeof(stackslot_t)];
 #endif /* REPLACE_RA_LINKAGE_AREA */
@@ -1593,14 +1608,9 @@ u1* replace_pop_activation_record(executionstate_t *es,
        /* find the new codeinfo */
 
        pv = md_codegen_get_pv_from_pc(ra);
-
        DOLOG( printf("PV = %p\n", (void*) pv); );
 
-       if (pv == NULL) /* XXX can this really happen? */
-               return NULL;
-
-       code = *(codeinfo **)(pv + CodeinfoPointer);
-
+       code = code_get_codeinfo_for_pv(pv);
        DOLOG( printf("CODE = %p\n", (void*) code); );
 
        /* return NULL if we reached native code */
@@ -1658,8 +1668,8 @@ static void replace_patch_method_pointer(methodptr *mpp,
                                  (void*) *mpp, (void*)entrypoint); );
 
 #if !defined(NDEBUG)
-       oldcode = *(codeinfo **)((u1*)(*mpp) + CodeinfoPointer);
-       newcode = *(codeinfo **)((u1*)(entrypoint) + CodeinfoPointer);
+       oldcode = code_get_codeinfo_for_pv(*mpp);
+       newcode = code_get_codeinfo_for_pv(entrypoint);
 
        DOLOG_SHORT( printf("\tpatch %s %p ", kind, (void*) oldcode);
                                 method_println(oldcode->m);
@@ -1734,12 +1744,6 @@ struct replace_patch_data_t {
        u1         *entrypoint;
 };
 
-#define CODEINFO_OF_CODE(entrypoint) \
-       (*(codeinfo **)((u1*)(entrypoint) + CodeinfoPointer))
-
-#define METHOD_OF_CODE(entrypoint) \
-       (CODEINFO_OF_CODE(entrypoint)->m)
-
 void replace_patch_callback(classinfo *c, struct replace_patch_data_t *pd)
 {
        vftbl_t *vftbl = c->vftbl;
@@ -1747,7 +1751,7 @@ void replace_patch_callback(classinfo *c, struct replace_patch_data_t *pd)
        if (vftbl != NULL
                && vftbl->vftbllength > pd->m->vftblindex
                && vftbl->table[pd->m->vftblindex] != &asm_abstractmethoderror
-               && METHOD_OF_CODE(vftbl->table[pd->m->vftblindex]) == pd->m)
+               && code_get_methodinfo_for_pv(vftbl->table[pd->m->vftblindex]) == pd->m)
        {
                replace_patch_class(c->vftbl, pd->m, pd->oldentrypoint, pd->entrypoint);
        }
@@ -1788,15 +1792,15 @@ void replace_patch_future_calls(u1 *ra,
                                                                sourceframe_t *callerframe,
                                                                sourceframe_t *calleeframe)
 {
-       u1             *patchpos;
-       methodptr       entrypoint;
-       methodptr       oldentrypoint;
-       bool            atentry;
-       stackframeinfo  sfi;
-       codeinfo       *calleecode;
-       methodinfo     *calleem;
-       java_object_t  *obj;
-       vftbl_t        *vftbl;
+       u1            *patchpos;
+       methodptr      entrypoint;
+       methodptr      oldentrypoint;
+       bool           atentry;
+       void          *pv;
+       codeinfo      *calleecode;
+       methodinfo    *calleem;
+       java_object_t *obj;
+       vftbl_t       *vftbl;
 
        assert(ra);
        assert(callerframe->down == calleeframe);
@@ -1819,8 +1823,8 @@ void replace_patch_future_calls(u1 *ra,
 
        /* get the position to patch, in case it was a statically bound call   */
 
-       sfi.pv = callerframe->fromcode->entrypoint;
-       patchpos = md_get_method_patch_address(ra, &sfi, NULL);
+       pv = callerframe->fromcode->entrypoint;
+       patchpos = md_jit_method_patch_address(pv, ra, NULL);
 
        if (patchpos == NULL) {
                /* the call was dispatched dynamically */
@@ -1956,14 +1960,14 @@ void replace_push_activation_record(executionstate_t *es,
 
 #if defined(REPLACE_RA_TOP_OF_FRAME)
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
-       if (!CODE_IS_LEAFMETHOD(calleecode))
+       if (!code_is_leafmethod(calleecode))
 #endif
                *--basesp = (ptrint) ra;
 #endif /* REPLACE_RA_TOP_OF_FRAME */
 
 #if defined(REPLACE_RA_LINKAGE_AREA)
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
-       if (!CODE_IS_LEAFMETHOD(calleecode))
+       if (!code_is_leafmethod(calleecode))
 #endif
                basesp[LA_LR_OFFSET / sizeof(stackslot_t)] = (ptrint) ra;
 #endif /* REPLACE_RA_LINKAGE_AREA */
@@ -2181,7 +2185,7 @@ rplpoint *replace_find_replacement_point_for_pc(codeinfo *code, u1 *pc)
 
 static void replace_pop_native_frame(executionstate_t *es,
                                                                         sourcestate_t *ss,
-                                                                        stackframeinfo *sfi)
+                                                                        stackframeinfo_t *sfi)
 {
        sourceframe_t *frame;
        codeinfo      *code;
@@ -2260,29 +2264,21 @@ static void replace_pop_native_frame(executionstate_t *es,
 # endif
 #endif
 
-       /* restore pv, pc, and sp */
-
-       if (sfi->pv == NULL) {
-               /* frame of a native function call */
-               es->pv = md_codegen_get_pv_from_pc(sfi->ra);
-       }
-       else {
-               es->pv = sfi->pv;
-       }
-       es->pc = ((sfi->xpc) ? sfi->xpc : sfi->ra) - 1;
-       es->sp = sfi->sp;
-
-       /* find the new codeinfo */
-
-       DOLOG( printf("PV = %p\n", (void*) es->pv); );
-
-       assert(es->pv != NULL);
+       /* restore codeinfo of the native stub */
 
-       code = *(codeinfo **)(es->pv + CodeinfoPointer);
+       code = code_get_codeinfo_for_pv(sfi->pv);
 
-       DOLOG( printf("CODE = %p\n", (void*) code); );
+       /* restore sp, pv, pc and codeinfo of the parent method */
 
-       es->code = code;
+       /* XXX michi: use this instead:
+       es->sp = sfi->sp + code->stackframesize; */
+       es->sp   = sfi->sp + (*(s4 *) (sfi->pv + FrameSize));
+#if defined(REPLACE_RA_BETWEEN_FRAMES)
+       es->sp  += SIZE_OF_STACKSLOT; /* skip return address */
+#endif
+       es->pv   = md_codegen_get_pv_from_pc(sfi->ra);
+       es->pc   = ((sfi->xpc) ? sfi->xpc : sfi->ra) - 1;
+       es->code = code_get_codeinfo_for_pv(es->pv);
 }
 
 
@@ -2323,6 +2319,13 @@ static void replace_push_native_frame(executionstate_t *es, sourcestate_t *ss)
 
        ss->frames = frame->down;
 
+       /* skip sp for the native stub */
+
+       es->sp -= (*(s4 *) (frame->sfi->pv + FrameSize));
+#if defined(REPLACE_RA_BETWEEN_FRAMES)
+       es->sp -= SIZE_OF_STACKSLOT; /* skip return address */
+#endif
+
        /* assert that the native frame has not moved */
 
        assert(es->sp == frame->sfi->sp);
@@ -2394,7 +2397,7 @@ static void replace_push_native_frame(executionstate_t *es, sourcestate_t *ss)
 *******************************************************************************/
 
 sourcestate_t *replace_recover_source_state(rplpoint *rp,
-                                                                                       stackframeinfo *sfi,
+                                                                                       stackframeinfo_t *sfi,
                                                                                    executionstate_t *es)
 {
        sourcestate_t *ss;
@@ -2748,7 +2751,7 @@ static void replace_build_execution_state(sourcestate_t *ss,
 
 static void replace_me(rplpoint *rp, executionstate_t *es)
 {
-       stackframeinfo      *sfi;
+       stackframeinfo_t    *sfi;
        sourcestate_t       *ss;
        sourceframe_t       *frame;
        s4                   dumpsize;
@@ -2939,7 +2942,7 @@ bool replace_me_wrapper(u1 *pc, void *context)
 #if defined(ENABLE_GC_CACAO)
 void replace_gc_from_native(threadobject *thread, u1 *pc, u1 *sp)
 {
-       stackframeinfo   *sfi;
+       stackframeinfo_t *sfi;
        executionstate_t *es;
        sourcestate_t    *ss;
 
@@ -2966,6 +2969,21 @@ void replace_gc_from_native(threadobject *thread, u1 *pc, u1 *sp)
 }
 #endif
 
+#if defined(ENABLE_GC_CACAO)
+void replace_gc_into_native(threadobject *thread)
+{
+       executionstate_t *es;
+       sourcestate_t    *ss;
+
+       /* get the executionstate and sourcestate for the given thread */
+       es = GC_EXECUTIONSTATE;
+       ss = GC_SOURCESTATE;
+
+       /* rebuild the stack of the given thread */
+       replace_build_execution_state(ss, es);
+}
+#endif
+
 
 /******************************************************************************/
 /* NOTE: No important code below.                                             */
@@ -3531,14 +3549,14 @@ void replace_sourcestate_println_short(sourcestate_t *ss)
 #endif
 
 #if !defined(NDEBUG)
-static void replace_stackframeinfo_println(stackframeinfo *sfi)
+static void replace_stackframeinfo_println(stackframeinfo_t *sfi)
 {
        printf("prev=%p pv=%p sp=%p ra=%p xpc=%p method=",
                        (void*)sfi->prev, (void*)sfi->pv, (void*)sfi->sp,
                        (void*)sfi->ra, (void*)sfi->xpc);
 
-       if (sfi->method)
-               method_println(sfi->method);
+       if (sfi->code)
+               method_println(sfi->code->m);
        else
                printf("(nil)\n");
 }