Fixes PR84 for mips.
authorChristian Thalinger <twisti@complang.tuwien.ac.at>
Mon, 18 Aug 2008 15:10:34 +0000 (17:10 +0200)
committerChristian Thalinger <twisti@complang.tuwien.ac.at>
Mon, 18 Aug 2008 15:10:34 +0000 (17:10 +0200)
* src/vm/jit/mips/patcher.c: Reordered data and code patching.

src/vm/jit/mips/patcher.c

index 1c2bc975c52015d3a04f7a08dd22f71595071a7b..76da9b2e1a6ec4e632d64294459441047ed9552b 100644 (file)
@@ -28,8 +28,6 @@
 #include <assert.h>
 #include <stdint.h>
 
-#include "vm/types.h"
-
 #include "vm/jit/mips/codegen.h"
 #include "vm/jit/mips/md.h"
 
 #include "vm/jit/patcher-common.h"
 
 
-#define PATCH_BACK_ORIGINAL_MCODE \
-       *((u4 *) pr->mpc) = (u4) pr->mcode; \
-       md_icacheflush((u1 *) pr->mpc, PATCHER_CALL_SIZE);
-
-
 /* patcher_patch_code **********************************************************
 
    Just patches back the original machine code.
@@ -62,7 +55,8 @@
 
 void patcher_patch_code(patchref_t *pr)
 {
-       PATCH_BACK_ORIGINAL_MCODE;
+       *((uint32_t*) pr->mpc) = (uint32_t) pr->mcode;
+       md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
 }
 
 
@@ -78,36 +72,29 @@ void patcher_patch_code(patchref_t *pr)
 
 bool patcher_get_putstatic(patchref_t *pr)
 {
-       unresolved_field *uf;
-       u1               *datap;
-       fieldinfo        *fi;
-
-       /* get stuff from the stack */
+       unresolved_field* uf    = (unresolved_field *) pr->ref;
+       uintptr_t*        datap = (uintptr_t*)         pr->datap;
 
-       uf    = (unresolved_field *) pr->ref;
-       datap = (u1 *)               pr->datap;
+       // Resolve the field.
+       fieldinfo* fi = resolve_field_eager(uf);
 
-       /* get the fieldinfo */
-
-       if (!(fi = resolve_field_eager(uf)))
+       if (fi == NULL)
                return false;
 
-       /* check if the field's class is initialized */
-
+       // Check if the field's class is initialized.
        if (!(fi->clazz->state & CLASS_INITIALIZED))
                if (!initialize_class(fi->clazz))
                        return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch the field value's address */
-
-       *((intptr_t *) datap) = (intptr_t) fi->value;
-
-       /* synchronize data cache */
+       // Patch the field value's address.
+       *datap = (uintptr_t) fi->value;
 
+       // Synchronize data cache.
        md_dcacheflush(datap, SIZEOF_VOID_P);
 
+       // Patch back the original code.
+       patcher_patch_code(pr);
+
        return true;
 }
 
@@ -123,41 +110,41 @@ bool patcher_get_putstatic(patchref_t *pr)
 
 bool patcher_get_putfield(patchref_t *pr)
 {
-       u1               *ra;
-       unresolved_field *uf;
-       fieldinfo        *fi;
-
-       /* get stuff from the stack */
+       uint32_t*         pc = (uint32_t*)         pr->mpc;
+       unresolved_field* uf = (unresolved_field*) pr->ref;
 
-       ra = (u1 *)               pr->mpc;
-       uf = (unresolved_field *) pr->ref;
+       // Resolve the field.
+       fieldinfo* fi = resolve_field_eager(uf);
 
-       /* get the fieldinfo */
-
-       if (!(fi = resolve_field_eager(uf)))
+       if (fi == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
 #if SIZEOF_VOID_P == 4
        if (IS_LNG_TYPE(fi->type)) {
 # if WORDS_BIGENDIAN == 1
-               /* ATTENTION: order of these instructions depend on M_LLD_INTERN */
-               *((u4 *) (ra + 0 * 4)) |= (s2) ((fi->offset + 0) & 0x0000ffff);
-               *((u4 *) (ra + 1 * 4)) |= (s2) ((fi->offset + 4) & 0x0000ffff);
+               // ATTENTION: order of these instructions depend on M_LLD_INTERN.
+               // The first instruction is patched back later.
+               pr->mcode |= (int16_t) ((fi->offset + 0) & 0x0000ffff);
+               pc[1]     |= (int16_t) ((fi->offset + 4) & 0x0000ffff);
 # else
-               /* ATTENTION: order of these instructions depend on M_LLD_INTERN */
-               *((u4 *) (ra + 0 * 4)) |= (s2) ((fi->offset + 4) & 0x0000ffff);
-               *((u4 *) (ra + 1 * 4)) |= (s2) ((fi->offset + 0) & 0x0000ffff);
+               // ATTENTION: order of these instructions depend on M_LLD_INTERN.
+               // The first instruction is patched back later.
+               pr->mcode |= (int16_t) ((fi->offset + 4) & 0x0000ffff);
+               pc[1]     |= (int16_t) ((fi->offset + 0) & 0x0000ffff);
 # endif
        }
        else
 #endif
-               *((u4 *) (ra + 0 * 4)) |= (s2) (fi->offset & 0x0000ffff);
+       {
+               // The instruction is patched back later.
+               pr->mcode |= (int16_t) (fi->offset & 0x0000ffff);
+       }
 
-       /* synchronize instruction cache */
+       // Synchronize instruction cache.
+       md_icacheflush(pc, 2 * 4);
 
-       md_icacheflush(ra, 2 * 4);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -191,30 +178,24 @@ bool patcher_get_putfield(patchref_t *pr)
 
 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
 {
-       constant_classref *cr;
-       u1                *datap;
-       classinfo         *c;
+       constant_classref* cr    = (constant_classref*) pr->ref;
+       uintptr_t*         datap = (uintptr_t*)         pr->datap;
 
-       /* get stuff from the stack */
+       // Resolve the class.
+       classinfo* c = resolve_classref_eager(cr);
 
-       cr    = (constant_classref *) pr->ref;
-       datap = (u1 *)                pr->datap;
-
-       /* get the classinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
+       if (c == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch the classinfo pointer */
-
-       *((intptr_t *) datap) = (intptr_t) c;
-
-       /* synchronize data cache */
+       // Patch the class.
+       *datap = (uintptr_t) c;
 
+       // Synchronize data cache.
        md_dcacheflush(datap, SIZEOF_VOID_P);
 
+       // Patch back the original code.
+       patcher_patch_code(pr);
+
        return true;
 }
 
@@ -232,30 +213,24 @@ bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
 
 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
 {
-       constant_classref *cr;
-       u1                *datap;
-       classinfo         *c;
-
-       /* get stuff from the stack */
+       constant_classref* cr    = (constant_classref *) pr->ref;
+       uintptr_t*         datap = (uintptr_t*)          pr->datap;
 
-       cr    = (constant_classref *) pr->ref;
-       datap = (u1 *)                pr->datap;
+       // Resolve the field.
+       classinfo* c = resolve_classref_eager(cr);
 
-       /* get the fieldinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
+       if (c == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch super class' vftbl */
-
-       *((intptr_t *) datap) = (intptr_t) c->vftbl;
-
-       /* synchronize data cache */
+       // Patch super class' vftbl.
+       *datap = (uintptr_t) c->vftbl;
 
+       // Synchronize data cache.
        md_dcacheflush(datap, SIZEOF_VOID_P);
 
+       // Patch back the original code.
+       patcher_patch_code(pr);
+
        return true;
 }
 
@@ -274,30 +249,24 @@ bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
 
 bool patcher_resolve_classref_to_flags(patchref_t *pr)
 {
-       constant_classref *cr;
-       u1                *datap;
-       classinfo         *c;
-
-       /* get stuff from the stack */
-
-       cr    = (constant_classref *) pr->ref;
-       datap = (u1 *)                pr->datap;
+       constant_classref* cr    = (constant_classref*) pr->ref;
+       int32_t*           datap = (int32_t*)           pr->datap;
 
-       /* get the fieldinfo */
+       // Resolve the class.
+       classinfo* c = resolve_classref_eager(cr);
 
-       if (!(c = resolve_classref_eager(cr)))
+       if (c == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch class flags */
-
-       *((int32_t *) datap) = (int32_t) c->flags;
-
-       /* synchronize data cache */
+       // Patch class flags.
+       *datap = (int32_t) c->flags;
 
+       // Synchronize data cache.
        md_dcacheflush(datap, sizeof(int32_t));
 
+       // Patch back the original code.
+       patcher_patch_code(pr);
+
        return true;
 }
 
@@ -315,30 +284,24 @@ bool patcher_resolve_classref_to_flags(patchref_t *pr)
 
 bool patcher_invokestatic_special(patchref_t *pr)
 {
-       unresolved_method *um;
-       u1                *datap;
-       methodinfo        *m;
+       unresolved_method* um    = (unresolved_method*) pr->ref;
+       void**             datap = (void*)              pr->datap;
 
-       /* get stuff from the stack */
+       // Resolve the method.
+       methodinfo* m = resolve_method_eager(um);
 
-       um    = (unresolved_method *) pr->ref;
-       datap = (u1 *)                pr->datap;
-
-       /* get the fieldinfo */
-
-       if (!(m = resolve_method_eager(um)))
+       if (m == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch stubroutine */
-
-       *((ptrint *) datap) = (ptrint) m->stubroutine;
-
-       /* synchronize data cache */
+       // Patch stubroutine.
+       *datap = (void*) m->stubroutine;
 
+       // Synchronize data cache.
        md_dcacheflush(datap, SIZEOF_VOID_P);
 
+       // Patch back the original code.
+       patcher_patch_code(pr);
+
        return true;
 }
 
@@ -357,31 +320,23 @@ bool patcher_invokestatic_special(patchref_t *pr)
 
 bool patcher_invokevirtual(patchref_t *pr)
 {
-       u1                *ra;
-       unresolved_method *um;
-       methodinfo        *m;
-
-       /* get stuff from the stack */
+       uint32_t*          pc = (uint32_t*)          pr->mpc;
+       unresolved_method* um = (unresolved_method*) pr->ref;
 
-       ra = (u1 *)                pr->mpc;
-       um = (unresolved_method *) pr->ref;
+       // Resolve the method.
+       methodinfo* m = resolve_method_eager(um);
 
-       /* get the fieldinfo */
-
-       if (!(m = resolve_method_eager(um)))
+       if (m == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch vftbl index */
-
-       *((s4 *) (ra + 1 * 4)) |=
-               (s4) ((OFFSET(vftbl_t, table[0]) +
-                          sizeof(methodptr) * m->vftblindex) & 0x0000ffff);
+       // Patch vftbl index.
+       pc[1] |= (int32_t) ((OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex) & 0x0000ffff);
 
-       /* synchronize instruction cache */
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 1, 1 * 4);
 
-       md_icacheflush(ra + 1 * 4, 1 * 4);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -402,36 +357,26 @@ bool patcher_invokevirtual(patchref_t *pr)
 
 bool patcher_invokeinterface(patchref_t *pr)
 {
-       u1                *ra;
-       unresolved_method *um;
-       methodinfo        *m;
+       uint32_t*          pc = (uint32_t*)          pr->mpc;
+       unresolved_method* um = (unresolved_method*) pr->ref;
 
-       /* get stuff from the stack */
+       // Resovlve the method.
+       methodinfo* m = resolve_method_eager(um);
 
-       ra = (u1 *)                pr->mpc;
-       um = (unresolved_method *) pr->ref;
-
-       /* get the fieldinfo */
-
-       if (!(m = resolve_method_eager(um)))
+       if (m == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch interfacetable index */
-
-       *((s4 *) (ra + 1 * 4)) |=
-               (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
-                          sizeof(methodptr*) * m->clazz->index) & 0x0000ffff);
+       // Patch interfacetable index.
+       pc[1] |= (int32_t) ((OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->clazz->index) & 0x0000ffff);
 
-       /* patch method offset */
+       // Patch method offset.
+       pc[2] |= (int32_t) ((sizeof(methodptr) * (m - m->clazz->methods)) & 0x0000ffff);
 
-       *((s4 *) (ra + 2 * 4)) |=
-               (s4) ((sizeof(methodptr) * (m - m->clazz->methods)) & 0x0000ffff);
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 1, 2 * 4);
 
-       /* synchronize instruction cache */
-
-       md_icacheflush(ra + 1 * 4, 2 * 4);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -453,34 +398,24 @@ bool patcher_invokeinterface(patchref_t *pr)
 
 bool patcher_checkcast_interface(patchref_t *pr)
 {
-       u1                *ra;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
-
-       ra = (u1 *)                pr->mpc;
-       cr = (constant_classref *) pr->ref;
+       uint32_t*          pc = (uint32_t*)          pr->mpc;
+       constant_classref* cr = (constant_classref*) pr->ref;
 
-       /* get the fieldinfo */
+       // Resolve the class.
+       classinfo* c = resolve_classref_eager(cr);
 
-       if (!(c = resolve_classref_eager(cr)))
+       if (c == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
+       // Patch super class index.
+       pc[2] |= (int32_t) (-(c->index) & 0x0000ffff);
+       pc[6] |= (int32_t) ((OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)) & 0x0000ffff);
 
-       /* patch super class index */
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 2, 5 * 4);
 
-       *((s4 *) (ra + 2 * 4)) |= (s4) (-(c->index) & 0x0000ffff);
-       /*      *((s4 *) (ra + 5 * 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) - */
-       /*                                                                       c->index * sizeof(methodptr*)) & 0x0000ffff); */
-       *((s4 *) (ra + 6 * 4)) |=
-               (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
-                          c->index * sizeof(methodptr*)) & 0x0000ffff);
-
-       /* synchronize instruction cache */
-
-       md_icacheflush(ra + 2 * 4, 5 * 4);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }
@@ -502,32 +437,24 @@ bool patcher_checkcast_interface(patchref_t *pr)
 
 bool patcher_instanceof_interface(patchref_t *pr)
 {
-       u1                *ra;
-       constant_classref *cr;
-       classinfo         *c;
-
-       /* get stuff from the stack */
+       uint32_t*          pc = (uint32_t*)          pr->mpc;
+       constant_classref* cr = (constant_classref*) pr->ref;
 
-       ra = (u1 *)                pr->mpc;
-       cr = (constant_classref *) pr->ref;
+       // Resolve the method.
+       classinfo* c = resolve_classref_eager(cr);
 
-       /* get the fieldinfo */
-
-       if (!(c = resolve_classref_eager(cr)))
+       if (c == NULL)
                return false;
 
-       PATCH_BACK_ORIGINAL_MCODE;
-
-       /* patch super class index */
-
-       *((s4 *) (ra + 2 * 4)) |= (s4) (-(c->index) & 0x0000ffff);
-       *((s4 *) (ra + 5 * 4)) |=
-               (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
-                          c->index * sizeof(methodptr*)) & 0x0000ffff);
+       // Patch super class index.
+       pc[2] |= (int32_t) (-(c->index) & 0x0000ffff);
+       pc[5] |= (int32_t) ((OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)) & 0x0000ffff);
 
-       /* synchronize instruction cache */
+       // Synchronize instruction cache.
+       md_icacheflush(pc + 2, 4 * 4);
 
-       md_icacheflush(ra + 2 * 4, 4 * 4);
+       // Patch back the original code.
+       patcher_patch_code(pr);
 
        return true;
 }