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