./:
[official-gcc.git] / libjava / interpret.cc
blobdbd5323a04b3655833b7222901e8db21e36257c2
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/classpath/jdwp/Jdwp.h>
44 #include <gnu/gcj/jvmti/Breakpoint.h>
45 #include <gnu/gcj/jvmti/BreakpointManager.h>
46 #include <gnu/gcj/jvmti/ExceptionEvent.h>
48 #ifdef INTERPRETER
50 // Execution engine for interpreted code.
51 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
53 #include <stdlib.h>
55 using namespace gcj;
57 static void throw_internal_error (const char *msg)
58 __attribute__ ((__noreturn__));
59 static void throw_incompatible_class_change_error (jstring msg)
60 __attribute__ ((__noreturn__));
61 static void throw_null_pointer_exception ()
62 __attribute__ ((__noreturn__));
64 static void throw_class_format_error (jstring msg)
65 __attribute__ ((__noreturn__));
66 static void throw_class_format_error (const char *msg)
67 __attribute__ ((__noreturn__));
69 #ifdef DIRECT_THREADED
70 // Lock to ensure that methods are not compiled concurrently.
71 // We could use a finer-grained lock here, however it is not safe to use
72 // the Class monitor as user code in another thread could hold it.
73 static _Jv_Mutex_t compile_mutex;
75 void
76 _Jv_InitInterpreter()
78 _Jv_MutexInit (&compile_mutex);
80 #else
81 void _Jv_InitInterpreter() {}
82 #endif
84 // The breakpoint instruction. For the direct threaded case,
85 // _Jv_InterpMethod::compile will initialize breakpoint_insn
86 // the first time it is called.
87 #ifdef DIRECT_THREADED
88 insn_slot _Jv_InterpMethod::bp_insn_slot;
89 pc_t _Jv_InterpMethod::breakpoint_insn = NULL;
90 #else
91 unsigned char _Jv_InterpMethod::bp_insn_opcode
92 = static_cast<unsigned char> (op_breakpoint);
93 pc_t _Jv_InterpMethod::breakpoint_insn = &_Jv_InterpMethod::bp_insn_opcode;
94 #endif
96 extern "C" double __ieee754_fmod (double,double);
98 static inline void dupx (_Jv_word *sp, int n, int x)
100 // first "slide" n+x elements n to the right
101 int top = n-1;
102 for (int i = 0; i < n+x; i++)
104 sp[(top-i)] = sp[(top-i)-n];
107 // next, copy the n top elements, n+x down
108 for (int i = 0; i < n; i++)
110 sp[top-(n+x)-i] = sp[top-i];
114 // Used to convert from floating types to integral types.
115 template<typename TO, typename FROM>
116 static inline TO
117 convert (FROM val, TO min, TO max)
119 TO ret;
120 if (val >= (FROM) max)
121 ret = max;
122 else if (val <= (FROM) min)
123 ret = min;
124 else if (val != val)
125 ret = 0;
126 else
127 ret = (TO) val;
128 return ret;
131 #define PUSHA(V) (sp++)->o = (V)
132 #define PUSHI(V) (sp++)->i = (V)
133 #define PUSHF(V) (sp++)->f = (V)
134 #if SIZEOF_VOID_P == 8
135 # define PUSHL(V) (sp->l = (V), sp += 2)
136 # define PUSHD(V) (sp->d = (V), sp += 2)
137 #else
138 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
139 (sp++)->ia[0] = w2.ia[0]; \
140 (sp++)->ia[0] = w2.ia[1]; } while (0)
141 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
142 (sp++)->ia[0] = w2.ia[0]; \
143 (sp++)->ia[0] = w2.ia[1]; } while (0)
144 #endif
146 #define POPA() ((--sp)->o)
147 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
148 #define POPF() ((jfloat) (--sp)->f)
149 #if SIZEOF_VOID_P == 8
150 # define POPL() (sp -= 2, (jlong) sp->l)
151 # define POPD() (sp -= 2, (jdouble) sp->d)
152 #else
153 # define POPL() ({ _Jv_word2 w2; \
154 w2.ia[1] = (--sp)->ia[0]; \
155 w2.ia[0] = (--sp)->ia[0]; w2.l; })
156 # define POPD() ({ _Jv_word2 w2; \
157 w2.ia[1] = (--sp)->ia[0]; \
158 w2.ia[0] = (--sp)->ia[0]; w2.d; })
159 #endif
161 #define LOADA(I) (sp++)->o = locals[I].o
162 #define LOADI(I) (sp++)->i = locals[I].i
163 #define LOADF(I) (sp++)->f = locals[I].f
164 #if SIZEOF_VOID_P == 8
165 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
166 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
167 #else
168 # define LOADL(I) do { jint __idx = (I); \
169 (sp++)->ia[0] = locals[__idx].ia[0]; \
170 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
171 } while (0)
172 # define LOADD(I) LOADL(I)
173 #endif
175 #define STOREA(I) \
176 do { \
177 DEBUG_LOCALS_INSN (I, 'o'); \
178 locals[I].o = (--sp)->o; \
179 } while (0)
180 #define STOREI(I) \
181 do { \
182 DEBUG_LOCALS_INSN (I, 'i'); \
183 locals[I].i = (--sp)->i; \
184 } while (0)
185 #define STOREF(I) \
186 do { \
187 DEBUG_LOCALS_INSN (I, 'f'); \
188 locals[I].f = (--sp)->f; \
189 } while (0)
190 #if SIZEOF_VOID_P == 8
191 # define STOREL(I) \
192 do { \
193 DEBUG_LOCALS_INSN (I, 'l'); \
194 (sp -= 2, locals[I].l = sp->l); \
195 } while (0)
196 # define STORED(I) \
197 do { \
198 DEBUG_LOCALS_INSN (I, 'd'); \
199 (sp -= 2, locals[I].d = sp->d); \
200 } while (0)
202 #else
203 # define STOREL(I) \
204 do { \
205 DEBUG_LOCALS_INSN (I, 'l'); \
206 jint __idx = (I); \
207 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
208 locals[__idx].ia[0] = (--sp)->ia[0]; \
209 } while (0)
210 # define STORED(I) \
211 do { \
212 DEBUG_LOCALS_INSN(I, 'd'); \
213 jint __idx = (I); \
214 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
215 locals[__idx].ia[0] = (--sp)->ia[0]; \
216 } while (0)
217 #endif
219 #define PEEKI(I) (locals+(I))->i
220 #define PEEKA(I) (locals+(I))->o
222 #define POKEI(I,V) \
223 DEBUG_LOCALS_INSN(I,'i'); \
224 ((locals+(I))->i = (V))
227 #define BINOPI(OP) { \
228 jint value2 = POPI(); \
229 jint value1 = POPI(); \
230 PUSHI(value1 OP value2); \
233 #define BINOPF(OP) { \
234 jfloat value2 = POPF(); \
235 jfloat value1 = POPF(); \
236 PUSHF(value1 OP value2); \
239 #define BINOPL(OP) { \
240 jlong value2 = POPL(); \
241 jlong value1 = POPL(); \
242 PUSHL(value1 OP value2); \
245 #define BINOPD(OP) { \
246 jdouble value2 = POPD(); \
247 jdouble value1 = POPD(); \
248 PUSHD(value1 OP value2); \
251 static inline jint
252 get1s (unsigned char* loc)
254 return *(signed char*)loc;
257 static inline jint
258 get1u (unsigned char* loc)
260 return *loc;
263 static inline jint
264 get2s(unsigned char* loc)
266 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
269 static inline jint
270 get2u (unsigned char* loc)
272 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
275 static jint
276 get4 (unsigned char* loc)
278 return (((jint)(loc[0])) << 24)
279 | (((jint)(loc[1])) << 16)
280 | (((jint)(loc[2])) << 8)
281 | (((jint)(loc[3])) << 0);
284 #define SAVE_PC() frame_desc.pc = pc
286 // We used to define this conditionally, depending on HANDLE_SEGV.
287 // However, that runs into a problem if a chunk in low memory is
288 // mapped and we try to look at a field near the end of a large
289 // object. See PR 26858 for details. It is, most likely, relatively
290 // inexpensive to simply do this check always.
291 #define NULLCHECK(X) \
292 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
294 // Note that we can still conditionally define NULLARRAYCHECK, since
295 // we know that all uses of an array will first reference the length
296 // field, which is first -- and thus will trigger a SEGV.
297 #ifdef HANDLE_SEGV
298 #define NULLARRAYCHECK(X) SAVE_PC()
299 #else
300 #define NULLARRAYCHECK(X) \
301 do \
303 SAVE_PC(); \
304 if ((X) == NULL) { throw_null_pointer_exception (); } \
305 } while (0)
306 #endif
308 #define ARRAYBOUNDSCHECK(array, index) \
309 do \
311 if (((unsigned) index) >= (unsigned) (array->length)) \
312 _Jv_ThrowBadArrayIndex (index); \
313 } while (0)
315 void
316 _Jv_InterpMethod::run_normal (ffi_cif *,
317 void *ret,
318 ffi_raw *args,
319 void *__this)
321 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
322 run (ret, args, _this);
325 void
326 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
327 void *ret,
328 ffi_raw *args,
329 void *__this)
331 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
332 run_debug (ret, args, _this);
335 void
336 _Jv_InterpMethod::run_synch_object (ffi_cif *,
337 void *ret,
338 ffi_raw *args,
339 void *__this)
341 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
343 jobject rcv = (jobject) args[0].ptr;
344 JvSynchronize mutex (rcv);
346 run (ret, args, _this);
349 void
350 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
351 void *ret,
352 ffi_raw *args,
353 void *__this)
355 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
357 jobject rcv = (jobject) args[0].ptr;
358 JvSynchronize mutex (rcv);
360 run_debug (ret, args, _this);
363 void
364 _Jv_InterpMethod::run_class (ffi_cif *,
365 void *ret,
366 ffi_raw *args,
367 void *__this)
369 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
370 _Jv_InitClass (_this->defining_class);
371 run (ret, args, _this);
374 void
375 _Jv_InterpMethod::run_class_debug (ffi_cif *,
376 void *ret,
377 ffi_raw *args,
378 void *__this)
380 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
381 _Jv_InitClass (_this->defining_class);
382 run_debug (ret, args, _this);
385 void
386 _Jv_InterpMethod::run_synch_class (ffi_cif *,
387 void *ret,
388 ffi_raw *args,
389 void *__this)
391 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
393 jclass sync = _this->defining_class;
394 _Jv_InitClass (sync);
395 JvSynchronize mutex (sync);
397 run (ret, args, _this);
400 void
401 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
402 void *ret,
403 ffi_raw *args,
404 void *__this)
406 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
408 jclass sync = _this->defining_class;
409 _Jv_InitClass (sync);
410 JvSynchronize mutex (sync);
412 run_debug (ret, args, _this);
415 #ifdef DIRECT_THREADED
416 // "Compile" a method by turning it from bytecode to direct-threaded
417 // code.
418 void
419 _Jv_InterpMethod::compile (const void * const *insn_targets)
421 insn_slot *insns = NULL;
422 int next = 0;
423 unsigned char *codestart = bytecode ();
424 unsigned char *end = codestart + code_length;
425 _Jv_word *pool_data = defining_class->constants.data;
427 #define SET_ONE(Field, Value) \
428 do \
430 if (first_pass) \
431 ++next; \
432 else \
433 insns[next++].Field = Value; \
435 while (0)
437 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
438 #define SET_INT(Value) SET_ONE (int_val, Value)
439 #define SET_DATUM(Value) SET_ONE (datum, Value)
441 // Map from bytecode PC to slot in INSNS.
442 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
443 for (int i = 0; i < code_length; ++i)
444 pc_mapping[i] = -1;
446 for (int i = 0; i < 2; ++i)
448 jboolean first_pass = i == 0;
450 if (! first_pass)
452 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
453 number_insn_slots = next;
454 next = 0;
457 unsigned char *pc = codestart;
458 while (pc < end)
460 int base_pc_val = pc - codestart;
461 if (first_pass)
462 pc_mapping[base_pc_val] = next;
464 java_opcode opcode = (java_opcode) *pc++;
465 // Just elide NOPs.
466 if (opcode == op_nop)
467 continue;
468 SET_INSN (insn_targets[opcode]);
470 switch (opcode)
472 case op_nop:
473 case op_aconst_null:
474 case op_iconst_m1:
475 case op_iconst_0:
476 case op_iconst_1:
477 case op_iconst_2:
478 case op_iconst_3:
479 case op_iconst_4:
480 case op_iconst_5:
481 case op_lconst_0:
482 case op_lconst_1:
483 case op_fconst_0:
484 case op_fconst_1:
485 case op_fconst_2:
486 case op_dconst_0:
487 case op_dconst_1:
488 case op_iload_0:
489 case op_iload_1:
490 case op_iload_2:
491 case op_iload_3:
492 case op_lload_0:
493 case op_lload_1:
494 case op_lload_2:
495 case op_lload_3:
496 case op_fload_0:
497 case op_fload_1:
498 case op_fload_2:
499 case op_fload_3:
500 case op_dload_0:
501 case op_dload_1:
502 case op_dload_2:
503 case op_dload_3:
504 case op_aload_0:
505 case op_aload_1:
506 case op_aload_2:
507 case op_aload_3:
508 case op_iaload:
509 case op_laload:
510 case op_faload:
511 case op_daload:
512 case op_aaload:
513 case op_baload:
514 case op_caload:
515 case op_saload:
516 case op_istore_0:
517 case op_istore_1:
518 case op_istore_2:
519 case op_istore_3:
520 case op_lstore_0:
521 case op_lstore_1:
522 case op_lstore_2:
523 case op_lstore_3:
524 case op_fstore_0:
525 case op_fstore_1:
526 case op_fstore_2:
527 case op_fstore_3:
528 case op_dstore_0:
529 case op_dstore_1:
530 case op_dstore_2:
531 case op_dstore_3:
532 case op_astore_0:
533 case op_astore_1:
534 case op_astore_2:
535 case op_astore_3:
536 case op_iastore:
537 case op_lastore:
538 case op_fastore:
539 case op_dastore:
540 case op_aastore:
541 case op_bastore:
542 case op_castore:
543 case op_sastore:
544 case op_pop:
545 case op_pop2:
546 case op_dup:
547 case op_dup_x1:
548 case op_dup_x2:
549 case op_dup2:
550 case op_dup2_x1:
551 case op_dup2_x2:
552 case op_swap:
553 case op_iadd:
554 case op_isub:
555 case op_imul:
556 case op_idiv:
557 case op_irem:
558 case op_ishl:
559 case op_ishr:
560 case op_iushr:
561 case op_iand:
562 case op_ior:
563 case op_ixor:
564 case op_ladd:
565 case op_lsub:
566 case op_lmul:
567 case op_ldiv:
568 case op_lrem:
569 case op_lshl:
570 case op_lshr:
571 case op_lushr:
572 case op_land:
573 case op_lor:
574 case op_lxor:
575 case op_fadd:
576 case op_fsub:
577 case op_fmul:
578 case op_fdiv:
579 case op_frem:
580 case op_dadd:
581 case op_dsub:
582 case op_dmul:
583 case op_ddiv:
584 case op_drem:
585 case op_ineg:
586 case op_i2b:
587 case op_i2c:
588 case op_i2s:
589 case op_lneg:
590 case op_fneg:
591 case op_dneg:
592 case op_i2l:
593 case op_i2f:
594 case op_i2d:
595 case op_l2i:
596 case op_l2f:
597 case op_l2d:
598 case op_f2i:
599 case op_f2l:
600 case op_f2d:
601 case op_d2i:
602 case op_d2l:
603 case op_d2f:
604 case op_lcmp:
605 case op_fcmpl:
606 case op_fcmpg:
607 case op_dcmpl:
608 case op_dcmpg:
609 case op_monitorenter:
610 case op_monitorexit:
611 case op_ireturn:
612 case op_lreturn:
613 case op_freturn:
614 case op_dreturn:
615 case op_areturn:
616 case op_return:
617 case op_athrow:
618 case op_arraylength:
619 // No argument, nothing else to do.
620 break;
622 case op_bipush:
623 SET_INT (get1s (pc));
624 ++pc;
625 break;
627 case op_ldc:
629 int index = get1u (pc);
630 ++pc;
631 // For an unresolved class we want to delay resolution
632 // until execution.
633 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
635 --next;
636 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
637 SET_INT (index);
639 else
640 SET_DATUM (pool_data[index].o);
642 break;
644 case op_ret:
645 case op_iload:
646 case op_lload:
647 case op_fload:
648 case op_dload:
649 case op_aload:
650 case op_istore:
651 case op_lstore:
652 case op_fstore:
653 case op_dstore:
654 case op_astore:
655 case op_newarray:
656 SET_INT (get1u (pc));
657 ++pc;
658 break;
660 case op_iinc:
661 SET_INT (get1u (pc));
662 SET_INT (get1s (pc + 1));
663 pc += 2;
664 break;
666 case op_ldc_w:
668 int index = get2u (pc);
669 pc += 2;
670 // For an unresolved class we want to delay resolution
671 // until execution.
672 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
674 --next;
675 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
676 SET_INT (index);
678 else
679 SET_DATUM (pool_data[index].o);
681 break;
683 case op_ldc2_w:
685 int index = get2u (pc);
686 pc += 2;
687 SET_DATUM (&pool_data[index]);
689 break;
691 case op_sipush:
692 SET_INT (get2s (pc));
693 pc += 2;
694 break;
696 case op_new:
697 case op_getstatic:
698 case op_getfield:
699 case op_putfield:
700 case op_putstatic:
701 case op_anewarray:
702 case op_instanceof:
703 case op_checkcast:
704 case op_invokespecial:
705 case op_invokestatic:
706 case op_invokevirtual:
707 SET_INT (get2u (pc));
708 pc += 2;
709 break;
711 case op_multianewarray:
712 SET_INT (get2u (pc));
713 SET_INT (get1u (pc + 2));
714 pc += 3;
715 break;
717 case op_jsr:
718 case op_ifeq:
719 case op_ifne:
720 case op_iflt:
721 case op_ifge:
722 case op_ifgt:
723 case op_ifle:
724 case op_if_icmpeq:
725 case op_if_icmpne:
726 case op_if_icmplt:
727 case op_if_icmpge:
728 case op_if_icmpgt:
729 case op_if_icmple:
730 case op_if_acmpeq:
731 case op_if_acmpne:
732 case op_ifnull:
733 case op_ifnonnull:
734 case op_goto:
736 int offset = get2s (pc);
737 pc += 2;
739 int new_pc = base_pc_val + offset;
741 bool orig_was_goto = opcode == op_goto;
743 // Thread jumps. We limit the loop count; this lets
744 // us avoid infinite loops if the bytecode contains
745 // such. `10' is arbitrary.
746 int count = 10;
747 while (codestart[new_pc] == op_goto && count-- > 0)
748 new_pc += get2s (&codestart[new_pc + 1]);
750 // If the jump takes us to a `return' instruction and
751 // the original branch was an unconditional goto, then
752 // we hoist the return.
753 opcode = (java_opcode) codestart[new_pc];
754 if (orig_was_goto
755 && (opcode == op_ireturn || opcode == op_lreturn
756 || opcode == op_freturn || opcode == op_dreturn
757 || opcode == op_areturn || opcode == op_return))
759 --next;
760 SET_INSN (insn_targets[opcode]);
762 else
763 SET_DATUM (&insns[pc_mapping[new_pc]]);
765 break;
767 case op_tableswitch:
769 while ((pc - codestart) % 4 != 0)
770 ++pc;
772 jint def = get4 (pc);
773 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
774 pc += 4;
776 int low = get4 (pc);
777 SET_INT (low);
778 pc += 4;
779 int high = get4 (pc);
780 SET_INT (high);
781 pc += 4;
783 for (int i = low; i <= high; ++i)
785 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
786 pc += 4;
789 break;
791 case op_lookupswitch:
793 while ((pc - codestart) % 4 != 0)
794 ++pc;
796 jint def = get4 (pc);
797 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
798 pc += 4;
800 jint npairs = get4 (pc);
801 pc += 4;
802 SET_INT (npairs);
804 while (npairs-- > 0)
806 jint match = get4 (pc);
807 jint offset = get4 (pc + 4);
808 SET_INT (match);
809 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
810 pc += 8;
813 break;
815 case op_invokeinterface:
817 jint index = get2u (pc);
818 pc += 2;
819 // We ignore the next two bytes.
820 pc += 2;
821 SET_INT (index);
823 break;
825 case op_wide:
827 opcode = (java_opcode) get1u (pc);
828 pc += 1;
829 jint val = get2u (pc);
830 pc += 2;
832 // We implement narrow and wide instructions using the
833 // same code in the interpreter. So we rewrite the
834 // instruction slot here.
835 if (! first_pass)
836 insns[next - 1].insn = (void *) insn_targets[opcode];
837 SET_INT (val);
839 if (opcode == op_iinc)
841 SET_INT (get2s (pc));
842 pc += 2;
845 break;
847 case op_jsr_w:
848 case op_goto_w:
850 jint offset = get4 (pc);
851 pc += 4;
852 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
854 break;
856 // Some "can't happen" cases that we include for
857 // error-checking purposes.
858 case op_putfield_1:
859 case op_putfield_2:
860 case op_putfield_4:
861 case op_putfield_8:
862 case op_putfield_a:
863 case op_putstatic_1:
864 case op_putstatic_2:
865 case op_putstatic_4:
866 case op_putstatic_8:
867 case op_putstatic_a:
868 case op_getfield_1:
869 case op_getfield_2s:
870 case op_getfield_2u:
871 case op_getfield_4:
872 case op_getfield_8:
873 case op_getfield_a:
874 case op_getstatic_1:
875 case op_getstatic_2s:
876 case op_getstatic_2u:
877 case op_getstatic_4:
878 case op_getstatic_8:
879 case op_getstatic_a:
880 case op_breakpoint:
881 default:
882 // Fail somehow.
883 break;
888 // Now update exceptions.
889 _Jv_InterpException *exc = exceptions ();
890 for (int i = 0; i < exc_count; ++i)
892 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
893 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
894 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
895 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
896 // during compilation.
897 jclass handler
898 = (_Jv_Linker::resolve_pool_entry (defining_class,
899 exc[i].handler_type.i)).clazz;
900 exc[i].handler_type.p = handler;
903 // Translate entries in the LineNumberTable from bytecode PC's to direct
904 // threaded interpreter instruction values.
905 for (int i = 0; i < line_table_len; i++)
907 int byte_pc = line_table[i].bytecode_pc;
908 // It isn't worth throwing an exception if this table is
909 // corrupted, but at the same time we don't want a crash.
910 if (byte_pc < 0 || byte_pc >= code_length)
911 byte_pc = 0;
912 line_table[i].pc = &insns[pc_mapping[byte_pc]];
915 prepared = insns;
917 if (breakpoint_insn == NULL)
919 bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
920 breakpoint_insn = &bp_insn_slot;
923 #endif /* DIRECT_THREADED */
925 /* Run the given method.
926 When args is NULL, don't run anything -- just compile it. */
927 void
928 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
930 #undef DEBUG
931 #undef DEBUG_LOCALS_INSN
932 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
934 #include "interpret-run.cc"
937 void
938 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
940 #define DEBUG
941 #undef DEBUG_LOCALS_INSN
942 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
944 #include "interpret-run.cc"
947 static void
948 throw_internal_error (const char *msg)
950 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
953 static void
954 throw_incompatible_class_change_error (jstring msg)
956 throw new java::lang::IncompatibleClassChangeError (msg);
959 static void
960 throw_null_pointer_exception ()
962 throw new java::lang::NullPointerException;
965 /* Look up source code line number for given bytecode (or direct threaded
966 interpreter) PC. */
968 _Jv_InterpMethod::get_source_line(pc_t mpc)
970 int line = line_table_len > 0 ? line_table[0].line : -1;
971 for (int i = 1; i < line_table_len; i++)
972 if (line_table[i].pc > mpc)
973 break;
974 else
975 line = line_table[i].line;
977 return line;
980 /** Do static initialization for fields with a constant initializer */
981 void
982 _Jv_InitField (jobject obj, jclass klass, int index)
984 using namespace java::lang::reflect;
986 if (obj != 0 && klass == 0)
987 klass = obj->getClass ();
989 if (!_Jv_IsInterpretedClass (klass))
990 return;
992 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
994 _Jv_Field * field = (&klass->fields[0]) + index;
996 if (index > klass->field_count)
997 throw_internal_error ("field out of range");
999 int init = iclass->field_initializers[index];
1000 if (init == 0)
1001 return;
1003 _Jv_Constants *pool = &klass->constants;
1004 int tag = pool->tags[init];
1006 if (! field->isResolved ())
1007 throw_internal_error ("initializing unresolved field");
1009 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
1010 throw_internal_error ("initializing non-static field with no object");
1012 void *addr = 0;
1014 if ((field->flags & Modifier::STATIC) != 0)
1015 addr = (void*) field->u.addr;
1016 else
1017 addr = (void*) (((char*)obj) + field->u.boffset);
1019 switch (tag)
1021 case JV_CONSTANT_String:
1023 jstring str;
1024 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1025 pool->data[init].string = str;
1026 pool->tags[init] = JV_CONSTANT_ResolvedString;
1028 /* fall through */
1030 case JV_CONSTANT_ResolvedString:
1031 if (! (field->type == &java::lang::String::class$
1032 || field->type == &java::lang::Class::class$))
1033 throw_class_format_error ("string initialiser to non-string field");
1035 *(jstring*)addr = pool->data[init].string;
1036 break;
1038 case JV_CONSTANT_Integer:
1040 int value = pool->data[init].i;
1042 if (field->type == JvPrimClass (boolean))
1043 *(jboolean*)addr = (jboolean)value;
1045 else if (field->type == JvPrimClass (byte))
1046 *(jbyte*)addr = (jbyte)value;
1048 else if (field->type == JvPrimClass (char))
1049 *(jchar*)addr = (jchar)value;
1051 else if (field->type == JvPrimClass (short))
1052 *(jshort*)addr = (jshort)value;
1054 else if (field->type == JvPrimClass (int))
1055 *(jint*)addr = (jint)value;
1057 else
1058 throw_class_format_error ("erroneous field initializer");
1060 break;
1062 case JV_CONSTANT_Long:
1063 if (field->type != JvPrimClass (long))
1064 throw_class_format_error ("erroneous field initializer");
1066 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1067 break;
1069 case JV_CONSTANT_Float:
1070 if (field->type != JvPrimClass (float))
1071 throw_class_format_error ("erroneous field initializer");
1073 *(jfloat*)addr = pool->data[init].f;
1074 break;
1076 case JV_CONSTANT_Double:
1077 if (field->type != JvPrimClass (double))
1078 throw_class_format_error ("erroneous field initializer");
1080 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1081 break;
1083 default:
1084 throw_class_format_error ("erroneous field initializer");
1088 inline static unsigned char*
1089 skip_one_type (unsigned char* ptr)
1091 int ch = *ptr++;
1093 while (ch == '[')
1095 ch = *ptr++;
1098 if (ch == 'L')
1100 do { ch = *ptr++; } while (ch != ';');
1103 return ptr;
1106 static ffi_type*
1107 get_ffi_type_from_signature (unsigned char* ptr)
1109 switch (*ptr)
1111 case 'L':
1112 case '[':
1113 return &ffi_type_pointer;
1114 break;
1116 case 'Z':
1117 // On some platforms a bool is a byte, on others an int.
1118 if (sizeof (jboolean) == sizeof (jbyte))
1119 return &ffi_type_sint8;
1120 else
1122 JvAssert (sizeof (jbyte) == sizeof (jint));
1123 return &ffi_type_sint32;
1125 break;
1127 case 'B':
1128 return &ffi_type_sint8;
1129 break;
1131 case 'C':
1132 return &ffi_type_uint16;
1133 break;
1135 case 'S':
1136 return &ffi_type_sint16;
1137 break;
1139 case 'I':
1140 return &ffi_type_sint32;
1141 break;
1143 case 'J':
1144 return &ffi_type_sint64;
1145 break;
1147 case 'F':
1148 return &ffi_type_float;
1149 break;
1151 case 'D':
1152 return &ffi_type_double;
1153 break;
1155 case 'V':
1156 return &ffi_type_void;
1157 break;
1160 throw_internal_error ("unknown type in signature");
1163 /* this function yields the number of actual arguments, that is, if the
1164 * function is non-static, then one is added to the number of elements
1165 * found in the signature */
1167 int
1168 _Jv_count_arguments (_Jv_Utf8Const *signature,
1169 jboolean staticp)
1171 unsigned char *ptr = (unsigned char*) signature->chars();
1172 int arg_count = staticp ? 0 : 1;
1174 /* first, count number of arguments */
1176 // skip '('
1177 ptr++;
1179 // count args
1180 while (*ptr != ')')
1182 ptr = skip_one_type (ptr);
1183 arg_count += 1;
1186 return arg_count;
1189 /* This beast will build a cif, given the signature. Memory for
1190 * the cif itself and for the argument types must be allocated by the
1191 * caller.
1194 int
1195 _Jv_init_cif (_Jv_Utf8Const* signature,
1196 int arg_count,
1197 jboolean staticp,
1198 ffi_cif *cif,
1199 ffi_type **arg_types,
1200 ffi_type **rtype_p)
1202 unsigned char *ptr = (unsigned char*) signature->chars();
1204 int arg_index = 0; // arg number
1205 int item_count = 0; // stack-item count
1207 // setup receiver
1208 if (!staticp)
1210 arg_types[arg_index++] = &ffi_type_pointer;
1211 item_count += 1;
1214 // skip '('
1215 ptr++;
1217 // assign arg types
1218 while (*ptr != ')')
1220 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1222 if (*ptr == 'J' || *ptr == 'D')
1223 item_count += 2;
1224 else
1225 item_count += 1;
1227 ptr = skip_one_type (ptr);
1230 // skip ')'
1231 ptr++;
1232 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1234 ptr = skip_one_type (ptr);
1235 if (ptr != (unsigned char*)signature->chars() + signature->len())
1236 throw_internal_error ("did not find end of signature");
1238 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1239 arg_count, rtype, arg_types) != FFI_OK)
1240 throw_internal_error ("ffi_prep_cif failed");
1242 if (rtype_p != NULL)
1243 *rtype_p = rtype;
1245 return item_count;
1248 #if FFI_NATIVE_RAW_API
1249 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1250 # define FFI_RAW_SIZE ffi_raw_size
1251 #else
1252 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1253 # define FFI_RAW_SIZE ffi_java_raw_size
1254 #endif
1256 /* we put this one here, and not in interpret.cc because it
1257 * calls the utility routines _Jv_count_arguments
1258 * which are static to this module. The following struct defines the
1259 * layout we use for the stubs, it's only used in the ncode method. */
1261 typedef struct {
1262 ffi_raw_closure closure;
1263 ffi_cif cif;
1264 ffi_type *arg_types[0];
1265 } ncode_closure;
1267 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1269 void *
1270 _Jv_InterpMethod::ncode ()
1272 using namespace java::lang::reflect;
1274 if (self->ncode != 0)
1275 return self->ncode;
1277 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1278 int arg_count = _Jv_count_arguments (self->signature, staticp);
1280 ncode_closure *closure =
1281 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1282 + arg_count * sizeof (ffi_type*));
1284 _Jv_init_cif (self->signature,
1285 arg_count,
1286 staticp,
1287 &closure->cif,
1288 &closure->arg_types[0],
1289 NULL);
1291 ffi_closure_fun fun;
1293 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1295 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1297 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1299 if (staticp)
1301 if (JVMTI::enabled)
1302 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1303 else
1304 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1306 else
1308 if (JVMTI::enabled)
1309 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1310 else
1311 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1314 else
1316 if (staticp)
1318 if (JVMTI::enabled)
1319 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1320 else
1321 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1323 else
1325 if (JVMTI::enabled)
1326 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1327 else
1328 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1332 FFI_PREP_RAW_CLOSURE (&closure->closure,
1333 &closure->cif,
1334 fun,
1335 (void*)this);
1337 self->ncode = (void*)closure;
1338 return self->ncode;
1341 /* Find the index of the given insn in the array of insn slots
1342 for this method. Returns -1 if not found. */
1343 jlong
1344 _Jv_InterpMethod::insn_index (pc_t pc)
1346 jlong left = 0;
1347 #ifdef DIRECT_THREADED
1348 jlong right = number_insn_slots;
1349 pc_t insns = prepared;
1350 #else
1351 jlong right = code_length;
1352 pc_t insns = bytecode ();
1353 #endif
1355 while (right >= 0)
1357 jlong mid = (left + right) / 2;
1358 if (&insns[mid] == pc)
1359 return mid;
1361 if (pc < &insns[mid])
1362 right = mid - 1;
1363 else
1364 left = mid + 1;
1367 return -1;
1370 // Method to check if an exception is caught at some location in a method
1371 // (meth). Returns true if this method (meth) contains a catch block for the
1372 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1373 // to the location of the beginning of the catch block.
1374 jboolean
1375 _Jv_InterpMethod::check_handler (pc_t *pc, _Jv_InterpMethod *meth,
1376 java::lang::Throwable *ex)
1378 #ifdef DIRECT_THREADED
1379 void *logical_pc = (void *) ((insn_slot *) (*pc) - 1);
1380 #else
1381 int logical_pc = (*pc) - 1 - meth->bytecode ();
1382 #endif
1383 _Jv_InterpException *exc = meth->exceptions ();
1384 jclass exc_class = ex->getClass ();
1386 for (int i = 0; i < meth->exc_count; i++)
1388 if (PCVAL (exc[i].start_pc) <= logical_pc
1389 && logical_pc < PCVAL (exc[i].end_pc))
1391 #ifdef DIRECT_THREADED
1392 jclass handler = (jclass) exc[i].handler_type.p;
1393 #else
1394 jclass handler = NULL;
1395 if (exc[i].handler_type.i != 0)
1396 handler
1397 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1399 #endif /* DIRECT_THREADED */
1400 if (handler == NULL || handler->isAssignableFrom (exc_class))
1402 #ifdef DIRECT_THREADED
1403 (*pc) = (insn_slot *) exc[i].handler_pc.p;
1404 #else
1405 (*pc) = meth->bytecode () + exc[i].handler_pc.i;
1406 #endif /* DIRECT_THREADED */
1407 return true;
1411 return false;
1415 void
1416 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1417 jintArray& line_numbers,
1418 jlongArray& code_indices)
1420 #ifdef DIRECT_THREADED
1421 /* For the DIRECT_THREADED case, if the method has not yet been
1422 * compiled, the linetable will change to insn slots instead of
1423 * bytecode PCs. It is probably easiest, in this case, to simply
1424 * compile the method and guarantee that we are using insn
1425 * slots.
1427 _Jv_CompileMethod (this);
1429 if (line_table_len > 0)
1431 start = 0;
1432 end = number_insn_slots;
1433 line_numbers = JvNewIntArray (line_table_len);
1434 code_indices = JvNewLongArray (line_table_len);
1436 jint* lines = elements (line_numbers);
1437 jlong* indices = elements (code_indices);
1438 for (int i = 0; i < line_table_len; ++i)
1440 lines[i] = line_table[i].line;
1441 indices[i] = insn_index (line_table[i].pc);
1444 #else // !DIRECT_THREADED
1445 if (line_table_len > 0)
1447 start = 0;
1448 end = code_length;
1449 line_numbers = JvNewIntArray (line_table_len);
1450 code_indices = JvNewLongArray (line_table_len);
1452 jint* lines = elements (line_numbers);
1453 jlong* indices = elements (code_indices);
1454 for (int i = 0; i < line_table_len; ++i)
1456 lines[i] = line_table[i].line;
1457 indices[i] = (jlong) line_table[i].bytecode_pc;
1460 #endif // !DIRECT_THREADED
1463 int
1464 _Jv_InterpMethod::get_local_var_table (char **name, char **sig,
1465 char **generic_sig, jlong *startloc,
1466 jint *length, jint *slot,
1467 int table_slot)
1469 if (local_var_table == NULL)
1470 return -2;
1471 if (table_slot >= local_var_table_len)
1472 return -1;
1473 else
1475 *name = local_var_table[table_slot].name;
1476 *sig = local_var_table[table_slot].descriptor;
1477 *generic_sig = local_var_table[table_slot].descriptor;
1479 *startloc = static_cast<jlong>
1480 (local_var_table[table_slot].bytecode_start_pc);
1481 *length = static_cast<jint> (local_var_table[table_slot].length);
1482 *slot = static_cast<jint> (local_var_table[table_slot].slot);
1484 return local_var_table_len - table_slot -1;
1487 pc_t
1488 _Jv_InterpMethod::install_break (jlong index)
1490 return set_insn (index, breakpoint_insn);
1493 pc_t
1494 _Jv_InterpMethod::get_insn (jlong index)
1496 pc_t code;
1498 #ifdef DIRECT_THREADED
1499 if (index >= number_insn_slots || index < 0)
1500 return NULL;
1502 code = prepared;
1503 #else // !DIRECT_THREADED
1504 if (index >= code_length || index < 0)
1505 return NULL;
1507 code = reinterpret_cast<pc_t> (bytecode ());
1508 #endif // !DIRECT_THREADED
1510 return &code[index];
1513 pc_t
1514 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1516 #ifdef DIRECT_THREADED
1517 if (index >= number_insn_slots || index < 0)
1518 return NULL;
1520 pc_t code = prepared;
1521 code[index].insn = insn->insn;
1522 #else // !DIRECT_THREADED
1523 if (index >= code_length || index < 0)
1524 return NULL;
1526 pc_t code = reinterpret_cast<pc_t> (bytecode ());
1527 code[index] = *insn;
1528 #endif // !DIRECT_THREADED
1530 return &code[index];
1533 void *
1534 _Jv_JNIMethod::ncode ()
1536 using namespace java::lang::reflect;
1538 if (self->ncode != 0)
1539 return self->ncode;
1541 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1542 int arg_count = _Jv_count_arguments (self->signature, staticp);
1544 ncode_closure *closure =
1545 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1546 + arg_count * sizeof (ffi_type*));
1548 ffi_type *rtype;
1549 _Jv_init_cif (self->signature,
1550 arg_count,
1551 staticp,
1552 &closure->cif,
1553 &closure->arg_types[0],
1554 &rtype);
1556 ffi_closure_fun fun;
1558 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1560 // Initialize the argument types and CIF that represent the actual
1561 // underlying JNI function.
1562 int extra_args = 1;
1563 if ((self->accflags & Modifier::STATIC))
1564 ++extra_args;
1565 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1566 * sizeof (ffi_type *));
1567 int offset = 0;
1568 jni_arg_types[offset++] = &ffi_type_pointer;
1569 if ((self->accflags & Modifier::STATIC))
1570 jni_arg_types[offset++] = &ffi_type_pointer;
1571 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1572 arg_count * sizeof (ffi_type *));
1574 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1575 extra_args + arg_count, rtype,
1576 jni_arg_types) != FFI_OK)
1577 throw_internal_error ("ffi_prep_cif failed for JNI function");
1579 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1581 // FIXME: for now we assume that all native methods for
1582 // interpreted code use JNI.
1583 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1585 FFI_PREP_RAW_CLOSURE (&closure->closure,
1586 &closure->cif,
1587 fun,
1588 (void*) this);
1590 self->ncode = (void *) closure;
1591 return self->ncode;
1594 static void
1595 throw_class_format_error (jstring msg)
1597 throw (msg
1598 ? new java::lang::ClassFormatError (msg)
1599 : new java::lang::ClassFormatError);
1602 static void
1603 throw_class_format_error (const char *msg)
1605 throw_class_format_error (JvNewStringLatin1 (msg));
1610 void
1611 _Jv_InterpreterEngine::do_verify (jclass klass)
1613 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1614 for (int i = 0; i < klass->method_count; i++)
1616 using namespace java::lang::reflect;
1617 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1618 _Jv_ushort accflags = klass->methods[i].accflags;
1619 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1621 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1622 _Jv_VerifyMethod (im);
1627 void
1628 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1630 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1631 for (int i = 0; i < klass->method_count; i++)
1633 // Just skip abstract methods. This is particularly important
1634 // because we don't resize the interpreted_methods array when
1635 // miranda methods are added to it.
1636 if ((klass->methods[i].accflags
1637 & java::lang::reflect::Modifier::ABSTRACT)
1638 != 0)
1639 continue;
1641 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1643 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1644 != 0)
1646 // You might think we could use a virtual `ncode' method in
1647 // the _Jv_MethodBase and unify the native and non-native
1648 // cases. Well, we can't, because we don't allocate these
1649 // objects using `new', and thus they don't get a vtable.
1650 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1651 klass->methods[i].ncode = jnim->ncode ();
1653 else if (imeth != 0) // it could be abstract
1655 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1656 klass->methods[i].ncode = im->ncode ();
1661 void
1662 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1663 int pointer_size,
1664 int other_size)
1666 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1668 // Splitting the allocations here lets us scan reference fields and
1669 // avoid scanning non-reference fields. How reference fields are
1670 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1671 // means that this memory will be scanned conservatively (same
1672 // difference, since we know all the contents here are pointers).
1673 // Then we put pointers into this memory into the 'fields'
1674 // structure. Most of these are interior pointers, which is ok (but
1675 // even so the pointer to the first reference field will be used and
1676 // that is not an interior pointer). The 'fields' array is also
1677 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1678 // be scanned. A pointer to this array is held by Class and thus
1679 // seen by the collector.
1680 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1681 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1683 for (int i = 0; i < klass->field_count; i++)
1685 _Jv_Field *field = &klass->fields[i];
1687 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1688 continue;
1690 char *base = field->isRef() ? reference_fields : non_reference_fields;
1691 field->u.addr = base + field->u.boffset;
1693 if (iclass->field_initializers[i] != 0)
1695 _Jv_Linker::resolve_field (field, klass->loader);
1696 _Jv_InitField (0, klass, i);
1700 // Now we don't need the field_initializers anymore, so let the
1701 // collector get rid of it.
1702 iclass->field_initializers = 0;
1705 _Jv_ResolvedMethod *
1706 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1707 jboolean staticp)
1709 int arg_count = _Jv_count_arguments (method->signature, staticp);
1711 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1712 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1713 + arg_count*sizeof (ffi_type*));
1715 result->stack_item_count
1716 = _Jv_init_cif (method->signature,
1717 arg_count,
1718 staticp,
1719 &result->cif,
1720 &result->arg_types[0],
1721 NULL);
1723 result->method = method;
1724 result->klass = klass;
1726 return result;
1729 void
1730 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1732 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1733 for (int i = 0; i < klass->method_count; i++)
1735 // Just skip abstract methods. This is particularly important
1736 // because we don't resize the interpreted_methods array when
1737 // miranda methods are added to it.
1738 if ((klass->methods[i].accflags
1739 & java::lang::reflect::Modifier::ABSTRACT)
1740 != 0)
1741 continue;
1742 // Miranda method additions mean that the `methods' array moves.
1743 // We cache a pointer into this array, so we have to update.
1744 iclass->interpreted_methods[i]->self = &klass->methods[i];
1748 #ifdef DIRECT_THREADED
1749 void
1750 _Jv_CompileMethod (_Jv_InterpMethod* method)
1752 if (method->prepared == NULL)
1754 if (JVMTI::enabled)
1755 _Jv_InterpMethod::run_debug (NULL, NULL, method);
1756 else
1757 _Jv_InterpMethod::run (NULL, NULL, method);
1760 #endif // DIRECT_THREADED
1762 #endif // INTERPRETER