* src/vm/jit/intrp/engine.c (THROW_CLASSCASTEXCEPTION): Really throw a
[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 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: asmpart.c 5670 2006-10-04 18:23:19Z edwin $
33
34 */
35
36
37 #include "config.h"
38
39 #include <assert.h>
40
41 #include "vm/types.h"
42
43 #include "arch.h"
44
45 #include "vm/builtin.h"
46 #include "vm/class.h"
47 #include "vm/exceptions.h"
48 #include "vm/loader.h"
49 #include "vm/options.h"
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/methodheader.h"
52 #include "vm/jit/intrp/intrp.h"
53
54
55 static bool intrp_asm_vm_call_method_intern(methodinfo *m, s4 vmargscount,
56                                                                                         vm_arg *vmargs)
57 {
58         java_objectheader *retval;
59         Cell              *sp;
60         s4                 i;
61         u1                *entrypoint;
62
63         sp = global_sp;
64         CLEAR_global_sp;
65
66         assert(sp != NULL);
67
68         for (i = 0; i < vmargscount; i++) {
69                 switch (vmargs[i].type) {
70                 case TYPE_INT:
71                 case TYPE_FLT:
72                 case TYPE_ADR:
73                         *(--sp) = (Cell) vmargs[i].data.l;
74                         break;
75                 case TYPE_LNG:
76                 case TYPE_DBL:
77                         sp -= 2;
78                         *((u8 *) sp) = vmargs[i].data.l;
79                         break;
80                 }
81         }
82
83         entrypoint = createcalljavafunction(m);
84
85         retval = engine((Inst *) entrypoint, sp, NULL);
86
87         /* XXX remove the method from the method table */
88
89         if (retval != NULL) {
90                 (void)builtin_throw_exception(retval);
91                 return false;
92         }
93         else
94                 return true;
95 }
96
97
98 java_objectheader *intrp_asm_vm_call_method(methodinfo *m, s4 vmargscount,
99                                                                                         vm_arg *vmargs)
100 {
101         java_objectheader *retval = NULL;
102
103         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
104                 if (m->parseddesc->returntype.type == TYPE_ADR)
105                         retval = (java_objectheader *)*global_sp++;
106                 else
107                         assert(m->parseddesc->returntype.type == TYPE_VOID);
108                 return retval;
109         } else
110                 return NULL;
111 }
112
113
114 s4 intrp_asm_vm_call_method_int(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
115 {
116         s4 retval = 0;
117
118         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
119                 if (m->parseddesc->returntype.type == TYPE_INT)
120                         retval = *global_sp++;
121                 else
122                         assert(m->parseddesc->returntype.type == TYPE_VOID);
123                 return retval;
124         } else
125                 return 0;
126 }
127
128
129 s8 intrp_asm_vm_call_method_long(methodinfo *m, s4 vmargscount, vm_arg *vmargs)
130 {
131         s8 retval;
132
133         assert(m->parseddesc->returntype.type == TYPE_LNG);
134
135         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
136                 retval = *(s8 *)global_sp;
137                 global_sp += 2;
138                 return retval;
139         } else
140                 return 0;
141 }
142
143
144 float intrp_asm_vm_call_method_float(methodinfo *m, s4 vmargscount,
145                                                                          vm_arg *vmargs)
146 {
147         float retval;
148
149         assert(m->parseddesc->returntype.type == TYPE_FLT);
150
151         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
152                 retval = *(float *)global_sp;
153                 global_sp += 1;
154                 return retval;
155         } else
156                 return 0.0;
157 }
158
159
160 double intrp_asm_vm_call_method_double(methodinfo *m, s4 vmargscount,
161                                                                            vm_arg *vmargs)
162 {
163         double retval;
164
165         assert(m->parseddesc->returntype.type == TYPE_DBL);
166
167         if (intrp_asm_vm_call_method_intern(m, vmargscount, vmargs)) {
168                 retval = *(double *)global_sp;
169                 global_sp += 2;
170                 return retval;
171         } else
172                 return 0.0;
173 }
174
175
176 Inst *intrp_asm_handle_exception(Inst *ip, java_objectheader *o, Cell *fp, Cell **new_spp, Cell **new_fpp)
177 {
178         classinfo            *c;
179         classref_or_classinfo cr;
180         s4                    framesize;
181         exceptionentry       *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 = codegen_get_pv_from_pc((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           ex                   =   (exceptionentry *) (((u1 *) f) + ExTableStart);
197           exceptiontablelength = *((s4 *)             (((u1 *) f) + ExTableSize));
198
199 #if !defined(NDEBUG)
200           if (opt_verbose || opt_verbosecall || opt_verboseexception)
201                   builtin_trace_exception(o, m, ip, 1);
202 #endif
203
204           for (i = 0; i < exceptiontablelength; i++) {
205                   ex--;
206
207                   cr = ex->catchtype;
208
209                   if (cr.any != NULL) {
210                           if (IS_CLASSREF(cr)) {
211                                   /* The exception class reference is unresolved. */
212                                   /* We have to do _eager_ resolving here. While the class of */
213                                   /* the exception object is guaranteed to be loaded, it may  */
214                                   /* well have been loaded by a different loader than the     */
215                                   /* defining loader of m's class, which is the one we must   */
216                                   /* use to resolve the catch class. Thus lazy resolving      */
217                                   /* might fail, even if the result of the resolution would   */
218                                   /* be an already loaded class.                              */
219
220                                   /* The resolving may involve Java code, so we need a usable */
221                                   /* global_sp. XXX is this a correct value for global_sp?    */
222
223                                   global_sp = (Cell *)(((u1 *)fp) - framesize - SIZEOF_VOID_P);
224
225                                   c = resolve_classref_eager(cr.ref);
226
227                                   if (c == NULL) {
228                                           /* Exception resolving the exception class, argh! */
229                                           /* XXX how to report that error? */
230                                           assert(0);
231                                   }
232
233                                   /* Ok, we resolved it. Enter it in the table, so we don't */
234                                   /* have to do this again.                                 */
235                                   /* XXX this write should be atomic. Is it?                */
236
237                                   ex->catchtype.cls = c;
238                           }
239                           else {
240                                   c = cr.cls;
241                                 
242                                   /* If the class is not linked, the exception object cannot */
243                                   /* be an instance of it.                                   */
244                                   if (!(c->state & CLASS_LINKED))
245                                           continue;
246                           }
247                   }
248
249                   if (ip-1 >= (Inst *) ex->startpc && ip-1 < (Inst *) ex->endpc &&
250                           (c == NULL || builtin_instanceof(o, c))) {
251                           *new_spp = (Cell *)(((u1 *)fp) - framesize - SIZEOF_VOID_P);
252                           *new_fpp = fp;
253                           return (Inst *) (ex->handlerpc);
254                   }
255           }
256
257           ip = (Inst *)access_local_cell(-framesize - SIZEOF_VOID_P);
258           fp = (Cell *)access_local_cell(-framesize);
259   }
260
261   return NULL; 
262 }
263
264
265 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out)
266 {
267         s4 sbv, sdv, sv;
268
269 #if defined(ENABLE_THREADS)
270         compiler_lock();
271 #endif
272
273         sbv = super->baseval;
274         sdv = super->diffval;
275         sv  = sub->baseval;
276
277 #if defined(ENABLE_THREADS)
278         compiler_unlock();
279 #endif
280
281         out->super_baseval = sbv;
282         out->super_diffval = sdv;
283         out->sub_baseval   = sv;
284 }
285
286
287 /*
288  * These are local overrides for various environment variables in Emacs.
289  * Please do not remove this and leave it at the end of the file, where
290  * Emacs will automagically detect them.
291  * ---------------------------------------------------------------------
292  * Local variables:
293  * mode: c
294  * indent-tabs-mode: t
295  * c-basic-offset: 4
296  * tab-width: 4
297  * End:
298  */