* Removed all Id tags.
[cacao.git] / src / vm / jit / s390 / patcher.c
index 91f0622b9bb77d0f12b75898da3f543acd902f5b..78c9b1797774239993890e7a15dd821e7b926a13 100644 (file)
 
    Authors: Christian Thalinger
 
-   Changes:
-
-   $Id: patcher.c 7373 2007-02-18 20:16:14Z pm $
+   Changes: Peter Molnar
 
 */
 
 
 #include "config.h"
-#include "vm/types.h"
 
-#include "vm/jit/s390/codegen.h"
+#include <assert.h>
+#include <stdint.h>
 
 #include "mm/memory.h"
 #include "native/native.h"
 #include "vm/builtin.h"
-#include "vmcore/class.h"
 #include "vm/exceptions.h"
-#include "vmcore/field.h"
 #include "vm/initialize.h"
+#include "vm/jit/patcher-common.h"
+#include "vm/jit/s390/codegen.h"
+#include "vm/jit/s390/md-abi.h"
+#include "vm/jit/stacktrace.h"
+#include "vm/resolve.h"
+#include "vm/types.h"
+#include "vmcore/class.h"
+#include "vmcore/field.h"
 #include "vmcore/options.h"
 #include "vmcore/references.h"
-#include "vmcore/resolve.h"
-#include "vm/jit/patcher.h"
-#include "vm/jit/stacktrace.h"
-
-#include <assert.h>
-#define OOPS() assert(0);
-#define __PORTED__
-
-/* patcher_wrapper *************************************************************
-
-   Wrapper for all patchers.  It also creates the stackframe info
-   structure.
-
-   If the return value of the patcher function is false, it gets the
-   exception object, clears the exception pointer and returns the
-   exception.
-
-*******************************************************************************/
-
-java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
-{
-#if 1
-       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 * 4));
-       o   = (java_objectheader *) *((ptrint *) (sp + 4 * 4));
-       f   = (functionptr)         *((ptrint *) (sp + 0 * 4));
-       
-       /* TODO here was PATCHER_CALL_SIZE previously ! */
-       xpc = xpc - 4; /* the patch position is 4 bytes before the RA */
-
-       *((ptrint *) (sp + 5 * 4)) = (ptrint) xpc;
-
-       /* store PV into the patcher function position */
-
-       *((ptrint *) (sp + 0 * 4)) = (ptrint) pv;
-
-       /* 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 * 4), ra, xpc);
-
-       /* call the proper patcher function */
-
-       result = (patcher_function)(sp);
-
-       /* remove the stackframeinfo */
-
-       stacktrace_remove_stackframeinfo(&sfi);
-
-       /* check for return value and exit accordingly */
-
-       if (result == false) {
-               e = exceptions_get_and_clear_exception();
-
-               PATCHER_MONITOREXIT;
-
-               return e;
-       }
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
-       return NULL;
-#else
-
-       stackframeinfo     sfi;
-       u1                *xpc;
-       java_objectheader *o;
-       u4                 mcode;
-       functionptr        f;
-       bool               result;
-       java_objectheader *e;
-
-       /* define the patcher function */
-
-       bool (*patcher_function)(u1 *);
-
-       assert(pv != NULL);
-
-       /* get stuff from the stack */
-
-       xpc = (u1 *)                *((ptrint *) (sp + 5 * 4));
-       o   = (java_objectheader *) *((ptrint *) (sp + 4 * 4));
-       f   = (functionptr)         *((ptrint *) (sp + 0 * 4));
-
-       /* Correct RA is calculated in codegen.c and stored in the patcher
-          stub stack.  There's no need to adjust xpc. */
-
-       /* store PV into the patcher function position */
-
-       *((ptrint *) (sp + 0 * 4)) = (ptrint) pv;
-
-       /* 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 */
-
-       stacktrace_create_extern_stackframeinfo(&sfi, pv, sp + 8 * 4, ra, xpc);
-
-       /* call the proper patcher function */
-
-       result = (patcher_function)(sp);
-
-       /* remove the stackframeinfo */
-
-       stacktrace_remove_stackframeinfo(&sfi);
-
-       /* check for return value and exit accordingly */
-
-       if (result == false) {
-               e = exceptions_get_and_clear_exception();
-
-               PATCHER_MONITOREXIT;
-
-               return e;
-       }
-
-       PATCHER_MARK_PATCHED_MONITOREXIT;
-
-       return NULL;
-#endif
-}
+#define PATCH_BACK_ORIGINAL_MCODE \
+       *((u2 *) pr->mpc) = (u2) pr->mcode;
 
+#define PATCHER_TRACE 
 
 /* patcher_get_putstatic *******************************************************
 
@@ -204,22 +63,18 @@ java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
 
 *******************************************************************************/
 
-bool patcher_get_putstatic(u1 *sp)
+bool patcher_get_putstatic(patchref_t *pr)
 {
-       u1               *ra;
-       u4                mcode;
        unresolved_field *uf;
-       s4                disp;
+       u1               *datap;
        fieldinfo        *fi;
-       u1               *pv;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 5 * 4));
-       mcode =                      *((u4 *)     (sp + 3 * 4));
-       uf    = (unresolved_field *) *((ptrint *) (sp + 2 * 4));
-       disp  =                      *((s4 *)     (sp + 1 * 4));
-       pv    = (u1 *)               *((ptrint *) (sp + 0 * 4));
+       uf    = (unresolved_field *) pr->ref;
+       datap = (u1 *)               pr->datap;
 
        /* get the fieldinfo */
 
@@ -232,11 +87,11 @@ bool patcher_get_putstatic(u1 *sp)
                if (!initialize_class(fi->class))
                        return false;
 
-       *((ptrint *) (pv + disp)) = (ptrint) &(fi->value);
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* patch back original code */
+       /* patch the field value's address */
 
-       *((u4 *) ra) = mcode;
+       *((intptr_t *) datap) = (intptr_t) fi->value;
 
        return true;
 }
@@ -246,338 +101,108 @@ bool patcher_get_putstatic(u1 *sp)
 
    Machine code:
 
-   <patched call position>
-   45 8b 8f 00 00 00 00             mov    0x0(%r15),%r9d
-
 *******************************************************************************/
 
-bool patcher_get_putfield(u1 *sp)
+bool patcher_get_putfield(patchref_t *pr)
 {
-       OOPS();
        u1               *ra;
-       u8                mcode;
        unresolved_field *uf;
        fieldinfo        *fi;
-       u1                byte;
-
-       /* get stuff from the stack */
-
-       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)))
-               return false;
-
-       /* patch back original code (instruction code is smaller than 8 bytes) */
-
-       *((u4 *) (ra + 0)) = (u4) mcode;
-       *((u1 *) (ra + 4)) = (u1) (mcode >> 32);
-
-       /* 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 */
-
-               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);
-       }
-
-       return true;
-}
-
-
-/* patcher_putfieldconst *******************************************************
-
-   Machine code:
-
-   <patched call position>
-   41 c7 85 00 00 00 00 7b 00 00 00    movl   $0x7b,0x0(%r13)
+       s4                disp;
 
-*******************************************************************************/
-
-bool patcher_putfieldconst(u1 *sp)
-{
-       OOPS();
-       u1               *ra;
-       u8                mcode;
-       unresolved_field *uf;
-       fieldinfo        *fi;
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 5 * 8));
-       mcode =                      *((u8 *)     (sp + 3 * 8));
-       uf    = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
+       ra    = (u1 *)               pr->mpc;
+       uf    = (unresolved_field *) pr->ref;
+       disp  =                      pr->disp;
 
        /* get the fieldinfo */
 
        if (!(fi = resolve_field_eager(uf)))
                return false;
 
-       /* patch back original code */
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       *((u8 *) ra) = mcode;
-
-       /* if we show disassembly, we have to skip the nop's */
+       /* If NOPs are generated, skip them */
 
        if (opt_shownops)
-               ra = ra + 5;
+               ra += PATCHER_NOPS_SKIP;
 
-       /* patch the field's offset */
+       /* If there is an operand load before, skip the load size passed in disp (see ICMD_PUTFIELD) */
 
-       if (IS_2_WORD_TYPE(fi->type) || IS_ADR_TYPE(fi->type)) {
-               /* handle special case when the base register is %r12 */
+       ra += disp;
 
-               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 */
+       /* patch correct offset */
 
-               if (*(ra + 2) == 0x84)
-                       *((u4 *) (ra + 4)) = (u4) (fi->offset);
-               else
-                       *((u4 *) (ra + 3)) = (u4) (fi->offset);
+       if (fi->type == TYPE_LNG) {
+               assert(N_VALID_DISP(fi->offset + 4));
+               /* 2 RX operations, for 2 words; each already contains a 0 or 4 offset. */
+               *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
+               ra += 4;
+               *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
+       } else {
+               assert(N_VALID_DISP(fi->offset));
+               /* 1 RX operation */
+               *((u4 *) ra) |= fi->offset;
        }
 
        return true;
 }
 
-
-/* patcher_aconst **************************************************************
-
-   Machine code:
-
-   <patched call position>
-   48 bf a0 f0 92 00 00 00 00 00    mov    $0x92f0a0,%rdi
-
-*******************************************************************************/
-
-bool patcher_aconst(u1 *sp)
-{
-       OOPS();
-       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 classinfo */
-
-       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 the classinfo pointer */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c;
-
-       return true;
-}
-
-
-/* patcher_builtin_multianewarray **********************************************
-
-   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
-
-*******************************************************************************/
-
-bool patcher_builtin_multianewarray(u1 *sp)
-{
-       OOPS();
-       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 classinfo */
-
-       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 the classinfo pointer */
-
-       *((ptrint *) (ra + 10 + 2)) = (ptrint) c;
-
-       return true;
-}
-
-
-/* patcher_builtin_arraycheckcast **********************************************
-
-   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
-
-*******************************************************************************/
-
-bool patcher_builtin_arraycheckcast(u1 *sp)
-{
-       OOPS();
-       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 classinfo */
-
-       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 the classinfo pointer */
-
-       *((ptrint *) (ra + 2)) = (ptrint) c;
-
-       return true;
-}
-
-
 /* patcher_invokestatic_special ************************************************
 
    Machine code:
 
 *******************************************************************************/
 
-__PORTED__ bool patcher_invokestatic_special(u1 *sp)
+bool patcher_invokestatic_special(patchref_t *pr)
 {
-       u1                *ra;
-       u4                 mcode;
        unresolved_method *um;
-       s4                 disp;
-       u1                *pv;
+       u1                *datap;
        methodinfo        *m;
 
+       PATCHER_TRACE;
+
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 4));
-       mcode =                       *((u4 *)     (sp + 3 * 4));
-       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 4));
-       disp  =                       *((s4 *)     (sp + 1 * 4));
-       pv    = (u1 *)                *((ptrint *) (sp + 0 * 4));
+       um    = (unresolved_method *) pr->ref;
+       datap = (u1 *)                pr->datap;
 
        /* get the fieldinfo */
 
        if (!(m = resolve_method_eager(um)))
                return false;
 
-       *((ptrint *) (pv + disp)) = (ptrint) m->stubroutine;
-
-       /* patch back original code */
-
-       *((u4 *) ra) = mcode;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        /* patch stubroutine */
 
+       *((ptrint *) datap) = (ptrint) m->stubroutine;
+
        return true;
 }
 
-
 /* patcher_invokevirtual *******************************************************
 
    Machine code:
 
-   <patched call position>
-   4c 8b 17                         mov    (%rdi),%r10
-   49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
-   48 ff d0                         callq  *%rax
-
 *******************************************************************************/
 
-bool patcher_invokevirtual(u1 *sp)
+bool patcher_invokevirtual(patchref_t *pr)
 {
-       OOPS();
        u1                *ra;
-       u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
+       s4                 off;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
+       ra    = (u1 *)                pr->mpc;
+       um    = (unresolved_method *) pr->ref;
 
        /* get the fieldinfo */
 
@@ -586,18 +211,23 @@ bool patcher_invokevirtual(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* if we show disassembly, we have to skip the nop's */
+       /* If NOPs are generated, skip them */
 
        if (opt_shownops)
-               ra = ra + 5;
+               ra += PATCHER_NOPS_SKIP;
 
        /* patch vftbl index */
 
-       *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, table[0]) +
+
+       off = (s4) (OFFSET(vftbl_t, table[0]) +
                                                                   sizeof(methodptr) * m->vftblindex);
 
+       assert(N_VALID_DISP(off));
+
+       *((s4 *)(ra + 4)) |= off;
+
        return true;
 }
 
@@ -606,27 +236,21 @@ bool patcher_invokevirtual(u1 *sp)
 
    Machine code:
 
-   <patched call position>
-   4c 8b 17                         mov    (%rdi),%r10
-   4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
-   49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
-   48 ff d0                         callq  *%rax
-
 *******************************************************************************/
 
-bool patcher_invokeinterface(u1 *sp)
+bool patcher_invokeinterface(patchref_t *pr)
 {
-       OOPS();
        u1                *ra;
-       u8                 mcode;
        unresolved_method *um;
        methodinfo        *m;
+       s4                 idx, off;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       um    = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
+       ra    = (u1 *)                pr->mpc;
+       um    = (unresolved_method *) pr->ref;
 
        /* get the fieldinfo */
 
@@ -635,22 +259,30 @@ bool patcher_invokeinterface(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* if we show disassembly, we have to skip the nop's */
+       /* If NOPs are generated, skip them */
 
        if (opt_shownops)
-               ra = ra + 5;
+               ra += PATCHER_NOPS_SKIP;
+
+       /* get interfacetable index */
 
-       /* patch interfacetable index */
+       idx = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
+               sizeof(methodptr) * m->class->index);
 
-       *((s4 *) (ra + 3 + 3)) = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
-                                                                  sizeof(methodptr) * m->class->index);
+       ASSERT_VALID_IMM(idx);
 
-       /* patch method offset */
+       /* get method offset */
 
-       *((s4 *) (ra + 3 + 7 + 3)) =
+       off =
                (s4) (sizeof(methodptr) * (m - m->class->methods));
+       ASSERT_VALID_DISP(off);
+
+       /* patch them */
+
+       *((s4 *)(ra + 4)) |= (u2)idx;
+       *((s4 *)(ra + 4 + 4 + 4)) |= off;
 
        return true;
 }
@@ -664,35 +296,29 @@ bool patcher_invokeinterface(u1 *sp)
 
 *******************************************************************************/
 
-__PORTED__ bool patcher_resolve_classref_to_flags(u1 *sp)
+bool patcher_resolve_classref_to_flags(patchref_t *pr)
 {
        constant_classref *cr;
-       s4                 disp;
-       u1                *pv;
+       u1                *datap;
        classinfo         *c;
-       u4                 mcode;
-       u1                *ra;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 4));
-       mcode =                       *((u4 *)     (sp + 3 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 4));
-       disp  =                       *((s4 *)     (sp + 1 * 4));
-       pv    = (u1 *)                *((ptrint *) (sp + 0 * 4));
+       cr    = (constant_classref *) pr->ref;
+       datap = (u1 *)                pr->datap;
 
        /* get the fieldinfo */
 
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch class flags */
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       *((s4 *) (pv + disp)) = (s4) c->flags;
-
-       /* patch back original code */
+       /* patch class flags */
 
-       *((u4 *) ra) = mcode;
+       *((s4 *) datap) = (s4) c->flags;
 
        return true;
 }
@@ -705,35 +331,29 @@ __PORTED__ bool patcher_resolve_classref_to_flags(u1 *sp)
 
 *******************************************************************************/
 
-__PORTED__ bool patcher_resolve_classref_to_classinfo(u1 *sp)
+bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
 {
        constant_classref *cr;
-       s4                 disp;
-       u1                *pv;
+       u1                *datap;
        classinfo         *c;
-       u4                 mcode;
-       u1                *ra;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 4));
-       mcode =                       *((u4 *)     (sp + 3 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 4));
-       disp  =                       *((s4 *)     (sp + 1 * 4));
-       pv    = (u1 *)                *((ptrint *) (sp + 0 * 4));
+       cr    = (constant_classref *) pr->ref;
+       datap = (u1 *)                pr->datap;
 
        /* get the classinfo */
 
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch the classinfo pointer */
-
-       *((ptrint *) (pv + disp)) = (ptrint) c;
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* patch back original code */
+       /* patch the classinfo pointer */
 
-       *((u4 *) ra) = mcode;
+       *((ptrint *) datap) = (ptrint) c;
 
        return true;
 }
@@ -745,35 +365,29 @@ __PORTED__ bool patcher_resolve_classref_to_classinfo(u1 *sp)
 
 *******************************************************************************/
 
-bool patcher_resolve_classref_to_vftbl(u1 *sp)
+bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
 {
        constant_classref *cr;
-       s4                 disp;
-       u1                *pv;
+       u1                *datap;
        classinfo         *c;
-       u4                 mcode;
-       u1                *ra;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 4));
-       mcode =                       *((u4 *)     (sp + 3 * 4));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 4));
-       disp  =                       *((s4 *)     (sp + 1 * 4));
-       pv    = (u1 *)                *((ptrint *) (sp + 0 * 4));
+       cr    = (constant_classref *) pr->ref;
+       datap = (u1 *)                pr->datap;
 
        /* get the fieldinfo */
 
        if (!(c = resolve_classref_eager(cr)))
                return false;
 
-       /* patch super class' vftbl */
-
-       *((ptrint *) (pv + disp)) = (ptrint) c->vftbl;
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* patch back original code */
+       /* patch super class' vftbl */
 
-       *((u4 *) ra) = mcode;
+       *((ptrint *) datap) = (ptrint) c->vftbl;
 
        return true;
 }
@@ -782,81 +396,21 @@ bool patcher_resolve_classref_to_vftbl(u1 *sp)
 
    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
-   4d 8b 9a 00 00 00 00             mov    0x0(%r10),%r11
-
 *******************************************************************************/
 
-bool patcher_checkcast_instanceof_interface(u1 *sp)
+bool patcher_checkcast_instanceof_interface(patchref_t *pr)
 {
-       OOPS();
-       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 index */
-
-       *((s4 *) (ra + 7 + 3)) = (s4) c->index;
-
-       *((s4 *) (ra + 7 + 7 + 3 + 6 + 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)
-{
-       OOPS();
        u1                *ra;
-       u8                 mcode;
        constant_classref *cr;
        classinfo         *c;
 
+       PATCHER_TRACE;
+
        /* get stuff from the stack */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
+       ra    = (u1 *)                pr->mpc;
+       cr    = (constant_classref *) pr->ref;
 
        /* get the fieldinfo */
 
@@ -865,67 +419,48 @@ bool patcher_checkcast_class(u1 *sp)
 
        /* patch back original code */
 
-       *((u8 *) ra) = mcode;
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* if we show disassembly, we have to skip the nop's */
+       /* If NOPs are generated, skip them */
 
        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;
-
-       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)
-{
-       OOPS();
-       u1                *ra;
-       u8                 mcode;
-       constant_classref *cr;
-       classinfo         *c;
+               ra += PATCHER_NOPS_SKIP;
 
-       /* get stuff from the stack */
+       /* patch super class index */
 
-       ra    = (u1 *)                *((ptrint *) (sp + 5 * 8));
-       mcode =                       *((u8 *)     (sp + 3 * 8));
-       cr    = (constant_classref *) *((ptrint *) (sp + 2 * 8));
+       /* From here, split your editor and open codegen.c */
 
-       /* get the fieldinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
-               return false;
+       switch (*(ra + 1) >> 4) {
+               case REG_ITMP1: 
+                       /* First M_ALD is into ITMP1 */
+                       /* INSTANCEOF code */
 
-       /* patch back original code */
+                       *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
+                       *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC) |=
+                               (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
+                                       c->index * sizeof(methodptr*));
 
-       *((u8 *) ra) = mcode;
+                       break;
 
-       /* if we show disassembly, we have to skip the nop's */
+               case REG_ITMP2:
+                       /* First M_ALD is into ITMP2 */
+                       /* CHECKCAST code */
 
-       if (opt_shownops)
-               ra = ra + 5;
+                       *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
+                       *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC + SZ_ILL) |=
+                               (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
+                                       c->index * sizeof(methodptr*));
 
-       /* patch super class' vftbl */
+                       break;
 
-       *((ptrint *) (ra + 2)) = (ptrint) c->vftbl;
+               default:
+                       assert(0);
+                       break;
+       }
 
        return true;
 }
 
-
 /* patcher_clinit **************************************************************
 
    May be used for GET/PUTSTATIC and in native stub.
@@ -934,17 +469,15 @@ bool patcher_instanceof_class(u1 *sp)
 
 *******************************************************************************/
 
-__PORTED__ bool patcher_clinit(u1 *sp)
+bool patcher_clinit(patchref_t *pr)
 {
-       u1        *ra;
-       u4         mcode;
        classinfo *c;
 
+       PATCHER_TRACE;
+
        /* get stuff from the stack */
 
-       ra    = (u1 *)        *((ptrint *) (sp + 5 * 4));
-       mcode =               *((u4 *)     (sp + 3 * 4));
-       c     = (classinfo *) *((ptrint *) (sp + 2 * 4));
+       c     = (classinfo *)pr->ref;
 
        /* check if the class is initialized */
 
@@ -954,7 +487,7 @@ __PORTED__ bool patcher_clinit(u1 *sp)
 
        /* patch back original code */
 
-       *((u4 *) ra) = mcode;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        return true;
 }
@@ -969,27 +502,24 @@ __PORTED__ bool patcher_clinit(u1 *sp)
 *******************************************************************************/
 
 #ifdef ENABLE_VERIFIER
-__PORTED__ bool patcher_athrow_areturn(u1 *sp)
+bool patcher_athrow_areturn(patchref_t *pr)
 {
-       u1               *ra;
-       u4                mcode;
        unresolved_class *uc;
-       classinfo        *c;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)               *((ptrint *) (sp + 5 * 4));
-       mcode =                      *((u4 *)     (sp + 3 * 4));
-       uc    = (unresolved_class *) *((ptrint *) (sp + 2 * 4));
+       uc    = (unresolved_class *) pr->ref;
 
-       /* resolve the class */
+       /* resolve the class and check subtype constraints */
 
-       if (!resolve_class(uc, resolveEager, false, &c))
+       if (!resolve_class_eager_no_access_check(uc))
                return false;
 
        /* patch back original code */
 
-       *((u4 *) ra) = mcode;
+       PATCH_BACK_ORIGINAL_MCODE;
 
        return true;
 }
@@ -1001,41 +531,34 @@ __PORTED__ bool patcher_athrow_areturn(u1 *sp)
 *******************************************************************************/
 
 #if !defined(WITH_STATIC_CLASSPATH)
-__PORTED__ bool patcher_resolve_native(u1 *sp)
+bool patcher_resolve_native_function(patchref_t *pr)
 {
-       u1          *ra;
-       u4           mcode;
        methodinfo  *m;
+       u1          *datap;
        functionptr  f;
-       s4           disp;
-       u1          *pv;
+
+       PATCHER_TRACE;
 
        /* get stuff from the stack */
 
-       ra    = (u1 *)         *((ptrint *) (sp + 5 * 4));
-       mcode =                *((u4 *)     (sp + 3 * 4));
-       disp  =                *((s4 *)     (sp + 1 * 4));
-       m     = (methodinfo *) *((ptrint *) (sp + 2 * 4));
-       pv    = (u1 *)         *((ptrint *) (sp + 0 * 4));
+       m     = (methodinfo *) pr->ref;
+       datap = (u1 *)         pr->datap;
 
        /* resolve native function */
 
        if (!(f = native_resolve_function(m)))
                return false;
 
-       /* patch native function pointer */
-
-       *((ptrint *) (pv + disp)) = (ptrint) f;
+       PATCH_BACK_ORIGINAL_MCODE;
 
-       /* patch back original code */
+       /* patch native function pointer */
 
-       *((u4 *) ra) = mcode;
+       *((ptrint *) datap) = (ptrint) f;
 
        return true;
 }
 #endif /* !defined(WITH_STATIC_CLASSPATH) */
 
-
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where