1 /* vm/jit/stack.h - stack analysis header
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Thalinger
29 $Id: stack.h 1735 2004-12-07 14:33:27Z twisti $
37 #include "vm/exceptions.h"
38 #include "vm/global.h"
39 #include "vm/jit/reg.h"
42 /**********************************************************************/
43 /* Macros used internally by analyse_stack */
44 /**********************************************************************/
47 #define COUNT(cnt) cnt++
52 /* convenient abbreviations */
53 #define CURKIND curstack->varkind
54 #define CURTYPE curstack->type
56 /*--------------------------------------------------*/
57 /* SIGNALING ERRORS */
58 /*--------------------------------------------------*/
60 #define TYPEPANIC {panic("Stack type mismatch");}
63 /*--------------------------------------------------*/
64 /* STACK UNDERFLOW/OVERFLOW CHECKS */
65 /*--------------------------------------------------*/
67 /* underflow checks */
69 #define REQUIRE(num) \
71 if (stackdepth < (num)) { \
73 new_verifyerror(m, "Unable to pop operand off an empty stack"); \
78 #define REQUIRE_1 REQUIRE(1)
79 #define REQUIRE_2 REQUIRE(2)
80 #define REQUIRE_3 REQUIRE(3)
81 #define REQUIRE_4 REQUIRE(4)
85 /* We allow ACONST instructions inserted as arguments to builtin
86 * functions to exceed the maximum stack depth. Maybe we should check
87 * against maximum stack depth only at block boundaries?
90 #define CHECKOVERFLOW \
92 if (stackdepth > m->maxstack) { \
93 if (iptr[0].opc != ICMD_ACONST || iptr[0].op1 == 0) { \
94 *exceptionptr = new_verifyerror(m, "Stack size too large"); \
101 /*--------------------------------------------------*/
102 /* ALLOCATING STACK SLOTS */
103 /*--------------------------------------------------*/
105 #define NEWSTACK(s,v,n) {new->prev=curstack;new->type=s;new->flags=0; \
106 new->varkind=v;new->varnum=n;curstack=new;new++;}
107 #define NEWSTACKn(s,n) NEWSTACK(s,UNDEFVAR,n)
108 #define NEWSTACK0(s) NEWSTACK(s,UNDEFVAR,0)
110 /* allocate the input stack for an exception handler */
111 #define NEWXSTACK {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
114 /*--------------------------------------------------*/
115 /* STACK MANIPULATION */
116 /*--------------------------------------------------*/
118 /* resetting to an empty operand stack */
119 #define STACKRESET {curstack=0;stackdepth=0;}
121 /* set the output stack of the current instruction */
122 #define SETDST {iptr->dst=curstack;}
124 /* The following macros do NOT check stackdepth, set stackdepth or iptr->dst */
125 #define POP(s) {if(s!=curstack->type){TYPEPANIC;} \
126 if(curstack->varkind==UNDEFVAR)curstack->varkind=TEMPVAR;\
127 curstack=curstack->prev;}
128 #define POPANY {if(curstack->varkind==UNDEFVAR)curstack->varkind=TEMPVAR; \
129 curstack=curstack->prev;}
130 #define COPY(s,d) {(d)->flags=0;(d)->type=(s)->type;\
131 (d)->varkind=(s)->varkind;(d)->varnum=(s)->varnum;}
134 /*--------------------------------------------------*/
135 /* STACK OPERATIONS MODELING */
136 /*--------------------------------------------------*/
138 /* The following macros are used to model the stack manipulations of
139 * different kinds of instructions.
141 * These macros check the input stackdepth and they set the output
142 * stackdepth and the output stack of the instruction (iptr->dst).
144 * These macros do *not* check for stack overflows!
147 #define PUSHCONST(s){NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
148 #define LOAD(s,v,n) {NEWSTACK(s,v,n);SETDST;stackdepth++;}
149 #define STORE(s) {REQUIRE_1;POP(s);SETDST;stackdepth--;}
150 #define OP1_0(s) {REQUIRE_1;POP(s);SETDST;stackdepth--;}
151 #define OP1_0ANY {REQUIRE_1;POPANY;SETDST;stackdepth--;}
152 #define OP0_1(s) {NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
153 #define OP1_1(s,d) {REQUIRE_1;POP(s);NEWSTACKn(d,stackdepth-1);SETDST;}
154 #define OP2_0(s) {REQUIRE_2;POP(s);POP(s);SETDST;stackdepth-=2;}
155 #define OPTT2_0(t,b){REQUIRE_2;POP(t);POP(b);SETDST;stackdepth-=2;}
156 #define OP2_1(s) {REQUIRE_2;POP(s);POP(s);NEWSTACKn(s,stackdepth-2);SETDST;stackdepth--;}
157 #define OP2IAT_1(s) {REQUIRE_2;POP(TYPE_INT);POP(TYPE_ADR);NEWSTACKn(s,stackdepth-2);\
158 SETDST;stackdepth--;}
159 #define OP2IT_1(s) {REQUIRE_2;POP(TYPE_INT);POP(s);NEWSTACKn(s,stackdepth-2);\
160 SETDST;stackdepth--;}
161 #define OPTT2_1(s,d){REQUIRE_2;POP(s);POP(s);NEWSTACKn(d,stackdepth-2);SETDST;stackdepth--;}
162 #define OP2_2(s) {REQUIRE_2;POP(s);POP(s);NEWSTACKn(s,stackdepth-2);\
163 NEWSTACKn(s,stackdepth-1);SETDST;}
164 #define OP3TIA_0(s) {REQUIRE_3;POP(s);POP(TYPE_INT);POP(TYPE_ADR);SETDST;stackdepth-=3;}
165 #define OP3_0(s) {REQUIRE_3;POP(s);POP(s);POP(s);SETDST;stackdepth-=3;}
166 #define POPMANY(i) {REQUIRE(i);stackdepth-=i;while(--i>=0){POPANY;}SETDST;}
167 #define DUP {REQUIRE_1;NEWSTACK(CURTYPE,CURKIND,curstack->varnum);SETDST; \
169 #define SWAP {REQUIRE_2;COPY(curstack,new);POPANY;COPY(curstack,new+1);POPANY;\
170 new[0].prev=curstack;new[1].prev=new;\
171 curstack=new+1;new+=2;SETDST;}
172 #define DUP_X1 {REQUIRE_2;COPY(curstack,new);COPY(curstack,new+2);POPANY;\
173 COPY(curstack,new+1);POPANY;new[0].prev=curstack;\
174 new[1].prev=new;new[2].prev=new+1;\
175 curstack=new+2;new+=3;SETDST;stackdepth++;}
176 #define DUP2_X1 {REQUIRE_3;COPY(curstack,new+1);COPY(curstack,new+4);POPANY;\
177 COPY(curstack,new);COPY(curstack,new+3);POPANY;\
178 COPY(curstack,new+2);POPANY;new[0].prev=curstack;\
179 new[1].prev=new;new[2].prev=new+1;\
180 new[3].prev=new+2;new[4].prev=new+3;\
181 curstack=new+4;new+=5;SETDST;stackdepth+=2;}
182 #define DUP_X2 {REQUIRE_3;COPY(curstack,new);COPY(curstack,new+3);POPANY;\
183 COPY(curstack,new+2);POPANY;COPY(curstack,new+1);POPANY;\
184 new[0].prev=curstack;new[1].prev=new;\
185 new[2].prev=new+1;new[3].prev=new+2;\
186 curstack=new+3;new+=4;SETDST;stackdepth++;}
187 #define DUP2_X2 {REQUIRE_4;COPY(curstack,new+1);COPY(curstack,new+5);POPANY;\
188 COPY(curstack,new);COPY(curstack,new+4);POPANY;\
189 COPY(curstack,new+3);POPANY;COPY(curstack,new+2);POPANY;\
190 new[0].prev=curstack;new[1].prev=new;\
191 new[2].prev=new+1;new[3].prev=new+2;\
192 new[4].prev=new+3;new[5].prev=new+4;\
193 curstack=new+5;new+=6;SETDST;stackdepth+=2;}
196 /*--------------------------------------------------*/
197 /* MACROS FOR HANDLING BASIC BLOCKS */
198 /*--------------------------------------------------*/
200 /* COPYCURSTACK makes a copy of the current operand stack (curstack)
201 * and returns it in the variable copy.
203 * This macro is used to propagate the operand stack from one basic
204 * block to another. The destination block receives the copy as its
207 #define COPYCURSTACK(copy) {\
220 copy->varkind=STACKVAR;\
231 /* BBEND is called at the end of each basic block (after the last
232 * instruction of the block has been processed).
235 #define BBEND(s,i){ \
236 i = stackdepth - 1; \
239 if ((copy->varkind == STACKVAR) && (copy->varnum > i)) \
240 copy->varkind = TEMPVAR; \
242 copy->varkind = STACKVAR; \
245 rd->interfaces[i][copy->type].type = copy->type; \
246 rd->interfaces[i][copy->type].flags |= copy->flags; \
247 i--; copy = copy->prev; \
249 i = bptr->indepth - 1; \
250 copy = bptr->instack; \
252 rd->interfaces[i][copy->type].type = copy->type; \
253 if (copy->varkind == STACKVAR) { \
254 if (copy->flags & SAVEDVAR) \
255 rd->interfaces[i][copy->type].flags |= SAVEDVAR; \
257 i--; copy = copy->prev; \
262 /* MARKREACHED marks the destination block <b> as reached. If this
263 * block has been reached before we check if stack depth and types
264 * match. Otherwise the destination block receives a copy of the
265 * current stack as its input stack.
267 * b...destination block
271 #define MARKREACHED(b,c) \
273 if ((b)->flags < 0) { \
276 (b)->instack = (c); \
277 (b)->indepth = stackdepth; \
279 stackptr s = curstack; \
280 stackptr t = (b)->instack; \
281 if ((b)->indepth != stackdepth) { \
282 show_icmd_method(m, cd, rd); \
283 panic("Stack depth mismatch"); \
286 if (s->type != t->type) \
295 /* function prototypes */
297 methodinfo *analyse_stack(methodinfo *m, codegendata *cd, registerdata *rd);
299 void icmd_print_stack(codegendata *cd, stackptr s);
300 char *icmd_builtin_name(functionptr bptr);
301 void show_icmd_method(methodinfo *m, codegendata *cd, registerdata *rd);
302 void show_icmd_block(methodinfo *m, codegendata *cd, basicblock *bptr);
303 void show_icmd(instruction *iptr, bool deadcode);
305 #endif /* _STACK_H */
309 * These are local overrides for various environment variables in Emacs.
310 * Please do not remove this and leave it at the end of the file, where
311 * Emacs will automagically detect them.
312 * ---------------------------------------------------------------------
315 * indent-tabs-mode: t