* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / src / vm / jit / m68k / patcher.c
index 0aab56cce7a14fcc95bc7657086b613cc5fa09d7..1bea411e74fadbe3f0a40f77ed81a2bb9ce33807 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/m68k/patcher.c - m68k patcher 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/types.h"
 
-#include "mm/memory.h"
-#include "native/native.h"
+#include "vm/jit/m68k/md.h"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
-#include "vm/initialize.h"
-#include "vm/resolve.h"
+#include "mm/memory.hpp"
+#include "native/native.hpp"
 
-#include "vm/jit/asmpart.h"
-#include "vm/jit/patcher-common.h"
-#include "vm/jit/md.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/class.hpp"
+#include "vm/field.hpp"
+#include "vm/initialize.hpp"
+#include "vm/options.h"
+#include "vm/references.h"
+#include "vm/resolve.hpp"
 
-#include "vmcore/class.h"
-#include "vmcore/field.h"
-#include "vmcore/options.h"
-#include "vmcore/references.h"
+#include "vm/jit/asmpart.h"
+#include "vm/jit/patcher-common.hpp"
 
 #include "codegen.h"
 
-/* patcher_wrapper *************************************************************
-
-   Wrapper for all patchers.  It also creates the stackframe info
-   structure.
-
-   If the return value of the patcher function is false, it gets the
-   exception object, clears the exception pointer and returns the
-   exception.
-
-*******************************************************************************/
-
-#if 0
-java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
-{
-       stackframeinfo_t   sfi;
-       u1                *xpc;
-       java_objectheader *o;
-       functionptr        f;
-       bool               result;
-       java_objectheader *e;
-
-       /* define the patcher function */
-
-       bool (*patcher_function)(u1 *);
-
-       /* get stuff from the stack */
-
-       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));*/
-       /* 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;
-
-
-       /* 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_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_stackframeinfo_add(&sfi, pv, sp + 7 * 4, xpc, xpc);
-
-       /* call the proper patcher function */
-       result = (patcher_function)(sp);
-
-
-       /* remove the stackframeinfo */
-       stacktrace_stackframeinfo_remove(&sfi);
-
-       /* check for return value and exit accordingly */
-       if (result == false) {
-               e = exceptions_get_and_clear_exception();
-
-               PATCHER_MONITOREXIT;
-
-               return e;
-       }
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
-       return NULL;
-}
-#endif
-
-
-#define PATCH_BACK_ORIGINAL_MCODE  *((u8*)(pr->mpc)) = pr->mcode
+#define PATCH_BACK_ORIGINAL_MCODE  *((u4*)(pr->mpc)) = pr->mcode
 
 /* patcher_initialize_class ****************************************************
 
@@ -151,7 +68,7 @@ void patcher_patch_code(patchref_t *pr)
 #endif
 
        PATCH_BACK_ORIGINAL_MCODE;
-       md_icacheflush((void*)pr->mpc, 8);
+       md_icacheflush((void*)pr->mpc, 2);
 
 }
 
@@ -213,9 +130,6 @@ bool patcher_invokevirtual(patchref_t *pr)
        /* 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( *((u2*)(ra+8)) == 0x286b);
 
        /* patch vftbl index */
@@ -223,7 +137,7 @@ bool patcher_invokevirtual(patchref_t *pr)
        *((s2 *) (ra + 10)) = disp;
 
        /* synchronize instruction cache */
-       md_icacheflush(ra + 10, 2);
+       md_icacheflush(pr->mpc, 10 + 2 + PATCHER_CALL_SIZE);
 
        return true;
 }
@@ -256,50 +170,16 @@ bool patcher_invokestatic_special(patchref_t *pr)
        /* 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((void*)(disp+2), SIZEOF_VOID_P);
+       md_icacheflush(pr->mpc, PATCHER_CALL_SIZE+2+SIZEOF_VOID_P);
 
        return true;
 }
 
 
-#if 0
-/* patcher_resolve_class *******************************************************
-
-   Resolves a given unresolved_class pointer.  This function does not
-   patch any data.
-
-*******************************************************************************/
-
-#ifdef ENABLE_VERIFIER
-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
@@ -323,11 +203,10 @@ bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
        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);
+       md_icacheflush(pr->mpc, PATCHER_CALL_SIZE + 2 + SIZEOF_VOID_P);
 
        return true;
 }
@@ -353,15 +232,15 @@ bool patcher_get_putstatic(patchref_t *pr)
                return false;
 
        /* check if the field's class is initialized */
-       if (!(fi->class->state & CLASS_INITIALIZED))
-               if (!initialize_class(fi->class))
+       if (!(fi->clazz->state & CLASS_INITIALIZED))
+               if (!initialize_class(fi->clazz))
                        return false;
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush(pr->mpc, 2);
 
        /* patch the field value's address */
-       if (opt_shownops) disp += PATCHER_CALL_SIZE;
        assert(*((uint16_t*)(disp)) == 0x247c);
        *((intptr_t *) (disp+2)) = (intptr_t) fi->value;
 
@@ -394,9 +273,7 @@ bool patcher_get_putfield(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
-
-       /* if we show NOPs, we have to skip them */
-       if (opt_shownops) ra += PATCHER_CALL_SIZE;
+       md_icacheflush(pr->mpc, 2);
 
        /* patch the field's offset */
        if (IS_LNG_TYPE(fi->type)) {
@@ -480,9 +357,9 @@ bool patcher_resolve_classref_to_flags(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush(pr->mpc, 2);
 
        /* patch class flags */
-       if (opt_shownops) disp += PATCHER_CALL_SIZE;
        assert( (*((u2*)(disp)) == 0x263c) || (*((u2*)(disp)) == 0x283c) );
        *((s4 *) (disp + 2)) = (s4) c->flags;
 
@@ -523,9 +400,9 @@ bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush(pr->mpc, 2);
 
        /* patch super class' vftbl */
-       if (opt_shownops) disp += PATCHER_CALL_SIZE;
        assert( (*((u2*)disp) == 0x287c) || (*((u2*)disp)== 0x267c) );
 
        *((s4 *) (disp+2)) = (s4) c->vftbl;
@@ -568,10 +445,8 @@ bool patcher_instanceof_interface(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush(pr->mpc, 2);
 
-       /* if we show NOPs, we have to skip them */
-       if (opt_shownops) ra += PATCHER_CALL_SIZE;
-               
        /* patch super class index */
        disp = -(c->index);
        assert( *((u2*)(ra + 8)) == 0xd8bc );
@@ -621,9 +496,7 @@ bool patcher_checkcast_interface(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
-
-       /* if we show NOPs, we have to skip them */
-       if (opt_shownops) ra += PATCHER_CALL_SIZE;
+       md_icacheflush(pr->mpc, 2);
 
        /* patch super class index */
        disp = -(c->index);
@@ -664,9 +537,9 @@ bool patcher_resolve_native_function(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush(pr->mpc, 2);
 
        /* patch native function pointer */
-       if (opt_shownops) disp += PATCHER_CALL_SIZE;
        *((ptrint *) (disp + 2)) = (ptrint) f;
 
        /* synchronize data cache */
@@ -705,19 +578,19 @@ bool patcher_invokeinterface(patchref_t *pr)
 
        /* patch back original code */
        PATCH_BACK_ORIGINAL_MCODE;
+       md_icacheflush(pr->mpc, 2);
 
        /* if we show NOPs, we have to skip them */
-       if (opt_shownops) ra += PATCHER_CALL_SIZE;
        assert( *((uint32_t*)ra) == 0x246f0000 );
 
        /* patch interfacetable index (first #0) */
-       disp = OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->class->index;
+       disp = OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->clazz->index;
        /* XXX this disp is negative, check! 
         * assert( (disp & 0x0000ffff) == disp);*/
        *((uint16_t *) (ra + 5 * 2)) = disp;
 
        /* patch method offset (second #0) */
-       disp = sizeof(methodptr) * (m - m->class->methods);
+       disp = sizeof(methodptr) * (m - m->clazz->methods);
        assert( (disp & 0x0000ffff) == disp);
        *((uint16_t *) (ra + 7 * 2)) = disp;
 
@@ -726,6 +599,8 @@ bool patcher_invokeinterface(patchref_t *pr)
 
        return true;
 }
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where