Copyright (C) 1996-2005, 2006, 2007, 2008
CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+ Copyright (C) 2008 Theobroma Systems Ltd.
This file is part of CACAO.
#include "mm/memory.h"
-#include "native/native.h"
+#include "native/native.hpp"
-#include "vm/builtin.h"
+#include "vm/jit/builtin.hpp"
#include "vm/class.h"
-#include "vm/field.h"
+#include "vm/field.hpp"
#include "vm/initialize.h"
#include "vm/options.h"
#include "vm/references.h"
#include "vm/resolve.h"
#include "vm/jit/asmpart.h"
-#include "vm/jit/patcher-common.h"
+#include "vm/jit/patcher-common.hpp"
-#define PATCH_BACK_ORIGINAL_MCODE \
- *((u4 *) pr->mpc) = (u4) pr->mcode; \
- md_icacheflush((u1 *) pr->mpc, 1 * 4);
-
#define gen_resolveload(inst,offset) \
assert((offset) >= -0x0fff && (offset) <= 0x0fff); \
assert(!((inst) & 0x0fff)); \
void patcher_patch_code(patchref_t *pr)
{
- PATCH_BACK_ORIGINAL_MCODE;
+ *((uint32_t*) pr->mpc) = (uint32_t) pr->mcode;
+ md_icacheflush((void*) pr->mpc, 1 * 4);
+}
+
+
+/**
+ * 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)
+{
+ uint32_t mcode = *((uint32_t*) pc);
+
+ // Check for the undefined instruction we use.
+ if ((mcode & 0x0ff000f0) != 0x07f000f0) {
+ return false;
+ }
+
+ return true;
}
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 */
+ // Patch the field value's address.
+ *datap = (uintptr_t) fi->value;
- *((intptr_t *) datap) = (intptr_t) fi->value;
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
bool patcher_get_putfield(patchref_t *pr)
{
- u1 *ra;
- u4 mcode;
- unresolved_field *uf;
- fieldinfo *fi;
+ uint32_t* pc = (uint32_t*) pr->mpc;
+ unresolved_field* uf = (unresolved_field*) pr->ref;
- /* get stuff from the stack */
- ra = (u1*) pr->mpc;
- mcode = pr->mcode;
- 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 we show disassembly, we have to skip the nop */
-
- if (opt_shownops)
- ra = ra + 1 * 4;
-
- /* patch the field's offset into the instruction */
+ // Patch the field's offset into the instruction.
switch(fi->type) {
case TYPE_ADR:
case TYPE_FLT:
#endif
assert(fi->offset <= 0x0fff);
- *((u4 *) (ra + 0 * 4)) |= (fi->offset & 0x0fff);
+ pr->mcode |= (fi->offset & 0x0fff);
break;
case TYPE_LNG:
case TYPE_DBL:
#endif
assert((fi->offset + 4) <= 0x0fff);
- *((u4 *) (ra + 0 * 4)) |= ((fi->offset + 0) & 0x0fff);
- *((u4 *) (ra + 1 * 4)) &= 0xfffff000;
- *((u4 *) (ra + 1 * 4)) |= ((fi->offset + 4) & 0x0fff);
+ pr->mcode |= ((fi->offset + 0) & 0x0fff);
+ pc[1] &= 0xfffff000;
+ pc[1] |= ((fi->offset + 4) & 0x0fff);
break;
#if !defined(ENABLE_SOFTFLOAT)
case TYPE_FLT:
case TYPE_DBL:
assert(fi->offset <= 0x03ff);
- *((u4 *) (ra + 0 * 4)) |= ((fi->offset >> 2) & 0x00ff);
+ pr->mcode |= ((fi->offset >> 2) & 0x00ff);
break;
#endif
}
- /* 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;
}
bool patcher_resolve_classref_to_classinfo(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;
+ uintptr_t* datap = (uintptr_t*) pr->datap;
- /* get the classinfo */
+ // 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 the classinfo pointer.
+ *datap = (uintptr_t) c;
- /* patch the classinfo pointer */
-
- *((ptrint *) datap) = (ptrint) c;
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
bool patcher_invokestatic_special(patchref_t *pr)
{
- unresolved_method *um;
- u1 *datap;
- methodinfo *m;
-
- /* get stuff from the stack */
+ unresolved_method* um = (unresolved_method*) pr->ref;
+ uintptr_t* datap = (uintptr_t*) pr->datap;
- um = (unresolved_method*) pr->ref;
- datap = (u1 *) pr->datap;
+ // Reolve the method.
+ methodinfo* m = resolve_method_eager(um);
- /* get the methodinfo */
-
- if (!(m = resolve_method_eager(um)))
+ if (m == NULL)
return false;
- PATCH_BACK_ORIGINAL_MCODE;
-
- /* patch stubroutine */
+ // Patch stubroutine.
+ *datap = (uintptr_t) m->stubroutine;
- *((ptrint *) datap) = (ptrint) m->stubroutine;
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
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 methodinfo */
-
- if (!(m = resolve_method_eager(um)))
+ if (m == NULL)
return false;
- PATCH_BACK_ORIGINAL_MCODE;
-
- /* if we show disassembly, we have to skip the nop */
-
- if (opt_shownops)
- ra = ra + 1 * 4;
+ // Patch vftbl index.
+ gen_resolveload(pc[1], (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
- /* patch vftbl index */
+ // Synchronize instruction cache.
+ md_icacheflush(pc + 1, 1 * 4);
- gen_resolveload(*((s4 *) (ra + 1 * 4)), (s4) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
-
- /* synchronize instruction cache */
-
- md_icacheflush(ra + 1 * 4, 1 * 4);
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
bool patcher_invokeinterface(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 methodinfo */
-
- if (!(m = resolve_method_eager(um)))
+ if (m == NULL)
return false;
- PATCH_BACK_ORIGINAL_MCODE;
-
- /* if we show disassembly, we have to skip the nop */
-
- if (opt_shownops)
- ra = ra + 1 * 4;
+ // Patch interfacetable index.
+ gen_resolveload(pc[1], (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->clazz->index));
- /* patch interfacetable index */
+ // Patch method offset.
+ gen_resolveload(pc[2], (int32_t) (sizeof(methodptr) * (m - m->clazz->methods)));
- gen_resolveload(*((s4 *) (ra + 1 * 4)), (s4) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr*) * m->clazz->index));
+ // Synchronize instruction cache.
+ md_icacheflush(pc + 1, 2 * 4);
- /* patch method offset */
-
- gen_resolveload(*((s4 *) (ra + 2 * 4)), (s4) (sizeof(methodptr) * (m - m->clazz->methods)));
-
- /* synchronize instruction cache */
-
- md_icacheflush(ra + 1 * 4, 2 * 4);
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
bool patcher_resolve_classref_to_flags(patchref_t *pr)
{
- constant_classref *cr;
- u1 *datap;
- classinfo *c;
+ constant_classref* cr = (constant_classref*) pr->ref;
+ int32_t* datap = (int32_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 class flags */
+ // Patch class flags.
+ *datap = (int32_t) c->flags;
- *((s4 *) datap) = (s4) c->flags;
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
bool patcher_resolve_classref_to_index(patchref_t *pr)
{
- constant_classref *cr;
- u1 *datap;
- classinfo *c;
+ constant_classref* cr = (constant_classref*) pr->ref;
+ int32_t* datap = (int32_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 super class index */
+ // Patch super class index.
+ *datap = (int32_t) c->index;
- *((s4 *) datap) = (s4) c->index;
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}
bool patcher_resolve_classref_to_vftbl(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.
- cr = (constant_classref *) pr->ref;
- datap = (u1 *) pr->datap;
+ classinfo* c = resolve_classref_eager(cr);
- /* get the classinfo */
-
- if (!(c = resolve_classref_eager(cr)))
+ if (c == NULL)
return false;
- PATCH_BACK_ORIGINAL_MCODE;
-
- /* patch super class' vftbl */
+ // Patch super class' vftbl.
+ *datap = (uintptr_t) c->vftbl;
- *((ptrint *) datap) = (ptrint) c->vftbl;
+ // Patch back the original code.
+ patcher_patch_code(pr);
return true;
}