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