* src/vm/jit/stack.c (new_stack_analyse): Made control-flow easier
[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                         Edwin Steiner
31
32    $Id: stack.h 5447 2006-09-09 21:33:48Z edwin $
33
34 */
35
36
37 #ifndef _STACK_H
38 #define _STACK_H
39
40 #include "config.h"
41
42 #include "vm/types.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
46 #include "vm/jit/jit.h"
47 #include "vm/jit/reg.h"
48
49
50 /* macros used internally by analyse_stack ************************************/
51
52 /*--------------------------------------------------*/
53 /* STACK DEPTH CHECKING                             */
54 /*--------------------------------------------------*/
55
56 #if defined(ENABLE_VERIFIER)
57 #define CHECK_STACK_DEPTH(depthA,depthB)                             \
58     do {                                                             \
59         if ((depthA) != (depthB))                                    \
60             goto throw_stack_depth_error;                            \
61     } while (0)
62 #else /* !ENABLE_VERIFIER */
63 #define CHECK_STACK_DEPTH(depthA,depthB)
64 #endif /* ENABLE_VERIFIER */
65
66
67 /*--------------------------------------------------*/
68 /* BASIC TYPE CHECKING                              */
69 /*--------------------------------------------------*/
70
71 /* XXX would be nice if we did not have to pass the expected type */
72
73 #if defined(ENABLE_VERIFIER)
74 #define CHECK_BASIC_TYPE(expected,actual)                            \
75     do {                                                             \
76         if ((actual) != (expected)) {                                \
77             expectedtype = (expected);                               \
78             goto throw_stack_type_error;                             \
79         }                                                            \
80     } while (0)
81 #else /* !ENABLE_VERIFIER */
82 #define CHECK_BASIC_TYPE(expected,actual)
83 #endif /* ENABLE_VERIFIER */
84
85 /*--------------------------------------------------*/
86 /* STACK UNDERFLOW/OVERFLOW CHECKS                  */
87 /*--------------------------------------------------*/
88
89 /* underflow checks */
90
91 #if defined(ENABLE_VERIFIER)
92 #define REQUIRE(num)                                                 \
93     do {                                                             \
94         if (stackdepth < (num))                                      \
95             goto throw_stack_underflow;                              \
96     } while (0)
97 #else /* !ENABLE_VERIFIER */
98 #define REQUIRE(num)
99 #endif /* ENABLE_VERIFIER */
100
101
102 /* overflow check */
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?
106  */
107
108 /* XXX we should find a way to remove the opc/op1 check */
109 #if defined(ENABLE_VERIFIER)
110 #define CHECKOVERFLOW                                                \
111     do {                                                             \
112         if (stackdepth > m->maxstack)                                \
113             if ((iptr->opc != ICMD_ACONST) || INSTRUCTION_MUST_CHECK(iptr))\
114                 goto throw_stack_overflow;                           \
115     } while(0)
116 #else /* !ENABLE_VERIFIER */
117 #define CHECKOVERFLOW
118 #endif /* ENABLE_VERIFIER */
119
120 /*--------------------------------------------------*/
121 /* ALLOCATING STACK SLOTS                           */
122 /*--------------------------------------------------*/
123
124 #define NEWSTACK(s,v,n)                                              \
125     do {                                                             \
126         sd.new->prev = curstack;                                     \
127         sd.new->type = (s);                                          \
128         sd.new->flags = 0;                                           \
129         sd.new->varkind = (v);                                       \
130         sd.new->varnum = (n);                                        \
131         curstack = sd.new;                                           \
132         sd.var[(n)].type = (s);                                      \
133         sd.var[(n)].flags = 0;                                       \
134         sd.new++;                                                    \
135     } while (0)
136
137 /* Initialize regoff, so -sia can show regnames even before reg.inc */
138 /* regs[rd->intregargnum] has to be set for this                    */
139 /* new->regoff = (IS_FLT_DBL_TYPE(s))?-1:rd->intreg_argnum; }       */
140
141 #define NEWSTACKn(s,n)  NEWSTACK(s,UNDEFVAR,n)
142 #define NEWSTACK0(s)    NEWSTACK(s,UNDEFVAR,0)
143
144
145 /* external macros ************************************************************/
146
147 #define BLOCK_OF(index)                                              \
148     (jd->new_basicblocks + jd->new_basicblockindex[index])
149
150
151 /* function prototypes ********************************************************/
152
153 bool stack_init(void);
154
155 bool new_stack_analyse(jitdata *jd);
156
157 #endif /* _STACK_H */
158
159
160 /*
161  * These are local overrides for various environment variables in Emacs.
162  * Please do not remove this and leave it at the end of the file, where
163  * Emacs will automagically detect them.
164  * ---------------------------------------------------------------------
165  * Local variables:
166  * mode: c
167  * indent-tabs-mode: t
168  * c-basic-offset: 4
169  * tab-width: 4
170  * End:
171  * vim:noexpandtab:sw=4:ts=4:
172  */