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