Christian Thalinger
Christian Ullrich
- $Id: stack.c 5385 2006-09-06 21:40:50Z twisti $
+ $Id: stack.c 5481 2006-09-12 21:23:56Z edwin $
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
+#include <limits.h>
#include "vm/types.h"
#if defined(ENABLE_STATISTICS)
#define STATISTICS_STACKDEPTH_DISTRIBUTION(distr) \
- do { \
- if (opt_stat) { \
- if (stackdepth >= 10) \
- count_store_depth[10]++; \
- else \
- count_store_depth[stackdepth]++; \
- } \
- } while (0)
+ do { \
+ if (opt_stat) { \
+ if (stackdepth >= 10) \
+ count_store_depth[10]++; \
+ else \
+ count_store_depth[stackdepth]++; \
+ } \
+ } while (0)
#else /* !defined(ENABLE_STATISTICS) */
#define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)
#endif
-/* stack_init ******************************************************************
+/* stackdata_t *****************************************************************
- Initialized the stack analysis subsystem (called by jit_init).
+ This struct holds internal data during stack analysis.
*******************************************************************************/
-bool stack_init(void)
-{
- return true;
-}
+typedef struct stackdata_t stackdata_t;
+struct stackdata_t {
+ basicblock *bptr; /* the current basic block being analysed */
+ stackptr new; /* next free stackelement */
+ s4 vartop; /* next free variable index */
+ s4 localcount; /* number of locals (at the start of var) */
+ s4 varcount; /* total number of variables allocated */
+ varinfo *var; /* variable array (same as jd->var) */
+ methodinfo *m; /* the method being analysed */
+ jitdata *jd; /* current jitdata */
+ basicblock *last_real_block; /* the last block before the empty one */
+ bool repeat; /* if true, iterate the analysis again */
+ exceptiontable **handlers; /* exception handlers for the current block */
+ exceptiontable *extableend; /* points to the last exception entry */
+ stackelement exstack; /* instack for exception handlers */
+};
-/* stack_analyse ***************************************************************
- Analyse_stack uses the intermediate code created by parse.c to
- build a model of the JVM operand stack for the current method.
-
- The following checks are performed:
- - check for operand stack underflow (before each instruction)
- - check for operand stack overflow (after[1] each instruction)
- - check for matching stack depth at merging points
- - check for matching basic types[2] at merging points
- - check basic types for instruction input (except for BUILTIN*
- opcodes, INVOKE* opcodes and MULTIANEWARRAY)
-
- [1]) Checking this after the instruction should be ok. parse.c
- counts the number of required stack slots in such a way that it is
- only vital that we don't exceed `maxstack` at basic block
- boundaries.
-
- [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
- DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
- types are not discerned.
+/* macros for allocating/releasing variable indices *****************/
-*******************************************************************************/
-#if defined(NEW_VAR)
-#define GET_NEW_INDEX(new_varindex) \
- do { \
- assert(jd->vartop < jd->varcount); \
- (new_varindex) = (jd->vartop)++; \
- } while(0)
+#define GET_NEW_INDEX(sd, new_varindex) \
+ do { \
+ assert((sd).vartop < (sd).varcount); \
+ (new_varindex) = ((sd).vartop)++; \
+ } while (0)
+
+/* Not implemented now - could be used to reuse varindices. */
+/* Pay attention to not release a localvar once implementing it! */
+#define RELEASE_INDEX(sd, varindex)
-/* not implemented now, can be used to reuse varindices */
-#define RELEASE_INDEX(varindex)
+#define GET_NEW_VAR(sd, new_varindex, newtype) \
+ do { \
+ GET_NEW_INDEX((sd), (new_varindex)); \
+ (sd).var[new_index].type = (newtype); \
+ } while (0)
+
+
+/* macros for querying variable properties **************************/
+
+#define IS_OUTVAR(sp) \
+ (sd.var[(sp)->varnum].flags & OUTVAR)
+
+#define IS_PREALLOC(sp) \
+ (sd.var[(sp)->varnum].flags & PREALLOC)
+
+#define IS_TEMPVAR(sp) \
+ ( ((sp)->varnum >= sd.localcount) \
+ && !(sd.var[(sp)->varnum].flags & (OUTVAR | PREALLOC)) )
+
+#define IS_LOCALVAR_SD(sd, sp) \
+ ((sp)->varnum < (sd).localcount)
+
+#define IS_LOCALVAR(sp) \
+ IS_LOCALVAR_SD(sd, (sp))
+
+
+/* macros for setting variable properties ****************************/
+
+#define SET_TEMPVAR(sp) \
+ do { \
+ if (IS_LOCALVAR((sp))) { \
+ GET_NEW_VAR(sd, new_index, (sp)->type); \
+ sd.var[new_index].flags = (sp)->flags; \
+ (sp)->varnum = new_index; \
+ (sp)->varkind = TEMPVAR; \
+ if ((sp)->creator) \
+ (sp)->creator->dst.varindex = new_index; \
+ } \
+ sd.var[(sp)->varnum].flags &= ~(OUTVAR | PREALLOC); \
+ } while (0);
+
+#define SET_PREALLOC(sp) \
+ do { \
+ assert(!IS_LOCALVAR((sp))); \
+ sd.var[(sp)->varnum].flags |= PREALLOC; \
+ } while (0);
+
+
+/* macros for source operands ***************************************/
#define CLR_S1 \
(iptr->s1.varindex = -1)
-#define USE_S1_LOCAL(type1)
-
#define USE_S1(type1) \
do { \
- REQUIRE_1; \
+ REQUIRE(1); \
CHECK_BASIC_TYPE(type1, curstack->type); \
iptr->s1.varindex = curstack->varnum; \
} while (0)
#define USE_S1_ANY \
do { \
- REQUIRE_1; \
+ REQUIRE(1); \
iptr->s1.varindex = curstack->varnum; \
} while (0)
#define USE_S1_S2(type1, type2) \
do { \
- REQUIRE_2; \
+ REQUIRE(2); \
CHECK_BASIC_TYPE(type1, curstack->prev->type); \
CHECK_BASIC_TYPE(type2, curstack->type); \
iptr->sx.s23.s2.varindex = curstack->varnum; \
#define USE_S1_S2_ANY_ANY \
do { \
- REQUIRE_2; \
+ REQUIRE(2); \
iptr->sx.s23.s2.varindex = curstack->varnum; \
iptr->s1.varindex = curstack->prev->varnum; \
} while (0)
#define USE_S1_S2_S3(type1, type2, type3) \
do { \
- REQUIRE_3; \
+ REQUIRE(3); \
CHECK_BASIC_TYPE(type1, curstack->prev->prev->type); \
CHECK_BASIC_TYPE(type2, curstack->prev->type); \
- CHECK_BASIC_TYPE(type3, curstack->type); \
- iptr->sx.s23.s3.varindex = curstack->varnum; \
- iptr->sx.s23.s2.varindex = curstack->prev->varnum; \
- iptr->s1.varindex = curstack->prev->prev->varnum; \
+ CHECK_BASIC_TYPE(type3, curstack->type); \
+ iptr->sx.s23.s3.varindex = curstack->varnum; \
+ iptr->sx.s23.s2.varindex = curstack->prev->varnum; \
+ iptr->s1.varindex = curstack->prev->prev->varnum; \
} while (0)
-#define CLR_DST \
- (iptr->dst.varindex = -1)
-
-#define DST(typed, varindex) \
+/* The POPANY macro does NOT check stackdepth, or set stackdepth! */
+#define POPANY \
do { \
- NEWSTACKn((varindex)); \
- iptr->dst.varindex = (varindex); \
- } while (0)
-
-#define DST_LOCALVAR(typed, index) \
- do { \
- NEWSTACK(typed, LOCALVAR, (index)); \
- iptr->dst.varindex = (index); \
+ if (curstack->varkind == UNDEFVAR) \
+ curstack->varkind = TEMPVAR; \
+ curstack = curstack->prev; \
} while (0)
-#define OP0_1(typed) \
+#define POP_S1(type1) \
do { \
- CLR_S1; \
- GET_NEW_INDEX(new_index); \
- DST(typed, new_index); \
- stackdepth++; \
+ USE_S1(type1); \
+ if (curstack->varkind == UNDEFVAR) \
+ curstack->varkind = TEMPVAR; \
+ curstack = curstack->prev; \
} while (0)
-#define OP1_0_ANY \
+#define POP_S1_ANY \
do { \
- POP_S1_ANY; \
- CLR_DST; \
- stackdepth--; \
+ USE_S1_ANY; \
+ if (curstack->varkind == UNDEFVAR) \
+ curstack->varkind = TEMPVAR; \
+ curstack = curstack->prev; \
} while (0)
-#define OP1_BRANCH(type1) \
+#define POP_S1_S2(type1, type2) \
do { \
- POP_S1(type1); \
- stackdepth--; \
+ USE_S1_S2(type1, type2); \
+ if (curstack->varkind == UNDEFVAR) \
+ curstack->varkind = TEMPVAR; \
+ if (curstack->prev->varkind == UNDEFVAR) \
+ curstack->prev->varkind = TEMPVAR; \
+ curstack = curstack->prev->prev; \
} while (0)
-#define OP1_1(type1, typed) \
+#define POP_S1_S2_ANY_ANY \
do { \
- POP_S1(type1); \
- GET_NEW_INDEX(new_index); \
- DST(typed, new_index; \
+ USE_S1_S2_ANY_ANY; \
+ if (curstack->varkind == UNDEFVAR) \
+ curstack->varkind = TEMPVAR; \
+ if (curstack->prev->varkind == UNDEFVAR) \
+ curstack->prev->varkind = TEMPVAR; \
+ curstack = curstack->prev->prev; \
} while (0)
-#define OP2_1(type1, type2, typed) \
+#define POP_S1_S2_S3(type1, type2, type3) \
do { \
- POP_S1_S2(type1, type2); \
- GET_NEW_INDEX(new_index); \
- DST(typed, new_index); \
- stackdepth--; \
+ USE_S1_S2_S3(type1, type2, type3); \
+ if (curstack->varkind == UNDEFVAR) \
+ curstack->varkind = TEMPVAR; \
+ if (curstack->prev->varkind == UNDEFVAR) \
+ curstack->prev->varkind = TEMPVAR; \
+ if (curstack->prev->prev->varkind == UNDEFVAR) \
+ curstack->prev->prev->varkind = TEMPVAR; \
+ curstack = curstack->prev->prev->prev; \
} while (0)
-#define DUP_SLOT(sp) \
- do { \
- if ((sp)->varkind != TEMPVAR) { \
- GET_NEW_INDEX(new_index); \
- NEWSTACK((sp)->type, TEMPVAR, new_index); \
- } \
- else \
- NEWSTACK((sp)->type, (sp)->varkind, (sp)->varnum); \
- } while(0)
+#define CLR_SX \
+ (iptr->sx.val.l = 0)
-#else /* defined(NEW_VAR) */
-#define CLR_S1 \
- (iptr->s1.var = NULL)
+/* macros for setting the destination operand ***********************/
-#define USE_S1_LOCAL(type1)
+#define CLR_DST \
+ (iptr->dst.varindex = -1)
-#define USE_S1(type1) \
+#define DST(typed, index) \
do { \
- REQUIRE_1; \
- CHECK_BASIC_TYPE(type1, curstack->type); \
- iptr->s1.var = curstack; \
+ NEWSTACKn((typed),(index)); \
+ curstack->creator = iptr; \
+ iptr->dst.varindex = (index); \
} while (0)
-#define USE_S1_ANY \
+#define DST_LOCALVAR(typed, index) \
do { \
- REQUIRE_1; \
- iptr->s1.var = curstack; \
+ NEWSTACK((typed), LOCALVAR, (index)); \
+ curstack->creator = iptr; \
+ iptr->dst.varindex = (index); \
} while (0)
-#define USE_S1_S2(type1, type2) \
- do { \
- REQUIRE_2; \
- CHECK_BASIC_TYPE(type1, curstack->prev->type); \
- CHECK_BASIC_TYPE(type2, curstack->type); \
- iptr->sx.s23.s2.var = curstack; \
- iptr->s1.var = curstack->prev; \
- } while (0)
-#define USE_S1_S2_ANY_ANY \
- do { \
- REQUIRE_2; \
- iptr->sx.s23.s2.var = curstack; \
- iptr->s1.var = curstack->prev; \
- } while (0)
+/* macro for propagating constant values ****************************/
-#define USE_S1_S2_S3(type1, type2, type3) \
+#define COPY_VAL_AND_TYPE(sd, sindex, dindex) \
do { \
- REQUIRE_3; \
- CHECK_BASIC_TYPE(type1, curstack->prev->prev->type); \
- CHECK_BASIC_TYPE(type2, curstack->prev->type); \
- CHECK_BASIC_TYPE(type3, curstack->type); \
- iptr->sx.s23.s3.var = curstack; \
- iptr->sx.s23.s2.var = curstack->prev; \
- iptr->s1.var = curstack->prev->prev; \
- } while (0)
-
-#define CLR_DST \
- (iptr->dst.var = NULL)
+ (sd).var[(dindex)].type = (sd).var[(sindex)].type; \
+ (sd).var[(dindex)].vv = (sd).var[(sindex)].vv; \
+ } while (0) \
-#define DST(typed, depth) \
- do { \
- NEWSTACKn(typed, (depth)); \
- iptr->dst.var = curstack; \
- } while (0)
-#define DST_LOCALVAR(typed, index) \
- do { \
- NEWSTACK(typed, LOCALVAR, (index)); \
- iptr->dst.var = curstack; \
- } while (0)
+/* stack modelling macros *******************************************/
#define OP0_1(typed) \
do { \
CLR_S1; \
- DST(typed, stackdepth); \
+ GET_NEW_VAR(sd, new_index, (typed)); \
+ DST(typed, new_index); \
stackdepth++; \
} while (0)
#define OP1_1(type1, typed) \
do { \
POP_S1(type1); \
- DST(typed, stackdepth - 1); \
+ GET_NEW_VAR(sd, new_index, (typed)); \
+ DST(typed, new_index); \
} while (0)
#define OP2_1(type1, type2, typed) \
do { \
POP_S1_S2(type1, type2); \
- DST(typed, stackdepth - 2); \
+ GET_NEW_VAR(sd, new_index, (typed)); \
+ DST(typed, new_index); \
stackdepth--; \
} while (0)
-#define DUP_SLOT(sp) \
+#define OP0_0 \
do { \
- if ((sp)->varkind != TEMPVAR) \
- NEWSTACK((sp)->type, TEMPVAR, stackdepth); \
- else \
- NEWSTACK((sp)->type, (sp)->varkind, (sp)->varnum); \
- } while(0)
-
-#endif /* defined(NEW_VAR) */
-
+ CLR_S1; \
+ CLR_DST; \
+ } while (0)
-#define POP_S1(type1) \
+#define OP0_BRANCH \
do { \
- USE_S1(type1); \
- if (curstack->varkind == UNDEFVAR) \
- curstack->varkind = TEMPVAR; \
- curstack = curstack->prev; \
+ CLR_S1; \
} while (0)
-#define POP_S1_ANY \
+#define OP1_0(type1) \
do { \
- USE_S1_ANY; \
- if (curstack->varkind == UNDEFVAR) \
- curstack->varkind = TEMPVAR; \
- curstack = curstack->prev; \
+ POP_S1(type1); \
+ CLR_DST; \
+ stackdepth--; \
} while (0)
-#define POP_S1_S2(type1, type2) \
+#define OP2_0(type1, type2) \
do { \
- USE_S1_S2(type1, type2); \
- if (curstack->varkind == UNDEFVAR) \
- curstack->varkind = TEMPVAR; \
- if (curstack->prev->varkind == UNDEFVAR) \
- curstack->prev->varkind = TEMPVAR; \
- curstack = curstack->prev->prev; \
+ POP_S1_S2(type1, type2); \
+ CLR_DST; \
+ stackdepth -= 2; \
} while (0)
-#define POP_S1_S2_ANY_ANY \
+#define OP2_BRANCH(type1, type2) \
do { \
- USE_S1_S2_ANY_ANY; \
- if (curstack->varkind == UNDEFVAR) \
- curstack->varkind = TEMPVAR; \
- if (curstack->prev->varkind == UNDEFVAR) \
- curstack->prev->varkind = TEMPVAR; \
- curstack = curstack->prev->prev; \
+ POP_S1_S2(type1, type2); \
+ stackdepth -= 2; \
} while (0)
-#define POP_S1_S2_S3(type1, type2, type3) \
+#define OP2_0_ANY_ANY \
do { \
- USE_S1_S2_S3(type1, type2, type3); \
- if (curstack->varkind == UNDEFVAR) \
- curstack->varkind = TEMPVAR; \
- if (curstack->prev->varkind == UNDEFVAR) \
- curstack->prev->varkind = TEMPVAR; \
- if (curstack->prev->prev->varkind == UNDEFVAR) \
- curstack->prev->prev->varkind = TEMPVAR; \
- curstack = curstack->prev->prev->prev; \
+ POP_S1_S2_ANY_ANY; \
+ CLR_DST; \
+ stackdepth -= 2; \
} while (0)
-#define CLR_SX \
- (iptr->sx.val.l = 0)
-
-#define OP0_0 \
+#define OP3_0(type1, type2, type3) \
do { \
- CLR_S1; \
+ POP_S1_S2_S3(type1, type2, type3); \
CLR_DST; \
+ stackdepth -= 3; \
} while (0)
-#define OP0_BRANCH \
+#define LOAD(type1, index) \
do { \
- CLR_S1; \
+ DST_LOCALVAR(type1, index); \
+ stackdepth++; \
} while (0)
-#define OP1_0(type1) \
+#define STORE(type1, index) \
do { \
POP_S1(type1); \
- CLR_DST; \
stackdepth--; \
} while (0)
-#define OP2_0(type1, type2) \
- do { \
- POP_S1_S2(type1, type2); \
- CLR_DST; \
- stackdepth -= 2; \
- } while (0)
-#define OP2_BRANCH(type1, type2) \
- do { \
- POP_S1_S2(type1, type2); \
- stackdepth -= 2; \
- } while (0)
+/* macros for DUP elimination ***************************************/
+
+/* XXX replace NEW_VAR with NEW_INDEX */
+#define DUP_SLOT(sp) \
+ do { \
+ GET_NEW_VAR(sd, new_index, (sp)->type); \
+ COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
+ NEWSTACK((sp)->type, TEMPVAR, new_index); \
+ } while(0)
+
+/* does not check input stackdepth */
+#define MOVE_UP(sp) \
+ do { \
+ iptr->opc = ICMD_MOVE; \
+ iptr->s1.varindex = (sp)->varnum; \
+ DUP_SLOT(sp); \
+ curstack->creator = iptr; \
+ iptr->dst.varindex = curstack->varnum; \
+ stackdepth++; \
+ } while (0)
+
+/* does not check input stackdepth */
+#define COPY_UP(sp) \
+ do { \
+ SET_TEMPVAR((sp)); \
+ iptr->opc = ICMD_COPY; \
+ iptr->s1.varindex = (sp)->varnum; \
+ DUP_SLOT(sp); \
+ curstack->creator = iptr; \
+ iptr->dst.varindex = curstack->varnum; \
+ stackdepth++; \
+ } while (0)
+
+#define COPY_DOWN(s, d) \
+ do { \
+ SET_TEMPVAR((s)); \
+ iptr->opc = ICMD_COPY; \
+ iptr->s1.varindex = (s)->varnum; \
+ iptr->dst.varindex = (d)->varnum; \
+ (d)->creator = iptr; \
+ } while (0)
+
+
+/* macros for branching / reaching basic blocks *********************/
+
+#define BRANCH_TARGET(bt, tempbptr) \
+ do { \
+ tempbptr = BLOCK_OF((bt).insindex); \
+ tempbptr = stack_mark_reached(&sd, tempbptr, curstack, \
+ stackdepth); \
+ if (tempbptr == NULL) \
+ return false; \
+ (bt).block = tempbptr; \
+ } while (0)
+
+#define BRANCH(tempbptr) \
+ BRANCH_TARGET(iptr->dst, tempbptr)
+
+
+/* forward declarations *******************************************************/
+
+static void stack_create_invars(stackdata_t *sd, basicblock *b,
+ stackptr curstack, int stackdepth);
+static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b);
+
+#if defined(STACK_VERBOSE)
+static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v);
+static void stack_verbose_show_variable(stackdata_t *sd, s4 index);
+static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr);
+static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse);
+static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend);
+#endif
+
+
+/* stack_init ******************************************************************
+
+ Initialized the stack analysis subsystem (called by jit_init).
+
+*******************************************************************************/
+
+bool stack_init(void)
+{
+ return true;
+}
+
+
+/* stack_grow_variable_array ***************************************************
+
+ Grow the variable array so the given number of additional variables fits in.
+
+ IN:
+ sd...........stack analysis data
+ num..........number of additional variables
+
+*******************************************************************************/
+
+static void stack_grow_variable_array(stackdata_t *sd, s4 num)
+{
+ s4 newcount;
+
+ assert(num >= 0);
+
+ if (num == 0)
+ return;
+
+ /* XXX avoid too many reallocations */
+ newcount = sd->varcount + num;
+
+ sd->var = DMREALLOC(sd->var, varinfo, sd->varcount, newcount);
+ sd->varcount = newcount;
+ sd->jd->var = sd->var;
+ sd->jd->varcount = newcount;
+}
+
+
+/* stack_append_block **********************************************************
+
+ Append the given block after the last real block of the method (before
+ the pseudo-block at the end).
+
+ IN:
+ sd...........stack analysis data
+ b............the block to append
+
+*******************************************************************************/
+
+static void stack_append_block(stackdata_t *sd, basicblock *b)
+{
+#if defined(STACK_VERBOSE)
+ printf("APPENDING BLOCK L%0d\n", b->nr);
+#endif
+
+ b->next = sd->last_real_block->next;
+ sd->last_real_block->next = b;
+ sd->last_real_block = b;
+ sd->jd->new_basicblockcount++;
+}
+
+
+/* stack_clone_block ***********************************************************
+
+ Create a copy of the given block and insert it at the end of the method.
+
+ CAUTION: This function does not copy the any variables or the instruction
+ list. It _does_, however, reserve space for the block's invars in the
+ variable array.
+
+ IN:
+ sd...........stack analysis data
+ b............the block to clone
+
+ RETURN VALUE:
+ a pointer to the copy
+
+*******************************************************************************/
+
+static basicblock * stack_clone_block(stackdata_t *sd, basicblock *b)
+{
+ basicblock *clone;
+
+ clone = DNEW(basicblock);
+ *clone = *b;
+
+ clone->iinstr = NULL;
+ clone->inlocals = NULL;
+ clone->invars = NULL;
+
+ clone->original = (b->original) ? b->original : b;
+ clone->copied_to = clone->original->copied_to;
+ clone->original->copied_to = clone;
+ clone->nr = sd->m->c_debug_nr++;
+ clone->next = NULL;
+ clone->flags = BBREACHED;
+
+ stack_append_block(sd, clone);
+
+ /* allocate space for the invars of the clone */
+
+ stack_grow_variable_array(sd, b->indepth);
+
+#if defined(STACK_VERBOSE)
+ printf("cloning block L%03d ------> L%03d\n", b->nr, clone->nr);
+#endif
+
+ return clone;
+}
+
+
+/* stack_create_invars *********************************************************
+
+ Create the invars for the given basic block. Also make a copy of the locals.
+
+ IN:
+ sd...........stack analysis data
+ b............block to create the invars for
+ curstack.....current stack top
+ stackdepth...current stack depth
+
+ This function creates STACKDEPTH invars and sets their types to the
+ types to the types of the corresponding slot in the current stack.
+
+*******************************************************************************/
+
+static void stack_create_invars(stackdata_t *sd, basicblock *b,
+ stackptr curstack, int stackdepth)
+{
+ stackptr sp;
+ int i;
+ int index;
+ varinfo *v;
+
+ assert(sd->vartop + stackdepth <= sd->varcount);
+
+ b->indepth = stackdepth;
+ b->invars = DMNEW(s4, stackdepth);
+
+ /* allocate the variable indices */
+ index = (sd->vartop += stackdepth);
+
+ i = stackdepth;
+ for (sp = curstack; i--; sp = sp->prev) {
+ b->invars[i] = --index;
+ v = sd->var + index;
+ v->type = sp->type;
+ v->flags = OUTVAR;
+ v->vv = sd->var[sp->varnum].vv;
+#if defined(STACK_VERBOSE) && 0
+ printf("\tinvar[%d]: %d\n", i, sd->var[b->invars[i]]);
+#endif
+ }
+
+ /* copy the current state of the local variables */
+
+ v = DMNEW(varinfo, sd->localcount);
+ b->inlocals = v;
+ for (i=0; i<sd->localcount; ++i)
+ *v++ = sd->var[i];
+}
+
+
+/* stack_create_invars_from_outvars ********************************************
+
+ Create the invars for the given basic block. Also make a copy of the locals.
+ Types are propagated from the outvars of the current block.
+
+ IN:
+ sd...........stack analysis data
+ b............block to create the invars for
+
+*******************************************************************************/
+
+static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b)
+{
+ int i;
+ int n;
+ varinfo *sv, *dv;
+
+ n = sd->bptr->outdepth;
+ assert(sd->vartop + n <= sd->varcount);
+
+ b->indepth = n;
+ b->invars = DMNEW(s4, n);
+
+ if (n) {
+ dv = sd->var + sd->vartop;
+
+ /* allocate the invars */
+
+ for (i=0; i<n; ++i, ++dv) {
+ sv = sd->var + sd->bptr->outvars[i];
+ b->invars[i] = sd->vartop++;
+ dv->type = sv->type;
+ dv->flags = OUTVAR;
+ dv->vv = sv->vv;
+ }
+ }
+
+ /* copy the current state of the local variables */
+
+ dv = DMNEW(varinfo, sd->localcount);
+ b->inlocals = dv;
+ for (i=0; i<sd->localcount; ++i)
+ *dv++ = sd->var[i];
+}
+
+
+/* stack_check_invars **********************************************************
+
+ Check the current stack against the invars of the given basic block.
+ Depth and types must match.
+
+ IN:
+ sd...........stack analysis data
+ b............block which invars to check against
+ curstack.....current stack top
+ stackdepth...current stack depth
+
+ RETURN VALUE:
+ the destinaton block
+ NULL.........a VerifyError has been thrown
+
+*******************************************************************************/
+
+static basicblock * stack_check_invars(stackdata_t *sd, basicblock *b,
+ stackptr curstack, int stackdepth)
+{
+ int i;
+ stackptr sp;
+ basicblock *orig;
+ bool separable;
+
+#if defined(STACK_VERBOSE)
+ printf("stack_check_invars(L%03d)\n", b->nr);
+#endif
+
+ /* find original of b */
+ if (b->original)
+ b = b->original;
+ orig = b;
+
+#if defined(STACK_VERBOSE)
+ printf("original is L%03d\n", orig->nr);
+#endif
+
+ i = orig->indepth;
+
+ if (i != stackdepth) {
+ exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
+ return NULL;
+ }
+
+ do {
+ separable = false;
+
+#if defined(STACK_VERBOSE)
+ printf("checking against ");
+ stack_verbose_show_block(sd, b); printf("\n");
+#endif
+
+ sp = curstack;
+ for (i = orig->indepth; i--; sp = sp->prev) {
+ if (sd->var[b->invars[i]].type != sp->type) {
+ exceptions_throw_verifyerror_for_stack(sd->m,
+ sd->var[b->invars[i]].type);
+ return NULL;
+ }
+
+ if (sp->type == TYPE_RET) {
+ if (sd->var[b->invars[i]].vv.retaddr != sd->var[sp->varnum].vv.retaddr) {
+ separable = true;
+ break;
+ }
+ }
+ }
+
+ if (b->inlocals) {
+ for (i=0; i<sd->localcount; ++i) {
+ if (sd->var[i].type == TYPE_RET && b->inlocals[i].type == TYPE_RET) {
+ if (sd->var[i].vv.retaddr != b->inlocals[i].vv.retaddr) {
+ separable = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (!separable) {
+ /* XXX mark mixed type variables void */
+ /* XXX cascading collapse? */
+#if defined(STACK_VERBOSE)
+ printf("------> using L%03d\n", b->nr);
+#endif
+ return b;
+ }
+ } while ((b = b->copied_to) != NULL);
+
+ b = stack_clone_block(sd, orig);
+ if (!b)
+ return NULL;
+
+ stack_create_invars(sd, b, curstack, stackdepth);
+ return b;
+}
+
+
+/* stack_check_invars_from_outvars *********************************************
+
+ Check the outvars of the current block against the invars of the given block.
+ Depth and types must match.
+
+ IN:
+ sd...........stack analysis data
+ b............block which invars to check against
+
+ RETURN VALUE:
+ the destinaton block
+ NULL.........a VerifyError has been thrown
+
+*******************************************************************************/
+
+static basicblock * stack_check_invars_from_outvars(stackdata_t *sd, basicblock *b)
+{
+ int i;
+ int n;
+ varinfo *sv, *dv;
+ basicblock *orig;
+ bool separable;
+
+#if defined(STACK_VERBOSE)
+ printf("stack_check_invars_from_outvars(L%03d)\n", b->nr);
+#endif
+
+ /* find original of b */
+ if (b->original)
+ b = b->original;
+ orig = b;
+
+#if defined(STACK_VERBOSE)
+ printf("original is L%03d\n", orig->nr);
+#endif
+
+ i = orig->indepth;
+ n = sd->bptr->outdepth;
+
+ if (i != n) {
+ exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
+ return NULL;
+ }
+
+ do {
+ separable = false;
+
+#if defined(STACK_VERBOSE)
+ printf("checking against ");
+ stack_verbose_show_block(sd, b); printf("\n");
+#endif
+
+ if (n) {
+ dv = sd->var + b->invars[0];
+
+ for (i=0; i<n; ++i, ++dv) {
+ sv = sd->var + sd->bptr->outvars[i];
+ if (sv->type != dv->type) {
+ exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
+ return NULL;
+ }
+
+ if (dv->type == TYPE_RET) {
+ if (sv->vv.retaddr != dv->vv.retaddr) {
+ separable = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (b->inlocals) {
+ for (i=0; i<sd->localcount; ++i) {
+ if (sd->var[i].type == TYPE_RET && b->inlocals[i].type == TYPE_RET) {
+ if (sd->var[i].vv.retaddr != b->inlocals[i].vv.retaddr) {
+ separable = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (!separable) {
+ /* XXX mark mixed type variables void */
+ /* XXX cascading collapse? */
+#if defined(STACK_VERBOSE)
+ printf("------> using L%03d\n", b->nr);
+#endif
+ return b;
+ }
+ } while ((b = b->copied_to) != NULL);
+
+ b = stack_clone_block(sd, orig);
+ if (!b)
+ return NULL;
+
+ stack_create_invars_from_outvars(sd, b);
+ return b;
+}
+
+
+/* stack_create_instack ********************************************************
+
+ Create the instack of the current basic block.
+
+ IN:
+ sd...........stack analysis data
+
+ RETURN VALUE:
+ the current stack top at the start of the basic block.
+
+*******************************************************************************/
+
+static stackptr stack_create_instack(stackdata_t *sd)
+{
+ stackptr sp;
+ int depth;
+ int index;
+
+ if ((depth = sd->bptr->indepth) == 0)
+ return NULL;
+
+ sp = (sd->new += depth);
+
+ while (depth--) {
+ sp--;
+ index = sd->bptr->invars[depth];
+ sp->varnum = index;
+ sp->type = sd->var[index].type;
+ sp->prev = sp - 1;
+ sp->creator = NULL;
+ sp->flags = 0;
+ sp->varkind = STACKVAR;
+ }
+ sp->prev = NULL;
+
+ /* return the top of the created stack */
+ return sd->new - 1;
+}
+
+
+/* stack_mark_reached **********************************************************
+
+ Mark the given block reached and propagate the current stack and locals to
+ it. This function specializes the target block, if necessary, and returns
+ a pointer to the specialized target.
+
+ IN:
+ sd...........stack analysis data
+ b............the block to reach
+ curstack.....the current stack top
+ stackdepth...the current stack depth
+
+ RETURN VALUE:
+ a pointer to (a specialized version of) the target
+ NULL.........a VerifyError has been thrown
+
+*******************************************************************************/
+
+static basicblock *stack_mark_reached(stackdata_t *sd, basicblock *b, stackptr curstack, int stackdepth)
+{
+#if defined(STACK_VERBOSE)
+ printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
+#endif
+ /* mark targets of backward branches */
+ if (b <= sd->bptr)
+ b->bitflags |= BBFLAG_REPLACEMENT;
+
+ if (b->flags < BBREACHED) {
+ /* b is reached for the first time. Create its invars. */
+
+#if defined(STACK_VERBOSE)
+ printf("reached L%03d for the first time\n", b->nr);
+#endif
+
+ stack_create_invars(sd, b, curstack, stackdepth);
+
+ b->flags = BBREACHED;
+
+ return b;
+ }
+ else {
+ /* b has been reached before. Check that its invars match. */
+
+ return stack_check_invars(sd, b, curstack, stackdepth);
+ }
+}
+
+
+/* stack_mark_reached_from_outvars *********************************************
+
+ Mark the given block reached and propagate the outvars of the current block
+ and the current locals to it. This function specializes the target block,
+ if necessary, and returns a pointer to the specialized target.
+
+ IN:
+ sd...........stack analysis data
+ b............the block to reach
+
+ RETURN VALUE:
+ a pointer to (a specialized version of) the target
+ NULL.........a VerifyError has been thrown
+
+*******************************************************************************/
+
+static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
+{
+#if defined(STACK_VERBOSE)
+ printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
+#endif
+ /* mark targets of backward branches */
+ if (b <= sd->bptr)
+ b->bitflags |= BBFLAG_REPLACEMENT;
+
+ if (b->flags < BBREACHED) {
+ /* b is reached for the first time. Create its invars. */
+
+#if defined(STACK_VERBOSE)
+ printf("reached L%03d for the first time\n", b->nr);
+#endif
+
+ stack_create_invars_from_outvars(sd, b);
+
+ b->flags = BBREACHED;
+
+ return b;
+ }
+ else {
+ /* b has been reached before. Check that its invars match. */
+
+ return stack_check_invars_from_outvars(sd, b);
+ }
+}
+
+
+/* stack_reach_next_block ******************************************************
+
+ Mark the following block reached and propagate the outvars of the current block
+ and the current locals to it. This function specializes the target block,
+ if necessary, and returns a pointer to the specialized target.
+
+ IN:
+ sd...........stack analysis data
+
+ RETURN VALUE:
+ a pointer to (a specialized version of) the following block
+ NULL.........a VerifyError has been thrown
+
+*******************************************************************************/
+
+static bool stack_reach_next_block(stackdata_t *sd)
+{
+ basicblock *tbptr;
+ instruction *iptr;
+
+ tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
+ tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
+ if (!tbptr)
+ return false;
+
+ if (tbptr != sd->bptr->next) {
+#if defined(STACK_VERBOSE)
+ printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
+#endif
+ iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
+ assert(iptr->opc == ICMD_NOP);
+ iptr->opc = ICMD_GOTO;
+ iptr->dst.block = tbptr;
+
+ if (tbptr->flags < BBFINISHED)
+ sd->repeat = true; /* XXX check if we really need to repeat */
+ }
+
+ return true;
+}
+
+
+/* stack_reach_handlers ********************************************************
+
+ Reach the exception handlers for the current block.
+
+ IN:
+ sd...........stack analysis data
+
+ RETURN VALUE:
+ true.........everything ok
+ false........a VerifyError has been thrown
+
+*******************************************************************************/
+
+static bool stack_reach_handlers(stackdata_t *sd)
+{
+ s4 i;
+ basicblock *tbptr;
+
+#if defined(STACK_VERBOSE)
+ printf("reaching exception handlers...\n");
+#endif
+
+ for (i=0; sd->handlers[i]; ++i) {
+ tbptr = sd->handlers[i]->handler;
+
+ tbptr->type = BBTYPE_EXH;
+ tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
+
+ /* reach (and specialize) the handler block */
+
+ tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
+
+ if (tbptr == NULL)
+ return false;
+
+ sd->handlers[i]->handler = tbptr;
+ }
+
+ return true;
+}
+
+
+/* stack_reanalyse_block ******************************************************
+
+ Re-analyse the current block. This is called if either the block itself
+ has already been analysed before, or the current block is a clone of an
+ already analysed block, and this clone is reached for the first time.
+ In the latter case, this function does all that is necessary for fully
+ cloning the block (cloning the instruction list and variables, etc.).
+
+ IN:
+ sd...........stack analysis data
+
+ RETURN VALUE:
+ true.........everything ok
+ false........a VerifyError has been thrown
+
+*******************************************************************************/
+
+#define RELOCATE(index) \
+ do { \
+ if ((index) >= blockvarstart) \
+ (index) += blockvarshift; \
+ else if ((index) >= invarstart) \
+ (index) += invarshift; \
+ } while (0)
+
+bool stack_reanalyse_block(stackdata_t *sd)
+{
+ instruction *iptr;
+ basicblock *b;
+ basicblock *orig;
+ s4 len;
+ s4 invarstart;
+ s4 blockvarstart;
+ s4 invarshift;
+ s4 blockvarshift;
+ s4 i, j;
+ s4 *argp;
+ branch_target_t *table;
+ lookup_target_t *lookup;
+ bool superblockend;
+ bool maythrow;
+ bool cloneinstructions;
+ exceptiontable *ex;
+
+#if defined(STACK_VERBOSE)
+ stack_verbose_block_enter(sd, true);
+#endif
+
+ b = sd->bptr;
+
+ if (!b->iinstr) {
+ orig = b->original;
+ assert(orig != NULL);
+
+ /* clone the instruction list */
+
+ cloneinstructions = true;
+
+ assert(orig->iinstr);
+ len = orig->icount;
+ iptr = DMNEW(instruction, len + 1);
+
+ MCOPY(iptr, orig->iinstr, instruction, len);
+ iptr[len].opc = ICMD_NOP;
+ b->iinstr = iptr;
+ b->icount = ++len;
+
+ /* allocate space for the clone's block variables */
+
+ stack_grow_variable_array(sd, orig->varcount);
+
+ /* we already have the invars set */
+
+ assert(b->indepth == orig->indepth);
+
+ /* calculate relocation shifts for invars and block variables */
+
+ if (orig->indepth) {
+ invarstart = orig->invars[0];
+ invarshift = b->invars[0] - invarstart;
+ }
+ else {
+ invarstart = INT_MAX;
+ invarshift = 0;
+ }
+ blockvarstart = orig->varstart;
+ blockvarshift = sd->vartop - blockvarstart;
+
+ /* copy block variables */
+
+ b->varstart = sd->vartop;
+ b->varcount = orig->varcount;
+ sd->vartop += b->varcount;
+ MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
+
+ /* copy outvars */
+
+ b->outdepth = orig->outdepth;
+ b->outvars = DMNEW(s4, orig->outdepth);
+ MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
+
+ /* clone exception handlers */
+
+ for (i=0; sd->handlers[i]; ++i) {
+ ex = DNEW(exceptiontable);
+ ex->handler = sd->handlers[i]->handler;
+ ex->start = b;
+ ex->end = b; /* XXX hack, see end of new_stack_analyse */
+ ex->catchtype = sd->handlers[i]->catchtype;
+ ex->down = NULL;
+
+ assert(sd->extableend->down == NULL);
+ sd->extableend->down = ex;
+ sd->extableend = ex;
+ sd->jd->cd->exceptiontablelength++;
+
+ sd->handlers[i] = ex;
+ }
+ }
+ else {
+ cloneinstructions = false;
+ invarshift = 0;
+ blockvarshift = 0;
+ invarstart = sd->vartop;
+ blockvarstart = sd->vartop;
+ iptr = b->iinstr;
+ }
+
+ if (b->original) {
+ /* find exception handlers for the cloned block */
+ len = 0;
+ ex = sd->jd->cd->exceptiontable;
+ for (; ex != NULL; ex = ex->down) {
+ /* XXX the cloned exception handlers have identical */
+ /* start end end blocks. */
+ if ((ex->start == b) && (ex->end == b)) {
+ sd->handlers[len++] = ex;
+ }
+ }
+ sd->handlers[len] = NULL;
+ }
+
+#if defined(STACK_VERBOSE)
+ printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
+ printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
+#endif
+
+ /* mark block as finished */
+
+ b->flags = BBFINISHED;
+
+ /* initialize locals at the start of this block */
+
+ if (b->inlocals)
+ MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
+
+ /* reach exception handlers for this block */
+
+ if (!stack_reach_handlers(sd))
+ return false;
+
+ superblockend = false;
+
+ for (len = b->icount; len--; iptr++) {
+#if defined(STACK_VERBOSE)
+ new_show_icmd(sd->jd, iptr, false, SHOW_STACK);
+ printf("\n");
+#endif
+
+ maythrow = false;
+
+ switch (iptr->opc) {
+ case ICMD_RET:
+ j = iptr->s1.varindex;
+
+ if (sd->var[j].type != TYPE_RET) {
+ exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
+ return false;
+ }
+
+ iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
+ superblockend = true;
+ break;
+
+ case ICMD_JSR:
+ iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
+ superblockend = true;
+ break;
+
+ case ICMD_RETURN:
+ superblockend = true;
+ break;
+
+ case ICMD_CHECKNULL:
+ case ICMD_PUTSTATICCONST:
+ maythrow = true;
+ break;
+
+ case ICMD_NOP:
+ case ICMD_IINC:
+ case ICMD_INLINE_START:
+ case ICMD_INLINE_END:
+ case ICMD_INLINE_GOTO:
+ break;
+
+ case ICMD_GOTO:
+ iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
+ superblockend = true;
+ break;
+
+ /* pop 0 push 1 const */
+
+ case ICMD_ACONST:
+ maythrow = true;
+ case ICMD_ICONST:
+ case ICMD_LCONST:
+ case ICMD_FCONST:
+ case ICMD_DCONST:
+
+ /* pop 0 push 1 load */
+
+ case ICMD_ILOAD:
+ case ICMD_LLOAD:
+ case ICMD_FLOAD:
+ case ICMD_DLOAD:
+ case ICMD_ALOAD:
+ RELOCATE(iptr->dst.varindex);
+ break;
+
+ /* pop 2 push 1 */
+
+ case ICMD_IALOAD:
+ case ICMD_LALOAD:
+ case ICMD_FALOAD:
+ case ICMD_DALOAD:
+ case ICMD_AALOAD:
+ case ICMD_BALOAD:
+ case ICMD_CALOAD:
+ case ICMD_SALOAD:
+ RELOCATE(iptr->sx.s23.s2.varindex);
+ RELOCATE(iptr->s1.varindex);
+ RELOCATE(iptr->dst.varindex);
+ maythrow = true;
+ break;
+
+ /* pop 3 push 0 */
+
+ case ICMD_IASTORE:
+ case ICMD_LASTORE:
+ case ICMD_FASTORE:
+ case ICMD_DASTORE:
+ case ICMD_AASTORE:
+ case ICMD_BASTORE:
+ case ICMD_CASTORE:
+ case ICMD_SASTORE:
+ RELOCATE(iptr->sx.s23.s3.varindex);
+ RELOCATE(iptr->sx.s23.s2.varindex);
+ RELOCATE(iptr->s1.varindex);
+ maythrow = true;
+ break;
+
+ /* pop 1 push 0 store */
+
+ case ICMD_ISTORE:
+ case ICMD_LSTORE:
+ case ICMD_FSTORE:
+ case ICMD_DSTORE:
+ case ICMD_ASTORE:
+ RELOCATE(iptr->s1.varindex);
+
+ j = iptr->dst.varindex;
+ COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
+ break;
+
+ /* pop 1 push 0 */
+
+ case ICMD_ARETURN:
+ case ICMD_ATHROW:
+ maythrow = true;
+ case ICMD_IRETURN:
+ case ICMD_LRETURN:
+ case ICMD_FRETURN:
+ case ICMD_DRETURN:
+ RELOCATE(iptr->s1.varindex);
+ superblockend = true;
+ break;
+
+ case ICMD_PUTSTATIC:
+ case ICMD_PUTFIELDCONST:
+ maythrow = true;
+ case ICMD_POP:
+ RELOCATE(iptr->s1.varindex);
+ break;
+
+ /* pop 1 push 0 branch */
+
+ case ICMD_IFNULL:
+ case ICMD_IFNONNULL:
+
+ case ICMD_IFEQ:
+ case ICMD_IFNE:
+ case ICMD_IFLT:
+ case ICMD_IFGE:
+ case ICMD_IFGT:
+ case ICMD_IFLE:
+
+ case ICMD_IF_LEQ:
+ case ICMD_IF_LNE:
+ case ICMD_IF_LLT:
+ case ICMD_IF_LGE:
+ case ICMD_IF_LGT:
+ case ICMD_IF_LLE:
+ RELOCATE(iptr->s1.varindex);
+ iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
+ break;
+
+ /* pop 1 push 0 table branch */
+
+ case ICMD_TABLESWITCH:
+ i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
+
+ if (cloneinstructions) {
+ table = DMNEW(branch_target_t, i);
+ MCOPY(table, iptr->dst.table, branch_target_t, i);
+ iptr->dst.table = table;
+ }
+ else {
+ table = iptr->dst.table;
+ }
+
+ RELOCATE(iptr->s1.varindex);
+ while (i--) {
+ table->block = stack_mark_reached_from_outvars(sd, table->block);
+ table++;
+ }
+ superblockend = true;
+ break;
+
+ case ICMD_LOOKUPSWITCH:
+ i = iptr->sx.s23.s2.lookupcount;
+ if (cloneinstructions) {
+ lookup = DMNEW(lookup_target_t, i);
+ MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
+ iptr->dst.lookup = lookup;
+ }
+ else {
+ lookup = iptr->dst.lookup;
+ }
+ RELOCATE(iptr->s1.varindex);
+ while (i--) {
+ lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
+ lookup++;
+ }
+ iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
+ superblockend = true;
+ break;
+
+ case ICMD_MONITORENTER:
+ case ICMD_MONITOREXIT:
+ RELOCATE(iptr->s1.varindex);
+ maythrow = true;
+ break;
+
+ /* pop 2 push 0 branch */
+
+ case ICMD_IF_ICMPEQ:
+ case ICMD_IF_ICMPNE:
+ case ICMD_IF_ICMPLT:
+ case ICMD_IF_ICMPGE:
+ case ICMD_IF_ICMPGT:
+ case ICMD_IF_ICMPLE:
+
+ case ICMD_IF_LCMPEQ:
+ case ICMD_IF_LCMPNE:
+ case ICMD_IF_LCMPLT:
+ case ICMD_IF_LCMPGE:
+ case ICMD_IF_LCMPGT:
+ case ICMD_IF_LCMPLE:
+
+ case ICMD_IF_FCMPEQ:
+ case ICMD_IF_FCMPNE:
+
+ case ICMD_IF_FCMPL_LT:
+ case ICMD_IF_FCMPL_GE:
+ case ICMD_IF_FCMPL_GT:
+ case ICMD_IF_FCMPL_LE:
+
+ case ICMD_IF_FCMPG_LT:
+ case ICMD_IF_FCMPG_GE:
+ case ICMD_IF_FCMPG_GT:
+ case ICMD_IF_FCMPG_LE:
+
+ case ICMD_IF_DCMPEQ:
+ case ICMD_IF_DCMPNE:
+
+ case ICMD_IF_DCMPL_LT:
+ case ICMD_IF_DCMPL_GE:
+ case ICMD_IF_DCMPL_GT:
+ case ICMD_IF_DCMPL_LE:
+
+ case ICMD_IF_DCMPG_LT:
+ case ICMD_IF_DCMPG_GE:
+ case ICMD_IF_DCMPG_GT:
+ case ICMD_IF_DCMPG_LE:
+
+ case ICMD_IF_ACMPEQ:
+ case ICMD_IF_ACMPNE:
+ RELOCATE(iptr->sx.s23.s2.varindex);
+ RELOCATE(iptr->s1.varindex);
+ iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
+ break;
+
+ /* pop 2 push 0 */
+
+ case ICMD_PUTFIELD:
+ case ICMD_IASTORECONST:
+ case ICMD_LASTORECONST:
+ case ICMD_AASTORECONST:
+ case ICMD_BASTORECONST:
+ case ICMD_CASTORECONST:
+ case ICMD_SASTORECONST:
+ maythrow = true;
+ case ICMD_POP2:
+ RELOCATE(iptr->sx.s23.s2.varindex);
+ RELOCATE(iptr->s1.varindex);
+ break;
+
+ /* pop 0 push 1 copy */
+
+ case ICMD_COPY:
+ case ICMD_MOVE:
+ RELOCATE(iptr->dst.varindex);
+ RELOCATE(iptr->s1.varindex);
+ COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
+ break;
+
+ /* pop 2 push 1 */
+
+ case ICMD_IDIV:
+ case ICMD_IREM:
+ case ICMD_LDIV:
+ case ICMD_LREM:
+ maythrow = true;
+ case ICMD_IADD:
+ case ICMD_ISUB:
+ case ICMD_IMUL:
+ case ICMD_ISHL:
+ case ICMD_ISHR:
+ case ICMD_IUSHR:
+ case ICMD_IAND:
+ case ICMD_IOR:
+ case ICMD_IXOR:
+ case ICMD_LADD:
+ case ICMD_LSUB:
+ case ICMD_LMUL:
+ case ICMD_LOR:
+ case ICMD_LAND:
+ case ICMD_LXOR:
+ case ICMD_LSHL:
+ case ICMD_LSHR:
+ case ICMD_LUSHR:
+ case ICMD_FADD:
+ case ICMD_FSUB:
+ case ICMD_FMUL:
+ case ICMD_FDIV:
+ case ICMD_FREM:
+ case ICMD_DADD:
+ case ICMD_DSUB:
+ case ICMD_DMUL:
+ case ICMD_DDIV:
+ case ICMD_DREM:
+ case ICMD_LCMP:
+ case ICMD_FCMPL:
+ case ICMD_FCMPG:
+ case ICMD_DCMPL:
+ case ICMD_DCMPG:
+ RELOCATE(iptr->sx.s23.s2.varindex);
+ RELOCATE(iptr->s1.varindex);
+ RELOCATE(iptr->dst.varindex);
+ break;
+
+ /* pop 1 push 1 */
+
+ case ICMD_CHECKCAST:
+ case ICMD_ARRAYLENGTH:
+ case ICMD_INSTANCEOF:
+ case ICMD_NEWARRAY:
+ case ICMD_ANEWARRAY:
+ maythrow = true;
+ case ICMD_GETFIELD:
+ case ICMD_IADDCONST:
+ case ICMD_ISUBCONST:
+ case ICMD_IMULCONST:
+ case ICMD_IMULPOW2:
+ case ICMD_IDIVPOW2:
+ case ICMD_IREMPOW2:
+ case ICMD_IANDCONST:
+ case ICMD_IORCONST:
+ case ICMD_IXORCONST:
+ case ICMD_ISHLCONST:
+ case ICMD_ISHRCONST:
+ case ICMD_IUSHRCONST:
+ case ICMD_LADDCONST:
+ case ICMD_LSUBCONST:
+ case ICMD_LMULCONST:
+ case ICMD_LMULPOW2:
+ case ICMD_LDIVPOW2:
+ case ICMD_LREMPOW2:
+ case ICMD_LANDCONST:
+ case ICMD_LORCONST:
+ case ICMD_LXORCONST:
+ case ICMD_LSHLCONST:
+ case ICMD_LSHRCONST:
+ case ICMD_LUSHRCONST:
+ case ICMD_INEG:
+ case ICMD_INT2BYTE:
+ case ICMD_INT2CHAR:
+ case ICMD_INT2SHORT:
+ case ICMD_LNEG:
+ case ICMD_FNEG:
+ case ICMD_DNEG:
+ case ICMD_I2L:
+ case ICMD_I2F:
+ case ICMD_I2D:
+ case ICMD_L2I:
+ case ICMD_L2F:
+ case ICMD_L2D:
+ case ICMD_F2I:
+ case ICMD_F2L:
+ case ICMD_F2D:
+ case ICMD_D2I:
+ case ICMD_D2L:
+ case ICMD_D2F:
+ RELOCATE(iptr->s1.varindex);
+ RELOCATE(iptr->dst.varindex);
+ break;
+
+ /* pop 0 push 1 */
+
+ case ICMD_GETSTATIC:
+ case ICMD_NEW:
+ maythrow = true;
+ RELOCATE(iptr->dst.varindex);
+ break;
+
+ /* pop many push any */
+
+ case ICMD_INVOKESTATIC:
+ case ICMD_INVOKESPECIAL:
+ case ICMD_INVOKEVIRTUAL:
+ case ICMD_INVOKEINTERFACE:
+ case ICMD_BUILTIN:
+ case ICMD_MULTIANEWARRAY:
+ i = iptr->s1.argcount;
+ if (cloneinstructions) {
+ argp = DMNEW(s4, i);
+ MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
+ iptr->sx.s23.s2.args = argp;
+ }
+ else {
+ argp = iptr->sx.s23.s2.args;
+ }
+
+ maythrow = true;
+ while (--i >= 0) {
+ RELOCATE(*argp);
+ argp++;
+ }
+ RELOCATE(iptr->dst.varindex);
+ break;
+
+ default:
+ *exceptionptr =
+ new_internalerror("Unknown ICMD %d during stack re-analysis",
+ iptr->opc);
+ return false;
+ } /* switch */
+
+#if defined(STACK_VERBOSE)
+ new_show_icmd(sd->jd, iptr, false, SHOW_STACK);
+ printf("\n");
+#endif
+ }
+
+ /* relocate outvars */
+
+ for (i=0; i<b->outdepth; ++i) {
+ RELOCATE(b->outvars[i]);
+ }
-#define OP2_0_ANY_ANY \
- do { \
- POP_S1_S2_ANY_ANY; \
- CLR_DST; \
- stackdepth -= 2; \
- } while (0)
+#if defined(STACK_VERBOSE)
+ stack_verbose_block_exit(sd, superblockend);
+#endif
-#define OP3_0(type1, type2, type3) \
- do { \
- POP_S1_S2_S3(type1, type2, type3); \
- CLR_DST; \
- stackdepth -= 3; \
- } while (0)
+ /* propagate to the next block */
-#define LOAD(type1, index) \
- do { \
- DST_LOCALVAR(type1, index); \
- stackdepth++; \
- } while (0)
+ if (!superblockend)
+ if (!stack_reach_next_block(sd))
+ return false;
-#define STORE(type1, index) \
- do { \
- POP_S1(type1); \
- stackdepth--; \
- } while (0)
+ return true;
+}
-#define BRANCH_TARGET(bt, tempbptr, tempsp) \
- do { \
- (bt).block = tempbptr = BLOCK_OF((bt).insindex); \
- MARKREACHED(tempbptr, tempsp); \
- } while (0)
-#define BRANCH(tempbptr, tempsp) \
- do { \
- iptr->dst.block = tempbptr = BLOCK_OF(iptr->dst.insindex); \
- MARKREACHED(tempbptr, tempsp); \
- } while (0)
+/* stack_analyse ***************************************************************
+
+ Analyse_stack uses the intermediate code created by parse.c to
+ build a model of the JVM operand stack for the current method.
+
+ The following checks are performed:
+ - check for operand stack underflow (before each instruction)
+ - check for operand stack overflow (after[1] each instruction)
+ - check for matching stack depth at merging points
+ - check for matching basic types[2] at merging points
+ - check basic types for instruction input (except for BUILTIN*
+ opcodes, INVOKE* opcodes and MULTIANEWARRAY)
+
+ [1]) Checking this after the instruction should be ok. parse.c
+ counts the number of required stack slots in such a way that it is
+ only vital that we don't exceed `maxstack` at basic block
+ boundaries.
+
+ [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
+ DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
+ types are not discerned.
+
+*******************************************************************************/
bool new_stack_analyse(jitdata *jd)
{
codeinfo *code;
codegendata *cd;
registerdata *rd;
- int b_count; /* basic block counter */
+ stackdata_t sd;
int b_index; /* basic block index */
int stackdepth;
stackptr curstack; /* current stack top */
- stackptr new;
stackptr copy;
int opcode; /* opcode of current instruction */
int i, j;
+ int javaindex;
int len; /* # of instructions after the current one */
bool superblockend; /* if true, no fallthrough to next block */
- bool repeat; /* if true, outermost loop must run again */
bool deadcode; /* true if no live code has been reached */
instruction *iptr; /* the current instruction */
- basicblock *bptr; /* the current basic block */
basicblock *tbptr;
+ basicblock *original;
+ exceptiontable *ex;
stackptr *last_store_boundary;
- stackptr last_pei_boundary;
- stackptr last_dup_boundary;
+ stackptr coalescing_boundary;
+
+ stackptr src1, src2, src3, src4, dst1, dst2;
branch_target_t *table;
lookup_target_t *lookup;
#if defined(ENABLE_STATISTICS)
int iteration_count; /* number of iterations of analysis */
#endif
-#if defined(NEW_VAR)
int new_index; /* used to get a new var index with GET_NEW_INDEX*/
-#endif
+
#if defined(STACK_VERBOSE)
new_show_method(jd, SHOW_PARSE);
#endif
cd = jd->cd;
rd = jd->rd;
+ /* initialize the stackdata_t struct */
+
+ sd.m = m;
+ sd.jd = jd;
+ sd.varcount = jd->varcount;
+ sd.vartop = jd->vartop;
+ sd.localcount = jd->localcount;
+ sd.var = jd->var;
+ sd.handlers = DMNEW(exceptiontable *, cd->exceptiontablelength + 1);
+ sd.exstack.type = TYPE_ADR;
+ sd.exstack.prev = NULL;
+ sd.exstack.varnum = 0; /* XXX do we need a real variable index here? */
+
#if defined(ENABLE_LSRA)
m->maxlifetimes = 0;
#endif
iteration_count = 0;
#endif
-#if defined(NEW_VAR)
- last_store_boundary = DMNEW(stackptr, jd->localcount);
-#else
- last_store_boundary = DMNEW(stackptr , cd->maxlocals);
-#endif
+ /* find the last real basic block */
+
+ sd.last_real_block = NULL;
+ tbptr = jd->new_basicblocks;
+ while (tbptr->next) {
+ sd.last_real_block = tbptr;
+ tbptr = tbptr->next;
+ }
+ assert(sd.last_real_block);
+
+ /* find the last exception handler */
+
+ if (cd->exceptiontablelength)
+ sd.extableend = cd->exceptiontable + cd->exceptiontablelength - 1;
+ else
+ sd.extableend = NULL;
+
+ /* init jd->interface_map */
+
+ jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
+ for (i = 0; i < m->maxstack * 5; i++)
+ jd->interface_map[i].flags = UNUSED;
- /* initialize in-stack of first block */
+ last_store_boundary = DMNEW(stackptr, cd->maxlocals);
+
+ /* initialize flags and invars (none) of first block */
- new = jd->new_stack;
jd->new_basicblocks[0].flags = BBREACHED;
- jd->new_basicblocks[0].instack = NULL;
jd->new_basicblocks[0].invars = NULL;
jd->new_basicblocks[0].indepth = 0;
- /* initialize in-stack of exception handlers */
-
- for (i = 0; i < cd->exceptiontablelength; i++) {
- bptr = BLOCK_OF(cd->exceptiontable[i].handlerpc);
- bptr->flags = BBREACHED;
- bptr->type = BBTYPE_EXH;
- bptr->instack = new;
- bptr->indepth = 1;
- bptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
- STACKRESET;
-#if defined(NEW_VAR)
- GET_NEW_INDEX(new_index);
- bptr->invars = DMNEW(s4, 1);
- bptr->invars[0] = new_index;
- NEWSTACK(TYPE_ADR, STACKVAR,new_index);
-#else
- bptr->invars = DMNEW(stackptr, 1);
- bptr->invars[0] = new;
- NEWXSTACK;
-#endif
- }
-
/* stack analysis loop (until fixpoint reached) **************************/
do {
/* initialize loop over basic blocks */
- b_count = jd->new_basicblockcount;
- bptr = jd->new_basicblocks;
+ sd.bptr = jd->new_basicblocks;
superblockend = true;
- repeat = false;
- STACKRESET;
+ sd.repeat = false;
+ curstack = NULL; stackdepth = 0;
deadcode = true;
/* iterate over basic blocks *****************************************/
- while (--b_count >= 0) {
-#if defined(STACK_VERBOSE)
- printf("ANALYZING BLOCK L%03d\n", bptr->nr);
-#endif
+ for (; sd.bptr; sd.bptr = sd.bptr->next) {
- if (bptr->flags == BBDELETED) {
+ if (sd.bptr->flags == BBDELETED) {
/* This block has been deleted - do nothing. */
+
+ continue;
}
- else if (superblockend && (bptr->flags < BBREACHED)) {
+
+ if (superblockend && (sd.bptr->flags < BBREACHED)) {
/* This block has not been reached so far, and we */
/* don't fall into it, so we'll have to iterate again. */
- repeat = true;
+
+ sd.repeat = true;
+ continue;
}
- else if (bptr->flags <= BBREACHED) {
- if (superblockend) {
- /* We know that bptr->flags == BBREACHED. */
- /* This block has been reached before. */
- stackdepth = bptr->indepth;
- }
- else if (bptr->flags < BBREACHED) {
- /* This block is reached for the first time now */
- /* by falling through from the previous block. */
- COPYCURSTACK(copy);
- bptr->instack = copy;
-
-#if defined(NEW_VAR)
- bptr->invars = DMNEW(s4, stackdepth);
- for (i=stackdepth; i--; copy = copy->prev)
- bptr->invars[i] = copy->varnum;
-#else
- bptr->invars = DMNEW(stackptr, stackdepth);
- for (i=stackdepth; i--; copy = copy->prev)
- bptr->invars[i] = copy;
-#endif
- bptr->indepth = stackdepth;
+
+ if (sd.bptr->flags > BBREACHED) {
+ /* This block is already finished. */
+
+ superblockend = true;
+ continue;
+ }
+
+ if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
+ /* This block is a clone and the original has not been */
+ /* analysed, yet. Analyse it on the next iteration. */
+
+ sd.repeat = true;
+ continue;
+ }
+
+ /* This block has to be analysed now. */
+
+ /* XXX The rest of this block is still indented one level too */
+ /* much in order to avoid a giant diff by changing that. */
+
+ /* We know that sd.bptr->flags == BBREACHED. */
+ /* This block has been reached before. */
+
+ assert(sd.bptr->flags == BBREACHED);
+ stackdepth = sd.bptr->indepth;
+
+ /* find exception handlers for this block */
+
+ /* determine the active exception handlers for this block */
+ /* XXX could use a faster algorithm with sorted lists or */
+ /* something? */
+
+ original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
+
+ len = 0;
+ ex = cd->exceptiontable;
+ for (; ex != NULL; ex = ex->down) {
+ if ((ex->start <= original) && (ex->end > original)) {
+ sd.handlers[len++] = ex;
+ }
}
- else {
- /* This block has been reached before. now we are */
- /* falling into it from the previous block. */
- /* Check that stack depth is well-defined. */
- CHECK_STACK_DEPTH(bptr->indepth, stackdepth);
+ sd.handlers[len] = NULL;
+
+
+ /* reanalyse cloned block */
+
+ if (sd.bptr->original) {
+ if (!stack_reanalyse_block(&sd))
+ return false;
+ continue;
}
+ /* reset the new pointer for allocating stackslots */
+
+ sd.new = jd->new_stack;
+
+ /* create the instack of this block */
+
+ curstack = stack_create_instack(&sd);
+
+ /* initialize locals at the start of this block */
+
+ if (sd.bptr->inlocals)
+ MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
+
/* set up local variables for analyzing this block */
- curstack = bptr->instack;
deadcode = false;
superblockend = false;
- bptr->flags = BBFINISHED;
- len = bptr->icount;
- iptr = bptr->iinstr;
- b_index = bptr - jd->new_basicblocks;
+ len = sd.bptr->icount;
+ iptr = sd.bptr->iinstr;
+ b_index = sd.bptr - jd->new_basicblocks;
+
+ /* mark the block as analysed */
+
+ sd.bptr->flags = BBFINISHED;
/* reset variables for dependency checking */
- last_pei_boundary = new;
- last_dup_boundary = new;
+ coalescing_boundary = sd.new;
for( i = 0; i < cd->maxlocals; i++)
- last_store_boundary[i] = new;
+ last_store_boundary[i] = sd.new;
- /* XXX store the start of the block's stack representation */
+ /* remember the start of this block's variables */
+
+ sd.bptr->varstart = sd.vartop;
+
+#if defined(STACK_VERBOSE)
+ stack_verbose_block_enter(&sd, false);
+#endif
+
+ /* reach exception handlers for this block */
- bptr->stack = new;
+ if (!stack_reach_handlers(&sd))
+ return false;
/* iterate over ICMDs ****************************************/
while (--len >= 0) {
+
#if defined(STACK_VERBOSE)
new_show_icmd(jd, iptr, false, SHOW_PARSE); printf("\n");
for( copy = curstack; copy; copy = copy->prev ) {
- printf("%d ", copy->type);
+ printf("%2d(%d", copy->varnum, copy->type);
+ if (IS_OUTVAR(copy))
+ printf("S");
+ if (IS_PREALLOC(copy))
+ printf("A");
+ printf(") ");
}
printf("\n");
#endif
break;
case ICMD_CHECKNULL:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_check_null);
USE_S1(TYPE_ADR);
CLR_SX;
break;
case ICMD_RET:
- USE_S1_LOCAL(TYPE_ADR);
+ j = iptr->s1.varindex =
+ jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
+
+ if (sd.var[j].type != TYPE_RET) {
+ exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
+ return false;
+ }
+
CLR_SX;
- CLR_DST;
-#if !defined(NEW_VAR)
- IF_NO_INTRP( rd->locals[iptr->s1.localindex][TYPE_ADR].type = TYPE_ADR; );
+
+ iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
+#if 0
+ IF_NO_INTRP( rd->locals[iptr->s1.localindex/*XXX invalid here*/][TYPE_ADR].type = TYPE_ADR; );
#endif
superblockend = true;
break;
iptr[2].opc = ICMD_NOP;
OP1_BRANCH(TYPE_LNG);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
COUNT(count_pcmd_bra);
COUNT(count_pcmd_op);
break;
/************************** ACONST OPTIMIZATIONS **************************/
case ICMD_ACONST:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_pcmd_load);
#if SUPPORT_CONST_STORE
IF_INTRP( goto normal_ACONST; )
case ICMD_DLOAD:
case ICMD_ALOAD:
COUNT(count_load_instruction);
- i = opcode - ICMD_ILOAD;
-#if defined(NEW_VAR)
- iptr->s1.varindex =
+ i = opcode - ICMD_ILOAD; /* type */
+
+ j = iptr->s1.varindex =
jd->local_map[iptr->s1.varindex * 5 + i];
-
- LOAD(i, iptr->s1.varindex);
-#else
- IF_NO_INTRP( rd->locals[iptr->s1.localindex][i].type =
- i; )
- LOAD(i, iptr->s1.localindex);
-#endif
+ if (sd.var[j].type == TYPE_RET) {
+ exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
+ return false;
+ }
+
+ LOAD(i, j);
break;
/* pop 2 push 1 */
case ICMD_FALOAD:
case ICMD_DALOAD:
case ICMD_AALOAD:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
iptr->flags.bits |= INS_FLAG_CHECK;
COUNT(count_check_null);
COUNT(count_check_bound);
case ICMD_BALOAD:
case ICMD_CALOAD:
case ICMD_SALOAD:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
iptr->flags.bits |= INS_FLAG_CHECK;
COUNT(count_check_null);
COUNT(count_check_bound);
case ICMD_IINC:
STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
-#if defined(NEW_VAR)
- iptr->s1.varindex =
- jd->local_map[iptr->s1.varindex * 5 + i];
+ last_store_boundary[iptr->s1.varindex] = sd.new;
- last_store_boundary[iptr->s1.varindex] = new;
-#else
- last_store_boundary[iptr->s1.localindex] = new;
-#endif
+ iptr->s1.varindex =
+ jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
copy = curstack;
i = stackdepth - 1;
while (copy) {
if ((copy->varkind == LOCALVAR) &&
- (copy->varnum == iptr->s1.localindex))
+ (copy->varnum == iptr->s1.varindex))
{
- copy->varkind = TEMPVAR;
-#if defined(NEW_VAR)
- GET_NEW_INDEX(new_index);
- copy->varnum = new_index;
-#else
- copy->varnum = i;
-#endif
+ assert(IS_LOCALVAR(copy));
+ SET_TEMPVAR(copy);
}
i--;
copy = copy->prev;
}
-#if defined(NEW_VAR)
iptr->dst.varindex = iptr->s1.varindex;
-#else
- iptr->dst.localindex = iptr->s1.localindex;
-#endif
break;
/* pop 1 push 0 store */
case ICMD_FSTORE:
case ICMD_DSTORE:
case ICMD_ASTORE:
- REQUIRE_1;
+ REQUIRE(1);
i = opcode - ICMD_ISTORE; /* type */
-#if defined(NEW_VAR)
+ javaindex = iptr->dst.varindex;
j = iptr->dst.varindex =
- jd->local_map[iptr->dst.varindex * 5 + 1]:
-
-#else
- j = iptr->dst.localindex; /* index */
+ jd->local_map[javaindex * 5 + i];
- IF_NO_INTRP( rd->locals[j][i].type = i; )
-#endif
+ COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
#if defined(ENABLE_STATISTICS)
if (opt_stat) {
count_pcmd_store++;
- i = new - curstack;
+ i = sd.new - curstack;
if (i >= 20)
count_store_length[20]++;
else
(copy->varnum == j))
{
copy->varkind = TEMPVAR;
-#if defined(NEW_VAR)
- GET_NEW_INDEX(new_index);
- copy->varnum = new_index;
-#else
- copy->varnum = i;
-#endif
+ assert(IS_LOCALVAR(copy));
+ SET_TEMPVAR(copy);
}
i--;
copy = copy->prev;
/* if the variable is already coalesced, don't bother */
- if (curstack->varkind == STACKVAR
- || (curstack->varkind == LOCALVAR
- && curstack->varnum != j))
+ /* We do not need to check against OUTVAR, as invars */
+ /* are always before the coalescing boundary. */
+
+ if (curstack->varkind == LOCALVAR)
goto store_tail;
/* there is no STORE Lj while curstack is live */
- if (curstack < last_store_boundary[j])
- goto assume_conflict;
-
- /* there is no PEI while curstack is live */
-
- if (curstack < last_pei_boundary)
+ if (curstack < last_store_boundary[javaindex])
goto assume_conflict;
- /* there is no non-consuming USE while curstack is live */
+ /* curstack must be after the coalescing boundary */
- if (curstack < last_dup_boundary)
+ if (curstack < coalescing_boundary)
goto assume_conflict;
/* there is no DEF LOCALVAR(j) while curstack is live */
- copy = new; /* most recent stackslot created + 1 */
+ copy = sd.new; /* most recent stackslot created + 1 */
while (--copy > curstack) {
if (copy->varkind == LOCALVAR && copy->varnum == j)
goto assume_conflict;
}
/* coalesce the temporary variable with Lj */
-#if defined(NEW_VAR)
- assert(currstack->varkind == TEMPVAR);
- RELEASE_INDEX(curstack->varnum);
-#endif
+ assert((curstack->varkind == TEMPVAR)
+ || (curstack->varkind == UNDEFVAR));
+ assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
+ assert(!IS_OUTVAR(curstack));
+ assert(!IS_PREALLOC(curstack));
+
+ assert(curstack->creator);
+ assert(curstack->creator->dst.varindex == curstack->varnum);
+ RELEASE_INDEX(sd, curstack);
curstack->varkind = LOCALVAR;
curstack->varnum = j;
+ curstack->creator->dst.varindex = j;
goto store_tail;
/* revert the coalescing, if it has been done earlier */
if ((curstack->varkind == LOCALVAR)
&& (curstack->varnum == j))
{
- curstack->varkind = TEMPVAR;
-#if defined(NEW_VAR)
- GET_NEW_INDEX(new_index);
- curstack->varnum = new_index;
-#else
- curstack->varnum = stackdepth-1;
-#endif
+ assert(IS_LOCALVAR(curstack));
+ SET_TEMPVAR(curstack);
}
/* remember the stack boundary at this store */
store_tail:
- last_store_boundary[j] = new;
+ last_store_boundary[javaindex] = sd.new;
STORE(opcode - ICMD_ISTORE, j);
break;
/* pop 3 push 0 */
case ICMD_AASTORE:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
iptr->flags.bits |= INS_FLAG_CHECK;
COUNT(count_check_null);
COUNT(count_check_bound);
copy = curstack;
while (copy) {
+ sd.var[copy->varnum].flags |= SAVEDVAR;
+ /* in case copy->varnum is/will be a LOCALVAR */
+ /* once and set back to a non LOCALVAR */
+ /* the correct SAVEDVAR flag has to be */
+ /* remembered in copy->flags, too */
copy->flags |= SAVEDVAR;
copy = copy->prev;
}
case ICMD_LASTORE:
case ICMD_FASTORE:
case ICMD_DASTORE:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
iptr->flags.bits |= INS_FLAG_CHECK;
COUNT(count_check_null);
COUNT(count_check_bound);
case ICMD_BASTORE:
case ICMD_CASTORE:
case ICMD_SASTORE:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
iptr->flags.bits |= INS_FLAG_CHECK;
COUNT(count_check_null);
COUNT(count_check_bound);
case ICMD_POP:
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_1;
+ REQUIRE(1);
if (IS_2_WORD_TYPE(curstack->type))
goto throw_stack_category_error;
}
case ICMD_FRETURN:
case ICMD_DRETURN:
case ICMD_ARETURN:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
IF_JIT( md_return_alloc(jd, curstack); )
COUNT(count_pcmd_return);
OP1_0(opcode - ICMD_IRETURN);
break;
case ICMD_ATHROW:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_check_null);
OP1_0(TYPE_ADR);
- STACKRESET;
+ curstack = NULL; stackdepth = 0;
superblockend = true;
break;
case ICMD_PUTSTATIC:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_pcmd_mem);
INSTRUCTION_GET_FIELDREF(iptr, fmiref);
OP1_0(fmiref->parseddesc.fd->type);
case ICMD_IFNONNULL:
COUNT(count_pcmd_bra);
OP1_BRANCH(TYPE_ADR);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
break;
case ICMD_IFEQ:
OP1_BRANCH(TYPE_INT);
/* iptr->sx.val.i = 0; */
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
break;
/* pop 0 push 0 branch */
case ICMD_GOTO:
COUNT(count_pcmd_bra);
OP0_BRANCH;
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
superblockend = true;
break;
OP1_BRANCH(TYPE_INT);
table = iptr->dst.table;
- BRANCH_TARGET(*table, tbptr, copy);
+ BRANCH_TARGET(*table, tbptr);
table++;
i = iptr->sx.s23.s3.tablehigh
- iptr->sx.s23.s2.tablelow + 1;
while (--i >= 0) {
- BRANCH_TARGET(*table, tbptr, copy);
+ BRANCH_TARGET(*table, tbptr);
table++;
}
superblockend = true;
COUNT(count_pcmd_table);
OP1_BRANCH(TYPE_INT);
- BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr, copy);
+ BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
lookup = iptr->dst.lookup;
i = iptr->sx.s23.s2.lookupcount;
while (--i >= 0) {
- BRANCH_TARGET(lookup->target, tbptr, copy);
+ BRANCH_TARGET(lookup->target, tbptr);
lookup++;
}
superblockend = true;
case ICMD_MONITORENTER:
case ICMD_MONITOREXIT:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_check_null);
OP1_0(TYPE_ADR);
break;
case ICMD_IF_ICMPLE:
COUNT(count_pcmd_bra);
OP2_BRANCH(TYPE_INT, TYPE_INT);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
break;
case ICMD_IF_ACMPEQ:
case ICMD_IF_ACMPNE:
COUNT(count_pcmd_bra);
OP2_BRANCH(TYPE_ADR, TYPE_ADR);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
break;
/* pop 2 push 0 */
case ICMD_PUTFIELD:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_check_null);
COUNT(count_pcmd_mem);
INSTRUCTION_GET_FIELDREF(iptr, fmiref);
break;
case ICMD_POP2:
- REQUIRE_1;
+ REQUIRE(1);
if (!IS_2_WORD_TYPE(curstack->type)) {
/* ..., cat1 */
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_2;
+ REQUIRE(2);
if (IS_2_WORD_TYPE(curstack->prev->type))
goto throw_stack_category_error;
}
case ICMD_DUP:
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_1;
+ REQUIRE(1);
if (IS_2_WORD_TYPE(curstack->type))
goto throw_stack_category_error;
}
COUNT(count_dup_instruction);
icmd_DUP:
- USE_S1_ANY; /* XXX live through */
- /* DUP_SLOT(iptr->s1.var); */
- DUP_SLOT(curstack);
- last_dup_boundary = new - 1;
-#if defined(NEW_VAR)
- iptr->dst.varindex = curstack->varnum;
-#else
- iptr->dst.var = curstack;
-#endif
- stackdepth++;
+ src1 = curstack;
+
+ COPY_UP(src1);
+ coalescing_boundary = sd.new - 1;
break;
case ICMD_DUP2:
- REQUIRE_1;
+ REQUIRE(1);
if (IS_2_WORD_TYPE(curstack->type)) {
/* ..., cat2 */
iptr->opc = ICMD_DUP;
goto icmd_DUP;
}
else {
- REQUIRE_2;
+ REQUIRE(2);
/* ..., ????, cat1 */
#ifdef ENABLE_VERIFIER
if (opt_verify) {
goto throw_stack_category_error;
}
#endif
- iptr->dst.dupslots = DMNEW(stackptr, 2 + 2);
- iptr->dst.dupslots[0] = curstack->prev; /* XXX live through */
- iptr->dst.dupslots[1] = curstack; /* XXX live through */
-
- DUP_SLOT(iptr->dst.dupslots[0]);
- iptr->dst.dupslots[2+0] = curstack;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[2+1] = curstack;
- last_dup_boundary = new;
- stackdepth += 2;
+ src1 = curstack->prev;
+ src2 = curstack;
+
+ COPY_UP(src1); iptr++; len--;
+ COPY_UP(src2);
+
+ coalescing_boundary = sd.new;
}
break;
case ICMD_DUP_X1:
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_2;
+ REQUIRE(2);
if (IS_2_WORD_TYPE(curstack->type) ||
IS_2_WORD_TYPE(curstack->prev->type))
goto throw_stack_category_error;
#endif
icmd_DUP_X1:
- iptr->dst.dupslots = DMNEW(stackptr, 2 + 3);
- iptr->dst.dupslots[0] = curstack->prev;
- iptr->dst.dupslots[1] = curstack;
+ src1 = curstack->prev;
+ src2 = curstack;
POPANY; POPANY;
+ stackdepth -= 2;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[2+0] = curstack;
- DUP_SLOT(iptr->dst.dupslots[0]);
- iptr->dst.dupslots[2+1] = curstack;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[2+2] = curstack;
- last_dup_boundary = new;
- stackdepth++;
+ DUP_SLOT(src2); dst1 = curstack; stackdepth++;
+
+ MOVE_UP(src1); iptr++; len--;
+ MOVE_UP(src2); iptr++; len--;
+
+ COPY_DOWN(curstack, dst1);
+
+ coalescing_boundary = sd.new;
break;
case ICMD_DUP2_X1:
- REQUIRE_2;
+ REQUIRE(2);
if (IS_2_WORD_TYPE(curstack->type)) {
/* ..., ????, cat2 */
#ifdef ENABLE_VERIFIER
/* ..., ????, cat1 */
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_3;
+ REQUIRE(3);
if (IS_2_WORD_TYPE(curstack->prev->type)
|| IS_2_WORD_TYPE(curstack->prev->prev->type))
goto throw_stack_category_error;
#endif
icmd_DUP2_X1:
- iptr->dst.dupslots = DMNEW(stackptr, 3 + 5);
- iptr->dst.dupslots[0] = curstack->prev->prev;
- iptr->dst.dupslots[1] = curstack->prev;
- iptr->dst.dupslots[2] = curstack;
+ src1 = curstack->prev->prev;
+ src2 = curstack->prev;
+ src3 = curstack;
POPANY; POPANY; POPANY;
+ stackdepth -= 3;
+
+ DUP_SLOT(src2); dst1 = curstack; stackdepth++;
+ DUP_SLOT(src3); dst2 = curstack; stackdepth++;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[3+0] = curstack;
- DUP_SLOT(iptr->dst.dupslots[2]);
- iptr->dst.dupslots[3+1] = curstack;
- DUP_SLOT(iptr->dst.dupslots[0]);
- iptr->dst.dupslots[3+2] = curstack;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[3+3] = curstack;
- DUP_SLOT(iptr->dst.dupslots[2]);
- iptr->dst.dupslots[3+4] = curstack;
- last_dup_boundary = new;
- stackdepth += 2;
+ MOVE_UP(src1); iptr++; len--;
+ MOVE_UP(src2); iptr++; len--;
+ MOVE_UP(src3); iptr++; len--;
+
+ COPY_DOWN(curstack, dst2); iptr++; len--;
+ COPY_DOWN(curstack->prev, dst1);
+
+ coalescing_boundary = sd.new;
}
break;
/* pop 3 push 4 dup */
case ICMD_DUP_X2:
- REQUIRE_2;
+ REQUIRE(2);
if (IS_2_WORD_TYPE(curstack->prev->type)) {
/* ..., cat2, ???? */
#ifdef ENABLE_VERIFIER
/* ..., cat1, ???? */
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_3;
+ REQUIRE(3);
if (IS_2_WORD_TYPE(curstack->type)
|| IS_2_WORD_TYPE(curstack->prev->prev->type))
goto throw_stack_category_error;
}
#endif
icmd_DUP_X2:
- iptr->dst.dupslots = DMNEW(stackptr, 3 + 4);
- iptr->dst.dupslots[0] = curstack->prev->prev;
- iptr->dst.dupslots[1] = curstack->prev;
- iptr->dst.dupslots[2] = curstack;
+ src1 = curstack->prev->prev;
+ src2 = curstack->prev;
+ src3 = curstack;
POPANY; POPANY; POPANY;
+ stackdepth -= 3;
- DUP_SLOT(iptr->dst.dupslots[2]);
- iptr->dst.dupslots[3+0] = curstack;
- DUP_SLOT(iptr->dst.dupslots[0]);
- iptr->dst.dupslots[3+1] = curstack;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[3+2] = curstack;
- DUP_SLOT(iptr->dst.dupslots[2]);
- iptr->dst.dupslots[3+3] = curstack;
- last_dup_boundary = new;
- stackdepth++;
+ DUP_SLOT(src3); dst1 = curstack; stackdepth++;
+
+ MOVE_UP(src1); iptr++; len--;
+ MOVE_UP(src2); iptr++; len--;
+ MOVE_UP(src3); iptr++; len--;
+
+ COPY_DOWN(curstack, dst1);
+
+ coalescing_boundary = sd.new;
}
break;
case ICMD_DUP2_X2:
- REQUIRE_2;
+ REQUIRE(2);
if (IS_2_WORD_TYPE(curstack->type)) {
/* ..., ????, cat2 */
if (IS_2_WORD_TYPE(curstack->prev->type)) {
/* ..., cat1, cat2 */
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_3;
+ REQUIRE(3);
if (IS_2_WORD_TYPE(curstack->prev->prev->type))
goto throw_stack_category_error;
}
}
}
- REQUIRE_3;
+ REQUIRE(3);
/* ..., ????, ????, cat1 */
if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
/* ..., cat1, ????, cat1 */
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_4;
+ REQUIRE(4);
if (IS_2_WORD_TYPE(curstack->prev->type)
|| IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
goto throw_stack_category_error;
}
#endif
- iptr->dst.dupslots = DMNEW(stackptr, 4 + 6);
- iptr->dst.dupslots[0] = curstack->prev->prev->prev;
- iptr->dst.dupslots[1] = curstack->prev->prev;
- iptr->dst.dupslots[2] = curstack->prev;
- iptr->dst.dupslots[3] = curstack;
+
+ src1 = curstack->prev->prev->prev;
+ src2 = curstack->prev->prev;
+ src3 = curstack->prev;
+ src4 = curstack;
POPANY; POPANY; POPANY; POPANY;
+ stackdepth -= 4;
+
+ DUP_SLOT(src3); dst1 = curstack; stackdepth++;
+ DUP_SLOT(src4); dst2 = curstack; stackdepth++;
- DUP_SLOT(iptr->dst.dupslots[2]);
- iptr->dst.dupslots[4+0] = curstack;
- DUP_SLOT(iptr->dst.dupslots[3]);
- iptr->dst.dupslots[4+1] = curstack;
- DUP_SLOT(iptr->dst.dupslots[0]);
- iptr->dst.dupslots[4+2] = curstack;
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[4+3] = curstack;
- DUP_SLOT(iptr->dst.dupslots[2]);
- iptr->dst.dupslots[4+4] = curstack;
- DUP_SLOT(iptr->dst.dupslots[3]);
- iptr->dst.dupslots[4+5] = curstack;
- last_dup_boundary = new;
- stackdepth += 2;
+ MOVE_UP(src1); iptr++; len--;
+ MOVE_UP(src2); iptr++; len--;
+ MOVE_UP(src3); iptr++; len--;
+ MOVE_UP(src4); iptr++; len--;
+
+ COPY_DOWN(curstack, dst2); iptr++; len--;
+ COPY_DOWN(curstack->prev, dst1);
+
+ coalescing_boundary = sd.new;
}
break;
case ICMD_SWAP:
#ifdef ENABLE_VERIFIER
if (opt_verify) {
- REQUIRE_2;
+ REQUIRE(2);
if (IS_2_WORD_TYPE(curstack->type)
|| IS_2_WORD_TYPE(curstack->prev->type))
goto throw_stack_category_error;
}
#endif
- iptr->dst.dupslots = DMNEW(stackptr, 2 + 2);
- iptr->dst.dupslots[0] = curstack->prev;
- iptr->dst.dupslots[1] = curstack;
+
+ src1 = curstack->prev;
+ src2 = curstack;
POPANY; POPANY;
+ stackdepth -= 2;
+
+ MOVE_UP(src2); iptr++; len--;
+ MOVE_UP(src1);
- DUP_SLOT(iptr->dst.dupslots[1]);
- iptr->dst.dupslots[2+0] = curstack;
- DUP_SLOT(iptr->dst.dupslots[0]);
- iptr->dst.dupslots[2+1] = curstack;
- last_dup_boundary = new;
+ coalescing_boundary = sd.new;
break;
/* pop 2 push 1 */
case ICMD_IDIV:
case ICMD_IREM:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
#if !SUPPORT_DIVISION
bte = iptr->sx.s23.s3.bte;
md = bte->md;
copy = curstack;
while (copy) {
+ sd.var[copy->varnum].flags |= SAVEDVAR;
copy->flags |= SAVEDVAR;
copy = copy->prev;
}
case ICMD_LDIV:
case ICMD_LREM:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
#if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
bte = iptr->sx.s23.s3.bte;
md = bte->md;
copy = curstack;
while (copy) {
+ sd.var[copy->varnum].flags |= SAVEDVAR;
copy->flags |= SAVEDVAR;
copy = copy->prev;
}
iptr[1].opc = ICMD_NOP;
OP2_BRANCH(TYPE_LNG, TYPE_LNG);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
COUNT(count_pcmd_bra);
break;
iptr[1].opc = ICMD_NOP;
OP2_BRANCH(TYPE_FLT, TYPE_FLT);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
COUNT(count_pcmd_bra);
break;
iptr[1].opc = ICMD_NOP;
OP2_BRANCH(TYPE_FLT, TYPE_FLT);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
COUNT(count_pcmd_bra);
break;
iptr[1].opc = ICMD_NOP;
OP2_BRANCH(TYPE_DBL, TYPE_DBL);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
COUNT(count_pcmd_bra);
break;
iptr[1].opc = ICMD_NOP;
OP2_BRANCH(TYPE_DBL, TYPE_DBL);
- BRANCH(tbptr, copy);
+ BRANCH(tbptr);
COUNT(count_pcmd_bra);
break;
break;
case ICMD_CHECKCAST:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
if (iptr->flags.bits & INS_FLAG_ARRAY) {
/* array type cast-check */
copy = curstack;
while (copy) {
+ sd.var[copy->varnum].flags |= SAVEDVAR;
copy->flags |= SAVEDVAR;
copy = copy->prev;
}
case ICMD_INSTANCEOF:
case ICMD_ARRAYLENGTH:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
OP1_1(TYPE_ADR, TYPE_INT);
break;
case ICMD_NEWARRAY:
case ICMD_ANEWARRAY:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
OP1_1(TYPE_INT, TYPE_ADR);
break;
case ICMD_GETFIELD:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_check_null);
COUNT(count_pcmd_mem);
INSTRUCTION_GET_FIELDREF(iptr, fmiref);
/* pop 0 push 1 */
case ICMD_GETSTATIC:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
COUNT(count_pcmd_mem);
INSTRUCTION_GET_FIELDREF(iptr, fmiref);
OP0_1(fmiref->parseddesc.fd->type);
break;
case ICMD_NEW:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
OP0_1(TYPE_ADR);
break;
case ICMD_JSR:
- OP0_1(TYPE_ADR);
+ OP0_1(TYPE_RET);
- BRANCH_TARGET(iptr->sx.s23.s3.jsrtarget, tbptr, copy);
+ assert(sd.bptr->next); /* XXX exception */
+ sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
+ tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
tbptr->type = BBTYPE_SBR;
+ tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
+ if (!tbptr)
+ return false;
+
+ iptr->sx.s23.s3.jsrtarget.block = tbptr;
+
/* We need to check for overflow right here because
* the pushed value is poped afterwards */
CHECKOVERFLOW;
- /* calculate stack after return */
- POPANY;
- stackdepth--;
+ superblockend = true;
+ /* XXX should not be marked as interface, as it does not need to be */
+ /* allocated. Same for the invar of the target. */
break;
/* pop many push any */
_callhandling:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
i = md->paramcount;
/* XXX optimize for <= 2 args */
/* XXX not for ICMD_BUILTIN */
iptr->s1.argcount = stackdepth;
- iptr->sx.s23.s2.args = DMNEW(stackptr, stackdepth);
+ iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
copy = curstack;
for (i-- ; i >= 0; i--) {
- iptr->sx.s23.s2.args[i] = copy;
+ iptr->sx.s23.s2.args[i] = copy->varnum;
- /* do not change STACKVARs to ARGVAR ->
- won't help anyway */
- if (copy->varkind != STACKVAR) {
+ /* do not change STACKVARs or LOCALVARS to ARGVAR*/
+ /* -> won't help anyway */
+ if (!(IS_OUTVAR(copy) || IS_LOCALVAR(copy))) {
#if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
- /* If we pass float arguments in integer argument registers, we
- * are not allowed to precolor them here. Floats have to be moved
- * to this regs explicitly in codegen().
- * Only arguments that are passed by stack anyway can be precolored
- * (michi 2005/07/24) */
- if (!(copy->flags & SAVEDVAR) &&
- (!IS_FLT_DBL_TYPE(copy->type) || md->params[i].inmemory)) {
+ /* If we pass float arguments in integer argument registers, we
+ * are not allowed to precolor them here. Floats have to be moved
+ * to this regs explicitly in codegen().
+ * Only arguments that are passed by stack anyway can be precolored
+ * (michi 2005/07/24) */
+ if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
+ (!IS_FLT_DBL_TYPE(copy->type)
+ || md->params[i].inmemory)) {
#else
- if (!(copy->flags & SAVEDVAR)) {
+ if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
#endif
- copy->varkind = ARGVAR;
- copy->varnum = i;
+
+ SET_PREALLOC(copy);
#if defined(ENABLE_INTRP)
if (!opt_intrp) {
#endif
if (md->params[i].inmemory) {
- copy->flags = INMEMORY;
- copy->regoff = md->params[i].regoff;
+ sd.var[copy->varnum].vv.regoff =
+ md->params[i].regoff;
+ sd.var[copy->varnum].flags |=
+ INMEMORY;
}
else {
- copy->flags = 0;
if (IS_FLT_DBL_TYPE(copy->type)) {
#if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
assert(0); /* XXX is this assert ok? */
#else
- copy->regoff =
- rd->argfltregs[md->params[i].regoff];
+ sd.var[copy->varnum].vv.regoff =
+ rd->argfltregs[md->params[i].regoff];
#endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
}
else {
#if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
if (IS_2_WORD_TYPE(copy->type))
- copy->regoff = PACK_REGS(
- rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
- rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
+ sd.var[copy->varnum].vv.regoff =
+ PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
+ rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
+
else
#endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
- copy->regoff =
- rd->argintregs[md->params[i].regoff];
+ sd.var[copy->varnum].vv.regoff =
+ rd->argintregs[md->params[i].regoff];
}
}
#if defined(ENABLE_INTRP)
copy = copy->prev;
}
- /* deal with live-through stack slots "under" the arguments */
+ /* deal with live-through stack slots "under" the */
+ /* arguments */
/* XXX not for ICMD_BUILTIN */
i = md->paramcount;
while (copy) {
- iptr->sx.s23.s2.args[i++] = copy;
- copy->flags |= SAVEDVAR;
+ SET_TEMPVAR(copy);
+ iptr->sx.s23.s2.args[i++] = copy->varnum;
+ sd.var[copy->varnum].flags |= SAVEDVAR;
copy = copy->prev;
}
/* push the return value */
if (md->returntype.type != TYPE_VOID) {
- DST(md->returntype.type, stackdepth);
+ GET_NEW_VAR(sd, new_index, md->returntype.type);
+ DST(md->returntype.type, new_index);
stackdepth++;
}
break;
break;
case ICMD_MULTIANEWARRAY:
- last_pei_boundary = new;
+ coalescing_boundary = sd.new;
if (rd->argintreguse < 3)
rd->argintreguse = 3;
REQUIRE(i);
- iptr->sx.s23.s2.args = DMNEW(stackptr, i);
+ iptr->sx.s23.s2.args = DMNEW(s4, i);
#if defined(SPECIALMEMUSE)
# if defined(__DARWIN__)
- if (rd->memuse < (i + INT_ARG_CNT + LA_SIZE_IN_POINTERS))
+ if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
# else
if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
#endif
copy = curstack;
while (--i >= 0) {
- /* check INT type here? Currently typecheck does this. */
- iptr->sx.s23.s2.args[i] = copy;
- if (!(copy->flags & SAVEDVAR)
- && (copy->varkind != STACKVAR)) {
+ /* check INT type here? Currently typecheck does this. */
+ iptr->sx.s23.s2.args[i] = copy->varnum;
+ if (!(sd.var[copy->varnum].flags & SAVEDVAR)
+ && (!IS_OUTVAR(copy))
+ && (!IS_LOCALVAR(copy)) ) {
copy->varkind = ARGVAR;
- copy->varnum = i + INT_ARG_CNT;
- copy->flags |= INMEMORY;
+ sd.var[copy->varnum].flags |=
+ INMEMORY & PREALLOC;
#if defined(SPECIALMEMUSE)
# if defined(__DARWIN__)
- copy->regoff = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
+ sd.var[copy->varnum].vv.regoff = i +
+ LA_SIZE_IN_POINTERS + INT_ARG_CNT;
# else
- copy->regoff = i + LA_SIZE_IN_POINTERS + 3;
+ sd.var[copy->varnum].vv.regoff = i +
+ LA_SIZE_IN_POINTERS + 3;
# endif
#else
# if defined(__I386__)
- copy->regoff = i + 3;
+ sd.var[copy->varnum].vv.regoff = i + 3;
# elif defined(__MIPS__) && SIZEOF_VOID_P == 4
- copy->regoff = i + 2;
+ sd.var[copy->varnum].vv.regoff = i + 2;
# else
- copy->regoff = i;
+ sd.var[copy->varnum].vv.regoff = i;
# endif /* defined(__I386__) */
#endif /* defined(SPECIALMEMUSE) */
}
copy = copy->prev;
}
while (copy) {
- copy->flags |= SAVEDVAR;
+ sd.var[copy->varnum].flags |= SAVEDVAR;
copy = copy->prev;
}
while (--i >= 0) {
POPANY;
}
- DST(TYPE_ADR, stackdepth);
+ GET_NEW_VAR(sd, new_index, TYPE_ADR);
+ DST(TYPE_ADR, new_index);
stackdepth++;
break;
iptr++;
} /* while instructions */
- /* set out-stack of block */
-
- bptr->outstack = curstack;
- bptr->outdepth = stackdepth;
- bptr->outvars = DMNEW(stackptr, stackdepth);
- for (i = stackdepth, copy = curstack; i--; copy = copy->prev)
- bptr->outvars[i] = copy;
-
/* stack slots at basic block end become interfaces */
+ sd.bptr->outdepth = stackdepth;
+ sd.bptr->outvars = DMNEW(s4, stackdepth);
+
i = stackdepth - 1;
for (copy = curstack; copy; i--, copy = copy->prev) {
- if ((copy->varkind == STACKVAR) && (copy->varnum > i)) {
-#if defined(NEW_VAR)
- /* with the new vars rd->interfaces will be removed */
- /* and all in and outvars have to be STACKVARS! */
- /* in the moment i.e. SWAP with in and out vars */
- /* an unresolvable conflict */
- assert(0);
-#endif
- copy->varkind = TEMPVAR;
- } else {
- copy->varkind = STACKVAR;
- copy->varnum = i;
+ varinfo *v;
+ s4 t;
+
+ /* with the new vars rd->interfaces will be removed */
+ /* and all in and outvars have to be STACKVARS! */
+ /* in the moment i.e. SWAP with in and out vars can */
+ /* create an unresolvable conflict */
+
+ SET_TEMPVAR(copy);
+ t = copy->type;
+ if (t == TYPE_RET)
+ t = TYPE_ADR;
+
+ v = sd.var + copy->varnum;
+ v->flags |= OUTVAR;
+
+ if (jd->interface_map[i*5 + t].flags == UNUSED) {
+ /* no interface var until now for this depth and */
+ /* type */
+ jd->interface_map[i*5 + t].flags = v->flags;
}
- IF_NO_INTRP(
- rd->interfaces[i][copy->type].type = copy->type;
- rd->interfaces[i][copy->type].flags |= copy->flags;
- );
+ else {
+ jd->interface_map[i*5 + t].flags |= v->flags;
+ }
+
+ sd.bptr->outvars[i] = copy->varnum;
}
/* check if interface slots at basic block begin must be saved */
-
IF_NO_INTRP(
- i = bptr->indepth - 1;
- for (copy = bptr->instack; copy; i--, copy = copy->prev) {
- rd->interfaces[i][copy->type].type = copy->type;
- if (copy->varkind == STACKVAR) {
- if (copy->flags & SAVEDVAR)
- rd->interfaces[i][copy->type].flags |= SAVEDVAR;
+ for (i=0; i<sd.bptr->indepth; ++i) {
+ varinfo *v = sd.var + sd.bptr->invars[i];
+ s4 t;
+
+ t = v->type;
+ if (t == TYPE_RET)
+ t = TYPE_ADR;
+
+ if (jd->interface_map[i*5 + t].flags == UNUSED) {
+ /* no interface var until now for this depth and */
+ /* type */
+ jd->interface_map[i*5 + t].flags = v->flags;
+ }
+ else {
+ jd->interface_map[i*5 + t].flags |= v->flags;
}
}
);
- } /* if */
- else
- superblockend = true;
+ /* store the number of this block's variables */
+
+ sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
+
+#if defined(STACK_VERBOSE)
+ stack_verbose_block_exit(&sd, superblockend);
+#endif
+
+ /* reach the following block, if any */
+
+ if (!superblockend)
+ if (!stack_reach_next_block(&sd))
+ return false;
+
+ } /* for blocks */
+
+ } while (sd.repeat && !deadcode);
+
+ /* XXX reset TYPE_RET to TYPE_ADR */
+
+ for (i=0; i<sd.vartop; ++i) {
+ if (sd.var[i].type == TYPE_RET)
+ sd.var[i].type = TYPE_ADR;
+ }
+
+ /* XXX hack to fix up the ranges of the cloned single-block handlers */
- bptr++;
- } /* while blocks */
- } while (repeat && !deadcode);
+ ex = cd->exceptiontable;
+ for (; ex != NULL; ex = ex->down) {
+ if (ex->start == ex->end) {
+ assert(ex->end->next);
+ ex->end = ex->end->next;
+ }
+ }
/* gather statistics *****************************************************/
count_javainstr += jd->new_instructioncount;
if (jd->new_stackcount > count_upper_bound_new_stack)
count_upper_bound_new_stack = jd->new_stackcount;
- if ((new - jd->new_stack) > count_max_new_stack)
- count_max_new_stack = (new - jd->new_stack);
-
- b_count = jd->new_basicblockcount;
- bptr = jd->new_basicblocks;
- while (--b_count >= 0) {
- if (bptr->flags > BBREACHED) {
- if (bptr->indepth >= 10)
+ if ((sd.new - jd->new_stack) > count_max_new_stack)
+ count_max_new_stack = (sd.new - jd->new_stack);
+
+ sd.bptr = jd->new_basicblocks;
+ for (; sd.bptr; sd.bptr = sd.bptr->next) {
+ if (sd.bptr->flags > BBREACHED) {
+ if (sd.bptr->indepth >= 10)
count_block_stack[10]++;
else
- count_block_stack[bptr->indepth]++;
- len = bptr->icount;
+ count_block_stack[sd.bptr->indepth]++;
+ len = sd.bptr->icount;
if (len < 10)
count_block_size_distribution[len]++;
else if (len <= 12)
else
count_block_size_distribution[17]++;
}
- bptr++;
}
if (iteration_count == 1)
}
+/* functions for verbose stack analysis output ********************************/
+
+#if defined(STACK_VERBOSE)
+static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
+{
+ printf("%c", show_jit_type_letters[v->type]);
+ if (v->type == TYPE_RET)
+ printf("{L%03d}", v->vv.retaddr->nr);
+}
+
+
+static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
+{
+ assert(index >= 0 && index < sd->vartop);
+ stack_verbose_show_varinfo(sd, sd->var + index);
+}
+
+
+static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
+{
+ s4 i;
+
+ printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
+ if (bptr->invars) {
+ for (i=0; i<bptr->indepth; ++i) {
+ if (i)
+ putchar(' ');
+ stack_verbose_show_variable(sd, bptr->invars[i]);
+ }
+ }
+ else
+ putchar('-');
+ printf("] inlocals [");
+ if (bptr->inlocals) {
+ for (i=0; i<sd->localcount; ++i) {
+ if (i)
+ putchar(' ');
+ stack_verbose_show_varinfo(sd, bptr->inlocals + i);
+ }
+ }
+ else
+ putchar('-');
+ printf("] out:%d [", bptr->outdepth);
+ if (bptr->outvars) {
+ for (i=0; i<bptr->outdepth; ++i) {
+ if (i)
+ putchar(' ');
+ stack_verbose_show_variable(sd, bptr->outvars[i]);
+ }
+ }
+ else
+ putchar('-');
+ printf("]");
+
+ if (bptr->original)
+ printf(" (clone of L%03d)", bptr->original->nr);
+ else {
+ basicblock *b = bptr->copied_to;
+ if (b) {
+ printf(" (copied to ");
+ for (; b; b = b->copied_to)
+ printf("L%03d ", b->nr);
+ printf(")");
+ }
+ }
+}
+
+
+static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
+{
+ int i;
+
+ printf("======================================== STACK %sANALYSE BLOCK ",
+ (reanalyse) ? "RE-" : "");
+ stack_verbose_show_block(sd, sd->bptr);
+ printf("\n");
+
+ if (sd->handlers[0]) {
+ printf("HANDLERS: ");
+ for (i=0; sd->handlers[i]; ++i) {
+ printf("L%03d ", sd->handlers[i]->handler->nr);
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+
+static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
+{
+ printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
+ stack_verbose_show_block(sd, sd->bptr);
+ printf("\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