2006-06-09 Paolo Carlini <pcarlini@suse.de>
[official-gcc.git] / libjava / interpret.cc
blob79121585bad2a9ca7ebaa49b7c369eb16e49938b
1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 #ifdef INTERPRETER
42 // Execution engine for interpreted code.
43 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
45 #include <stdlib.h>
47 using namespace gcj;
49 static void throw_internal_error (const char *msg)
50 __attribute__ ((__noreturn__));
51 static void throw_incompatible_class_change_error (jstring msg)
52 __attribute__ ((__noreturn__));
53 static void throw_null_pointer_exception ()
54 __attribute__ ((__noreturn__));
56 static void throw_class_format_error (jstring msg)
57 __attribute__ ((__noreturn__));
58 static void throw_class_format_error (const char *msg)
59 __attribute__ ((__noreturn__));
61 #ifdef DIRECT_THREADED
62 // Lock to ensure that methods are not compiled concurrently.
63 // We could use a finer-grained lock here, however it is not safe to use
64 // the Class monitor as user code in another thread could hold it.
65 static _Jv_Mutex_t compile_mutex;
67 void
68 _Jv_InitInterpreter()
70 _Jv_MutexInit (&compile_mutex);
72 #else
73 void _Jv_InitInterpreter() {}
74 #endif
76 extern "C" double __ieee754_fmod (double,double);
78 static inline void dupx (_Jv_word *sp, int n, int x)
80 // first "slide" n+x elements n to the right
81 int top = n-1;
82 for (int i = 0; i < n+x; i++)
84 sp[(top-i)] = sp[(top-i)-n];
87 // next, copy the n top elements, n+x down
88 for (int i = 0; i < n; i++)
90 sp[top-(n+x)-i] = sp[top-i];
94 // Used to convert from floating types to integral types.
95 template<typename TO, typename FROM>
96 static inline TO
97 convert (FROM val, TO min, TO max)
99 TO ret;
100 if (val >= (FROM) max)
101 ret = max;
102 else if (val <= (FROM) min)
103 ret = min;
104 else if (val != val)
105 ret = 0;
106 else
107 ret = (TO) val;
108 return ret;
111 #define PUSHA(V) (sp++)->o = (V)
112 #define PUSHI(V) (sp++)->i = (V)
113 #define PUSHF(V) (sp++)->f = (V)
114 #if SIZEOF_VOID_P == 8
115 # define PUSHL(V) (sp->l = (V), sp += 2)
116 # define PUSHD(V) (sp->d = (V), sp += 2)
117 #else
118 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
119 (sp++)->ia[0] = w2.ia[0]; \
120 (sp++)->ia[0] = w2.ia[1]; } while (0)
121 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
122 (sp++)->ia[0] = w2.ia[0]; \
123 (sp++)->ia[0] = w2.ia[1]; } while (0)
124 #endif
126 #define POPA() ((--sp)->o)
127 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
128 #define POPF() ((jfloat) (--sp)->f)
129 #if SIZEOF_VOID_P == 8
130 # define POPL() (sp -= 2, (jlong) sp->l)
131 # define POPD() (sp -= 2, (jdouble) sp->d)
132 #else
133 # define POPL() ({ _Jv_word2 w2; \
134 w2.ia[1] = (--sp)->ia[0]; \
135 w2.ia[0] = (--sp)->ia[0]; w2.l; })
136 # define POPD() ({ _Jv_word2 w2; \
137 w2.ia[1] = (--sp)->ia[0]; \
138 w2.ia[0] = (--sp)->ia[0]; w2.d; })
139 #endif
141 #define LOADA(I) (sp++)->o = locals[I].o
142 #define LOADI(I) (sp++)->i = locals[I].i
143 #define LOADF(I) (sp++)->f = locals[I].f
144 #if SIZEOF_VOID_P == 8
145 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
146 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
147 #else
148 # define LOADL(I) do { jint __idx = (I); \
149 (sp++)->ia[0] = locals[__idx].ia[0]; \
150 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
151 } while (0)
152 # define LOADD(I) LOADL(I)
153 #endif
155 #define STOREA(I) locals[I].o = (--sp)->o
156 #define STOREI(I) locals[I].i = (--sp)->i
157 #define STOREF(I) locals[I].f = (--sp)->f
158 #if SIZEOF_VOID_P == 8
159 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
160 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
161 #else
162 # define STOREL(I) do { jint __idx = (I); \
163 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
164 locals[__idx].ia[0] = (--sp)->ia[0]; \
165 } while (0)
166 # define STORED(I) STOREL(I)
167 #endif
169 #define PEEKI(I) (locals+(I))->i
170 #define PEEKA(I) (locals+(I))->o
172 #define POKEI(I,V) ((locals+(I))->i = (V))
175 #define BINOPI(OP) { \
176 jint value2 = POPI(); \
177 jint value1 = POPI(); \
178 PUSHI(value1 OP value2); \
181 #define BINOPF(OP) { \
182 jfloat value2 = POPF(); \
183 jfloat value1 = POPF(); \
184 PUSHF(value1 OP value2); \
187 #define BINOPL(OP) { \
188 jlong value2 = POPL(); \
189 jlong value1 = POPL(); \
190 PUSHL(value1 OP value2); \
193 #define BINOPD(OP) { \
194 jdouble value2 = POPD(); \
195 jdouble value1 = POPD(); \
196 PUSHD(value1 OP value2); \
199 static inline jint get1s(unsigned char* loc) {
200 return *(signed char*)loc;
203 static inline jint get1u(unsigned char* loc) {
204 return *loc;
207 static inline jint get2s(unsigned char* loc) {
208 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
211 static inline jint get2u(unsigned char* loc) {
212 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
215 static jint get4(unsigned char* loc) {
216 return (((jint)(loc[0])) << 24)
217 | (((jint)(loc[1])) << 16)
218 | (((jint)(loc[2])) << 8)
219 | (((jint)(loc[3])) << 0);
222 #define SAVE_PC() frame_desc.pc = pc
224 // We used to define this conditionally, depending on HANDLE_SEGV.
225 // However, that runs into a problem if a chunk in low memory is
226 // mapped and we try to look at a field near the end of a large
227 // object. See PR 26858 for details. It is, most likely, relatively
228 // inexpensive to simply do this check always.
229 #define NULLCHECK(X) \
230 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
232 // Note that we can still conditionally define NULLARRAYCHECK, since
233 // we know that all uses of an array will first reference the length
234 // field, which is first -- and thus will trigger a SEGV.
235 #ifdef HANDLE_SEGV
236 #define NULLARRAYCHECK(X) SAVE_PC()
237 #else
238 #define NULLARRAYCHECK(X) \
239 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
240 #endif
242 #define ARRAYBOUNDSCHECK(array, index) \
243 do \
245 if (((unsigned) index) >= (unsigned) (array->length)) \
246 _Jv_ThrowBadArrayIndex (index); \
248 while (0)
250 void
251 _Jv_InterpMethod::run_normal (ffi_cif *,
252 void* ret,
253 ffi_raw * args,
254 void* __this)
256 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
257 run (ret, args, _this);
260 void
261 _Jv_InterpMethod::run_synch_object (ffi_cif *,
262 void* ret,
263 ffi_raw * args,
264 void* __this)
266 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
268 jobject rcv = (jobject) args[0].ptr;
269 JvSynchronize mutex (rcv);
271 run (ret, args, _this);
274 void
275 _Jv_InterpMethod::run_class (ffi_cif *,
276 void* ret,
277 ffi_raw * args,
278 void* __this)
280 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
281 _Jv_InitClass (_this->defining_class);
282 run (ret, args, _this);
285 void
286 _Jv_InterpMethod::run_synch_class (ffi_cif *,
287 void* ret,
288 ffi_raw * args,
289 void* __this)
291 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
293 jclass sync = _this->defining_class;
294 _Jv_InitClass (sync);
295 JvSynchronize mutex (sync);
297 run (ret, args, _this);
300 #ifdef DIRECT_THREADED
301 // "Compile" a method by turning it from bytecode to direct-threaded
302 // code.
303 void
304 _Jv_InterpMethod::compile (const void * const *insn_targets)
306 insn_slot *insns = NULL;
307 int next = 0;
308 unsigned char *codestart = bytecode ();
309 unsigned char *end = codestart + code_length;
310 _Jv_word *pool_data = defining_class->constants.data;
312 #define SET_ONE(Field, Value) \
313 do \
315 if (first_pass) \
316 ++next; \
317 else \
318 insns[next++].Field = Value; \
320 while (0)
322 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
323 #define SET_INT(Value) SET_ONE (int_val, Value)
324 #define SET_DATUM(Value) SET_ONE (datum, Value)
326 // Map from bytecode PC to slot in INSNS.
327 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
328 for (int i = 0; i < code_length; ++i)
329 pc_mapping[i] = -1;
331 for (int i = 0; i < 2; ++i)
333 jboolean first_pass = i == 0;
335 if (! first_pass)
337 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
338 number_insn_slots = next;
339 next = 0;
342 unsigned char *pc = codestart;
343 while (pc < end)
345 int base_pc_val = pc - codestart;
346 if (first_pass)
347 pc_mapping[base_pc_val] = next;
349 java_opcode opcode = (java_opcode) *pc++;
350 // Just elide NOPs.
351 if (opcode == op_nop)
352 continue;
353 SET_INSN (insn_targets[opcode]);
355 switch (opcode)
357 case op_nop:
358 case op_aconst_null:
359 case op_iconst_m1:
360 case op_iconst_0:
361 case op_iconst_1:
362 case op_iconst_2:
363 case op_iconst_3:
364 case op_iconst_4:
365 case op_iconst_5:
366 case op_lconst_0:
367 case op_lconst_1:
368 case op_fconst_0:
369 case op_fconst_1:
370 case op_fconst_2:
371 case op_dconst_0:
372 case op_dconst_1:
373 case op_iload_0:
374 case op_iload_1:
375 case op_iload_2:
376 case op_iload_3:
377 case op_lload_0:
378 case op_lload_1:
379 case op_lload_2:
380 case op_lload_3:
381 case op_fload_0:
382 case op_fload_1:
383 case op_fload_2:
384 case op_fload_3:
385 case op_dload_0:
386 case op_dload_1:
387 case op_dload_2:
388 case op_dload_3:
389 case op_aload_0:
390 case op_aload_1:
391 case op_aload_2:
392 case op_aload_3:
393 case op_iaload:
394 case op_laload:
395 case op_faload:
396 case op_daload:
397 case op_aaload:
398 case op_baload:
399 case op_caload:
400 case op_saload:
401 case op_istore_0:
402 case op_istore_1:
403 case op_istore_2:
404 case op_istore_3:
405 case op_lstore_0:
406 case op_lstore_1:
407 case op_lstore_2:
408 case op_lstore_3:
409 case op_fstore_0:
410 case op_fstore_1:
411 case op_fstore_2:
412 case op_fstore_3:
413 case op_dstore_0:
414 case op_dstore_1:
415 case op_dstore_2:
416 case op_dstore_3:
417 case op_astore_0:
418 case op_astore_1:
419 case op_astore_2:
420 case op_astore_3:
421 case op_iastore:
422 case op_lastore:
423 case op_fastore:
424 case op_dastore:
425 case op_aastore:
426 case op_bastore:
427 case op_castore:
428 case op_sastore:
429 case op_pop:
430 case op_pop2:
431 case op_dup:
432 case op_dup_x1:
433 case op_dup_x2:
434 case op_dup2:
435 case op_dup2_x1:
436 case op_dup2_x2:
437 case op_swap:
438 case op_iadd:
439 case op_isub:
440 case op_imul:
441 case op_idiv:
442 case op_irem:
443 case op_ishl:
444 case op_ishr:
445 case op_iushr:
446 case op_iand:
447 case op_ior:
448 case op_ixor:
449 case op_ladd:
450 case op_lsub:
451 case op_lmul:
452 case op_ldiv:
453 case op_lrem:
454 case op_lshl:
455 case op_lshr:
456 case op_lushr:
457 case op_land:
458 case op_lor:
459 case op_lxor:
460 case op_fadd:
461 case op_fsub:
462 case op_fmul:
463 case op_fdiv:
464 case op_frem:
465 case op_dadd:
466 case op_dsub:
467 case op_dmul:
468 case op_ddiv:
469 case op_drem:
470 case op_ineg:
471 case op_i2b:
472 case op_i2c:
473 case op_i2s:
474 case op_lneg:
475 case op_fneg:
476 case op_dneg:
477 case op_i2l:
478 case op_i2f:
479 case op_i2d:
480 case op_l2i:
481 case op_l2f:
482 case op_l2d:
483 case op_f2i:
484 case op_f2l:
485 case op_f2d:
486 case op_d2i:
487 case op_d2l:
488 case op_d2f:
489 case op_lcmp:
490 case op_fcmpl:
491 case op_fcmpg:
492 case op_dcmpl:
493 case op_dcmpg:
494 case op_monitorenter:
495 case op_monitorexit:
496 case op_ireturn:
497 case op_lreturn:
498 case op_freturn:
499 case op_dreturn:
500 case op_areturn:
501 case op_return:
502 case op_athrow:
503 case op_arraylength:
504 // No argument, nothing else to do.
505 break;
507 case op_bipush:
508 SET_INT (get1s (pc));
509 ++pc;
510 break;
512 case op_ldc:
514 int index = get1u (pc);
515 ++pc;
516 // For an unresolved class we want to delay resolution
517 // until execution.
518 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
520 --next;
521 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
522 SET_INT (index);
524 else
525 SET_DATUM (pool_data[index].o);
527 break;
529 case op_ret:
530 case op_iload:
531 case op_lload:
532 case op_fload:
533 case op_dload:
534 case op_aload:
535 case op_istore:
536 case op_lstore:
537 case op_fstore:
538 case op_dstore:
539 case op_astore:
540 case op_newarray:
541 SET_INT (get1u (pc));
542 ++pc;
543 break;
545 case op_iinc:
546 SET_INT (get1u (pc));
547 SET_INT (get1s (pc + 1));
548 pc += 2;
549 break;
551 case op_ldc_w:
553 int index = get2u (pc);
554 pc += 2;
555 // For an unresolved class we want to delay resolution
556 // until execution.
557 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
559 --next;
560 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
561 SET_INT (index);
563 else
564 SET_DATUM (pool_data[index].o);
566 break;
568 case op_ldc2_w:
570 int index = get2u (pc);
571 pc += 2;
572 SET_DATUM (&pool_data[index]);
574 break;
576 case op_sipush:
577 SET_INT (get2s (pc));
578 pc += 2;
579 break;
581 case op_new:
582 case op_getstatic:
583 case op_getfield:
584 case op_putfield:
585 case op_putstatic:
586 case op_anewarray:
587 case op_instanceof:
588 case op_checkcast:
589 case op_invokespecial:
590 case op_invokestatic:
591 case op_invokevirtual:
592 SET_INT (get2u (pc));
593 pc += 2;
594 break;
596 case op_multianewarray:
597 SET_INT (get2u (pc));
598 SET_INT (get1u (pc + 2));
599 pc += 3;
600 break;
602 case op_jsr:
603 case op_ifeq:
604 case op_ifne:
605 case op_iflt:
606 case op_ifge:
607 case op_ifgt:
608 case op_ifle:
609 case op_if_icmpeq:
610 case op_if_icmpne:
611 case op_if_icmplt:
612 case op_if_icmpge:
613 case op_if_icmpgt:
614 case op_if_icmple:
615 case op_if_acmpeq:
616 case op_if_acmpne:
617 case op_ifnull:
618 case op_ifnonnull:
619 case op_goto:
621 int offset = get2s (pc);
622 pc += 2;
624 int new_pc = base_pc_val + offset;
626 bool orig_was_goto = opcode == op_goto;
628 // Thread jumps. We limit the loop count; this lets
629 // us avoid infinite loops if the bytecode contains
630 // such. `10' is arbitrary.
631 int count = 10;
632 while (codestart[new_pc] == op_goto && count-- > 0)
633 new_pc += get2s (&codestart[new_pc + 1]);
635 // If the jump takes us to a `return' instruction and
636 // the original branch was an unconditional goto, then
637 // we hoist the return.
638 opcode = (java_opcode) codestart[new_pc];
639 if (orig_was_goto
640 && (opcode == op_ireturn || opcode == op_lreturn
641 || opcode == op_freturn || opcode == op_dreturn
642 || opcode == op_areturn || opcode == op_return))
644 --next;
645 SET_INSN (insn_targets[opcode]);
647 else
648 SET_DATUM (&insns[pc_mapping[new_pc]]);
650 break;
652 case op_tableswitch:
654 while ((pc - codestart) % 4 != 0)
655 ++pc;
657 jint def = get4 (pc);
658 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
659 pc += 4;
661 int low = get4 (pc);
662 SET_INT (low);
663 pc += 4;
664 int high = get4 (pc);
665 SET_INT (high);
666 pc += 4;
668 for (int i = low; i <= high; ++i)
670 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
671 pc += 4;
674 break;
676 case op_lookupswitch:
678 while ((pc - codestart) % 4 != 0)
679 ++pc;
681 jint def = get4 (pc);
682 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
683 pc += 4;
685 jint npairs = get4 (pc);
686 pc += 4;
687 SET_INT (npairs);
689 while (npairs-- > 0)
691 jint match = get4 (pc);
692 jint offset = get4 (pc + 4);
693 SET_INT (match);
694 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
695 pc += 8;
698 break;
700 case op_invokeinterface:
702 jint index = get2u (pc);
703 pc += 2;
704 // We ignore the next two bytes.
705 pc += 2;
706 SET_INT (index);
708 break;
710 case op_wide:
712 opcode = (java_opcode) get1u (pc);
713 pc += 1;
714 jint val = get2u (pc);
715 pc += 2;
717 // We implement narrow and wide instructions using the
718 // same code in the interpreter. So we rewrite the
719 // instruction slot here.
720 if (! first_pass)
721 insns[next - 1].insn = (void *) insn_targets[opcode];
722 SET_INT (val);
724 if (opcode == op_iinc)
726 SET_INT (get2s (pc));
727 pc += 2;
730 break;
732 case op_jsr_w:
733 case op_goto_w:
735 jint offset = get4 (pc);
736 pc += 4;
737 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
739 break;
741 // Some "can't happen" cases that we include for
742 // error-checking purposes.
743 case op_putfield_1:
744 case op_putfield_2:
745 case op_putfield_4:
746 case op_putfield_8:
747 case op_putfield_a:
748 case op_putstatic_1:
749 case op_putstatic_2:
750 case op_putstatic_4:
751 case op_putstatic_8:
752 case op_putstatic_a:
753 case op_getfield_1:
754 case op_getfield_2s:
755 case op_getfield_2u:
756 case op_getfield_4:
757 case op_getfield_8:
758 case op_getfield_a:
759 case op_getstatic_1:
760 case op_getstatic_2s:
761 case op_getstatic_2u:
762 case op_getstatic_4:
763 case op_getstatic_8:
764 case op_getstatic_a:
765 default:
766 // Fail somehow.
767 break;
772 // Now update exceptions.
773 _Jv_InterpException *exc = exceptions ();
774 for (int i = 0; i < exc_count; ++i)
776 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
777 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
778 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
779 jclass handler
780 = (_Jv_Linker::resolve_pool_entry (defining_class,
781 exc[i].handler_type.i)).clazz;
782 exc[i].handler_type.p = handler;
785 // Translate entries in the LineNumberTable from bytecode PC's to direct
786 // threaded interpreter instruction values.
787 for (int i = 0; i < line_table_len; i++)
789 int byte_pc = line_table[i].bytecode_pc;
790 // It isn't worth throwing an exception if this table is
791 // corrupted, but at the same time we don't want a crash.
792 if (byte_pc < 0 || byte_pc >= code_length)
793 byte_pc = 0;
794 line_table[i].pc = &insns[pc_mapping[byte_pc]];
797 prepared = insns;
799 #endif /* DIRECT_THREADED */
801 /* Run the given method.
802 When args is NULL, don't run anything -- just compile it. */
803 void
804 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
806 using namespace java::lang::reflect;
808 // FRAME_DESC registers this particular invocation as the top-most
809 // interpreter frame. This lets the stack tracing code (for
810 // Throwable) print information about the method being interpreted
811 // rather than about the interpreter itself. FRAME_DESC has a
812 // destructor so it cleans up automatically when the interpreter
813 // returns.
814 java::lang::Thread *thread = java::lang::Thread::currentThread();
815 _Jv_InterpFrame frame_desc (meth, thread);
817 _Jv_word stack[meth->max_stack];
818 _Jv_word *sp = stack;
820 _Jv_word locals[meth->max_locals];
822 #define INSN_LABEL(op) &&insn_##op
824 static const void *const insn_target[] =
826 INSN_LABEL(nop),
827 INSN_LABEL(aconst_null),
828 INSN_LABEL(iconst_m1),
829 INSN_LABEL(iconst_0),
830 INSN_LABEL(iconst_1),
831 INSN_LABEL(iconst_2),
832 INSN_LABEL(iconst_3),
833 INSN_LABEL(iconst_4),
834 INSN_LABEL(iconst_5),
835 INSN_LABEL(lconst_0),
836 INSN_LABEL(lconst_1),
837 INSN_LABEL(fconst_0),
838 INSN_LABEL(fconst_1),
839 INSN_LABEL(fconst_2),
840 INSN_LABEL(dconst_0),
841 INSN_LABEL(dconst_1),
842 INSN_LABEL(bipush),
843 INSN_LABEL(sipush),
844 INSN_LABEL(ldc),
845 INSN_LABEL(ldc_w),
846 INSN_LABEL(ldc2_w),
847 INSN_LABEL(iload),
848 INSN_LABEL(lload),
849 INSN_LABEL(fload),
850 INSN_LABEL(dload),
851 INSN_LABEL(aload),
852 INSN_LABEL(iload_0),
853 INSN_LABEL(iload_1),
854 INSN_LABEL(iload_2),
855 INSN_LABEL(iload_3),
856 INSN_LABEL(lload_0),
857 INSN_LABEL(lload_1),
858 INSN_LABEL(lload_2),
859 INSN_LABEL(lload_3),
860 INSN_LABEL(fload_0),
861 INSN_LABEL(fload_1),
862 INSN_LABEL(fload_2),
863 INSN_LABEL(fload_3),
864 INSN_LABEL(dload_0),
865 INSN_LABEL(dload_1),
866 INSN_LABEL(dload_2),
867 INSN_LABEL(dload_3),
868 INSN_LABEL(aload_0),
869 INSN_LABEL(aload_1),
870 INSN_LABEL(aload_2),
871 INSN_LABEL(aload_3),
872 INSN_LABEL(iaload),
873 INSN_LABEL(laload),
874 INSN_LABEL(faload),
875 INSN_LABEL(daload),
876 INSN_LABEL(aaload),
877 INSN_LABEL(baload),
878 INSN_LABEL(caload),
879 INSN_LABEL(saload),
880 INSN_LABEL(istore),
881 INSN_LABEL(lstore),
882 INSN_LABEL(fstore),
883 INSN_LABEL(dstore),
884 INSN_LABEL(astore),
885 INSN_LABEL(istore_0),
886 INSN_LABEL(istore_1),
887 INSN_LABEL(istore_2),
888 INSN_LABEL(istore_3),
889 INSN_LABEL(lstore_0),
890 INSN_LABEL(lstore_1),
891 INSN_LABEL(lstore_2),
892 INSN_LABEL(lstore_3),
893 INSN_LABEL(fstore_0),
894 INSN_LABEL(fstore_1),
895 INSN_LABEL(fstore_2),
896 INSN_LABEL(fstore_3),
897 INSN_LABEL(dstore_0),
898 INSN_LABEL(dstore_1),
899 INSN_LABEL(dstore_2),
900 INSN_LABEL(dstore_3),
901 INSN_LABEL(astore_0),
902 INSN_LABEL(astore_1),
903 INSN_LABEL(astore_2),
904 INSN_LABEL(astore_3),
905 INSN_LABEL(iastore),
906 INSN_LABEL(lastore),
907 INSN_LABEL(fastore),
908 INSN_LABEL(dastore),
909 INSN_LABEL(aastore),
910 INSN_LABEL(bastore),
911 INSN_LABEL(castore),
912 INSN_LABEL(sastore),
913 INSN_LABEL(pop),
914 INSN_LABEL(pop2),
915 INSN_LABEL(dup),
916 INSN_LABEL(dup_x1),
917 INSN_LABEL(dup_x2),
918 INSN_LABEL(dup2),
919 INSN_LABEL(dup2_x1),
920 INSN_LABEL(dup2_x2),
921 INSN_LABEL(swap),
922 INSN_LABEL(iadd),
923 INSN_LABEL(ladd),
924 INSN_LABEL(fadd),
925 INSN_LABEL(dadd),
926 INSN_LABEL(isub),
927 INSN_LABEL(lsub),
928 INSN_LABEL(fsub),
929 INSN_LABEL(dsub),
930 INSN_LABEL(imul),
931 INSN_LABEL(lmul),
932 INSN_LABEL(fmul),
933 INSN_LABEL(dmul),
934 INSN_LABEL(idiv),
935 INSN_LABEL(ldiv),
936 INSN_LABEL(fdiv),
937 INSN_LABEL(ddiv),
938 INSN_LABEL(irem),
939 INSN_LABEL(lrem),
940 INSN_LABEL(frem),
941 INSN_LABEL(drem),
942 INSN_LABEL(ineg),
943 INSN_LABEL(lneg),
944 INSN_LABEL(fneg),
945 INSN_LABEL(dneg),
946 INSN_LABEL(ishl),
947 INSN_LABEL(lshl),
948 INSN_LABEL(ishr),
949 INSN_LABEL(lshr),
950 INSN_LABEL(iushr),
951 INSN_LABEL(lushr),
952 INSN_LABEL(iand),
953 INSN_LABEL(land),
954 INSN_LABEL(ior),
955 INSN_LABEL(lor),
956 INSN_LABEL(ixor),
957 INSN_LABEL(lxor),
958 INSN_LABEL(iinc),
959 INSN_LABEL(i2l),
960 INSN_LABEL(i2f),
961 INSN_LABEL(i2d),
962 INSN_LABEL(l2i),
963 INSN_LABEL(l2f),
964 INSN_LABEL(l2d),
965 INSN_LABEL(f2i),
966 INSN_LABEL(f2l),
967 INSN_LABEL(f2d),
968 INSN_LABEL(d2i),
969 INSN_LABEL(d2l),
970 INSN_LABEL(d2f),
971 INSN_LABEL(i2b),
972 INSN_LABEL(i2c),
973 INSN_LABEL(i2s),
974 INSN_LABEL(lcmp),
975 INSN_LABEL(fcmpl),
976 INSN_LABEL(fcmpg),
977 INSN_LABEL(dcmpl),
978 INSN_LABEL(dcmpg),
979 INSN_LABEL(ifeq),
980 INSN_LABEL(ifne),
981 INSN_LABEL(iflt),
982 INSN_LABEL(ifge),
983 INSN_LABEL(ifgt),
984 INSN_LABEL(ifle),
985 INSN_LABEL(if_icmpeq),
986 INSN_LABEL(if_icmpne),
987 INSN_LABEL(if_icmplt),
988 INSN_LABEL(if_icmpge),
989 INSN_LABEL(if_icmpgt),
990 INSN_LABEL(if_icmple),
991 INSN_LABEL(if_acmpeq),
992 INSN_LABEL(if_acmpne),
993 INSN_LABEL(goto),
994 INSN_LABEL(jsr),
995 INSN_LABEL(ret),
996 INSN_LABEL(tableswitch),
997 INSN_LABEL(lookupswitch),
998 INSN_LABEL(ireturn),
999 INSN_LABEL(lreturn),
1000 INSN_LABEL(freturn),
1001 INSN_LABEL(dreturn),
1002 INSN_LABEL(areturn),
1003 INSN_LABEL(return),
1004 INSN_LABEL(getstatic),
1005 INSN_LABEL(putstatic),
1006 INSN_LABEL(getfield),
1007 INSN_LABEL(putfield),
1008 INSN_LABEL(invokevirtual),
1009 INSN_LABEL(invokespecial),
1010 INSN_LABEL(invokestatic),
1011 INSN_LABEL(invokeinterface),
1012 0, /* Unused. */
1013 INSN_LABEL(new),
1014 INSN_LABEL(newarray),
1015 INSN_LABEL(anewarray),
1016 INSN_LABEL(arraylength),
1017 INSN_LABEL(athrow),
1018 INSN_LABEL(checkcast),
1019 INSN_LABEL(instanceof),
1020 INSN_LABEL(monitorenter),
1021 INSN_LABEL(monitorexit),
1022 #ifdef DIRECT_THREADED
1023 0, // wide
1024 #else
1025 INSN_LABEL(wide),
1026 #endif
1027 INSN_LABEL(multianewarray),
1028 INSN_LABEL(ifnull),
1029 INSN_LABEL(ifnonnull),
1030 INSN_LABEL(goto_w),
1031 INSN_LABEL(jsr_w),
1032 #ifdef DIRECT_THREADED
1033 INSN_LABEL (ldc_class)
1034 #else
1036 #endif
1039 pc_t pc;
1041 #ifdef DIRECT_THREADED
1043 #define NEXT_INSN goto *((pc++)->insn)
1044 #define INTVAL() ((pc++)->int_val)
1045 #define AVAL() ((pc++)->datum)
1047 #define GET1S() INTVAL ()
1048 #define GET2S() INTVAL ()
1049 #define GET1U() INTVAL ()
1050 #define GET2U() INTVAL ()
1051 #define AVAL1U() AVAL ()
1052 #define AVAL2U() AVAL ()
1053 #define AVAL2UP() AVAL ()
1054 #define SKIP_GOTO ++pc
1055 #define GOTO_VAL() (insn_slot *) pc->datum
1056 #define PCVAL(unionval) unionval.p
1057 #define AMPAMP(label) &&label
1059 // Compile if we must. NOTE: Double-check locking.
1060 if (meth->prepared == NULL)
1062 _Jv_MutexLock (&compile_mutex);
1063 if (meth->prepared == NULL)
1064 meth->compile (insn_target);
1065 _Jv_MutexUnlock (&compile_mutex);
1068 // If we're only compiling, stop here
1069 if (args == NULL)
1070 return;
1072 pc = (insn_slot *) meth->prepared;
1074 #else
1076 #define NEXT_INSN goto *(insn_target[*pc++])
1078 #define GET1S() get1s (pc++)
1079 #define GET2S() (pc += 2, get2s (pc- 2))
1080 #define GET1U() get1u (pc++)
1081 #define GET2U() (pc += 2, get2u (pc - 2))
1082 // Note that these could be more efficient when not handling 'ldc
1083 // class'.
1084 #define AVAL1U() \
1085 ({ int index = get1u (pc++); \
1086 resolve_pool_entry (meth->defining_class, index).o; })
1087 #define AVAL2U() \
1088 ({ int index = get2u (pc); pc += 2; \
1089 resolve_pool_entry (meth->defining_class, index).o; })
1090 // Note that we don't need to resolve the pool entry here as class
1091 // constants are never wide.
1092 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1093 #define SKIP_GOTO pc += 2
1094 #define GOTO_VAL() pc - 1 + get2s (pc)
1095 #define PCVAL(unionval) unionval.i
1096 #define AMPAMP(label) NULL
1098 pc = bytecode ();
1100 #endif /* DIRECT_THREADED */
1102 #define TAKE_GOTO pc = GOTO_VAL ()
1104 /* Go straight at it! the ffi raw format matches the internal
1105 stack representation exactly. At least, that's the idea.
1107 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1109 _Jv_word *pool_data = meth->defining_class->constants.data;
1111 /* These three are temporaries for common code used by several
1112 instructions. */
1113 void (*fun)();
1114 _Jv_ResolvedMethod* rmeth;
1115 int tmpval;
1119 // We keep nop around. It is used if we're interpreting the
1120 // bytecodes and not doing direct threading.
1121 insn_nop:
1122 NEXT_INSN;
1124 /* The first few instructions here are ordered according to their
1125 frequency, in the hope that this will improve code locality a
1126 little. */
1128 insn_aload_0: // 0x2a
1129 LOADA (0);
1130 NEXT_INSN;
1132 insn_iload: // 0x15
1133 LOADI (GET1U ());
1134 NEXT_INSN;
1136 insn_iload_1: // 0x1b
1137 LOADI (1);
1138 NEXT_INSN;
1140 insn_invokevirtual: // 0xb6
1142 int index = GET2U ();
1144 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1145 * value already is resolved. If we want to clutter up the
1146 * code here to gain a little performance, then we can check
1147 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1148 * directly. For now, I don't think it is worth it. */
1150 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1151 index)).rmethod;
1153 sp -= rmeth->stack_item_count;
1155 if (rmeth->method->accflags & Modifier::FINAL)
1157 // We can't rely on NULLCHECK working if the method is final.
1158 SAVE_PC();
1159 if (! sp[0].o)
1160 throw_null_pointer_exception ();
1162 // Final methods might not appear in the vtable.
1163 fun = (void (*)()) rmeth->method->ncode;
1165 else
1167 NULLCHECK (sp[0].o);
1168 jobject rcv = sp[0].o;
1169 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1170 fun = (void (*)()) table->get_method (rmeth->method->index);
1173 #ifdef DIRECT_THREADED
1174 // Rewrite instruction so that we use a faster pre-resolved
1175 // method.
1176 pc[-2].insn = &&invokevirtual_resolved;
1177 pc[-1].datum = rmeth;
1178 #endif /* DIRECT_THREADED */
1180 goto perform_invoke;
1182 #ifdef DIRECT_THREADED
1183 invokevirtual_resolved:
1185 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1186 sp -= rmeth->stack_item_count;
1188 if (rmeth->method->accflags & Modifier::FINAL)
1190 // We can't rely on NULLCHECK working if the method is final.
1191 SAVE_PC();
1192 if (! sp[0].o)
1193 throw_null_pointer_exception ();
1195 // Final methods might not appear in the vtable.
1196 fun = (void (*)()) rmeth->method->ncode;
1198 else
1200 jobject rcv = sp[0].o;
1201 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1202 fun = (void (*)()) table->get_method (rmeth->method->index);
1205 goto perform_invoke;
1206 #endif /* DIRECT_THREADED */
1208 perform_invoke:
1210 SAVE_PC();
1212 /* here goes the magic again... */
1213 ffi_cif *cif = &rmeth->cif;
1214 ffi_raw *raw = (ffi_raw*) sp;
1216 _Jv_value rvalue;
1218 #if FFI_NATIVE_RAW_API
1219 /* We assume that this is only implemented if it's correct */
1220 /* to use it here. On a 64 bit machine, it never is. */
1221 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1222 #else
1223 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1224 #endif
1226 int rtype = cif->rtype->type;
1228 /* the likelyhood of object, int, or void return is very high,
1229 * so those are checked before the switch */
1230 if (rtype == FFI_TYPE_POINTER)
1232 PUSHA (rvalue.object_value);
1234 else if (rtype == FFI_TYPE_SINT32)
1236 PUSHI (rvalue.int_value);
1238 else if (rtype == FFI_TYPE_VOID)
1240 /* skip */
1242 else
1244 switch (rtype)
1246 case FFI_TYPE_SINT8:
1247 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1248 break;
1250 case FFI_TYPE_SINT16:
1251 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1252 break;
1254 case FFI_TYPE_UINT16:
1255 PUSHI (rvalue.int_value & 0xffff);
1256 break;
1258 case FFI_TYPE_FLOAT:
1259 PUSHF (rvalue.float_value);
1260 break;
1262 case FFI_TYPE_DOUBLE:
1263 PUSHD (rvalue.double_value);
1264 break;
1266 case FFI_TYPE_SINT64:
1267 PUSHL (rvalue.long_value);
1268 break;
1270 default:
1271 throw_internal_error ("unknown return type in invokeXXX");
1275 NEXT_INSN;
1277 insn_aconst_null:
1278 PUSHA (NULL);
1279 NEXT_INSN;
1281 insn_iconst_m1:
1282 PUSHI (-1);
1283 NEXT_INSN;
1285 insn_iconst_0:
1286 PUSHI (0);
1287 NEXT_INSN;
1289 insn_iconst_1:
1290 PUSHI (1);
1291 NEXT_INSN;
1293 insn_iconst_2:
1294 PUSHI (2);
1295 NEXT_INSN;
1297 insn_iconst_3:
1298 PUSHI (3);
1299 NEXT_INSN;
1301 insn_iconst_4:
1302 PUSHI (4);
1303 NEXT_INSN;
1305 insn_iconst_5:
1306 PUSHI (5);
1307 NEXT_INSN;
1309 insn_lconst_0:
1310 PUSHL (0);
1311 NEXT_INSN;
1313 insn_lconst_1:
1314 PUSHL (1);
1315 NEXT_INSN;
1317 insn_fconst_0:
1318 PUSHF (0);
1319 NEXT_INSN;
1321 insn_fconst_1:
1322 PUSHF (1);
1323 NEXT_INSN;
1325 insn_fconst_2:
1326 PUSHF (2);
1327 NEXT_INSN;
1329 insn_dconst_0:
1330 PUSHD (0);
1331 NEXT_INSN;
1333 insn_dconst_1:
1334 PUSHD (1);
1335 NEXT_INSN;
1337 insn_bipush:
1338 // For direct threaded, bipush and sipush are the same.
1339 #ifndef DIRECT_THREADED
1340 PUSHI (GET1S ());
1341 NEXT_INSN;
1342 #endif /* DIRECT_THREADED */
1343 insn_sipush:
1344 PUSHI (GET2S ());
1345 NEXT_INSN;
1347 insn_ldc:
1348 // For direct threaded, ldc and ldc_w are the same.
1349 #ifndef DIRECT_THREADED
1350 PUSHA ((jobject) AVAL1U ());
1351 NEXT_INSN;
1352 #endif /* DIRECT_THREADED */
1353 insn_ldc_w:
1354 PUSHA ((jobject) AVAL2U ());
1355 NEXT_INSN;
1357 #ifdef DIRECT_THREADED
1358 // For direct threaded we have a separate 'ldc class' operation.
1359 insn_ldc_class:
1361 // We could rewrite the instruction at this point.
1362 int index = INTVAL ();
1363 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1364 index)).o;
1365 PUSHA (k);
1367 NEXT_INSN;
1368 #endif /* DIRECT_THREADED */
1370 insn_ldc2_w:
1372 void *where = AVAL2UP ();
1373 memcpy (sp, where, 2*sizeof (_Jv_word));
1374 sp += 2;
1376 NEXT_INSN;
1378 insn_lload:
1379 LOADL (GET1U ());
1380 NEXT_INSN;
1382 insn_fload:
1383 LOADF (GET1U ());
1384 NEXT_INSN;
1386 insn_dload:
1387 LOADD (GET1U ());
1388 NEXT_INSN;
1390 insn_aload:
1391 LOADA (GET1U ());
1392 NEXT_INSN;
1394 insn_iload_0:
1395 LOADI (0);
1396 NEXT_INSN;
1398 insn_iload_2:
1399 LOADI (2);
1400 NEXT_INSN;
1402 insn_iload_3:
1403 LOADI (3);
1404 NEXT_INSN;
1406 insn_lload_0:
1407 LOADL (0);
1408 NEXT_INSN;
1410 insn_lload_1:
1411 LOADL (1);
1412 NEXT_INSN;
1414 insn_lload_2:
1415 LOADL (2);
1416 NEXT_INSN;
1418 insn_lload_3:
1419 LOADL (3);
1420 NEXT_INSN;
1422 insn_fload_0:
1423 LOADF (0);
1424 NEXT_INSN;
1426 insn_fload_1:
1427 LOADF (1);
1428 NEXT_INSN;
1430 insn_fload_2:
1431 LOADF (2);
1432 NEXT_INSN;
1434 insn_fload_3:
1435 LOADF (3);
1436 NEXT_INSN;
1438 insn_dload_0:
1439 LOADD (0);
1440 NEXT_INSN;
1442 insn_dload_1:
1443 LOADD (1);
1444 NEXT_INSN;
1446 insn_dload_2:
1447 LOADD (2);
1448 NEXT_INSN;
1450 insn_dload_3:
1451 LOADD (3);
1452 NEXT_INSN;
1454 insn_aload_1:
1455 LOADA(1);
1456 NEXT_INSN;
1458 insn_aload_2:
1459 LOADA(2);
1460 NEXT_INSN;
1462 insn_aload_3:
1463 LOADA(3);
1464 NEXT_INSN;
1466 insn_iaload:
1468 jint index = POPI();
1469 jintArray arr = (jintArray) POPA();
1470 NULLARRAYCHECK (arr);
1471 ARRAYBOUNDSCHECK (arr, index);
1472 PUSHI( elements(arr)[index] );
1474 NEXT_INSN;
1476 insn_laload:
1478 jint index = POPI();
1479 jlongArray arr = (jlongArray) POPA();
1480 NULLARRAYCHECK (arr);
1481 ARRAYBOUNDSCHECK (arr, index);
1482 PUSHL( elements(arr)[index] );
1484 NEXT_INSN;
1486 insn_faload:
1488 jint index = POPI();
1489 jfloatArray arr = (jfloatArray) POPA();
1490 NULLARRAYCHECK (arr);
1491 ARRAYBOUNDSCHECK (arr, index);
1492 PUSHF( elements(arr)[index] );
1494 NEXT_INSN;
1496 insn_daload:
1498 jint index = POPI();
1499 jdoubleArray arr = (jdoubleArray) POPA();
1500 NULLARRAYCHECK (arr);
1501 ARRAYBOUNDSCHECK (arr, index);
1502 PUSHD( elements(arr)[index] );
1504 NEXT_INSN;
1506 insn_aaload:
1508 jint index = POPI();
1509 jobjectArray arr = (jobjectArray) POPA();
1510 NULLARRAYCHECK (arr);
1511 ARRAYBOUNDSCHECK (arr, index);
1512 PUSHA( elements(arr)[index] );
1514 NEXT_INSN;
1516 insn_baload:
1518 jint index = POPI();
1519 jbyteArray arr = (jbyteArray) POPA();
1520 NULLARRAYCHECK (arr);
1521 ARRAYBOUNDSCHECK (arr, index);
1522 PUSHI( elements(arr)[index] );
1524 NEXT_INSN;
1526 insn_caload:
1528 jint index = POPI();
1529 jcharArray arr = (jcharArray) POPA();
1530 NULLARRAYCHECK (arr);
1531 ARRAYBOUNDSCHECK (arr, index);
1532 PUSHI( elements(arr)[index] );
1534 NEXT_INSN;
1536 insn_saload:
1538 jint index = POPI();
1539 jshortArray arr = (jshortArray) POPA();
1540 NULLARRAYCHECK (arr);
1541 ARRAYBOUNDSCHECK (arr, index);
1542 PUSHI( elements(arr)[index] );
1544 NEXT_INSN;
1546 insn_istore:
1547 STOREI (GET1U ());
1548 NEXT_INSN;
1550 insn_lstore:
1551 STOREL (GET1U ());
1552 NEXT_INSN;
1554 insn_fstore:
1555 STOREF (GET1U ());
1556 NEXT_INSN;
1558 insn_dstore:
1559 STORED (GET1U ());
1560 NEXT_INSN;
1562 insn_astore:
1563 STOREA (GET1U ());
1564 NEXT_INSN;
1566 insn_istore_0:
1567 STOREI (0);
1568 NEXT_INSN;
1570 insn_istore_1:
1571 STOREI (1);
1572 NEXT_INSN;
1574 insn_istore_2:
1575 STOREI (2);
1576 NEXT_INSN;
1578 insn_istore_3:
1579 STOREI (3);
1580 NEXT_INSN;
1582 insn_lstore_0:
1583 STOREL (0);
1584 NEXT_INSN;
1586 insn_lstore_1:
1587 STOREL (1);
1588 NEXT_INSN;
1590 insn_lstore_2:
1591 STOREL (2);
1592 NEXT_INSN;
1594 insn_lstore_3:
1595 STOREL (3);
1596 NEXT_INSN;
1598 insn_fstore_0:
1599 STOREF (0);
1600 NEXT_INSN;
1602 insn_fstore_1:
1603 STOREF (1);
1604 NEXT_INSN;
1606 insn_fstore_2:
1607 STOREF (2);
1608 NEXT_INSN;
1610 insn_fstore_3:
1611 STOREF (3);
1612 NEXT_INSN;
1614 insn_dstore_0:
1615 STORED (0);
1616 NEXT_INSN;
1618 insn_dstore_1:
1619 STORED (1);
1620 NEXT_INSN;
1622 insn_dstore_2:
1623 STORED (2);
1624 NEXT_INSN;
1626 insn_dstore_3:
1627 STORED (3);
1628 NEXT_INSN;
1630 insn_astore_0:
1631 STOREA(0);
1632 NEXT_INSN;
1634 insn_astore_1:
1635 STOREA(1);
1636 NEXT_INSN;
1638 insn_astore_2:
1639 STOREA(2);
1640 NEXT_INSN;
1642 insn_astore_3:
1643 STOREA(3);
1644 NEXT_INSN;
1646 insn_iastore:
1648 jint value = POPI();
1649 jint index = POPI();
1650 jintArray arr = (jintArray) POPA();
1651 NULLARRAYCHECK (arr);
1652 ARRAYBOUNDSCHECK (arr, index);
1653 elements(arr)[index] = value;
1655 NEXT_INSN;
1657 insn_lastore:
1659 jlong value = POPL();
1660 jint index = POPI();
1661 jlongArray arr = (jlongArray) POPA();
1662 NULLARRAYCHECK (arr);
1663 ARRAYBOUNDSCHECK (arr, index);
1664 elements(arr)[index] = value;
1666 NEXT_INSN;
1668 insn_fastore:
1670 jfloat value = POPF();
1671 jint index = POPI();
1672 jfloatArray arr = (jfloatArray) POPA();
1673 NULLARRAYCHECK (arr);
1674 ARRAYBOUNDSCHECK (arr, index);
1675 elements(arr)[index] = value;
1677 NEXT_INSN;
1679 insn_dastore:
1681 jdouble value = POPD();
1682 jint index = POPI();
1683 jdoubleArray arr = (jdoubleArray) POPA();
1684 NULLARRAYCHECK (arr);
1685 ARRAYBOUNDSCHECK (arr, index);
1686 elements(arr)[index] = value;
1688 NEXT_INSN;
1690 insn_aastore:
1692 jobject value = POPA();
1693 jint index = POPI();
1694 jobjectArray arr = (jobjectArray) POPA();
1695 NULLARRAYCHECK (arr);
1696 ARRAYBOUNDSCHECK (arr, index);
1697 _Jv_CheckArrayStore (arr, value);
1698 elements(arr)[index] = value;
1700 NEXT_INSN;
1702 insn_bastore:
1704 jbyte value = (jbyte) POPI();
1705 jint index = POPI();
1706 jbyteArray arr = (jbyteArray) POPA();
1707 NULLARRAYCHECK (arr);
1708 ARRAYBOUNDSCHECK (arr, index);
1709 elements(arr)[index] = value;
1711 NEXT_INSN;
1713 insn_castore:
1715 jchar value = (jchar) POPI();
1716 jint index = POPI();
1717 jcharArray arr = (jcharArray) POPA();
1718 NULLARRAYCHECK (arr);
1719 ARRAYBOUNDSCHECK (arr, index);
1720 elements(arr)[index] = value;
1722 NEXT_INSN;
1724 insn_sastore:
1726 jshort value = (jshort) POPI();
1727 jint index = POPI();
1728 jshortArray arr = (jshortArray) POPA();
1729 NULLARRAYCHECK (arr);
1730 ARRAYBOUNDSCHECK (arr, index);
1731 elements(arr)[index] = value;
1733 NEXT_INSN;
1735 insn_pop:
1736 sp -= 1;
1737 NEXT_INSN;
1739 insn_pop2:
1740 sp -= 2;
1741 NEXT_INSN;
1743 insn_dup:
1744 sp[0] = sp[-1];
1745 sp += 1;
1746 NEXT_INSN;
1748 insn_dup_x1:
1749 dupx (sp, 1, 1); sp+=1;
1750 NEXT_INSN;
1752 insn_dup_x2:
1753 dupx (sp, 1, 2); sp+=1;
1754 NEXT_INSN;
1756 insn_dup2:
1757 sp[0] = sp[-2];
1758 sp[1] = sp[-1];
1759 sp += 2;
1760 NEXT_INSN;
1762 insn_dup2_x1:
1763 dupx (sp, 2, 1); sp+=2;
1764 NEXT_INSN;
1766 insn_dup2_x2:
1767 dupx (sp, 2, 2); sp+=2;
1768 NEXT_INSN;
1770 insn_swap:
1772 jobject tmp1 = POPA();
1773 jobject tmp2 = POPA();
1774 PUSHA (tmp1);
1775 PUSHA (tmp2);
1777 NEXT_INSN;
1779 insn_iadd:
1780 BINOPI(+);
1781 NEXT_INSN;
1783 insn_ladd:
1784 BINOPL(+);
1785 NEXT_INSN;
1787 insn_fadd:
1788 BINOPF(+);
1789 NEXT_INSN;
1791 insn_dadd:
1792 BINOPD(+);
1793 NEXT_INSN;
1795 insn_isub:
1796 BINOPI(-);
1797 NEXT_INSN;
1799 insn_lsub:
1800 BINOPL(-);
1801 NEXT_INSN;
1803 insn_fsub:
1804 BINOPF(-);
1805 NEXT_INSN;
1807 insn_dsub:
1808 BINOPD(-);
1809 NEXT_INSN;
1811 insn_imul:
1812 BINOPI(*);
1813 NEXT_INSN;
1815 insn_lmul:
1816 BINOPL(*);
1817 NEXT_INSN;
1819 insn_fmul:
1820 BINOPF(*);
1821 NEXT_INSN;
1823 insn_dmul:
1824 BINOPD(*);
1825 NEXT_INSN;
1827 insn_idiv:
1829 jint value2 = POPI();
1830 jint value1 = POPI();
1831 jint res = _Jv_divI (value1, value2);
1832 PUSHI (res);
1834 NEXT_INSN;
1836 insn_ldiv:
1838 jlong value2 = POPL();
1839 jlong value1 = POPL();
1840 jlong res = _Jv_divJ (value1, value2);
1841 PUSHL (res);
1843 NEXT_INSN;
1845 insn_fdiv:
1847 jfloat value2 = POPF();
1848 jfloat value1 = POPF();
1849 jfloat res = value1 / value2;
1850 PUSHF (res);
1852 NEXT_INSN;
1854 insn_ddiv:
1856 jdouble value2 = POPD();
1857 jdouble value1 = POPD();
1858 jdouble res = value1 / value2;
1859 PUSHD (res);
1861 NEXT_INSN;
1863 insn_irem:
1865 jint value2 = POPI();
1866 jint value1 = POPI();
1867 jint res = _Jv_remI (value1, value2);
1868 PUSHI (res);
1870 NEXT_INSN;
1872 insn_lrem:
1874 jlong value2 = POPL();
1875 jlong value1 = POPL();
1876 jlong res = _Jv_remJ (value1, value2);
1877 PUSHL (res);
1879 NEXT_INSN;
1881 insn_frem:
1883 jfloat value2 = POPF();
1884 jfloat value1 = POPF();
1885 jfloat res = __ieee754_fmod (value1, value2);
1886 PUSHF (res);
1888 NEXT_INSN;
1890 insn_drem:
1892 jdouble value2 = POPD();
1893 jdouble value1 = POPD();
1894 jdouble res = __ieee754_fmod (value1, value2);
1895 PUSHD (res);
1897 NEXT_INSN;
1899 insn_ineg:
1901 jint value = POPI();
1902 PUSHI (value * -1);
1904 NEXT_INSN;
1906 insn_lneg:
1908 jlong value = POPL();
1909 PUSHL (value * -1);
1911 NEXT_INSN;
1913 insn_fneg:
1915 jfloat value = POPF();
1916 PUSHF (value * -1);
1918 NEXT_INSN;
1920 insn_dneg:
1922 jdouble value = POPD();
1923 PUSHD (value * -1);
1925 NEXT_INSN;
1927 insn_ishl:
1929 jint shift = (POPI() & 0x1f);
1930 jint value = POPI();
1931 PUSHI (value << shift);
1933 NEXT_INSN;
1935 insn_lshl:
1937 jint shift = (POPI() & 0x3f);
1938 jlong value = POPL();
1939 PUSHL (value << shift);
1941 NEXT_INSN;
1943 insn_ishr:
1945 jint shift = (POPI() & 0x1f);
1946 jint value = POPI();
1947 PUSHI (value >> shift);
1949 NEXT_INSN;
1951 insn_lshr:
1953 jint shift = (POPI() & 0x3f);
1954 jlong value = POPL();
1955 PUSHL (value >> shift);
1957 NEXT_INSN;
1959 insn_iushr:
1961 jint shift = (POPI() & 0x1f);
1962 _Jv_uint value = (_Jv_uint) POPI();
1963 PUSHI ((jint) (value >> shift));
1965 NEXT_INSN;
1967 insn_lushr:
1969 jint shift = (POPI() & 0x3f);
1970 _Jv_ulong value = (_Jv_ulong) POPL();
1971 PUSHL ((jlong) (value >> shift));
1973 NEXT_INSN;
1975 insn_iand:
1976 BINOPI (&);
1977 NEXT_INSN;
1979 insn_land:
1980 BINOPL (&);
1981 NEXT_INSN;
1983 insn_ior:
1984 BINOPI (|);
1985 NEXT_INSN;
1987 insn_lor:
1988 BINOPL (|);
1989 NEXT_INSN;
1991 insn_ixor:
1992 BINOPI (^);
1993 NEXT_INSN;
1995 insn_lxor:
1996 BINOPL (^);
1997 NEXT_INSN;
1999 insn_iinc:
2001 jint index = GET1U ();
2002 jint amount = GET1S ();
2003 locals[index].i += amount;
2005 NEXT_INSN;
2007 insn_i2l:
2008 {jlong value = POPI(); PUSHL (value);}
2009 NEXT_INSN;
2011 insn_i2f:
2012 {jfloat value = POPI(); PUSHF (value);}
2013 NEXT_INSN;
2015 insn_i2d:
2016 {jdouble value = POPI(); PUSHD (value);}
2017 NEXT_INSN;
2019 insn_l2i:
2020 {jint value = POPL(); PUSHI (value);}
2021 NEXT_INSN;
2023 insn_l2f:
2024 {jfloat value = POPL(); PUSHF (value);}
2025 NEXT_INSN;
2027 insn_l2d:
2028 {jdouble value = POPL(); PUSHD (value);}
2029 NEXT_INSN;
2031 insn_f2i:
2033 using namespace java::lang;
2034 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2035 PUSHI(value);
2037 NEXT_INSN;
2039 insn_f2l:
2041 using namespace java::lang;
2042 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2043 PUSHL(value);
2045 NEXT_INSN;
2047 insn_f2d:
2048 { jdouble value = POPF (); PUSHD(value); }
2049 NEXT_INSN;
2051 insn_d2i:
2053 using namespace java::lang;
2054 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2055 PUSHI(value);
2057 NEXT_INSN;
2059 insn_d2l:
2061 using namespace java::lang;
2062 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2063 PUSHL(value);
2065 NEXT_INSN;
2067 insn_d2f:
2068 { jfloat value = POPD (); PUSHF(value); }
2069 NEXT_INSN;
2071 insn_i2b:
2072 { jbyte value = POPI (); PUSHI(value); }
2073 NEXT_INSN;
2075 insn_i2c:
2076 { jchar value = POPI (); PUSHI(value); }
2077 NEXT_INSN;
2079 insn_i2s:
2080 { jshort value = POPI (); PUSHI(value); }
2081 NEXT_INSN;
2083 insn_lcmp:
2085 jlong value2 = POPL ();
2086 jlong value1 = POPL ();
2087 if (value1 > value2)
2088 { PUSHI (1); }
2089 else if (value1 == value2)
2090 { PUSHI (0); }
2091 else
2092 { PUSHI (-1); }
2094 NEXT_INSN;
2096 insn_fcmpl:
2097 tmpval = -1;
2098 goto fcmp;
2100 insn_fcmpg:
2101 tmpval = 1;
2103 fcmp:
2105 jfloat value2 = POPF ();
2106 jfloat value1 = POPF ();
2107 if (value1 > value2)
2108 PUSHI (1);
2109 else if (value1 == value2)
2110 PUSHI (0);
2111 else if (value1 < value2)
2112 PUSHI (-1);
2113 else
2114 PUSHI (tmpval);
2116 NEXT_INSN;
2118 insn_dcmpl:
2119 tmpval = -1;
2120 goto dcmp;
2122 insn_dcmpg:
2123 tmpval = 1;
2125 dcmp:
2127 jdouble value2 = POPD ();
2128 jdouble value1 = POPD ();
2129 if (value1 > value2)
2130 PUSHI (1);
2131 else if (value1 == value2)
2132 PUSHI (0);
2133 else if (value1 < value2)
2134 PUSHI (-1);
2135 else
2136 PUSHI (tmpval);
2138 NEXT_INSN;
2140 insn_ifeq:
2142 if (POPI() == 0)
2143 TAKE_GOTO;
2144 else
2145 SKIP_GOTO;
2147 NEXT_INSN;
2149 insn_ifne:
2151 if (POPI() != 0)
2152 TAKE_GOTO;
2153 else
2154 SKIP_GOTO;
2156 NEXT_INSN;
2158 insn_iflt:
2160 if (POPI() < 0)
2161 TAKE_GOTO;
2162 else
2163 SKIP_GOTO;
2165 NEXT_INSN;
2167 insn_ifge:
2169 if (POPI() >= 0)
2170 TAKE_GOTO;
2171 else
2172 SKIP_GOTO;
2174 NEXT_INSN;
2176 insn_ifgt:
2178 if (POPI() > 0)
2179 TAKE_GOTO;
2180 else
2181 SKIP_GOTO;
2183 NEXT_INSN;
2185 insn_ifle:
2187 if (POPI() <= 0)
2188 TAKE_GOTO;
2189 else
2190 SKIP_GOTO;
2192 NEXT_INSN;
2194 insn_if_icmpeq:
2196 jint value2 = POPI();
2197 jint value1 = POPI();
2198 if (value1 == value2)
2199 TAKE_GOTO;
2200 else
2201 SKIP_GOTO;
2203 NEXT_INSN;
2205 insn_if_icmpne:
2207 jint value2 = POPI();
2208 jint value1 = POPI();
2209 if (value1 != value2)
2210 TAKE_GOTO;
2211 else
2212 SKIP_GOTO;
2214 NEXT_INSN;
2216 insn_if_icmplt:
2218 jint value2 = POPI();
2219 jint value1 = POPI();
2220 if (value1 < value2)
2221 TAKE_GOTO;
2222 else
2223 SKIP_GOTO;
2225 NEXT_INSN;
2227 insn_if_icmpge:
2229 jint value2 = POPI();
2230 jint value1 = POPI();
2231 if (value1 >= value2)
2232 TAKE_GOTO;
2233 else
2234 SKIP_GOTO;
2236 NEXT_INSN;
2238 insn_if_icmpgt:
2240 jint value2 = POPI();
2241 jint value1 = POPI();
2242 if (value1 > value2)
2243 TAKE_GOTO;
2244 else
2245 SKIP_GOTO;
2247 NEXT_INSN;
2249 insn_if_icmple:
2251 jint value2 = POPI();
2252 jint value1 = POPI();
2253 if (value1 <= value2)
2254 TAKE_GOTO;
2255 else
2256 SKIP_GOTO;
2258 NEXT_INSN;
2260 insn_if_acmpeq:
2262 jobject value2 = POPA();
2263 jobject value1 = POPA();
2264 if (value1 == value2)
2265 TAKE_GOTO;
2266 else
2267 SKIP_GOTO;
2269 NEXT_INSN;
2271 insn_if_acmpne:
2273 jobject value2 = POPA();
2274 jobject value1 = POPA();
2275 if (value1 != value2)
2276 TAKE_GOTO;
2277 else
2278 SKIP_GOTO;
2280 NEXT_INSN;
2282 insn_goto_w:
2283 #ifndef DIRECT_THREADED
2284 // For direct threaded, goto and goto_w are the same.
2285 pc = pc - 1 + get4 (pc);
2286 NEXT_INSN;
2287 #endif /* DIRECT_THREADED */
2288 insn_goto:
2289 TAKE_GOTO;
2290 NEXT_INSN;
2292 insn_jsr_w:
2293 #ifndef DIRECT_THREADED
2294 // For direct threaded, jsr and jsr_w are the same.
2296 pc_t next = pc - 1 + get4 (pc);
2297 pc += 4;
2298 PUSHA ((jobject) pc);
2299 pc = next;
2301 NEXT_INSN;
2302 #endif /* DIRECT_THREADED */
2303 insn_jsr:
2305 pc_t next = GOTO_VAL();
2306 SKIP_GOTO;
2307 PUSHA ((jobject) pc);
2308 pc = next;
2310 NEXT_INSN;
2312 insn_ret:
2314 jint index = GET1U ();
2315 pc = (pc_t) PEEKA (index);
2317 NEXT_INSN;
2319 insn_tableswitch:
2321 #ifdef DIRECT_THREADED
2322 void *def = (pc++)->datum;
2324 int index = POPI();
2326 jint low = INTVAL ();
2327 jint high = INTVAL ();
2329 if (index < low || index > high)
2330 pc = (insn_slot *) def;
2331 else
2332 pc = (insn_slot *) ((pc + index - low)->datum);
2333 #else
2334 pc_t base_pc = pc - 1;
2335 int index = POPI ();
2337 pc_t base = (pc_t) bytecode ();
2338 while ((pc - base) % 4 != 0)
2339 ++pc;
2341 jint def = get4 (pc);
2342 jint low = get4 (pc + 4);
2343 jint high = get4 (pc + 8);
2344 if (index < low || index > high)
2345 pc = base_pc + def;
2346 else
2347 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2348 #endif /* DIRECT_THREADED */
2350 NEXT_INSN;
2352 insn_lookupswitch:
2354 #ifdef DIRECT_THREADED
2355 void *def = (pc++)->insn;
2357 int index = POPI();
2359 jint npairs = INTVAL ();
2361 int max = npairs - 1;
2362 int min = 0;
2364 // Simple binary search...
2365 while (min < max)
2367 int half = (min + max) / 2;
2368 int match = pc[2 * half].int_val;
2370 if (index == match)
2372 // Found it.
2373 pc = (insn_slot *) pc[2 * half + 1].datum;
2374 NEXT_INSN;
2376 else if (index < match)
2377 // We can use HALF - 1 here because we check again on
2378 // loop exit.
2379 max = half - 1;
2380 else
2381 // We can use HALF + 1 here because we check again on
2382 // loop exit.
2383 min = half + 1;
2385 if (index == pc[2 * min].int_val)
2386 pc = (insn_slot *) pc[2 * min + 1].datum;
2387 else
2388 pc = (insn_slot *) def;
2389 #else
2390 unsigned char *base_pc = pc-1;
2391 int index = POPI();
2393 unsigned char* base = bytecode ();
2394 while ((pc-base) % 4 != 0)
2395 ++pc;
2397 jint def = get4 (pc);
2398 jint npairs = get4 (pc+4);
2400 int max = npairs-1;
2401 int min = 0;
2403 // Simple binary search...
2404 while (min < max)
2406 int half = (min+max)/2;
2407 int match = get4 (pc+ 4*(2 + 2*half));
2409 if (index == match)
2410 min = max = half;
2411 else if (index < match)
2412 // We can use HALF - 1 here because we check again on
2413 // loop exit.
2414 max = half - 1;
2415 else
2416 // We can use HALF + 1 here because we check again on
2417 // loop exit.
2418 min = half + 1;
2421 if (index == get4 (pc+ 4*(2 + 2*min)))
2422 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2423 else
2424 pc = base_pc + def;
2425 #endif /* DIRECT_THREADED */
2427 NEXT_INSN;
2429 insn_areturn:
2430 *(jobject *) retp = POPA ();
2431 return;
2433 insn_lreturn:
2434 *(jlong *) retp = POPL ();
2435 return;
2437 insn_freturn:
2438 *(jfloat *) retp = POPF ();
2439 return;
2441 insn_dreturn:
2442 *(jdouble *) retp = POPD ();
2443 return;
2445 insn_ireturn:
2446 *(jint *) retp = POPI ();
2447 return;
2449 insn_return:
2450 return;
2452 insn_getstatic:
2454 jint fieldref_index = GET2U ();
2455 SAVE_PC(); // Constant pool resolution could throw.
2456 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2457 _Jv_Field *field = pool_data[fieldref_index].field;
2459 if ((field->flags & Modifier::STATIC) == 0)
2460 throw_incompatible_class_change_error
2461 (JvNewStringLatin1 ("field no longer static"));
2463 jclass type = field->type;
2465 // We rewrite the instruction once we discover what it refers
2466 // to.
2467 void *newinsn = NULL;
2468 if (type->isPrimitive ())
2470 switch (type->size_in_bytes)
2472 case 1:
2473 PUSHI (*field->u.byte_addr);
2474 newinsn = AMPAMP (getstatic_resolved_1);
2475 break;
2477 case 2:
2478 if (type == JvPrimClass (char))
2480 PUSHI (*field->u.char_addr);
2481 newinsn = AMPAMP (getstatic_resolved_char);
2483 else
2485 PUSHI (*field->u.short_addr);
2486 newinsn = AMPAMP (getstatic_resolved_short);
2488 break;
2490 case 4:
2491 PUSHI(*field->u.int_addr);
2492 newinsn = AMPAMP (getstatic_resolved_4);
2493 break;
2495 case 8:
2496 PUSHL(*field->u.long_addr);
2497 newinsn = AMPAMP (getstatic_resolved_8);
2498 break;
2501 else
2503 PUSHA(*field->u.object_addr);
2504 newinsn = AMPAMP (getstatic_resolved_obj);
2507 #ifdef DIRECT_THREADED
2508 pc[-2].insn = newinsn;
2509 pc[-1].datum = field->u.addr;
2510 #endif /* DIRECT_THREADED */
2512 NEXT_INSN;
2514 #ifdef DIRECT_THREADED
2515 getstatic_resolved_1:
2516 PUSHI (*(jbyte *) AVAL ());
2517 NEXT_INSN;
2519 getstatic_resolved_char:
2520 PUSHI (*(jchar *) AVAL ());
2521 NEXT_INSN;
2523 getstatic_resolved_short:
2524 PUSHI (*(jshort *) AVAL ());
2525 NEXT_INSN;
2527 getstatic_resolved_4:
2528 PUSHI (*(jint *) AVAL ());
2529 NEXT_INSN;
2531 getstatic_resolved_8:
2532 PUSHL (*(jlong *) AVAL ());
2533 NEXT_INSN;
2535 getstatic_resolved_obj:
2536 PUSHA (*(jobject *) AVAL ());
2537 NEXT_INSN;
2538 #endif /* DIRECT_THREADED */
2540 insn_getfield:
2542 jint fieldref_index = GET2U ();
2543 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2544 _Jv_Field *field = pool_data[fieldref_index].field;
2546 if ((field->flags & Modifier::STATIC) != 0)
2547 throw_incompatible_class_change_error
2548 (JvNewStringLatin1 ("field is static"));
2550 jclass type = field->type;
2551 jint field_offset = field->u.boffset;
2553 jobject obj = POPA();
2554 NULLCHECK(obj);
2556 void *newinsn = NULL;
2557 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2558 if (type->isPrimitive ())
2560 switch (type->size_in_bytes)
2562 case 1:
2563 PUSHI (val->byte_value);
2564 newinsn = AMPAMP (getfield_resolved_1);
2565 break;
2567 case 2:
2568 if (type == JvPrimClass (char))
2570 PUSHI (val->char_value);
2571 newinsn = AMPAMP (getfield_resolved_char);
2573 else
2575 PUSHI (val->short_value);
2576 newinsn = AMPAMP (getfield_resolved_short);
2578 break;
2580 case 4:
2581 PUSHI (val->int_value);
2582 newinsn = AMPAMP (getfield_resolved_4);
2583 break;
2585 case 8:
2586 PUSHL (val->long_value);
2587 newinsn = AMPAMP (getfield_resolved_8);
2588 break;
2591 else
2593 PUSHA (val->object_value);
2594 newinsn = AMPAMP (getfield_resolved_obj);
2597 #ifdef DIRECT_THREADED
2598 pc[-2].insn = newinsn;
2599 pc[-1].int_val = field_offset;
2600 #endif /* DIRECT_THREADED */
2602 NEXT_INSN;
2604 #ifdef DIRECT_THREADED
2605 getfield_resolved_1:
2607 char *obj = (char *) POPA ();
2608 NULLCHECK (obj);
2609 PUSHI (*(jbyte *) (obj + INTVAL ()));
2611 NEXT_INSN;
2613 getfield_resolved_char:
2615 char *obj = (char *) POPA ();
2616 NULLCHECK (obj);
2617 PUSHI (*(jchar *) (obj + INTVAL ()));
2619 NEXT_INSN;
2621 getfield_resolved_short:
2623 char *obj = (char *) POPA ();
2624 NULLCHECK (obj);
2625 PUSHI (*(jshort *) (obj + INTVAL ()));
2627 NEXT_INSN;
2629 getfield_resolved_4:
2631 char *obj = (char *) POPA ();
2632 NULLCHECK (obj);
2633 PUSHI (*(jint *) (obj + INTVAL ()));
2635 NEXT_INSN;
2637 getfield_resolved_8:
2639 char *obj = (char *) POPA ();
2640 NULLCHECK (obj);
2641 PUSHL (*(jlong *) (obj + INTVAL ()));
2643 NEXT_INSN;
2645 getfield_resolved_obj:
2647 char *obj = (char *) POPA ();
2648 NULLCHECK (obj);
2649 PUSHA (*(jobject *) (obj + INTVAL ()));
2651 NEXT_INSN;
2652 #endif /* DIRECT_THREADED */
2654 insn_putstatic:
2656 jint fieldref_index = GET2U ();
2657 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2658 _Jv_Field *field = pool_data[fieldref_index].field;
2660 jclass type = field->type;
2662 // ResolvePoolEntry cannot check this
2663 if ((field->flags & Modifier::STATIC) == 0)
2664 throw_incompatible_class_change_error
2665 (JvNewStringLatin1 ("field no longer static"));
2667 void *newinsn = NULL;
2668 if (type->isPrimitive ())
2670 switch (type->size_in_bytes)
2672 case 1:
2674 jint value = POPI();
2675 *field->u.byte_addr = value;
2676 newinsn = AMPAMP (putstatic_resolved_1);
2677 break;
2680 case 2:
2682 jint value = POPI();
2683 *field->u.char_addr = value;
2684 newinsn = AMPAMP (putstatic_resolved_2);
2685 break;
2688 case 4:
2690 jint value = POPI();
2691 *field->u.int_addr = value;
2692 newinsn = AMPAMP (putstatic_resolved_4);
2693 break;
2696 case 8:
2698 jlong value = POPL();
2699 *field->u.long_addr = value;
2700 newinsn = AMPAMP (putstatic_resolved_8);
2701 break;
2705 else
2707 jobject value = POPA();
2708 *field->u.object_addr = value;
2709 newinsn = AMPAMP (putstatic_resolved_obj);
2712 #ifdef DIRECT_THREADED
2713 pc[-2].insn = newinsn;
2714 pc[-1].datum = field->u.addr;
2715 #endif /* DIRECT_THREADED */
2717 NEXT_INSN;
2719 #ifdef DIRECT_THREADED
2720 putstatic_resolved_1:
2721 *(jbyte *) AVAL () = POPI ();
2722 NEXT_INSN;
2724 putstatic_resolved_2:
2725 *(jchar *) AVAL () = POPI ();
2726 NEXT_INSN;
2728 putstatic_resolved_4:
2729 *(jint *) AVAL () = POPI ();
2730 NEXT_INSN;
2732 putstatic_resolved_8:
2733 *(jlong *) AVAL () = POPL ();
2734 NEXT_INSN;
2736 putstatic_resolved_obj:
2737 *(jobject *) AVAL () = POPA ();
2738 NEXT_INSN;
2739 #endif /* DIRECT_THREADED */
2741 insn_putfield:
2743 jint fieldref_index = GET2U ();
2744 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2745 _Jv_Field *field = pool_data[fieldref_index].field;
2747 jclass type = field->type;
2749 if ((field->flags & Modifier::STATIC) != 0)
2750 throw_incompatible_class_change_error
2751 (JvNewStringLatin1 ("field is static"));
2753 jint field_offset = field->u.boffset;
2755 void *newinsn = NULL;
2756 if (type->isPrimitive ())
2758 switch (type->size_in_bytes)
2760 case 1:
2762 jint value = POPI();
2763 jobject obj = POPA();
2764 NULLCHECK(obj);
2765 *(jbyte*) ((char*)obj + field_offset) = value;
2766 newinsn = AMPAMP (putfield_resolved_1);
2767 break;
2770 case 2:
2772 jint value = POPI();
2773 jobject obj = POPA();
2774 NULLCHECK(obj);
2775 *(jchar*) ((char*)obj + field_offset) = value;
2776 newinsn = AMPAMP (putfield_resolved_2);
2777 break;
2780 case 4:
2782 jint value = POPI();
2783 jobject obj = POPA();
2784 NULLCHECK(obj);
2785 *(jint*) ((char*)obj + field_offset) = value;
2786 newinsn = AMPAMP (putfield_resolved_4);
2787 break;
2790 case 8:
2792 jlong value = POPL();
2793 jobject obj = POPA();
2794 NULLCHECK(obj);
2795 *(jlong*) ((char*)obj + field_offset) = value;
2796 newinsn = AMPAMP (putfield_resolved_8);
2797 break;
2801 else
2803 jobject value = POPA();
2804 jobject obj = POPA();
2805 NULLCHECK(obj);
2806 *(jobject*) ((char*)obj + field_offset) = value;
2807 newinsn = AMPAMP (putfield_resolved_obj);
2810 #ifdef DIRECT_THREADED
2811 pc[-2].insn = newinsn;
2812 pc[-1].int_val = field_offset;
2813 #endif /* DIRECT_THREADED */
2815 NEXT_INSN;
2817 #ifdef DIRECT_THREADED
2818 putfield_resolved_1:
2820 jint val = POPI ();
2821 char *obj = (char *) POPA ();
2822 NULLCHECK (obj);
2823 *(jbyte *) (obj + INTVAL ()) = val;
2825 NEXT_INSN;
2827 putfield_resolved_2:
2829 jint val = POPI ();
2830 char *obj = (char *) POPA ();
2831 NULLCHECK (obj);
2832 *(jchar *) (obj + INTVAL ()) = val;
2834 NEXT_INSN;
2836 putfield_resolved_4:
2838 jint val = POPI ();
2839 char *obj = (char *) POPA ();
2840 NULLCHECK (obj);
2841 *(jint *) (obj + INTVAL ()) = val;
2843 NEXT_INSN;
2845 putfield_resolved_8:
2847 jlong val = POPL ();
2848 char *obj = (char *) POPA ();
2849 NULLCHECK (obj);
2850 *(jlong *) (obj + INTVAL ()) = val;
2852 NEXT_INSN;
2854 putfield_resolved_obj:
2856 jobject val = POPA ();
2857 char *obj = (char *) POPA ();
2858 NULLCHECK (obj);
2859 *(jobject *) (obj + INTVAL ()) = val;
2861 NEXT_INSN;
2862 #endif /* DIRECT_THREADED */
2864 insn_invokespecial:
2866 int index = GET2U ();
2868 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2869 index)).rmethod;
2871 sp -= rmeth->stack_item_count;
2873 // We don't use NULLCHECK here because we can't rely on that
2874 // working for <init>. So instead we do an explicit test.
2875 if (! sp[0].o)
2877 SAVE_PC();
2878 throw_null_pointer_exception ();
2881 fun = (void (*)()) rmeth->method->ncode;
2883 #ifdef DIRECT_THREADED
2884 // Rewrite instruction so that we use a faster pre-resolved
2885 // method.
2886 pc[-2].insn = &&invokespecial_resolved;
2887 pc[-1].datum = rmeth;
2888 #endif /* DIRECT_THREADED */
2890 goto perform_invoke;
2892 #ifdef DIRECT_THREADED
2893 invokespecial_resolved:
2895 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2896 sp -= rmeth->stack_item_count;
2897 // We don't use NULLCHECK here because we can't rely on that
2898 // working for <init>. So instead we do an explicit test.
2899 if (! sp[0].o)
2901 SAVE_PC();
2902 throw_null_pointer_exception ();
2904 fun = (void (*)()) rmeth->method->ncode;
2906 goto perform_invoke;
2907 #endif /* DIRECT_THREADED */
2909 insn_invokestatic:
2911 int index = GET2U ();
2913 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2914 index)).rmethod;
2916 sp -= rmeth->stack_item_count;
2918 fun = (void (*)()) rmeth->method->ncode;
2920 #ifdef DIRECT_THREADED
2921 // Rewrite instruction so that we use a faster pre-resolved
2922 // method.
2923 pc[-2].insn = &&invokestatic_resolved;
2924 pc[-1].datum = rmeth;
2925 #endif /* DIRECT_THREADED */
2927 goto perform_invoke;
2929 #ifdef DIRECT_THREADED
2930 invokestatic_resolved:
2932 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2933 sp -= rmeth->stack_item_count;
2934 fun = (void (*)()) rmeth->method->ncode;
2936 goto perform_invoke;
2937 #endif /* DIRECT_THREADED */
2939 insn_invokeinterface:
2941 int index = GET2U ();
2943 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2944 index)).rmethod;
2946 sp -= rmeth->stack_item_count;
2948 jobject rcv = sp[0].o;
2950 NULLCHECK (rcv);
2952 fun = (void (*)())
2953 _Jv_LookupInterfaceMethod (rcv->getClass (),
2954 rmeth->method->name,
2955 rmeth->method->signature);
2957 #ifdef DIRECT_THREADED
2958 // Rewrite instruction so that we use a faster pre-resolved
2959 // method.
2960 pc[-2].insn = &&invokeinterface_resolved;
2961 pc[-1].datum = rmeth;
2962 #else
2963 // Skip dummy bytes.
2964 pc += 2;
2965 #endif /* DIRECT_THREADED */
2967 goto perform_invoke;
2969 #ifdef DIRECT_THREADED
2970 invokeinterface_resolved:
2972 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2973 sp -= rmeth->stack_item_count;
2974 jobject rcv = sp[0].o;
2975 NULLCHECK (rcv);
2976 fun = (void (*)())
2977 _Jv_LookupInterfaceMethod (rcv->getClass (),
2978 rmeth->method->name,
2979 rmeth->method->signature);
2981 goto perform_invoke;
2982 #endif /* DIRECT_THREADED */
2984 insn_new:
2986 int index = GET2U ();
2987 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2988 index)).clazz;
2989 /* VM spec, section 3.11.5 */
2990 if ((klass->getModifiers() & Modifier::ABSTRACT)
2991 || klass->isInterface())
2992 throw new java::lang::InstantiationException;
2993 jobject res = _Jv_AllocObject (klass);
2994 PUSHA (res);
2996 #ifdef DIRECT_THREADED
2997 pc[-2].insn = &&new_resolved;
2998 pc[-1].datum = klass;
2999 #endif /* DIRECT_THREADED */
3001 NEXT_INSN;
3003 #ifdef DIRECT_THREADED
3004 new_resolved:
3006 jclass klass = (jclass) AVAL ();
3007 jobject res = _Jv_AllocObject (klass);
3008 PUSHA (res);
3010 NEXT_INSN;
3011 #endif /* DIRECT_THREADED */
3013 insn_newarray:
3015 int atype = GET1U ();
3016 int size = POPI();
3017 jobject result = _Jv_NewArray (atype, size);
3018 PUSHA (result);
3020 NEXT_INSN;
3022 insn_anewarray:
3024 int index = GET2U ();
3025 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3026 index)).clazz;
3027 int size = POPI();
3028 jobject result = _Jv_NewObjectArray (size, klass, 0);
3029 PUSHA (result);
3031 #ifdef DIRECT_THREADED
3032 pc[-2].insn = &&anewarray_resolved;
3033 pc[-1].datum = klass;
3034 #endif /* DIRECT_THREADED */
3036 NEXT_INSN;
3038 #ifdef DIRECT_THREADED
3039 anewarray_resolved:
3041 jclass klass = (jclass) AVAL ();
3042 int size = POPI ();
3043 jobject result = _Jv_NewObjectArray (size, klass, 0);
3044 PUSHA (result);
3046 NEXT_INSN;
3047 #endif /* DIRECT_THREADED */
3049 insn_arraylength:
3051 __JArray *arr = (__JArray*)POPA();
3052 NULLARRAYCHECK (arr);
3053 PUSHI (arr->length);
3055 NEXT_INSN;
3057 insn_athrow:
3059 jobject value = POPA();
3060 throw static_cast<jthrowable>(value);
3062 NEXT_INSN;
3064 insn_checkcast:
3066 SAVE_PC();
3067 jobject value = POPA();
3068 jint index = GET2U ();
3069 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3070 index)).clazz;
3072 value = (jobject) _Jv_CheckCast (to, value);
3074 PUSHA (value);
3076 #ifdef DIRECT_THREADED
3077 pc[-2].insn = &&checkcast_resolved;
3078 pc[-1].datum = to;
3079 #endif /* DIRECT_THREADED */
3081 NEXT_INSN;
3083 #ifdef DIRECT_THREADED
3084 checkcast_resolved:
3086 SAVE_PC();
3087 jobject value = POPA ();
3088 jclass to = (jclass) AVAL ();
3089 value = (jobject) _Jv_CheckCast (to, value);
3090 PUSHA (value);
3092 NEXT_INSN;
3093 #endif /* DIRECT_THREADED */
3095 insn_instanceof:
3097 SAVE_PC();
3098 jobject value = POPA();
3099 jint index = GET2U ();
3100 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3101 index)).clazz;
3102 PUSHI (to->isInstance (value));
3104 #ifdef DIRECT_THREADED
3105 pc[-2].insn = &&instanceof_resolved;
3106 pc[-1].datum = to;
3107 #endif /* DIRECT_THREADED */
3109 NEXT_INSN;
3111 #ifdef DIRECT_THREADED
3112 instanceof_resolved:
3114 jobject value = POPA ();
3115 jclass to = (jclass) AVAL ();
3116 PUSHI (to->isInstance (value));
3118 NEXT_INSN;
3119 #endif /* DIRECT_THREADED */
3121 insn_monitorenter:
3123 jobject value = POPA();
3124 NULLCHECK(value);
3125 _Jv_MonitorEnter (value);
3127 NEXT_INSN;
3129 insn_monitorexit:
3131 jobject value = POPA();
3132 NULLCHECK(value);
3133 _Jv_MonitorExit (value);
3135 NEXT_INSN;
3137 insn_ifnull:
3139 jobject val = POPA();
3140 if (val == NULL)
3141 TAKE_GOTO;
3142 else
3143 SKIP_GOTO;
3145 NEXT_INSN;
3147 insn_ifnonnull:
3149 jobject val = POPA();
3150 if (val != NULL)
3151 TAKE_GOTO;
3152 else
3153 SKIP_GOTO;
3155 NEXT_INSN;
3157 insn_multianewarray:
3159 int kind_index = GET2U ();
3160 int dim = GET1U ();
3162 jclass type
3163 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3164 kind_index)).clazz;
3165 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3167 for (int i = dim - 1; i >= 0; i--)
3169 sizes[i] = POPI ();
3172 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3174 PUSHA (res);
3176 NEXT_INSN;
3178 #ifndef DIRECT_THREADED
3179 insn_wide:
3181 jint the_mod_op = get1u (pc++);
3182 jint wide = get2u (pc); pc += 2;
3184 switch (the_mod_op)
3186 case op_istore:
3187 STOREI (wide);
3188 NEXT_INSN;
3190 case op_fstore:
3191 STOREF (wide);
3192 NEXT_INSN;
3194 case op_astore:
3195 STOREA (wide);
3196 NEXT_INSN;
3198 case op_lload:
3199 LOADL (wide);
3200 NEXT_INSN;
3202 case op_dload:
3203 LOADD (wide);
3204 NEXT_INSN;
3206 case op_iload:
3207 LOADI (wide);
3208 NEXT_INSN;
3210 case op_fload:
3211 LOADF (wide);
3212 NEXT_INSN;
3214 case op_aload:
3215 LOADA (wide);
3216 NEXT_INSN;
3218 case op_lstore:
3219 STOREL (wide);
3220 NEXT_INSN;
3222 case op_dstore:
3223 STORED (wide);
3224 NEXT_INSN;
3226 case op_ret:
3227 pc = (unsigned char*) PEEKA (wide);
3228 NEXT_INSN;
3230 case op_iinc:
3232 jint amount = get2s (pc); pc += 2;
3233 jint value = PEEKI (wide);
3234 POKEI (wide, value+amount);
3236 NEXT_INSN;
3238 default:
3239 throw_internal_error ("illegal bytecode modified by wide");
3243 #endif /* DIRECT_THREADED */
3245 catch (java::lang::Throwable *ex)
3247 #ifdef DIRECT_THREADED
3248 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3249 #else
3250 int logical_pc = pc - 1 - bytecode ();
3251 #endif
3252 _Jv_InterpException *exc = meth->exceptions ();
3253 jclass exc_class = ex->getClass ();
3255 for (int i = 0; i < meth->exc_count; i++)
3257 if (PCVAL (exc[i].start_pc) <= logical_pc
3258 && logical_pc < PCVAL (exc[i].end_pc))
3260 #ifdef DIRECT_THREADED
3261 jclass handler = (jclass) exc[i].handler_type.p;
3262 #else
3263 jclass handler = NULL;
3264 if (exc[i].handler_type.i != 0)
3265 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3266 exc[i].handler_type.i)).clazz;
3267 #endif /* DIRECT_THREADED */
3269 if (handler == NULL || handler->isAssignableFrom (exc_class))
3271 #ifdef DIRECT_THREADED
3272 pc = (insn_slot *) exc[i].handler_pc.p;
3273 #else
3274 pc = bytecode () + exc[i].handler_pc.i;
3275 #endif /* DIRECT_THREADED */
3276 sp = stack;
3277 sp++->o = ex; // Push exception.
3278 NEXT_INSN;
3283 // No handler, so re-throw.
3284 throw ex;
3288 static void
3289 throw_internal_error (const char *msg)
3291 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3294 static void
3295 throw_incompatible_class_change_error (jstring msg)
3297 throw new java::lang::IncompatibleClassChangeError (msg);
3300 static void
3301 throw_null_pointer_exception ()
3303 throw new java::lang::NullPointerException;
3306 /* Look up source code line number for given bytecode (or direct threaded
3307 interpreter) PC. */
3309 _Jv_InterpMethod::get_source_line(pc_t mpc)
3311 int line = line_table_len > 0 ? line_table[0].line : -1;
3312 for (int i = 1; i < line_table_len; i++)
3313 if (line_table[i].pc > mpc)
3314 break;
3315 else
3316 line = line_table[i].line;
3318 return line;
3321 /** Do static initialization for fields with a constant initializer */
3322 void
3323 _Jv_InitField (jobject obj, jclass klass, int index)
3325 using namespace java::lang::reflect;
3327 if (obj != 0 && klass == 0)
3328 klass = obj->getClass ();
3330 if (!_Jv_IsInterpretedClass (klass))
3331 return;
3333 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3335 _Jv_Field * field = (&klass->fields[0]) + index;
3337 if (index > klass->field_count)
3338 throw_internal_error ("field out of range");
3340 int init = iclass->field_initializers[index];
3341 if (init == 0)
3342 return;
3344 _Jv_Constants *pool = &klass->constants;
3345 int tag = pool->tags[init];
3347 if (! field->isResolved ())
3348 throw_internal_error ("initializing unresolved field");
3350 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3351 throw_internal_error ("initializing non-static field with no object");
3353 void *addr = 0;
3355 if ((field->flags & Modifier::STATIC) != 0)
3356 addr = (void*) field->u.addr;
3357 else
3358 addr = (void*) (((char*)obj) + field->u.boffset);
3360 switch (tag)
3362 case JV_CONSTANT_String:
3364 jstring str;
3365 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3366 pool->data[init].string = str;
3367 pool->tags[init] = JV_CONSTANT_ResolvedString;
3369 /* fall through */
3371 case JV_CONSTANT_ResolvedString:
3372 if (! (field->type == &java::lang::String::class$
3373 || field->type == &java::lang::Class::class$))
3374 throw_class_format_error ("string initialiser to non-string field");
3376 *(jstring*)addr = pool->data[init].string;
3377 break;
3379 case JV_CONSTANT_Integer:
3381 int value = pool->data[init].i;
3383 if (field->type == JvPrimClass (boolean))
3384 *(jboolean*)addr = (jboolean)value;
3386 else if (field->type == JvPrimClass (byte))
3387 *(jbyte*)addr = (jbyte)value;
3389 else if (field->type == JvPrimClass (char))
3390 *(jchar*)addr = (jchar)value;
3392 else if (field->type == JvPrimClass (short))
3393 *(jshort*)addr = (jshort)value;
3395 else if (field->type == JvPrimClass (int))
3396 *(jint*)addr = (jint)value;
3398 else
3399 throw_class_format_error ("erroneous field initializer");
3401 break;
3403 case JV_CONSTANT_Long:
3404 if (field->type != JvPrimClass (long))
3405 throw_class_format_error ("erroneous field initializer");
3407 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3408 break;
3410 case JV_CONSTANT_Float:
3411 if (field->type != JvPrimClass (float))
3412 throw_class_format_error ("erroneous field initializer");
3414 *(jfloat*)addr = pool->data[init].f;
3415 break;
3417 case JV_CONSTANT_Double:
3418 if (field->type != JvPrimClass (double))
3419 throw_class_format_error ("erroneous field initializer");
3421 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3422 break;
3424 default:
3425 throw_class_format_error ("erroneous field initializer");
3429 inline static unsigned char*
3430 skip_one_type (unsigned char* ptr)
3432 int ch = *ptr++;
3434 while (ch == '[')
3436 ch = *ptr++;
3439 if (ch == 'L')
3441 do { ch = *ptr++; } while (ch != ';');
3444 return ptr;
3447 static ffi_type*
3448 get_ffi_type_from_signature (unsigned char* ptr)
3450 switch (*ptr)
3452 case 'L':
3453 case '[':
3454 return &ffi_type_pointer;
3455 break;
3457 case 'Z':
3458 // On some platforms a bool is a byte, on others an int.
3459 if (sizeof (jboolean) == sizeof (jbyte))
3460 return &ffi_type_sint8;
3461 else
3463 JvAssert (sizeof (jbyte) == sizeof (jint));
3464 return &ffi_type_sint32;
3466 break;
3468 case 'B':
3469 return &ffi_type_sint8;
3470 break;
3472 case 'C':
3473 return &ffi_type_uint16;
3474 break;
3476 case 'S':
3477 return &ffi_type_sint16;
3478 break;
3480 case 'I':
3481 return &ffi_type_sint32;
3482 break;
3484 case 'J':
3485 return &ffi_type_sint64;
3486 break;
3488 case 'F':
3489 return &ffi_type_float;
3490 break;
3492 case 'D':
3493 return &ffi_type_double;
3494 break;
3496 case 'V':
3497 return &ffi_type_void;
3498 break;
3501 throw_internal_error ("unknown type in signature");
3504 /* this function yields the number of actual arguments, that is, if the
3505 * function is non-static, then one is added to the number of elements
3506 * found in the signature */
3508 int
3509 _Jv_count_arguments (_Jv_Utf8Const *signature,
3510 jboolean staticp)
3512 unsigned char *ptr = (unsigned char*) signature->chars();
3513 int arg_count = staticp ? 0 : 1;
3515 /* first, count number of arguments */
3517 // skip '('
3518 ptr++;
3520 // count args
3521 while (*ptr != ')')
3523 ptr = skip_one_type (ptr);
3524 arg_count += 1;
3527 return arg_count;
3530 /* This beast will build a cif, given the signature. Memory for
3531 * the cif itself and for the argument types must be allocated by the
3532 * caller.
3535 static int
3536 init_cif (_Jv_Utf8Const* signature,
3537 int arg_count,
3538 jboolean staticp,
3539 ffi_cif *cif,
3540 ffi_type **arg_types,
3541 ffi_type **rtype_p)
3543 unsigned char *ptr = (unsigned char*) signature->chars();
3545 int arg_index = 0; // arg number
3546 int item_count = 0; // stack-item count
3548 // setup receiver
3549 if (!staticp)
3551 arg_types[arg_index++] = &ffi_type_pointer;
3552 item_count += 1;
3555 // skip '('
3556 ptr++;
3558 // assign arg types
3559 while (*ptr != ')')
3561 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3563 if (*ptr == 'J' || *ptr == 'D')
3564 item_count += 2;
3565 else
3566 item_count += 1;
3568 ptr = skip_one_type (ptr);
3571 // skip ')'
3572 ptr++;
3573 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3575 ptr = skip_one_type (ptr);
3576 if (ptr != (unsigned char*)signature->chars() + signature->len())
3577 throw_internal_error ("did not find end of signature");
3579 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3580 arg_count, rtype, arg_types) != FFI_OK)
3581 throw_internal_error ("ffi_prep_cif failed");
3583 if (rtype_p != NULL)
3584 *rtype_p = rtype;
3586 return item_count;
3589 #if FFI_NATIVE_RAW_API
3590 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3591 # define FFI_RAW_SIZE ffi_raw_size
3592 #else
3593 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3594 # define FFI_RAW_SIZE ffi_java_raw_size
3595 #endif
3597 /* we put this one here, and not in interpret.cc because it
3598 * calls the utility routines _Jv_count_arguments
3599 * which are static to this module. The following struct defines the
3600 * layout we use for the stubs, it's only used in the ncode method. */
3602 typedef struct {
3603 ffi_raw_closure closure;
3604 ffi_cif cif;
3605 ffi_type *arg_types[0];
3606 } ncode_closure;
3608 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3610 void *
3611 _Jv_InterpMethod::ncode ()
3613 using namespace java::lang::reflect;
3615 if (self->ncode != 0)
3616 return self->ncode;
3618 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3619 int arg_count = _Jv_count_arguments (self->signature, staticp);
3621 ncode_closure *closure =
3622 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3623 + arg_count * sizeof (ffi_type*));
3625 init_cif (self->signature,
3626 arg_count,
3627 staticp,
3628 &closure->cif,
3629 &closure->arg_types[0],
3630 NULL);
3632 ffi_closure_fun fun;
3634 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3636 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3638 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3640 if (staticp)
3641 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3642 else
3643 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3645 else
3647 if (staticp)
3648 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3649 else
3650 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3653 FFI_PREP_RAW_CLOSURE (&closure->closure,
3654 &closure->cif,
3655 fun,
3656 (void*)this);
3658 self->ncode = (void*)closure;
3659 return self->ncode;
3662 #ifdef DIRECT_THREADED
3663 /* Find the index of the given insn in the array of insn slots
3664 for this method. Returns -1 if not found. */
3665 jlong
3666 _Jv_InterpMethod::insn_index (pc_t pc)
3668 jlong left = 0;
3669 jlong right = number_insn_slots;
3670 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3672 while (right >= 0)
3674 jlong mid = (left + right) / 2;
3675 if (&slots[mid] == pc)
3676 return mid;
3678 if (pc < &slots[mid])
3679 right = mid - 1;
3680 else
3681 left = mid + 1;
3684 return -1;
3686 #endif // DIRECT_THREADED
3688 void
3689 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
3690 jintArray& line_numbers,
3691 jlongArray& code_indices)
3693 #ifdef DIRECT_THREADED
3694 /* For the DIRECT_THREADED case, if the method has not yet been
3695 * compiled, the linetable will change to insn slots instead of
3696 * bytecode PCs. It is probably easiest, in this case, to simply
3697 * compile the method and guarantee that we are using insn
3698 * slots.
3700 _Jv_CompileMethod (this);
3702 if (line_table_len > 0)
3704 start = 0;
3705 end = number_insn_slots;
3706 line_numbers = JvNewIntArray (line_table_len);
3707 code_indices = JvNewLongArray (line_table_len);
3709 jint* lines = elements (line_numbers);
3710 jlong* indices = elements (code_indices);
3711 for (int i = 0; i < line_table_len; ++i)
3713 lines[i] = line_table[i].line;
3714 indices[i] = insn_index (line_table[i].pc);
3717 #else // !DIRECT_THREADED
3718 if (line_table_len > 0)
3720 start = 0;
3721 end = code_length;
3722 line_numbers = JvNewIntArray (line_table_len);
3723 code_indices = JvNewLongArray (line_table_len);
3725 jint* lines = elements (line_numbers);
3726 jlong* indices = elements (code_indices);
3727 for (int i = 0; i < line_table_len; ++i)
3729 lines[i] = line_table[i].line;
3730 indices[i] = (jlong) line_table[i].bytecode_pc;
3733 #endif // !DIRECT_THREADED
3736 void *
3737 _Jv_JNIMethod::ncode ()
3739 using namespace java::lang::reflect;
3741 if (self->ncode != 0)
3742 return self->ncode;
3744 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3745 int arg_count = _Jv_count_arguments (self->signature, staticp);
3747 ncode_closure *closure =
3748 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3749 + arg_count * sizeof (ffi_type*));
3751 ffi_type *rtype;
3752 init_cif (self->signature,
3753 arg_count,
3754 staticp,
3755 &closure->cif,
3756 &closure->arg_types[0],
3757 &rtype);
3759 ffi_closure_fun fun;
3761 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3763 // Initialize the argument types and CIF that represent the actual
3764 // underlying JNI function.
3765 int extra_args = 1;
3766 if ((self->accflags & Modifier::STATIC))
3767 ++extra_args;
3768 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3769 * sizeof (ffi_type *));
3770 int offset = 0;
3771 jni_arg_types[offset++] = &ffi_type_pointer;
3772 if ((self->accflags & Modifier::STATIC))
3773 jni_arg_types[offset++] = &ffi_type_pointer;
3774 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3775 arg_count * sizeof (ffi_type *));
3777 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3778 extra_args + arg_count, rtype,
3779 jni_arg_types) != FFI_OK)
3780 throw_internal_error ("ffi_prep_cif failed for JNI function");
3782 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3784 // FIXME: for now we assume that all native methods for
3785 // interpreted code use JNI.
3786 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3788 FFI_PREP_RAW_CLOSURE (&closure->closure,
3789 &closure->cif,
3790 fun,
3791 (void*) this);
3793 self->ncode = (void *) closure;
3794 return self->ncode;
3797 static void
3798 throw_class_format_error (jstring msg)
3800 throw (msg
3801 ? new java::lang::ClassFormatError (msg)
3802 : new java::lang::ClassFormatError);
3805 static void
3806 throw_class_format_error (const char *msg)
3808 throw_class_format_error (JvNewStringLatin1 (msg));
3813 void
3814 _Jv_InterpreterEngine::do_verify (jclass klass)
3816 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3817 for (int i = 0; i < klass->method_count; i++)
3819 using namespace java::lang::reflect;
3820 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3821 _Jv_ushort accflags = klass->methods[i].accflags;
3822 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3824 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3825 _Jv_VerifyMethod (im);
3830 void
3831 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3833 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3834 for (int i = 0; i < klass->method_count; i++)
3836 // Just skip abstract methods. This is particularly important
3837 // because we don't resize the interpreted_methods array when
3838 // miranda methods are added to it.
3839 if ((klass->methods[i].accflags
3840 & java::lang::reflect::Modifier::ABSTRACT)
3841 != 0)
3842 continue;
3844 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3846 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3847 != 0)
3849 // You might think we could use a virtual `ncode' method in
3850 // the _Jv_MethodBase and unify the native and non-native
3851 // cases. Well, we can't, because we don't allocate these
3852 // objects using `new', and thus they don't get a vtable.
3853 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3854 klass->methods[i].ncode = jnim->ncode ();
3856 else if (imeth != 0) // it could be abstract
3858 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3859 klass->methods[i].ncode = im->ncode ();
3864 void
3865 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3866 int pointer_size,
3867 int other_size)
3869 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3871 // Splitting the allocations here lets us scan reference fields and
3872 // avoid scanning non-reference fields. How reference fields are
3873 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
3874 // means that this memory will be scanned conservatively (same
3875 // difference, since we know all the contents here are pointers).
3876 // Then we put pointers into this memory into the 'fields'
3877 // structure. Most of these are interior pointers, which is ok (but
3878 // even so the pointer to the first reference field will be used and
3879 // that is not an interior pointer). The 'fields' array is also
3880 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
3881 // be scanned. A pointer to this array is held by Class and thus
3882 // seen by the collector.
3883 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
3884 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
3886 for (int i = 0; i < klass->field_count; i++)
3888 _Jv_Field *field = &klass->fields[i];
3890 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3891 continue;
3893 char *base = field->isRef() ? reference_fields : non_reference_fields;
3894 field->u.addr = base + field->u.boffset;
3896 if (iclass->field_initializers[i] != 0)
3898 _Jv_Linker::resolve_field (field, klass->loader);
3899 _Jv_InitField (0, klass, i);
3903 // Now we don't need the field_initializers anymore, so let the
3904 // collector get rid of it.
3905 iclass->field_initializers = 0;
3908 _Jv_ResolvedMethod *
3909 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3910 jboolean staticp)
3912 int arg_count = _Jv_count_arguments (method->signature, staticp);
3914 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3915 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3916 + arg_count*sizeof (ffi_type*));
3918 result->stack_item_count
3919 = init_cif (method->signature,
3920 arg_count,
3921 staticp,
3922 &result->cif,
3923 &result->arg_types[0],
3924 NULL);
3926 result->method = method;
3927 result->klass = klass;
3929 return result;
3932 void
3933 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3935 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3936 for (int i = 0; i < klass->method_count; i++)
3938 // Just skip abstract methods. This is particularly important
3939 // because we don't resize the interpreted_methods array when
3940 // miranda methods are added to it.
3941 if ((klass->methods[i].accflags
3942 & java::lang::reflect::Modifier::ABSTRACT)
3943 != 0)
3944 continue;
3945 // Miranda method additions mean that the `methods' array moves.
3946 // We cache a pointer into this array, so we have to update.
3947 iclass->interpreted_methods[i]->self = &klass->methods[i];
3951 #ifdef DIRECT_THREADED
3952 void
3953 _Jv_CompileMethod (_Jv_InterpMethod* method)
3955 if (method->prepared == NULL)
3956 _Jv_InterpMethod::run (NULL, NULL, method);
3958 #endif // DIRECT_THREADED
3960 #endif // INTERPRETER