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