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