* src/vm/jit/patcher-common.cpp: Conditionally restored NOP-insertion at
[cacao.git] / src / vm / jit / x86_64 / patcher.c
index ef1551844c5ff95f16829d8d2c31cf1c1b87b1b7..81cc09e0e8bbc78583471327af0d14c9203634c0 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/x86_64/patcher.c - x86_64 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, 2009
+   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 7596 2007-03-28 21:05:53Z twisti $
-
 */
 
 
 #include "config.h"
-#include "vm/types.h"
 
-#include "vm/jit/x86_64/codegen.h"
+#include <stdint.h>
 
-#include "mm/memory.h"
+#include "vm/types.h"
 
-#include "native/native.h"
+#include "vm/jit/x86_64/codegen.h"
+#include "vm/jit/x86_64/md.h"
 
-#include "vm/builtin.h"
-#include "vm/exceptions.h"
-#include "vm/initialize.h"
+#include "mm/memory.hpp"
 
-#include "vm/jit/patcher.h"
-#include "vm/jit/stacktrace.h"
+#include "native/native.hpp"
 
-#include "vmcore/class.h"
-#include "vmcore/field.h"
-#include "vmcore/options.h"
-#include "vmcore/references.h"
-#include "vm/resolve.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/class.hpp"
+#include "vm/field.hpp"
+#include "vm/initialize.hpp"
+#include "vm/options.h"
+#include "vm/references.h"
+#include "vm/resolve.hpp"
 
+#include "vm/jit/patcher-common.hpp"
 
-/* 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 + 5 * 8));
-       o   = (java_objectheader *) *((ptrint *) (sp + 4 * 8));
-       f   = (functionptr)         *((ptrint *) (sp + 0 * 8));
-
-       /* calculate and set the new return address */
-
-       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;
-
-       /* 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);
+       *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;
+       md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
+}
 
-       /* check for return value and exit accordingly */
+/**
+ * Check if the trap instruction at the given PC is valid.
+ *
+ * @param pc Program counter.
+ *
+ * @return true if valid, false otherwise.
+ */
+bool patcher_is_valid_trap_instruction_at(void* pc)
+{
+       uint16_t mcode = *((uint16_t*) pc);
 
-       if (result == false) {
-               e = exceptions_get_and_clear_exception();
+       // Check for the undefined instruction we use.
+       return (mcode == 0x0b0f);
+}
 
-               PATCHER_MONITOREXIT;
 
-               return e;
-       }
+/* patcher_resolve_classref_to_classinfo ***************************************
 
-       PATCHER_MARK_PATCHED_MONITOREXIT;
+   ACONST:
 
-       return NULL;
-}
+   <patched call position>
+   48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
 
+   MULTIANEWARRAY:
 
-/* patcher_get_putstatic *******************************************************
+   <patched call position>
+   48 be 30 40 b2 00 00 00 00 00    mov    $0xb24030,%rsi
+   48 89 e2                         mov    %rsp,%rdx
+   48 b8 7c 96 4b 00 00 00 00 00    mov    $0x4b967c,%rax
+   48 ff d0                         callq  *%rax
 
-   Machine code:
+   ARRAYCHECKCAST:
 
    <patched call position>
-   4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
-   49 8b 32                         mov    (%r10),%rsi
+   48 be b8 3f b2 00 00 00 00 00    mov    $0xb23fb8,%rsi
+   48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
+   48 ff d0                         callq  *%rax
 
 *******************************************************************************/
 
-bool patcher_get_putstatic(u1 *sp)
+bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
 {
-       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));
+       constant_classref* cr    = (constant_classref*) pr->ref;
+       uintptr_t*         datap = (uintptr_t*)         pr->datap;
 
-       /* get the fieldinfo */
+       // Resolve the class.
+       classinfo* c = resolve_classref_eager(cr);
 
-       if (!(fi = resolve_field_eager(uf)))
+       if (c == NULL)
                return false;
 
-       /* check if the field's class is initialized */
+       // Patch the class.
+       *datap = (uintptr_t) c;
 
-       if (!(fi->class->state & CLASS_INITIALIZED))
-               if (!initialize_class(fi->class))
-                       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 + 5;
+       // Synchronize data cache.
+       md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
 
-       /* patch the field value's address */
-
-       *((ptrint *) (ra + 7 + disp)) = (ptrint) &(fi->value);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
 
 
-/* patcher_get_putfield ********************************************************
+/* patcher_resolve_classref_to_vftbl *******************************************
 
-   Machine code:
+   CHECKCAST (class):
+   INSTANCEOF (class):
 
    <patched call position>
-   45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
 
 *******************************************************************************/
 
-bool patcher_get_putfield(u1 *sp)
+bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
 {
-       u1               *ra;
-       u8                mcode;
-       unresolved_field *uf;
-       fieldinfo        *fi;
-       u1                byte;
+       constant_classref* cr    = (constant_classref*) pr->ref;
+       uintptr_t*         datap = (uintptr_t*)         pr->datap;
 
-       /* get stuff from the stack */
+       // Resolve the field.
+       classinfo* c = resolve_classref_eager(cr);
 
-       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)))
+       if (c == NULL)
                return false;
 
-       /* patch back original code (instruction code is smaller than 8 bytes) */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
+       // Patch super class' vftbl.
+       *datap = (uintptr_t) c->vftbl;
 
-       /* if we show disassembly, we have to skip the nop's */
+       // Synchronize data cache.
+       md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
 
-       if (opt_shownops)
-               ra = ra + 5;
-
-       /* patch the field's offset: we check for the field type, because the     */
-       /* instructions have different lengths                                    */
-
-       if (IS_INT_LNG_TYPE(fi->type)) {
-               /* check for special case: %rsp or %r12 as base register */
-
-               byte = *(ra + 3);
-
-               if (byte == 0x24)
-                       *((u4 *) (ra + 4)) = (u4) (fi->offset);
-               else
-                       *((u4 *) (ra + 3)) = (u4) (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);
-               else
-                       *((u4 *) (ra + 5)) = (u4) (fi->offset);
-       }
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
 
 
-/* patcher_putfieldconst *******************************************************
+/* patcher_resolve_classref_to_flags *******************************************
 
-   Machine code:
+   CHECKCAST/INSTANCEOF:
 
    <patched call position>
-   41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
 
 *******************************************************************************/
 
-bool patcher_putfieldconst(u1 *sp)
+bool patcher_resolve_classref_to_flags(patchref_t *pr)
 {
-       u1               *ra;
-       u8                mcode;
-       unresolved_field *uf;
-       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));
+       constant_classref* cr    = (constant_classref*) pr->ref;
+/*     int32_t*           datap = (int32_t*)           pr->datap; */
+       uint8_t*           ra    = (uint8_t*)           pr->mpc;
 
-       /* get the fieldinfo */
+       // Resolve the field.
+       classinfo* c = resolve_classref_eager(cr);
 
-       if (!(fi = resolve_field_eager(uf)))
+       if (c == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
+       ra += PATCHER_CALL_SIZE;
 
-       if (opt_shownops)
-               ra = ra + 5;
+       // Patch class flags.
+/*     *datap = c->flags; */
+       *((int32_t*) (ra + 2)) = c->flags;
 
-       /* patch the field's offset */
-
-       if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
-               /* 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);
-               }
-       }
-       else {
-               /* handle special case when the base register is %r12 */
+       // Synchronize data cache.
+/*     md_dcacheflush(datap, sizeof(int32_t)); */
+       md_icacheflush(ra + 2, sizeof(int32_t));
 
-               if (*(ra + 2) == 0x84)
-                       *((u4 *) (ra + 4)) = (u4) (fi->offset);
-               else
-                       *((u4 *) (ra + 3)) = (u4) (fi->offset);
-       }
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
 
 
-/* patcher_aconst **************************************************************
+/* patcher_get_putstatic *******************************************************
 
    Machine code:
 
    <patched call position>
-   48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
+   4d 8b 15 86 fe ff ff             mov    -378(%rip),%r10
+   49 8b 32                         mov    (%r10),%rsi
 
 *******************************************************************************/
 
-bool patcher_aconst(u1 *sp)
+bool patcher_get_putstatic(patchref_t *pr)
 {
-       u1                *ra;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
+       unresolved_field* uf    = (unresolved_field*) pr->ref;
+       uintptr_t*        datap = (uintptr_t*)        pr->datap;
+       uint8_t*          ra    = (uint8_t*)          pr->mpc;
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
+       // Resolve the field.
+       fieldinfo* fi = resolve_field_eager(uf);
 
-       /* get the classinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
+       if (fi == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
+       ra += PATCHER_CALL_SIZE;
 
-       /* if we show disassembly, we have to skip the nop's */
+       // Check if the field's class is initialized/
+       if (!(fi->clazz->state & CLASS_INITIALIZED))
+               if (!initialize_class(fi->clazz))
+                       return false;
 
-       if (opt_shownops)
-               ra = ra + 5;
+       // Patch the field value's address.
+       *datap = (uintptr_t) fi->value;
 
-       /* patch the classinfo pointer */
+       // Synchronize data cache.
+       md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
 
-       *((ptrint *) (ra + 2)) = (ptrint) c;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
 
 
-/* patcher_builtin_multianewarray **********************************************
+/* patcher_get_putfield ********************************************************
 
    Machine code:
 
    <patched call position>
-   48 bf 02 00 00 00 00 00 00 00    mov    $0x2,%rdi
-   48 be 30 40 b2 00 00 00 00 00    mov    $0xb24030,%rsi
-   48 89 e2                         mov    %rsp,%rdx
-   48 b8 7c 96 4b 00 00 00 00 00    mov    $0x4b967c,%rax
-   48 ff d0                         callq  *%rax
+   45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
 
 *******************************************************************************/
 
-bool patcher_builtin_multianewarray(u1 *sp)
+bool patcher_get_putfield(patchref_t *pr)
 {
-       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));
+       uint8_t*          pc = (uint8_t*)          pr->mpc;
+       unresolved_field* uf = (unresolved_field*) pr->ref;
 
-       /* get the classinfo */
+       // Resolve the field.
+       fieldinfo* fi = resolve_field_eager(uf);
 
-       if (!(c = resolve_classref_eager(cr)))
+       if (fi == NULL)
                return false;
 
-       /* patch back original code */
+       pc += PATCHER_CALL_SIZE;
 
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_shownops)
-               ra = ra + 5;
+       // Patch the field's offset: we check for the field type, because
+       // the instructions have different lengths.
+       if (IS_INT_LNG_TYPE(fi->type)) {
+               // Check for special case: %rsp or %r12 as base register.
+               if (pc[3] == 0x24)
+                       *((int32_t*) (pc + 4)) = fi->offset;
+               else
+                       *((int32_t*) (pc + 3)) = fi->offset;
+       }
+       else {
+               // Check for special case: %rsp or %r12 as base register.
+               if (pc[5] == 0x24)
+                       *((int32_t*) (pc + 6)) = fi->offset;
+               else
+                       *((int32_t*) (pc + 5)) = fi->offset;
+       }
 
-       /* patch the classinfo pointer */
+       // Synchronize instruction cache.
+       md_icacheflush(pc, 6 + sizeof(int32_t));
 
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) c;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
 
 
-/* patcher_builtin_arraycheckcast **********************************************
+/* patcher_putfieldconst *******************************************************
 
    Machine code:
 
    <patched call position>
-   48 be b8 3f b2 00 00 00 00 00    mov    $0xb23fb8,%rsi
-   48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
-   48 ff d0                         callq  *%rax
+   41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
 
 *******************************************************************************/
 
-bool patcher_builtin_arraycheckcast(u1 *sp)
+bool patcher_putfieldconst(patchref_t *pr)
 {
-       u1                *ra;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
+       uint8_t*          pc = (uint8_t*)          pr->mpc;
+       unresolved_field* uf = (unresolved_field*) pr->ref;
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
+       // Resolve the field.
+       fieldinfo* fi = resolve_field_eager(uf);
 
-       /* get the classinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
+       if (fi == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
+       pc += PATCHER_CALL_SIZE;
 
-       /* if we show disassembly, we have to skip the nop's */
-
-       if (opt_shownops)
-               ra = ra + 5;
+       // Patch the field's offset.
+       if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
+               // Handle special case when the base register is %r12.
+               if (pc[12] == 0x94)
+                       *((uint32_t*) (pc + 14)) = fi->offset;
+               else
+                       *((uint32_t*) (pc + 13)) = fi->offset;
+       }
+       else {
+               // Handle special case when the base register is %r12.
+               if (pc[2] == 0x84)
+                       *((uint32_t*) (pc + 4)) = fi->offset;
+               else
+                       *((uint32_t*) (pc + 3)) = fi->offset;
+       }
 
-       /* patch the classinfo pointer */
+       // Synchronize instruction cache.
+       md_icacheflush(pc, 14 + sizeof(int32_t));
 
-       *((ptrint *) (ra + 2)) = (ptrint) c;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -456,39 +342,25 @@ 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;
-       s4                 disp;
-       methodinfo        *m;
+       unresolved_method* um    = (unresolved_method*) pr->ref;
+       uintptr_t*         datap = (uintptr_t*)         pr->datap;
 
-       /* get stuff from the stack */
+       // Resolve the method.
+       methodinfo* m = resolve_method_eager(um);
 
-       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)))
+       if (m == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
+       // Patch stubroutine.
+       *datap = (uintptr_t) m->stubroutine;
 
-       if (opt_shownops)
-               ra = ra + PATCHER_CALL_SIZE;
+       // Synchronize data cache.
+       md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
 
-       /* patch stubroutine */
-
-/*     *((ptrint *) (ra + 2)) = (ptrint) m->stubroutine; */
-       *((ptrint *) (ra + 7 + disp)) = (ptrint) m->stubroutine;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -505,37 +377,27 @@ 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 + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
+       uint8_t*           pc = (uint8_t*)           pr->mpc;
+       unresolved_method* um = (unresolved_method*) pr->ref;
 
-       /* get the fieldinfo */
+       // Resovlve the method.
+       methodinfo* m = resolve_method_eager(um);
 
-       if (!(m = resolve_method_eager(um)))
+       if (m == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
+       pc += PATCHER_CALL_SIZE;
 
-       if (opt_shownops)
-               ra = ra + 5;
+       // Patch vftbl index.
+       *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex);
 
-       /* patch vftbl index */
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
 
-       *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, table[0]) +
-                                                                  sizeof(methodptr) * m->vftblindex);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -553,88 +415,30 @@ 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;
+       uint8_t*           pc = (uint8_t*)           pr->mpc;
+       unresolved_method* um = (unresolved_method*) pr->ref;
 
-       /* get stuff from the stack */
+       // Resolve the method.
+       methodinfo* m = resolve_method_eager(um);
 
-       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)))
+       if (m == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
+       pc += PATCHER_CALL_SIZE;
 
-       if (opt_shownops)
-               ra = ra + 5;
+       // Patch interfacetable index.
+       *((int32_t*) (pc + 3 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index);
 
-       /* patch interfacetable index */
+       // Patch method offset.
+       *((int32_t*) (pc + 3 + 7 + 3)) = (int32_t) (sizeof(methodptr) * (m - m->clazz->methods));
 
-       *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
-                                                                  sizeof(methodptr) * m->class->index);
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P);
 
-       /* patch method offset */
-
-       *((s4 *) (ra + 3 + 7 + 3)) =
-               (s4) (sizeof(methodptr) * (m - m->class->methods));
-
-       return true;
-}
-
-
-/* patcher_checkcast_instanceof_flags ******************************************
-
-   Machine code:
-
-   <patched call position>
-   41 ba 00 00 00 00                mov    $0x0,%r10d
-   41 81 e2 00 02 00 00             and    $0x200,%r10d
-   0f 84 35 00 00 00                je     0x00002aaaaab01479
-
-*******************************************************************************/
-
-bool patcher_checkcast_instanceof_flags(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 + 5;
-
-       /* patch class flags */
-
-       *((s4 *) (ra + 2)) = (s4) c->flags;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -653,89 +457,29 @@ bool patcher_checkcast_instanceof_flags(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_checkcast_interface(u1 *sp)
+bool patcher_checkcast_interface(patchref_t *pr)
 {
-       u1                *ra;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
+       uint8_t*           pc = (uint8_t*)           pr->mpc;
+       constant_classref* cr = (constant_classref*) pr->ref;
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
+       // Resolve the class.
+       classinfo* c = resolve_classref_eager(cr);
 
-       /* get the fieldinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
+       if (c == NULL)
                return false;
 
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
+       pc += PATCHER_CALL_SIZE;
 
-       if (opt_shownops)
-               ra = ra + PATCHER_CALL_SIZE;
+       // Patch super class index.
+       *((int32_t*) (pc + 7 + 3)) = c->index;
 
-       /* patch super class index */
+       *((int32_t*) (pc + 7 + 7 + 6 + 8 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
 
-       *((s4 *) (ra + 7 + 3)) = (s4) c->index;
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t));
 
-       *((s4 *) (ra + 7 + 7 + 6 + 8 + 3)) =
-               (s4) (OFFSET(vftbl_t, interfacetable[0]) -
-                         c->index * sizeof(methodptr*));
-
-       return true;
-}
-
-
-/* patcher_checkcast_class *****************************************************
-
-   Machine code:
-
-   <patched call position>
-   49 bb 00 00 00 00 00 00 00 00    mov    $0x0,%r11
-   45 8b 92 20 00 00 00             mov    0x20(%r10),%r10d
-   45 8b 9b 20 00 00 00             mov    0x20(%r11),%r11d
-   4d 29 da                         sub    %r11,%r10
-   49 bb 00 00 00 00 00 00 00 00    mov    $0x0,%r11
-
-*******************************************************************************/
-
-bool patcher_checkcast_class(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 + 5;
-
-       /* patch super class' vftbl */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
-       *((ptrint *) (ra + 10 + 7 + 7 + 3 + 2)) = (ptrint) c->vftbl;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -753,207 +497,32 @@ bool patcher_checkcast_class(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_instanceof_interface(u1 *sp)
+bool patcher_instanceof_interface(patchref_t *pr)
 {
-       u1                *ra;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
+       uint8_t*           pc = (uint8_t*)           pr->mpc;
+       constant_classref* cr = (constant_classref*) pr->ref;
 
-       /* get stuff from the stack */
+       // Resolve the class.
+       classinfo* c = resolve_classref_eager(cr);
 
-       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)))
+       if (c == NULL)
                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;
-}
-
-
-/* patcher_instanceof_class ****************************************************
-
-   Machine code:
-
-   <patched call position>
-   49 ba 00 00 00 00 00 00 00 00    mov    $0x0,%r10
-
-*******************************************************************************/
-
-bool patcher_instanceof_class(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 + 5;
-
-       /* patch super class' vftbl */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
-
-       return true;
-}
-
-
-/* patcher_clinit **************************************************************
-
-   May be used for GET/PUTSTATIC and in native stub.
-
-   Machine code:
-
-   <patched call position>
-   4d 8b 15 92 ff ff ff             mov    -110(%rip),%r10
-   49 89 1a                         mov    %rbx,(%r10)
-
-*******************************************************************************/
-
-bool patcher_clinit(u1 *sp)
-{
-       u1        *ra;
-       u8         mcode;
-       classinfo *c;
-
-       /* get stuff from the stack */
-
-       ra    = (u1 *)        *((ptrint *) (sp + 5 * 8));
-       mcode =               *((u8 *)     (sp + 3 * 8));
-       c     = (classinfo *) *((ptrint *) (sp + 2 * 8));
-
-       /* check if the class is initialized */
-
-       if (!(c->state & CLASS_INITIALIZED))
-               if (!initialize_class(c))
-                       return false;
-
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
-
-       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;
-
-       return true;
-}
-#endif /* ENABLE_VERIFIER */
-
-
-/* patcher_resolve_native ******************************************************
-
-   Machine code:
-
-   <patched call position>
-   48 b8 00 00 00 00 00 00 00 00    mov    $0x0,%rax
-   48 ff d0                         callq  *%rax
-
-*******************************************************************************/
-
-#if !defined(WITH_STATIC_CLASSPATH)
-bool patcher_resolve_native(u1 *sp)
-{
-       u1          *ra;
-       u8           mcode;
-       methodinfo  *m;
-       functionptr  f;
-
-       /* get stuff from the stack */
-
-       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)))
-               return false;
-
-       /* patch back original code */
-
-       *((u8 *) ra) = mcode;
+       pc += PATCHER_CALL_SIZE;
 
-       /* if we show disassembly, we have to skip the nop's */
+       // Patch super class index.
+       *((int32_t*) (pc + 7 + 3)) = c->index;
 
-       if (opt_shownops)
-               ra = ra + 5;
+       *((int32_t*) (pc + 7 + 7 + 6 + 3)) = (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*));
 
-       /* patch native function pointer */
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 7 + 3, sizeof(int32_t) + 6 + 3 + sizeof(int32_t));
 
-       *((ptrint *) (ra + 2)) = (ptrint) f;
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
-#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
 /*