1 /* vm/jit/stack.h - stack analysis header
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
29 Changes: Christian Ullrich
31 $Id: stack.h 4678 2006-03-22 23:17:27Z edwin $
43 #include "vm/exceptions.h"
44 #include "vm/global.h"
45 #include "vm/jit/jit.h"
46 #include "vm/jit/reg.h"
49 /* macros used internally by analyse_stack ************************************/
51 #if defined(ENABLE_LSRA)
52 # define INC_LIFETIMES(a) { m->maxlifetimes += (a); }
54 # define INC_LIFETIMES(a)
57 /* convenient abbreviations */
58 #define CURKIND curstack->varkind
59 #define CURTYPE curstack->type
62 /*--------------------------------------------------*/
63 /* BASIC TYPE CHECKING */
64 /*--------------------------------------------------*/
66 /* XXX would be nice if we did not have to pass the expected type */
68 #if defined(ENABLE_VERIFIER)
69 #define CHECK_BASIC_TYPE(expected,actual) \
71 if ((actual) != (expected)) { \
72 expectedtype = (expected); \
73 goto throw_stack_type_error; \
76 #else /* !ENABLE_VERIFIER */
77 #define CHECK_BASIC_TYPE(expected,actual)
78 #endif /* ENABLE_VERIFIER */
80 /*--------------------------------------------------*/
81 /* STACK UNDERFLOW/OVERFLOW CHECKS */
82 /*--------------------------------------------------*/
84 /* underflow checks */
86 #if defined(ENABLE_VERIFIER)
87 #define REQUIRE(num) \
89 if (stackdepth < (num)) \
90 goto throw_stack_underflow; \
92 #else /* !ENABLE_VERIFIER */
94 #endif /* ENABLE_VERIFIER */
96 #define REQUIRE_1 REQUIRE(1)
97 #define REQUIRE_2 REQUIRE(2)
98 #define REQUIRE_3 REQUIRE(3)
99 #define REQUIRE_4 REQUIRE(4)
103 /* We allow ACONST instructions inserted as arguments to builtin
104 * functions to exceed the maximum stack depth. Maybe we should check
105 * against maximum stack depth only at block boundaries?
108 /* XXX we should find a way to remove the opc/op1 check */
109 #if defined(ENABLE_VERIFIER)
110 #define CHECKOVERFLOW \
112 if (stackdepth > m->maxstack) \
113 if (iptr[0].opc != ICMD_ACONST || iptr[0].op1 == 0) \
114 goto throw_stack_overflow; \
116 #else /* !ENABLE_VERIFIER */
117 #define CHECKOVERFLOW
118 #endif /* ENABLE_VERIFIER */
120 /*--------------------------------------------------*/
121 /* ALLOCATING STACK SLOTS */
122 /*--------------------------------------------------*/
124 #define NEWSTACK_(s,v,n) \
126 new->prev = curstack; \
129 new->varkind = (v); \
136 /* Initialize regoff, so -sia can show regnames even before reg.inc */
137 /* regs[rd->intregargnum has to be set for this */
138 /* new->regoff = (IS_FLT_DBL_TYPE(s))?-1:rd->intreg_argnum; }*/
140 #define NEWSTACK(s,v,n) { NEWSTACK_(s,v,n); INC_LIFETIMES(1); }
142 #define NEWSTACKn(s,n) NEWSTACK(s,UNDEFVAR,n)
143 #define NEWSTACK0(s) NEWSTACK(s,UNDEFVAR,0)
145 /* allocate the input stack for an exception handler */
146 #define NEWXSTACK {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
149 /*--------------------------------------------------*/
150 /* STACK MANIPULATION */
151 /*--------------------------------------------------*/
153 /* resetting to an empty operand stack */
162 /* set the output stack of the current instruction */
164 #define SETDST iptr->dst = curstack;
167 /* The following macros do NOT check stackdepth, set stackdepth or iptr->dst */
171 CHECK_BASIC_TYPE((s),curstack->type); \
172 if (curstack->varkind == UNDEFVAR) \
173 curstack->varkind = TEMPVAR; \
174 curstack = curstack->prev; \
179 if (curstack->varkind == UNDEFVAR) \
180 curstack->varkind = TEMPVAR; \
181 curstack = curstack->prev; \
184 /* Do not copy Interface Stackslots over DUPx, Swaps! */
188 (d)->type = (s)->type; \
189 if ( (s)->varkind != STACKVAR) { \
190 (d)->varkind = (s)->varkind; \
191 (d)->varnum = (s)->varnum; \
193 (d)->varkind = TEMPVAR; \
199 /*--------------------------------------------------*/
200 /* STACK OPERATIONS MODELING */
201 /*--------------------------------------------------*/
203 /* The following macros are used to model the stack manipulations of
204 * different kinds of instructions.
206 * These macros check the input stackdepth and they set the output
207 * stackdepth and the output stack of the instruction (iptr->dst).
209 * These macros do *not* check for stack overflows!
212 #define PUSHCONST(s){NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
213 #define LOAD(s,v,n) {NEWSTACK(s,v,n);SETDST;stackdepth++;}
214 #define STORE(s) {REQUIRE_1;POP(s);SETDST;stackdepth--;}
234 NEWSTACKn(s, stackdepth); \
243 NEWSTACKn(d, stackdepth - 1);\
256 #define OPTT2_0(t,b) \
270 NEWSTACKn(s, stackdepth - 2); \
275 #define OP2IAT_1(s) \
280 NEWSTACKn(s, stackdepth - 2); \
290 NEWSTACKn(s, stackdepth - 2); \
295 #define OPTT2_1(s,d) \
300 NEWSTACKn(d, stackdepth - 2); \
310 NEWSTACKn(s, stackdepth - 2); \
311 NEWSTACKn(s, stackdepth - 1); \
315 #define OP3TIA_0(s) \
339 while(--(i) >= 0) { \
345 /* Do not copy Interface Stackslots over DUP! */
346 #define DUP {REQUIRE_1; \
347 if (CURKIND != STACKVAR) { \
348 NEWSTACK(CURTYPE,CURKIND,curstack->varnum); \
350 NEWSTACK(CURTYPE, TEMPVAR, stackdepth); \
352 SETDST; stackdepth++; INC_LIFETIMES(1);}
353 #define SWAP {REQUIRE_2;COPY(curstack,new);POPANY;COPY(curstack,new+1);POPANY;\
354 new[0].prev=curstack;new[1].prev=new;\
355 curstack=new+1;new+=2;SETDST;}
356 #define DUP_X1 {REQUIRE_2;COPY(curstack,new);COPY(curstack,new+2);POPANY;\
357 COPY(curstack,new+1);POPANY;new[0].prev=curstack;\
358 new[1].prev=new;new[2].prev=new+1;\
359 curstack=new+2;new+=3;SETDST;stackdepth++; INC_LIFETIMES(3);}
360 #define DUP2_X1 {REQUIRE_3;COPY(curstack,new+1);COPY(curstack,new+4);POPANY;\
361 COPY(curstack,new);COPY(curstack,new+3);POPANY;\
362 COPY(curstack,new+2);POPANY;new[0].prev=curstack;\
363 new[1].prev=new;new[2].prev=new+1;\
364 new[3].prev=new+2;new[4].prev=new+3;\
365 curstack=new+4;new+=5;SETDST;stackdepth+=2; INC_LIFETIMES(5);}
366 #define DUP_X2 {REQUIRE_3;COPY(curstack,new);COPY(curstack,new+3);POPANY;\
367 COPY(curstack,new+2);POPANY;COPY(curstack,new+1);POPANY;\
368 new[0].prev=curstack;new[1].prev=new;\
369 new[2].prev=new+1;new[3].prev=new+2;\
370 curstack=new+3;new+=4;SETDST;stackdepth++; INC_LIFETIMES(4);}
371 #define DUP2_X2 {REQUIRE_4;COPY(curstack,new+1);COPY(curstack,new+5);POPANY;\
372 COPY(curstack,new);COPY(curstack,new+4);POPANY;\
373 COPY(curstack,new+3);POPANY;COPY(curstack,new+2);POPANY;\
374 new[0].prev=curstack;new[1].prev=new;\
375 new[2].prev=new+1;new[3].prev=new+2;\
376 new[4].prev=new+3;new[5].prev=new+4;\
377 curstack=new+5;new+=6;SETDST;stackdepth+=2; INC_LIFETIMES(6);}
380 /*--------------------------------------------------*/
381 /* MACROS FOR HANDLING BASIC BLOCKS */
382 /*--------------------------------------------------*/
384 /* COPYCURSTACK makes a copy of the current operand stack (curstack)
385 * and returns it in the variable copy.
387 * This macro is used to propagate the operand stack from one basic
388 * block to another. The destination block receives the copy as its
391 #define COPYCURSTACK(copy) {\
404 copy->varkind=STACKVAR;\
415 /* BBEND is called at the end of each basic block (after the last
416 * instruction of the block has been processed).
420 #if defined(ENABLE_INTRP)
421 #define IF_NO_INTRP(x) if (!opt_intrp) { x }
423 #define IF_NO_INTRP(x) { x }
426 #define BBEND(s,i) { \
427 (i) = stackdepth - 1; \
430 if ((copy->varkind == STACKVAR) && (copy->varnum > (i))) \
431 copy->varkind = TEMPVAR; \
433 copy->varkind = STACKVAR; \
436 IF_NO_INTRP(rd->interfaces[(i)][copy->type].type = copy->type; \
437 rd->interfaces[(i)][copy->type].flags |= copy->flags;) \
438 (i)--; copy = copy->prev; \
440 (i) = bptr->indepth - 1; \
441 copy = bptr->instack; \
444 rd->interfaces[(i)][copy->type].type = copy->type; \
445 if (copy->varkind == STACKVAR) { \
446 if (copy->flags & SAVEDVAR) \
447 rd->interfaces[(i)][copy->type].flags |= SAVEDVAR; \
450 (i)--; copy = copy->prev; \
455 /* MARKREACHED marks the destination block <b> as reached. If this
456 * block has been reached before we check if stack depth and types
457 * match. Otherwise the destination block receives a copy of the
458 * current stack as its input stack.
460 * b...destination block
464 #define MARKREACHED(b,c) \
467 (b)->bitflags |= BBFLAG_REPLACEMENT; \
468 if ((b)->flags < BBREACHED) { \
470 (b)->flags = BBREACHED; \
471 (b)->instack = (c); \
472 (b)->indepth = stackdepth; \
474 stackptr s = curstack; \
475 stackptr t = (b)->instack; \
476 if ((b)->indepth != stackdepth) { \
477 *exceptionptr = new_verifyerror(m,"Stack depth mismatch"); \
481 CHECK_BASIC_TYPE(s->type,t->type); \
489 /* function prototypes ********************************************************/
491 bool stack_init(void);
493 methodinfo *analyse_stack(methodinfo *m, codegendata *cd, registerdata *rd);
495 void stack_print(codegendata *cd, stackptr s);
496 void show_icmd_method(methodinfo *m, codegendata *cd, registerdata *rd);
497 void show_icmd_block(methodinfo *m, codegendata *cd, basicblock *bptr);
498 void show_icmd(instruction *iptr, bool deadcode);
500 /* machine dependent return value handling function */
501 void md_return_alloc(methodinfo *m, registerdata *rd, s4 return_type,
504 #endif /* _STACK_H */
508 * These are local overrides for various environment variables in Emacs.
509 * Please do not remove this and leave it at the end of the file, where
510 * Emacs will automagically detect them.
511 * ---------------------------------------------------------------------
514 * indent-tabs-mode: t
518 * vim:noexpandtab:sw=4:ts=4: