jvmti-int.h (_Jv_ReportJVMTIExceptionThrow): Declare.
[gcc.git] / libjava / interpret.cc
1 // interpret.cc - Code for the interpreter
2
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
12
13 #include <config.h>
14 #include <platform.h>
15
16 #pragma implementation "java-interp.h"
17
18 #include <jvm.h>
19 #include <java-cpool.h>
20 #include <java-interp.h>
21 #include <java/lang/System.h>
22 #include <java/lang/String.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/StringBuffer.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/InternalError.h>
29 #include <java/lang/NullPointerException.h>
30 #include <java/lang/ArithmeticException.h>
31 #include <java/lang/IncompatibleClassChangeError.h>
32 #include <java/lang/InstantiationException.h>
33 #include <java/lang/Thread.h>
34 #include <java-insns.h>
35 #include <java-signal.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <execution.h>
38 #include <java/lang/reflect/Modifier.h>
39
40 #include <jvmti.h>
41 #include "jvmti-int.h"
42
43 #include <gnu/gcj/jvmti/Breakpoint.h>
44 #include <gnu/gcj/jvmti/BreakpointManager.h>
45 #include <gnu/gcj/jvmti/ExceptionEvent.h>
46
47 #ifdef INTERPRETER
48
49 // Execution engine for interpreted code.
50 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
51
52 #include <stdlib.h>
53
54 using namespace gcj;
55
56 static void throw_internal_error (const char *msg)
57 __attribute__ ((__noreturn__));
58 static void throw_incompatible_class_change_error (jstring msg)
59 __attribute__ ((__noreturn__));
60 static void throw_null_pointer_exception ()
61 __attribute__ ((__noreturn__));
62
63 static void throw_class_format_error (jstring msg)
64 __attribute__ ((__noreturn__));
65 static void throw_class_format_error (const char *msg)
66 __attribute__ ((__noreturn__));
67
68 static void find_catch_location (jthrowable, jthread, jmethodID *, jlong *);
69
70 // A macro to facilitate JVMTI exception reporting
71 #define REPORT_EXCEPTION(Jthrowable) \
72 do { \
73 if (JVMTI_REQUESTED_EVENT (Exception)) \
74 _Jv_ReportJVMTIExceptionThrow (Jthrowable); \
75 } \
76 while (0)
77
78 #ifdef DIRECT_THREADED
79 // Lock to ensure that methods are not compiled concurrently.
80 // We could use a finer-grained lock here, however it is not safe to use
81 // the Class monitor as user code in another thread could hold it.
82 static _Jv_Mutex_t compile_mutex;
83
84 void
85 _Jv_InitInterpreter()
86 {
87 _Jv_MutexInit (&compile_mutex);
88 }
89 #else
90 void _Jv_InitInterpreter() {}
91 #endif
92
93 // The breakpoint instruction. For the direct threaded case,
94 // _Jv_InterpMethod::compile will initialize breakpoint_insn
95 // the first time it is called.
96 #ifdef DIRECT_THREADED
97 insn_slot _Jv_InterpMethod::bp_insn_slot;
98 pc_t _Jv_InterpMethod::breakpoint_insn = NULL;
99 #else
100 unsigned char _Jv_InterpMethod::bp_insn_opcode
101 = static_cast<unsigned char> (op_breakpoint);
102 pc_t _Jv_InterpMethod::breakpoint_insn = &_Jv_InterpMethod::bp_insn_opcode;
103 #endif
104
105 extern "C" double __ieee754_fmod (double,double);
106
107 static inline void dupx (_Jv_word *sp, int n, int x)
108 {
109 // first "slide" n+x elements n to the right
110 int top = n-1;
111 for (int i = 0; i < n+x; i++)
112 {
113 sp[(top-i)] = sp[(top-i)-n];
114 }
115
116 // next, copy the n top elements, n+x down
117 for (int i = 0; i < n; i++)
118 {
119 sp[top-(n+x)-i] = sp[top-i];
120 }
121 }
122
123 // Used to convert from floating types to integral types.
124 template<typename TO, typename FROM>
125 static inline TO
126 convert (FROM val, TO min, TO max)
127 {
128 TO ret;
129 if (val >= (FROM) max)
130 ret = max;
131 else if (val <= (FROM) min)
132 ret = min;
133 else if (val != val)
134 ret = 0;
135 else
136 ret = (TO) val;
137 return ret;
138 }
139
140 #define PUSHA(V) (sp++)->o = (V)
141 #define PUSHI(V) (sp++)->i = (V)
142 #define PUSHF(V) (sp++)->f = (V)
143 #if SIZEOF_VOID_P == 8
144 # define PUSHL(V) (sp->l = (V), sp += 2)
145 # define PUSHD(V) (sp->d = (V), sp += 2)
146 #else
147 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
148 (sp++)->ia[0] = w2.ia[0]; \
149 (sp++)->ia[0] = w2.ia[1]; } while (0)
150 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
151 (sp++)->ia[0] = w2.ia[0]; \
152 (sp++)->ia[0] = w2.ia[1]; } while (0)
153 #endif
154
155 #define POPA() ((--sp)->o)
156 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
157 #define POPF() ((jfloat) (--sp)->f)
158 #if SIZEOF_VOID_P == 8
159 # define POPL() (sp -= 2, (jlong) sp->l)
160 # define POPD() (sp -= 2, (jdouble) sp->d)
161 #else
162 # define POPL() ({ _Jv_word2 w2; \
163 w2.ia[1] = (--sp)->ia[0]; \
164 w2.ia[0] = (--sp)->ia[0]; w2.l; })
165 # define POPD() ({ _Jv_word2 w2; \
166 w2.ia[1] = (--sp)->ia[0]; \
167 w2.ia[0] = (--sp)->ia[0]; w2.d; })
168 #endif
169
170 #define LOADA(I) (sp++)->o = locals[I].o
171 #define LOADI(I) (sp++)->i = locals[I].i
172 #define LOADF(I) (sp++)->f = locals[I].f
173 #if SIZEOF_VOID_P == 8
174 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
175 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
176 #else
177 # define LOADL(I) do { jint __idx = (I); \
178 (sp++)->ia[0] = locals[__idx].ia[0]; \
179 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
180 } while (0)
181 # define LOADD(I) LOADL(I)
182 #endif
183
184 #define STOREA(I) \
185 do { \
186 DEBUG_LOCALS_INSN (I, 'o'); \
187 locals[I].o = (--sp)->o; \
188 } while (0)
189 #define STOREI(I) \
190 do { \
191 DEBUG_LOCALS_INSN (I, 'i'); \
192 locals[I].i = (--sp)->i; \
193 } while (0)
194 #define STOREF(I) \
195 do { \
196 DEBUG_LOCALS_INSN (I, 'f'); \
197 locals[I].f = (--sp)->f; \
198 } while (0)
199 #if SIZEOF_VOID_P == 8
200 # define STOREL(I) \
201 do { \
202 DEBUG_LOCALS_INSN (I, 'l'); \
203 DEBUG_LOCALS_INSN (I + 1, 'x'); \
204 (sp -= 2, locals[I].l = sp->l); \
205 } while (0)
206 # define STORED(I) \
207 do { \
208 DEBUG_LOCALS_INSN (I, 'd'); \
209 DEBUG_LOCALS_INSN (I + 1, 'x'); \
210 (sp -= 2, locals[I].d = sp->d); \
211 } while (0)
212
213 #else
214 # define STOREL(I) \
215 do { \
216 DEBUG_LOCALS_INSN (I, 'l'); \
217 DEBUG_LOCALS_INSN (I + 1, 'x'); \
218 jint __idx = (I); \
219 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
220 locals[__idx].ia[0] = (--sp)->ia[0]; \
221 } while (0)
222 # define STORED(I) \
223 do { \
224 DEBUG_LOCALS_INSN (I, 'd'); \
225 DEBUG_LOCALS_INSN (I + 1, 'x'); \
226 jint __idx = (I); \
227 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
228 locals[__idx].ia[0] = (--sp)->ia[0]; \
229 } while (0)
230 #endif
231
232 #define PEEKI(I) (locals+(I))->i
233 #define PEEKA(I) (locals+(I))->o
234
235 #define POKEI(I,V) \
236 DEBUG_LOCALS_INSN(I,'i'); \
237 ((locals+(I))->i = (V))
238
239
240 #define BINOPI(OP) { \
241 jint value2 = POPI(); \
242 jint value1 = POPI(); \
243 PUSHI(value1 OP value2); \
244 }
245
246 #define BINOPF(OP) { \
247 jfloat value2 = POPF(); \
248 jfloat value1 = POPF(); \
249 PUSHF(value1 OP value2); \
250 }
251
252 #define BINOPL(OP) { \
253 jlong value2 = POPL(); \
254 jlong value1 = POPL(); \
255 PUSHL(value1 OP value2); \
256 }
257
258 #define BINOPD(OP) { \
259 jdouble value2 = POPD(); \
260 jdouble value1 = POPD(); \
261 PUSHD(value1 OP value2); \
262 }
263
264 static inline jint
265 get1s (unsigned char* loc)
266 {
267 return *(signed char*)loc;
268 }
269
270 static inline jint
271 get1u (unsigned char* loc)
272 {
273 return *loc;
274 }
275
276 static inline jint
277 get2s(unsigned char* loc)
278 {
279 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
280 }
281
282 static inline jint
283 get2u (unsigned char* loc)
284 {
285 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
286 }
287
288 static jint
289 get4 (unsigned char* loc)
290 {
291 return (((jint)(loc[0])) << 24)
292 | (((jint)(loc[1])) << 16)
293 | (((jint)(loc[2])) << 8)
294 | (((jint)(loc[3])) << 0);
295 }
296
297 #define SAVE_PC() frame_desc.pc = pc
298
299 // We used to define this conditionally, depending on HANDLE_SEGV.
300 // However, that runs into a problem if a chunk in low memory is
301 // mapped and we try to look at a field near the end of a large
302 // object. See PR 26858 for details. It is, most likely, relatively
303 // inexpensive to simply do this check always.
304 #define NULLCHECK(X) \
305 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
306
307 // Note that we can still conditionally define NULLARRAYCHECK, since
308 // we know that all uses of an array will first reference the length
309 // field, which is first -- and thus will trigger a SEGV.
310 #ifdef HANDLE_SEGV
311 #define NULLARRAYCHECK(X) SAVE_PC()
312 #else
313 #define NULLARRAYCHECK(X) \
314 do \
315 { \
316 SAVE_PC(); \
317 if ((X) == NULL) { throw_null_pointer_exception (); } \
318 } while (0)
319 #endif
320
321 #define ARRAYBOUNDSCHECK(array, index) \
322 do \
323 { \
324 if (((unsigned) index) >= (unsigned) (array->length)) \
325 _Jv_ThrowBadArrayIndex (index); \
326 } while (0)
327
328 void
329 _Jv_InterpMethod::run_normal (ffi_cif *,
330 void *ret,
331 ffi_raw *args,
332 void *__this)
333 {
334 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
335 run (ret, args, _this);
336 }
337
338 void
339 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
340 void *ret,
341 ffi_raw *args,
342 void *__this)
343 {
344 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
345 run_debug (ret, args, _this);
346 }
347
348 void
349 _Jv_InterpMethod::run_synch_object (ffi_cif *,
350 void *ret,
351 ffi_raw *args,
352 void *__this)
353 {
354 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
355
356 jobject rcv = (jobject) args[0].ptr;
357 JvSynchronize mutex (rcv);
358
359 run (ret, args, _this);
360 }
361
362 void
363 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
364 void *ret,
365 ffi_raw *args,
366 void *__this)
367 {
368 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
369
370 jobject rcv = (jobject) args[0].ptr;
371 JvSynchronize mutex (rcv);
372
373 run_debug (ret, args, _this);
374 }
375
376 void
377 _Jv_InterpMethod::run_class (ffi_cif *,
378 void *ret,
379 ffi_raw *args,
380 void *__this)
381 {
382 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
383 _Jv_InitClass (_this->defining_class);
384 run (ret, args, _this);
385 }
386
387 void
388 _Jv_InterpMethod::run_class_debug (ffi_cif *,
389 void *ret,
390 ffi_raw *args,
391 void *__this)
392 {
393 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
394 _Jv_InitClass (_this->defining_class);
395 run_debug (ret, args, _this);
396 }
397
398 void
399 _Jv_InterpMethod::run_synch_class (ffi_cif *,
400 void *ret,
401 ffi_raw *args,
402 void *__this)
403 {
404 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
405
406 jclass sync = _this->defining_class;
407 _Jv_InitClass (sync);
408 JvSynchronize mutex (sync);
409
410 run (ret, args, _this);
411 }
412
413 void
414 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
415 void *ret,
416 ffi_raw *args,
417 void *__this)
418 {
419 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
420
421 jclass sync = _this->defining_class;
422 _Jv_InitClass (sync);
423 JvSynchronize mutex (sync);
424
425 run_debug (ret, args, _this);
426 }
427
428 #ifdef DIRECT_THREADED
429 // "Compile" a method by turning it from bytecode to direct-threaded
430 // code.
431 void
432 _Jv_InterpMethod::compile (const void * const *insn_targets)
433 {
434 insn_slot *insns = NULL;
435 int next = 0;
436 unsigned char *codestart = bytecode ();
437 unsigned char *end = codestart + code_length;
438 _Jv_word *pool_data = defining_class->constants.data;
439
440 #define SET_ONE(Field, Value) \
441 do \
442 { \
443 if (first_pass) \
444 ++next; \
445 else \
446 insns[next++].Field = Value; \
447 } \
448 while (0)
449
450 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
451 #define SET_INT(Value) SET_ONE (int_val, Value)
452 #define SET_DATUM(Value) SET_ONE (datum, Value)
453
454 // Map from bytecode PC to slot in INSNS.
455 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
456 for (int i = 0; i < code_length; ++i)
457 pc_mapping[i] = -1;
458
459 for (int i = 0; i < 2; ++i)
460 {
461 jboolean first_pass = i == 0;
462
463 if (! first_pass)
464 {
465 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
466 number_insn_slots = next;
467 next = 0;
468 }
469
470 unsigned char *pc = codestart;
471 while (pc < end)
472 {
473 int base_pc_val = pc - codestart;
474 if (first_pass)
475 pc_mapping[base_pc_val] = next;
476
477 java_opcode opcode = (java_opcode) *pc++;
478 // Just elide NOPs.
479 if (opcode == op_nop)
480 continue;
481 SET_INSN (insn_targets[opcode]);
482
483 switch (opcode)
484 {
485 case op_nop:
486 case op_aconst_null:
487 case op_iconst_m1:
488 case op_iconst_0:
489 case op_iconst_1:
490 case op_iconst_2:
491 case op_iconst_3:
492 case op_iconst_4:
493 case op_iconst_5:
494 case op_lconst_0:
495 case op_lconst_1:
496 case op_fconst_0:
497 case op_fconst_1:
498 case op_fconst_2:
499 case op_dconst_0:
500 case op_dconst_1:
501 case op_iload_0:
502 case op_iload_1:
503 case op_iload_2:
504 case op_iload_3:
505 case op_lload_0:
506 case op_lload_1:
507 case op_lload_2:
508 case op_lload_3:
509 case op_fload_0:
510 case op_fload_1:
511 case op_fload_2:
512 case op_fload_3:
513 case op_dload_0:
514 case op_dload_1:
515 case op_dload_2:
516 case op_dload_3:
517 case op_aload_0:
518 case op_aload_1:
519 case op_aload_2:
520 case op_aload_3:
521 case op_iaload:
522 case op_laload:
523 case op_faload:
524 case op_daload:
525 case op_aaload:
526 case op_baload:
527 case op_caload:
528 case op_saload:
529 case op_istore_0:
530 case op_istore_1:
531 case op_istore_2:
532 case op_istore_3:
533 case op_lstore_0:
534 case op_lstore_1:
535 case op_lstore_2:
536 case op_lstore_3:
537 case op_fstore_0:
538 case op_fstore_1:
539 case op_fstore_2:
540 case op_fstore_3:
541 case op_dstore_0:
542 case op_dstore_1:
543 case op_dstore_2:
544 case op_dstore_3:
545 case op_astore_0:
546 case op_astore_1:
547 case op_astore_2:
548 case op_astore_3:
549 case op_iastore:
550 case op_lastore:
551 case op_fastore:
552 case op_dastore:
553 case op_aastore:
554 case op_bastore:
555 case op_castore:
556 case op_sastore:
557 case op_pop:
558 case op_pop2:
559 case op_dup:
560 case op_dup_x1:
561 case op_dup_x2:
562 case op_dup2:
563 case op_dup2_x1:
564 case op_dup2_x2:
565 case op_swap:
566 case op_iadd:
567 case op_isub:
568 case op_imul:
569 case op_idiv:
570 case op_irem:
571 case op_ishl:
572 case op_ishr:
573 case op_iushr:
574 case op_iand:
575 case op_ior:
576 case op_ixor:
577 case op_ladd:
578 case op_lsub:
579 case op_lmul:
580 case op_ldiv:
581 case op_lrem:
582 case op_lshl:
583 case op_lshr:
584 case op_lushr:
585 case op_land:
586 case op_lor:
587 case op_lxor:
588 case op_fadd:
589 case op_fsub:
590 case op_fmul:
591 case op_fdiv:
592 case op_frem:
593 case op_dadd:
594 case op_dsub:
595 case op_dmul:
596 case op_ddiv:
597 case op_drem:
598 case op_ineg:
599 case op_i2b:
600 case op_i2c:
601 case op_i2s:
602 case op_lneg:
603 case op_fneg:
604 case op_dneg:
605 case op_i2l:
606 case op_i2f:
607 case op_i2d:
608 case op_l2i:
609 case op_l2f:
610 case op_l2d:
611 case op_f2i:
612 case op_f2l:
613 case op_f2d:
614 case op_d2i:
615 case op_d2l:
616 case op_d2f:
617 case op_lcmp:
618 case op_fcmpl:
619 case op_fcmpg:
620 case op_dcmpl:
621 case op_dcmpg:
622 case op_monitorenter:
623 case op_monitorexit:
624 case op_ireturn:
625 case op_lreturn:
626 case op_freturn:
627 case op_dreturn:
628 case op_areturn:
629 case op_return:
630 case op_athrow:
631 case op_arraylength:
632 // No argument, nothing else to do.
633 break;
634
635 case op_bipush:
636 SET_INT (get1s (pc));
637 ++pc;
638 break;
639
640 case op_ldc:
641 {
642 int index = get1u (pc);
643 ++pc;
644 // For an unresolved class we want to delay resolution
645 // until execution.
646 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
647 {
648 --next;
649 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
650 SET_INT (index);
651 }
652 else
653 SET_DATUM (pool_data[index].o);
654 }
655 break;
656
657 case op_ret:
658 case op_iload:
659 case op_lload:
660 case op_fload:
661 case op_dload:
662 case op_aload:
663 case op_istore:
664 case op_lstore:
665 case op_fstore:
666 case op_dstore:
667 case op_astore:
668 case op_newarray:
669 SET_INT (get1u (pc));
670 ++pc;
671 break;
672
673 case op_iinc:
674 SET_INT (get1u (pc));
675 SET_INT (get1s (pc + 1));
676 pc += 2;
677 break;
678
679 case op_ldc_w:
680 {
681 int index = get2u (pc);
682 pc += 2;
683 // For an unresolved class we want to delay resolution
684 // until execution.
685 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
686 {
687 --next;
688 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
689 SET_INT (index);
690 }
691 else
692 SET_DATUM (pool_data[index].o);
693 }
694 break;
695
696 case op_ldc2_w:
697 {
698 int index = get2u (pc);
699 pc += 2;
700 SET_DATUM (&pool_data[index]);
701 }
702 break;
703
704 case op_sipush:
705 SET_INT (get2s (pc));
706 pc += 2;
707 break;
708
709 case op_new:
710 case op_getstatic:
711 case op_getfield:
712 case op_putfield:
713 case op_putstatic:
714 case op_anewarray:
715 case op_instanceof:
716 case op_checkcast:
717 case op_invokespecial:
718 case op_invokestatic:
719 case op_invokevirtual:
720 SET_INT (get2u (pc));
721 pc += 2;
722 break;
723
724 case op_multianewarray:
725 SET_INT (get2u (pc));
726 SET_INT (get1u (pc + 2));
727 pc += 3;
728 break;
729
730 case op_jsr:
731 case op_ifeq:
732 case op_ifne:
733 case op_iflt:
734 case op_ifge:
735 case op_ifgt:
736 case op_ifle:
737 case op_if_icmpeq:
738 case op_if_icmpne:
739 case op_if_icmplt:
740 case op_if_icmpge:
741 case op_if_icmpgt:
742 case op_if_icmple:
743 case op_if_acmpeq:
744 case op_if_acmpne:
745 case op_ifnull:
746 case op_ifnonnull:
747 case op_goto:
748 {
749 int offset = get2s (pc);
750 pc += 2;
751
752 int new_pc = base_pc_val + offset;
753
754 bool orig_was_goto = opcode == op_goto;
755
756 // Thread jumps. We limit the loop count; this lets
757 // us avoid infinite loops if the bytecode contains
758 // such. `10' is arbitrary.
759 int count = 10;
760 while (codestart[new_pc] == op_goto && count-- > 0)
761 new_pc += get2s (&codestart[new_pc + 1]);
762
763 // If the jump takes us to a `return' instruction and
764 // the original branch was an unconditional goto, then
765 // we hoist the return.
766 opcode = (java_opcode) codestart[new_pc];
767 if (orig_was_goto
768 && (opcode == op_ireturn || opcode == op_lreturn
769 || opcode == op_freturn || opcode == op_dreturn
770 || opcode == op_areturn || opcode == op_return))
771 {
772 --next;
773 SET_INSN (insn_targets[opcode]);
774 }
775 else
776 SET_DATUM (&insns[pc_mapping[new_pc]]);
777 }
778 break;
779
780 case op_tableswitch:
781 {
782 while ((pc - codestart) % 4 != 0)
783 ++pc;
784
785 jint def = get4 (pc);
786 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
787 pc += 4;
788
789 int low = get4 (pc);
790 SET_INT (low);
791 pc += 4;
792 int high = get4 (pc);
793 SET_INT (high);
794 pc += 4;
795
796 for (int i = low; i <= high; ++i)
797 {
798 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
799 pc += 4;
800 }
801 }
802 break;
803
804 case op_lookupswitch:
805 {
806 while ((pc - codestart) % 4 != 0)
807 ++pc;
808
809 jint def = get4 (pc);
810 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
811 pc += 4;
812
813 jint npairs = get4 (pc);
814 pc += 4;
815 SET_INT (npairs);
816
817 while (npairs-- > 0)
818 {
819 jint match = get4 (pc);
820 jint offset = get4 (pc + 4);
821 SET_INT (match);
822 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
823 pc += 8;
824 }
825 }
826 break;
827
828 case op_invokeinterface:
829 {
830 jint index = get2u (pc);
831 pc += 2;
832 // We ignore the next two bytes.
833 pc += 2;
834 SET_INT (index);
835 }
836 break;
837
838 case op_wide:
839 {
840 opcode = (java_opcode) get1u (pc);
841 pc += 1;
842 jint val = get2u (pc);
843 pc += 2;
844
845 // We implement narrow and wide instructions using the
846 // same code in the interpreter. So we rewrite the
847 // instruction slot here.
848 if (! first_pass)
849 insns[next - 1].insn = (void *) insn_targets[opcode];
850 SET_INT (val);
851
852 if (opcode == op_iinc)
853 {
854 SET_INT (get2s (pc));
855 pc += 2;
856 }
857 }
858 break;
859
860 case op_jsr_w:
861 case op_goto_w:
862 {
863 jint offset = get4 (pc);
864 pc += 4;
865 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
866 }
867 break;
868
869 // Some "can't happen" cases that we include for
870 // error-checking purposes.
871 case op_putfield_1:
872 case op_putfield_2:
873 case op_putfield_4:
874 case op_putfield_8:
875 case op_putfield_a:
876 case op_putstatic_1:
877 case op_putstatic_2:
878 case op_putstatic_4:
879 case op_putstatic_8:
880 case op_putstatic_a:
881 case op_getfield_1:
882 case op_getfield_2s:
883 case op_getfield_2u:
884 case op_getfield_4:
885 case op_getfield_8:
886 case op_getfield_a:
887 case op_getstatic_1:
888 case op_getstatic_2s:
889 case op_getstatic_2u:
890 case op_getstatic_4:
891 case op_getstatic_8:
892 case op_getstatic_a:
893 case op_breakpoint:
894 default:
895 // Fail somehow.
896 break;
897 }
898 }
899 }
900
901 // Now update exceptions.
902 _Jv_InterpException *exc = exceptions ();
903 for (int i = 0; i < exc_count; ++i)
904 {
905 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
906 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
907 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
908 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
909 // during compilation.
910 jclass handler
911 = (_Jv_Linker::resolve_pool_entry (defining_class,
912 exc[i].handler_type.i)).clazz;
913 exc[i].handler_type.p = handler;
914 }
915
916 // Translate entries in the LineNumberTable from bytecode PC's to direct
917 // threaded interpreter instruction values.
918 for (int i = 0; i < line_table_len; i++)
919 {
920 int byte_pc = line_table[i].bytecode_pc;
921 // It isn't worth throwing an exception if this table is
922 // corrupted, but at the same time we don't want a crash.
923 if (byte_pc < 0 || byte_pc >= code_length)
924 byte_pc = 0;
925 line_table[i].pc = &insns[pc_mapping[byte_pc]];
926 }
927
928 prepared = insns;
929
930 if (breakpoint_insn == NULL)
931 {
932 bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
933 breakpoint_insn = &bp_insn_slot;
934 }
935 }
936 #endif /* DIRECT_THREADED */
937
938 /* Run the given method.
939 When args is NULL, don't run anything -- just compile it. */
940 void
941 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
942 {
943 #undef DEBUG
944 #undef DEBUG_LOCALS_INSN
945 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
946
947 #include "interpret-run.cc"
948 }
949
950 void
951 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
952 {
953 #define DEBUG
954 #undef DEBUG_LOCALS_INSN
955 #define DEBUG_LOCALS_INSN(s, t) \
956 do \
957 { \
958 frame_desc.locals_type[s] = t; \
959 } \
960 while (0)
961
962 #include "interpret-run.cc"
963 }
964
965 static void
966 throw_internal_error (const char *msg)
967 {
968 jthrowable t = new java::lang::InternalError (JvNewStringLatin1 (msg));
969 REPORT_EXCEPTION (t);
970 throw t;
971 }
972
973 static void
974 throw_incompatible_class_change_error (jstring msg)
975 {
976 jthrowable t = new java::lang::IncompatibleClassChangeError (msg);
977 REPORT_EXCEPTION (t);
978 throw t;
979 }
980
981 static void
982 throw_null_pointer_exception ()
983 {
984 jthrowable t = new java::lang::NullPointerException;
985 REPORT_EXCEPTION (t);
986 throw t;
987 }
988
989 /* Look up source code line number for given bytecode (or direct threaded
990 interpreter) PC. */
991 int
992 _Jv_InterpMethod::get_source_line(pc_t mpc)
993 {
994 int line = line_table_len > 0 ? line_table[0].line : -1;
995 for (int i = 1; i < line_table_len; i++)
996 if (line_table[i].pc > mpc)
997 break;
998 else
999 line = line_table[i].line;
1000
1001 return line;
1002 }
1003
1004 /** Do static initialization for fields with a constant initializer */
1005 void
1006 _Jv_InitField (jobject obj, jclass klass, int index)
1007 {
1008 using namespace java::lang::reflect;
1009
1010 if (obj != 0 && klass == 0)
1011 klass = obj->getClass ();
1012
1013 if (!_Jv_IsInterpretedClass (klass))
1014 return;
1015
1016 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
1017
1018 _Jv_Field * field = (&klass->fields[0]) + index;
1019
1020 if (index > klass->field_count)
1021 throw_internal_error ("field out of range");
1022
1023 int init = iclass->field_initializers[index];
1024 if (init == 0)
1025 return;
1026
1027 _Jv_Constants *pool = &klass->constants;
1028 int tag = pool->tags[init];
1029
1030 if (! field->isResolved ())
1031 throw_internal_error ("initializing unresolved field");
1032
1033 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
1034 throw_internal_error ("initializing non-static field with no object");
1035
1036 void *addr = 0;
1037
1038 if ((field->flags & Modifier::STATIC) != 0)
1039 addr = (void*) field->u.addr;
1040 else
1041 addr = (void*) (((char*)obj) + field->u.boffset);
1042
1043 switch (tag)
1044 {
1045 case JV_CONSTANT_String:
1046 {
1047 jstring str;
1048 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1049 pool->data[init].string = str;
1050 pool->tags[init] = JV_CONSTANT_ResolvedString;
1051 }
1052 /* fall through */
1053
1054 case JV_CONSTANT_ResolvedString:
1055 if (! (field->type == &java::lang::String::class$
1056 || field->type == &java::lang::Class::class$))
1057 throw_class_format_error ("string initialiser to non-string field");
1058
1059 *(jstring*)addr = pool->data[init].string;
1060 break;
1061
1062 case JV_CONSTANT_Integer:
1063 {
1064 int value = pool->data[init].i;
1065
1066 if (field->type == JvPrimClass (boolean))
1067 *(jboolean*)addr = (jboolean)value;
1068
1069 else if (field->type == JvPrimClass (byte))
1070 *(jbyte*)addr = (jbyte)value;
1071
1072 else if (field->type == JvPrimClass (char))
1073 *(jchar*)addr = (jchar)value;
1074
1075 else if (field->type == JvPrimClass (short))
1076 *(jshort*)addr = (jshort)value;
1077
1078 else if (field->type == JvPrimClass (int))
1079 *(jint*)addr = (jint)value;
1080
1081 else
1082 throw_class_format_error ("erroneous field initializer");
1083 }
1084 break;
1085
1086 case JV_CONSTANT_Long:
1087 if (field->type != JvPrimClass (long))
1088 throw_class_format_error ("erroneous field initializer");
1089
1090 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1091 break;
1092
1093 case JV_CONSTANT_Float:
1094 if (field->type != JvPrimClass (float))
1095 throw_class_format_error ("erroneous field initializer");
1096
1097 *(jfloat*)addr = pool->data[init].f;
1098 break;
1099
1100 case JV_CONSTANT_Double:
1101 if (field->type != JvPrimClass (double))
1102 throw_class_format_error ("erroneous field initializer");
1103
1104 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1105 break;
1106
1107 default:
1108 throw_class_format_error ("erroneous field initializer");
1109 }
1110 }
1111
1112 inline static unsigned char*
1113 skip_one_type (unsigned char* ptr)
1114 {
1115 int ch = *ptr++;
1116
1117 while (ch == '[')
1118 {
1119 ch = *ptr++;
1120 }
1121
1122 if (ch == 'L')
1123 {
1124 do { ch = *ptr++; } while (ch != ';');
1125 }
1126
1127 return ptr;
1128 }
1129
1130 static ffi_type*
1131 get_ffi_type_from_signature (unsigned char* ptr)
1132 {
1133 switch (*ptr)
1134 {
1135 case 'L':
1136 case '[':
1137 return &ffi_type_pointer;
1138 break;
1139
1140 case 'Z':
1141 // On some platforms a bool is a byte, on others an int.
1142 if (sizeof (jboolean) == sizeof (jbyte))
1143 return &ffi_type_sint8;
1144 else
1145 {
1146 JvAssert (sizeof (jbyte) == sizeof (jint));
1147 return &ffi_type_sint32;
1148 }
1149 break;
1150
1151 case 'B':
1152 return &ffi_type_sint8;
1153 break;
1154
1155 case 'C':
1156 return &ffi_type_uint16;
1157 break;
1158
1159 case 'S':
1160 return &ffi_type_sint16;
1161 break;
1162
1163 case 'I':
1164 return &ffi_type_sint32;
1165 break;
1166
1167 case 'J':
1168 return &ffi_type_sint64;
1169 break;
1170
1171 case 'F':
1172 return &ffi_type_float;
1173 break;
1174
1175 case 'D':
1176 return &ffi_type_double;
1177 break;
1178
1179 case 'V':
1180 return &ffi_type_void;
1181 break;
1182 }
1183
1184 throw_internal_error ("unknown type in signature");
1185 }
1186
1187 /* this function yields the number of actual arguments, that is, if the
1188 * function is non-static, then one is added to the number of elements
1189 * found in the signature */
1190
1191 int
1192 _Jv_count_arguments (_Jv_Utf8Const *signature,
1193 jboolean staticp)
1194 {
1195 unsigned char *ptr = (unsigned char*) signature->chars();
1196 int arg_count = staticp ? 0 : 1;
1197
1198 /* first, count number of arguments */
1199
1200 // skip '('
1201 ptr++;
1202
1203 // count args
1204 while (*ptr != ')')
1205 {
1206 ptr = skip_one_type (ptr);
1207 arg_count += 1;
1208 }
1209
1210 return arg_count;
1211 }
1212
1213 /* This beast will build a cif, given the signature. Memory for
1214 * the cif itself and for the argument types must be allocated by the
1215 * caller.
1216 */
1217
1218 int
1219 _Jv_init_cif (_Jv_Utf8Const* signature,
1220 int arg_count,
1221 jboolean staticp,
1222 ffi_cif *cif,
1223 ffi_type **arg_types,
1224 ffi_type **rtype_p)
1225 {
1226 unsigned char *ptr = (unsigned char*) signature->chars();
1227
1228 int arg_index = 0; // arg number
1229 int item_count = 0; // stack-item count
1230
1231 // setup receiver
1232 if (!staticp)
1233 {
1234 arg_types[arg_index++] = &ffi_type_pointer;
1235 item_count += 1;
1236 }
1237
1238 // skip '('
1239 ptr++;
1240
1241 // assign arg types
1242 while (*ptr != ')')
1243 {
1244 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1245
1246 if (*ptr == 'J' || *ptr == 'D')
1247 item_count += 2;
1248 else
1249 item_count += 1;
1250
1251 ptr = skip_one_type (ptr);
1252 }
1253
1254 // skip ')'
1255 ptr++;
1256 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1257
1258 ptr = skip_one_type (ptr);
1259 if (ptr != (unsigned char*)signature->chars() + signature->len())
1260 throw_internal_error ("did not find end of signature");
1261
1262 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1263 arg_count, rtype, arg_types) != FFI_OK)
1264 throw_internal_error ("ffi_prep_cif failed");
1265
1266 if (rtype_p != NULL)
1267 *rtype_p = rtype;
1268
1269 return item_count;
1270 }
1271
1272 #if FFI_NATIVE_RAW_API
1273 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1274 # define FFI_RAW_SIZE ffi_raw_size
1275 #else
1276 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1277 # define FFI_RAW_SIZE ffi_java_raw_size
1278 #endif
1279
1280 /* we put this one here, and not in interpret.cc because it
1281 * calls the utility routines _Jv_count_arguments
1282 * which are static to this module. The following struct defines the
1283 * layout we use for the stubs, it's only used in the ncode method. */
1284
1285 typedef struct {
1286 ffi_raw_closure closure;
1287 _Jv_ClosureList list;
1288 ffi_cif cif;
1289 ffi_type *arg_types[0];
1290 } ncode_closure;
1291
1292 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1293
1294 void *
1295 _Jv_InterpMethod::ncode (jclass klass)
1296 {
1297 using namespace java::lang::reflect;
1298
1299 if (self->ncode != 0)
1300 return self->ncode;
1301
1302 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1303 int arg_count = _Jv_count_arguments (self->signature, staticp);
1304
1305 void *code;
1306 ncode_closure *closure =
1307 (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure)
1308 + arg_count * sizeof (ffi_type*),
1309 &code);
1310 closure->list.registerClosure (klass, closure);
1311
1312 _Jv_init_cif (self->signature,
1313 arg_count,
1314 staticp,
1315 &closure->cif,
1316 &closure->arg_types[0],
1317 NULL);
1318
1319 ffi_closure_fun fun;
1320
1321 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1322
1323 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1324
1325 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1326 {
1327 if (staticp)
1328 {
1329 if (JVMTI::enabled)
1330 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1331 else
1332 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1333 }
1334 else
1335 {
1336 if (JVMTI::enabled)
1337 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1338 else
1339 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1340 }
1341 }
1342 else
1343 {
1344 if (staticp)
1345 {
1346 if (JVMTI::enabled)
1347 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1348 else
1349 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1350 }
1351 else
1352 {
1353 if (JVMTI::enabled)
1354 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1355 else
1356 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1357 }
1358 }
1359
1360 FFI_PREP_RAW_CLOSURE (&closure->closure,
1361 &closure->cif,
1362 fun,
1363 (void*)this,
1364 code);
1365
1366 self->ncode = code;
1367
1368 return self->ncode;
1369 }
1370
1371 /* Find the index of the given insn in the array of insn slots
1372 for this method. Returns -1 if not found. */
1373 jlong
1374 _Jv_InterpMethod::insn_index (pc_t pc)
1375 {
1376 jlong left = 0;
1377 #ifdef DIRECT_THREADED
1378 jlong right = number_insn_slots;
1379 pc_t insns = prepared;
1380 #else
1381 jlong right = code_length;
1382 pc_t insns = bytecode ();
1383 #endif
1384
1385 while (right >= 0)
1386 {
1387 jlong mid = (left + right) / 2;
1388 if (&insns[mid] == pc)
1389 return mid;
1390
1391 if (pc < &insns[mid])
1392 right = mid - 1;
1393 else
1394 left = mid + 1;
1395 }
1396
1397 return -1;
1398 }
1399
1400 // Method to check if an exception is caught at some location in a method
1401 // (meth). Returns true if this method (meth) contains a catch block for the
1402 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1403 // to the location of the beginning of the catch block.
1404 jboolean
1405 _Jv_InterpMethod::check_handler (pc_t *pc, _Jv_InterpMethod *meth,
1406 java::lang::Throwable *ex)
1407 {
1408 #ifdef DIRECT_THREADED
1409 void *logical_pc = (void *) ((insn_slot *) (*pc) - 1);
1410 #else
1411 int logical_pc = (*pc) - 1 - meth->bytecode ();
1412 #endif
1413 _Jv_InterpException *exc = meth->exceptions ();
1414 jclass exc_class = ex->getClass ();
1415
1416 for (int i = 0; i < meth->exc_count; i++)
1417 {
1418 if (PCVAL (exc[i].start_pc) <= logical_pc
1419 && logical_pc < PCVAL (exc[i].end_pc))
1420 {
1421 #ifdef DIRECT_THREADED
1422 jclass handler = (jclass) exc[i].handler_type.p;
1423 #else
1424 jclass handler = NULL;
1425 if (exc[i].handler_type.i != 0)
1426 handler
1427 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1428 ex$
1429 #endif /* DIRECT_THREADED */
1430 if (handler == NULL || handler->isAssignableFrom (exc_class))
1431 {
1432 #ifdef DIRECT_THREADED
1433 (*pc) = (insn_slot *) exc[i].handler_pc.p;
1434 #else
1435 (*pc) = meth->bytecode () + exc[i].handler_pc.i;
1436 #endif /* DIRECT_THREADED */
1437 return true;
1438 }
1439 }
1440 }
1441 return false;
1442 }
1443
1444
1445 void
1446 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1447 jintArray& line_numbers,
1448 jlongArray& code_indices)
1449 {
1450 #ifdef DIRECT_THREADED
1451 /* For the DIRECT_THREADED case, if the method has not yet been
1452 * compiled, the linetable will change to insn slots instead of
1453 * bytecode PCs. It is probably easiest, in this case, to simply
1454 * compile the method and guarantee that we are using insn
1455 * slots.
1456 */
1457 _Jv_CompileMethod (this);
1458
1459 if (line_table_len > 0)
1460 {
1461 start = 0;
1462 end = number_insn_slots;
1463 line_numbers = JvNewIntArray (line_table_len);
1464 code_indices = JvNewLongArray (line_table_len);
1465
1466 jint* lines = elements (line_numbers);
1467 jlong* indices = elements (code_indices);
1468 for (int i = 0; i < line_table_len; ++i)
1469 {
1470 lines[i] = line_table[i].line;
1471 indices[i] = insn_index (line_table[i].pc);
1472 }
1473 }
1474 #else // !DIRECT_THREADED
1475 if (line_table_len > 0)
1476 {
1477 start = 0;
1478 end = code_length;
1479 line_numbers = JvNewIntArray (line_table_len);
1480 code_indices = JvNewLongArray (line_table_len);
1481
1482 jint* lines = elements (line_numbers);
1483 jlong* indices = elements (code_indices);
1484 for (int i = 0; i < line_table_len; ++i)
1485 {
1486 lines[i] = line_table[i].line;
1487 indices[i] = (jlong) line_table[i].bytecode_pc;
1488 }
1489 }
1490 #endif // !DIRECT_THREADED
1491 }
1492
1493 int
1494 _Jv_InterpMethod::get_local_var_table (char **name, char **sig,
1495 char **generic_sig, jlong *startloc,
1496 jint *length, jint *slot,
1497 int table_slot)
1498 {
1499 if (local_var_table == NULL)
1500 return -2;
1501 if (table_slot >= local_var_table_len)
1502 return -1;
1503 else
1504 {
1505 *name = local_var_table[table_slot].name;
1506 *sig = local_var_table[table_slot].descriptor;
1507 *generic_sig = local_var_table[table_slot].descriptor;
1508
1509 *startloc = static_cast<jlong>
1510 (local_var_table[table_slot].bytecode_start_pc);
1511 *length = static_cast<jint> (local_var_table[table_slot].length);
1512 *slot = static_cast<jint> (local_var_table[table_slot].slot);
1513 }
1514 return local_var_table_len - table_slot -1;
1515 }
1516
1517 pc_t
1518 _Jv_InterpMethod::install_break (jlong index)
1519 {
1520 return set_insn (index, breakpoint_insn);
1521 }
1522
1523 pc_t
1524 _Jv_InterpMethod::get_insn (jlong index)
1525 {
1526 pc_t code;
1527
1528 #ifdef DIRECT_THREADED
1529 if (index >= number_insn_slots || index < 0)
1530 return NULL;
1531
1532 code = prepared;
1533 #else // !DIRECT_THREADED
1534 if (index >= code_length || index < 0)
1535 return NULL;
1536
1537 code = reinterpret_cast<pc_t> (bytecode ());
1538 #endif // !DIRECT_THREADED
1539
1540 return &code[index];
1541 }
1542
1543 pc_t
1544 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1545 {
1546 #ifdef DIRECT_THREADED
1547 if (index >= number_insn_slots || index < 0)
1548 return NULL;
1549
1550 pc_t code = prepared;
1551 code[index].insn = insn->insn;
1552 #else // !DIRECT_THREADED
1553 if (index >= code_length || index < 0)
1554 return NULL;
1555
1556 pc_t code = reinterpret_cast<pc_t> (bytecode ());
1557 code[index] = *insn;
1558 #endif // !DIRECT_THREADED
1559
1560 return &code[index];
1561 }
1562
1563 void *
1564 _Jv_JNIMethod::ncode (jclass klass)
1565 {
1566 using namespace java::lang::reflect;
1567
1568 if (self->ncode != 0)
1569 return self->ncode;
1570
1571 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1572 int arg_count = _Jv_count_arguments (self->signature, staticp);
1573
1574 void *code;
1575 ncode_closure *closure =
1576 (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure)
1577 + arg_count * sizeof (ffi_type*),
1578 &code);
1579 closure->list.registerClosure (klass, closure);
1580
1581 ffi_type *rtype;
1582 _Jv_init_cif (self->signature,
1583 arg_count,
1584 staticp,
1585 &closure->cif,
1586 &closure->arg_types[0],
1587 &rtype);
1588
1589 ffi_closure_fun fun;
1590
1591 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1592
1593 // Initialize the argument types and CIF that represent the actual
1594 // underlying JNI function.
1595 int extra_args = 1;
1596 if ((self->accflags & Modifier::STATIC))
1597 ++extra_args;
1598 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1599 * sizeof (ffi_type *));
1600 int offset = 0;
1601 jni_arg_types[offset++] = &ffi_type_pointer;
1602 if ((self->accflags & Modifier::STATIC))
1603 jni_arg_types[offset++] = &ffi_type_pointer;
1604 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1605 arg_count * sizeof (ffi_type *));
1606
1607 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1608 extra_args + arg_count, rtype,
1609 jni_arg_types) != FFI_OK)
1610 throw_internal_error ("ffi_prep_cif failed for JNI function");
1611
1612 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1613
1614 // FIXME: for now we assume that all native methods for
1615 // interpreted code use JNI.
1616 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1617
1618 FFI_PREP_RAW_CLOSURE (&closure->closure,
1619 &closure->cif,
1620 fun,
1621 (void*) this,
1622 code);
1623
1624 self->ncode = code;
1625 return self->ncode;
1626 }
1627
1628 static void
1629 throw_class_format_error (jstring msg)
1630 {
1631 jthrowable t = (msg
1632 ? new java::lang::ClassFormatError (msg)
1633 : new java::lang::ClassFormatError);
1634 REPORT_EXCEPTION (t);
1635 throw t;
1636 }
1637
1638 static void
1639 throw_class_format_error (const char *msg)
1640 {
1641 throw_class_format_error (JvNewStringLatin1 (msg));
1642 }
1643
1644 /* This function finds the method and location where the exception EXC
1645 is caught in the stack frame. On return, it sets CATCH_METHOD and
1646 CATCH_LOCATION with the method and location where the catch will
1647 occur. If the exception is not caught, these are set to 0.
1648
1649 This function should only be used with the DEBUG interpreter. */
1650 static void
1651 find_catch_location (::java::lang::Throwable *exc, jthread thread,
1652 jmethodID *catch_method, jlong *catch_loc)
1653 {
1654 *catch_method = 0;
1655 *catch_loc = 0;
1656
1657 _Jv_InterpFrame *frame
1658 = reinterpret_cast<_Jv_InterpFrame *> (thread->interp_frame);
1659 while (frame != NULL)
1660 {
1661 pc_t pc = frame->get_pc ();
1662 _Jv_InterpMethod *imeth
1663 = reinterpret_cast<_Jv_InterpMethod *> (frame->self);
1664 if (imeth->check_handler (&pc, imeth, exc))
1665 {
1666 // This method handles the exception.
1667 *catch_method = imeth->get_method ();
1668 *catch_loc = imeth->insn_index (pc);
1669 return;
1670 }
1671
1672 frame = frame->next_interp;
1673 }
1674 }
1675
1676 /* This method handles JVMTI notifications of thrown exceptions. It
1677 calls find_catch_location to figure out where the exception is
1678 caught (if it is caught).
1679
1680 Like find_catch_location, this should only be called with the
1681 DEBUG interpreter. Since a few exceptions occur outside the
1682 interpreter proper, it is important to not call this function
1683 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1684 void
1685 _Jv_ReportJVMTIExceptionThrow (jthrowable ex)
1686 {
1687 jthread thread = ::java::lang::Thread::currentThread ();
1688 _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
1689 jmethodID throw_meth = frame->self->get_method ();
1690 jlocation throw_loc = -1;
1691 if (frame->frame_type == frame_interpreter)
1692 {
1693 _Jv_InterpFrame * iframe
1694 = reinterpret_cast<_Jv_InterpFrame *> (frame);
1695 _Jv_InterpMethod *imeth
1696 = reinterpret_cast<_Jv_InterpMethod *> (frame->self);
1697 throw_loc = imeth->insn_index (iframe->get_pc ());
1698 }
1699
1700 jlong catch_loc;
1701 jmethodID catch_method;
1702 find_catch_location (ex, thread, &catch_method, &catch_loc);
1703 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION, thread,
1704 _Jv_GetCurrentJNIEnv (), throw_meth, throw_loc,
1705 ex, catch_method, catch_loc);
1706 }
1707
1708 \f
1709
1710 void
1711 _Jv_InterpreterEngine::do_verify (jclass klass)
1712 {
1713 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1714 for (int i = 0; i < klass->method_count; i++)
1715 {
1716 using namespace java::lang::reflect;
1717 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1718 _Jv_ushort accflags = klass->methods[i].accflags;
1719 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1720 {
1721 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1722 _Jv_VerifyMethod (im);
1723 }
1724 }
1725 }
1726
1727 void
1728 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1729 {
1730 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1731 for (int i = 0; i < klass->method_count; i++)
1732 {
1733 // Just skip abstract methods. This is particularly important
1734 // because we don't resize the interpreted_methods array when
1735 // miranda methods are added to it.
1736 if ((klass->methods[i].accflags
1737 & java::lang::reflect::Modifier::ABSTRACT)
1738 != 0)
1739 continue;
1740
1741 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1742
1743 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1744 != 0)
1745 {
1746 // You might think we could use a virtual `ncode' method in
1747 // the _Jv_MethodBase and unify the native and non-native
1748 // cases. Well, we can't, because we don't allocate these
1749 // objects using `new', and thus they don't get a vtable.
1750 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1751 klass->methods[i].ncode = jnim->ncode (klass);
1752 }
1753 else if (imeth != 0) // it could be abstract
1754 {
1755 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1756 klass->methods[i].ncode = im->ncode (klass);
1757 }
1758 }
1759 }
1760
1761 _Jv_ClosureList **
1762 _Jv_InterpreterEngine::do_get_closure_list (jclass klass)
1763 {
1764 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1765
1766 if (!iclass->closures)
1767 iclass->closures = _Jv_ClosureListFinalizer ();
1768
1769 return iclass->closures;
1770 }
1771
1772 void
1773 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1774 int pointer_size,
1775 int other_size)
1776 {
1777 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1778
1779 // Splitting the allocations here lets us scan reference fields and
1780 // avoid scanning non-reference fields. How reference fields are
1781 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1782 // means that this memory will be scanned conservatively (same
1783 // difference, since we know all the contents here are pointers).
1784 // Then we put pointers into this memory into the 'fields'
1785 // structure. Most of these are interior pointers, which is ok (but
1786 // even so the pointer to the first reference field will be used and
1787 // that is not an interior pointer). The 'fields' array is also
1788 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1789 // be scanned. A pointer to this array is held by Class and thus
1790 // seen by the collector.
1791 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1792 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1793
1794 for (int i = 0; i < klass->field_count; i++)
1795 {
1796 _Jv_Field *field = &klass->fields[i];
1797
1798 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1799 continue;
1800
1801 char *base = field->isRef() ? reference_fields : non_reference_fields;
1802 field->u.addr = base + field->u.boffset;
1803
1804 if (iclass->field_initializers[i] != 0)
1805 {
1806 _Jv_Linker::resolve_field (field, klass->loader);
1807 _Jv_InitField (0, klass, i);
1808 }
1809 }
1810
1811 // Now we don't need the field_initializers anymore, so let the
1812 // collector get rid of it.
1813 iclass->field_initializers = 0;
1814 }
1815
1816 _Jv_ResolvedMethod *
1817 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1818 jboolean staticp)
1819 {
1820 int arg_count = _Jv_count_arguments (method->signature, staticp);
1821
1822 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1823 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1824 + arg_count*sizeof (ffi_type*));
1825
1826 result->stack_item_count
1827 = _Jv_init_cif (method->signature,
1828 arg_count,
1829 staticp,
1830 &result->cif,
1831 &result->arg_types[0],
1832 NULL);
1833
1834 result->method = method;
1835 result->klass = klass;
1836
1837 return result;
1838 }
1839
1840 void
1841 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1842 {
1843 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1844 for (int i = 0; i < klass->method_count; i++)
1845 {
1846 // Just skip abstract methods. This is particularly important
1847 // because we don't resize the interpreted_methods array when
1848 // miranda methods are added to it.
1849 if ((klass->methods[i].accflags
1850 & java::lang::reflect::Modifier::ABSTRACT)
1851 != 0)
1852 continue;
1853 // Miranda method additions mean that the `methods' array moves.
1854 // We cache a pointer into this array, so we have to update.
1855 iclass->interpreted_methods[i]->self = &klass->methods[i];
1856 }
1857 }
1858
1859 #ifdef DIRECT_THREADED
1860 void
1861 _Jv_CompileMethod (_Jv_InterpMethod* method)
1862 {
1863 if (method->prepared == NULL)
1864 {
1865 if (JVMTI::enabled)
1866 _Jv_InterpMethod::run_debug (NULL, NULL, method);
1867 else
1868 _Jv_InterpMethod::run (NULL, NULL, method);
1869 }
1870 }
1871 #endif // DIRECT_THREADED
1872
1873 #endif // INTERPRETER