* changed src/vm/jit/m68k/asmpart.S (asm_patcher_wrapper): Removed.
[cacao.git] / src / vm / jit / m68k / patcher.c
index 9c53444bab70ca0cc686c06ddd4585ebf1b9f34e..0aab56cce7a14fcc95bc7657086b613cc5fa09d7 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: arch.h 5330 2006-09-05 18:43:12Z edwin $
-
 */
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 
 #include "vm/types.h"
 
@@ -41,9 +40,8 @@
 #include "vm/resolve.h"
 
 #include "vm/jit/asmpart.h"
-#include "vm/jit/patcher.h"
+#include "vm/jit/patcher-common.h"
 #include "vm/jit/md.h"
-#include "vm/jit/methodheader.h"
 #include "vm/jit/stacktrace.h"
 
 #include "vmcore/class.h"
 
 *******************************************************************************/
 
+#if 0
 java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 {
-       stackframeinfo     sfi;
+       stackframeinfo_t   sfi;
        u1                *xpc;
        java_objectheader *o;
        functionptr        f;
        bool               result;
        java_objectheader *e;
-       u4                 mcode, xmcode;
 
        /* define the patcher function */
 
@@ -83,52 +81,41 @@ java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
        xpc = (u1 *)                *((ptrint *) (sp + 6 * 4));
        /* REG_ITMP3                              sp + 5 * 4 */
        o   = (java_objectheader *) *((ptrint *) (sp + 4 * 4));
-       mcode =                     *((u4*)      (sp + 3 * 4));
-       xmcode =                    *((u4*)      (sp + 2 * 4));
+       /*mcode =                                   *((u4*)      (sp + 3 * 4));*/
+       /*xmcode =                                      *((u4*)      (sp + 2 * 4));*/
        /* unresolved file                        sp + 1 * 4 */
        f   = (functionptr)         *((ptrint *) (sp + 0 * 4));
 
 
        /* calculate and set the new return address */
-
        xpc = xpc - PATCHER_CALL_SIZE;
        *((ptrint *) (sp + 6 * 4)) = (ptrint) xpc;
 
-       /* patch back original code */
-
-       *((u4*)(xpc))   = mcode;
-       *((u4*)(xpc+4)) = xmcode;
-       md_icacheflush(xpc, 8);
 
        /* cast the passed function to a patcher function */
-
        patcher_function = (bool (*)(u1 *)) (ptrint) f;
 
        /* enter a monitor on the patching position */
-
        PATCHER_MONITORENTER;
 
        /* create the stackframeinfo */
 
        /* RA is passed as NULL, but the XPC is correct and can be used in
-          stacktrace_create_extern_stackframeinfo for
-          md_codegen_get_pv_from_pc. */
+          stacktrace_stackframeinfo_add for md_codegen_get_pv_from_pc. */
 
        /*
        fprintf(stderr, "EXT STACKFRAME: sfi=%x pv=%x, sp=%x, xpc=%x\n", &sfi, pv, sp+7*4, xpc);
        */
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp + 7 * 4, xpc, xpc);
+       stacktrace_stackframeinfo_add(&sfi, pv, sp + 7 * 4, xpc, xpc);
 
        /* call the proper patcher function */
-
        result = (patcher_function)(sp);
 
-       /* remove the stackframeinfo */
 
-       stacktrace_remove_stackframeinfo(&sfi);
+       /* remove the stackframeinfo */
+       stacktrace_stackframeinfo_remove(&sfi);
 
        /* check for return value and exit accordingly */
-
        if (result == false) {
                e = exceptions_get_and_clear_exception();
 
@@ -140,7 +127,36 @@ java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 
        return NULL;
 }
+#endif
+
+
+#define PATCH_BACK_ORIGINAL_MCODE  *((u8*)(pr->mpc)) = pr->mcode
+
+/* patcher_initialize_class ****************************************************
+
+       just patch back original code
+
+*******************************************************************************/
+
+
+void patcher_patch_code(patchref_t *pr)        
+{
+#if 0
+       u1* xpc    = (u1 *)      *((ptrint *) (sp + 6 * 4));
+       u4 mcode  = *((u4*)      (sp + 3 * 4));
+       u4 xmcode = *((u4*)      (sp + 2 * 4));
+
+       *((u4*)(xpc))   = mcode;
+       *((u4*)(xpc+4)) = xmcode;
+#endif
+
+       PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush((void*)pr->mpc, 8);
+
+}
 
+
+#if 0
 /* patcher_initialize_class ****************************************************
 
    Initalizes a given classinfo pointer.  This function does not patch
@@ -148,22 +164,25 @@ java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 
 *******************************************************************************/
 
-bool patcher_initialize_class(u1 *sp)
+bool patcher_initialize_class(patchref_t *pr)
 {
        classinfo *c;
+       u4                 xpc, mcode, xmcode;
 
        /* get stuff from the stack */
-
        c = (classinfo *) *((ptrint *) (sp + 1 * 4));
 
        /* check if the class is initialized */
-
        if (!(c->state & CLASS_INITIALIZED))
                if (!initialize_class(c))
                        return false;
 
+       /* patch back original code */
+       patcher_patch_back(sp);
+
        return true;
 }
+#endif
 
 /* patcher_invokevirtual *******************************************************
 
@@ -176,7 +195,7 @@ bool patcher_initialize_class(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_invokevirtual(u1 *sp)
+bool patcher_invokevirtual(patchref_t *pr)
 {
        u1                *ra;
        unresolved_method *um;
@@ -184,13 +203,16 @@ bool patcher_invokevirtual(u1 *sp)
        s2                 disp;
 
        /* get stuff from the stack */
-       ra = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       um = (unresolved_method *) *((ptrint *) (sp + 1 * 4));
+       ra = (u1 *)                pr->mpc;
+       um = (unresolved_method *) pr->ref;
 
        /* get the fieldinfo */
        if (!(m = resolve_method_eager(um)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* if we show NOPs, we have to skip them */
        if (opt_shownops) ra += PATCHER_CALL_SIZE;
 
@@ -217,36 +239,37 @@ bool patcher_invokevirtual(u1 *sp)
 
 ******************************************************************************/
 
-bool patcher_invokestatic_special(u1 *sp)
+bool patcher_invokestatic_special(patchref_t *pr)
 {
        unresolved_method *um;
        s4                 disp;
        methodinfo        *m;
 
        /* get stuff from the stack */
-
-       disp =                       *((s4 *)     (sp + 6 * 4));
-       um   = (unresolved_method *) *((ptrint *) (sp + 1 * 4));
+       disp =                       pr->mpc;
+       um   = (unresolved_method *) pr->ref;
 
        /* get the fieldinfo */
-
        if (!(m = resolve_method_eager(um)))
                return false;
 
-       /* patch stubroutine */
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
 
+       /* patch stubroutine */
        if (opt_shownops) disp += PATCHER_CALL_SIZE;
 
        *((ptrint *) (disp+2)) = (ptrint) m->stubroutine;
 
        /* synchronize inst cache */
 
-       md_icacheflush(disp+2, SIZEOF_VOID_P);
+       md_icacheflush((void*)(disp+2), SIZEOF_VOID_P);
 
        return true;
 }
 
 
+#if 0
 /* patcher_resolve_class *******************************************************
 
    Resolves a given unresolved_class pointer.  This function does not
@@ -255,27 +278,33 @@ bool patcher_invokestatic_special(u1 *sp)
 *******************************************************************************/
 
 #ifdef ENABLE_VERIFIER
-bool patcher_resolve_class(u1 *sp)
+bool patcher_resolve_class(patchref_t *pr)
 {
        unresolved_class *uc;
        classinfo        *c;
+       s4                              disp;
 
        /* get stuff from the stack */
        uc = (unresolved_class *) *((ptrint *) (sp + 1 * 4));
+       disp =                    *((s4 *)     (sp + 6 * 4));
 
        /* resolve the class */
        if (!resolve_class(uc, resolveEager, false, &c))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        return true;
 }
 #endif /* ENABLE_VERIFIER */
+#endif
 
 /* patcher_resolve_classref_to_classinfo ***************************************
   ACONST:
        0x4028f2ca:   2479 0000 0000    moveal 0x00000000,%a2
 *******************************************************************************/
-bool patcher_resolve_classref_to_classinfo(u1 *sp)
+bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
 {
        constant_classref *cr;
        s4                 disp;
@@ -283,23 +312,21 @@ bool patcher_resolve_classref_to_classinfo(u1 *sp)
        classinfo         *c;
 
        /* get stuff from the stack */
-
-       cr   = (constant_classref *) *((ptrint *) (sp + 1 * 4));
-       disp =                       *((s4 *)     (sp + 6 * 4));
+       cr   = (constant_classref *) pr->ref;
+       disp =                       pr->mpc;
 
        /* get the classinfo */
-
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch the classinfo pointer */
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
 
+       /* patch the classinfo pointer */
        if (opt_shownops) disp += PATCHER_CALL_SIZE;
-
        *((ptrint *) (disp+2)) = (ptrint) c;
 
        /* synchronize inst cache */
-
        md_icacheflush(disp+2, SIZEOF_VOID_P);
 
        return true;
@@ -311,18 +338,15 @@ bool patcher_resolve_classref_to_classinfo(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_get_putstatic(u1 *sp)
+bool patcher_get_putstatic(patchref_t *pr)
 {
-       u1               *ra;
-       u4                mcode;
        unresolved_field *uf;
        s4                disp;
-       u1               *pv;
        fieldinfo        *fi;
 
        /* get stuff from the stack */
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 1 * 4));
-       disp  =                       *((s4 *)     (sp + 6 * 4));
+       uf    = (unresolved_field *)  pr->ref;
+       disp  =                       pr->mpc;
 
        /* get the fieldinfo */
        if (!(fi = resolve_field_eager(uf)))
@@ -333,13 +357,16 @@ bool patcher_get_putstatic(u1 *sp)
                if (!initialize_class(fi->class))
                        return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* patch the field value's address */
        if (opt_shownops) disp += PATCHER_CALL_SIZE;
        assert(*((uint16_t*)(disp)) == 0x247c);
-       *((ptrint *) (disp+2)) = (ptrint) &(fi->value);
+       *((intptr_t *) (disp+2)) = (intptr_t) fi->value;
 
        /* synchronize inst cache */
-       md_icacheflush(disp+2, SIZEOF_VOID_P);
+       md_icacheflush((void*)(disp+2), SIZEOF_VOID_P);
 
        return true;
 }
@@ -352,52 +379,69 @@ bool patcher_get_putstatic(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_get_putfield(u1 *sp)
+bool patcher_get_putfield(patchref_t *pr)
 {
        u1               *ra;
        unresolved_field *uf;
        fieldinfo        *fi;
-       s2                disp;
 
-       ra = (u1 *)               *((ptrint *) (sp + 6 * 4));
-       uf = (unresolved_field *) *((ptrint *) (sp + 1 * 4));
+       ra = (u1 *)               pr->mpc;
+       uf = (unresolved_field *) pr->ref;
 
        /* get the fieldinfo */
        if (!(fi = resolve_field_eager(uf)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* if we show NOPs, we have to skip them */
        if (opt_shownops) ra += PATCHER_CALL_SIZE;
 
        /* patch the field's offset */
        if (IS_LNG_TYPE(fi->type)) {
-               /* If the field has type long, we have to patch two
-                  instructions.  But we have to check which instruction
-                  is first.  We do that with the offset of the first
-                  instruction. */
-               assert(0);
-               disp = *((u4 *) (ra + 1 * 4));
-
-               if (disp == 4) {
-                       *((u4 *) (ra + 1 * 4)) |= (s2) ((fi->offset + 4) & 0x0000ffff);
-                       *((u4 *) (ra + 2 * 4)) |= (s2) ((fi->offset + 0) & 0x0000ffff);
-               }
-               else {
-                       *((u4 *) (ra + 1 * 4)) |= (s2) ((fi->offset + 0) & 0x0000ffff);
-                       *((u4 *) (ra + 2 * 4)) |= (s2) ((fi->offset + 4) & 0x0000ffff);
-               }
-       } else  {
                /*
-                *      0x40adb3f6:     0x254d0000      movel %a5,%a2@(0)
-                *                            ^^^^                     ^
-                *                            to be patched
+                *      0x40d05bb2:     0x25440000      movel %d4,%a2@(0)
+                *      0x40d05bb6:     0x25430004      movel %d3,%a2@(4)
+                *                                            ^^^^
+                *                                            both 0000 and 0004 have to be patched
                 */
-               assert( (*((uint32_t*)ra) & 0xffff0000) == *((uint32_t*)ra) );
+
                assert( (fi->offset & 0x0000ffff) == fi->offset );
-               *((uint32_t*)ra) |= fi->offset;
 
-               /* synchronize instruction cache */
-               md_icacheflush(ra, 1 * 4);
+               assert( (*((uint32_t*)ra) & 0xffff0000) == *((uint32_t*)ra) );
+               assert( (*((uint32_t*)(ra+4)) & 0xffff0004) == *((uint32_t*)(ra+4)) );
+
+               *((int16_t *) (ra + 2)) = (int16_t) ((fi->offset) & 0x0000ffff);
+               *((int16_t *) (ra + 6)) = (int16_t) ((fi->offset + 4) & 0x0000ffff);
+
+               md_icacheflush(ra, 2 * 4);
+       } else  {
+               /*      Multiple cases here, int, adr, flt and dbl. */
+               if ( (*((uint32_t*)ra) & 0xfff00000) == 0xf2200000 )    {
+                       /* flt/dbl case 
+                        * 0x40d3ddc2:     0xf22944c0 0x0000xxxx        fsmoves %a1@(0),%fp1
+                        *                                  ^^^^
+                        *                                  patch here 
+                        */
+                       assert( (fi->offset & 0x0000ffff) == fi->offset );
+                       assert( (*((uint32_t*)(ra+4)) & 0x0000ffff) == *((uint32_t*)(ra+4)) );
+                       *((int16_t*)(ra+4)) = (int16_t)fi->offset;
+
+                       md_icacheflush(ra+4, 1 * 4);
+               } else  {
+                       /*      int/adr case
+                        *      0x40adb3f6:     0x254d0000      movel %a5,%a2@(0)
+                        *                            ^^^^                     ^
+                        *                            to be patched
+                        */
+                       assert( (*((uint32_t*)ra) & 0xffff0000) == *((uint32_t*)ra) );
+                       assert( (fi->offset & 0x0000ffff) == fi->offset );
+                       *((int16_t*)(ra+2)) = (int16_t)fi->offset;
+
+                       /* synchronize instruction cache */
+                       md_icacheflush(ra, 1 * 4);
+               }
        }
 
        return true;
@@ -420,27 +464,30 @@ INSTANCEOF:
 
 *******************************************************************************/
 
-bool patcher_resolve_classref_to_flags(u1 *sp)
+bool patcher_resolve_classref_to_flags(patchref_t *pr)
 {
        constant_classref *cr;
        s4                 disp;
        classinfo         *c;
 
        /* get stuff from the stack */
-       cr   = (constant_classref *) *((ptrint *) (sp + 1 * 4));
-       disp =                       *((s4 *)     (sp + 6 * 4));
+       cr   = (constant_classref *) pr->ref;
+       disp =                       pr->mpc;
 
        /* get the fieldinfo */
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* patch class flags */
        if (opt_shownops) disp += PATCHER_CALL_SIZE;
        assert( (*((u2*)(disp)) == 0x263c) || (*((u2*)(disp)) == 0x283c) );
        *((s4 *) (disp + 2)) = (s4) c->flags;
 
        /* synchronize insn cache */
-       md_icacheflush(disp + 2, SIZEOF_VOID_P);
+       md_icacheflush((void*)(disp + 2), SIZEOF_VOID_P);
 
        return true;
 }
@@ -460,20 +507,23 @@ bool patcher_resolve_classref_to_flags(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_resolve_classref_to_vftbl(u1 *sp)
+bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
 {
        constant_classref *cr;
        s4                 disp;
        classinfo         *c;
 
        /* get stuff from the stack */
-       cr   = (constant_classref *) *((ptrint *) (sp + 1 * 4));
-       disp =                       *((s4 *)     (sp + 6 * 4));
+       cr   = (constant_classref *) pr->ref;
+       disp =                       pr->mpc;
 
        /* get the fieldinfo */
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* patch super class' vftbl */
        if (opt_shownops) disp += PATCHER_CALL_SIZE;
        assert( (*((u2*)disp) == 0x287c) || (*((u2*)disp)== 0x267c) );
@@ -481,7 +531,7 @@ bool patcher_resolve_classref_to_vftbl(u1 *sp)
        *((s4 *) (disp+2)) = (s4) c->vftbl;
 
        /* synchronize insin cache */
-       md_icacheflush(disp+2, SIZEOF_VOID_P);
+       md_icacheflush((void*)(disp+2), SIZEOF_VOID_P);
 
        return true;
 }
@@ -500,7 +550,7 @@ bool patcher_resolve_classref_to_vftbl(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_instanceof_interface(u1 *sp)
+bool patcher_instanceof_interface(patchref_t *pr)
 {
        u1                *ra;
        constant_classref *cr;
@@ -509,13 +559,16 @@ bool patcher_instanceof_interface(u1 *sp)
 
        /* get stuff from the stack */
 
-       ra = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       cr = (constant_classref *) *((ptrint *) (sp + 1 * 4));
+       ra = (u1 *)                pr->mpc;
+       cr = (constant_classref *) pr->ref;
 
        /* get the fieldinfo */
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* if we show NOPs, we have to skip them */
        if (opt_shownops) ra += PATCHER_CALL_SIZE;
                
@@ -532,7 +585,7 @@ bool patcher_instanceof_interface(u1 *sp)
        *((s2 *) (ra + 20)) = disp;
 
        /* synchronize instruction cache */
-       md_icacheflush(ra + 10, 12);
+       md_icacheflush((void*)(ra + 10), 12);
 
        return true;
 }
@@ -551,7 +604,7 @@ bool patcher_instanceof_interface(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_checkcast_interface(u1 *sp)
+bool patcher_checkcast_interface(patchref_t *pr)
 {
        u1                *ra;
        constant_classref *cr;
@@ -559,13 +612,16 @@ bool patcher_checkcast_interface(u1 *sp)
        s4                 disp;
 
        /* get stuff from the stack */
-       ra = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       cr = (constant_classref *) *((ptrint *) (sp + 1 * 4));
+       ra = (u1 *)                pr->mpc;
+       cr = (constant_classref *) pr->ref;
 
        /* get the fieldinfo */
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* if we show NOPs, we have to skip them */
        if (opt_shownops) ra += PATCHER_CALL_SIZE;
 
@@ -580,43 +636,45 @@ bool patcher_checkcast_interface(u1 *sp)
        *((s2 *) (ra + 24)) = disp;
        
        /* synchronize instruction cache */
-       md_icacheflush(ra + 10, 16);
+       md_icacheflush((void*)(ra + 10), 16);
 
        return true;
 }
 
+#if 0
 /* patcher_resolve_native_function *********************************************
 
    XXX
 
 *******************************************************************************/
 
-#if !defined(WITH_STATIC_CLASSPATH)
-bool patcher_resolve_native_function(u1 *sp)
+bool patcher_resolve_native_function(patchref_t *pr)
 {
        methodinfo  *m;
        s4           disp;
        functionptr  f;
 
        /* get stuff from the stack */
-       m    = (methodinfo *) *((ptrint *) (sp + 1 * 4));
-       disp =                *((s4 *)     (sp + 6 * 4));
+       m    = (methodinfo *) pr->ref;
+       disp =                pr->mpc;
 
        /* resolve native function */
        if (!(f = native_resolve_function(m)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* patch native function pointer */
        if (opt_shownops) disp += PATCHER_CALL_SIZE;
        *((ptrint *) (disp + 2)) = (ptrint) f;
 
        /* synchronize data cache */
-       md_icacheflush(disp + 2, SIZEOF_VOID_P);
+       md_icacheflush((void*)(disp + 2), SIZEOF_VOID_P);
 
        return true;
 }
-#endif /* !defined(WITH_STATIC_CLASSPATH) */
-
+#endif
 
 /* patcher_invokeinterface *****************************************************
 
@@ -629,7 +687,7 @@ bool patcher_resolve_native_function(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_invokeinterface(u1 *sp)
+bool patcher_invokeinterface(patchref_t *pr)
 {
        u1                *ra;
        unresolved_method *um;
@@ -637,14 +695,17 @@ bool patcher_invokeinterface(u1 *sp)
        s4                 disp;
 
        /* get stuff from the stack */
-       ra = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       um = (unresolved_method *) *((ptrint *) (sp + 1 * 4));
+       ra = (u1 *)                pr->mpc;
+       um = (unresolved_method *) pr->ref;
 
 
        /* get the fieldinfo */
        if (!(m = resolve_method_eager(um)))
                return false;
 
+       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
+
        /* if we show NOPs, we have to skip them */
        if (opt_shownops) ra += PATCHER_CALL_SIZE;
        assert( *((uint32_t*)ra) == 0x246f0000 );
@@ -661,7 +722,7 @@ bool patcher_invokeinterface(u1 *sp)
        *((uint16_t *) (ra + 7 * 2)) = disp;
 
        /* synchronize instruction cache */
-       md_icacheflush(ra + 5 * 2, 2 * 2);
+       md_icacheflush((void*)(ra + 5 * 2), 2 * 2);
 
        return true;
 }