Merged trunk and subtype.
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29
30 #include "vm/types.h"
31
32 #include "arch.h"
33
34 #include "threads/thread.hpp"
35
36 #include "vm/jit/builtin.hpp"
37 #include "vm/class.h"
38 #include "vm/linker.h"
39 #include "vm/loader.hpp"
40 #include "vm/options.h"
41
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/methodheader.h"
44 #include "vm/jit/methodtree.h"
45 #include "vm/jit/dseg.h"
46
47 #include "vm/jit/intrp/intrp.h"
48
49 #if defined(ENABLE_VMLOG)
50 #include <vmlog_cacao.h>
51 #endif
52
53
54 static bool intrp_asm_vm_call_method_intern(methodinfo *m, s4 vmargscount,
55                                                                                         vm_arg *vmargs)
56 {
57         java_objectheader *retval;
58         Cell              *sp;
59         s4                 i;
60         u1                *entrypoint;
61
62         sp = global_sp;
63         CLEAR_global_sp;
64
65         assert(sp != NULL);
66
67         for (i = 0; i < vmargscount; i++) {
68                 switch (vmargs[i].type) {
69                 case TYPE_INT:
70                 case TYPE_FLT:
71                 case TYPE_ADR:
72                         *(--sp) = (Cell) vmargs[i].data.l;
73                         break;
74                 case TYPE_LNG:
75                 case TYPE_DBL:
76                         sp -= 2;
77                         *((u8 *) sp) = vmargs[i].data.l;
78                         break;
79                 }
80         }
81
82         entrypoint = createcalljavafunction(m);
83
84         retval = engine((Inst *) entrypoint, sp, NULL);
85
86         /* XXX remove the method from the method table */
87
88         if (retval != NULL) {
89                 (void)builtin_throw_exception(retval);
90                 return false;
91         }
92         else
93                 return true;
94 }
95
96
97 java_objectheader *intrp_asm_vm_call_method(methodinfo *m, s4 vmargscount,
98                                                                                         vm_arg *vmargs)
99 {
100         java_objectheader *retval = NULL;
101
102         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
103                 if (m->parseddesc->returntype.type == TYPE_ADR)
104                         retval = (java_objectheader *)*global_sp++;
105                 else
106                         assert(m->parseddesc->returntype.type == TYPE_VOID);
107                 return retval;
108         } else
109                 return NULL;
110 }
111
112
113 s4 intrp_asm_vm_call_method_int(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
114 {
115         s4 retval = 0;
116
117         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
118                 if (m->parseddesc->returntype.type == TYPE_INT)
119                         retval = *global_sp++;
120                 else
121                         assert(m->parseddesc->returntype.type == TYPE_VOID);
122                 return retval;
123         } else
124                 return 0;
125 }
126
127
128 s8 intrp_asm_vm_call_method_long(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
129 {
130         s8 retval;
131
132         assert(m->parseddesc->returntype.type == TYPE_LNG);
133
134         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
135                 retval = *(s8 *)global_sp;
136                 global_sp += 2;
137                 return retval;
138         } else
139                 return 0;
140 }
141
142
143 float intrp_asm_vm_call_method_float(methodinfo *m, s4 vmargscount,
144                                                                          vm_arg *vmargs)
145 {
146         float retval;
147
148         assert(m->parseddesc->returntype.type == TYPE_FLT);
149
150         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
151                 retval = *(float *)global_sp;
152                 global_sp += 1;
153                 return retval;
154         } else
155                 return 0.0;
156 }
157
158
159 double intrp_asm_vm_call_method_double(methodinfo *m, s4 vmargscount,
160                                                                            vm_arg *vmargs)
161 {
162         double retval;
163
164         assert(m->parseddesc->returntype.type == TYPE_DBL);
165
166         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
167                 retval = *(double *)global_sp;
168                 global_sp += 2;
169                 return retval;
170         } else
171                 return 0.0;
172 }
173
174
175 Inst *intrp_asm_handle_exception(Inst *ip, java_objectheader *o, Cell *fp, Cell **new_spp, Cell **new_fpp)
176 {
177         classinfo            *c;
178         classref_or_classinfo cr;
179         s4                    framesize;
180         s4                    issync;
181         dseg_exception_entry *ex;
182         s4                    exceptiontablelength;
183         s4                    i;
184
185   /* for a description of the stack see IRETURN in java.vmg */
186
187   for (; fp != NULL; ) {
188           u1 *f = methodtree_find((u1 *) (ip - 1));
189
190           /* get methodinfo pointer from method header */
191
192           codeinfo *code = *((codeinfo **) ((u1 *)f + CodeinfoPointer));
193           methodinfo *m = code->m;
194
195           framesize            = *((s4 *)             (((u1 *) f) + FrameSize));
196           issync               = *((s4 *)             (((u1 *) f) + IsSync));
197           ex                   =   (dseg_exception_entry *) 
198                                                                                                   (((u1 *) f) + ExTableStart);
199           exceptiontablelength = *((s4 *)             (((u1 *) f) + ExTableSize));
200
201 #if !defined(NDEBUG)
202           if (opt_verbose || opt_verbosecall || opt_verboseexception)
203                   builtin_trace_exception(o, m, ip, 1);
204 #endif
205
206 #if defined(ENABLE_VMLOG)
207           vmlog_cacao_throw(o);
208 #endif
209
210           for (i = 0; i < exceptiontablelength; i++) {
211                   ex--;
212
213                   cr = ex->catchtype;
214
215                   if (cr.any == NULL) {
216                           /* catch all */
217                           c = NULL;
218                   }
219                   else {
220                           if (IS_CLASSREF(cr)) {
221                                   /* The exception class reference is unresolved. */
222                                   /* We have to do _eager_ resolving here. While the class of */
223                                   /* the exception object is guaranteed to be loaded, it may  */
224                                   /* well have been loaded by a different loader than the     */
225                                   /* defining loader of m's class, which is the one we must   */
226                                   /* use to resolve the catch class. Thus lazy resolving      */
227                                   /* might fail, even if the result of the resolution would   */
228                                   /* be an already loaded class.                              */
229
230                                   /* The resolving may involve Java code, so we need a usable */
231                                   /* global_sp. XXX is this a correct value for global_sp?    */
232
233                                   global_sp = (Cell *)(((u1 *)fp) - framesize - SIZEOF_VOID_P);
234
235                                   c = resolve_classref_eager(cr.ref);
236
237                                   CLEAR_global_sp;
238
239                                   if (c == NULL) {
240                                           /* Exception resolving the exception class, argh! */
241                                           /* XXX how to report that error? */
242                                           assert(0);
243                                   }
244
245                                   /* Ok, we resolved it. Enter it in the table, so we don't */
246                                   /* have to do this again.                                 */
247                                   /* XXX this write should be atomic. Is it?                */
248
249                                   ex->catchtype.cls = c;
250                           }
251                           else {
252                                   c = cr.cls;
253                                 
254                                   /* If the class is not linked, the exception object cannot */
255                                   /* be an instance of it.                                   */
256                                   if (!(c->state & CLASS_LINKED))
257                                           continue;
258                           }
259                   }
260
261                   if (ip-1 >= (Inst *) ex->startpc && ip-1 < (Inst *) ex->endpc &&
262                           (c == NULL || builtin_instanceof(o, c))) 
263                   {
264 #if defined(ENABLE_VMLOG)
265                           vmlog_cacao_catch(o);
266 #endif
267
268                           *new_spp = (Cell *)(((u1 *)fp) - framesize - SIZEOF_VOID_P);
269                           *new_fpp = fp;
270                           return (Inst *) (ex->handlerpc);
271                   }
272           }
273
274 #if defined(ENABLE_THREADS)
275           /* is this method synchronized? */
276
277           if (issync) {
278                   java_objectheader *syncobj;
279
280                   /* get synchronization object */
281
282                   if (m->flags & ACC_STATIC) {
283                           syncobj = (java_objectheader *) m->clazz;
284                   }
285                   else {
286                           syncobj = (java_objectheader *) access_local_cell(-framesize + SIZEOF_VOID_P);
287                   }
288
289                   assert(syncobj != NULL);
290
291                   lock_monitor_exit(syncobj);
292           }
293 #endif /* defined(ENABLE_THREADS) */
294
295           /* unwind stack frame */
296
297 #if defined(ENABLE_VMLOG)
298           vmlog_cacao_unwnd_method(m);
299 #endif
300
301           ip = (Inst *)access_local_cell(-framesize - SIZEOF_VOID_P);
302           fp = (Cell *)access_local_cell(-framesize);
303   }
304
305   return NULL; 
306 }
307
308
309 void intrp_asm_abstractmethoderror(void)
310 {
311         vm_abort("intrp_asm_abstractmethoderror: IMPLEMENT ME!");
312 }
313
314
315 /*
316  * These are local overrides for various environment variables in Emacs.
317  * Please do not remove this and leave it at the end of the file, where
318  * Emacs will automagically detect them.
319  * ---------------------------------------------------------------------
320  * Local variables:
321  * mode: c
322  * indent-tabs-mode: t
323  * c-basic-offset: 4
324  * tab-width: 4
325  * End:
326  * vim:noexpandtab:sw=4:ts=4:
327  */