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