* Removed all Id tags.
[cacao.git] / src / vm / jit / intrp / asmpart.c
1 /* src/vm/jit/intrp/asmpart.c - Java-C interface functions for Interpreter
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 #include "config.h"
29
30 #include <assert.h>
31
32 #include "vm/types.h"
33
34 #include "arch.h"
35
36 #if defined(ENABLE_THREADS)
37 # include "threads/native/threads.h"
38 #else
39 # include "threads/none/threads.h"
40 #endif
41
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/methodheader.h"
47 #include "vm/jit/intrp/intrp.h"
48 #include "vm/jit/dseg.h"
49
50 #include "vmcore/class.h"
51 #include "vmcore/linker.h"
52 #include "vmcore/loader.h"
53 #include "vmcore/options.h"
54
55 #if defined(ENABLE_VMLOG)
56 #include <vmlog_cacao.h>
57 #endif
58
59
60 static bool intrp_asm_vm_call_method_intern(methodinfo *m, s4 vmargscount,
61                                                                                         vm_arg *vmargs)
62 {
63         java_objectheader *retval;
64         Cell              *sp;
65         s4                 i;
66         u1                *entrypoint;
67
68         sp = global_sp;
69         CLEAR_global_sp;
70
71         assert(sp != NULL);
72
73         for (i = 0; i < vmargscount; i++) {
74                 switch (vmargs[i].type) {
75                 case TYPE_INT:
76                 case TYPE_FLT:
77                 case TYPE_ADR:
78                         *(--sp) = (Cell) vmargs[i].data.l;
79                         break;
80                 case TYPE_LNG:
81                 case TYPE_DBL:
82                         sp -= 2;
83                         *((u8 *) sp) = vmargs[i].data.l;
84                         break;
85                 }
86         }
87
88         entrypoint = createcalljavafunction(m);
89
90         retval = engine((Inst *) entrypoint, sp, NULL);
91
92         /* XXX remove the method from the method table */
93
94         if (retval != NULL) {
95                 (void)builtin_throw_exception(retval);
96                 return false;
97         }
98         else
99                 return true;
100 }
101
102
103 java_objectheader *intrp_asm_vm_call_method(methodinfo *m, s4 vmargscount,
104                                                                                         vm_arg *vmargs)
105 {
106         java_objectheader *retval = NULL;
107
108         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
109                 if (m->parseddesc->returntype.type == TYPE_ADR)
110                         retval = (java_objectheader *)*global_sp++;
111                 else
112                         assert(m->parseddesc->returntype.type == TYPE_VOID);
113                 return retval;
114         } else
115                 return NULL;
116 }
117
118
119 s4 intrp_asm_vm_call_method_int(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
120 {
121         s4 retval = 0;
122
123         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
124                 if (m->parseddesc->returntype.type == TYPE_INT)
125                         retval = *global_sp++;
126                 else
127                         assert(m->parseddesc->returntype.type == TYPE_VOID);
128                 return retval;
129         } else
130                 return 0;
131 }
132
133
134 s8 intrp_asm_vm_call_method_long(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
135 {
136         s8 retval;
137
138         assert(m->parseddesc->returntype.type == TYPE_LNG);
139
140         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
141                 retval = *(s8 *)global_sp;
142                 global_sp += 2;
143                 return retval;
144         } else
145                 return 0;
146 }
147
148
149 float intrp_asm_vm_call_method_float(methodinfo *m, s4 vmargscount,
150                                                                          vm_arg *vmargs)
151 {
152         float retval;
153
154         assert(m->parseddesc->returntype.type == TYPE_FLT);
155
156         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
157                 retval = *(float *)global_sp;
158                 global_sp += 1;
159                 return retval;
160         } else
161                 return 0.0;
162 }
163
164
165 double intrp_asm_vm_call_method_double(methodinfo *m, s4 vmargscount,
166                                                                            vm_arg *vmargs)
167 {
168         double retval;
169
170         assert(m->parseddesc->returntype.type == TYPE_DBL);
171
172         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
173                 retval = *(double *)global_sp;
174                 global_sp += 2;
175                 return retval;
176         } else
177                 return 0.0;
178 }
179
180
181 Inst *intrp_asm_handle_exception(Inst *ip, java_objectheader *o, Cell *fp, Cell **new_spp, Cell **new_fpp)
182 {
183         classinfo            *c;
184         classref_or_classinfo cr;
185         s4                    framesize;
186         s4                    issync;
187         dseg_exception_entry *ex;
188         s4                    exceptiontablelength;
189         s4                    i;
190
191   /* for a description of the stack see IRETURN in java.vmg */
192
193   for (; fp != NULL; ) {
194           u1 *f = codegen_get_pv_from_pc((u1 *) (ip - 1));
195
196           /* get methodinfo pointer from method header */
197
198           codeinfo *code = *((codeinfo **) ((u1 *)f + CodeinfoPointer));
199           methodinfo *m = code->m;
200
201           framesize            = *((s4 *)             (((u1 *) f) + FrameSize));
202           issync               = *((s4 *)             (((u1 *) f) + IsSync));
203           ex                   =   (dseg_exception_entry *) 
204                                                                                                   (((u1 *) f) + ExTableStart);
205           exceptiontablelength = *((s4 *)             (((u1 *) f) + ExTableSize));
206
207 #if !defined(NDEBUG)
208           if (opt_verbose || opt_verbosecall || opt_verboseexception)
209                   builtin_trace_exception(o, m, ip, 1);
210 #endif
211
212 #if defined(ENABLE_VMLOG)
213           vmlog_cacao_throw(o);
214 #endif
215
216           for (i = 0; i < exceptiontablelength; i++) {
217                   ex--;
218
219                   cr = ex->catchtype;
220
221                   if (cr.any == NULL) {
222                           /* catch all */
223                           c = NULL;
224                   }
225                   else {
226                           if (IS_CLASSREF(cr)) {
227                                   /* The exception class reference is unresolved. */
228                                   /* We have to do _eager_ resolving here. While the class of */
229                                   /* the exception object is guaranteed to be loaded, it may  */
230                                   /* well have been loaded by a different loader than the     */
231                                   /* defining loader of m's class, which is the one we must   */
232                                   /* use to resolve the catch class. Thus lazy resolving      */
233                                   /* might fail, even if the result of the resolution would   */
234                                   /* be an already loaded class.                              */
235
236                                   /* The resolving may involve Java code, so we need a usable */
237                                   /* global_sp. XXX is this a correct value for global_sp?    */
238
239                                   global_sp = (Cell *)(((u1 *)fp) - framesize - SIZEOF_VOID_P);
240
241                                   c = resolve_classref_eager(cr.ref);
242
243                                   CLEAR_global_sp;
244
245                                   if (c == NULL) {
246                                           /* Exception resolving the exception class, argh! */
247                                           /* XXX how to report that error? */
248                                           assert(0);
249                                   }
250
251                                   /* Ok, we resolved it. Enter it in the table, so we don't */
252                                   /* have to do this again.                                 */
253                                   /* XXX this write should be atomic. Is it?                */
254
255                                   ex->catchtype.cls = c;
256                           }
257                           else {
258                                   c = cr.cls;
259                                 
260                                   /* If the class is not linked, the exception object cannot */
261                                   /* be an instance of it.                                   */
262                                   if (!(c->state & CLASS_LINKED))
263                                           continue;
264                           }
265                   }
266
267                   if (ip-1 >= (Inst *) ex->startpc && ip-1 < (Inst *) ex->endpc &&
268                           (c == NULL || builtin_instanceof(o, c))) 
269                   {
270 #if defined(ENABLE_VMLOG)
271                           vmlog_cacao_catch(o);
272 #endif
273
274                           *new_spp = (Cell *)(((u1 *)fp) - framesize - SIZEOF_VOID_P);
275                           *new_fpp = fp;
276                           return (Inst *) (ex->handlerpc);
277                   }
278           }
279
280 #if defined(ENABLE_THREADS)
281           /* is this method synchronized? */
282
283           if (issync) {
284                   java_objectheader *syncobj;
285
286                   /* get synchronization object */
287
288                   if (m->flags & ACC_STATIC) {
289                           syncobj = (java_objectheader *) m->class;
290                   }
291                   else {
292                           syncobj = (java_objectheader *) access_local_cell(-framesize + SIZEOF_VOID_P);
293                   }
294
295                   assert(syncobj != NULL);
296
297                   lock_monitor_exit(syncobj);
298           }
299 #endif /* defined(ENABLE_THREADS) */
300
301           /* unwind stack frame */
302
303 #if defined(ENABLE_VMLOG)
304           vmlog_cacao_unwnd_method(m);
305 #endif
306
307           ip = (Inst *)access_local_cell(-framesize - SIZEOF_VOID_P);
308           fp = (Cell *)access_local_cell(-framesize);
309   }
310
311   return NULL; 
312 }
313
314
315 void intrp_asm_abstractmethoderror(void)
316 {
317         vm_abort("intrp_asm_abstractmethoderror: IMPLEMENT ME!");
318 }
319
320
321 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out)
322 {
323         s4 sbv, sdv, sv;
324
325         LOCK_MONITOR_ENTER(linker_classrenumber_lock);
326
327         sbv = super->baseval;
328         sdv = super->diffval;
329         sv  = sub->baseval;
330
331         LOCK_MONITOR_EXIT(linker_classrenumber_lock);
332
333         out->super_baseval = sbv;
334         out->super_diffval = sdv;
335         out->sub_baseval   = sv;
336 }
337
338
339 /*
340  * These are local overrides for various environment variables in Emacs.
341  * Please do not remove this and leave it at the end of the file, where
342  * Emacs will automagically detect them.
343  * ---------------------------------------------------------------------
344  * Local variables:
345  * mode: c
346  * indent-tabs-mode: t
347  * c-basic-offset: 4
348  * tab-width: 4
349  * End:
350  * vim:noexpandtab:sw=4:ts=4:
351  */