Unified variables changes for common/i386.
[cacao.git] / src / vm / jit / stack.h
1 /* vm/jit/stack.h - stack analysis header
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes: Christian Ullrich
30
31    $Id: stack.h 5404 2006-09-07 13:29:05Z christian $
32
33 */
34
35
36 #ifndef _STACK_H
37 #define _STACK_H
38
39 #include "config.h"
40
41 #include "vm/types.h"
42
43 #include "vm/exceptions.h"
44 #include "vm/global.h"
45 #include "vm/jit/jit.h"
46 #include "vm/jit/reg.h"
47
48
49 /* macros used internally by analyse_stack ************************************/
50
51 /* convenient abbreviations */
52 #define CURKIND    curstack->varkind
53 #define CURTYPE    curstack->type
54
55
56 /*--------------------------------------------------*/
57 /* STACK DEPTH CHECKING                             */
58 /*--------------------------------------------------*/
59
60 #if defined(ENABLE_VERIFIER)
61 #define CHECK_STACK_DEPTH(depthA,depthB) \
62         do { \
63                 if ((depthA) != (depthB)) \
64                         goto throw_stack_depth_error; \
65         } while (0)
66 #else /* !ENABLE_VERIFIER */
67 #define CHECK_STACK_DEPTH(depthA,depthB)
68 #endif /* ENABLE_VERIFIER */
69
70
71 /*--------------------------------------------------*/
72 /* BASIC TYPE CHECKING                              */
73 /*--------------------------------------------------*/
74
75 /* XXX would be nice if we did not have to pass the expected type */
76
77 #if defined(ENABLE_VERIFIER)
78 #define CHECK_BASIC_TYPE(expected,actual) \
79         do { \
80                 if ((actual) != (expected)) { \
81                         expectedtype = (expected); \
82                         goto throw_stack_type_error; \
83                 } \
84         } while (0)
85 #else /* !ENABLE_VERIFIER */
86 #define CHECK_BASIC_TYPE(expected,actual)
87 #endif /* ENABLE_VERIFIER */
88
89 /*--------------------------------------------------*/
90 /* STACK UNDERFLOW/OVERFLOW CHECKS                  */
91 /*--------------------------------------------------*/
92
93 /* underflow checks */
94
95 #if defined(ENABLE_VERIFIER)
96 #define REQUIRE(num) \
97     do { \
98         if (stackdepth < (num)) \
99                         goto throw_stack_underflow; \
100         } while (0)
101 #else /* !ENABLE_VERIFIER */
102 #define REQUIRE(num)
103 #endif /* ENABLE_VERIFIER */
104
105 #define REQUIRE_1     REQUIRE(1)
106 #define REQUIRE_2     REQUIRE(2)
107 #define REQUIRE_3     REQUIRE(3)
108 #define REQUIRE_4     REQUIRE(4)
109
110
111 /* overflow check */
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?
115  */
116
117 /* XXX we should find a way to remove the opc/op1 check */
118 #if defined(ENABLE_VERIFIER)
119 #define CHECKOVERFLOW \
120         do { \
121                 if (stackdepth > m->maxstack) \
122                         if ((iptr->opc != ICMD_ACONST) || INSTRUCTION_MUST_CHECK(iptr)) \
123                                 goto throw_stack_overflow; \
124         } while(0)
125 #else /* !ENABLE_VERIFIER */
126 #define CHECKOVERFLOW
127 #endif /* ENABLE_VERIFIER */
128
129 /*--------------------------------------------------*/
130 /* ALLOCATING STACK SLOTS                           */
131 /*--------------------------------------------------*/
132
133 #define NEWSTACK(s,v,n) \
134     do { \
135         new->prev = curstack; \
136         new->type = (s); \
137         new->flags = 0; \
138         new->varkind = (v); \
139         new->varnum = (n); \
140         curstack = new; \
141                 jd->var[(n)].type = (s); \
142                 jd->var[(n)].flags = 0;  \
143         new++; \
144     } while (0)
145
146 /* Initialize regoff, so -sia can show regnames even before reg.inc */
147 /* regs[rd->intregargnum] has to be set for this                    */
148 /* new->regoff = (IS_FLT_DBL_TYPE(s))?-1:rd->intreg_argnum; }       */
149
150 #define NEWSTACKn(s,n)  NEWSTACK(s,UNDEFVAR,n)
151 #define NEWSTACK0(s)    NEWSTACK(s,UNDEFVAR,0)
152
153 /* allocate the input stack for an exception handler */
154 #define NEWXSTACK   {NEWSTACK(TYPE_ADR,STACKVAR,0);curstack=0;}
155
156 /*--------------------------------------------------*/
157 /* STACK MANIPULATION                               */
158 /*--------------------------------------------------*/
159
160 /* resetting to an empty operand stack */
161
162 #define STACKRESET \
163     do { \
164         curstack = 0; \
165         stackdepth = 0; \
166     } while (0)
167
168
169 /* set the output stack of the current instruction */
170
171 #define SETDST    iptr->dst = curstack;
172
173
174 /* The following macros do NOT check stackdepth, set stackdepth or iptr->dst */
175
176 #define POP(s) \
177     do { \
178                 CHECK_BASIC_TYPE((s),curstack->type); \
179         if (curstack->varkind == UNDEFVAR) \
180             curstack->varkind = TEMPVAR; \
181         curstack = curstack->prev; \
182     } while (0)
183
184 #define POPANY \
185     do { \
186         if (curstack->varkind == UNDEFVAR) \
187             curstack->varkind = TEMPVAR; \
188         curstack = curstack->prev; \
189     } while (0)
190
191 /* Do not copy Interface Stackslots over DUPx, Swaps! */
192 #define COPY(s,d) \
193     do { \
194         (d)->flags = 0; \
195         (d)->type = (s)->type; \
196                 if ( (s)->varkind != STACKVAR) {                \
197                         (d)->varkind = (s)->varkind; \
198                         (d)->varnum = (s)->varnum;       \
199                 } else { \
200                         (d)->varkind = TEMPVAR; \
201                         (d)->varnum = 0; \
202                 } \
203     } while (0)
204
205
206 /*--------------------------------------------------*/
207 /* MACROS FOR HANDLING BASIC BLOCKS                 */
208 /*--------------------------------------------------*/
209
210 /* COPYCURSTACK makes a copy of the current operand stack (curstack)
211  * and returns it in the variable copy.
212  *
213  * This macro is used to propagate the operand stack from one basic
214  * block to another. The destination block receives the copy as its
215  * input stack.
216  */
217 #if defined(NEW_VAR)
218 # define COPYCURSTACK(copy) {\
219         stackptr s;\
220         if(curstack){\
221                 s=curstack;\
222                 new+=stackdepth;\
223                 copy=new;\
224                 while(s){\
225                         copy--;                                                         \
226                         copy->prev=copy-1;\
227                         copy->type=s->type;\
228                         copy->flags=0;\
229                         copy->varkind=STACKVAR;\
230                         copy->varnum=s->varnum;\
231                         SET_OUTVAR(s);             \
232                         s=s->prev;\
233                         }\
234                 copy->prev=NULL;\
235                 copy=new-1;\
236                 }\
237         else\
238                 copy=NULL;\
239 }
240 #else
241 # define COPYCURSTACK(copy) {\
242         int d;\
243         stackptr s;\
244         if(curstack){\
245                 s=curstack;\
246                 new+=stackdepth;\
247                 d=stackdepth;\
248                 copy=new;\
249                 while(s){\
250                         copy--;d--;\
251                         copy->prev=copy-1;\
252                         copy->type=s->type;\
253                         copy->flags=0;\
254                         copy->varkind=STACKVAR;\
255                         copy->varnum=d;\
256                         s=s->prev;\
257                         }\
258                 copy->prev=NULL;\
259                 copy=new-1;\
260                 }\
261         else\
262                 copy=NULL;\
263 }
264 #endif
265
266 /* MARKREACHED marks the destination block <b> as reached. If this
267  * block has been reached before we check if stack depth and types
268  * match. Otherwise the destination block receives a copy of the
269  * current stack as its input stack.
270  *
271  * b...destination block
272  * c...current stack
273  */
274
275 /* XXX this macro is much too big! */
276
277 #define MARKREACHED(b,c) \
278     do { \
279                 if ((b) <= (bptr)) \
280                         (b)->bitflags |= BBFLAG_REPLACEMENT; \
281             if ((b)->flags < BBREACHED) { \
282                         int locali; \
283                     COPYCURSTACK((c)); \
284             (b)->flags = BBREACHED; \
285             (b)->instack = (c); \
286             (b)->indepth = stackdepth; \
287                         (b)->invars = DMNEW(s4, stackdepth); \
288                         for (locali = stackdepth; locali--; (c) = (c)->prev) {  \
289                                 (b)->invars[locali] = (c)->varnum;                                      \
290                                 SET_OUTVAR((c));                                                        \
291                         }                                                                                                               \
292         } else { \
293             stackptr s = curstack; \
294             stackptr t = (b)->instack; \
295                         CHECK_STACK_DEPTH((b)->indepth, stackdepth); \
296                     while (s) { \
297                                 CHECK_BASIC_TYPE(s->type,t->type); \
298                             s = s->prev; \
299                 t = t->prev; \
300                         } \
301                 } \
302     } while (0)
303
304
305 /* external macros ************************************************************/
306
307 #define BLOCK_OF(index)                                              \
308     (jd->new_basicblocks + jd->new_basicblockindex[index])
309
310
311 /* function prototypes ********************************************************/
312
313 bool stack_init(void);
314
315 bool new_stack_analyse(jitdata *jd);
316
317 #endif /* _STACK_H */
318
319
320 /*
321  * These are local overrides for various environment variables in Emacs.
322  * Please do not remove this and leave it at the end of the file, where
323  * Emacs will automagically detect them.
324  * ---------------------------------------------------------------------
325  * Local variables:
326  * mode: c
327  * indent-tabs-mode: t
328  * c-basic-offset: 4
329  * tab-width: 4
330  * End:
331  * vim:noexpandtab:sw=4:ts=4:
332  */