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 4676 2006-03-22 18:32:47Z 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 /* SIGNALING ERRORS */
64 /*--------------------------------------------------*/
66 #define TYPE_VERIFYERROR(t) \
68 exceptions_throw_verifyerror_for_stack(m,t); \
73 /*--------------------------------------------------*/
74 /* STACK UNDERFLOW/OVERFLOW CHECKS */
75 /*--------------------------------------------------*/
77 /* underflow checks */
79 #if defined(ENABLE_VERIFIER)
80 #define REQUIRE(num) \
82 if (stackdepth < (num)) \
83 goto throw_stack_underflow; \
85 #else /* !ENABLE_VERIFIER */
87 #endif /* ENABLE_VERIFIER */
89 #define REQUIRE_1 REQUIRE(1)
90 #define REQUIRE_2 REQUIRE(2)
91 #define REQUIRE_3 REQUIRE(3)
92 #define REQUIRE_4 REQUIRE(4)
96 /* We allow ACONST instructions inserted as arguments to builtin
97 * functions to exceed the maximum stack depth. Maybe we should check
98 * against maximum stack depth only at block boundaries?
101 /* XXX we should find a way to remove the opc/op1 check */
102 #if defined(ENABLE_VERIFIER)
103 #define CHECKOVERFLOW \
105 if (stackdepth > m->maxstack) \
106 if (iptr[0].opc != ICMD_ACONST || iptr[0].op1 == 0) \
107 goto throw_stack_overflow; \
109 #else /* !ENABLE_VERIFIER */
110 #define CHECKOVERFLOW
111 #endif /* ENABLE_VERIFIER */
113 /*--------------------------------------------------*/
114 /* ALLOCATING STACK SLOTS */
115 /*--------------------------------------------------*/
117 #define NEWSTACK_(s,v,n) \
119 new->prev = curstack; \
122 new->varkind = (v); \
129 /* Initialize regoff, so -sia can show regnames even before reg.inc */
130 /* regs[rd->intregargnum has to be set for this */
131 /* new->regoff = (IS_FLT_DBL_TYPE(s))?-1:rd->intreg_argnum; }*/
133 #define NEWSTACK(s,v,n) { NEWSTACK_(s,v,n); INC_LIFETIMES(1); }
135 #define NEWSTACKn(s,n) NEWSTACK(s,UNDEFVAR,n)
136 #define NEWSTACK0(s) NEWSTACK(s,UNDEFVAR,0)
138 /* allocate the input stack for an exception handler */
139 #define NEWXSTACK {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
142 /*--------------------------------------------------*/
143 /* STACK MANIPULATION */
144 /*--------------------------------------------------*/
146 /* resetting to an empty operand stack */
155 /* set the output stack of the current instruction */
157 #define SETDST iptr->dst = curstack;
160 /* The following macros do NOT check stackdepth, set stackdepth or iptr->dst */
164 if ((s) != curstack->type) { \
165 TYPE_VERIFYERROR((s)); \
167 if (curstack->varkind == UNDEFVAR) \
168 curstack->varkind = TEMPVAR; \
169 curstack = curstack->prev; \
174 if (curstack->varkind == UNDEFVAR) \
175 curstack->varkind = TEMPVAR; \
176 curstack = curstack->prev; \
179 /* Do not copy Interface Stackslots over DUPx, Swaps! */
183 (d)->type = (s)->type; \
184 if ( (s)->varkind != STACKVAR) { \
185 (d)->varkind = (s)->varkind; \
186 (d)->varnum = (s)->varnum; \
188 (d)->varkind = TEMPVAR; \
194 /*--------------------------------------------------*/
195 /* STACK OPERATIONS MODELING */
196 /*--------------------------------------------------*/
198 /* The following macros are used to model the stack manipulations of
199 * different kinds of instructions.
201 * These macros check the input stackdepth and they set the output
202 * stackdepth and the output stack of the instruction (iptr->dst).
204 * These macros do *not* check for stack overflows!
207 #define PUSHCONST(s){NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
208 #define LOAD(s,v,n) {NEWSTACK(s,v,n);SETDST;stackdepth++;}
209 #define STORE(s) {REQUIRE_1;POP(s);SETDST;stackdepth--;}
229 NEWSTACKn(s, stackdepth); \
238 NEWSTACKn(d, stackdepth - 1);\
251 #define OPTT2_0(t,b) \
265 NEWSTACKn(s, stackdepth - 2); \
270 #define OP2IAT_1(s) \
275 NEWSTACKn(s, stackdepth - 2); \
285 NEWSTACKn(s, stackdepth - 2); \
290 #define OPTT2_1(s,d) \
295 NEWSTACKn(d, stackdepth - 2); \
305 NEWSTACKn(s, stackdepth - 2); \
306 NEWSTACKn(s, stackdepth - 1); \
310 #define OP3TIA_0(s) \
334 while(--(i) >= 0) { \
340 /* Do not copy Interface Stackslots over DUP! */
341 #define DUP {REQUIRE_1; \
342 if (CURKIND != STACKVAR) { \
343 NEWSTACK(CURTYPE,CURKIND,curstack->varnum); \
345 NEWSTACK(CURTYPE, TEMPVAR, stackdepth); \
347 SETDST; stackdepth++; INC_LIFETIMES(1);}
348 #define SWAP {REQUIRE_2;COPY(curstack,new);POPANY;COPY(curstack,new+1);POPANY;\
349 new[0].prev=curstack;new[1].prev=new;\
350 curstack=new+1;new+=2;SETDST;}
351 #define DUP_X1 {REQUIRE_2;COPY(curstack,new);COPY(curstack,new+2);POPANY;\
352 COPY(curstack,new+1);POPANY;new[0].prev=curstack;\
353 new[1].prev=new;new[2].prev=new+1;\
354 curstack=new+2;new+=3;SETDST;stackdepth++; INC_LIFETIMES(3);}
355 #define DUP2_X1 {REQUIRE_3;COPY(curstack,new+1);COPY(curstack,new+4);POPANY;\
356 COPY(curstack,new);COPY(curstack,new+3);POPANY;\
357 COPY(curstack,new+2);POPANY;new[0].prev=curstack;\
358 new[1].prev=new;new[2].prev=new+1;\
359 new[3].prev=new+2;new[4].prev=new+3;\
360 curstack=new+4;new+=5;SETDST;stackdepth+=2; INC_LIFETIMES(5);}
361 #define DUP_X2 {REQUIRE_3;COPY(curstack,new);COPY(curstack,new+3);POPANY;\
362 COPY(curstack,new+2);POPANY;COPY(curstack,new+1);POPANY;\
363 new[0].prev=curstack;new[1].prev=new;\
364 new[2].prev=new+1;new[3].prev=new+2;\
365 curstack=new+3;new+=4;SETDST;stackdepth++; INC_LIFETIMES(4);}
366 #define DUP2_X2 {REQUIRE_4;COPY(curstack,new+1);COPY(curstack,new+5);POPANY;\
367 COPY(curstack,new);COPY(curstack,new+4);POPANY;\
368 COPY(curstack,new+3);POPANY;COPY(curstack,new+2);POPANY;\
369 new[0].prev=curstack;new[1].prev=new;\
370 new[2].prev=new+1;new[3].prev=new+2;\
371 new[4].prev=new+3;new[5].prev=new+4;\
372 curstack=new+5;new+=6;SETDST;stackdepth+=2; INC_LIFETIMES(6);}
375 /*--------------------------------------------------*/
376 /* MACROS FOR HANDLING BASIC BLOCKS */
377 /*--------------------------------------------------*/
379 /* COPYCURSTACK makes a copy of the current operand stack (curstack)
380 * and returns it in the variable copy.
382 * This macro is used to propagate the operand stack from one basic
383 * block to another. The destination block receives the copy as its
386 #define COPYCURSTACK(copy) {\
399 copy->varkind=STACKVAR;\
410 /* BBEND is called at the end of each basic block (after the last
411 * instruction of the block has been processed).
415 #if defined(ENABLE_INTRP)
416 #define IF_NO_INTRP(x) if (!opt_intrp) { x }
418 #define IF_NO_INTRP(x) { x }
421 #define BBEND(s,i) { \
422 (i) = stackdepth - 1; \
425 if ((copy->varkind == STACKVAR) && (copy->varnum > (i))) \
426 copy->varkind = TEMPVAR; \
428 copy->varkind = STACKVAR; \
431 IF_NO_INTRP(rd->interfaces[(i)][copy->type].type = copy->type; \
432 rd->interfaces[(i)][copy->type].flags |= copy->flags;) \
433 (i)--; copy = copy->prev; \
435 (i) = bptr->indepth - 1; \
436 copy = bptr->instack; \
439 rd->interfaces[(i)][copy->type].type = copy->type; \
440 if (copy->varkind == STACKVAR) { \
441 if (copy->flags & SAVEDVAR) \
442 rd->interfaces[(i)][copy->type].flags |= SAVEDVAR; \
445 (i)--; copy = copy->prev; \
450 /* MARKREACHED marks the destination block <b> as reached. If this
451 * block has been reached before we check if stack depth and types
452 * match. Otherwise the destination block receives a copy of the
453 * current stack as its input stack.
455 * b...destination block
459 #define MARKREACHED(b,c) \
462 (b)->bitflags |= BBFLAG_REPLACEMENT; \
463 if ((b)->flags < BBREACHED) { \
465 (b)->flags = BBREACHED; \
466 (b)->instack = (c); \
467 (b)->indepth = stackdepth; \
469 stackptr s = curstack; \
470 stackptr t = (b)->instack; \
471 if ((b)->indepth != stackdepth) { \
472 *exceptionptr = new_verifyerror(m,"Stack depth mismatch"); \
476 if (s->type != t->type) \
477 TYPE_VERIFYERROR(t->type); \
485 /* function prototypes ********************************************************/
487 bool stack_init(void);
489 methodinfo *analyse_stack(methodinfo *m, codegendata *cd, registerdata *rd);
491 void stack_print(codegendata *cd, stackptr s);
492 void show_icmd_method(methodinfo *m, codegendata *cd, registerdata *rd);
493 void show_icmd_block(methodinfo *m, codegendata *cd, basicblock *bptr);
494 void show_icmd(instruction *iptr, bool deadcode);
496 /* machine dependent return value handling function */
497 void md_return_alloc(methodinfo *m, registerdata *rd, s4 return_type,
500 #endif /* _STACK_H */
504 * These are local overrides for various environment variables in Emacs.
505 * Please do not remove this and leave it at the end of the file, where
506 * Emacs will automagically detect them.
507 * ---------------------------------------------------------------------
510 * indent-tabs-mode: t
514 * vim:noexpandtab:sw=4:ts=4: