d5c7438ec92ba62db625c61bfa65ac6cfd72ae68
[cacao.git] / src / vm / jit / parse.h
1 /* src/vm/jit/parse.h - parser 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    Author:  Christian Thalinger
28             Edwin Steiner
29
30    $Id: parse.h 7663 2007-04-04 22:14:42Z twisti $
31
32 */
33
34
35 #ifndef _PARSE_H
36 #define _PARSE_H
37
38 #include "config.h"
39 #include "vm/types.h"
40
41 #include "vm/global.h"
42 #include "vm/jit/codegen-common.h"
43
44
45 /* macros for verifier checks during parsing **********************************/
46
47 #if defined(ENABLE_VERIFIER)
48
49 /* We have to check local variables indices here because they are             */
50 /* used in stack.c to index the locals array.                                 */
51
52 #define INDEX_ONEWORD(num) \
53     do { \
54         if (((num) < 0) || ((num) >= m->maxlocals)) \
55             goto throw_illegal_local_variable_number; \
56     } while (0)
57
58 #define INDEX_TWOWORD(num) \
59     do { \
60         if (((num) < 0) || (((num) + 1) >= m->maxlocals)) \
61             goto throw_illegal_local_variable_number; \
62     } while (0)
63
64 /* CHECK_BYTECODE_INDEX(i) checks whether i is a valid bytecode index.        */
65 /* The end of the bytecode (i == m->jcodelength) is considered valid.         */
66
67 #define CHECK_BYTECODE_INDEX(i) \
68     do { \
69         if (((i) < 0) || ((i) >= m->jcodelength)) \
70                         goto throw_invalid_bytecode_index; \
71     } while (0)
72
73 /* CHECK_BYTECODE_INDEX_EXCLUSIVE is used for the exclusive ends               */
74 /* of exception handler ranges.                                                */
75 #define CHECK_BYTECODE_INDEX_EXCLUSIVE(i) \
76     do { \
77         if ((i) < 0 || (i) > m->jcodelength) \
78                         goto throw_invalid_bytecode_index; \
79     } while (0)
80
81 #else /* !defined(ENABLE_VERIFIER) */
82
83 #define INDEX_ONEWORD(num)
84 #define INDEX_TWOWORD(num)
85 #define CHECK_BYTECODE_INDEX(i)
86 #define CHECK_BYTECODE_INDEX_EXCLUSIVE(i)
87
88 #endif /* defined(ENABLE_VERIFIER) */
89
90
91 /* basic block generating macro ***********************************************/
92
93 #define MARK_BASICBLOCK(pd, i)                                       \
94     do {                                                             \
95         (pd)->basicblockstart[(i)] = 1;                              \
96     } while (0)
97
98 #define INSTRUCTIONS_CHECK(i)                                        \
99     if ((ircount + (i)) > pd.instructionslength)                     \
100         iptr = parse_realloc_instructions(&pd, ircount, (i))
101
102
103 /* intermediate code generating macros ****************************************/
104
105 /* These macros ALWAYS set the following fields of *iptr to valid values:     */
106 /*     iptr->opc                                                              */
107 /*     iptr->flags                                                            */
108 /*     iptr->line                                                             */
109
110 /* These macros do NOT touch the following fields of *iptr, unless a value is */
111 /* given for them:                                                            */
112 /*     iptr->s1                                                               */
113 /*     iptr->sx                                                               */
114 /*     iptr->dst                                                              */
115
116 /* The _PREPARE macros omit the PINC, so you can set additional fields        */
117 /* afterwards.                                                                */
118
119 #define PINC                                                         \
120     iptr++; ircount++
121
122 #define OP_PREPARE_FLAGS(o, f)                                       \
123     iptr->opc         = (o);                                         \
124     iptr->line        = currentline;                                 \
125     iptr->flags.bits |= (f) | (ircount << INS_FLAG_ID_SHIFT);
126
127 #define OP_PREPARE_ZEROFLAGS(o)                                      \
128     OP_PREPARE_FLAGS(o, 0)
129
130 #define OP_PREPARE(o)                                                \
131     OP_PREPARE_ZEROFLAGS(o)
132
133 #define OP(o)                                                        \
134     OP_PREPARE_ZEROFLAGS(o);                                         \
135     PINC
136
137 #define OP_CHECK_EXCEPTION(o)                                        \
138     OP_PREPARE_FLAGS(o, INS_FLAG_CHECK);                             \
139     PINC
140
141 #define OP_LOADCONST_I(v)                                            \
142     OP_PREPARE_ZEROFLAGS(ICMD_ICONST);                               \
143     iptr->sx.val.i           = (v);                                  \
144     PINC
145
146 #define OP_LOADCONST_L(v)                                            \
147     OP_PREPARE_ZEROFLAGS(ICMD_LCONST);                               \
148     iptr->sx.val.l           = (v);                                  \
149     PINC
150
151 #define OP_LOADCONST_F(v)                                            \
152     OP_PREPARE_ZEROFLAGS(ICMD_FCONST);                               \
153     iptr->sx.val.f           = (v);                                  \
154     PINC
155
156 #define OP_LOADCONST_D(v)                                            \
157     OP_PREPARE_ZEROFLAGS(ICMD_DCONST);                               \
158     iptr->sx.val.d           = (v);                                  \
159     PINC
160
161 #define OP_LOADCONST_NULL()                                          \
162     OP_PREPARE_FLAGS(ICMD_ACONST, INS_FLAG_CHECK);                   \
163     iptr->sx.val.anyptr      = NULL;                                 \
164     PINC
165
166 #define OP_LOADCONST_STRING(v)                                       \
167     OP_PREPARE_FLAGS(ICMD_ACONST, INS_FLAG_CHECK);                   \
168     iptr->sx.val.stringconst = (v);                                  \
169     PINC
170
171 #define OP_LOADCONST_CLASSINFO_OR_CLASSREF_FLAGS(cl, cr, extraflags) \
172     OP_PREPARE(ICMD_ACONST);                                         \
173     if (cl) {                                                        \
174         iptr->sx.val.c.cls   = (cl);                                 \
175         iptr->flags.bits     |= INS_FLAG_CLASS | (extraflags);       \
176     }                                                                \
177     else {                                                           \
178         iptr->sx.val.c.ref   = (cr);                                 \
179         iptr->flags.bits     |= INS_FLAG_CLASS | INS_FLAG_UNRESOLVED \
180                              | (extraflags);                         \
181     }                                                                \
182     PINC
183
184 #define OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr)              \
185     OP_LOADCONST_CLASSINFO_OR_CLASSREF_FLAGS((c), (cr), INS_FLAG_CHECK)
186
187 #define OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr)            \
188     OP_LOADCONST_CLASSINFO_OR_CLASSREF_FLAGS((c), (cr), 0)
189
190 #define OP_S3_CLASSINFO_OR_CLASSREF(o, c, cr, extraflags)            \
191     OP_PREPARE(o);                                                   \
192     if (c) {                                                         \
193         iptr->sx.s23.s3.c.cls= (c);                                  \
194         iptr->flags.bits     |= (extraflags);                        \
195     }                                                                \
196     else {                                                           \
197         iptr->sx.s23.s3.c.ref= (cr);                                 \
198         iptr->flags.bits     |= INS_FLAG_UNRESOLVED | (extraflags);  \
199     }                                                                \
200     PINC
201
202 #define OP_INSINDEX(o, iindex)                                       \
203     OP_PREPARE_ZEROFLAGS(o);                                         \
204     iptr->dst.insindex       = (iindex);                             \
205     PINC
206
207 # define OP_LOCALINDEX(o,index)                                      \
208     OP_PREPARE_ZEROFLAGS(o);                                         \
209     iptr->s1.varindex      = (index);                                \
210     PINC
211
212 # define OP_LOCALINDEX_I(o,index,v)                                  \
213     OP_PREPARE_ZEROFLAGS(o);                                         \
214     iptr->s1.varindex      = (index);                                \
215     iptr->sx.val.i           = (v);                                  \
216     PINC
217
218 # define LOCALTYPE_USED(index,type)                                  \
219     do {                                                             \
220         local_map[(index) * 5 + (type)] = 1;                         \
221     } while (0)
222
223 #define OP_LOAD_ONEWORD(o,index,type)                                \
224     do {                                                             \
225         INDEX_ONEWORD(index);                                        \
226         OP_LOCALINDEX(o,index);                                      \
227         LOCALTYPE_USED(index,type);                                  \
228     } while (0)
229
230 #define OP_LOAD_TWOWORD(o,index,type)                                \
231     do {                                                             \
232         INDEX_TWOWORD(index);                                        \
233         OP_LOCALINDEX(o,index);                                      \
234         LOCALTYPE_USED(index,type);                                  \
235     } while (0)
236
237 # define OP_STORE_ONEWORD(o,index,type)                              \
238     do {                                                             \
239         INDEX_ONEWORD(index);                                        \
240         OP_PREPARE_ZEROFLAGS(o);                                     \
241         iptr->dst.varindex = (index);                                \
242         LOCALTYPE_USED(index,type);                                  \
243         PINC;                                                        \
244     } while (0)
245
246 # define OP_STORE_TWOWORD(o,index,type)                              \
247     do {                                                             \
248         INDEX_TWOWORD(index);                                        \
249         OP_PREPARE_ZEROFLAGS(o);                                     \
250         iptr->dst.varindex = (index);                                \
251         LOCALTYPE_USED(index,type);                                  \
252         PINC;                                                        \
253     } while (0)
254
255 #define OP_BUILTIN_CHECK_EXCEPTION(bte)                              \
256     jd->isleafmethod         = false;                                \
257     OP_PREPARE_FLAGS(ICMD_BUILTIN, INS_FLAG_CHECK);                  \
258     iptr->sx.s23.s3.bte      = (bte);                                \
259     PINC
260
261 #define OP_BUILTIN_NO_EXCEPTION(bte)                                 \
262     jd->isleafmethod         = false;                                \
263     OP_PREPARE_ZEROFLAGS(ICMD_BUILTIN);                              \
264     iptr->sx.s23.s3.bte      = (bte);                                \
265     PINC
266
267 #define OP_BUILTIN_ARITHMETIC(opcode, bte)                           \
268     jd->isleafmethod         = false;                                \
269     OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);                        \
270     iptr->sx.s23.s3.bte      = (bte);                                \
271     PINC
272
273 /* CAUTION: You must set iptr->flags yourself when using this!                */
274 #define OP_FMIREF_PREPARE(o, fmiref)                                 \
275     OP_PREPARE(o);                                                   \
276     iptr->sx.s23.s3.fmiref   = (fmiref);
277
278
279 /* function prototypes ********************************************************/
280
281 bool parse(jitdata *jd);
282
283 #endif /* _PARSE_H */
284
285
286 /*
287  * These are local overrides for various environment variables in Emacs.
288  * Please do not remove this and leave it at the end of the file, where
289  * Emacs will automagically detect them.
290  * ---------------------------------------------------------------------
291  * Local variables:
292  * mode: c
293  * indent-tabs-mode: t
294  * c-basic-offset: 4
295  * tab-width: 4
296  * End:
297  * vim:noexpandtab:sw=4:ts=4:
298  */
299