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