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 5365 2006-09-06 10:56:43Z 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 /* convenient abbreviations */
52 #define CURKIND curstack->varkind
53 #define CURTYPE curstack->type
56 /*--------------------------------------------------*/
57 /* STACK DEPTH CHECKING */
58 /*--------------------------------------------------*/
60 #if defined(ENABLE_VERIFIER)
61 #define CHECK_STACK_DEPTH(depthA,depthB) \
63 if ((depthA) != (depthB)) \
64 goto throw_stack_depth_error; \
66 #else /* !ENABLE_VERIFIER */
67 #define CHECK_STACK_DEPTH(depthA,depthB)
68 #endif /* ENABLE_VERIFIER */
71 /*--------------------------------------------------*/
72 /* BASIC TYPE CHECKING */
73 /*--------------------------------------------------*/
75 /* XXX would be nice if we did not have to pass the expected type */
77 #if defined(ENABLE_VERIFIER)
78 #define CHECK_BASIC_TYPE(expected,actual) \
80 if ((actual) != (expected)) { \
81 expectedtype = (expected); \
82 goto throw_stack_type_error; \
85 #else /* !ENABLE_VERIFIER */
86 #define CHECK_BASIC_TYPE(expected,actual)
87 #endif /* ENABLE_VERIFIER */
89 /*--------------------------------------------------*/
90 /* STACK UNDERFLOW/OVERFLOW CHECKS */
91 /*--------------------------------------------------*/
93 /* underflow checks */
95 #if defined(ENABLE_VERIFIER)
96 #define REQUIRE(num) \
98 if (stackdepth < (num)) \
99 goto throw_stack_underflow; \
101 #else /* !ENABLE_VERIFIER */
103 #endif /* ENABLE_VERIFIER */
105 #define REQUIRE_1 REQUIRE(1)
106 #define REQUIRE_2 REQUIRE(2)
107 #define REQUIRE_3 REQUIRE(3)
108 #define REQUIRE_4 REQUIRE(4)
112 /* We allow ACONST instructions inserted as arguments to builtin
113 * functions to exceed the maximum stack depth. Maybe we should check
114 * against maximum stack depth only at block boundaries?
117 /* XXX we should find a way to remove the opc/op1 check */
118 #if defined(ENABLE_VERIFIER)
119 #define CHECKOVERFLOW \
121 if (stackdepth > m->maxstack) \
122 if ((iptr->opc != ICMD_ACONST) || !(iptr->flags.bits & INS_FLAG_NOCHECK)) \
123 goto throw_stack_overflow; \
125 #else /* !ENABLE_VERIFIER */
126 #define CHECKOVERFLOW
127 #endif /* ENABLE_VERIFIER */
129 /*--------------------------------------------------*/
130 /* ALLOCATING STACK SLOTS */
131 /*--------------------------------------------------*/
133 #define NEWSTACK(s,v,n) \
135 new->prev = curstack; \
138 new->varkind = (v); \
144 /* Initialize regoff, so -sia can show regnames even before reg.inc */
145 /* regs[rd->intregargnum] has to be set for this */
146 /* new->regoff = (IS_FLT_DBL_TYPE(s))?-1:rd->intreg_argnum; } */
148 #define NEWSTACKn(s,n) NEWSTACK(s,UNDEFVAR,n)
149 #define NEWSTACK0(s) NEWSTACK(s,UNDEFVAR,0)
151 /* allocate the input stack for an exception handler */
152 #define NEWXSTACK {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
155 /*--------------------------------------------------*/
156 /* STACK MANIPULATION */
157 /*--------------------------------------------------*/
159 /* resetting to an empty operand stack */
168 /* set the output stack of the current instruction */
170 #define SETDST iptr->dst = curstack;
173 /* The following macros do NOT check stackdepth, set stackdepth or iptr->dst */
177 CHECK_BASIC_TYPE((s),curstack->type); \
178 if (curstack->varkind == UNDEFVAR) \
179 curstack->varkind = TEMPVAR; \
180 curstack = curstack->prev; \
185 if (curstack->varkind == UNDEFVAR) \
186 curstack->varkind = TEMPVAR; \
187 curstack = curstack->prev; \
190 /* Do not copy Interface Stackslots over DUPx, Swaps! */
194 (d)->type = (s)->type; \
195 if ( (s)->varkind != STACKVAR) { \
196 (d)->varkind = (s)->varkind; \
197 (d)->varnum = (s)->varnum; \
199 (d)->varkind = TEMPVAR; \
205 /*--------------------------------------------------*/
206 /* STACK OPERATIONS MODELING */
207 /*--------------------------------------------------*/
209 /* The following macros are used to model the stack manipulations of
210 * different kinds of instructions.
212 * These macros check the input stackdepth and they set the output
213 * stackdepth and the output stack of the instruction (iptr->dst).
215 * These macros do *not* check for stack overflows!
218 #define PUSHCONST(s){NEWSTACKn(s,stackdepth);SETDST;stackdepth++;}
219 #define LOAD(s,v,n) {NEWSTACK(s,v,n);SETDST;stackdepth++;}
220 #define STORE(s) {REQUIRE_1;POP(s);SETDST;stackdepth--;}
240 NEWSTACKn(s, stackdepth); \
249 NEWSTACKn(d, stackdepth - 1);\
262 #define OPTT2_0(t,b) \
276 NEWSTACKn(s, stackdepth - 2); \
281 #define OP2IAT_1(s) \
286 NEWSTACKn(s, stackdepth - 2); \
296 NEWSTACKn(s, stackdepth - 2); \
301 #define OPTT2_1(s,d) \
306 NEWSTACKn(d, stackdepth - 2); \
316 NEWSTACKn(s, stackdepth - 2); \
317 NEWSTACKn(s, stackdepth - 1); \
321 #define OP3TIA_0(s) \
345 while(--(i) >= 0) { \
351 /* Do not copy Interface Stackslots over DUP! */
352 #define DUP {REQUIRE_1; \
353 if (CURKIND != STACKVAR) { \
354 NEWSTACK(CURTYPE,CURKIND,curstack->varnum); \
356 NEWSTACK(CURTYPE, TEMPVAR, stackdepth); \
358 SETDST; stackdepth++;}
359 #define SWAP {REQUIRE_2;COPY(curstack,new);POPANY;COPY(curstack,new+1);POPANY;\
360 new[0].prev=curstack;new[1].prev=new;\
361 curstack=new+1;new+=2;SETDST;}
362 #define DUP_X1 {REQUIRE_2;COPY(curstack,new);COPY(curstack,new+2);POPANY;\
363 COPY(curstack,new+1);POPANY;new[0].prev=curstack;\
364 new[1].prev=new;new[2].prev=new+1;\
365 curstack=new+2;new+=3;SETDST;stackdepth++;}
366 #define DUP2_X1 {REQUIRE_3;COPY(curstack,new+1);COPY(curstack,new+4);POPANY;\
367 COPY(curstack,new);COPY(curstack,new+3);POPANY;\
368 COPY(curstack,new+2);POPANY;new[0].prev=curstack;\
369 new[1].prev=new;new[2].prev=new+1;\
370 new[3].prev=new+2;new[4].prev=new+3;\
371 curstack=new+4;new+=5;SETDST;stackdepth+=2;}
372 #define DUP_X2 {REQUIRE_3;COPY(curstack,new);COPY(curstack,new+3);POPANY;\
373 COPY(curstack,new+2);POPANY;COPY(curstack,new+1);POPANY;\
374 new[0].prev=curstack;new[1].prev=new;\
375 new[2].prev=new+1;new[3].prev=new+2;\
376 curstack=new+3;new+=4;SETDST;stackdepth++;}
377 #define DUP2_X2 {REQUIRE_4;COPY(curstack,new+1);COPY(curstack,new+5);POPANY;\
378 COPY(curstack,new);COPY(curstack,new+4);POPANY;\
379 COPY(curstack,new+3);POPANY;COPY(curstack,new+2);POPANY;\
380 new[0].prev=curstack;new[1].prev=new;\
381 new[2].prev=new+1;new[3].prev=new+2;\
382 new[4].prev=new+3;new[5].prev=new+4;\
383 curstack=new+5;new+=6;SETDST;stackdepth+=2;}
386 /*--------------------------------------------------*/
387 /* MACROS FOR HANDLING BASIC BLOCKS */
388 /*--------------------------------------------------*/
390 /* COPYCURSTACK makes a copy of the current operand stack (curstack)
391 * and returns it in the variable copy.
393 * This macro is used to propagate the operand stack from one basic
394 * block to another. The destination block receives the copy as its
397 #define COPYCURSTACK(copy) {\
410 copy->varkind=STACKVAR;\
421 /* MARKREACHED marks the destination block <b> as reached. If this
422 * block has been reached before we check if stack depth and types
423 * match. Otherwise the destination block receives a copy of the
424 * current stack as its input stack.
426 * b...destination block
430 /* XXX this macro is much too big! */
432 #define MARKREACHED(b,c) \
435 (b)->bitflags |= BBFLAG_REPLACEMENT; \
436 if ((b)->flags < BBREACHED) { \
439 (b)->flags = BBREACHED; \
440 (b)->instack = (c); \
441 (b)->indepth = stackdepth; \
442 (b)->invars = DMNEW(stackptr, stackdepth); \
443 for (locali = stackdepth; locali--; (c) = (c)->prev) \
444 (b)->invars[locali] = (c); \
446 stackptr s = curstack; \
447 stackptr t = (b)->instack; \
448 CHECK_STACK_DEPTH((b)->indepth, stackdepth); \
450 CHECK_BASIC_TYPE(s->type,t->type); \
458 /* external macros ************************************************************/
460 #define BLOCK_OF(index) \
461 (jd->new_basicblocks + jd->new_basicblockindex[index])
464 /* function prototypes ********************************************************/
466 bool stack_init(void);
468 bool new_stack_analyse(jitdata *jd);
470 #endif /* _STACK_H */
474 * These are local overrides for various environment variables in Emacs.
475 * Please do not remove this and leave it at the end of the file, where
476 * Emacs will automagically detect them.
477 * ---------------------------------------------------------------------
480 * indent-tabs-mode: t
484 * vim:noexpandtab:sw=4:ts=4: