1 /* src/vm/jit/intrp/engine.c - #included by engine1.c and engine2.c
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 /* #define VM_DEBUG */
36 #include "vm/jit/intrp/intrp.h"
38 #include "md-abi.h" /* required for TRACE_ARGS_NUM */
40 #include "mm/memory.h"
42 #include "threads/thread.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
47 #include "vm/jit/methodheader.h"
48 #include "vm/jit/patcher.h"
49 #include "vm/jit/stacktrace.h"
51 #include "vmcore/loader.h"
52 #include "vmcore/options.h"
55 #if defined(ENABLE_THREADS)
56 # ifndef USE_FAKE_ATOMIC_INSTRUCTIONS
57 # include "machine-instr.h"
59 # include "threads/posix/generic-primitives.h"
63 #if !defined(STORE_ORDER_BARRIER) && !defined(ENABLE_THREADS)
64 #define STORE_ORDER_BARRIER() /* nothing */
68 /* threading macros */
69 #define GCC_PR15242_WORKAROUND
70 #ifdef GCC_PR15242_WORKAROUND
72 # define DO_GOTO goto before_goto
75 # define DO_GOTO goto *ip[-1]
80 # define SET_IP(p) do {ip=(p); NEXT_P0;} while (0)
81 # define NEXT_INST (*IP)
82 # define INC_IP(const_inc) do { ip+=(const_inc);} while (0)
84 # define NEXT_P1 (ip++)
85 # define NEXT_P2 do {NEXT_P1_5; DO_GOTO;} while(0)
87 #define NEXT ({DEF_CA NEXT_P1; NEXT_P2;})
88 #define IPTOS NEXT_INST
90 #if defined(__POWERPC__) || defined(__POWERPC64__) || defined(__SPARC__)
94 #if defined(USE_spTOS)
101 #if defined(__I386__)
102 /* works with gcc-2.95.4 20011002 (Debian prerelease) without static supers */
103 #define SPREG /* __asm__("%esi") */
104 #define TOSREG /* __asm__("%ecx") */
105 #elif defined(__X86_64__)
106 /* works with gcc-4.0.2 (Debian 4.0.2-2) */
107 #define SPREG /* __asm__("%r15") */
115 /* conversion on fetch */
118 #define SUPER_END vm_count_block(IP)
121 #define vm_uncount_block(_ip) /* nothing */
125 #define THROW0 goto throw
127 #define THROW(_ball) do { \
128 __asm__(""); /* work around gcc PR 25285 */ \
129 goto *throw_##_ball; \
132 #define THROW(_ball) do { \
133 goto throw_##_ball##1; \
137 #define THROWCODE(_ball) \
140 *exceptionptr = (stacktrace_inline_##_ball(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP)); \
144 #define CHECK_NULL_PTR(ptr) \
146 if ((ptr) == NULL) { \
147 THROW(nullpointerexception); \
151 #define THROW_CLASSCASTEXCEPTION(o) \
153 classcastexception_object = o; \
154 THROW(classcastexception); \
157 #define CHECK_OUT_OF_BOUNDS(_array, _idx) \
159 if (length_array(_array) <= (u4) (_idx)) { \
160 arrayindexoutofbounds_index = (_idx); \
161 THROW(arrayindexoutofboundsexception); \
165 #define CHECK_ZERO_DIVISOR(_divisor) \
166 { if (_divisor == 0) \
167 THROW(arithmeticexception); \
171 /* !! alignment bug */
172 #define access_local_long(_offset) \
173 ( *(s8 *)(((u1 *)fp) + (_offset)) )
176 #define length_array(array) \
177 ( ((java_arrayheader*)(array))->size )
179 #define access_array_int(array, index) \
180 ((((java_intarray*)(array))->data)[index])
182 #define access_array_long(array, index) \
183 ((((java_longarray*)(array))->data)[index])
185 #define access_array_char(array, index) \
186 ((((java_chararray*)(array))->data)[index])
188 #define access_array_short(array, index) \
189 ((((java_shortarray*)(array))->data)[index])
191 #define access_array_byte(array, index) \
192 ((((java_bytearray*)(array))->data)[index])
194 #define access_array_addr(array, index) \
195 ((((java_objectarray*)(array))->data)[index])
197 #define access_array_float(array, index) \
198 ((((java_floatarray*)(array))->data)[index])
200 /* (see createcompilerstub in codegen.c) */
201 #define FRAMESIZE(stub) (((Cell *)stub)[1])
204 #define CLEARSTACK(_start, _end) \
205 do {Cell *__start=(_start); MSET(__start,0,u1,(_end)-__start); } while (0)
207 #define CLEARSTACK(_start, _end)
212 #define NAME(_x) if (vm_debug) {fprintf(vm_out, "%lx: %-20s, ", (long)(ip-1), _x); fprintf(vm_out,"fp=%p, sp=%p", fp, sp);}
217 #define LABEL2(name) J_##name: __asm__("");
218 #define LABEL3(name) K_##name: __asm__("");
222 engine(Inst *ip0, Cell * sp0, Cell * fp)
225 register Cell *sp SPREG = sp0;
226 /* Inst ca1; XXX unused? */ /* code address; this is the next dispatched instruction */
227 IF_spTOS(register Cell spTOS TOSREG;)
228 static Inst labels[] = {
229 #define INST_ADDR(_inst) (&&I_##_inst)
230 #include <java-labels.i>
233 #define INST_ADDR(_inst) (&&J_##_inst)
234 #include <java-labels.i>
236 #define INST_ADDR(_inst) (&&K_##_inst)
237 #include <java-labels.i>
240 (Label)&&before_goto,
242 #define INST_ADDR(_inst) (&&H_##_inst)
243 #include <java-labels.i>
246 /* local variables for the various throw codes; this helps make
247 potentially throwing instructions relocatable (instead of a
248 non-relocatable direct jump, they perform an indirect jump) */
249 Label throw_arithmeticexception = &&throw_arithmeticexception1;
250 Label throw_arrayindexoutofboundsexception = &&throw_arrayindexoutofboundsexception1;
251 Label throw_classcastexception = &&throw_classcastexception1;
252 Label throw_nullpointerexception = &&throw_nullpointerexception1;
253 Label throw_arraystoreexception = &&throw_arraystoreexception1;
254 java_objectheader *classcastexception_object = NULL;
255 s4 arrayindexoutofbounds_index = 0; /* pass the index to the throw code */
258 fprintf(vm_out,"entering engine(%p,%p,%p)\n",ip0,sp,fp);
260 return (java_objectheader *)labels;
267 /* ensure that gcc does not constant-propagate the contents of
268 these variables and thus undo our relocatability work */
269 throw_arithmeticexception = 0;
270 throw_arrayindexoutofboundsexception = 0;
271 throw_classcastexception = 0;
272 throw_nullpointerexception = 0;
273 throw_arraystoreexception = 0;
275 /* the actual codes jumped to through the ...exception variables */
276 THROWCODE(arithmeticexception);
277 THROWCODE(nullpointerexception);
278 THROWCODE(arraystoreexception);
280 throw_classcastexception1:
282 *exceptionptr = stacktrace_inline_classcastexception(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP, classcastexception_object);
286 throw_arrayindexoutofboundsexception1:
288 *exceptionptr = stacktrace_inline_arrayindexoutofboundsexception(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP, arrayindexoutofbounds_index);
293 /* I don't have a clue where these things come from,
294 but I've put them in macros.h for the moment */
295 IF_spTOS(spTOS = sp[0]);
300 #define INST_ADDR(_inst) (&&I_##_inst)
303 after_last: return NULL;
308 * These are local overrides for various environment variables in Emacs.
309 * Please do not remove this and leave it at the end of the file, where
310 * Emacs will automagically detect them.
311 * ---------------------------------------------------------------------
314 * indent-tabs-mode: t
318 * vim:noexpandtab:sw=4:ts=4: