* Removed all Id tags.
[cacao.git] / src / vm / jit / replace.c
index f76e46e36a0118b00ba02aa5ebc4f695746e7dc6..03969f4a6125b2fcbb3ce0235002ae29d050b437 100644 (file)
@@ -1,6 +1,6 @@
-/* vm/jit/replace.c - on-stack replacement of methods
+/* src/vm/jit/replace.c - on-stack replacement of methods
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   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
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Edwin Steiner
-
-   $Id$
-
 */
 
 #include "config.h"
 #include "arch.h"
 
 #include "mm/memory.h"
+
+#include "threads/threads-common.h"
+
 #include "toolbox/logging.h"
-#include "vm/options.h"
+
 #include "vm/stringlocal.h"
+
 #include "vm/jit/abi.h"
-#include "vm/jit/jit.h"
-#include "vm/jit/replace.h"
-#include "vm/jit/stack.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/disass.h"
-#include "vm/jit/show.h"
+#include "vm/jit/jit.h"
+#include "vm/jit/md.h"
 #include "vm/jit/methodheader.h"
+#include "vm/jit/replace.h"
+#include "vm/jit/show.h"
+#include "vm/jit/stack.h"
+
+#include "vmcore/options.h"
+#include "vmcore/classcache.h"
 
-#include "native/include/java_lang_String.h"
 
 #define REPLACE_PATCH_DYNAMIC_CALL
 /*#define REPLACE_PATCH_ALL*/
 
+#if defined(ENABLE_VMLOG)
+#include <vmlog_cacao.h>
+#endif
 
 /*** architecture-dependent configuration *************************************/
 
 /* first unset the macros (default) */
 #undef REPLACE_RA_BETWEEN_FRAMES
 #undef REPLACE_RA_TOP_OF_FRAME
+#undef REPLACE_RA_LINKAGE_AREA
 #undef REPLACE_LEAFMETHODS_RA_REGISTER
+#undef REPLACE_REG_RA
 
-/* i386 and x86_64 */
-#if defined(__I386__) || defined(__X86_64__)
+/* i386, x86_64 and m68k */
+#if defined(__I386__) || defined(__X86_64__) || defined(__M68K__)
 #define REPLACE_RA_BETWEEN_FRAMES
 /* alpha */
 #elif defined(__ALPHA__)
 #define REPLACE_RA_TOP_OF_FRAME
 #define REPLACE_LEAFMETHODS_RA_REGISTER
+#define REPLACE_REG_RA REG_RA
+/* powerpc */
+#elif defined(__POWERPC__)
+#define REPLACE_RA_LINKAGE_AREA
+#define REPLACE_LEAFMETHODS_RA_REGISTER
+#define REPLACE_REG_RA REG_ITMP3 /* the execution state has the LR in itmp3 */
+/* s390 */
+#elif defined(__S390__)
+#define REPLACE_RA_TOP_OF_FRAME
+#define REPLACE_REG_RA REG_ITMP3
 #endif
 
 
@@ -91,15 +107,14 @@ typedef u8 stackslot_t;
 
 /*** debugging ****************************************************************/
 
-/*#define REPLACE_VERBOSE*/
-
 #if !defined(NDEBUG)
-static void java_value_print(s4 type, u8 value);
-#endif /* !defined(NDEBUG) */
+static void java_value_print(s4 type, replace_val_t value);
+static void replace_stackframeinfo_println(stackframeinfo *sfi);
+#endif
 
-#if !defined(NDEBUG) && defined(REPLACE_VERBOSE)
-#define DOLOG(code) do{ if (1) { code; } } while(0)
-#define DOLOG_SHORT(code) do{ if (1) { code; } } while(0)
+#if !defined(NDEBUG)
+#define DOLOG(code)        do{ if (opt_TraceReplacement > 1) { code; } } while(0)
+#define DOLOG_SHORT(code)  do{ if (opt_TraceReplacement > 0) { code; } } while(0)
 #else
 #define DOLOG(code)
 #define DOLOG_SHORT(code)
@@ -230,17 +245,17 @@ static void replace_create_replacement_point(jitdata *jd,
                                continue;
 
                        ra->index = i;
-                       if (index < UNUSED) {
-                               ra->regoff = (UNUSED - index) - 1;
-                               ra->type = TYPE_RET;
-                               ra->flags = 0;
-                       }
-                       else {
+                       if (index >= 0) {
                                v = VAR(index);
                                ra->flags = v->flags & (INMEMORY);
                                ra->regoff = v->vv.regoff;
                                ra->type = v->type;
                        }
+                       else {
+                               ra->regoff = RETADDR_FROM_JAVALOCAL(index);
+                               ra->type = TYPE_RET;
+                               ra->flags = 0;
+                       }
                        ra++;
                }
        }
@@ -597,6 +612,9 @@ bool replace_create_replacement_points(jitdata *jd)
                        replace_create_replacement_point(jd, iinfo, rp++,
                                        bptr->type, bptr->iinstr, &ra,
                                        bptr->javalocals, bptr->invars + i, bptr->indepth - i, 0);
+
+                       if (JITDATA_HAS_FLAG_COUNTDOWN(jd))
+                               rp[-1].flags |= RPLPOINT_FLAG_COUNTDOWN;
                }
 
                /* iterate over the instructions */
@@ -693,6 +711,9 @@ bool replace_create_replacement_points(jitdata *jd)
        code->globalcount   = 0;
        code->savedintcount = INT_SAV_CNT - rd->savintreguse;
        code->savedfltcount = FLT_SAV_CNT - rd->savfltreguse;
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+       code->savedadrcount = ADR_SAV_CNT - rd->savadrreguse;
+#endif
        code->memuse        = rd->memuse;
        code->stackframesize = jd->cd->stackframesize;
 
@@ -806,7 +827,7 @@ void replace_activate_replacement_points(codeinfo *code, bool mappable)
 
                savedmcode -= REPLACEMENT_PATCH_SIZE;
 
-#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__) || defined(__S390__)) && defined(ENABLE_JIT)
                md_patch_replacement_point(code, index, rp, savedmcode);
 #endif
                rp->flags |= RPLPOINT_FLAG_ACTIVE;
@@ -834,6 +855,23 @@ void replace_deactivate_replacement_points(codeinfo *code)
        s4        count;
        u1       *savedmcode;
 
+       if (code->savedmcode == NULL) {
+               /* disarm countdown points by patching the branches */
+
+               i = code->rplpointcount;
+               rp = code->rplpoints;
+               for (; i--; rp++) {
+                       if ((rp->flags & (RPLPOINT_FLAG_ACTIVE | RPLPOINT_FLAG_COUNTDOWN))
+                                       == RPLPOINT_FLAG_COUNTDOWN)
+                       {
+#if 0
+                               *(s4*) (rp->pc + 9) = 0; /* XXX machine dependent! */
+#endif
+                       }
+               }
+               return;
+       }
+
        assert(code->savedmcode != NULL);
        savedmcode = code->savedmcode;
 
@@ -851,7 +889,7 @@ void replace_deactivate_replacement_points(codeinfo *code)
                DOLOG( printf("deactivate replacement point:\n");
                           replace_replacement_point_println(rp, 1); fflush(stdout); );
 
-#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__) || defined(__S390__)) && defined(ENABLE_JIT)
                md_patch_replacement_point(code, -1, rp, savedmcode);
 #endif
 
@@ -880,7 +918,6 @@ void replace_deactivate_replacement_points(codeinfo *code)
    
    IN:
           es...............execution state
-          sp...............stack pointer of the execution state (XXX eliminate?)
           ra...............allocation
           javaval..........where to put the value
 
@@ -890,19 +927,18 @@ void replace_deactivate_replacement_points(codeinfo *code)
 *******************************************************************************/
 
 static void replace_read_value(executionstate_t *es,
-                                                          stackslot_t *sp,
                                                           rplalloc *ra,
-                                                          u8 *javaval)
+                                                          replace_val_t *javaval)
 {
        if (ra->flags & INMEMORY) {
                /* XXX HAS_4BYTE_STACKSLOT may not be the right discriminant here */
 #ifdef HAS_4BYTE_STACKSLOT
                if (IS_2_WORD_TYPE(ra->type)) {
-                       *javaval = *(u8*)(sp + ra->regoff);
+                       javaval->l = *(u8*)(es->sp + ra->regoff);
                }
                else {
 #endif
-                       *javaval = sp[ra->regoff];
+                       javaval->p = *(ptrint*)(es->sp + ra->regoff);
 #ifdef HAS_4BYTE_STACKSLOT
                }
 #endif
@@ -910,10 +946,25 @@ static void replace_read_value(executionstate_t *es,
        else {
                /* allocated register */
                if (IS_FLT_DBL_TYPE(ra->type)) {
-                       *javaval = es->fltregs[ra->regoff];
+                       javaval->d = es->fltregs[ra->regoff];
+
+                       if (ra->type == TYPE_FLT)
+                               javaval->f = javaval->d;
                }
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+               else if (IS_ADR_TYPE(ra->type)) {
+                       javaval->p = es->adrregs[ra->regoff];
+               }
+#endif
                else {
-                       *javaval = es->intregs[ra->regoff];
+#if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
+                       if (ra->type == TYPE_LNG) {
+                               javaval->words.lo = es->intregs[GET_LOW_REG(ra->regoff)];
+                               javaval->words.hi = es->intregs[GET_HIGH_REG(ra->regoff)];
+                       }
+                       else
+#endif /* defined(SUPPORT_COMBINE_INTEGER_REGISTERS) */
+                               javaval->p = es->intregs[ra->regoff];
                }
        }
 }
@@ -925,53 +976,99 @@ static void replace_read_value(executionstate_t *es,
    
    IN:
           es...............execution state
-          sp...............stack pointer of the execution state (XXX eliminate?)
           ra...............allocation
           *javaval.........the value
 
 *******************************************************************************/
 
 static void replace_write_value(executionstate_t *es,
-                                                           stackslot_t *sp,
                                                            rplalloc *ra,
-                                                           u8 *javaval)
+                                                           replace_val_t *javaval)
 {
        if (ra->flags & INMEMORY) {
                /* XXX HAS_4BYTE_STACKSLOT may not be the right discriminant here */
 #ifdef HAS_4BYTE_STACKSLOT
                if (IS_2_WORD_TYPE(ra->type)) {
-                       *(u8*)(sp + ra->regoff) = *javaval;
+                       *(u8*)(es->sp + ra->regoff) = javaval->l;
                }
                else {
 #endif
-                       sp[ra->regoff] = *javaval;
+                       *(ptrint*)(es->sp + ra->regoff) = javaval->p;
 #ifdef HAS_4BYTE_STACKSLOT
                }
 #endif
        }
        else {
                /* allocated register */
-               if (IS_FLT_DBL_TYPE(ra->type)) {
-                       es->fltregs[ra->regoff] = *javaval;
-               }
-               else {
-                       es->intregs[ra->regoff] = *javaval;
+               switch (ra->type) {
+                       case TYPE_FLT:
+                               es->fltregs[ra->regoff] = (double) javaval->f;
+                               break;
+                       case TYPE_DBL:
+                               es->fltregs[ra->regoff] = javaval->d;
+                               break;
+#if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
+                       case TYPE_LNG:
+                               es->intregs[GET_LOW_REG(ra->regoff)] = javaval->words.lo;
+                               es->intregs[GET_HIGH_REG(ra->regoff)] = javaval->words.hi;
+                               break;
+#endif
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+                       case TYPE_ADR:
+                               es->adrregs[ra->regoff] = javaval->p;
+#endif
+                       default:
+                               es->intregs[ra->regoff] = javaval->p;
                }
        }
 }
 
 
-/* replace_read_executionstate *************************************************
+/* replace_new_sourceframe *****************************************************
 
-   Read the given executions state and translate it to a source frame.
+   Allocate a new source frame and insert it at the front of the frame list.
    
+   IN:
+          ss...............the source state
+
+   OUT:
+          ss->frames.......set to new frame (the new head of the frame list).
+
+   RETURN VALUE:
+       returns the new frame
+
+*******************************************************************************/
+
+static sourceframe_t *replace_new_sourceframe(sourcestate_t *ss)
+{
+       sourceframe_t *frame;
+
+       frame = DNEW(sourceframe_t);
+       MZERO(frame, sourceframe_t, 1);
+
+       frame->down = ss->frames;
+       ss->frames = frame;
+
+       return frame;
+}
+
+
+/* replace_read_executionstate *************************************************
+
+   Read a source frame from the given executions state.
+   The new source frame is pushed to the front of the frame list of the
+   source state.
+
    IN:
        rp...............replacement point at which `es` was taken
           es...............execution state
-          ss...............where to put the source state
+          ss...............the source state to add the source frame to
+          topframe.........true, if the first (top-most) source frame on the
+                           stack is to be read
 
    OUT:
-       *ss..............the source state derived from the execution state
+       *ss..............the source state with the newly created source frame
+                           added
   
 *******************************************************************************/
 
@@ -985,6 +1082,7 @@ static s4 replace_normalize_type_map[] = {
 /* RPLPOINT_TYPE_BODY   |--> */ RPLPOINT_TYPE_STD
 };
 
+
 static void replace_read_executionstate(rplpoint *rp,
                                                                                executionstate_t *es,
                                                                                sourcestate_t *ss,
@@ -1016,45 +1114,39 @@ static void replace_read_executionstate(rplpoint *rp,
 
        /* calculate base stack pointer */
 
-       basesp = sp + code_get_stack_frame_size(code);
+       basesp = sp + code->stackframesize;
 
        /* create the source frame */
 
-       frame = DNEW(sourceframe_t);
-       frame->down = ss->frames;
+       frame = replace_new_sourceframe(ss);
        frame->method = rp->method;
        frame->id = rp->id;
        assert(rp->type >= 0 && rp->type < sizeof(replace_normalize_type_map)/sizeof(s4));
        frame->type = replace_normalize_type_map[rp->type];
-       frame->instance = 0;
-       frame->syncslotcount = 0;
-       frame->syncslots = NULL;
        frame->fromrp = rp;
        frame->fromcode = code;
-       frame->torp = NULL;
-       frame->tocode = NULL;
-
-       ss->frames = frame;
 
        /* read local variables */
 
        count = m->maxlocals;
        frame->javalocalcount = count;
-       frame->javalocals = DMNEW(u8, count);
+       frame->javalocals = DMNEW(replace_val_t, count);
        frame->javalocaltype = DMNEW(u1, count);
 
-#if !defined(NDEBUG)
        /* mark values as undefined */
        for (i=0; i<count; ++i) {
-               frame->javalocals[i] = (u8) 0x00dead0000dead00ULL;
+#if !defined(NDEBUG)
+               frame->javalocals[i].l = (u8) 0x00dead0000dead00ULL;
+#endif
                frame->javalocaltype[i] = TYPE_VOID;
        }
 
        /* some entries in the intregs array are not meaningful */
        /*es->intregs[REG_ITMP3] = (u8) 0x11dead1111dead11ULL;*/
-       es->intregs[REG_SP   ] = (u8) 0x11dead1111dead11ULL;
+#if !defined(NDEBUG)
+       es->intregs[REG_SP   ] = (ptrint) 0x11dead1111dead11ULL;
 #ifdef REG_PV
-       es->intregs[REG_PV   ] = (u8) 0x11dead1111dead11ULL;
+       es->intregs[REG_PV   ] = (ptrint) 0x11dead1111dead11ULL;
 #endif
 #endif /* !defined(NDEBUG) */
 
@@ -1067,9 +1159,9 @@ static void replace_read_executionstate(rplpoint *rp,
                assert(i < m->maxlocals);
                frame->javalocaltype[i] = ra->type;
                if (ra->type == TYPE_RET)
-                       frame->javalocals[i] = ra->regoff;
+                       frame->javalocals[i].i = ra->regoff;
                else
-                       replace_read_value(es, sp, ra, frame->javalocals + i);
+                       replace_read_value(es, ra, frame->javalocals + i);
                ra++;
                count--;
        }
@@ -1094,12 +1186,12 @@ static void replace_read_executionstate(rplpoint *rp,
                instra.regoff = md->params[0].regoff;
                if (md->params[0].inmemory) {
                        instra.flags = INMEMORY;
-                       instra.regoff += (1 + code->stackframesize);
+                       instra.regoff += (1 + code->stackframesize) * SIZE_OF_STACKSLOT;
                }
                else {
                        instra.flags = 0;
                }
-               replace_read_value(es, sp, &instra, &(frame->instance));
+               replace_read_value(es, &instra, &(frame->instance));
 #endif
        }
 #endif /* defined(REPLACE_PATCH_DYNAMIC_CALL) */
@@ -1107,13 +1199,13 @@ static void replace_read_executionstate(rplpoint *rp,
        /* read stack slots */
 
        frame->javastackdepth = count;
-       frame->javastack = DMNEW(u8, count);
+       frame->javastack = DMNEW(replace_val_t, count);
        frame->javastacktype = DMNEW(u1, count);
 
 #if !defined(NDEBUG)
        /* mark values as undefined */
        for (i=0; i<count; ++i) {
-               frame->javastack[i] = (u8) 0x00dead0000dead00ULL;
+               frame->javastack[i].l = (u8) 0x00dead0000dead00ULL;
                frame->javastacktype[i] = TYPE_VOID;
        }
 #endif /* !defined(NDEBUG) */
@@ -1126,7 +1218,8 @@ static void replace_read_executionstate(rplpoint *rp,
                assert(count);
 
                assert(ra->index == RPLALLOC_STACK);
-               frame->javastack[i] = sp[-1];
+               assert(ra->type == TYPE_ADR);
+               frame->javastack[i].p = sp[-1];
                frame->javastacktype[i] = TYPE_ADR; /* XXX RET */
                count--;
                i++;
@@ -1136,7 +1229,8 @@ static void replace_read_executionstate(rplpoint *rp,
                assert(count);
 
                assert(ra->index == RPLALLOC_STACK);
-               frame->javastack[i] = es->intregs[REG_ITMP1];
+               assert(ra->type == TYPE_ADR);
+               frame->javastack[i].p = es->intregs[REG_ITMP1];
                frame->javastacktype[i] = TYPE_ADR; /* XXX RET */
                count--;
                i++;
@@ -1146,7 +1240,7 @@ static void replace_read_executionstate(rplpoint *rp,
                assert(count);
 
                assert(ra->index == RPLALLOC_STACK);
-               frame->javastack[i] = 0;
+               frame->javastack[i].l = 0;
                frame->javastacktype[i] = TYPE_VOID;
                count--;
                i++;
@@ -1168,8 +1262,8 @@ static void replace_read_executionstate(rplpoint *rp,
                                assert(calleeframe->syncslots == NULL);
 
                                calleeframe->syncslotcount = 1;
-                               calleeframe->syncslots = DMNEW(u8, 1);
-                               replace_read_value(es,sp,ra,calleeframe->syncslots);
+                               calleeframe->syncslots = DMNEW(replace_val_t, 1);
+                               replace_read_value(es,ra,calleeframe->syncslots);
                        }
 
                        frame->javastackdepth--;
@@ -1185,9 +1279,9 @@ static void replace_read_executionstate(rplpoint *rp,
                }
                else {
                        if (ra->type == TYPE_RET)
-                               frame->javastack[i] = ra->regoff;
+                               frame->javastack[i].i = ra->regoff;
                        else
-                               replace_read_value(es,sp,ra,frame->javastack + i);
+                               replace_read_value(es,ra,frame->javastack + i);
                        frame->javastacktype[i] = ra->type;
                        i++;
                }
@@ -1197,13 +1291,16 @@ static void replace_read_executionstate(rplpoint *rp,
 
 /* replace_write_executionstate ************************************************
 
-   Translate the given source state into an execution state.
-   
+   Pop a source frame from the front of the frame list of the given source state
+   and write its values into the execution state.
+
    IN:
        rp...............replacement point for which execution state should be
-                           creates
-          es...............where to put the execution state
+                           created
+          es...............the execution state to modify
           ss...............the given source state
+          topframe.........true, if this is the last (top-most) source frame to be
+                           translated
 
    OUT:
        *es..............the execution state derived from the source state
@@ -1239,7 +1336,7 @@ static void replace_write_executionstate(rplpoint *rp,
 
        sp = (stackslot_t *) es->sp;
 
-       basesp = sp + code_get_stack_frame_size(code);
+       basesp = sp + code->stackframesize;
 
        /* in some cases the top stack slot is passed in REG_ITMP1 */
 
@@ -1260,7 +1357,7 @@ static void replace_write_executionstate(rplpoint *rp,
                        /* XXX assert that it matches this rplpoint */
                }
                else
-                       replace_write_value(es, sp, ra, frame->javalocals + i);
+                       replace_write_value(es, ra, frame->javalocals + i);
                count--;
                ra++;
        }
@@ -1277,7 +1374,7 @@ static void replace_write_executionstate(rplpoint *rp,
                assert(ra->index == RPLALLOC_STACK);
                assert(i < frame->javastackdepth);
                assert(frame->javastacktype[i] == TYPE_ADR);
-               sp[-1] = frame->javastack[i];
+               sp[-1] = frame->javastack[i].p;
                count--;
                i++;
                ra++;
@@ -1288,7 +1385,7 @@ static void replace_write_executionstate(rplpoint *rp,
                assert(ra->index == RPLALLOC_STACK);
                assert(i < frame->javastackdepth);
                assert(frame->javastacktype[i] == TYPE_ADR);
-               es->intregs[REG_ITMP1] = frame->javastack[i];
+               es->intregs[REG_ITMP1] = frame->javastack[i].p;
                count--;
                i++;
                ra++;
@@ -1317,7 +1414,7 @@ static void replace_write_executionstate(rplpoint *rp,
                                assert(frame->down->syncslotcount == 1); /* XXX need to understand more cases */
                                assert(frame->down->syncslots != NULL);
 
-                               replace_write_value(es,sp,ra,frame->down->syncslots);
+                               replace_write_value(es,ra,frame->down->syncslots);
                        }
                        continue;
                }
@@ -1336,7 +1433,7 @@ static void replace_write_executionstate(rplpoint *rp,
                                /* XXX assert that it matches this rplpoint */
                        }
                        else {
-                               replace_write_value(es,sp,ra,frame->javastack + i);
+                               replace_write_value(es,ra,frame->javastack + i);
                        }
                        i++;
                }
@@ -1383,7 +1480,7 @@ u1* replace_pop_activation_record(executionstate_t *es,
 
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
        if (CODE_IS_LEAFMETHOD(es->code))
-               ra = (u1*) es->intregs[REG_RA];
+               ra = (u1*) (ptrint) es->intregs[REPLACE_REG_RA];
        else
 #endif
                ra = md_stacktrace_get_returnaddress(es->sp,
@@ -1391,21 +1488,7 @@ u1* replace_pop_activation_record(executionstate_t *es,
 
        DOLOG( printf("return address: %p\n", (void*)ra); );
 
-       /* find the new codeinfo */
-
-       pv = md_codegen_get_pv_from_pc(ra);
-
-       DOLOG( printf("PV = %p\n", (void*) pv); );
-
-       if (pv == NULL)
-               return NULL;
-
-       code = *(codeinfo **)(pv + CodeinfoPointer);
-
-       DOLOG( printf("CODE = %p\n", (void*) code); );
-
-       if (code == NULL)
-               return NULL;
+       assert(ra);
 
        /* calculate the base of the stack frame */
 
@@ -1418,9 +1501,9 @@ u1* replace_pop_activation_record(executionstate_t *es,
        assert(frame->syncslots == NULL);
        count = code_get_sync_slot_count(es->code);
        frame->syncslotcount = count;
-       frame->syncslots = DMNEW(u8, count);
+       frame->syncslots = DMNEW(replace_val_t, count);
        for (i=0; i<count; ++i) {
-               frame->syncslots[i] = sp[es->code->memuse + i];
+               frame->syncslots[i].p = sp[es->code->memuse + i]; /* XXX */
        }
 
        /* restore return address, if part of frame */
@@ -1429,9 +1512,16 @@ u1* replace_pop_activation_record(executionstate_t *es,
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
        if (!CODE_IS_LEAFMETHOD(es->code))
 #endif
-               es->intregs[REG_RA] = *--basesp;
+               es->intregs[REPLACE_REG_RA] = *--basesp;
 #endif /* REPLACE_RA_TOP_OF_FRAME */
 
+#if defined(REPLACE_RA_LINKAGE_AREA)
+#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
+       if (!CODE_IS_LEAFMETHOD(es->code))
+#endif
+               es->intregs[REPLACE_REG_RA] = basesp[LA_LR_OFFSET / sizeof(stackslot_t)];
+#endif /* REPLACE_RA_LINKAGE_AREA */
+
        /* restore saved int registers */
 
        reg = INT_REG_CNT;
@@ -1449,13 +1539,19 @@ u1* replace_pop_activation_record(executionstate_t *es,
                while (nregdescfloat[--reg] != REG_SAV)
                        ;
                basesp -= STACK_SLOTS_PER_FLOAT;
-               es->fltregs[reg] = *(u8*)basesp;
+               es->fltregs[reg] = *(double*)basesp;
        }
 
-       /* Set the new pc. Subtract one so we do not hit the replacement point */
-       /* of the instruction following the call, if there is one.             */
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+       /* restore saved adr registers */
 
-       es->pc = ra - 1;
+       reg = ADR_REG_CNT;
+       for (i=0; i<es->code->savedadrcount; ++i) {
+               while (nregdescadr[--reg] != REG_SAV)
+                       ;
+               es->adrregs[reg] = *--basesp;
+       }
+#endif
 
        /* adjust the stackpointer */
 
@@ -1465,24 +1561,51 @@ u1* replace_pop_activation_record(executionstate_t *es,
        es->sp += SIZE_OF_STACKSLOT; /* skip return address */
 #endif
 
+       /* Set the new pc. Subtract one so we do not hit the replacement point */
+       /* of the instruction following the call, if there is one.             */
+
+       es->pc = ra - 1;
+
+       /* find the new codeinfo */
+
+       pv = md_codegen_get_pv_from_pc(ra);
+
+       DOLOG( printf("PV = %p\n", (void*) pv); );
+
+       if (pv == NULL) /* XXX can this really happen? */
+               return NULL;
+
+       code = *(codeinfo **)(pv + CodeinfoPointer);
+
+       DOLOG( printf("CODE = %p\n", (void*) code); );
+
+       /* return NULL if we reached native code */
+
        es->pv = pv;
        es->code = code;
 
+       /* in debugging mode clobber non-saved registers */
+
 #if !defined(NDEBUG)
        /* for debugging */
        for (i=0; i<INT_REG_CNT; ++i)
                if ((nregdescint[i] != REG_SAV)
 #if defined(REG_RA)
-                               && (i != REG_RA)
+                               && (i != REPLACE_REG_RA)
 #endif
                        )
-                       es->intregs[i] = 0x33dead3333dead33ULL;
+                       es->intregs[i] = (ptrint) 0x33dead3333dead33ULL;
        for (i=0; i<FLT_REG_CNT; ++i)
                if (nregdescfloat[i] != REG_SAV)
-                       es->fltregs[i] = 0x33dead3333dead33ULL;
+                       *(u8*)&(es->fltregs[i]) = 0x33dead3333dead33ULL;
+# if defined(HAS_ADDRESS_REGISTER_FILE)
+       for (i=0; i<ADR_REG_CNT; ++i)
+               if (nregdescadr[i] != REG_SAV)
+                       es->adrregs[i] = (ptrint) 0x33dead3333dead33ULL;
+# endif
 #endif /* !defined(NDEBUG) */
 
-       return ra;
+       return (code) ? ra : NULL;
 }
 
 
@@ -1528,6 +1651,103 @@ static void replace_patch_method_pointer(methodptr *mpp,
 }
 
 
+/* replace_patch_class *********************************************************
+
+   Patch a method in the given class.
+
+   IN:
+          vftbl............vftbl of the class
+          m................the method to patch
+          oldentrypoint....the old entrypoint to replace
+          entrypoint.......the new entrypoint
+
+*******************************************************************************/
+
+void replace_patch_class(vftbl_t *vftbl,
+                                                methodinfo *m,
+                                                u1 *oldentrypoint,
+                                                u1 *entrypoint)
+{
+       s4                 i;
+       methodptr         *mpp;
+       methodptr         *mppend;
+
+       /* patch the vftbl of the class */
+
+       replace_patch_method_pointer(vftbl->table + m->vftblindex,
+                                                                entrypoint,
+                                                                "virtual  ");
+
+       /* patch the interface tables */
+
+       assert(oldentrypoint);
+
+       for (i=0; i < vftbl->interfacetablelength; ++i) {
+               mpp = vftbl->interfacetable[-i];
+               mppend = mpp + vftbl->interfacevftbllength[i];
+               for (; mpp != mppend; ++mpp)
+                       if (*mpp == oldentrypoint) {
+                               replace_patch_method_pointer(mpp, entrypoint, "interface");
+                       }
+       }
+}
+
+
+/* replace_patch_class_hierarchy ***********************************************
+
+   Patch a method in all loaded classes.
+
+   IN:
+          m................the method to patch
+          oldentrypoint....the old entrypoint to replace
+          entrypoint.......the new entrypoint
+
+*******************************************************************************/
+
+struct replace_patch_data_t {
+       methodinfo *m;
+       u1         *oldentrypoint;
+       u1         *entrypoint;
+};
+
+#define CODEINFO_OF_CODE(entrypoint) \
+       (*(codeinfo **)((u1*)(entrypoint) + CodeinfoPointer))
+
+#define METHOD_OF_CODE(entrypoint) \
+       (CODEINFO_OF_CODE(entrypoint)->m)
+
+void replace_patch_callback(classinfo *c, struct replace_patch_data_t *pd)
+{
+       vftbl_t *vftbl = c->vftbl;
+
+       if (vftbl != NULL
+               && vftbl->vftbllength > pd->m->vftblindex
+               && vftbl->table[pd->m->vftblindex] != &asm_abstractmethoderror
+               && METHOD_OF_CODE(vftbl->table[pd->m->vftblindex]) == pd->m)
+       {
+               replace_patch_class(c->vftbl, pd->m, pd->oldentrypoint, pd->entrypoint);
+       }
+}
+
+void replace_patch_class_hierarchy(methodinfo *m,
+                                                                  u1 *oldentrypoint,
+                                                                  u1 *entrypoint)
+{
+       struct replace_patch_data_t pd;
+
+       pd.m = m;
+       pd.oldentrypoint = oldentrypoint;
+       pd.entrypoint = entrypoint;
+
+       DOLOG_SHORT( printf("patching class hierarchy: ");
+                            method_println(m); );
+
+       classcache_foreach_loaded_class(
+                       (classcache_foreach_functionptr_t) &replace_patch_callback,
+                       (void*) &pd);
+}
+
+
 /* replace_patch_future_calls **************************************************
 
    Analyse a call site and depending on the kind of call patch the call, the
@@ -1544,18 +1764,15 @@ void replace_patch_future_calls(u1 *ra,
                                                                sourceframe_t *callerframe,
                                                                sourceframe_t *calleeframe)
 {
-       u1                *patchpos;
-       methodptr          entrypoint;
-       methodptr          oldentrypoint;
-       methodptr         *mpp;
-       methodptr         *mppend;
-       bool               atentry;
-       stackframeinfo     sfi;
-       codeinfo          *calleecode;
-       methodinfo        *calleem;
-       java_objectheader *obj;
-       struct _vftbl     *vftbl;
-       s4                 i;
+       u1             *patchpos;
+       methodptr       entrypoint;
+       methodptr       oldentrypoint;
+       bool            atentry;
+       stackframeinfo  sfi;
+       codeinfo       *calleecode;
+       methodinfo     *calleem;
+       java_object_t  *obj;
+       vftbl_t        *vftbl;
 
        assert(ra);
        assert(callerframe->down == calleeframe);
@@ -1591,46 +1808,31 @@ void replace_patch_future_calls(u1 *ra,
 
                assert((calleem->flags & ACC_STATIC) == 0);
 
+               oldentrypoint = calleeframe->fromcode->entrypoint;
+
                /* we need to know the instance */
 
-               if (!calleeframe->instance) {
+               if (!calleeframe->instance.a) {
                        DOLOG_SHORT( printf("WARNING: object instance unknown!\n"); );
+                       replace_patch_class_hierarchy(calleem, oldentrypoint, entrypoint);
                        return;
                }
 
                /* get the vftbl */
 
-               obj = (java_objectheader *) (ptrint) calleeframe->instance;
+               obj = calleeframe->instance.a;
                vftbl = obj->vftbl;
 
                assert(vftbl->class->vftbl == vftbl);
 
                DOLOG_SHORT( printf("\tclass: "); class_println(vftbl->class); );
 
-               /* patch the vftbl of the class */
-
-               replace_patch_method_pointer(vftbl->table + calleem->vftblindex,
-                                                                        entrypoint,
-                                                                        "virtual");
-
-               /* patch the interface tables */
-
-               oldentrypoint = calleeframe->fromcode->entrypoint;
-               assert(oldentrypoint);
-
-               for (i=0; i < vftbl->interfacetablelength; ++i) {
-                       mpp = vftbl->interfacetable[-i];
-                       mppend = mpp + vftbl->interfacevftbllength[i];
-                       for (; mpp != mppend; ++mpp)
-                               if (*mpp == oldentrypoint) {
-                                       replace_patch_method_pointer(mpp, entrypoint, "interface");
-                               }
-               }
+               replace_patch_class(vftbl, calleem, oldentrypoint, entrypoint);
        }
        else {
                /* the call was statically bound */
 
-               replace_patch_method_pointer((methodptr *) patchpos, entrypoint, "static");
+               replace_patch_method_pointer((methodptr *) patchpos, entrypoint, "static   ");
        }
 }
 
@@ -1645,7 +1847,8 @@ void replace_patch_future_calls(u1 *ra,
    IN:
           es...............execution state
           rpcall...........the replacement point at the call site
-          callerframe......source frame of the caller
+          callerframe......source frame of the caller, or NULL for creating the
+                           first frame
           calleeframe......source frame of the callee, must have been mapped
 
    OUT:
@@ -1667,10 +1870,11 @@ void replace_push_activation_record(executionstate_t *es,
        codeinfo    *calleecode;
 
        assert(es);
-       assert(rpcall && rpcall->type == RPLPOINT_TYPE_CALL);
-       assert(callerframe);
+       assert(!rpcall || callerframe);
+    assert(!rpcall || rpcall->type == RPLPOINT_TYPE_CALL);
+       assert(!rpcall || rpcall == callerframe->torp);
        assert(calleeframe);
-       assert(calleeframe == callerframe->down);
+       assert(!callerframe || calleeframe == callerframe->down);
 
        /* the compilation unit we are entering */
 
@@ -1679,7 +1883,10 @@ void replace_push_activation_record(executionstate_t *es,
 
        /* calculate the return address */
 
-       ra = rpcall->pc + rpcall->callsize;
+       if (rpcall)
+               ra = rpcall->pc + rpcall->callsize;
+       else
+               ra = es->pc + 1 /* XXX this is ugly */;
 
        /* write the return address */
 
@@ -1689,15 +1896,15 @@ void replace_push_activation_record(executionstate_t *es,
        *((stackslot_t *)es->sp) = (stackslot_t) ra;
 #endif /* REPLACE_RA_BETWEEN_FRAMES */
 
-#if defined(REPLACE_RA_TOP_OF_FRAME)
-       es->intregs[REG_RA] = (u8) ra;
+#if defined(REPLACE_REG_RA)
+       es->intregs[REPLACE_REG_RA] = (ptrint) ra;
 #endif
 
        /* we move into a new code unit */
 
        es->code = calleecode;
 
-       /* set the new pc XXX not needed */
+       /* set the new pc XXX not needed? */
 
        es->pc = calleecode->entrypoint;
 
@@ -1714,19 +1921,29 @@ void replace_push_activation_record(executionstate_t *es,
 
        /* in debug mode, invalidate stack frame first */
 
-#if !defined(NDEBUG)
+       /* XXX may not invalidate linkage area used by native code! */
+#if !defined(NDEBUG) && 0
        for (i=0; i<(basesp - sp); ++i) {
                sp[i] = 0xdeaddeadU;
        }
 #endif
 
+       /* save the return address register */
+
 #if defined(REPLACE_RA_TOP_OF_FRAME)
 #if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
        if (!CODE_IS_LEAFMETHOD(calleecode))
 #endif
-               *--basesp = ra;
+               *--basesp = (ptrint) ra;
 #endif /* REPLACE_RA_TOP_OF_FRAME */
 
+#if defined(REPLACE_RA_LINKAGE_AREA)
+#if defined(REPLACE_LEAFMETHODS_RA_REGISTER)
+       if (!CODE_IS_LEAFMETHOD(calleecode))
+#endif
+               basesp[LA_LR_OFFSET / sizeof(stackslot_t)] = (ptrint) ra;
+#endif /* REPLACE_RA_LINKAGE_AREA */
+
        /* save int registers */
 
        reg = INT_REG_CNT;
@@ -1735,8 +1952,9 @@ void replace_push_activation_record(executionstate_t *es,
                        ;
                *--basesp = es->intregs[reg];
 
-#if !defined(NDEBUG)
-               es->intregs[reg] = 0x44dead4444dead44ULL;
+               /* XXX may not clobber saved regs used by native code! */
+#if !defined(NDEBUG) && 0
+               es->intregs[reg] = (ptrint) 0x44dead4444dead44ULL;
 #endif
        }
 
@@ -1748,19 +1966,36 @@ void replace_push_activation_record(executionstate_t *es,
                while (nregdescfloat[--reg] != REG_SAV)
                        ;
                basesp -= STACK_SLOTS_PER_FLOAT;
-               *(u8*)basesp = es->fltregs[reg];
+               *(double*)basesp = es->fltregs[reg];
 
-#if !defined(NDEBUG)
-               es->fltregs[reg] = 0x44dead4444dead44ULL;
+               /* XXX may not clobber saved regs used by native code! */
+#if !defined(NDEBUG) && 0
+               *(u8*)&(es->fltregs[reg]) = 0x44dead4444dead44ULL;
+#endif
+       }
+
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+       /* save adr registers */
+
+       reg = ADR_REG_CNT;
+       for (i=0; i<calleecode->savedadrcount; ++i) {
+               while (nregdescadr[--reg] != REG_SAV)
+                       ;
+               *--basesp = es->adrregs[reg];
+
+               /* XXX may not clobber saved regs used by native code! */
+#if !defined(NDEBUG) && 0
+               es->adrregs[reg] = (ptrint) 0x44dead4444dead44ULL;
 #endif
        }
+#endif
 
        /* write slots used for synchronization */
 
        count = code_get_sync_slot_count(calleecode);
        assert(count == calleeframe->syncslotcount);
        for (i=0; i<count; ++i) {
-               sp[calleecode->memuse + i] = calleeframe->syncslots[i];
+               sp[calleecode->memuse + i] = calleeframe->syncslots[i].p;
        }
 
        /* set the PV */
@@ -1769,12 +2004,14 @@ void replace_push_activation_record(executionstate_t *es,
 
        /* redirect future invocations */
 
+       if (callerframe && rpcall) {
 #if defined(REPLACE_PATCH_ALL)
-       if (rpcall->type == callerframe->fromrp->type)
+               if (rpcall->type == callerframe->fromrp->type)
 #else
-       if (rpcall == callerframe->fromrp)
+               if (rpcall == callerframe->fromrp)
 #endif
-               replace_patch_future_calls(ra, callerframe, calleeframe);
+                       replace_patch_future_calls(ra, callerframe, calleeframe);
+       }
 }
 
 
@@ -1804,6 +2041,7 @@ rplpoint * replace_find_replacement_point(codeinfo *code,
        s4        stacki;
        rplalloc *ra;
 
+       assert(code);
        assert(frame);
 
        DOLOG( printf("searching replacement point for:\n");
@@ -1830,13 +2068,13 @@ rplpoint * replace_find_replacement_point(codeinfo *code,
                                if (ra->type == TYPE_RET) {
                                        if (ra->index == RPLALLOC_STACK) {
                                                assert(stacki < frame->javastackdepth);
-                                               if (frame->javastack[stacki] != ra->regoff)
+                                               if (frame->javastack[stacki].i != ra->regoff)
                                                        goto no_match;
                                                stacki++;
                                        }
                                        else {
                                                assert(ra->index >= 0 && ra->index < frame->javalocalcount);
-                                               if (frame->javalocals[ra->index] != ra->regoff)
+                                               if (frame->javalocals[ra->index].i != ra->regoff)
                                                        goto no_match;
                                        }
                                }
@@ -1883,24 +2121,210 @@ rplpoint *replace_find_replacement_point_for_pc(codeinfo *code, u1 *pc)
        rplpoint *rp;
        s4        i;
 
+       DOLOG( printf("searching for rp in %p ", (void*)code);
+                  method_println(code->m); );
+
        found = NULL;
 
        rp = code->rplpoints;
-       for (i=0; i<code->rplpointcount; ++i, ++rp)
+       for (i=0; i<code->rplpointcount; ++i, ++rp) {
+               DOLOG( replace_replacement_point_println(rp, 2); );
                if (rp->pc <= pc)
                        found = rp;
+       }
 
        return found;
 }
 
 
+/* replace_pop_native_frame ****************************************************
+
+   Unroll a native frame in the execution state and create a source frame
+   for it.
+
+   IN:
+          es...............current execution state
+          ss...............the current source state
+          sfi..............stackframeinfo for the native frame
+
+   OUT:
+       es...............execution state after unrolling the native frame
+          ss...............gets the added native source frame
+
+*******************************************************************************/
+
+static void replace_pop_native_frame(executionstate_t *es,
+                                                                        sourcestate_t *ss,
+                                                                        stackframeinfo *sfi)
+{
+       sourceframe_t *frame;
+       codeinfo      *code;
+       s4             i,j;
+
+       assert(sfi);
+
+       frame = replace_new_sourceframe(ss);
+
+       frame->sfi = sfi;
+
+       /* remember pc and size of native frame */
+
+       frame->nativepc = es->pc;
+       frame->nativeframesize = sfi->sp - es->sp;
+       assert(frame->nativeframesize >= 0);
+
+       /* remember values of saved registers */
+
+       j = 0;
+       for (i=0; i<INT_REG_CNT; ++i) {
+               if (nregdescint[i] == REG_SAV)
+                       frame->nativesavint[j++] = es->intregs[i];
+       }
+
+       j = 0;
+       for (i=0; i<FLT_REG_CNT; ++i) {
+               if (nregdescfloat[i] == REG_SAV)
+                       frame->nativesavflt[j++] = es->fltregs[i];
+       }
+
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+       j = 0;
+       for (i=0; i<ADR_REG_CNT; ++i) {
+               if (nregdescadr[i] == REG_SAV)
+                       frame->nativesavadr[j++] = es->adrregs[i];
+       }
+#endif
+
+       /* restore saved registers */
+
+#if 0
+       /* XXX we don't have them, yet, in the sfi, so clear them */
+
+       for (i=0; i<INT_REG_CNT; ++i) {
+               if (nregdescint[i] == REG_SAV)
+                       es->intregs[i] = 0;
+       }
+
+       for (i=0; i<FLT_REG_CNT; ++i) {
+               if (nregdescfloat[i] == REG_SAV)
+                       es->fltregs[i] = 0.0;
+       }
+
+# if defined(HAS_ADDRESS_REGISTER_FILE)
+       for (i=0; i<ADR_REG_CNT; ++i) {
+               if (nregdescadr[i] == REG_SAV)
+                       es->adrregs[i] = 0;
+       }
+# endif
+#endif
+
+       /* restore pv, pc, and sp */
+
+       if (sfi->pv == NULL) {
+               /* frame of a native function call */
+               es->pv = md_codegen_get_pv_from_pc(sfi->ra);
+       }
+       else {
+               es->pv = sfi->pv;
+       }
+       es->pc = ((sfi->xpc) ? sfi->xpc : sfi->ra) - 1;
+       es->sp = sfi->sp;
+
+       /* find the new codeinfo */
+
+       DOLOG( printf("PV = %p\n", (void*) es->pv); );
+
+       assert(es->pv != NULL);
+
+       code = *(codeinfo **)(es->pv + CodeinfoPointer);
+
+       DOLOG( printf("CODE = %p\n", (void*) code); );
+
+       es->code = code;
+}
+
+
+/* replace_push_native_frame ***************************************************
+
+   Rebuild a native frame onto the execution state and remove its source frame.
+
+   Note: The native frame is "rebuild" by setting fields like PC and stack
+         pointer in the execution state accordingly. Values in the
+                stackframeinfo may be modified, but the actual stack frame of the
+                native code is not touched.
+
+   IN:
+          es...............current execution state
+          ss...............the current source state
+
+   OUT:
+       es...............execution state after re-rolling the native frame
+          ss...............the native source frame is removed
+
+*******************************************************************************/
+
+static void replace_push_native_frame(executionstate_t *es, sourcestate_t *ss)
+{
+       sourceframe_t *frame;
+       s4             i,j;
+
+       assert(es);
+       assert(ss);
+
+       DOLOG( printf("pushing native frame\n"); );
+
+       /* remove the frame from the source state */
+
+       frame = ss->frames;
+       assert(frame);
+       assert(REPLACE_IS_NATIVE_FRAME(frame));
+
+       ss->frames = frame->down;
+
+       /* assert that the native frame has not moved */
+
+       assert(es->sp == frame->sfi->sp);
+
+       /* restore saved registers */
+
+       j = 0;
+       for (i=0; i<INT_REG_CNT; ++i) {
+               if (nregdescint[i] == REG_SAV)
+                       es->intregs[i] = frame->nativesavint[j++];
+       }
+
+       j = 0;
+       for (i=0; i<FLT_REG_CNT; ++i) {
+               if (nregdescfloat[i] == REG_SAV)
+                       es->fltregs[i] = frame->nativesavflt[j++];
+       }
+
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+       j = 0;
+       for (i=0; i<ADR_REG_CNT; ++i) {
+               if (nregdescadr[i] == REG_SAV)
+                       es->adrregs[i] = frame->nativesavadr[j++];
+       }
+#endif
+
+       /* skip the native frame on the machine stack */
+
+       es->sp -= frame->nativeframesize;
+
+       /* set the pc the next frame must return to */
+
+       es->pc = frame->nativepc;
+}
+
+
 /* replace_recover_source_state ************************************************
 
    Recover the source state from the given replacement point and execution
    state.
 
    IN:
-       rp...............replacement point that has been reached
+       rp...............replacement point that has been reached, if any
+          sfi..............stackframeinfo, if called from native code
           es...............execution state at the replacement point rp
 
    RETURN VALUE:
@@ -1909,10 +2333,12 @@ rplpoint *replace_find_replacement_point_for_pc(codeinfo *code, u1 *pc)
 *******************************************************************************/
 
 sourcestate_t *replace_recover_source_state(rplpoint *rp,
+                                                                                       stackframeinfo *sfi,
                                                                                    executionstate_t *es)
 {
        sourcestate_t *ss;
        u1            *ra;
+       bool           locked;
 #if defined(REPLACE_STATISTICS)
        s4             depth;
 #endif
@@ -1922,11 +2348,34 @@ sourcestate_t *replace_recover_source_state(rplpoint *rp,
        ss = DNEW(sourcestate_t);
        ss->frames = NULL;
 
+       /* get the stackframeinfo if none is given */
+
+       if (sfi == NULL)
+               sfi = STACKFRAMEINFO;
+
        /* each iteration of the loop recovers one source frame */
 
        depth = 0;
+       locked = false;
+
+       while (rp || sfi) {
+
+               DOLOG( replace_executionstate_println(es); );
 
-       while (true) {
+               /* if we are not at a replacement point, it is a native frame */
+
+               if (rp == NULL) {
+                       DOLOG( printf("native frame: sfi: "); replace_stackframeinfo_println(sfi); );
+
+                       locked = true;
+                       replace_pop_native_frame(es, ss, sfi);
+                       sfi = sfi->prev;
+
+                       if (es->code == NULL)
+                               continue;
+
+                       goto after_machine_frame;
+               }
 
                /* read the values for this source frame from the execution state */
 
@@ -1936,12 +2385,23 @@ sourcestate_t *replace_recover_source_state(rplpoint *rp,
 
                replace_read_executionstate(rp, es, ss, ss->frames == NULL);
 
+#if defined(ENABLE_VMLOG)
+               vmlog_cacao_unrol_method(ss->frames->method);
+#endif
+
 #if defined(REPLACE_STATISTICS)
                REPLACE_COUNT(stat_frames);
                depth++;
                replace_statistics_source_frame(ss->frames);
 #endif
 
+               /* in locked areas (below native frames), identity map the frame */
+
+               if (locked) {
+                       ss->frames->torp = ss->frames->fromrp;
+                       ss->frames->tocode = ss->frames->fromcode;
+               }
+
                /* unroll to the next (outer) frame */
 
                if (rp->parent) {
@@ -1955,16 +2415,23 @@ sourcestate_t *replace_recover_source_state(rplpoint *rp,
                        REPLACE_COUNT(stat_unroll_inline);
                }
                else {
-                       /* this frame had been called at machine-level */
+                       /* this frame had been called at machine-level. pop it. */
 
                        DOLOG( printf("UNWIND\n"); );
 
                        ra = replace_pop_activation_record(es, ss->frames);
                        if (ra == NULL) {
-                               DOLOG( printf("BREAKING\n"); );
-                               break;
+                               DOLOG( printf("REACHED NATIVE CODE\n"); );
+
+                               rp = NULL;
+
+                               break; /* XXX remove to activate native frames */
+                               continue;
                        }
 
+                       /* find the replacement point at the call site */
+
+after_machine_frame:
                        rp = replace_find_replacement_point_for_pc(es->code, es->pc);
 
                        if (rp == NULL)
@@ -1991,7 +2458,6 @@ sourcestate_t *replace_recover_source_state(rplpoint *rp,
    frame, it is (re)compiled.
 
    IN:
-       firstcode........XXX temporary hack, will be removed
        ss...............the source state
 
    OUT:
@@ -2004,7 +2470,7 @@ sourcestate_t *replace_recover_source_state(rplpoint *rp,
 
 *******************************************************************************/
 
-static bool replace_map_source_state(codeinfo *firstcode, sourcestate_t *ss)
+static bool replace_map_source_state(sourcestate_t *ss)
 {
        sourceframe_t *frame;
        codeinfo      *code;
@@ -2015,41 +2481,64 @@ static bool replace_map_source_state(codeinfo *firstcode, sourcestate_t *ss)
 #endif
 
        parent = NULL;
+       code = NULL;
 
        /* iterate over the source frames from outermost to innermost */
 
-       code = firstcode; /* XXX should get code for first frame */
-
-       frame = ss->frames;
-       while (true) {
+       for (frame = ss->frames; frame != NULL; frame = frame->down) {
 
-               /* map this frame */
+               /* XXX skip native frames */
 
-               rp = replace_find_replacement_point(code, frame, parent);
+               if (REPLACE_IS_NATIVE_FRAME(frame)) {
+                       parent = NULL;
+                       continue;
+               }
 
-               frame->tocode = code;
-               frame->torp = rp;
+               /* map frames which are not already mapped */
 
-               /* go down one frame */
+               if (frame->tocode) {
+                       code = frame->tocode;
+                       rp = frame->torp;
+                       assert(rp);
+               }
+               else {
+                       assert(frame->torp == NULL);
 
-               frame = frame->down;
-               if (frame == NULL)
-                       break;
+                       if (parent == NULL) {
+                               /* find code for this frame */
 
-               if (rp->type == RPLPOINT_TYPE_CALL) {
 #if defined(REPLACE_STATISTICS)
-                       oldcode = frame->method->code;
+                               oldcode = frame->method->code;
 #endif
-                       code = jit_get_current_code(frame->method);
+                               /* request optimization of hot methods and their callers */
+
+                               if (frame->method->hitcountdown < 0
+                                       || (frame->down && frame->down->method->hitcountdown < 0))
+                                       jit_request_optimization(frame->method);
 
-                       if (code == NULL)
-                               return false; /* exception */
+                               code = jit_get_current_code(frame->method);
 
-                       REPLACE_COUNT_IF(stat_recompile, code != oldcode);
+                               if (code == NULL)
+                                       return false; /* exception */
 
+                               REPLACE_COUNT_IF(stat_recompile, code != oldcode);
+                       }
+
+                       assert(code);
+
+                       /* map this frame */
+
+                       rp = replace_find_replacement_point(code, frame, parent);
+
+                       frame->tocode = code;
+                       frame->torp = rp;
+               }
+
+               if (rp->type == RPLPOINT_TYPE_CALL) {
                        parent = NULL;
                }
                else {
+                       /* inlining */
                        parent = rp;
                }
        }
@@ -2081,37 +2570,60 @@ static void replace_build_execution_state_intern(sourcestate_t *ss,
 {
        rplpoint      *rp;
        sourceframe_t *prevframe;
+       rplpoint      *parent;
 
-       while (true) {
+       parent = NULL;
+       prevframe = NULL;
+       rp = NULL;
 
-               rp = ss->frames->torp;
+       while (ss->frames) {
 
+               if (REPLACE_IS_NATIVE_FRAME(ss->frames)) {
+                       prevframe = ss->frames;
+                       replace_push_native_frame(es, ss);
+                       parent = NULL;
+                       rp = NULL;
+                       continue;
+               }
+
+               if (parent == NULL) {
+                       /* create a machine-level stack frame */
+
+                       DOLOG( printf("pushing activation record for:\n");
+                                  if (rp) replace_replacement_point_println(rp, 1);
+                                  else printf("\tfirst frame\n"); );
+
+                       replace_push_activation_record(es, rp, prevframe, ss->frames);
+
+                       DOLOG( replace_executionstate_println(es); );
+               }
+
+               rp = ss->frames->torp;
                assert(rp);
-               assert(es->code == ss->frames->tocode);
 
                DOLOG( printf("creating execution state for%s:\n",
                                (ss->frames->down == NULL) ? " TOPFRAME" : "");
                           replace_replacement_point_println(ss->frames->fromrp, 1);
                           replace_replacement_point_println(rp, 1); );
 
+               es->code = ss->frames->tocode;
                prevframe = ss->frames;
 
-               replace_write_executionstate(rp, es, ss, ss->frames->down == NULL);
+#if defined(ENABLE_VMLOG)
+               vmlog_cacao_rerol_method(ss->frames->method);
+#endif
 
-               if (ss->frames == NULL)
-                       break;
+               replace_write_executionstate(rp, es, ss, ss->frames->down == NULL);
 
                DOLOG( replace_executionstate_println(es); );
 
                if (rp->type == RPLPOINT_TYPE_CALL) {
-
-                       DOLOG( printf("pushing activation record for:\n");
-                                  replace_replacement_point_println(rp, 1); );
-
-                       replace_push_activation_record(es, rp, prevframe, ss->frames);
+                       parent = NULL;
+               }
+               else {
+                       /* inlining */
+                       parent = rp;
                }
-
-               DOLOG( replace_executionstate_println(es); );
        }
 }
 
@@ -2222,6 +2734,55 @@ void replace_free_safestack(replace_safestack_t *st, executionstate_t *tmpes)
 }
 
 
+/* replace_me_wrapper **********************************************************
+
+   TODO: Document me!
+
+*******************************************************************************/
+
+bool replace_me_wrapper(u1 *pc)
+{
+       codeinfo         *code;
+       rplpoint         *rp;
+       executionstate_t  es;
+
+       /* search the codeinfo for the given PC */
+
+       code = code_find_codeinfo_for_pc(pc);
+       assert(code);
+
+       /* search for a replacement point at the given PC */
+
+#if 0
+       rp = replace_find_replacement_point_for_pc(code, pc);
+       assert(rp == NULL || rp->pc == pc);
+#else
+       {
+               int i;
+               rplpoint *rp2;
+               rp = NULL;
+               for (i=0,rp2=code->rplpoints; i<code->rplpointcount; i++,rp2++) {
+                       if (rp2->pc == pc)
+                               rp = rp2;
+               }
+       }
+#endif
+
+       /* check if the replacement point is active */
+
+       if (rp != NULL && (rp->flags & RPLPOINT_FLAG_ACTIVE)) {
+
+               /*md_replace_executionstate_read(&es, context);*/
+
+               replace_me(rp, &es);
+
+               return true;
+       }
+       else
+               return false;
+}
+
+
 /* replace_me ******************************************************************
  
    This function is called by asm_replacement_out when a thread reaches
@@ -2263,11 +2824,11 @@ void replace_me(rplpoint *rp, executionstate_t *es)
 
        /* recover source state */
 
-       ss = replace_recover_source_state(rp, es);
+       ss = replace_recover_source_state(rp, NULL, es);
 
        /* map the source state */
 
-       if (!replace_map_source_state(es->code, ss))
+       if (!replace_map_source_state(ss))
                vm_abort("exception during method replacement");
 
        DOLOG( replace_sourcestate_println(ss); );
@@ -2301,8 +2862,8 @@ void replace_me(rplpoint *rp, executionstate_t *es)
 
        /* call the assembler code for the last phase of replacement */
 
-#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
-       asm_replacement_in(&(safestack->es), safestack);
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__) || defined(__S390__)) && defined(ENABLE_JIT)
+       /*asm_replacement_in(&(safestack->es), safestack);*/
 #endif
 
        abort(); /* NOT REACHED */
@@ -2458,6 +3019,8 @@ void replace_replacement_point_println(rplpoint *rp, int depth)
                        replace_type_str[rp->type]);
        if (rp->flags & RPLPOINT_FLAG_NOTRAP)
                printf(" NOTRAP");
+       if (rp->flags & RPLPOINT_FLAG_COUNTDOWN)
+               printf(" COUNTDOWN");
        if (rp->flags & RPLPOINT_FLAG_ACTIVE)
                printf(" ACTIVE");
        printf(" parent:%p\n", (void*)rp->parent);
@@ -2522,6 +3085,9 @@ void replace_show_replacement_points(codeinfo *code)
        printf("\ttotal allocations : %d\n",code->regalloccount);
        printf("\tsaved int regs    : %d\n",code->savedintcount);
        printf("\tsaved flt regs    : %d\n",code->savedfltcount);
+#if defined(HAS_ADDRESS_REGISTER_FILE)
+       printf("\tsaved adr regs    : %d\n",code->savedadrcount);
+#endif
        printf("\tmemuse            : %d\n",code->memuse);
 
        printf("\n");
@@ -2574,9 +3140,9 @@ void replace_executionstate_println(executionstate_t *es)
                else
                        printf(" ");
 #if SIZEOF_VOID_P == 8
-               printf("%-3s = %016llx",regs[i],(unsigned long long)es->intregs[i]);
+               printf("%-3s = %016llx",abi_registers_integer_name[i],(unsigned long long)es->intregs[i]);
 #else
-               printf("%-3s = %08lx",regs[i],(unsigned long)es->intregs[i]);
+               printf("%-3s = %08lx",abi_registers_integer_name[i],(unsigned long)es->intregs[i]);
 #endif
                if (i%4 == 3)
                        printf("\n");
@@ -2590,6 +3156,17 @@ void replace_executionstate_println(executionstate_t *es)
                if (i%4 == 3)
                        printf("\n");
        }
+# if defined(HAS_ADDRESS_REGISTER_FILE)
+       for (i=0; i<ADR_REG_CNT; ++i) {
+               if (i%4 == 0)
+                       printf("\t");
+               else
+                       printf(" ");
+               printf("A%02d = %016llx",i,(unsigned long long)es->adrregs[i]);
+               if (i%4 == 3)
+                       printf("\n");
+       }
+# endif
 #endif
 
        sp = (stackslot_t *) es->sp;
@@ -2598,7 +3175,7 @@ void replace_executionstate_println(executionstate_t *es)
 
        if (es->code) {
                methoddesc *md = es->code->m->parseddesc;
-               slots = code_get_stack_frame_size(es->code);
+               slots = es->code->stackframesize;
                extraslots = 1 + md->memuse;
        }
        else
@@ -2633,32 +3210,41 @@ void replace_executionstate_println(executionstate_t *es)
 #endif
 
 #if !defined(NDEBUG)
-static void java_value_print(s4 type, u8 value)
+static void java_value_print(s4 type, replace_val_t value)
 {
-       java_objectheader *obj;
-       utf               *u;
+       java_object_t *obj;
+       utf           *u;
 
-       printf("%016llx",(unsigned long long) value);
+       printf("%016llx",(unsigned long long) value.l);
 
        if (type < 0 || type > TYPE_RET)
                printf(" <INVALID TYPE:%d>", type);
        else
                printf(" %s", show_jit_type_names[type]);
 
-       if (type == TYPE_ADR && value != 0) {
-               obj = (java_objectheader *) (ptrint) value;
+       if (type == TYPE_ADR && value.a != NULL) {
+               obj = value.a;
                putchar(' ');
                utf_display_printable_ascii_classname(obj->vftbl->class->name);
 
                if (obj->vftbl->class == class_java_lang_String) {
                        printf(" \"");
-                       u = javastring_toutf((java_lang_String *)obj, false);
+                       u = javastring_toutf(obj, false);
                        utf_display_printable_ascii(u);
                        printf("\"");
                }
        }
-       else if (type == TYPE_INT || type == TYPE_LNG) {
-               printf(" %lld", (long long) value);
+       else if (type == TYPE_INT) {
+               printf(" %ld", (long) value.i);
+       }
+       else if (type == TYPE_LNG) {
+               printf(" %lld", (long long) value.l);
+       }
+       else if (type == TYPE_FLT) {
+               printf(" %f", value.f);
+       }
+       else if (type == TYPE_DBL) {
+               printf(" %f", value.d);
        }
 }
 #endif /* !defined(NDEBUG) */
@@ -2667,16 +3253,38 @@ static void java_value_print(s4 type, u8 value)
 #if !defined(NDEBUG)
 void replace_source_frame_println(sourceframe_t *frame)
 {
-       s4 i;
+       s4 i,j;
        s4 t;
 
+       if (REPLACE_IS_NATIVE_FRAME(frame)) {
+               printf("\tNATIVE\n");
+               printf("\tsfi: "); replace_stackframeinfo_println(frame->sfi);
+               printf("\tnativepc: %p\n", frame->nativepc);
+               printf("\tframesize: %d\n", frame->nativeframesize);
+
+               j = 0;
+               for (i=0; i<INT_REG_CNT; ++i) {
+                       if (nregdescint[i] == REG_SAV)
+                               printf("\t%s = %p\n", abi_registers_integer_name[i], (void*)frame->nativesavint[j++]);
+               }
+
+               j = 0;
+               for (i=0; i<FLT_REG_CNT; ++i) {
+                       if (nregdescfloat[i] == REG_SAV)
+                               printf("\tF%02d = %f\n", i, frame->nativesavflt[j++]);
+               }
+
+               printf("\n");
+               return;
+       }
+
        printf("\t");
        method_println(frame->method);
        printf("\tid: %d\n", frame->id);
        printf("\ttype: %s\n", replace_type_str[frame->type]);
        printf("\n");
 
-       if (frame->instance) {
+       if (frame->instance.a) {
                printf("\tinstance: ");
                java_value_print(TYPE_ADR, frame->instance);
                printf("\n");
@@ -2719,9 +3327,9 @@ void replace_source_frame_println(sourceframe_t *frame)
                for (i=0; i<frame->syncslotcount; ++i) {
                        printf("\tslot[%2d] = ",i);
 #ifdef HAS_4BYTE_STACKSLOT
-                       printf("%08lx\n",(unsigned long) frame->syncslots[i]);
+                       printf("%08lx\n",(unsigned long) frame->syncslots[i].p);
 #else
-                       printf("%016llx\n",(unsigned long long) frame->syncslots[i]);
+                       printf("%016llx\n",(unsigned long long) frame->syncslots[i].p);
 #endif
                }
                printf("\n");
@@ -2797,6 +3405,13 @@ void replace_sourcestate_println_short(sourcestate_t *ss)
        for (frame = ss->frames; frame != NULL; frame = frame->down) {
                printf("\t");
 
+               if (REPLACE_IS_NATIVE_FRAME(frame)) {
+                       printf("NATIVE (pc %p size %d) ",
+                                       (void*)frame->nativepc, frame->nativeframesize);
+                       replace_stackframeinfo_println(frame->sfi);
+                       continue;
+               }
+
                if (frame->torp) {
                        printf("%c", (frame->torp == frame->fromrp) ? '=' : '+');
                }
@@ -2818,6 +3433,20 @@ void replace_sourcestate_println_short(sourcestate_t *ss)
 }
 #endif
 
+#if !defined(NDEBUG)
+static void replace_stackframeinfo_println(stackframeinfo *sfi)
+{
+       printf("prev=%p pv=%p sp=%p ra=%p xpc=%p method=",
+                       (void*)sfi->prev, (void*)sfi->pv, (void*)sfi->sp,
+                       (void*)sfi->ra, (void*)sfi->xpc);
+
+       if (sfi->method)
+               method_println(sfi->method);
+       else
+               printf("(nil)\n");
+}
+#endif
+
 /*
  * 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