* Merged in twisti-branch.
[cacao.git] / src / vm / jit / i386 / patcher.c
index 386501ae77c9479548ea5937cc3493a7d1f9d888..93bc168faa87693c31c182dd53e436e0e3a0b135 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/i386/patcher.c - i386 code patching functions
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   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
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Christian Thalinger
-
-   Changes:
-
-   $Id: patcher.c 4530 2006-02-21 09:11:53Z twisti $
+   $Id: patcher.c 7571 2007-03-24 22:37:09Z twisti $
 
 */
 
 #include "config.h"
 #include "vm/types.h"
 
+#include "vm/jit/i386/codegen.h"
+
 #include "mm/memory.h"
+
 #include "native/native.h"
+
 #include "vm/builtin.h"
-#include "vm/class.h"
-#include "vm/field.h"
+#include "vm/exceptions.h"
 #include "vm/initialize.h"
-#include "vm/options.h"
-#include "vm/resolve.h"
-#include "vm/references.h"
+
 #include "vm/jit/patcher.h"
+#include "vm/jit/stacktrace.h"
 
+#include "vmcore/class.h"
+#include "vmcore/field.h"
+#include "vmcore/options.h"
+#include "vm/resolve.h"
+#include "vmcore/references.h"
 
-/* patcher_get_putstatic *******************************************************
 
-   Machine code:
+/* patcher_wrapper *************************************************************
 
-   <patched call position>
-   b8 00 00 00 00             mov    $0x00000000,%eax
+   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.
 
 *******************************************************************************/
 
-bool patcher_get_putstatic(u1 *sp)
+java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 {
-       u1                *ra;
+       stackframeinfo     sfi;
+       u1                *xpc;
        java_objectheader *o;
-       u8                 mcode;
-       unresolved_field  *uf;
-       fieldinfo         *fi;
+       functionptr        f;
+       bool               result;
+       java_objectheader *e;
+
+       /* define the patcher function */
+
+       bool (*patcher_function)(u1 *);
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 0 * 4));
+       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 */
 
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
+       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;
 
-       /* get the fieldinfo */
+       /* 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();
 
-       if (!(fi = resolve_field_eager(uf))) {
                PATCHER_MONITOREXIT;
 
-               return false;
+               return e;
        }
 
-       /* check if the field's class is initialized */
+       PATCHER_MARK_PATCHED_MONITOREXIT;
+
+       return NULL;
+}
 
-       if (!(fi->class->state & CLASS_INITIALIZED)) {
-               if (!initialize_class(fi->class)) {
-                       PATCHER_MONITOREXIT;
 
+/* patcher_get_putstatic *******************************************************
+
+   Machine code:
+
+   <patched call position>
+   b8 00 00 00 00             mov    $0x00000000,%eax
+
+*******************************************************************************/
+
+bool patcher_get_putstatic(u1 *sp)
+{
+       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));
+
+       /* get the fieldinfo */
+
+       if (!(fi = resolve_field_eager(uf)))
+               return false;
+
+       /* check if the field's class is initialized */
+
+       if (!(fi->class->state & CLASS_INITIALIZED))
+               if (!initialize_class(fi->class))
                        return false;
-               }
-       }
 
        /* patch back original code */
 
@@ -104,15 +166,13 @@ bool patcher_get_putstatic(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the field value's address */
 
        *((ptrint *) (ra + 1)) = (ptrint) &(fi->value);
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -128,33 +188,21 @@ bool patcher_get_putstatic(u1 *sp)
 
 bool patcher_getfield(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       unresolved_field  *uf;
-       fieldinfo         *fi;
+       u1               *ra;
+       u8                mcode;
+       unresolved_field *uf;
+       fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
+       mcode =                      *((u8 *)     (sp + 2 * 4));
+       uf    = (unresolved_field *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(fi = resolve_field_eager(uf))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(fi = resolve_field_eager(uf)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -163,7 +211,7 @@ bool patcher_getfield(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the field's offset */
@@ -175,8 +223,6 @@ bool patcher_getfield(u1 *sp)
        if (fi->type == TYPE_LNG)
                *((u4 *) (ra + 6 + 2)) = (u4) (fi->offset + 4);
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -192,33 +238,21 @@ bool patcher_getfield(u1 *sp)
 
 bool patcher_putfield(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       unresolved_field  *uf;
-       fieldinfo         *fi;
+       u1               *ra;
+       u8                mcode;
+       unresolved_field *uf;
+       fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
+       mcode =                      *((u8 *)     (sp + 2 * 4));
+       uf    = (unresolved_field *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(fi = resolve_field_eager(uf))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(fi = resolve_field_eager(uf)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -227,29 +261,25 @@ bool patcher_putfield(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the field's offset */
 
        if (fi->type != TYPE_LNG) {
                *((u4 *) (ra + 2)) = (u4) (fi->offset);
-
-       else {
-               /* long code is very special:
+       }
+       else {
+               /* The long code is special:
                 *
-                * 8b 8c 24 00 00 00 00       mov    0x00000000(%esp),%ecx
-                * 8b 94 24 00 00 00 00       mov    0x00000000(%esp),%edx
                 * 89 8d 00 00 00 00          mov    %ecx,0x00000000(%ebp)
                 * 89 95 00 00 00 00          mov    %edx,0x00000000(%ebp)
                 */
 
-               *((u4 *) (ra + 7 + 7 + 2)) = (u4) (fi->offset);
-               *((u4 *) (ra + 7 + 7 + 6 + 2)) = (u4) (fi->offset + 4);
+               *((u4 *) (ra + 2))     = (u4) (fi->offset);
+               *((u4 *) (ra + 6 + 2)) = (u4) (fi->offset + 4);
        }
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -265,33 +295,21 @@ bool patcher_putfield(u1 *sp)
 
 bool patcher_putfieldconst(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       unresolved_field  *uf;
-       fieldinfo         *fi;
+       u1               *ra;
+       u8                mcode;
+       unresolved_field *uf;
+       fieldinfo        *fi;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
+       mcode =                      *((u8 *)     (sp + 2 * 4));
+       uf    = (unresolved_field *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(fi = resolve_field_eager(uf))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(fi = resolve_field_eager(uf)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -300,27 +318,25 @@ bool patcher_putfieldconst(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the field's offset */
 
        if (!IS_2_WORD_TYPE(fi->type)) {
                *((u4 *) (ra + 2)) = (u4) (fi->offset);
-
-       else {
+       }
+       else {
                /* long/double code is different:
                 *
                 * c7 80 00 00 00 00 c8 01 00 00    movl   $0x1c8,0x0(%eax)
                 * c7 80 04 00 00 00 00 00 00 00    movl   $0x0,0x4(%eax)
                 */
 
-               *((u4 *) (ra + 2)) = (u4) (fi->offset);
+               *((u4 *) (ra + 2))      = (u4) (fi->offset);
                *((u4 *) (ra + 10 + 2)) = (u4) (fi->offset + 4);
        }
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -338,32 +354,20 @@ bool patcher_putfieldconst(u1 *sp)
 bool patcher_aconst(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
        /* get the classinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -372,15 +376,13 @@ bool patcher_aconst(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the classinfo pointer */
 
        *((ptrint *) (ra + 1)) = (ptrint) c;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -403,32 +405,20 @@ bool patcher_aconst(u1 *sp)
 bool patcher_builtin_multianewarray(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
        /* get the classinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -437,7 +427,7 @@ bool patcher_builtin_multianewarray(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the classinfo pointer */
@@ -446,9 +436,8 @@ bool patcher_builtin_multianewarray(u1 *sp)
 
        /* patch new function address */
 
-       *((ptrint *) (ra + 7 + 8 + 2 + 3 + 4 + 1)) = (ptrint) BUILTIN_multianewarray;
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+       *((ptrint *) (ra + 7 + 8 + 2 + 3 + 4 + 1)) =
+               (ptrint) BUILTIN_multianewarray;
 
        return true;
 }
@@ -468,32 +457,20 @@ bool patcher_builtin_multianewarray(u1 *sp)
 bool patcher_builtin_arraycheckcast(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
        /* get the classinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -502,7 +479,7 @@ bool patcher_builtin_arraycheckcast(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch the classinfo pointer */
@@ -513,8 +490,6 @@ bool patcher_builtin_arraycheckcast(u1 *sp)
 
        *((ptrint *) (ra + 8 + 1)) = (ptrint) BUILTIN_arraycheckcast;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -532,32 +507,20 @@ bool patcher_builtin_arraycheckcast(u1 *sp)
 bool patcher_invokestatic_special(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       um    = (unresolved_method *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       um    = (unresolved_method *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(m = resolve_method_eager(um))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(m = resolve_method_eager(um)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -566,15 +529,13 @@ bool patcher_invokestatic_special(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch stubroutine */
 
        *((ptrint *) (ra + 1)) = (ptrint) m->stubroutine;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -593,32 +554,20 @@ bool patcher_invokestatic_special(u1 *sp)
 bool patcher_invokevirtual(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       um    = (unresolved_method *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       um    = (unresolved_method *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(m = resolve_method_eager(um))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(m = resolve_method_eager(um)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -627,7 +576,7 @@ bool patcher_invokevirtual(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch vftbl index */
@@ -635,8 +584,6 @@ bool patcher_invokevirtual(u1 *sp)
        *((s4 *) (ra + 2 + 2)) = (s4) (OFFSET(vftbl_t, table[0]) +
                                                                   sizeof(methodptr) * m->vftblindex);
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -656,32 +603,20 @@ bool patcher_invokevirtual(u1 *sp)
 bool patcher_invokeinterface(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       um    = (unresolved_method *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       um    = (unresolved_method *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(m = resolve_method_eager(um))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(m = resolve_method_eager(um)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -690,7 +625,7 @@ bool patcher_invokeinterface(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch interfacetable index */
@@ -703,8 +638,6 @@ bool patcher_invokeinterface(u1 *sp)
        *((s4 *) (ra + 2 + 6 + 2)) =
                (s4) (sizeof(methodptr) * (m - m->class->methods));
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -721,32 +654,20 @@ bool patcher_invokeinterface(u1 *sp)
 bool patcher_checkcast_instanceof_flags(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -755,20 +676,18 @@ bool patcher_checkcast_instanceof_flags(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch class flags */
 
        *((s4 *) (ra + 1)) = (s4) c->flags;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
 
-/* patcher_checkcast_instanceof_interface **************************************
+/* patcher_checkcast_interface *************************************************
 
    Machine code:
 
@@ -776,40 +695,82 @@ 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(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
-       /* calculate and set the new return address */
+       /* get the fieldinfo */
 
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
+       if (!(c = resolve_classref_eager(cr)))
+               return false;
 
-       PATCHER_MONITORENTER;
+       /* patch back original code */
 
-       /* get the fieldinfo */
+       *((u4 *) (ra + 0)) = (u4) mcode;
+       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
+       /* if we show disassembly, we have to skip the nop's */
+
+       if (opt_shownops)
+               ra = ra + 5;
+
+       /* patch super class index */
+
+       *((s4 *) (ra + 6 + 2)) = (s4) c->index;
+
+       *((s4 *) (ra + 6 + 6 + 2 + 6 + 6 + 2)) =
+               (s4) (OFFSET(vftbl_t, interfacetable[0]) -
+                         c->index * sizeof(methodptr*));
+
+       return true;
+}
+
+
+/* patcher_instanceof_interface ************************************************
+
+   Machine code:
+
+   <patched call position>
+   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_instanceof_interface(u1 *sp)
+{
+       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));
 
+       /* get the fieldinfo */
+
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -818,7 +779,7 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch super class index */
@@ -829,8 +790,6 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
                (s4) (OFFSET(vftbl_t, interfacetable[0]) -
                          c->index * sizeof(methodptr*));
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -851,32 +810,20 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
 bool patcher_checkcast_class(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -885,7 +832,7 @@ bool patcher_checkcast_class(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch super class' vftbl */
@@ -893,8 +840,6 @@ bool patcher_checkcast_class(u1 *sp)
        *((ptrint *) (ra + 1)) = (ptrint) c->vftbl;
        *((ptrint *) (ra + 5 + 6 + 6 + 2 + 1)) = (ptrint) c->vftbl;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -914,32 +859,20 @@ bool patcher_checkcast_class(u1 *sp)
 bool patcher_instanceof_class(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 6 * 4));
+       mcode =                       *((u8 *)     (sp + 2 * 4));
+       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 4));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -948,15 +881,13 @@ bool patcher_instanceof_class(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch super class' vftbl */
 
        *((ptrint *) (ra + 1)) = (ptrint) c->vftbl;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -973,42 +904,27 @@ bool patcher_instanceof_class(u1 *sp)
 
 bool patcher_clinit(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       classinfo         *c;
+       u1        *ra;
+       u8         mcode;
+       classinfo *c;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       c     = (classinfo *)         *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       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)) {
-                       PATCHER_MONITOREXIT;
-
+       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);
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -1024,41 +940,26 @@ bool patcher_clinit(u1 *sp)
 #ifdef ENABLE_VERIFIER
 bool patcher_athrow_areturn(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       unresolved_class  *uc;
-       classinfo         *c;
+       u1               *ra;
+       u8                mcode;
+       unresolved_class *uc;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       uc    = (unresolved_class *)  *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
+       ra    = (u1 *)               *((ptrint *) (sp + 6 * 4));
+       mcode =                      *((u8 *)     (sp + 2 * 4));
+       uc    = (unresolved_class *) *((ptrint *) (sp + 1 * 4));
 
-       PATCHER_MONITORENTER;
-
-       /* resolve the class */
-
-       if (!resolve_class(uc, resolveEager, false, &c)) {
-               PATCHER_MONITOREXIT;
+       /* 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);
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 #endif /* ENABLE_VERIFIER */
@@ -1078,33 +979,21 @@ bool patcher_athrow_areturn(u1 *sp)
 #if !defined(WITH_STATIC_CLASSPATH)
 bool patcher_resolve_native(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       methodinfo        *m;
-       functionptr        f;
+       u1          *ra;
+       u8           mcode;
+       methodinfo  *m;
+       functionptr  f;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 4));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 4));
-       mcode =                       *((u8 *)     (sp + 1 * 4));
-       m     = (methodinfo *)        *((ptrint *) (sp + 0 * 4));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 4)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)         *((ptrint *) (sp + 6 * 4));
+       mcode =                *((u8 *)     (sp + 2 * 4));
+       m     = (methodinfo *) *((ptrint *) (sp + 1 * 4));
 
        /* resolve native function */
 
-       if (!(f = native_resolve_function(m))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(f = native_resolve_function(m)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -1113,15 +1002,13 @@ bool patcher_resolve_native(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
        /* patch native function pointer */
 
        *((ptrint *) (ra + 4)) = (ptrint) f;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 #endif /* !defined(WITH_STATIC_CLASSPATH) */