* src/vm/jit/alpha/patcher.c (patcher_wrapper): Added return address
[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
29    Changes: Edwin Steiner
30
31    $Id: parse.h 5096 2006-07-10 14:02:25Z twisti $
32
33 */
34
35
36 #ifndef _PARSE_H
37 #define _PARSE_H
38
39 #include "config.h"
40 #include "vm/types.h"
41
42 #include "vm/global.h"
43 #include "vm/jit/codegen-common.h"
44
45
46 /* macros for verifier checks during parsing **********************************/
47
48 #if defined(ENABLE_VERIFIER)
49
50 /* We have to check local variables indices here because they are             */
51 /* used in stack.c to index the locals array.                                 */
52
53 #define INDEX_ONEWORD(num) \
54     do { \
55         if (((num) < 0) || ((num) >= m->maxlocals)) \
56             goto throw_illegal_local_variable_number; \
57     } while (0)
58
59 #define INDEX_TWOWORD(num) \
60     do { \
61         if (((num) < 0) || (((num) + 1) >= m->maxlocals)) \
62             goto throw_illegal_local_variable_number; \
63     } while (0)
64
65 /* CHECK_BYTECODE_INDEX(i) checks whether i is a valid bytecode index.        */
66 /* The end of the bytecode (i == m->jcodelength) is considered valid.         */
67
68 #define CHECK_BYTECODE_INDEX(i) \
69     do { \
70         if (((i) < 0) || ((i) >= m->jcodelength)) \
71                         goto throw_invalid_bytecode_index; \
72     } while (0)
73
74 /* CHECK_BYTECODE_INDEX_EXCLUSIVE is used for the exclusive ends               */
75 /* of exception handler ranges.                                                */
76 #define CHECK_BYTECODE_INDEX_EXCLUSIVE(i) \
77     do { \
78         if ((i) < 0 || (i) > m->jcodelength) \
79                         goto throw_invalid_bytecode_index; \
80     } while (0)
81
82 #else /* !define(ENABLE_VERIFIER) */
83
84 #define INDEX_ONEWORD(num)
85 #define INDEX_TWOWORD(num)
86 #define CHECK_BYTECODE_INDEX(i)
87 #define CHECK_BYTECODE_INDEX_EXCLUSIVE(i)
88
89 #endif /* define(ENABLE_VERIFIER) */
90
91
92 /* basic block generating macro ***********************************************/
93
94 #define new_block_insert(i) \
95     do { \
96         if (!(jd->new_basicblockindex[(i)] & 1)) { \
97             b_count++; \
98             jd->new_basicblockindex[(i)] |= 1; \
99         } \
100     } while (0)
101
102 #define block_insert(i) \
103     do { \
104         if (!(m->basicblockindex[(i)] & 1)) { \
105             b_count++; \
106             m->basicblockindex[(i)] |= 1; \
107         } \
108     } while (0)
109
110
111 /* intermediate code generating macros ****************************************/
112
113 /* These macros ALWAYS set the following fields of *iptr to valid values:     */
114 /*     iptr->opc                                                              */
115 /*     iptr->flags                                                            */
116 /*     iptr->line                                                             */
117
118 /* These macros do NOT touch the following fields of *iptr, unless a value is */
119 /* given for them:                                                            */
120 /*     iptr->s1                                                               */
121 /*     iptr->sx                                                               */
122 /*     iptr->dst                                                              */
123
124 /* The _PREPARE macros omit the PINC, so you can set additional fields        */
125 /* afterwards.                                                                */
126 /* CAUTION: Some of the _PREPARE macros don't set iptr->flags!                */
127
128 #define PINC                                                           \
129     iptr++; ipc++
130
131 /* CAUTION: You must set iptr->flags yourself when using this!                */
132 #define NEW_OP_PREPARE(o)                                              \
133     iptr->opc                = (o);                                    \
134     iptr->line               = currentline;
135
136 #define NEW_OP_PREPARE_ZEROFLAGS(o)                                    \
137     iptr->opc                = (o);                                    \
138     iptr->line               = currentline;                            \
139     iptr->flags.bits         = 0;
140
141 #define NEW_OP(o)                                                      \
142         NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
143     PINC
144
145 #define NEW_OP_LOADCONST_I(v)                                          \
146         NEW_OP_PREPARE_ZEROFLAGS(ICMD_ICONST);                             \
147     iptr->sx.val.i           = (v);                                    \
148     PINC
149
150 #define NEW_OP_LOADCONST_L(v)                                          \
151         NEW_OP_PREPARE_ZEROFLAGS(ICMD_LCONST);                             \
152     iptr->sx.val.l           = (v);                                    \
153     PINC
154
155 #define NEW_OP_LOADCONST_F(v)                                          \
156         NEW_OP_PREPARE_ZEROFLAGS(ICMD_FCONST);                             \
157     iptr->sx.val.f           = (v);                                    \
158     PINC
159
160 #define NEW_OP_LOADCONST_D(v)                                          \
161         NEW_OP_PREPARE_ZEROFLAGS(ICMD_DCONST);                             \
162     iptr->sx.val.d           = (v);                                    \
163     PINC
164
165 #define NEW_OP_LOADCONST_NULL()                                        \
166         NEW_OP_PREPARE_ZEROFLAGS(ICMD_ACONST);                             \
167     iptr->sx.val.anyptr      = NULL;                                   \
168     PINC
169
170 #define NEW_OP_LOADCONST_STRING(v)                                     \
171         NEW_OP_PREPARE_ZEROFLAGS(ICMD_ACONST);                             \
172     iptr->sx.val.stringconst = (v);                                    \
173     PINC
174
175 #define NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, extraflags)      \
176         NEW_OP_PREPARE(ICMD_ACONST);                                       \
177     if (c) {                                                           \
178         iptr->sx.val.c.cls   = (c);                                    \
179         iptr->flags.bits     = INS_FLAG_CLASS | (extraflags);          \
180     }                                                                  \
181     else {                                                             \
182         iptr->sx.val.c.ref   = (cr);                                   \
183         iptr->flags.bits     = INS_FLAG_CLASS | INS_FLAG_UNRESOLVED    \
184                              | (extraflags);                           \
185     }                                                                  \
186     PINC
187
188 #define NEW_OP_S3_CLASSINFO_OR_CLASSREF(o, c, cr, extraflags)          \
189         NEW_OP_PREPARE(o);                                                 \
190     if (c) {                                                           \
191         iptr->sx.s23.s3.c.cls= (c);                                    \
192         iptr->flags.bits     = (extraflags);                           \
193     }                                                                  \
194     else {                                                             \
195         iptr->sx.s23.s3.c.ref= (cr);                                   \
196         iptr->flags.bits     = INS_FLAG_UNRESOLVED | (extraflags);     \
197     }                                                                  \
198     PINC
199
200 #define NEW_OP_INSINDEX(o, iindex)                                     \
201         NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
202     iptr->dst.insindex       = (iindex);                               \
203     PINC
204
205 #define NEW_OP_LOCALINDEX(o,index)                                     \
206         NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
207     iptr->s1.localindex      = (index);                                \
208     PINC
209
210 #define NEW_OP_LOCALINDEX_I(o,index,v)                                 \
211         NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
212     iptr->s1.localindex      = (index);                                \
213     iptr->sx.val.i           = (v);                                    \
214     PINC
215
216 #define NEW_OP_LOAD_ONEWORD(o,index)                                   \
217     do {                                                               \
218         INDEX_ONEWORD(index);                                          \
219         NEW_OP_LOCALINDEX(o,index);                                    \
220     } while (0)
221
222 #define NEW_OP_LOAD_TWOWORD(o,index)                                   \
223     do {                                                               \
224         INDEX_TWOWORD(index);                                          \
225         NEW_OP_LOCALINDEX(o,index);                                    \
226     } while (0)
227
228 #define NEW_OP_STORE_ONEWORD(o,index)                                  \
229     do {                                                               \
230         INDEX_ONEWORD(index);                                          \
231         NEW_OP_LOCALINDEX(o,index);                                    \
232     } while (0)
233
234 #define NEW_OP_STORE_TWOWORD(o,index)                                  \
235     do {                                                               \
236         INDEX_TWOWORD(index);                                          \
237         NEW_OP_LOCALINDEX(o,index);                                    \
238     } while (0)
239
240 #define NEW_OP_BUILTIN_CHECK_EXCEPTION(bte)                            \
241     jd->isleafmethod         = false;                                  \
242         NEW_OP_PREPARE_ZEROFLAGS(ICMD_BUILTIN);                            \
243     iptr->sx.s23.s3.bte      = (bte);                                  \
244     PINC
245
246 #define NEW_OP_BUILTIN_NO_EXCEPTION(bte)                               \
247     jd->isleafmethod         = false;                                  \
248         NEW_OP_PREPARE(ICMD_BUILTIN);                                      \
249     iptr->sx.s23.s3.bte      = (bte);                                  \
250     iptr->flags.bits         = INS_FLAG_NOCHECK;                       \
251     PINC
252
253 #define NEW_OP_BUILTIN_ARITHMETIC(opcode, bte)                         \
254     jd->isleafmethod         = false;                                  \
255         NEW_OP_PREPARE_ZEROFLAGS(opcode);                                  \
256     iptr->sx.s23.s3.bte      = (bte);                                  \
257     PINC
258
259 /* CAUTION: You must set iptr->flags yourself when using this!                */
260 #define NEW_OP_FMIREF_PREPARE(o, fmiref)                               \
261         NEW_OP_PREPARE(o);                                                 \
262     iptr->sx.s23.s3.fmiref   = (fmiref);
263
264 /* old macros for intermediate code generation ********************************/
265
266 #define LOADCONST_I(v) \
267     iptr->opc    = ICMD_ICONST; \
268     iptr->val.i  = (v); \
269     iptr->line   = currentline; \
270     PINC
271
272 #define LOADCONST_L(v) \
273     iptr->opc    = ICMD_LCONST; \
274     iptr->val.l  = (v); \
275     iptr->line   = currentline; \
276     PINC
277
278 #define LOADCONST_F(v) \
279     iptr->opc    = ICMD_FCONST; \
280     iptr->val.f  = (v); \
281     iptr->line   = currentline; \
282     PINC
283
284 #define LOADCONST_D(v) \
285     iptr->opc    = ICMD_DCONST; \
286     iptr->val.d  = (v); \
287     iptr->line   = currentline; \
288     PINC
289
290 #define LOADCONST_A(v) \
291     iptr->opc    = ICMD_ACONST; \
292     iptr->val.a  = (v); \
293     iptr->line   = currentline; \
294     PINC
295
296 /* ACONST instructions generated as arguments for builtin functions
297  * have op1 set to non-zero. This is used for stack overflow checking
298  * in stack.c. */
299 /* XXX in the new instruction format use a flag for this */
300 /* XXX target used temporarily as flag */
301 #define LOADCONST_A_BUILTIN(c,cr) \
302     iptr->opc    = ICMD_ACONST; \
303     iptr->op1    = 1; \
304     iptr->line   = currentline; \
305         if (c) { \
306                 iptr->val.a = c; iptr->target = (void*) 0x02; \
307         } \
308         else { \
309                 iptr->val.a = cr; iptr->target = (void*) 0x03; \
310         } \
311     PINC
312
313 #define OP(o) \
314     iptr->opc    = (o); \
315     iptr->line   = currentline; \
316     PINC
317
318 #define OP1(o,o1) \
319     iptr->opc    = (o); \
320     iptr->op1    = (o1); \
321     iptr->line   = currentline; \
322     PINC
323
324 #define OP2I(o,o1,v) \
325     iptr->opc    = (o); \
326     iptr->op1    = (o1); \
327     iptr->val.i  = (v); \
328     iptr->line   = currentline; \
329     PINC
330
331 #define OP2A_NOINC(o,o1,v,l) \
332     iptr->opc    = (o); \
333     iptr->op1    = (o1); \
334     iptr->val.a  = (v); \
335     iptr->line   = (l);
336
337 #define OP2A(o,o1,v,l) \
338     OP2A_NOINC(o,o1,v,l); \
339     PINC
340
341 #define OP2AT(o,o1,v,t,l) \
342     OP2A_NOINC(o,o1,v,l); \
343     iptr->target = (t); \
344     PINC
345
346 #define BUILTIN(v,o1,t,l) \
347     jd->isleafmethod = false; \
348     iptr->opc        = ICMD_BUILTIN; \
349     iptr->op1        = (o1); \
350     iptr->val.a      = (v); \
351     iptr->target     = (t); \
352     iptr->line       = (l); \
353     PINC
354
355 #define OP1LOAD_ONEWORD(o,o1) \
356     do { \
357                 INDEX_ONEWORD(o1); \
358         OP1(o,o1); \
359     } while (0)
360
361 #define OP1LOAD_TWOWORD(o,o1) \
362     do { \
363                 INDEX_TWOWORD(o1); \
364         OP1(o,o1); \
365     } while (0)
366
367 #define OP1STORE_ONEWORD(o,o1) \
368     do { \
369                 INDEX_ONEWORD(o1); \
370         OP1(o,o1); \
371     } while (0)
372
373 #define OP1STORE_TWOWORD(o,o1) \
374     do { \
375                 INDEX_TWOWORD(o1); \
376         OP1(o,o1); \
377     } while (0)
378
379
380 /* macros for byte code fetching ***********************************************
381
382         fetch a byte code of given size from position p in code array jcode
383
384 *******************************************************************************/
385
386 #define code_get_u1(p,m)  m->jcode[p]
387 #define code_get_s1(p,m)  ((s1)m->jcode[p])
388 #define code_get_u2(p,m)  ((((u2)m->jcode[p]) << 8) + m->jcode[p + 1])
389 #define code_get_s2(p,m)  ((s2)((((u2)m->jcode[p]) << 8) + m->jcode[p + 1]))
390 #define code_get_u4(p,m)  ((((u4)m->jcode[p]) << 24) + (((u4)m->jcode[p + 1]) << 16) \
391                         +(((u4)m->jcode[p + 2]) << 8) + m->jcode[p + 3])
392 #define code_get_s4(p,m)  ((s4)((((u4)m->jcode[p]) << 24) + (((u4)m->jcode[p + 1]) << 16) \
393                              +(((u4)m->jcode[p + 2]) << 8) + m->jcode[p + 3]))
394 /* XXX read 32bit aligned big-endian values directly */
395
396
397 /* function prototypes ********************************************************/
398
399 bool parse(jitdata *jd);
400
401 #endif /* _PARSE_H */
402
403
404 /*
405  * These are local overrides for various environment variables in Emacs.
406  * Please do not remove this and leave it at the end of the file, where
407  * Emacs will automagically detect them.
408  * ---------------------------------------------------------------------
409  * Local variables:
410  * mode: c
411  * indent-tabs-mode: t
412  * c-basic-offset: 4
413  * tab-width: 4
414  * End:
415  * vim:noexpandtab:sw=4:ts=4:
416  */
417