* src/vm/jit/parse.c, src/vm/jit/parse.h (Changes): Merged with
[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 5959 2006-11-12 13:31:14Z edwin $
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(i)                                           \
94     do {                                                             \
95         if (!(jd->basicblockindex[(i)] & 1)) {                   \
96             b_count++;                                               \
97             jd->basicblockindex[(i)] |= 1;                       \
98         }                                                            \
99     } while (0)
100
101 #define INSTRUCTIONS_CHECK(i)                                        \
102     if ((ipc + (i)) > pd.instructionslength)                         \
103         iptr = parse_realloc_instructions(&pd, ipc, (i))
104
105
106 /* intermediate code generating macros ****************************************/
107
108 /* These macros ALWAYS set the following fields of *iptr to valid values:     */
109 /*     iptr->opc                                                              */
110 /*     iptr->flags                                                            */
111 /*     iptr->line                                                             */
112
113 /* These macros do NOT touch the following fields of *iptr, unless a value is */
114 /* given for them:                                                            */
115 /*     iptr->s1                                                               */
116 /*     iptr->sx                                                               */
117 /*     iptr->dst                                                              */
118
119 /* The _PREPARE macros omit the PINC, so you can set additional fields        */
120 /* afterwards.                                                                */
121
122 #define PINC                                                         \
123     iptr++; ipc++
124
125 #define OP_PREPARE_FLAGS(o, f)                                       \
126     iptr->opc                = (o);                                  \
127     iptr->line               = currentline;                          \
128     iptr->flags.bits         = (f) | (ipc << INS_FLAG_ID_SHIFT);
129
130 #define OP_PREPARE_ZEROFLAGS(o)                                      \
131     OP_PREPARE_FLAGS(o, 0)
132
133 #define OP_PREPARE(o)                                                \
134     OP_PREPARE_ZEROFLAGS(o)
135
136 #define OP(o)                                                        \
137     OP_PREPARE_ZEROFLAGS(o);                                         \
138     PINC
139
140 #define OP_LOADCONST_I(v)                                            \
141     OP_PREPARE_ZEROFLAGS(ICMD_ICONST);                               \
142     iptr->sx.val.i           = (v);                                  \
143     PINC
144
145 #define OP_LOADCONST_L(v)                                            \
146     OP_PREPARE_ZEROFLAGS(ICMD_LCONST);                               \
147     iptr->sx.val.l           = (v);                                  \
148     PINC
149
150 #define OP_LOADCONST_F(v)                                            \
151     OP_PREPARE_ZEROFLAGS(ICMD_FCONST);                               \
152     iptr->sx.val.f           = (v);                                  \
153     PINC
154
155 #define OP_LOADCONST_D(v)                                            \
156     OP_PREPARE_ZEROFLAGS(ICMD_DCONST);                               \
157     iptr->sx.val.d           = (v);                                  \
158     PINC
159
160 #define OP_LOADCONST_NULL()                                          \
161     OP_PREPARE_FLAGS(ICMD_ACONST, INS_FLAG_CHECK);                   \
162     iptr->sx.val.anyptr      = NULL;                                 \
163     PINC
164
165 #define OP_LOADCONST_STRING(v)                                       \
166     OP_PREPARE_FLAGS(ICMD_ACONST, INS_FLAG_CHECK);                   \
167     iptr->sx.val.stringconst = (v);                                  \
168     PINC
169
170 #define OP_LOADCONST_CLASSINFO_OR_CLASSREF_FLAGS(cl, cr, extraflags) \
171     OP_PREPARE(ICMD_ACONST);                                         \
172     if (cl) {                                                        \
173         iptr->sx.val.c.cls   = (cl);                                 \
174         iptr->flags.bits     |= INS_FLAG_CLASS | (extraflags);       \
175     }                                                                \
176     else {                                                           \
177         iptr->sx.val.c.ref   = (cr);                                 \
178         iptr->flags.bits     |= INS_FLAG_CLASS | INS_FLAG_UNRESOLVED \
179                              | (extraflags);                         \
180     }                                                                \
181     PINC
182
183 #define OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr)              \
184     OP_LOADCONST_CLASSINFO_OR_CLASSREF_FLAGS((c), (cr), INS_FLAG_CHECK)
185
186 #define OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr)            \
187     OP_LOADCONST_CLASSINFO_OR_CLASSREF_FLAGS((c), (cr), 0)
188
189 #define OP_S3_CLASSINFO_OR_CLASSREF(o, c, cr, extraflags)            \
190     OP_PREPARE(o);                                                   \
191     if (c) {                                                         \
192         iptr->sx.s23.s3.c.cls= (c);                                  \
193         iptr->flags.bits     |= (extraflags);                        \
194     }                                                                \
195     else {                                                           \
196         iptr->sx.s23.s3.c.ref= (cr);                                 \
197         iptr->flags.bits     |= INS_FLAG_UNRESOLVED | (extraflags);  \
198     }                                                                \
199     PINC
200
201 #define OP_INSINDEX(o, iindex)                                       \
202     OP_PREPARE_ZEROFLAGS(o);                                         \
203     iptr->dst.insindex       = (iindex);                             \
204     PINC
205
206 # define OP_LOCALINDEX(o,index)                                      \
207     OP_PREPARE_ZEROFLAGS(o);                                         \
208     iptr->s1.varindex      = (index);                                \
209     PINC
210
211 # define OP_LOCALINDEX_I(o,index,v)                                  \
212     OP_PREPARE_ZEROFLAGS(o);                                         \
213     iptr->s1.varindex      = (index);                                \
214     iptr->sx.val.i           = (v);                                  \
215     PINC
216
217 # define LOCALTYPE_USED(index,type)                                  \
218     do {                                                             \
219         local_map[(index) * 5 + (type)] = 1;                         \
220     } while (0)
221
222 #define OP_LOAD_ONEWORD(o,index,type)                                \
223     do {                                                             \
224         INDEX_ONEWORD(index);                                        \
225         OP_LOCALINDEX(o,index);                                      \
226         LOCALTYPE_USED(index,type);                                  \
227     } while (0)
228
229 #define OP_LOAD_TWOWORD(o,index,type)                                \
230     do {                                                             \
231         INDEX_TWOWORD(index);                                        \
232         OP_LOCALINDEX(o,index);                                      \
233         LOCALTYPE_USED(index,type);                                  \
234     } while (0)
235
236 # define OP_STORE_ONEWORD(o,index,type)                              \
237     do {                                                             \
238         INDEX_ONEWORD(index);                                        \
239         OP_PREPARE_ZEROFLAGS(o);                                     \
240         iptr->dst.varindex = (index);                                \
241         LOCALTYPE_USED(index,type);                                  \
242         PINC;                                                        \
243     } while (0)
244
245 # define OP_STORE_TWOWORD(o,index,type)                              \
246     do {                                                             \
247         INDEX_TWOWORD(index);                                        \
248         OP_PREPARE_ZEROFLAGS(o);                                     \
249         iptr->dst.varindex = (index);                                \
250         LOCALTYPE_USED(index,type);                                  \
251         PINC;                                                        \
252     } while (0)
253
254 #define OP_BUILTIN_CHECK_EXCEPTION(bte)                              \
255     jd->isleafmethod         = false;                                \
256     OP_PREPARE_FLAGS(ICMD_BUILTIN, INS_FLAG_CHECK);                  \
257     iptr->sx.s23.s3.bte      = (bte);                                \
258     PINC
259
260 #define OP_BUILTIN_NO_EXCEPTION(bte)                                 \
261     jd->isleafmethod         = false;                                \
262     OP_PREPARE_ZEROFLAGS(ICMD_BUILTIN);                              \
263     iptr->sx.s23.s3.bte      = (bte);                                \
264     PINC
265
266 #define OP_BUILTIN_ARITHMETIC(opcode, bte)                           \
267     jd->isleafmethod         = false;                                \
268     OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);                        \
269     iptr->sx.s23.s3.bte      = (bte);                                \
270     PINC
271
272 /* CAUTION: You must set iptr->flags yourself when using this!                */
273 #define OP_FMIREF_PREPARE(o, fmiref)                                 \
274     OP_PREPARE(o);                                                   \
275     iptr->sx.s23.s3.fmiref   = (fmiref);
276
277
278 /* external macros ************************************************************/
279
280 #define BLOCK_OF(index)                                              \
281     (jd->basicblocks + jd->basicblockindex[index])
282
283
284 /* function prototypes ********************************************************/
285
286 bool parse(jitdata *jd);
287
288 #endif /* _PARSE_H */
289
290
291 /*
292  * These are local overrides for various environment variables in Emacs.
293  * Please do not remove this and leave it at the end of the file, where
294  * Emacs will automagically detect them.
295  * ---------------------------------------------------------------------
296  * Local variables:
297  * mode: c
298  * indent-tabs-mode: t
299  * c-basic-offset: 4
300  * tab-width: 4
301  * End:
302  * vim:noexpandtab:sw=4:ts=4:
303  */
304