2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libjava / interpret.cc
blob79276258c3d1280fefe53d6f53cd074ed8ff1862
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 DEBUG_LOCALS_INSN (I + 1, 'x'); \
195 (sp -= 2, locals[I].l = sp->l); \
196 } while (0)
197 # define STORED(I) \
198 do { \
199 DEBUG_LOCALS_INSN (I, 'd'); \
200 DEBUG_LOCALS_INSN (I + 1, 'x'); \
201 (sp -= 2, locals[I].d = sp->d); \
202 } while (0)
204 #else
205 # define STOREL(I) \
206 do { \
207 DEBUG_LOCALS_INSN (I, 'l'); \
208 DEBUG_LOCALS_INSN (I + 1, 'x'); \
209 jint __idx = (I); \
210 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
211 locals[__idx].ia[0] = (--sp)->ia[0]; \
212 } while (0)
213 # define STORED(I) \
214 do { \
215 DEBUG_LOCALS_INSN (I, 'd'); \
216 DEBUG_LOCALS_INSN (I + 1, 'x'); \
217 jint __idx = (I); \
218 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
219 locals[__idx].ia[0] = (--sp)->ia[0]; \
220 } while (0)
221 #endif
223 #define PEEKI(I) (locals+(I))->i
224 #define PEEKA(I) (locals+(I))->o
226 #define POKEI(I,V) \
227 DEBUG_LOCALS_INSN(I,'i'); \
228 ((locals+(I))->i = (V))
231 #define BINOPI(OP) { \
232 jint value2 = POPI(); \
233 jint value1 = POPI(); \
234 PUSHI(value1 OP value2); \
237 #define BINOPF(OP) { \
238 jfloat value2 = POPF(); \
239 jfloat value1 = POPF(); \
240 PUSHF(value1 OP value2); \
243 #define BINOPL(OP) { \
244 jlong value2 = POPL(); \
245 jlong value1 = POPL(); \
246 PUSHL(value1 OP value2); \
249 #define BINOPD(OP) { \
250 jdouble value2 = POPD(); \
251 jdouble value1 = POPD(); \
252 PUSHD(value1 OP value2); \
255 static inline jint
256 get1s (unsigned char* loc)
258 return *(signed char*)loc;
261 static inline jint
262 get1u (unsigned char* loc)
264 return *loc;
267 static inline jint
268 get2s(unsigned char* loc)
270 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
273 static inline jint
274 get2u (unsigned char* loc)
276 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
279 static jint
280 get4 (unsigned char* loc)
282 return (((jint)(loc[0])) << 24)
283 | (((jint)(loc[1])) << 16)
284 | (((jint)(loc[2])) << 8)
285 | (((jint)(loc[3])) << 0);
288 #define SAVE_PC() frame_desc.pc = pc
290 // We used to define this conditionally, depending on HANDLE_SEGV.
291 // However, that runs into a problem if a chunk in low memory is
292 // mapped and we try to look at a field near the end of a large
293 // object. See PR 26858 for details. It is, most likely, relatively
294 // inexpensive to simply do this check always.
295 #define NULLCHECK(X) \
296 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
298 // Note that we can still conditionally define NULLARRAYCHECK, since
299 // we know that all uses of an array will first reference the length
300 // field, which is first -- and thus will trigger a SEGV.
301 #ifdef HANDLE_SEGV
302 #define NULLARRAYCHECK(X) SAVE_PC()
303 #else
304 #define NULLARRAYCHECK(X) \
305 do \
307 SAVE_PC(); \
308 if ((X) == NULL) { throw_null_pointer_exception (); } \
309 } while (0)
310 #endif
312 #define ARRAYBOUNDSCHECK(array, index) \
313 do \
315 if (((unsigned) index) >= (unsigned) (array->length)) \
316 _Jv_ThrowBadArrayIndex (index); \
317 } while (0)
319 void
320 _Jv_InterpMethod::run_normal (ffi_cif *,
321 void *ret,
322 ffi_raw *args,
323 void *__this)
325 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
326 run (ret, args, _this);
329 void
330 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
331 void *ret,
332 ffi_raw *args,
333 void *__this)
335 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
336 run_debug (ret, args, _this);
339 void
340 _Jv_InterpMethod::run_synch_object (ffi_cif *,
341 void *ret,
342 ffi_raw *args,
343 void *__this)
345 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
347 jobject rcv = (jobject) args[0].ptr;
348 JvSynchronize mutex (rcv);
350 run (ret, args, _this);
353 void
354 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
355 void *ret,
356 ffi_raw *args,
357 void *__this)
359 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
361 jobject rcv = (jobject) args[0].ptr;
362 JvSynchronize mutex (rcv);
364 run_debug (ret, args, _this);
367 void
368 _Jv_InterpMethod::run_class (ffi_cif *,
369 void *ret,
370 ffi_raw *args,
371 void *__this)
373 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
374 _Jv_InitClass (_this->defining_class);
375 run (ret, args, _this);
378 void
379 _Jv_InterpMethod::run_class_debug (ffi_cif *,
380 void *ret,
381 ffi_raw *args,
382 void *__this)
384 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
385 _Jv_InitClass (_this->defining_class);
386 run_debug (ret, args, _this);
389 void
390 _Jv_InterpMethod::run_synch_class (ffi_cif *,
391 void *ret,
392 ffi_raw *args,
393 void *__this)
395 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
397 jclass sync = _this->defining_class;
398 _Jv_InitClass (sync);
399 JvSynchronize mutex (sync);
401 run (ret, args, _this);
404 void
405 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
406 void *ret,
407 ffi_raw *args,
408 void *__this)
410 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
412 jclass sync = _this->defining_class;
413 _Jv_InitClass (sync);
414 JvSynchronize mutex (sync);
416 run_debug (ret, args, _this);
419 #ifdef DIRECT_THREADED
420 // "Compile" a method by turning it from bytecode to direct-threaded
421 // code.
422 void
423 _Jv_InterpMethod::compile (const void * const *insn_targets)
425 insn_slot *insns = NULL;
426 int next = 0;
427 unsigned char *codestart = bytecode ();
428 unsigned char *end = codestart + code_length;
429 _Jv_word *pool_data = defining_class->constants.data;
431 #define SET_ONE(Field, Value) \
432 do \
434 if (first_pass) \
435 ++next; \
436 else \
437 insns[next++].Field = Value; \
439 while (0)
441 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
442 #define SET_INT(Value) SET_ONE (int_val, Value)
443 #define SET_DATUM(Value) SET_ONE (datum, Value)
445 // Map from bytecode PC to slot in INSNS.
446 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
447 for (int i = 0; i < code_length; ++i)
448 pc_mapping[i] = -1;
450 for (int i = 0; i < 2; ++i)
452 jboolean first_pass = i == 0;
454 if (! first_pass)
456 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
457 number_insn_slots = next;
458 next = 0;
461 unsigned char *pc = codestart;
462 while (pc < end)
464 int base_pc_val = pc - codestart;
465 if (first_pass)
466 pc_mapping[base_pc_val] = next;
468 java_opcode opcode = (java_opcode) *pc++;
469 // Just elide NOPs.
470 if (opcode == op_nop)
471 continue;
472 SET_INSN (insn_targets[opcode]);
474 switch (opcode)
476 case op_nop:
477 case op_aconst_null:
478 case op_iconst_m1:
479 case op_iconst_0:
480 case op_iconst_1:
481 case op_iconst_2:
482 case op_iconst_3:
483 case op_iconst_4:
484 case op_iconst_5:
485 case op_lconst_0:
486 case op_lconst_1:
487 case op_fconst_0:
488 case op_fconst_1:
489 case op_fconst_2:
490 case op_dconst_0:
491 case op_dconst_1:
492 case op_iload_0:
493 case op_iload_1:
494 case op_iload_2:
495 case op_iload_3:
496 case op_lload_0:
497 case op_lload_1:
498 case op_lload_2:
499 case op_lload_3:
500 case op_fload_0:
501 case op_fload_1:
502 case op_fload_2:
503 case op_fload_3:
504 case op_dload_0:
505 case op_dload_1:
506 case op_dload_2:
507 case op_dload_3:
508 case op_aload_0:
509 case op_aload_1:
510 case op_aload_2:
511 case op_aload_3:
512 case op_iaload:
513 case op_laload:
514 case op_faload:
515 case op_daload:
516 case op_aaload:
517 case op_baload:
518 case op_caload:
519 case op_saload:
520 case op_istore_0:
521 case op_istore_1:
522 case op_istore_2:
523 case op_istore_3:
524 case op_lstore_0:
525 case op_lstore_1:
526 case op_lstore_2:
527 case op_lstore_3:
528 case op_fstore_0:
529 case op_fstore_1:
530 case op_fstore_2:
531 case op_fstore_3:
532 case op_dstore_0:
533 case op_dstore_1:
534 case op_dstore_2:
535 case op_dstore_3:
536 case op_astore_0:
537 case op_astore_1:
538 case op_astore_2:
539 case op_astore_3:
540 case op_iastore:
541 case op_lastore:
542 case op_fastore:
543 case op_dastore:
544 case op_aastore:
545 case op_bastore:
546 case op_castore:
547 case op_sastore:
548 case op_pop:
549 case op_pop2:
550 case op_dup:
551 case op_dup_x1:
552 case op_dup_x2:
553 case op_dup2:
554 case op_dup2_x1:
555 case op_dup2_x2:
556 case op_swap:
557 case op_iadd:
558 case op_isub:
559 case op_imul:
560 case op_idiv:
561 case op_irem:
562 case op_ishl:
563 case op_ishr:
564 case op_iushr:
565 case op_iand:
566 case op_ior:
567 case op_ixor:
568 case op_ladd:
569 case op_lsub:
570 case op_lmul:
571 case op_ldiv:
572 case op_lrem:
573 case op_lshl:
574 case op_lshr:
575 case op_lushr:
576 case op_land:
577 case op_lor:
578 case op_lxor:
579 case op_fadd:
580 case op_fsub:
581 case op_fmul:
582 case op_fdiv:
583 case op_frem:
584 case op_dadd:
585 case op_dsub:
586 case op_dmul:
587 case op_ddiv:
588 case op_drem:
589 case op_ineg:
590 case op_i2b:
591 case op_i2c:
592 case op_i2s:
593 case op_lneg:
594 case op_fneg:
595 case op_dneg:
596 case op_i2l:
597 case op_i2f:
598 case op_i2d:
599 case op_l2i:
600 case op_l2f:
601 case op_l2d:
602 case op_f2i:
603 case op_f2l:
604 case op_f2d:
605 case op_d2i:
606 case op_d2l:
607 case op_d2f:
608 case op_lcmp:
609 case op_fcmpl:
610 case op_fcmpg:
611 case op_dcmpl:
612 case op_dcmpg:
613 case op_monitorenter:
614 case op_monitorexit:
615 case op_ireturn:
616 case op_lreturn:
617 case op_freturn:
618 case op_dreturn:
619 case op_areturn:
620 case op_return:
621 case op_athrow:
622 case op_arraylength:
623 // No argument, nothing else to do.
624 break;
626 case op_bipush:
627 SET_INT (get1s (pc));
628 ++pc;
629 break;
631 case op_ldc:
633 int index = get1u (pc);
634 ++pc;
635 // For an unresolved class we want to delay resolution
636 // until execution.
637 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
639 --next;
640 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
641 SET_INT (index);
643 else
644 SET_DATUM (pool_data[index].o);
646 break;
648 case op_ret:
649 case op_iload:
650 case op_lload:
651 case op_fload:
652 case op_dload:
653 case op_aload:
654 case op_istore:
655 case op_lstore:
656 case op_fstore:
657 case op_dstore:
658 case op_astore:
659 case op_newarray:
660 SET_INT (get1u (pc));
661 ++pc;
662 break;
664 case op_iinc:
665 SET_INT (get1u (pc));
666 SET_INT (get1s (pc + 1));
667 pc += 2;
668 break;
670 case op_ldc_w:
672 int index = get2u (pc);
673 pc += 2;
674 // For an unresolved class we want to delay resolution
675 // until execution.
676 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
678 --next;
679 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
680 SET_INT (index);
682 else
683 SET_DATUM (pool_data[index].o);
685 break;
687 case op_ldc2_w:
689 int index = get2u (pc);
690 pc += 2;
691 SET_DATUM (&pool_data[index]);
693 break;
695 case op_sipush:
696 SET_INT (get2s (pc));
697 pc += 2;
698 break;
700 case op_new:
701 case op_getstatic:
702 case op_getfield:
703 case op_putfield:
704 case op_putstatic:
705 case op_anewarray:
706 case op_instanceof:
707 case op_checkcast:
708 case op_invokespecial:
709 case op_invokestatic:
710 case op_invokevirtual:
711 SET_INT (get2u (pc));
712 pc += 2;
713 break;
715 case op_multianewarray:
716 SET_INT (get2u (pc));
717 SET_INT (get1u (pc + 2));
718 pc += 3;
719 break;
721 case op_jsr:
722 case op_ifeq:
723 case op_ifne:
724 case op_iflt:
725 case op_ifge:
726 case op_ifgt:
727 case op_ifle:
728 case op_if_icmpeq:
729 case op_if_icmpne:
730 case op_if_icmplt:
731 case op_if_icmpge:
732 case op_if_icmpgt:
733 case op_if_icmple:
734 case op_if_acmpeq:
735 case op_if_acmpne:
736 case op_ifnull:
737 case op_ifnonnull:
738 case op_goto:
740 int offset = get2s (pc);
741 pc += 2;
743 int new_pc = base_pc_val + offset;
745 bool orig_was_goto = opcode == op_goto;
747 // Thread jumps. We limit the loop count; this lets
748 // us avoid infinite loops if the bytecode contains
749 // such. `10' is arbitrary.
750 int count = 10;
751 while (codestart[new_pc] == op_goto && count-- > 0)
752 new_pc += get2s (&codestart[new_pc + 1]);
754 // If the jump takes us to a `return' instruction and
755 // the original branch was an unconditional goto, then
756 // we hoist the return.
757 opcode = (java_opcode) codestart[new_pc];
758 if (orig_was_goto
759 && (opcode == op_ireturn || opcode == op_lreturn
760 || opcode == op_freturn || opcode == op_dreturn
761 || opcode == op_areturn || opcode == op_return))
763 --next;
764 SET_INSN (insn_targets[opcode]);
766 else
767 SET_DATUM (&insns[pc_mapping[new_pc]]);
769 break;
771 case op_tableswitch:
773 while ((pc - codestart) % 4 != 0)
774 ++pc;
776 jint def = get4 (pc);
777 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
778 pc += 4;
780 int low = get4 (pc);
781 SET_INT (low);
782 pc += 4;
783 int high = get4 (pc);
784 SET_INT (high);
785 pc += 4;
787 for (int i = low; i <= high; ++i)
789 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
790 pc += 4;
793 break;
795 case op_lookupswitch:
797 while ((pc - codestart) % 4 != 0)
798 ++pc;
800 jint def = get4 (pc);
801 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
802 pc += 4;
804 jint npairs = get4 (pc);
805 pc += 4;
806 SET_INT (npairs);
808 while (npairs-- > 0)
810 jint match = get4 (pc);
811 jint offset = get4 (pc + 4);
812 SET_INT (match);
813 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
814 pc += 8;
817 break;
819 case op_invokeinterface:
821 jint index = get2u (pc);
822 pc += 2;
823 // We ignore the next two bytes.
824 pc += 2;
825 SET_INT (index);
827 break;
829 case op_wide:
831 opcode = (java_opcode) get1u (pc);
832 pc += 1;
833 jint val = get2u (pc);
834 pc += 2;
836 // We implement narrow and wide instructions using the
837 // same code in the interpreter. So we rewrite the
838 // instruction slot here.
839 if (! first_pass)
840 insns[next - 1].insn = (void *) insn_targets[opcode];
841 SET_INT (val);
843 if (opcode == op_iinc)
845 SET_INT (get2s (pc));
846 pc += 2;
849 break;
851 case op_jsr_w:
852 case op_goto_w:
854 jint offset = get4 (pc);
855 pc += 4;
856 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
858 break;
860 // Some "can't happen" cases that we include for
861 // error-checking purposes.
862 case op_putfield_1:
863 case op_putfield_2:
864 case op_putfield_4:
865 case op_putfield_8:
866 case op_putfield_a:
867 case op_putstatic_1:
868 case op_putstatic_2:
869 case op_putstatic_4:
870 case op_putstatic_8:
871 case op_putstatic_a:
872 case op_getfield_1:
873 case op_getfield_2s:
874 case op_getfield_2u:
875 case op_getfield_4:
876 case op_getfield_8:
877 case op_getfield_a:
878 case op_getstatic_1:
879 case op_getstatic_2s:
880 case op_getstatic_2u:
881 case op_getstatic_4:
882 case op_getstatic_8:
883 case op_getstatic_a:
884 case op_breakpoint:
885 default:
886 // Fail somehow.
887 break;
892 // Now update exceptions.
893 _Jv_InterpException *exc = exceptions ();
894 for (int i = 0; i < exc_count; ++i)
896 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
897 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
898 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
899 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
900 // during compilation.
901 jclass handler
902 = (_Jv_Linker::resolve_pool_entry (defining_class,
903 exc[i].handler_type.i)).clazz;
904 exc[i].handler_type.p = handler;
907 // Translate entries in the LineNumberTable from bytecode PC's to direct
908 // threaded interpreter instruction values.
909 for (int i = 0; i < line_table_len; i++)
911 int byte_pc = line_table[i].bytecode_pc;
912 // It isn't worth throwing an exception if this table is
913 // corrupted, but at the same time we don't want a crash.
914 if (byte_pc < 0 || byte_pc >= code_length)
915 byte_pc = 0;
916 line_table[i].pc = &insns[pc_mapping[byte_pc]];
919 prepared = insns;
921 if (breakpoint_insn == NULL)
923 bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
924 breakpoint_insn = &bp_insn_slot;
927 #endif /* DIRECT_THREADED */
929 /* Run the given method.
930 When args is NULL, don't run anything -- just compile it. */
931 void
932 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
934 #undef DEBUG
935 #undef DEBUG_LOCALS_INSN
936 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
938 #include "interpret-run.cc"
941 void
942 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
944 #define DEBUG
945 #undef DEBUG_LOCALS_INSN
946 #define DEBUG_LOCALS_INSN(s, t) \
947 do \
949 frame_desc.locals_type[s] = t; \
951 while (0)
953 #include "interpret-run.cc"
956 static void
957 throw_internal_error (const char *msg)
959 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
962 static void
963 throw_incompatible_class_change_error (jstring msg)
965 throw new java::lang::IncompatibleClassChangeError (msg);
968 static void
969 throw_null_pointer_exception ()
971 throw new java::lang::NullPointerException;
974 /* Look up source code line number for given bytecode (or direct threaded
975 interpreter) PC. */
977 _Jv_InterpMethod::get_source_line(pc_t mpc)
979 int line = line_table_len > 0 ? line_table[0].line : -1;
980 for (int i = 1; i < line_table_len; i++)
981 if (line_table[i].pc > mpc)
982 break;
983 else
984 line = line_table[i].line;
986 return line;
989 /** Do static initialization for fields with a constant initializer */
990 void
991 _Jv_InitField (jobject obj, jclass klass, int index)
993 using namespace java::lang::reflect;
995 if (obj != 0 && klass == 0)
996 klass = obj->getClass ();
998 if (!_Jv_IsInterpretedClass (klass))
999 return;
1001 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
1003 _Jv_Field * field = (&klass->fields[0]) + index;
1005 if (index > klass->field_count)
1006 throw_internal_error ("field out of range");
1008 int init = iclass->field_initializers[index];
1009 if (init == 0)
1010 return;
1012 _Jv_Constants *pool = &klass->constants;
1013 int tag = pool->tags[init];
1015 if (! field->isResolved ())
1016 throw_internal_error ("initializing unresolved field");
1018 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
1019 throw_internal_error ("initializing non-static field with no object");
1021 void *addr = 0;
1023 if ((field->flags & Modifier::STATIC) != 0)
1024 addr = (void*) field->u.addr;
1025 else
1026 addr = (void*) (((char*)obj) + field->u.boffset);
1028 switch (tag)
1030 case JV_CONSTANT_String:
1032 jstring str;
1033 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1034 pool->data[init].string = str;
1035 pool->tags[init] = JV_CONSTANT_ResolvedString;
1037 /* fall through */
1039 case JV_CONSTANT_ResolvedString:
1040 if (! (field->type == &java::lang::String::class$
1041 || field->type == &java::lang::Class::class$))
1042 throw_class_format_error ("string initialiser to non-string field");
1044 *(jstring*)addr = pool->data[init].string;
1045 break;
1047 case JV_CONSTANT_Integer:
1049 int value = pool->data[init].i;
1051 if (field->type == JvPrimClass (boolean))
1052 *(jboolean*)addr = (jboolean)value;
1054 else if (field->type == JvPrimClass (byte))
1055 *(jbyte*)addr = (jbyte)value;
1057 else if (field->type == JvPrimClass (char))
1058 *(jchar*)addr = (jchar)value;
1060 else if (field->type == JvPrimClass (short))
1061 *(jshort*)addr = (jshort)value;
1063 else if (field->type == JvPrimClass (int))
1064 *(jint*)addr = (jint)value;
1066 else
1067 throw_class_format_error ("erroneous field initializer");
1069 break;
1071 case JV_CONSTANT_Long:
1072 if (field->type != JvPrimClass (long))
1073 throw_class_format_error ("erroneous field initializer");
1075 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1076 break;
1078 case JV_CONSTANT_Float:
1079 if (field->type != JvPrimClass (float))
1080 throw_class_format_error ("erroneous field initializer");
1082 *(jfloat*)addr = pool->data[init].f;
1083 break;
1085 case JV_CONSTANT_Double:
1086 if (field->type != JvPrimClass (double))
1087 throw_class_format_error ("erroneous field initializer");
1089 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1090 break;
1092 default:
1093 throw_class_format_error ("erroneous field initializer");
1097 inline static unsigned char*
1098 skip_one_type (unsigned char* ptr)
1100 int ch = *ptr++;
1102 while (ch == '[')
1104 ch = *ptr++;
1107 if (ch == 'L')
1109 do { ch = *ptr++; } while (ch != ';');
1112 return ptr;
1115 static ffi_type*
1116 get_ffi_type_from_signature (unsigned char* ptr)
1118 switch (*ptr)
1120 case 'L':
1121 case '[':
1122 return &ffi_type_pointer;
1123 break;
1125 case 'Z':
1126 // On some platforms a bool is a byte, on others an int.
1127 if (sizeof (jboolean) == sizeof (jbyte))
1128 return &ffi_type_sint8;
1129 else
1131 JvAssert (sizeof (jbyte) == sizeof (jint));
1132 return &ffi_type_sint32;
1134 break;
1136 case 'B':
1137 return &ffi_type_sint8;
1138 break;
1140 case 'C':
1141 return &ffi_type_uint16;
1142 break;
1144 case 'S':
1145 return &ffi_type_sint16;
1146 break;
1148 case 'I':
1149 return &ffi_type_sint32;
1150 break;
1152 case 'J':
1153 return &ffi_type_sint64;
1154 break;
1156 case 'F':
1157 return &ffi_type_float;
1158 break;
1160 case 'D':
1161 return &ffi_type_double;
1162 break;
1164 case 'V':
1165 return &ffi_type_void;
1166 break;
1169 throw_internal_error ("unknown type in signature");
1172 /* this function yields the number of actual arguments, that is, if the
1173 * function is non-static, then one is added to the number of elements
1174 * found in the signature */
1176 int
1177 _Jv_count_arguments (_Jv_Utf8Const *signature,
1178 jboolean staticp)
1180 unsigned char *ptr = (unsigned char*) signature->chars();
1181 int arg_count = staticp ? 0 : 1;
1183 /* first, count number of arguments */
1185 // skip '('
1186 ptr++;
1188 // count args
1189 while (*ptr != ')')
1191 ptr = skip_one_type (ptr);
1192 arg_count += 1;
1195 return arg_count;
1198 /* This beast will build a cif, given the signature. Memory for
1199 * the cif itself and for the argument types must be allocated by the
1200 * caller.
1203 int
1204 _Jv_init_cif (_Jv_Utf8Const* signature,
1205 int arg_count,
1206 jboolean staticp,
1207 ffi_cif *cif,
1208 ffi_type **arg_types,
1209 ffi_type **rtype_p)
1211 unsigned char *ptr = (unsigned char*) signature->chars();
1213 int arg_index = 0; // arg number
1214 int item_count = 0; // stack-item count
1216 // setup receiver
1217 if (!staticp)
1219 arg_types[arg_index++] = &ffi_type_pointer;
1220 item_count += 1;
1223 // skip '('
1224 ptr++;
1226 // assign arg types
1227 while (*ptr != ')')
1229 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1231 if (*ptr == 'J' || *ptr == 'D')
1232 item_count += 2;
1233 else
1234 item_count += 1;
1236 ptr = skip_one_type (ptr);
1239 // skip ')'
1240 ptr++;
1241 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1243 ptr = skip_one_type (ptr);
1244 if (ptr != (unsigned char*)signature->chars() + signature->len())
1245 throw_internal_error ("did not find end of signature");
1247 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1248 arg_count, rtype, arg_types) != FFI_OK)
1249 throw_internal_error ("ffi_prep_cif failed");
1251 if (rtype_p != NULL)
1252 *rtype_p = rtype;
1254 return item_count;
1257 #if FFI_NATIVE_RAW_API
1258 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1259 # define FFI_RAW_SIZE ffi_raw_size
1260 #else
1261 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1262 # define FFI_RAW_SIZE ffi_java_raw_size
1263 #endif
1265 /* we put this one here, and not in interpret.cc because it
1266 * calls the utility routines _Jv_count_arguments
1267 * which are static to this module. The following struct defines the
1268 * layout we use for the stubs, it's only used in the ncode method. */
1270 typedef struct {
1271 ffi_raw_closure closure;
1272 ffi_cif cif;
1273 ffi_type *arg_types[0];
1274 } ncode_closure;
1276 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1278 void *
1279 _Jv_InterpMethod::ncode ()
1281 using namespace java::lang::reflect;
1283 if (self->ncode != 0)
1284 return self->ncode;
1286 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1287 int arg_count = _Jv_count_arguments (self->signature, staticp);
1289 ncode_closure *closure =
1290 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1291 + arg_count * sizeof (ffi_type*));
1293 _Jv_init_cif (self->signature,
1294 arg_count,
1295 staticp,
1296 &closure->cif,
1297 &closure->arg_types[0],
1298 NULL);
1300 ffi_closure_fun fun;
1302 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1304 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1306 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1308 if (staticp)
1310 if (JVMTI::enabled)
1311 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1312 else
1313 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1315 else
1317 if (JVMTI::enabled)
1318 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1319 else
1320 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1323 else
1325 if (staticp)
1327 if (JVMTI::enabled)
1328 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1329 else
1330 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1332 else
1334 if (JVMTI::enabled)
1335 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1336 else
1337 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1341 FFI_PREP_RAW_CLOSURE (&closure->closure,
1342 &closure->cif,
1343 fun,
1344 (void*)this);
1346 self->ncode = (void*)closure;
1347 return self->ncode;
1350 /* Find the index of the given insn in the array of insn slots
1351 for this method. Returns -1 if not found. */
1352 jlong
1353 _Jv_InterpMethod::insn_index (pc_t pc)
1355 jlong left = 0;
1356 #ifdef DIRECT_THREADED
1357 jlong right = number_insn_slots;
1358 pc_t insns = prepared;
1359 #else
1360 jlong right = code_length;
1361 pc_t insns = bytecode ();
1362 #endif
1364 while (right >= 0)
1366 jlong mid = (left + right) / 2;
1367 if (&insns[mid] == pc)
1368 return mid;
1370 if (pc < &insns[mid])
1371 right = mid - 1;
1372 else
1373 left = mid + 1;
1376 return -1;
1379 // Method to check if an exception is caught at some location in a method
1380 // (meth). Returns true if this method (meth) contains a catch block for the
1381 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1382 // to the location of the beginning of the catch block.
1383 jboolean
1384 _Jv_InterpMethod::check_handler (pc_t *pc, _Jv_InterpMethod *meth,
1385 java::lang::Throwable *ex)
1387 #ifdef DIRECT_THREADED
1388 void *logical_pc = (void *) ((insn_slot *) (*pc) - 1);
1389 #else
1390 int logical_pc = (*pc) - 1 - meth->bytecode ();
1391 #endif
1392 _Jv_InterpException *exc = meth->exceptions ();
1393 jclass exc_class = ex->getClass ();
1395 for (int i = 0; i < meth->exc_count; i++)
1397 if (PCVAL (exc[i].start_pc) <= logical_pc
1398 && logical_pc < PCVAL (exc[i].end_pc))
1400 #ifdef DIRECT_THREADED
1401 jclass handler = (jclass) exc[i].handler_type.p;
1402 #else
1403 jclass handler = NULL;
1404 if (exc[i].handler_type.i != 0)
1405 handler
1406 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1408 #endif /* DIRECT_THREADED */
1409 if (handler == NULL || handler->isAssignableFrom (exc_class))
1411 #ifdef DIRECT_THREADED
1412 (*pc) = (insn_slot *) exc[i].handler_pc.p;
1413 #else
1414 (*pc) = meth->bytecode () + exc[i].handler_pc.i;
1415 #endif /* DIRECT_THREADED */
1416 return true;
1420 return false;
1424 void
1425 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1426 jintArray& line_numbers,
1427 jlongArray& code_indices)
1429 #ifdef DIRECT_THREADED
1430 /* For the DIRECT_THREADED case, if the method has not yet been
1431 * compiled, the linetable will change to insn slots instead of
1432 * bytecode PCs. It is probably easiest, in this case, to simply
1433 * compile the method and guarantee that we are using insn
1434 * slots.
1436 _Jv_CompileMethod (this);
1438 if (line_table_len > 0)
1440 start = 0;
1441 end = number_insn_slots;
1442 line_numbers = JvNewIntArray (line_table_len);
1443 code_indices = JvNewLongArray (line_table_len);
1445 jint* lines = elements (line_numbers);
1446 jlong* indices = elements (code_indices);
1447 for (int i = 0; i < line_table_len; ++i)
1449 lines[i] = line_table[i].line;
1450 indices[i] = insn_index (line_table[i].pc);
1453 #else // !DIRECT_THREADED
1454 if (line_table_len > 0)
1456 start = 0;
1457 end = code_length;
1458 line_numbers = JvNewIntArray (line_table_len);
1459 code_indices = JvNewLongArray (line_table_len);
1461 jint* lines = elements (line_numbers);
1462 jlong* indices = elements (code_indices);
1463 for (int i = 0; i < line_table_len; ++i)
1465 lines[i] = line_table[i].line;
1466 indices[i] = (jlong) line_table[i].bytecode_pc;
1469 #endif // !DIRECT_THREADED
1472 int
1473 _Jv_InterpMethod::get_local_var_table (char **name, char **sig,
1474 char **generic_sig, jlong *startloc,
1475 jint *length, jint *slot,
1476 int table_slot)
1478 if (local_var_table == NULL)
1479 return -2;
1480 if (table_slot >= local_var_table_len)
1481 return -1;
1482 else
1484 *name = local_var_table[table_slot].name;
1485 *sig = local_var_table[table_slot].descriptor;
1486 *generic_sig = local_var_table[table_slot].descriptor;
1488 *startloc = static_cast<jlong>
1489 (local_var_table[table_slot].bytecode_start_pc);
1490 *length = static_cast<jint> (local_var_table[table_slot].length);
1491 *slot = static_cast<jint> (local_var_table[table_slot].slot);
1493 return local_var_table_len - table_slot -1;
1496 pc_t
1497 _Jv_InterpMethod::install_break (jlong index)
1499 return set_insn (index, breakpoint_insn);
1502 pc_t
1503 _Jv_InterpMethod::get_insn (jlong index)
1505 pc_t code;
1507 #ifdef DIRECT_THREADED
1508 if (index >= number_insn_slots || index < 0)
1509 return NULL;
1511 code = prepared;
1512 #else // !DIRECT_THREADED
1513 if (index >= code_length || index < 0)
1514 return NULL;
1516 code = reinterpret_cast<pc_t> (bytecode ());
1517 #endif // !DIRECT_THREADED
1519 return &code[index];
1522 pc_t
1523 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1525 #ifdef DIRECT_THREADED
1526 if (index >= number_insn_slots || index < 0)
1527 return NULL;
1529 pc_t code = prepared;
1530 code[index].insn = insn->insn;
1531 #else // !DIRECT_THREADED
1532 if (index >= code_length || index < 0)
1533 return NULL;
1535 pc_t code = reinterpret_cast<pc_t> (bytecode ());
1536 code[index] = *insn;
1537 #endif // !DIRECT_THREADED
1539 return &code[index];
1542 void *
1543 _Jv_JNIMethod::ncode ()
1545 using namespace java::lang::reflect;
1547 if (self->ncode != 0)
1548 return self->ncode;
1550 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1551 int arg_count = _Jv_count_arguments (self->signature, staticp);
1553 ncode_closure *closure =
1554 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1555 + arg_count * sizeof (ffi_type*));
1557 ffi_type *rtype;
1558 _Jv_init_cif (self->signature,
1559 arg_count,
1560 staticp,
1561 &closure->cif,
1562 &closure->arg_types[0],
1563 &rtype);
1565 ffi_closure_fun fun;
1567 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1569 // Initialize the argument types and CIF that represent the actual
1570 // underlying JNI function.
1571 int extra_args = 1;
1572 if ((self->accflags & Modifier::STATIC))
1573 ++extra_args;
1574 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1575 * sizeof (ffi_type *));
1576 int offset = 0;
1577 jni_arg_types[offset++] = &ffi_type_pointer;
1578 if ((self->accflags & Modifier::STATIC))
1579 jni_arg_types[offset++] = &ffi_type_pointer;
1580 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1581 arg_count * sizeof (ffi_type *));
1583 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1584 extra_args + arg_count, rtype,
1585 jni_arg_types) != FFI_OK)
1586 throw_internal_error ("ffi_prep_cif failed for JNI function");
1588 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1590 // FIXME: for now we assume that all native methods for
1591 // interpreted code use JNI.
1592 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1594 FFI_PREP_RAW_CLOSURE (&closure->closure,
1595 &closure->cif,
1596 fun,
1597 (void*) this);
1599 self->ncode = (void *) closure;
1600 return self->ncode;
1603 static void
1604 throw_class_format_error (jstring msg)
1606 throw (msg
1607 ? new java::lang::ClassFormatError (msg)
1608 : new java::lang::ClassFormatError);
1611 static void
1612 throw_class_format_error (const char *msg)
1614 throw_class_format_error (JvNewStringLatin1 (msg));
1619 void
1620 _Jv_InterpreterEngine::do_verify (jclass klass)
1622 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1623 for (int i = 0; i < klass->method_count; i++)
1625 using namespace java::lang::reflect;
1626 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1627 _Jv_ushort accflags = klass->methods[i].accflags;
1628 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1630 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1631 _Jv_VerifyMethod (im);
1636 void
1637 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1639 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1640 for (int i = 0; i < klass->method_count; i++)
1642 // Just skip abstract methods. This is particularly important
1643 // because we don't resize the interpreted_methods array when
1644 // miranda methods are added to it.
1645 if ((klass->methods[i].accflags
1646 & java::lang::reflect::Modifier::ABSTRACT)
1647 != 0)
1648 continue;
1650 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1652 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1653 != 0)
1655 // You might think we could use a virtual `ncode' method in
1656 // the _Jv_MethodBase and unify the native and non-native
1657 // cases. Well, we can't, because we don't allocate these
1658 // objects using `new', and thus they don't get a vtable.
1659 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1660 klass->methods[i].ncode = jnim->ncode ();
1662 else if (imeth != 0) // it could be abstract
1664 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1665 klass->methods[i].ncode = im->ncode ();
1670 void
1671 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1672 int pointer_size,
1673 int other_size)
1675 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1677 // Splitting the allocations here lets us scan reference fields and
1678 // avoid scanning non-reference fields. How reference fields are
1679 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1680 // means that this memory will be scanned conservatively (same
1681 // difference, since we know all the contents here are pointers).
1682 // Then we put pointers into this memory into the 'fields'
1683 // structure. Most of these are interior pointers, which is ok (but
1684 // even so the pointer to the first reference field will be used and
1685 // that is not an interior pointer). The 'fields' array is also
1686 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1687 // be scanned. A pointer to this array is held by Class and thus
1688 // seen by the collector.
1689 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1690 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1692 for (int i = 0; i < klass->field_count; i++)
1694 _Jv_Field *field = &klass->fields[i];
1696 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1697 continue;
1699 char *base = field->isRef() ? reference_fields : non_reference_fields;
1700 field->u.addr = base + field->u.boffset;
1702 if (iclass->field_initializers[i] != 0)
1704 _Jv_Linker::resolve_field (field, klass->loader);
1705 _Jv_InitField (0, klass, i);
1709 // Now we don't need the field_initializers anymore, so let the
1710 // collector get rid of it.
1711 iclass->field_initializers = 0;
1714 _Jv_ResolvedMethod *
1715 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1716 jboolean staticp)
1718 int arg_count = _Jv_count_arguments (method->signature, staticp);
1720 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1721 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1722 + arg_count*sizeof (ffi_type*));
1724 result->stack_item_count
1725 = _Jv_init_cif (method->signature,
1726 arg_count,
1727 staticp,
1728 &result->cif,
1729 &result->arg_types[0],
1730 NULL);
1732 result->method = method;
1733 result->klass = klass;
1735 return result;
1738 void
1739 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1741 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1742 for (int i = 0; i < klass->method_count; i++)
1744 // Just skip abstract methods. This is particularly important
1745 // because we don't resize the interpreted_methods array when
1746 // miranda methods are added to it.
1747 if ((klass->methods[i].accflags
1748 & java::lang::reflect::Modifier::ABSTRACT)
1749 != 0)
1750 continue;
1751 // Miranda method additions mean that the `methods' array moves.
1752 // We cache a pointer into this array, so we have to update.
1753 iclass->interpreted_methods[i]->self = &klass->methods[i];
1757 #ifdef DIRECT_THREADED
1758 void
1759 _Jv_CompileMethod (_Jv_InterpMethod* method)
1761 if (method->prepared == NULL)
1763 if (JVMTI::enabled)
1764 _Jv_InterpMethod::run_debug (NULL, NULL, method);
1765 else
1766 _Jv_InterpMethod::run (NULL, NULL, method);
1769 #endif // DIRECT_THREADED
1771 #endif // INTERPRETER