1 /* src/vm/jit/intrp/engine.c - #included by engine1.c and engine2.c
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
32 $Id: engine.c 5692 2006-10-05 15:33:42Z edwin $
43 #include "vm/jit/intrp/intrp.h"
45 #include "md-abi.h" /* required for TRACE_ARGS_NUM */
47 #include "vm/builtin.h"
48 #include "vm/exceptions.h"
49 #include "vm/loader.h"
50 #include "vm/options.h"
51 #include "vm/jit/methodheader.h"
52 #include "vm/jit/patcher.h"
54 #if defined(ENABLE_THREADS)
55 # ifndef USE_FAKE_ATOMIC_INSTRUCTIONS
56 # include "machine-instr.h"
58 # include "threads/native/generic-primitives.h"
62 #if !defined(STORE_ORDER_BARRIER) && !defined(ENABLE_THREADS)
63 #define STORE_ORDER_BARRIER() /* nothing */
67 /* threading macros */
68 #define GCC_PR15242_WORKAROUND
69 #ifdef GCC_PR15242_WORKAROUND
71 # define DO_GOTO goto before_goto
74 # define DO_GOTO goto *ip[-1]
79 # define SET_IP(p) do {ip=(p); NEXT_P0;} while (0)
80 # define NEXT_INST (*IP)
81 # define INC_IP(const_inc) do { ip+=(const_inc);} while (0)
83 # define NEXT_P1 (ip++)
84 # define NEXT_P2 do {NEXT_P1_5; DO_GOTO;} while(0)
86 #define NEXT ({DEF_CA NEXT_P1; NEXT_P2;})
87 #define IPTOS NEXT_INST
89 #if defined(__POWERPC__) || defined(__POWERPC64__) || defined(__SPARC__)
93 #if defined(USE_spTOS)
100 #if defined(__I386__)
101 /* works with gcc-2.95.4 20011002 (Debian prerelease) without static supers */
102 #define SPREG /* __asm__("%esi") */
103 #define TOSREG /* __asm__("%ecx") */
104 #elif defined(__X86_64__)
105 /* works with gcc-4.0.2 (Debian 4.0.2-2) */
106 #define SPREG /* __asm__("%r15") */
114 /* conversion on fetch */
117 #define SUPER_END vm_count_block(IP)
120 #define vm_uncount_block(_ip) /* nothing */
124 #define THROW0 goto throw
126 #define THROW(_ball) do { \
127 __asm__(""); /* work around gcc PR 25285 */ \
128 goto *throw_##_ball; \
131 #define THROW(_ball) do { \
132 goto throw_##_ball##1; \
136 #define THROWCODE(_ball) \
139 *exceptionptr = (stacktrace_inline_##_ball(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP)); \
143 #define CHECK_NULL_PTR(ptr) \
145 if ((ptr) == NULL) { \
146 THROW(nullpointerexception); \
150 #define THROW_CLASSCASTEXCEPTION(o) \
152 classcastexception_object = o; \
153 THROW(classcastexception); \
156 #define CHECK_OUT_OF_BOUNDS(_array, _idx) \
158 if (length_array(_array) <= (u4) (_idx)) { \
159 arrayindexoutofbounds_index = (_idx); \
160 THROW(arrayindexoutofboundsexception); \
164 #define CHECK_ZERO_DIVISOR(_divisor) \
165 { if (_divisor == 0) \
166 THROW(arithmeticexception); \
170 /* !! alignment bug */
171 #define access_local_long(_offset) \
172 ( *(s8 *)(((u1 *)fp) + (_offset)) )
175 #define length_array(array) \
176 ( ((java_arrayheader*)(array))->size )
178 #define access_array_int(array, index) \
179 ((((java_intarray*)(array))->data)[index])
181 #define access_array_long(array, index) \
182 ((((java_longarray*)(array))->data)[index])
184 #define access_array_char(array, index) \
185 ((((java_chararray*)(array))->data)[index])
187 #define access_array_short(array, index) \
188 ((((java_shortarray*)(array))->data)[index])
190 #define access_array_byte(array, index) \
191 ((((java_bytearray*)(array))->data)[index])
193 #define access_array_addr(array, index) \
194 ((((java_objectarray*)(array))->data)[index])
196 #define access_array_float(array, index) \
197 ((((java_floatarray*)(array))->data)[index])
199 /* (see createcompilerstub in codegen.c) */
200 #define FRAMESIZE(stub) (((Cell *)stub)[1])
203 #define CLEARSTACK(_start, _end) \
204 do {Cell *__start=(_start); MSET(__start,0,u1,(_end)-__start); } while (0)
206 #define CLEARSTACK(_start, _end)
211 #define NAME(_x) if (vm_debug) {fprintf(vm_out, "%lx: %-20s, ", (long)(ip-1), _x); fprintf(vm_out,"fp=%p, sp=%p", fp, sp);}
216 #define LABEL2(name) J_##name: __asm__("");
217 #define LABEL3(name) K_##name: __asm__("");
221 engine(Inst *ip0, Cell * sp0, Cell * fp)
224 register Cell *sp SPREG = sp0;
225 Inst ca1; /* code address; this is the next dispatched instruction */
226 IF_spTOS(register Cell spTOS TOSREG;)
227 static Inst labels[] = {
228 #define INST_ADDR(_inst) (&&I_##_inst)
229 #include "java-labels.i"
232 #define INST_ADDR(_inst) (&&J_##_inst)
233 #include "java-labels.i"
235 #define INST_ADDR(_inst) (&&K_##_inst)
236 #include "java-labels.i"
239 (Label)&&before_goto,
241 #define INST_ADDR(_inst) (&&H_##_inst)
242 #include "java-labels.i"
245 /* local variables for the various throw codes; this helps make
246 potentially throwing instructions relocatable (instead of a
247 non-relocatable direct jump, they perform an indirect jump) */
248 Label throw_arithmeticexception = &&throw_arithmeticexception1;
249 Label throw_arrayindexoutofboundsexception = &&throw_arrayindexoutofboundsexception1;
250 Label throw_classcastexception = &&throw_classcastexception1;
251 Label throw_nullpointerexception = &&throw_nullpointerexception1;
252 Label throw_arraystoreexception = &&throw_arraystoreexception1;
253 java_objectheader *classcastexception_object;
254 s4 arrayindexoutofbounds_index; /* pass the index to the throw code */
257 fprintf(vm_out,"entering engine(%p,%p,%p)\n",ip0,sp,fp);
259 return (java_objectheader *)labels;
266 /* ensure that gcc does not constant-propagate the contents of
267 these variables and thus undo our relocatability work */
268 throw_arithmeticexception = 0;
269 throw_arrayindexoutofboundsexception = 0;
270 throw_classcastexception = 0;
271 throw_nullpointerexception = 0;
272 throw_arraystoreexception = 0;
274 /* the actual codes jumped to through the ...exception variables */
275 THROWCODE(arithmeticexception);
276 THROWCODE(nullpointerexception);
277 THROWCODE(arraystoreexception);
279 throw_classcastexception1:
281 *exceptionptr = stacktrace_inline_classcastexception(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP, classcastexception_object);
285 throw_arrayindexoutofboundsexception1:
287 *exceptionptr = stacktrace_inline_arrayindexoutofboundsexception(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP, arrayindexoutofbounds_index);
292 /* I don't have a clue where these things come from,
293 but I've put them in macros.h for the moment */
294 IF_spTOS(spTOS = sp[0]);
299 #define INST_ADDR(_inst) (&&I_##_inst)
302 after_last: return NULL;
307 * These are local overrides for various environment variables in Emacs.
308 * Please do not remove this and leave it at the end of the file, where
309 * Emacs will automagically detect them.
310 * ---------------------------------------------------------------------
313 * indent-tabs-mode: t