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