* Removed all Id tags.
[cacao.git] / src / vm / jit / intrp / engine.c
1 /* src/vm/jit/intrp/engine.c - #included by engine1.c and engine2.c
2
3    Copyright (C) 1996-2005, 2006, 2007 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 */
26
27
28 /* #define VM_DEBUG */
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "arch.h"
37
38 #include "vm/jit/intrp/intrp.h"
39
40 #include "md-abi.h"                           /* required for TRACE_ARGS_NUM */
41
42 #include "mm/memory.h"
43
44 #if defined(ENABLE_THREADS)
45 # include "threads/native/threads.h"
46 #else
47 # include "threads/none/threads.h"
48 #endif
49
50 #include "vm/builtin.h"
51 #include "vm/exceptions.h"
52
53 #include "vm/jit/methodheader.h"
54 #include "vm/jit/patcher.h"
55 #include "vm/jit/stacktrace.h"
56
57 #include "vmcore/loader.h"
58 #include "vmcore/options.h"
59
60
61 #if defined(ENABLE_THREADS)
62 # ifndef USE_FAKE_ATOMIC_INSTRUCTIONS
63 #  include "machine-instr.h"
64 # else
65 #  include "threads/native/generic-primitives.h"
66 # endif
67 #endif
68
69 #if !defined(STORE_ORDER_BARRIER) && !defined(ENABLE_THREADS)
70 #define STORE_ORDER_BARRIER() /* nothing */
71 #endif
72
73
74 /* threading macros */
75 #define GCC_PR15242_WORKAROUND
76 #ifdef GCC_PR15242_WORKAROUND
77 #  define NEXT_P1_5
78 #  define DO_GOTO goto before_goto
79 #else
80 #  define NEXT_P1_5
81 #  define DO_GOTO goto *ip[-1]
82 #endif
83
84 #  define NEXT_P0
85 #  define IP            (ip)
86 #  define SET_IP(p)     do {ip=(p); NEXT_P0;} while (0)
87 #  define NEXT_INST     (*IP)
88 #  define INC_IP(const_inc)     do { ip+=(const_inc);} while (0)
89 #  define DEF_CA
90 #  define NEXT_P1       (ip++)
91 #  define NEXT_P2   do {NEXT_P1_5; DO_GOTO;} while(0)
92
93 #define NEXT ({DEF_CA NEXT_P1; NEXT_P2;})
94 #define IPTOS NEXT_INST
95
96 #if defined(__POWERPC__) || defined(__POWERPC64__) || defined(__SPARC__)
97 # define USE_spTOS
98 #endif
99
100 #if defined(USE_spTOS)
101 #define IF_spTOS(x) x
102 #else
103 #define IF_spTOS(x)
104 #define spTOS (sp[0])
105 #endif
106
107 #if defined(__I386__)
108 /* works with gcc-2.95.4 20011002 (Debian prerelease) without static supers */
109 #define SPREG /* __asm__("%esi") */
110 #define TOSREG /* __asm__("%ecx") */
111 #elif defined(__X86_64__)
112 /* works with gcc-4.0.2 (Debian 4.0.2-2) */
113 #define SPREG /* __asm__("%r15") */
114 #define TOSREG
115 #else
116 #define SPREG
117 #define TOSREG
118 #endif
119
120
121 /* conversion on fetch */
122
123 #ifdef VM_PROFILING
124 #define SUPER_END  vm_count_block(IP)
125 #else
126 #define SUPER_END
127 #define vm_uncount_block(_ip)   /* nothing */
128 #endif
129
130
131 #define THROW0       goto throw
132 #if 1
133 #define THROW(_ball) do { \
134                        __asm__(""); /* work around gcc PR 25285 */ \
135                        goto *throw_##_ball; \
136                      } while (0)
137 #else
138 #define THROW(_ball) do { \
139                        goto throw_##_ball##1; \
140                      } while (0)
141 #endif
142
143 #define THROWCODE(_ball) \
144     throw_##_ball##1: \
145         global_sp = sp; \
146         *exceptionptr = (stacktrace_inline_##_ball(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP)); \
147         CLEAR_global_sp; \
148         THROW0;
149
150 #define CHECK_NULL_PTR(ptr) \
151     { \
152         if ((ptr) == NULL) { \
153             THROW(nullpointerexception); \
154             } \
155         }
156
157 #define THROW_CLASSCASTEXCEPTION(o) \
158     { \
159                 classcastexception_object = o; \
160         THROW(classcastexception); \
161         }
162
163 #define CHECK_OUT_OF_BOUNDS(_array, _idx)              \
164         {                                            \
165           if (length_array(_array) <= (u4) (_idx)) { \
166                 arrayindexoutofbounds_index = (_idx); \
167                                 THROW(arrayindexoutofboundsexception); \
168           } \
169         }
170
171 #define CHECK_ZERO_DIVISOR(_divisor) \
172   { if (_divisor == 0) \
173       THROW(arithmeticexception); \
174   } 
175
176 #if 0
177 /* !! alignment bug */
178 #define access_local_long(_offset) \
179         ( *(s8 *)(((u1 *)fp) + (_offset)) )
180 #endif
181
182 #define length_array(array)                          \
183         ( ((java_arrayheader*)(array))->size )
184
185 #define access_array_int(array, index)               \
186         ((((java_intarray*)(array))->data)[index])
187
188 #define access_array_long(array, index)               \
189         ((((java_longarray*)(array))->data)[index])
190
191 #define access_array_char(array, index)               \
192         ((((java_chararray*)(array))->data)[index])
193
194 #define access_array_short(array, index)               \
195         ((((java_shortarray*)(array))->data)[index])
196
197 #define access_array_byte(array, index)               \
198         ((((java_bytearray*)(array))->data)[index])
199
200 #define access_array_addr(array, index)               \
201         ((((java_objectarray*)(array))->data)[index])
202
203 #define access_array_float(array, index)               \
204         ((((java_floatarray*)(array))->data)[index])
205
206 /* (see createcompilerstub in codegen.c) */
207 #define FRAMESIZE(stub) (((Cell *)stub)[1])
208
209 #if 0
210 #define CLEARSTACK(_start, _end) \
211          do {Cell *__start=(_start); MSET(__start,0,u1,(_end)-__start); } while (0)
212 #else
213 #define CLEARSTACK(_start, _end)
214 #endif
215
216
217 #ifdef VM_DEBUG
218 #define NAME(_x) if (vm_debug) {fprintf(vm_out, "%lx: %-20s, ", (long)(ip-1), _x); fprintf(vm_out,"fp=%p, sp=%p", fp, sp);}
219 #else
220 #define NAME(_x)
221 #endif
222
223 #define LABEL2(name) J_##name: __asm__("");
224 #define LABEL3(name) K_##name: __asm__("");
225
226
227 java_objectheader *
228 engine(Inst *ip0, Cell * sp0, Cell * fp)
229 {
230   Inst *ip;
231   register Cell *sp SPREG = sp0;
232   /* Inst ca1; XXX unused? */ /* code address; this is the next dispatched instruction */
233   IF_spTOS(register Cell spTOS TOSREG;)
234   static Inst   labels[] = {
235 #define INST_ADDR(_inst) (&&I_##_inst)
236 #include <java-labels.i>
237 #undef INST_ADDR
238           NULL,
239 #define INST_ADDR(_inst) (&&J_##_inst)
240 #include <java-labels.i>
241 #undef INST_ADDR
242 #define INST_ADDR(_inst) (&&K_##_inst)
243 #include <java-labels.i>
244 #undef INST_ADDR
245     (Label)&&after_last,
246     (Label)&&before_goto,
247     (Label)&&after_goto,
248 #define INST_ADDR(_inst) (&&H_##_inst)
249 #include <java-labels.i>
250 #undef INST_ADDR
251   };
252   /* local variables for the various throw codes; this helps make
253          potentially throwing instructions relocatable (instead of a
254          non-relocatable direct jump, they perform an indirect jump) */
255   Label throw_arithmeticexception            = &&throw_arithmeticexception1;
256   Label throw_arrayindexoutofboundsexception = &&throw_arrayindexoutofboundsexception1;
257   Label throw_classcastexception                         = &&throw_classcastexception1;  
258   Label throw_nullpointerexception                   = &&throw_nullpointerexception1;
259   Label throw_arraystoreexception            = &&throw_arraystoreexception1;
260   java_objectheader *classcastexception_object = NULL;
261   s4 arrayindexoutofbounds_index = 0; /* pass the index to the throw code */
262
263   if (vm_debug)
264       fprintf(vm_out,"entering engine(%p,%p,%p)\n",ip0,sp,fp);
265   if (ip0 == NULL) {
266     return (java_objectheader *)labels;
267   }
268
269   if (0) {
270   before_goto:
271           goto *ip[-1];
272   after_goto:
273           /* ensure that gcc does not constant-propagate the contents of
274                  these variables and thus undo our relocatability work */
275           throw_arithmeticexception = 0;
276           throw_arrayindexoutofboundsexception = 0;
277           throw_classcastexception = 0;
278           throw_nullpointerexception = 0;
279           throw_arraystoreexception = 0;
280
281       /* the actual codes jumped to through the ...exception variables */
282           THROWCODE(arithmeticexception);
283           THROWCODE(nullpointerexception);
284           THROWCODE(arraystoreexception);
285
286   throw_classcastexception1:
287           global_sp = sp;
288           *exceptionptr = stacktrace_inline_classcastexception(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP, classcastexception_object);
289           CLEAR_global_sp;
290           THROW0;
291
292   throw_arrayindexoutofboundsexception1:
293           global_sp = sp;
294           *exceptionptr = stacktrace_inline_arrayindexoutofboundsexception(NULL, (u1 *) fp, (u1 *) IP, (u1 *) IP, arrayindexoutofbounds_index);
295           CLEAR_global_sp;
296           THROW0;
297   }
298
299   /* I don't have a clue where these things come from,
300      but I've put them in macros.h for the moment */
301   IF_spTOS(spTOS = sp[0]);
302
303   SET_IP(ip0);
304   NEXT;
305
306 #define INST_ADDR(_inst) (&&I_##_inst)
307 #include <java-vm.i>
308 #undef NAME
309  after_last: return NULL;
310 }
311
312
313 /*
314  * These are local overrides for various environment variables in Emacs.
315  * Please do not remove this and leave it at the end of the file, where
316  * Emacs will automagically detect them.
317  * ---------------------------------------------------------------------
318  * Local variables:
319  * mode: c
320  * indent-tabs-mode: t
321  * c-basic-offset: 4
322  * tab-width: 4
323  * End:
324  * vim:noexpandtab:sw=4:ts=4:
325  */