Merged trunk and subtype.
[cacao.git] / src / vm / jit / i386 / patcher.c
index 59332b6b1d1aaa34c188e7dc7a9bb6e25b6b7631..034ca21ef2239421c87fcad37a950f382a7e9550 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/i386/patcher.c - i386 code patching 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.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: patcher.c 7486 2007-03-08 13:50:07Z twisti $
-
 */
 
 
 #include "config.h"
+
+#include <stdint.h>
+
 #include "vm/types.h"
 
 #include "vm/jit/i386/codegen.h"
+#include "vm/jit/i386/md.h"
 
 #include "mm/memory.h"
 
 #include "native/native.h"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/class.h"
+#include "vm/field.hpp"
 #include "vm/initialize.h"
+#include "vm/options.h"
+#include "vm/references.h"
+#include "vm/resolve.h"
 
-#include "vm/jit/patcher.h"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/patcher-common.hpp"
 
-#include "vmcore/class.h"
-#include "vmcore/field.h"
-#include "vmcore/options.h"
-#include "vm/resolve.h"
-#include "vmcore/references.h"
 
+#define PATCH_BACK_ORIGINAL_MCODE                                                      \
+       do {                                                                                                    \
+               *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;          \
+               md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);     \
+       } while (0)
 
-/* patcher_wrapper *************************************************************
 
-   Wrapper for all patchers.  It also creates the stackframe info
-   structure.
+/* patcher_patch_code **********************************************************
 
-   If the return value of the patcher function is false, it gets the
-   exception object, clears the exception pointer and returns the
-   exception.
+   Just patches back the original machine code.
 
 *******************************************************************************/
 
-java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
+void patcher_patch_code(patchref_t *pr)
 {
-       stackframeinfo     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));
-       o   = (java_objectheader *) *((ptrint *) (sp + 4 * 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_create_extern_stackframeinfo for
-          md_codegen_get_pv_from_pc. */
-
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp + 7 * 4, xpc, xpc);
-
-       /* call the proper patcher function */
-
-       result = (patcher_function)(sp);
-
-       /* remove the stackframeinfo */
-
-       stacktrace_remove_stackframeinfo(&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;
+       PATCH_BACK_ORIGINAL_MCODE;
 }
 
 
@@ -135,18 +75,16 @@ java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 
 *******************************************************************************/
 
-bool patcher_get_putstatic(u1 *sp)
+bool patcher_get_putstatic(patchref_t *pr)
 {
        u1               *ra;
-       u8                mcode;
        unresolved_field *uf;
        fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
-       mcode =                      *((u8 *)     (sp + 2 * 4));
-       uf    = (unresolved_field *) *((ptrint *) (sp + 1 * 4));
+       ra    = (u1 *)               pr->mpc;
+       uf    = (unresolved_field *) pr->ref;
 
        /* get the fieldinfo */
 
@@ -155,23 +93,15 @@ bool patcher_get_putstatic(u1 *sp)
 
        /* 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the field value's address */
 
-       *((ptrint *) (ra + 1)) = (ptrint) &(fi->value);
+       *((intptr_t *) (ra + 1)) = (intptr_t) fi->value;
 
        return true;
 }
@@ -186,33 +116,23 @@ bool patcher_get_putstatic(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_getfield(u1 *sp)
+bool patcher_getfield(patchref_t *pr)
 {
        u1               *ra;
-       u8                mcode;
        unresolved_field *uf;
        fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
-       mcode =                      *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the field's offset */
 
@@ -236,33 +156,23 @@ bool patcher_getfield(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_putfield(u1 *sp)
+bool patcher_putfield(patchref_t *pr)
 {
        u1               *ra;
-       u8                mcode;
        unresolved_field *uf;
        fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
-       mcode =                      *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the field's offset */
 
@@ -293,33 +203,23 @@ bool patcher_putfield(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_putfieldconst(u1 *sp)
+bool patcher_putfieldconst(patchref_t *pr)
 {
        u1               *ra;
-       u8                mcode;
        unresolved_field *uf;
        fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
-       mcode =                      *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the field's offset */
 
@@ -351,33 +251,23 @@ bool patcher_putfieldconst(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_aconst(u1 *sp)
+bool patcher_aconst(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
+       ra    = (u1 *)                pr->mpc;
+       cr    = (constant_classref *) pr->ref;
 
        /* get the classinfo */
 
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch back original code */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the classinfo pointer */
 
@@ -402,43 +292,28 @@ bool patcher_aconst(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_builtin_multianewarray(u1 *sp)
+bool patcher_builtin_multianewarray(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
+       ra    = (u1 *)                pr->mpc;
+       cr    = (constant_classref *) pr->ref;
 
        /* get the classinfo */
 
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch back original code */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the classinfo pointer */
 
        *((ptrint *) (ra + 7 + 4)) = (ptrint) c;
 
-       /* patch new function address */
-
-       *((ptrint *) (ra + 7 + 8 + 2 + 3 + 4 + 1)) =
-               (ptrint) BUILTIN_multianewarray;
-
        return true;
 }
 
@@ -454,33 +329,23 @@ bool patcher_builtin_multianewarray(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_builtin_arraycheckcast(u1 *sp)
+bool patcher_builtin_arraycheckcast(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
+       ra    = (u1 *)                pr->mpc;
+       cr    = (constant_classref *) pr->ref;
 
        /* get the classinfo */
 
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch back original code */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch the classinfo pointer */
 
@@ -504,33 +369,23 @@ bool patcher_builtin_arraycheckcast(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_invokestatic_special(u1 *sp)
+bool patcher_invokestatic_special(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch stubroutine */
 
@@ -551,33 +406,23 @@ bool patcher_invokestatic_special(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_invokevirtual(u1 *sp)
+bool patcher_invokevirtual(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch vftbl index */
 
@@ -600,43 +445,33 @@ bool patcher_invokevirtual(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_invokeinterface(u1 *sp)
+bool patcher_invokeinterface(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch interfacetable index */
 
        *((s4 *) (ra + 2 + 2)) = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
-                                                                  sizeof(methodptr) * m->class->index);
+                                                                  sizeof(methodptr) * m->clazz->index);
 
        /* patch method offset */
 
        *((s4 *) (ra + 2 + 6 + 2)) =
-               (s4) (sizeof(methodptr) * (m - m->class->methods));
+               (s4) (sizeof(methodptr) * (m - m->clazz->methods));
 
        return true;
 }
@@ -651,33 +486,23 @@ bool patcher_invokeinterface(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_checkcast_instanceof_flags(u1 *sp)
+bool patcher_checkcast_instanceof_flags(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch class flags */
 
@@ -687,7 +512,7 @@ bool patcher_checkcast_instanceof_flags(u1 *sp)
 }
 
 
-/* patcher_checkcast_instanceof_interface **************************************
+/* patcher_checkcast_interface *************************************************
 
    Machine code:
 
@@ -695,44 +520,35 @@ bool patcher_checkcast_instanceof_flags(u1 *sp)
    8b 91 00 00 00 00          mov    0x00000000(%ecx),%edx
    81 ea 00 00 00 00          sub    $0x00000000,%edx
    85 d2                      test   %edx,%edx
-   0f 8e 00 00 00 00          jle    0x00000000
+   0f 8f 06 00 00 00          jg     0x00000000
+   8b 35 03 00 00 00          mov    0x3,%esi
    8b 91 00 00 00 00          mov    0x00000000(%ecx),%edx
 
 *******************************************************************************/
 
-bool patcher_checkcast_instanceof_interface(u1 *sp)
+bool patcher_checkcast_interface(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch super class index */
 
        *((s4 *) (ra + 6 + 2)) = (s4) c->index;
 
-       *((s4 *) (ra + 6 + 6 + 2 + 6 + 2)) =
+       *((s4 *) (ra + 6 + 6 + 2 + 6 + 6 + 2)) =
                (s4) (OFFSET(vftbl_t, interfacetable[0]) -
                          c->index * sizeof(methodptr*));
 
@@ -740,95 +556,79 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
 }
 
 
-/* patcher_checkcast_class *****************************************************
+/* patcher_instanceof_interface ************************************************
 
    Machine code:
 
    <patched call position>
-   ba 00 00 00 00             mov    $0x00000000,%edx
-   8b 89 00 00 00 00          mov    0x00000000(%ecx),%ecx
-   8b 92 00 00 00 00          mov    0x00000000(%edx),%edx
-   29 d1                      sub    %edx,%ecx
-   ba 00 00 00 00             mov    $0x00000000,%edx
+   8b 91 00 00 00 00          mov    0x00000000(%ecx),%edx
+   81 ea 00 00 00 00          sub    $0x00000000,%edx
+   85 d2                      test   %edx,%edx
+   0f 8e 13 00 00 00          jle    0x00000000
+   8b 91 00 00 00 00          mov    0x00000000(%ecx),%edx
 
 *******************************************************************************/
 
-bool patcher_checkcast_class(u1 *sp)
+bool patcher_instanceof_interface(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       /* patch super class index */
 
-       /* patch super class' vftbl */
+       *((s4 *) (ra + 6 + 2)) = (s4) c->index;
 
-       *((ptrint *) (ra + 1)) = (ptrint) c->vftbl;
-       *((ptrint *) (ra + 5 + 6 + 6 + 2 + 1)) = (ptrint) c->vftbl;
+       *((s4 *) (ra + 6 + 6 + 2 + 6 + 2)) =
+               (s4) (OFFSET(vftbl_t, interfacetable[0]) -
+                         c->index * sizeof(methodptr*));
 
        return true;
 }
 
 
-/* patcher_instanceof_class ****************************************************
+/* patcher_checkcast_class *****************************************************
 
    Machine code:
 
    <patched call position>
-   b9 00 00 00 00             mov    $0x0,%ecx
-   8b 40 14                   mov    0x14(%eax),%eax
-   8b 51 18                   mov    0x18(%ecx),%edx
-   8b 49 14                   mov    0x14(%ecx),%ecx
+   ba 00 00 00 00             mov    $0x00000000,%edx
+   8b 89 00 00 00 00          mov    0x00000000(%ecx),%ecx
+   8b 92 00 00 00 00          mov    0x00000000(%edx),%edx
+   29 d1                      sub    %edx,%ecx
+   ba 00 00 00 00             mov    $0x00000000,%edx
 
 *******************************************************************************/
 
-bool patcher_instanceof_class(u1 *sp)
+bool patcher_checkcast_class(patchref_t *pr)
 {
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
-       mcode =                       *((u8 *)     (sp + 2 * 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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch super class' vftbl */
 
@@ -838,126 +638,42 @@ bool patcher_instanceof_class(u1 *sp)
 }
 
 
-/* patcher_clinit **************************************************************
-
-   Is used int PUT/GETSTATIC and native stub.
-
-   Machine code:
-
-   <patched call position>
-
-*******************************************************************************/
-
-bool patcher_clinit(u1 *sp)
-{
-       u1        *ra;
-       u8         mcode;
-       classinfo *c;
-
-       /* get stuff from the stack */
-
-       ra    = (u1 *)        *((ptrint *) (sp + 6 * 4));
-       mcode =               *((u8 *)     (sp + 2 * 4));
-       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 */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       return true;
-}
-
-
-/* patcher_athrow_areturn ******************************************************
-
-   Machine code:
-
-   <patched call position>
-
-*******************************************************************************/
-
-#ifdef ENABLE_VERIFIER
-bool patcher_athrow_areturn(u1 *sp)
-{
-       u1               *ra;
-       u8                mcode;
-       unresolved_class *uc;
-
-       /* get stuff from the stack */
-
-       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
-       mcode =                      *((u8 *)     (sp + 2 * 4));
-       uc    = (unresolved_class *) *((ptrint *) (sp + 1 * 4));
-
-       /* resolve the class and check subtype constraints */
-
-       if (!resolve_class_eager_no_access_check(uc))
-               return false;
-
-       /* patch back original code */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       return true;
-}
-#endif /* ENABLE_VERIFIER */
-
-
-/* patcher_resolve_native ******************************************************
-
-   Is used in native stub.
+/* patcher_instanceof_class ****************************************************
 
    Machine code:
 
    <patched call position>
-   c7 44 24 04 28 90 01 40    movl   $0x40019028,0x4(%esp)
+   b9 00 00 00 00             mov    $0x0,%ecx
+   8b 40 14                   mov    0x14(%eax),%eax
+   8b 51 18                   mov    0x18(%ecx),%edx
+   8b 49 14                   mov    0x14(%ecx),%ecx
 
 *******************************************************************************/
 
-#if !defined(WITH_STATIC_CLASSPATH)
-bool patcher_resolve_native(u1 *sp)
+bool patcher_instanceof_class(patchref_t *pr)
 {
-       u1          *ra;
-       u8           mcode;
-       methodinfo  *m;
-       functionptr  f;
+       u1                *ra;
+       constant_classref *cr;
+       classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)         *((ptrint *) (sp + 6 * 4));
-       mcode =                *((u8 *)     (sp + 2 * 4));
-       m     = (methodinfo *) *((ptrint *) (sp + 1 * 4));
+       ra    = (u1 *)                pr->mpc;
+       cr    = (constant_classref *) pr->ref;
 
-       /* resolve native function */
+       /* get the fieldinfo */
 
-       if (!(f = native_resolve_function(m)))
+       if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
-
-       /* patch native function pointer */
+       /* patch super class' vftbl */
 
-       *((ptrint *) (ra + 4)) = (ptrint) f;
+       *((ptrint *) (ra + 1)) = (ptrint) c->vftbl;
 
        return true;
 }
-#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
 /*