* Removed all Id tags.
[cacao.git] / src / vm / jit / x86_64 / patcher.c
index 812ca9e901d0ed29a58e77025213ef2c9040b8fc..4d6cfa39a2ea415f71b252eb20aed15cacfde8ed 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
 
-   Copyright (C) 1996-2005 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 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
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
-
-   Contact: cacao@complang.tuwien.ac.at
-
-   Authors: Christian Thalinger
-
-   Changes:
-
-   $Id: patcher.c 3463 2005-10-20 10:07:16Z edwin $
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
 */
 
 
 #include "config.h"
+
+#include <stdint.h>
+
 #include "vm/types.h"
 
+#include "vm/jit/x86_64/codegen.h"
+
 #include "mm/memory.h"
+
 #include "native/native.h"
+
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
-#include "vm/field.h"
 #include "vm/initialize.h"
-#include "vm/options.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 "vmcore/references.h"
+#include "vm/resolve.h"
 
-/* patcher_get_putstatic *******************************************************
 
-   Machine code:
+/* patcher_wrapper *************************************************************
 
-   <patched call position>
-   4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
-   49 8b 32                         mov    (%r10),%rsi
+   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_object_t *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       unresolved_field  *uf;
-       s4                 disp;
-       fieldinfo         *fi;
-       s4                 offset;
+       stackframeinfo     sfi;
+       u1                *xpc;
+       java_object_t     *o;
+       functionptr        f;
+       bool               result;
+       java_handle_t     *e;
+
+       /* define the patcher function */
+
+       bool (*patcher_function)(u1 *);
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 1 * 8));
-       disp  =                       *((s4 *)     (sp + 0 * 8));
+       xpc = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       o   = (java_object_t *)     *((ptrint *) (sp + 4 * 8));
+       f   = (functionptr)         *((ptrint *) (sp + 0 * 8));
 
        /* calculate and set the new return address */
 
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
+       xpc = xpc - PATCHER_CALL_SIZE;
+
+       *((ptrint *) (sp + 5 * 8)) = (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 + 6 * 8, 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;
 
-       if (!initialize_class(fi->class)) {
-               PATCHER_MONITOREXIT;
+       return NULL;
+}
+
+
+/* patcher_get_putstatic *******************************************************
+
+   Machine code:
+
+   <patched call position>
+   4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
+   49 8b 32                         mov    (%r10),%rsi
+
+*******************************************************************************/
+
+bool patcher_get_putstatic(u1 *sp)
+{
+       u1               *ra;
+       u8                mcode;
+       unresolved_field *uf;
+       s4                disp;
+       fieldinfo        *fi;
+
+       /* get stuff from the stack */
+
+       ra    = (u1 *)               *((ptrint *) (sp + 5 * 8));
+       mcode =                      *((u8 *)     (sp + 3 * 8));
+       uf    = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
+       disp  =                      *((s4 *)     (sp + 1 * 8));
 
+       /* 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,18 +169,12 @@ 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;
 
-       /* get RIP offset from machine instruction */
+       /* patch the field value's address */
 
-       offset = *((u4 *) (ra + 3));
-
-       /* patch the field value's address (+ 7: is the size of the RIP move) */
-
-       *((ptrint *) (ra + 7 + offset)) = (ptrint) &(fi->value);
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+       *((intptr_t *) (ra + 7 + disp)) = (intptr_t) fi->value;
 
        return true;
 }
@@ -132,34 +191,22 @@ bool patcher_get_putstatic(u1 *sp)
 
 bool patcher_get_putfield(u1 *sp)
 {
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       unresolved_field  *uf;
-       fieldinfo         *fi;
-       u1                 byte;
+       u1               *ra;
+       u8                mcode;
+       unresolved_field *uf;
+       fieldinfo        *fi;
+       u1                byte;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)               *((ptrint *) (sp + 5 * 8));
+       mcode =                      *((u8 *)     (sp + 3 * 8));
+       uf    = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(fi = resolve_field_eager(uf))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(fi = resolve_field_eager(uf)))
                return false;
-       }
 
        /* patch back original code (instruction code is smaller than 8 bytes) */
 
@@ -168,7 +215,7 @@ bool patcher_get_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: we check for the field type, because the     */
@@ -180,23 +227,21 @@ bool patcher_get_putfield(u1 *sp)
                byte = *(ra + 3);
 
                if (byte == 0x24)
-                       *((u4 *) (ra + 4)) = (u4) (fi->offset);
+                       *((int32_t *) (ra + 4)) = fi->offset;
                else
-                       *((u4 *) (ra + 3)) = (u4) (fi->offset);
-
-       else {
+                       *((int32_t *) (ra + 3)) = fi->offset;
+       }
+       else {
                /* check for special case: %rsp or %r12 as base register */
 
                byte = *(ra + 5);
 
                if (byte == 0x24)
-                       *((u4 *) (ra + 6)) = (u4) (fi->offset);
+                       *((int32_t *) (ra + 6)) = fi->offset;
                else
-                       *((u4 *) (ra + 5)) = (u4) (fi->offset);
+                       *((int32_t *) (ra + 5)) = fi->offset;
        }
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -212,33 +257,21 @@ bool patcher_get_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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       uf    = (unresolved_field *)  *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)               *((ptrint *) (sp + 5 * 8));
+       mcode =                      *((u8 *)     (sp + 3 * 8));
+       uf    = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(fi = resolve_field_eager(uf))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(fi = resolve_field_eager(uf)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -246,7 +279,7 @@ 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 */
@@ -255,158 +288,66 @@ bool patcher_putfieldconst(u1 *sp)
                /* handle special case when the base register is %r12 */
 
                if (*(ra + 2) == 0x84) {
-                       *((u4 *) (ra + 4)) = (u4) (fi->offset);
-                       *((u4 *) (ra + 12 + 4)) = (u4) (fi->offset + 4);
-
-               } else {
-                       *((u4 *) (ra + 3)) = (u4) (fi->offset);
-                       *((u4 *) (ra + 11 + 3)) = (u4) (fi->offset + 4);
+                       *((uint32_t *) (ra + 4))      = fi->offset;
+                       *((uint32_t *) (ra + 12 + 4)) = fi->offset + 4;
                }
-
-       } else {
+               else {
+                       *((uint32_t *) (ra + 3))      = fi->offset;
+                       *((uint32_t *) (ra + 11 + 3)) = fi->offset + 4;
+               }
+       }
+       else {
                /* handle special case when the base register is %r12 */
 
                if (*(ra + 2) == 0x84)
-                       *((u4 *) (ra + 4)) = (u4) (fi->offset);
+                       *((uint32_t *) (ra + 4)) = fi->offset;
                else
-                       *((u4 *) (ra + 3)) = (u4) (fi->offset);
+                       *((uint32_t *) (ra + 3)) = fi->offset;
        }
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
 
-/* patcher_builtin_new *********************************************************
+/* patcher_aconst **************************************************************
 
    Machine code:
 
-   48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
    <patched call position>
-   48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
-   48 ff d0                         callq  *%rax
-
-*******************************************************************************/
-
-bool patcher_builtin_new(u1 *sp)
-{
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
-
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - (10 + 5);
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
-
-       /* get the classinfo */
-
-       if (!(c = resolve_classref_eager_nonabstract(cr))) {
-               PATCHER_MONITOREXIT;
-
-               return false;
-       }
-
-       if (!initialize_class(c)) {
-               PATCHER_MONITOREXIT;
-
-               return false;
-       }
-
-       /* patch back original code */
-
-       *((u8 *) (ra + 10)) = mcode;
-
-       /* patch the classinfo pointer */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c;
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
-
-       /* patch new function address */
-
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_new;
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
-       return true;
-}
-
-
-/* patcher_builtin_newarray ****************************************************
-
-   Machine code:
-
-   48 be 88 13 9b 00 00 00 00 00    mov    $0x9b1388,%rsi
-   <patched call position>
-   48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
-   48 ff d0                         callq  *%rax
+   48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
 
 *******************************************************************************/
 
-bool patcher_builtin_newarray(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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - (10 + 5);
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the classinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
-       *((u8 *) (ra + 10)) = mcode;
-
-       /* patch the class' vftbl pointer */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
+       *((u8 *) ra) = mcode;
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
+       if (opt_shownops)
                ra = ra + 5;
 
-       /* patch new function address */
-
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_newarray;
+       /* patch the classinfo pointer */
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+       *((ptrint *) (ra + 2)) = (ptrint) c;
 
        return true;
 }
@@ -428,32 +369,20 @@ bool patcher_builtin_newarray(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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the classinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -461,18 +390,12 @@ 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 class' vftbl pointer */
-
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) c->vftbl;
-
-       /* patch new function address */
-
-       *((ptrint *) (ra + 10 + 10 + 3 + 2)) = (ptrint) BUILTIN_multianewarray;
+       /* patch the classinfo pointer */
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+       *((ptrint *) (ra + 10 + 2)) = (ptrint) c;
 
        return true;
 }
@@ -492,32 +415,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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the classinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -525,82 +436,12 @@ 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 class' vftbl pointer */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
-
-       /* patch new function address */
-
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_arraycheckcast;
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
-       return true;
-}
-
-
-/* patcher_builtin_arrayinstanceof *********************************************
-
-   Machine code:
-
-   48 be 30 3c b2 00 00 00 00 00    mov    $0xb23c30,%rsi
-   <patched call position>
-   48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
-   48 ff d0                         callq  *%rax
-
-*******************************************************************************/
-
-bool patcher_builtin_arrayinstanceof(u1 *sp)
-{
-       u1                *ra;
-       java_objectheader *o;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
-
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - (10 + 5);
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
-
-       /* get the classinfo */
-
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
-               return false;
-       }
-
-       /* patch back original code */
-
-       *((u8 *) (ra + 10)) = mcode;
-
-       /* patch the class' vftbl pointer */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_showdisassemble)
-               ra = ra + 5;
-
-       /* patch new function address */
-
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) BUILTIN_arrayinstanceof;
+       /* patch the classinfo pointer */
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+       *((ptrint *) (ra + 2)) = (ptrint) c;
 
        return true;
 }
@@ -619,46 +460,36 @@ bool patcher_builtin_arrayinstanceof(u1 *sp)
 bool patcher_invokestatic_special(u1 *sp)
 {
        u1                *ra;
-       java_objectheader *o;
        u8                 mcode;
        unresolved_method *um;
+       s4                 disp;
        methodinfo        *m;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 4 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       um    = (unresolved_method *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
+       disp  =                       *((s4 *)     (sp + 1 * 8));
 
        /* get the fieldinfo */
 
-       if (!(m = resolve_method_eager(um))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(m = resolve_method_eager(um)))
                return false;
-       }
+
        /* patch back original code */
 
        *((u8 *) ra) = mcode;
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       if (opt_shownops)
+               ra = ra + PATCHER_CALL_SIZE;
 
        /* patch stubroutine */
 
-       *((ptrint *) (ra + 2)) = (ptrint) m->stubroutine;
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+/*     *((ptrint *) (ra + 2)) = (ptrint) m->stubroutine; */
+       *((ptrint *) (ra + 7 + disp)) = (ptrint) m->stubroutine;
 
        return true;
 }
@@ -678,32 +509,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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       um    = (unresolved_method *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(m = resolve_method_eager(um))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(m = resolve_method_eager(um)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -711,7 +530,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 */
@@ -719,8 +538,6 @@ bool patcher_invokevirtual(u1 *sp)
        *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, table[0]) +
                                                                   sizeof(methodptr) * m->vftblindex);
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -740,32 +557,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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       um    = (unresolved_method *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(m = resolve_method_eager(um))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(m = resolve_method_eager(um)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -773,7 +578,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 */
@@ -786,8 +591,6 @@ bool patcher_invokeinterface(u1 *sp)
        *((s4 *) (ra + 3 + 7 + 3)) =
                (s4) (sizeof(methodptr) * (m - m->class->methods));
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -806,32 +609,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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -839,61 +630,47 @@ 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 + 2)) = (s4) c->flags;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
 
-/* patcher_checkcast_instanceof_interface **************************************
+/* patcher_checkcast_interface *************************************************
 
    Machine code:
 
    <patched call position>
    45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
-   49 81 eb 00 00 00 00             sub    $0x0,%r11
-   4d 85 db                         test   %r11,%r11
-   0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
+   41 81 fb 00 00 00 00             cmp    $0x0,%r11d
+   0f 8f 08 00 00 00                jg     0x00002aaaaae511d5
+   48 8b 0c 25 03 00 00 00          mov    0x3,%rcx
    4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
 
 *******************************************************************************/
 
-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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -901,19 +678,17 @@ bool patcher_checkcast_instanceof_interface(u1 *sp)
 
        /* if we show disassembly, we have to skip the nop's */
 
-       if (opt_showdisassemble)
-               ra = ra + 5;
+       if (opt_shownops)
+               ra = ra + PATCHER_CALL_SIZE;
 
        /* patch super class index */
 
        *((s4 *) (ra + 7 + 3)) = (s4) c->index;
 
-       *((s4 *) (ra + 7 + 7 + 3 + 6 + 3)) =
+       *((s4 *) (ra + 7 + 7 + 6 + 8 + 3)) =
                (s4) (OFFSET(vftbl_t, interfacetable[0]) -
                          c->index * sizeof(methodptr*));
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -934,32 +709,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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -967,7 +730,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 */
@@ -975,7 +738,56 @@ bool patcher_checkcast_class(u1 *sp)
        *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
        *((ptrint *) (ra + 10 + 7 + 7 + 3 + 2)) = (ptrint) c->vftbl;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+       return true;
+}
+
+
+/* patcher_instanceof_interface ************************************************
+
+   Machine code:
+
+   <patched call position>
+   45 8b 9a 1c 00 00 00             mov    0x1c(%r10),%r11d
+   41 81 fb 00 00 00 00             cmp    $0x0,%r11d
+   0f 8e 94 04 00 00                jle    0x00002aaaaab018f8
+   4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
+
+*******************************************************************************/
+
+bool patcher_instanceof_interface(u1 *sp)
+{
+       u1                *ra;
+       u8                 mcode;
+       constant_classref *cr;
+       classinfo         *c;
+
+       /* get stuff from the stack */
+
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
+
+       /* get the fieldinfo */
+
+       if (!(c = resolve_classref_eager(cr)))
+               return false;
+
+       /* patch back original code */
+
+       *((u8 *) ra) = mcode;
+
+       /* if we show disassembly, we have to skip the nop's */
+
+       if (opt_shownops)
+               ra = ra + PATCHER_CALL_SIZE;
+
+       /* patch super class index */
+
+       *((s4 *) (ra + 7 + 3)) = (s4) c->index;
+
+       *((s4 *) (ra + 7 + 7 + 6 + 3)) =
+               (s4) (OFFSET(vftbl_t, interfacetable[0]) -
+                         c->index * sizeof(methodptr*));
 
        return true;
 }
@@ -993,32 +805,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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
+       mcode =                       *((u8 *)     (sp + 3 * 8));
+       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
 
        /* get the fieldinfo */
 
-       if (!(c = resolve_classref_eager(cr))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(c = resolve_classref_eager(cr)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -1026,15 +826,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 + 2)) = (ptrint) c->vftbl;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
 
@@ -1053,41 +851,63 @@ 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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       c     = (classinfo *)         *((ptrint *) (sp + 1 * 8));
+       ra    = (u1 *)        *((ptrint *) (sp + 5 * 8));
+       mcode =               *((u8 *)     (sp + 3 * 8));
+       c     = (classinfo *) *((ptrint *) (sp + 2 * 8));
 
-       /* calculate and set the new return address */
+       /* check if the class is initialized */
 
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
+       if (!(c->state & CLASS_INITIALIZED))
+               if (!initialize_class(c))
+                       return false;
 
-       PATCHER_MONITORENTER;
+       /* patch back original code */
 
-       /* check if the class is initialized */
+       *((u8 *) ra) = mcode;
 
-       if (!initialize_class(c)) {
-               PATCHER_MONITOREXIT;
+       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 + 5 * 8));
+       mcode =                      *((u8 *)     (sp + 3 * 8));
+       uc    = (unresolved_class *) *((ptrint *) (sp + 2 * 8));
+
+       /* resolve the class and check subtype constraints */
+
+       if (!resolve_class_eager_no_access_check(uc))
                return false;
-       }
 
        /* patch back original code */
 
        *((u8 *) ra) = mcode;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
+#endif /* ENABLE_VERIFIER */
 
 
 /* patcher_resolve_native ******************************************************
@@ -1100,36 +920,24 @@ bool patcher_clinit(u1 *sp)
 
 *******************************************************************************/
 
-#if !defined(ENABLE_STATICVM)
+#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 * 8));
-       o     = (java_objectheader *) *((ptrint *) (sp + 3 * 8));
-       mcode =                       *((u8 *)     (sp + 2 * 8));
-       m     = (methodinfo *)        *((ptrint *) (sp + 1 * 8));
-
-       /* calculate and set the new return address */
-
-       ra = ra - 5;
-       *((ptrint *) (sp + 4 * 8)) = (ptrint) ra;
-
-       PATCHER_MONITORENTER;
+       ra    = (u1 *)         *((ptrint *) (sp + 5 * 8));
+       mcode =                *((u8 *)     (sp + 3 * 8));
+       m     = (methodinfo *) *((ptrint *) (sp + 2 * 8));
 
        /* resolve native function */
 
-       if (!(f = native_resolve_function(m))) {
-               PATCHER_MONITOREXIT;
-
+       if (!(f = native_resolve_function(m)))
                return false;
-       }
 
        /* patch back original code */
 
@@ -1137,18 +945,16 @@ 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 + 2)) = (ptrint) f;
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
        return true;
 }
-#endif /* !defined(ENABLE_STATICVM) */
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
 /*