* src/vm/jit/code.c (code_get_stack_frame_size): Implement stack alignment
[cacao.git] / src / vm / jit / replace.c
index 22c37b58f544cc21c16042f396bcb35ff2aec470..d4a19c893f4ff1413f61215671bc937c62bf6ebd 100644 (file)
 #include "vm/types.h"
 
 #include <assert.h>
+#include <stdlib.h>
 
 #include "mm/memory.h"
+#include "toolbox/logging.h"
 #include "vm/options.h"
+#include "vm/jit/jit.h"
 #include "vm/jit/replace.h"
+#include "vm/jit/asmpart.h"
+#include "vm/jit/disass.h"
+#include "arch.h"
+
+/*** constants used internally ************************************************/
+
+#define TOP_IS_NORMAL    0
+#define TOP_IS_ON_STACK  1
+#define TOP_IS_IN_ITMP1  2
 
 /* replace_create_replacement_points *******************************************
  
@@ -74,10 +86,12 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
        rplpoint *rp;
        int alloccount;
        int globalcount;
-       s2 *regalloc;
-       s2 *ra;
+       rplalloc *regalloc;
+       rplalloc *ra;
        int i;
+       int t;
        stackptr sp;
+       bool indexused;
 
        /* assert that we wont overwrite already allocated data */
        
@@ -112,28 +126,62 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
 
        /* count global register allocations */
 
-       globalcount = m->maxlocals;
+       globalcount = 0;
+
+       for (i=0; i<m->maxlocals; ++i) {
+               indexused = false;
+               for (t=0; t<5; ++t) {
+#if defined(ENABLE_INTRP)
+                       if (!opt_intrp) {
+#endif
+                               if (rd->locals[i][t].type == t) {
+                                       globalcount++;
+                                       indexused = true;
+                               }
+#if defined(ENABLE_INTRP)
+                       }
+#endif
+               }
+               if (!indexused)
+                       globalcount++; /* dummy rplalloc */
+       }
+
        alloccount += globalcount;
 
        /* allocate replacement point array and allocation array */
        
        rplpoints = MNEW(rplpoint,count);
-       regalloc = MNEW(s2,alloccount);
+       regalloc = MNEW(rplalloc,alloccount);
        ra = regalloc;
 
        /* store global register allocations */
 
        for (i=0; i<m->maxlocals; ++i) {
+               indexused = false;
+               for (t=0; t<5; ++t) {
 #if defined(ENABLE_INTRP)
-               if (!opt_intrp) {
+                       if (!opt_intrp) {
 #endif
-               *ra++ = rd->locals[i][0].regoff; /* XXX */
+                               if (rd->locals[i][t].type == t) {
+                                       ra->flags = rd->locals[i][t].flags & (INMEMORY);
+                                       ra->index = rd->locals[i][t].regoff;
+                                       ra->type  = t;
+                                       ra->next = (indexused) ? 0 : 1;
+                                       ra++;
+                                       indexused = true;
+                               }
 #if defined(ENABLE_INTRP)
+                       }
+#endif
                }
-               else {
-                       *ra++ = 0;
+               if (!indexused) {
+                       /* dummy rplalloc */
+                       ra->type = -1;
+                       ra->flags = 0;
+                       ra->index = 0;
+                       ra->next = 1;
+                       ra++;
                }
-#endif
        }
 
        /* initialize replacement point structs */
@@ -147,15 +195,20 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
 
                rp->pc = NULL;        /* set by codegen */
                rp->outcode = NULL;   /* set by codegen */
-               rp->hashlink = NULL;
                rp->code = code;
                rp->target = NULL;
                rp->regalloc = ra;
+               rp->flags = 0;
+               rp->type = bptr->type;
 
                /* store local allocation info */
 
                for (sp = bptr->instack; sp; sp = sp->prev) {
-                       *ra++ = sp->regoff; /* XXX */
+                       ra->flags = sp->flags & (INMEMORY);
+                       ra->index = sp->regoff;
+                       ra->type  = sp->type;
+                       ra->next  = 1;
+                       ra++;
                }
 
                rp->regalloccount = ra - rp->regalloc;
@@ -170,6 +223,10 @@ bool replace_create_replacement_points(codeinfo *code,registerdata *rd)
        code->regalloc = regalloc;
        code->regalloccount = alloccount;
        code->globalcount = globalcount;
+       code->savedintcount = INT_SAV_CNT - rd->savintreguse;
+       code->savedfltcount = FLT_SAV_CNT - rd->savfltreguse;
+       code->memuse = rd->memuse;
+       code->isleafmethod = m->isleafmethod; /* XXX will be moved to codeinfo */
 
        /* everything alright */
 
@@ -193,7 +250,7 @@ void replace_free_replacement_points(codeinfo *code)
                MFREE(code->rplpoints,rplpoint,code->rplpointcount);
 
        if (code->regalloc)
-               MFREE(code->regalloc,s2,code->regalloccount);
+               MFREE(code->regalloc,rplalloc,code->regalloccount);
 
        code->rplpoints = NULL;
        code->rplpointcount = 0;
@@ -226,7 +283,7 @@ void replace_activate_replacement_point(rplpoint *rp,rplpoint *target)
        
        rp->target = target;
        
-#if defined(__I386__) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
        md_patch_replacement_point(rp);
 #endif
 }
@@ -254,11 +311,448 @@ void replace_deactivate_replacement_point(rplpoint *rp)
        
        rp->target = NULL;
        
-#if defined(__I386__) && defined(ENABLE_JIT)
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
        md_patch_replacement_point(rp);
 #endif
 }
 
+/* replace_read_executionstate *************************************************
+
+   Read the given executions state and translate it to a source state.
+   
+   IN:
+       rp...............replacement point at which `es` was taken
+          es...............execution state
+          ss...............where to put the source state
+
+   OUT:
+       *ss..............the source state derived from the execution state
+  
+*******************************************************************************/
+
+inline static void replace_read_value(executionstate *es,
+#ifdef HAS_4BYTE_STACKSLOT
+                                                                         u4 *sp,
+#else
+                                                                         u8 *sp,
+#endif
+                                                                         rplalloc *ra,
+                                                                         u8 *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->index);
+               }
+               else {
+#endif
+                       *javaval = sp[ra->index];
+#ifdef HAS_4BYTE_STACKSLOT
+               }
+#endif
+       }
+       else {
+               /* allocated register */
+               if (IS_FLT_DBL_TYPE(ra->type)) {
+                       *javaval = es->fltregs[ra->index];
+               }
+               else {
+                       *javaval = es->intregs[ra->index];
+               }
+       }
+}
+
+inline static void replace_write_value(executionstate *es,
+#ifdef HAS_4BYTE_STACKSLOT
+                                                                         u4 *sp,
+#else
+                                                                         u8 *sp,
+#endif
+                                                                         rplalloc *ra,
+                                                                         u8 *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->index) = *javaval;
+               }
+               else {
+#endif
+                       sp[ra->index] = *javaval;
+#ifdef HAS_4BYTE_STACKSLOT
+               }
+#endif
+       }
+       else {
+               /* allocated register */
+               if (IS_FLT_DBL_TYPE(ra->type)) {
+                       es->fltregs[ra->index] = *javaval;
+               }
+               else {
+                       es->intregs[ra->index] = *javaval;
+               }
+       }
+}
+
+static void replace_read_executionstate(rplpoint *rp,executionstate *es,
+                                                                        sourcestate *ss)
+{
+       methodinfo *m;
+       codeinfo *code;
+       int count;
+       int i;
+       int t;
+       int allocs;
+       rplalloc *ra;
+       methoddesc *md;
+       int topslot;
+#ifdef HAS_4BYTE_STACKSLOT
+       u4 *sp;
+       u4 *basesp;
+#else
+       u8 *sp;
+       u8 *basesp;
+#endif
+
+       code = rp->code;
+       m = code->m;
+       md = m->parseddesc;
+       topslot = TOP_IS_NORMAL;
+
+       /* stack pointers */
+
+#ifdef HAS_4BYTE_STACKSLOT
+       sp = (u4*) es->sp;
+#else
+       sp = (u8*) es->sp;
+#endif
+
+       /* on some architectures the returnAddress is passed on the stack by JSR */
+
+#if defined(__I386__) || defined(__X86_64__)
+       if (rp->type == BBTYPE_SBR) {
+               sp++;
+               topslot = TOP_IS_ON_STACK;
+       }
+#endif
+
+       /* in some cases the top stack slot is passed in REG_ITMP1 */
+
+       if (  (rp->type == BBTYPE_EXH)
+#if defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)
+          || (rp->type == BBTYPE_SBR)
+#endif
+          )
+       {
+               topslot = TOP_IS_IN_ITMP1;
+       }
+
+       /* calculate base stack pointer */
+       
+       basesp = sp + code_get_stack_frame_size(code);
+
+       ss->stackbase = (u1*) basesp;
+
+       /* read local variables */
+
+       count = m->maxlocals;
+       ss->javalocalcount = count;
+       ss->javalocals = DMNEW(u8,count * 5);
+
+#ifndef NDEBUG
+       /* mark values as undefined */
+       for (i=0; i<count*5; ++i)
+               ss->javalocals[i] = (u8) 0x00dead0000dead00ULL;
+
+       /* some entries in the intregs array are not meaningful */
+       /*es->intregs[REG_ITMP3] = (u8) 0x11dead1111dead11ULL;*/
+       es->intregs[REG_SP   ] = (u8) 0x11dead1111dead11ULL;
+#ifdef REG_PV
+       es->intregs[REG_PV   ] = (u8) 0x11dead1111dead11ULL;
+#endif
+#endif /* NDEBUG */
+       
+       ra = code->regalloc;
+
+       i = -1;
+       for (allocs = code->globalcount; allocs--; ra++) {
+               if (ra->next)
+                       i++;
+               t = ra->type;
+               if (t == -1)
+                       continue; /* dummy rplalloc */
+
+               replace_read_value(es,sp,ra,ss->javalocals + (5*i+t));
+       }
+
+       /* read stack slots */
+
+       count = rp->regalloccount;
+       ss->javastackdepth = count;
+       ss->javastack = DMNEW(u8,count);
+
+#ifndef NDEBUG
+       /* mark values as undefined */
+       for (i=0; i<count; ++i)
+               ss->javastack[i] = (u8) 0x00dead0000dead00ULL;
+#endif
+       
+       i = 0;
+       ra = rp->regalloc;
+
+       /* the first stack slot is special in SBR and EXH blocks */
+
+       if (topslot == TOP_IS_ON_STACK) {
+               assert(count);
+               
+               ss->javastack[i] = sp[-1];
+               count--;
+               i++;
+               ra++;
+       }
+       else if (topslot == TOP_IS_IN_ITMP1) {
+               assert(count);
+
+               ss->javastack[i] = es->intregs[REG_ITMP1];
+               count--;
+               i++;
+               ra++;
+       }
+       
+       /* read remaining stack slots */
+       
+       for (; count--; ra++, i++) {
+               assert(ra->next);
+
+               replace_read_value(es,sp,ra,ss->javastack + i);
+       }
+
+       /* read unused callee saved int regs */
+       
+       count = INT_SAV_CNT;
+       for (i=0; count > code->savedintcount; ++i) {
+               assert(i < INT_REG_CNT);
+               if (nregdescint[i] == REG_SAV)
+                       ss->savedintregs[--count] = es->intregs[i];
+       }
+
+       /* read saved int regs */
+
+       for (i=0; i<code->savedintcount; ++i) {
+               ss->savedintregs[i] = *--basesp;
+       }
+
+       /* read unused callee saved flt regs */
+       
+       count = FLT_SAV_CNT;
+       for (i=0; count > code->savedfltcount; ++i) {
+               assert(i < FLT_REG_CNT);
+               if (nregdescfloat[i] == REG_SAV)
+                       ss->savedfltregs[--count] = es->fltregs[i];
+       }
+
+       /* read saved flt regs */
+
+       for (i=0; i<code->savedfltcount; ++i) {
+#ifdef HAS_4BYTE_STACKSLOT
+               basesp -= 2;
+#else
+               basesp--;
+#endif
+               ss->savedfltregs[i] = *(u8*)basesp;
+       }
+
+       /* read slots used for synchronization */
+
+       count = code_get_sync_slot_count(code);
+       ss->syncslotcount = count;
+       ss->syncslots = DMNEW(u8,count);
+       for (i=0; i<count; ++i) {
+               ss->syncslots[i] = sp[code->memuse + i];
+       }
+}
+
+/* replace_write_executionstate ************************************************
+
+   Translate the given source state into an execution state.
+   
+   IN:
+       rp...............replacement point for which execution state should be
+                           creates
+          es...............where to put the execution state
+          ss...............the given source state
+
+   OUT:
+       *es..............the execution state derived from the source state
+  
+*******************************************************************************/
+
+static void replace_write_executionstate(rplpoint *rp,executionstate *es,
+                                                                                sourcestate *ss)
+{
+       methodinfo *m;
+       codeinfo *code;
+       int count;
+       int i;
+       int t;
+       int allocs;
+       rplalloc *ra;
+       methoddesc *md;
+       int topslot;
+#ifdef HAS_4BYTE_STACKSLOT
+       u4 *sp;
+       u4 *basesp;
+#else
+       u8 *sp;
+       u8 *basesp;
+#endif
+
+       code = rp->code;
+       m = code->m;
+       md = m->parseddesc;
+       topslot = TOP_IS_NORMAL;
+       
+       /* calculate stack pointer */
+       
+#ifdef HAS_4BYTE_STACKSLOT
+       basesp = (u4*) ss->stackbase;
+#else
+       basesp = (u8*) ss->stackbase;
+#endif
+       
+       sp = basesp - code_get_stack_frame_size(code);
+
+       /* on some architectures the returnAddress is passed on the stack by JSR */
+
+#if defined(__I386__) || defined(__X86_64__)
+       if (rp->type == BBTYPE_SBR) {
+               topslot = TOP_IS_ON_STACK;
+       }
+#endif
+       
+       /* in some cases the top stack slot is passed in REG_ITMP1 */
+
+       if (  (rp->type == BBTYPE_EXH)
+#if defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)
+          || (rp->type == BBTYPE_SBR) 
+#endif
+          )
+       {
+               topslot = TOP_IS_IN_ITMP1;
+       }
+
+       /* in debug mode, invalidate stack frame first */
+
+#ifndef NDEBUG
+       for (i=0; i<(basesp - sp); ++i) {
+               sp[i] = 0xdeaddeadU;
+       }
+#endif
+
+       /* write local variables */
+
+       count = m->maxlocals;
+
+       ra = code->regalloc;
+
+       i = -1;
+       for (allocs = code->globalcount; allocs--; ra++) {
+               if (ra->next)
+                       i++;
+
+               assert(i >= 0 && i < m->maxlocals);
+               
+               t = ra->type;
+               if (t == -1)
+                       continue; /* dummy rplalloc */
+
+               replace_write_value(es,sp,ra,ss->javalocals + (5*i+t));
+       }
+
+       /* write stack slots */
+
+       count = rp->regalloccount;
+
+       i = 0;
+       ra = rp->regalloc;
+
+       /* the first stack slot is special in SBR and EXH blocks */
+
+       if (topslot == TOP_IS_ON_STACK) {
+               assert(count);
+               
+               sp[-1] = ss->javastack[i];
+               count--;
+               i++;
+               ra++;
+       }
+       else if (topslot == TOP_IS_IN_ITMP1) {
+               assert(count);
+
+               es->intregs[REG_ITMP1] = ss->javastack[i];
+               count--;
+               i++;
+               ra++;
+       }
+
+       /* write remaining stack slots */
+       
+       for (; count--; ra++, i++) {
+               assert(ra->next);
+
+               replace_write_value(es,sp,ra,ss->javastack + i);
+       }
+       
+       /* write unused callee saved int regs */
+       
+       count = INT_SAV_CNT;
+       for (i=0; count > code->savedintcount; ++i) {
+               assert(i < INT_REG_CNT);
+               if (nregdescint[i] == REG_SAV)
+                       es->intregs[i] = ss->savedintregs[--count];
+       }
+
+       /* write saved int regs */
+
+       for (i=0; i<code->savedintcount; ++i) {
+               *--basesp = ss->savedintregs[i];
+       }
+
+       /* write unused callee saved flt regs */
+       
+       count = FLT_SAV_CNT;
+       for (i=0; count > code->savedfltcount; ++i) {
+               assert(i < FLT_REG_CNT);
+               if (nregdescfloat[i] == REG_SAV)
+                       es->fltregs[i] = ss->savedfltregs[--count];
+       }
+
+       /* write saved flt regs */
+
+       for (i=0; i<code->savedfltcount; ++i) {
+#ifdef HAS_4BYTE_STACKSLOT
+               basesp -= 2;
+#else
+               basesp--;
+#endif
+               *(u8*)basesp = ss->savedfltregs[i];
+       }
+
+       /* write slots used for synchronization */
+
+       count = code_get_sync_slot_count(code);
+       assert(count == ss->syncslotcount);
+       for (i=0; i<count; ++i) {
+               sp[code->memuse + i] = ss->syncslots[i];
+       }
+
+       /* set new pc */
+
+       es->pc = rp->pc;
+}
+
 /* replace_me ******************************************************************
  
    This function is called by asm_replacement_out when a thread reaches
@@ -269,24 +763,61 @@ void replace_deactivate_replacement_point(rplpoint *rp)
   
    IN:
        rp...............replacement point that has been reached
-          es...............executions state read by asm_replacement_out
+          es...............execution state read by asm_replacement_out
   
 *******************************************************************************/
 
 void replace_me(rplpoint *rp,executionstate *es)
 {
        rplpoint *target;
+       sourcestate ss;
+       s4 dumpsize;
        
+       /* mark start of dump memory area */
+
+       dumpsize = dump_size();
+
+       /* fetch the target of the replacement */
+
        target = rp->target;
+
+       /* XXX DEBUG turn of self-replacement */
+       if (target == rp)
+               replace_deactivate_replacement_point(rp);
        
 #ifndef NDEBUG
        printf("replace_me(%p,%p)\n",(void*)rp,(void*)es);
        fflush(stdout);
        replace_replacement_point_println(rp);
-       replace_executionstate_println(es);
+       replace_executionstate_println(es,rp->code);
 #endif
 
-       es->pc = rp->target->pc;
+       /* read execution state of old code */
+
+       replace_read_executionstate(rp,es,&ss);
+
+#ifndef NDEBUG
+       replace_sourcestate_println(&ss);
+#endif
+
+       /* write execution state of new code */
+
+       replace_write_executionstate(target,es,&ss);
+
+#ifndef NDEBUG
+       replace_executionstate_println(es,target->code);
+#endif
+       
+       /* release dump area */
+
+       dump_release(dumpsize);
+
+       /* enter new code */
+
+#if (defined(__I386__) || defined(__X86_64__) || defined(__ALPHA__) || defined(__POWERPC__) || defined(__MIPS__)) && defined(ENABLE_JIT)
+       asm_replacement_in(es);
+#endif
+       abort();
 }
 
 /* replace_replacement_point_println *******************************************
@@ -299,6 +830,10 @@ void replace_me(rplpoint *rp,executionstate *es)
 *******************************************************************************/
 
 #ifndef NDEBUG
+static const char *type_char = "IJFDA";
+
+#define TYPECHAR(t)  (((t) >= 0 && (t) <= 4) ? type_char[t] : '?')
+
 void replace_replacement_point_println(rplpoint *rp)
 {
        int j;
@@ -308,14 +843,18 @@ void replace_replacement_point_println(rplpoint *rp)
                return;
        }
 
-       printf("rplpoint %p pc:%p out:%p target:%p mcode:%016llx allocs:%d = [",
+       printf("rplpoint %p pc:%p out:%p target:%p mcode:%016llx type:%01d flags:%01x ra:%d = [",
                        (void*)rp,rp->pc,rp->outcode,(void*)rp->target,
-                       rp->mcode,rp->regalloccount);
+                       (unsigned long long)rp->mcode,rp->type,rp->flags,rp->regalloccount);
 
        for (j=0; j<rp->regalloccount; ++j)
-               printf(" %02d",rp->regalloc[j]);
+               printf("%c%1c%01x:%02d",
+                               (rp->regalloc[j].next) ? '^' : ' ',
+                               TYPECHAR(rp->regalloc[j].type),
+                               rp->regalloc[j].flags,
+                               rp->regalloc[j].index);
 
-       printf("] method:");
+       printf("]\n          method: ");
        method_print(rp->code->m);
 
        printf("\n");
@@ -343,8 +882,21 @@ void replace_show_replacement_points(codeinfo *code)
        }
 
        printf("\treplacement points: %d\n",code->rplpointcount);
-       printf("\tglobal allocations: %d\n",code->globalcount);
+       printf("\tglobal allocations: %d = [",code->globalcount);
+
+       for (i=0; i<code->globalcount; ++i)
+               printf("%c%1c%01x:%02d",
+                               (code->regalloc[i].next) ? '^' : ' ',
+                               TYPECHAR(code->regalloc[i].type),
+                               code->regalloc[i].flags,code->regalloc[i].index);
+
+       printf("]\n");
+
        printf("\ttotal allocations : %d\n",code->regalloccount);
+       printf("\tsaved int regs    : %d\n",code->savedintcount);
+       printf("\tsaved flt regs    : %d\n",code->savedfltcount);
+       printf("\tmemuse            : %d\n",code->memuse);
+
        printf("\n");
 
        for (i=0; i<code->rplpointcount; ++i) {
@@ -352,6 +904,7 @@ void replace_show_replacement_points(codeinfo *code)
 
                assert(rp->code == code);
                
+               printf("\t");
                replace_replacement_point_println(rp);
        }
 }
@@ -363,13 +916,21 @@ void replace_show_replacement_points(codeinfo *code)
   
    IN:
        es...............the execution state to print
+          code.............the codeinfo for which this execution state is meant
+                           (may be NULL)
   
 *******************************************************************************/
 
 #ifndef NDEBUG
-void replace_executionstate_println(executionstate *es)
+void replace_executionstate_println(executionstate *es,codeinfo *code)
 {
        int i;
+       int slots;
+#ifdef HAS_4BYTE_STACKSLOT
+       u4 *sp;
+#else
+       u8 *sp;
+#endif
 
        if (!es) {
                printf("(executionstate *)NULL\n");
@@ -378,10 +939,115 @@ void replace_executionstate_println(executionstate *es)
 
        printf("executionstate %p:\n",(void*)es);
        printf("\tpc = %p\n",(void*)es->pc);
-       for (i=0; i<3; ++i) {
-               printf("\tregs[%2d] = %016llx\n",i,(u8)es->regs[i]);
+       printf("\tsp = %p\n",(void*)es->sp);
+       printf("\tpv = %p\n",(void*)es->pv);
+       for (i=0; i<INT_REG_CNT; ++i) {
+               printf("\t%-3s = %016llx\n",regs[i],(unsigned long long)es->intregs[i]);
+       }
+       for (i=0; i<FLT_REG_CNT; ++i) {
+               printf("\tfltregs[%2d] = %016llx\n",i,(unsigned long long)es->fltregs[i]);
+       }
+
+#ifdef HAS_4BYTE_STACKSLOT
+       sp = (u4*) es->sp;
+#else
+       sp = (u8*) es->sp;
+#endif
+
+       if (code)
+               slots = code_get_stack_frame_size(code);
+       else
+               slots = 0;
+
+       printf("\tstack slots at sp:\n");
+       for (i=0; i<slots; ++i) {
+#ifdef HAS_4BYTE_STACKSLOT
+               printf("\t\t%08lx\n",(unsigned long)*sp++);
+#else
+               printf("\t\t%016llx\n",(unsigned long long)*sp++);
+#endif
+       }
+
+       printf("\n");
+}
+#endif
+
+/* replace_sourcestate_println *************************************************
+   Print source state
+  
+   IN:
+       ss...............the source state to print
+  
+*******************************************************************************/
+
+#ifndef NDEBUG
+void replace_sourcestate_println(sourcestate *ss)
+{
+       int i;
+       int t;
+       int reg;
+
+       if (!ss) {
+               printf("(sourcestate *)NULL\n");
+               return;
+       }
+
+       printf("sourcestate %p: stackbase=%p\n",(void*)ss,(void*)ss->stackbase);
+
+       printf("\tlocals (%d):\n",ss->javalocalcount);
+       for (i=0; i<ss->javalocalcount; ++i) {
+               for (t=0; t<5; ++t) {
+                       if (ss->javalocals[i*5+t] != 0x00dead0000dead00ULL) {
+                               printf("\tlocal[%c%2d] = ",TYPECHAR(t),i);
+                               printf("%016llx\n",(unsigned long long) ss->javalocals[i*5+t]);
+                       }
+               }
+       }
+
+       printf("\n");
+
+       printf("\tstack (depth %d):\n",ss->javastackdepth);
+       for (i=0; i<ss->javastackdepth; ++i) {
+               printf("\tstack[%2d] = ",i);
+               printf("%016llx\n",(unsigned long long) ss->javastack[i]);
+       }
+
+       printf("\n");
+
+       printf("\tsaved int registers (%d):\n",INT_SAV_CNT);
+       reg = INT_REG_CNT;
+       for (i=0; i<INT_SAV_CNT; ++i) {
+               while (nregdescint[--reg] != REG_SAV)
+                       ;
+               if (ss->savedintregs[i] != 0x00dead0000dead00ULL) {
+                       printf("\t%-3s = ",regs[reg]);
+                       printf("%016llx\n",(unsigned long long) ss->savedintregs[i]);
+               }
+       }
+
+       printf("\n");
+
+       printf("\tsaved float registers (%d):\n",FLT_SAV_CNT);
+       for (i=0; i<FLT_SAV_CNT; ++i) {
+               if (ss->savedfltregs[i] != 0x00dead0000dead00ULL) {
+                       printf("\tsavedfltreg[%2d] = ",i);
+                       printf("%016llx\n",(unsigned long long) ss->savedfltregs[i]);
+               }
        }
+       
+       printf("\n");
 
+       printf("\tsynchronization slots (%d):\n",ss->syncslotcount);
+       for (i=0; i<ss->syncslotcount; ++i) {
+               printf("\tslot[%2d] = ",i);
+#ifdef HAS_4BYTE_STACKSLOT
+               printf("%08lx\n",(unsigned long) ss->syncslots[i]);
+#else
+               printf("%016llx\n",(unsigned long long) ss->syncslots[i]);
+#endif
+       }
+       
        printf("\n");
 }
 #endif