Mark ChangeLog
[official-gcc.git] / libjava / interpret.cc
blobb2c667b95139ea6011dd1d8bfac7567fb28bdfc1
1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 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 // Define this to get the direct-threaded interpreter. If undefined,
17 // we revert to a basic bytecode interpreter. The former is faster
18 // but uses more memory.
19 #define DIRECT_THREADED
21 #pragma implementation "java-interp.h"
23 #include <jvm.h>
24 #include <java-cpool.h>
25 #include <java-interp.h>
26 #include <java/lang/System.h>
27 #include <java/lang/String.h>
28 #include <java/lang/Integer.h>
29 #include <java/lang/Long.h>
30 #include <java/lang/StringBuffer.h>
31 #include <java/lang/Class.h>
32 #include <java/lang/reflect/Modifier.h>
33 #include <java/lang/ClassCastException.h>
34 #include <java/lang/VirtualMachineError.h>
35 #include <java/lang/InternalError.h>
36 #include <java/lang/NullPointerException.h>
37 #include <java/lang/ArithmeticException.h>
38 #include <java/lang/IncompatibleClassChangeError.h>
39 #include <java/lang/InstantiationException.h>
40 #include <java/lang/Thread.h>
41 #include <java-insns.h>
42 #include <java-signal.h>
43 #include <java/lang/ClassFormatError.h>
44 #include <execution.h>
45 #include <java/lang/reflect/Modifier.h>
47 #ifdef INTERPRETER
49 // Execution engine for interpreted code.
50 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
52 #include <stdlib.h>
54 using namespace gcj;
56 static void throw_internal_error (char *msg)
57 __attribute__ ((__noreturn__));
58 static void throw_incompatible_class_change_error (jstring msg)
59 __attribute__ ((__noreturn__));
60 #ifndef HANDLE_SEGV
61 static void throw_null_pointer_exception ()
62 __attribute__ ((__noreturn__));
63 #endif
65 static void throw_class_format_error (jstring msg)
66 __attribute__ ((__noreturn__));
67 static void throw_class_format_error (char *msg)
68 __attribute__ ((__noreturn__));
70 #ifdef DIRECT_THREADED
71 // Lock to ensure that methods are not compiled concurrently.
72 // We could use a finer-grained lock here, however it is not safe to use
73 // the Class monitor as user code in another thread could hold it.
74 static _Jv_Mutex_t compile_mutex;
76 void
77 _Jv_InitInterpreter()
79 _Jv_MutexInit (&compile_mutex);
81 #else
82 void _Jv_InitInterpreter() {}
83 #endif
85 extern "C" double __ieee754_fmod (double,double);
87 // This represents a single slot in the "compiled" form of the
88 // bytecode.
89 union insn_slot
91 // Address of code.
92 void *insn;
93 // An integer value used by an instruction.
94 jint int_val;
95 // A pointer value used by an instruction.
96 void *datum;
99 // The type of the PC depends on whether we're doing direct threading
100 // or a more ordinary bytecode interpreter.
101 #ifdef DIRECT_THREADED
102 typedef insn_slot *pc_t;
103 #else
104 typedef unsigned char *pc_t;
105 #endif
107 static inline void dupx (_Jv_word *sp, int n, int x)
109 // first "slide" n+x elements n to the right
110 int top = n-1;
111 for (int i = 0; i < n+x; i++)
113 sp[(top-i)] = sp[(top-i)-n];
116 // next, copy the n top elements, n+x down
117 for (int i = 0; i < n; i++)
119 sp[top-(n+x)-i] = sp[top-i];
124 // Used to convert from floating types to integral types.
125 template<typename TO, typename FROM>
126 static inline TO
127 convert (FROM val, TO min, TO max)
129 TO ret;
130 if (val >= (FROM) max)
131 ret = max;
132 else if (val <= (FROM) min)
133 ret = min;
134 else if (val != val)
135 ret = 0;
136 else
137 ret = (TO) val;
138 return ret;
141 #define PUSHA(V) (sp++)->o = (V)
142 #define PUSHI(V) (sp++)->i = (V)
143 #define PUSHF(V) (sp++)->f = (V)
144 #if SIZEOF_VOID_P == 8
145 # define PUSHL(V) (sp->l = (V), sp += 2)
146 # define PUSHD(V) (sp->d = (V), sp += 2)
147 #else
148 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
149 (sp++)->ia[0] = w2.ia[0]; \
150 (sp++)->ia[0] = w2.ia[1]; } while (0)
151 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
152 (sp++)->ia[0] = w2.ia[0]; \
153 (sp++)->ia[0] = w2.ia[1]; } while (0)
154 #endif
156 #define POPA() ((--sp)->o)
157 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
158 #define POPF() ((jfloat) (--sp)->f)
159 #if SIZEOF_VOID_P == 8
160 # define POPL() (sp -= 2, (jlong) sp->l)
161 # define POPD() (sp -= 2, (jdouble) sp->d)
162 #else
163 # define POPL() ({ _Jv_word2 w2; \
164 w2.ia[1] = (--sp)->ia[0]; \
165 w2.ia[0] = (--sp)->ia[0]; w2.l; })
166 # define POPD() ({ _Jv_word2 w2; \
167 w2.ia[1] = (--sp)->ia[0]; \
168 w2.ia[0] = (--sp)->ia[0]; w2.d; })
169 #endif
171 #define LOADA(I) (sp++)->o = locals[I].o
172 #define LOADI(I) (sp++)->i = locals[I].i
173 #define LOADF(I) (sp++)->f = locals[I].f
174 #if SIZEOF_VOID_P == 8
175 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
176 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
177 #else
178 # define LOADL(I) do { jint __idx = (I); \
179 (sp++)->ia[0] = locals[__idx].ia[0]; \
180 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
181 } while (0)
182 # define LOADD(I) LOADL(I)
183 #endif
185 #define STOREA(I) locals[I].o = (--sp)->o
186 #define STOREI(I) locals[I].i = (--sp)->i
187 #define STOREF(I) locals[I].f = (--sp)->f
188 #if SIZEOF_VOID_P == 8
189 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
190 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
191 #else
192 # define STOREL(I) do { jint __idx = (I); \
193 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
194 locals[__idx].ia[0] = (--sp)->ia[0]; \
195 } while (0)
196 # define STORED(I) STOREL(I)
197 #endif
199 #define PEEKI(I) (locals+(I))->i
200 #define PEEKA(I) (locals+(I))->o
202 #define POKEI(I,V) ((locals+(I))->i = (V))
205 #define BINOPI(OP) { \
206 jint value2 = POPI(); \
207 jint value1 = POPI(); \
208 PUSHI(value1 OP value2); \
211 #define BINOPF(OP) { \
212 jfloat value2 = POPF(); \
213 jfloat value1 = POPF(); \
214 PUSHF(value1 OP value2); \
217 #define BINOPL(OP) { \
218 jlong value2 = POPL(); \
219 jlong value1 = POPL(); \
220 PUSHL(value1 OP value2); \
223 #define BINOPD(OP) { \
224 jdouble value2 = POPD(); \
225 jdouble value1 = POPD(); \
226 PUSHD(value1 OP value2); \
229 static inline jint get1s(unsigned char* loc) {
230 return *(signed char*)loc;
233 static inline jint get1u(unsigned char* loc) {
234 return *loc;
237 static inline jint get2s(unsigned char* loc) {
238 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
241 static inline jint get2u(unsigned char* loc) {
242 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
245 static jint get4(unsigned char* loc) {
246 return (((jint)(loc[0])) << 24)
247 | (((jint)(loc[1])) << 16)
248 | (((jint)(loc[2])) << 8)
249 | (((jint)(loc[3])) << 0);
253 #ifdef HANDLE_SEGV
254 #define NULLCHECK(X)
255 #define NULLARRAYCHECK(X)
256 #else
257 #define NULLCHECK(X) \
258 do { if ((X)==NULL) throw_null_pointer_exception (); } while (0)
259 #define NULLARRAYCHECK(X) \
260 do { if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
261 #endif
263 #define ARRAYBOUNDSCHECK(array, index) \
264 do \
266 if (((unsigned) index) >= (unsigned) (array->length)) \
267 _Jv_ThrowBadArrayIndex (index); \
269 while (0)
271 void
272 _Jv_InterpMethod::run_normal (ffi_cif *,
273 void* ret,
274 ffi_raw * args,
275 void* __this)
277 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
278 _this->run (ret, args);
281 void
282 _Jv_InterpMethod::run_synch_object (ffi_cif *,
283 void* ret,
284 ffi_raw * args,
285 void* __this)
287 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
289 jobject rcv = (jobject) args[0].ptr;
290 JvSynchronize mutex (rcv);
292 _this->run (ret, args);
295 void
296 _Jv_InterpMethod::run_class (ffi_cif *,
297 void* ret,
298 ffi_raw * args,
299 void* __this)
301 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
302 _Jv_InitClass (_this->defining_class);
303 _this->run (ret, args);
306 void
307 _Jv_InterpMethod::run_synch_class (ffi_cif *,
308 void* ret,
309 ffi_raw * args,
310 void* __this)
312 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
314 jclass sync = _this->defining_class;
315 _Jv_InitClass (sync);
316 JvSynchronize mutex (sync);
318 _this->run (ret, args);
321 #ifdef DIRECT_THREADED
322 // "Compile" a method by turning it from bytecode to direct-threaded
323 // code.
324 void
325 _Jv_InterpMethod::compile (const void * const *insn_targets)
327 insn_slot *insns = NULL;
328 int next = 0;
329 unsigned char *codestart = bytecode ();
330 unsigned char *end = codestart + code_length;
331 _Jv_word *pool_data = defining_class->constants.data;
333 #define SET_ONE(Field, Value) \
334 do \
336 if (first_pass) \
337 ++next; \
338 else \
339 insns[next++].Field = Value; \
341 while (0)
343 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
344 #define SET_INT(Value) SET_ONE (int_val, Value)
345 #define SET_DATUM(Value) SET_ONE (datum, Value)
347 // Map from bytecode PC to slot in INSNS.
348 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
349 for (int i = 0; i < code_length; ++i)
350 pc_mapping[i] = -1;
352 for (int i = 0; i < 2; ++i)
354 jboolean first_pass = i == 0;
356 if (! first_pass)
358 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
359 next = 0;
362 unsigned char *pc = codestart;
363 while (pc < end)
365 int base_pc_val = pc - codestart;
366 if (first_pass)
367 pc_mapping[base_pc_val] = next;
369 java_opcode opcode = (java_opcode) *pc++;
370 // Just elide NOPs.
371 if (opcode == op_nop)
372 continue;
373 SET_INSN (insn_targets[opcode]);
375 switch (opcode)
377 case op_nop:
378 case op_aconst_null:
379 case op_iconst_m1:
380 case op_iconst_0:
381 case op_iconst_1:
382 case op_iconst_2:
383 case op_iconst_3:
384 case op_iconst_4:
385 case op_iconst_5:
386 case op_lconst_0:
387 case op_lconst_1:
388 case op_fconst_0:
389 case op_fconst_1:
390 case op_fconst_2:
391 case op_dconst_0:
392 case op_dconst_1:
393 case op_iload_0:
394 case op_iload_1:
395 case op_iload_2:
396 case op_iload_3:
397 case op_lload_0:
398 case op_lload_1:
399 case op_lload_2:
400 case op_lload_3:
401 case op_fload_0:
402 case op_fload_1:
403 case op_fload_2:
404 case op_fload_3:
405 case op_dload_0:
406 case op_dload_1:
407 case op_dload_2:
408 case op_dload_3:
409 case op_aload_0:
410 case op_aload_1:
411 case op_aload_2:
412 case op_aload_3:
413 case op_iaload:
414 case op_laload:
415 case op_faload:
416 case op_daload:
417 case op_aaload:
418 case op_baload:
419 case op_caload:
420 case op_saload:
421 case op_istore_0:
422 case op_istore_1:
423 case op_istore_2:
424 case op_istore_3:
425 case op_lstore_0:
426 case op_lstore_1:
427 case op_lstore_2:
428 case op_lstore_3:
429 case op_fstore_0:
430 case op_fstore_1:
431 case op_fstore_2:
432 case op_fstore_3:
433 case op_dstore_0:
434 case op_dstore_1:
435 case op_dstore_2:
436 case op_dstore_3:
437 case op_astore_0:
438 case op_astore_1:
439 case op_astore_2:
440 case op_astore_3:
441 case op_iastore:
442 case op_lastore:
443 case op_fastore:
444 case op_dastore:
445 case op_aastore:
446 case op_bastore:
447 case op_castore:
448 case op_sastore:
449 case op_pop:
450 case op_pop2:
451 case op_dup:
452 case op_dup_x1:
453 case op_dup_x2:
454 case op_dup2:
455 case op_dup2_x1:
456 case op_dup2_x2:
457 case op_swap:
458 case op_iadd:
459 case op_isub:
460 case op_imul:
461 case op_idiv:
462 case op_irem:
463 case op_ishl:
464 case op_ishr:
465 case op_iushr:
466 case op_iand:
467 case op_ior:
468 case op_ixor:
469 case op_ladd:
470 case op_lsub:
471 case op_lmul:
472 case op_ldiv:
473 case op_lrem:
474 case op_lshl:
475 case op_lshr:
476 case op_lushr:
477 case op_land:
478 case op_lor:
479 case op_lxor:
480 case op_fadd:
481 case op_fsub:
482 case op_fmul:
483 case op_fdiv:
484 case op_frem:
485 case op_dadd:
486 case op_dsub:
487 case op_dmul:
488 case op_ddiv:
489 case op_drem:
490 case op_ineg:
491 case op_i2b:
492 case op_i2c:
493 case op_i2s:
494 case op_lneg:
495 case op_fneg:
496 case op_dneg:
497 case op_i2l:
498 case op_i2f:
499 case op_i2d:
500 case op_l2i:
501 case op_l2f:
502 case op_l2d:
503 case op_f2i:
504 case op_f2l:
505 case op_f2d:
506 case op_d2i:
507 case op_d2l:
508 case op_d2f:
509 case op_lcmp:
510 case op_fcmpl:
511 case op_fcmpg:
512 case op_dcmpl:
513 case op_dcmpg:
514 case op_monitorenter:
515 case op_monitorexit:
516 case op_ireturn:
517 case op_lreturn:
518 case op_freturn:
519 case op_dreturn:
520 case op_areturn:
521 case op_return:
522 case op_athrow:
523 case op_arraylength:
524 // No argument, nothing else to do.
525 break;
527 case op_bipush:
528 SET_INT (get1s (pc));
529 ++pc;
530 break;
532 case op_ldc:
534 int index = get1u (pc);
535 ++pc;
536 SET_DATUM (pool_data[index].o);
538 break;
540 case op_ret:
541 case op_iload:
542 case op_lload:
543 case op_fload:
544 case op_dload:
545 case op_aload:
546 case op_istore:
547 case op_lstore:
548 case op_fstore:
549 case op_dstore:
550 case op_astore:
551 case op_newarray:
552 SET_INT (get1u (pc));
553 ++pc;
554 break;
556 case op_iinc:
557 SET_INT (get1u (pc));
558 SET_INT (get1s (pc + 1));
559 pc += 2;
560 break;
562 case op_ldc_w:
564 int index = get2u (pc);
565 pc += 2;
566 SET_DATUM (pool_data[index].o);
568 break;
570 case op_ldc2_w:
572 int index = get2u (pc);
573 pc += 2;
574 SET_DATUM (&pool_data[index]);
576 break;
578 case op_sipush:
579 SET_INT (get2s (pc));
580 pc += 2;
581 break;
583 case op_new:
584 case op_getstatic:
585 case op_getfield:
586 case op_putfield:
587 case op_putstatic:
588 case op_anewarray:
589 case op_instanceof:
590 case op_checkcast:
591 case op_invokespecial:
592 case op_invokestatic:
593 case op_invokevirtual:
594 SET_INT (get2u (pc));
595 pc += 2;
596 break;
598 case op_multianewarray:
599 SET_INT (get2u (pc));
600 SET_INT (get1u (pc + 2));
601 pc += 3;
602 break;
604 case op_jsr:
605 case op_ifeq:
606 case op_ifne:
607 case op_iflt:
608 case op_ifge:
609 case op_ifgt:
610 case op_ifle:
611 case op_if_icmpeq:
612 case op_if_icmpne:
613 case op_if_icmplt:
614 case op_if_icmpge:
615 case op_if_icmpgt:
616 case op_if_icmple:
617 case op_if_acmpeq:
618 case op_if_acmpne:
619 case op_ifnull:
620 case op_ifnonnull:
621 case op_goto:
623 int offset = get2s (pc);
624 pc += 2;
626 int new_pc = base_pc_val + offset;
628 bool orig_was_goto = opcode == op_goto;
630 // Thread jumps. We limit the loop count; this lets
631 // us avoid infinite loops if the bytecode contains
632 // such. `10' is arbitrary.
633 int count = 10;
634 while (codestart[new_pc] == op_goto && count-- > 0)
635 new_pc += get2s (&codestart[new_pc + 1]);
637 // If the jump takes us to a `return' instruction and
638 // the original branch was an unconditional goto, then
639 // we hoist the return.
640 opcode = (java_opcode) codestart[new_pc];
641 if (orig_was_goto
642 && (opcode == op_ireturn || opcode == op_lreturn
643 || opcode == op_freturn || opcode == op_dreturn
644 || opcode == op_areturn || opcode == op_return))
646 --next;
647 SET_INSN (insn_targets[opcode]);
649 else
650 SET_DATUM (&insns[pc_mapping[new_pc]]);
652 break;
654 case op_tableswitch:
656 while ((pc - codestart) % 4 != 0)
657 ++pc;
659 jint def = get4 (pc);
660 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
661 pc += 4;
663 int low = get4 (pc);
664 SET_INT (low);
665 pc += 4;
666 int high = get4 (pc);
667 SET_INT (high);
668 pc += 4;
670 for (int i = low; i <= high; ++i)
672 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
673 pc += 4;
676 break;
678 case op_lookupswitch:
680 while ((pc - codestart) % 4 != 0)
681 ++pc;
683 jint def = get4 (pc);
684 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
685 pc += 4;
687 jint npairs = get4 (pc);
688 pc += 4;
689 SET_INT (npairs);
691 while (npairs-- > 0)
693 jint match = get4 (pc);
694 jint offset = get4 (pc + 4);
695 SET_INT (match);
696 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
697 pc += 8;
700 break;
702 case op_invokeinterface:
704 jint index = get2u (pc);
705 pc += 2;
706 // We ignore the next two bytes.
707 pc += 2;
708 SET_INT (index);
710 break;
712 case op_wide:
714 opcode = (java_opcode) get1u (pc);
715 pc += 1;
716 jint val = get2u (pc);
717 pc += 2;
719 // We implement narrow and wide instructions using the
720 // same code in the interpreter. So we rewrite the
721 // instruction slot here.
722 if (! first_pass)
723 insns[next - 1].insn = (void *) insn_targets[opcode];
724 SET_INT (val);
726 if (opcode == op_iinc)
728 SET_INT (get2s (pc));
729 pc += 2;
732 break;
734 case op_jsr_w:
735 case op_goto_w:
737 jint offset = get4 (pc);
738 pc += 4;
739 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
741 break;
743 // Some "can't happen" cases that we include for
744 // error-checking purposes.
745 case op_putfield_1:
746 case op_putfield_2:
747 case op_putfield_4:
748 case op_putfield_8:
749 case op_putfield_a:
750 case op_putstatic_1:
751 case op_putstatic_2:
752 case op_putstatic_4:
753 case op_putstatic_8:
754 case op_putstatic_a:
755 case op_getfield_1:
756 case op_getfield_2s:
757 case op_getfield_2u:
758 case op_getfield_4:
759 case op_getfield_8:
760 case op_getfield_a:
761 case op_getstatic_1:
762 case op_getstatic_2s:
763 case op_getstatic_2u:
764 case op_getstatic_4:
765 case op_getstatic_8:
766 case op_getstatic_a:
767 default:
768 // Fail somehow.
769 break;
774 // Now update exceptions.
775 _Jv_InterpException *exc = exceptions ();
776 for (int i = 0; i < exc_count; ++i)
778 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
779 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
780 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
781 jclass handler
782 = (_Jv_Linker::resolve_pool_entry (defining_class,
783 exc[i].handler_type.i)).clazz;
784 exc[i].handler_type.p = handler;
787 prepared = insns;
789 #endif /* DIRECT_THREADED */
791 // These exist so that the stack-tracing code can find the boundaries
792 // of the interpreter.
793 void *_Jv_StartOfInterpreter;
794 void *_Jv_EndOfInterpreter;
795 extern "C" void *_Unwind_FindEnclosingFunction (void *pc);
797 void
798 _Jv_InterpMethod::run (void *retp, ffi_raw *args)
800 using namespace java::lang::reflect;
802 // Record the address of the start of this member function in
803 // _Jv_StartOfInterpreter. Such a write to a global variable
804 // without acquiring a lock is correct iff reads and writes of words
805 // in memory are atomic, but Java requires that anyway.
806 foo:
807 if (_Jv_StartOfInterpreter == NULL)
808 _Jv_StartOfInterpreter = _Unwind_FindEnclosingFunction (&&foo);
810 // FRAME_DESC registers this particular invocation as the top-most
811 // interpreter frame. This lets the stack tracing code (for
812 // Throwable) print information about the method being interpreted
813 // rather than about the interpreter itself. FRAME_DESC has a
814 // destructor so it cleans up automatically when the interpreter
815 // returns.
816 java::lang::Thread *thread = java::lang::Thread::currentThread();
817 _Jv_MethodChain frame_desc (this,
818 (_Jv_MethodChain **) &thread->interp_frame);
820 _Jv_word stack[max_stack];
821 _Jv_word *sp = stack;
823 _Jv_word locals[max_locals];
825 /* Go straight at it! the ffi raw format matches the internal
826 stack representation exactly. At least, that's the idea.
828 memcpy ((void*) locals, (void*) args, args_raw_size);
830 _Jv_word *pool_data = defining_class->constants.data;
832 /* These three are temporaries for common code used by several
833 instructions. */
834 void (*fun)();
835 _Jv_ResolvedMethod* rmeth;
836 int tmpval;
838 #define INSN_LABEL(op) &&insn_##op
840 static const void *const insn_target[] =
842 INSN_LABEL(nop),
843 INSN_LABEL(aconst_null),
844 INSN_LABEL(iconst_m1),
845 INSN_LABEL(iconst_0),
846 INSN_LABEL(iconst_1),
847 INSN_LABEL(iconst_2),
848 INSN_LABEL(iconst_3),
849 INSN_LABEL(iconst_4),
850 INSN_LABEL(iconst_5),
851 INSN_LABEL(lconst_0),
852 INSN_LABEL(lconst_1),
853 INSN_LABEL(fconst_0),
854 INSN_LABEL(fconst_1),
855 INSN_LABEL(fconst_2),
856 INSN_LABEL(dconst_0),
857 INSN_LABEL(dconst_1),
858 INSN_LABEL(bipush),
859 INSN_LABEL(sipush),
860 INSN_LABEL(ldc),
861 INSN_LABEL(ldc_w),
862 INSN_LABEL(ldc2_w),
863 INSN_LABEL(iload),
864 INSN_LABEL(lload),
865 INSN_LABEL(fload),
866 INSN_LABEL(dload),
867 INSN_LABEL(aload),
868 INSN_LABEL(iload_0),
869 INSN_LABEL(iload_1),
870 INSN_LABEL(iload_2),
871 INSN_LABEL(iload_3),
872 INSN_LABEL(lload_0),
873 INSN_LABEL(lload_1),
874 INSN_LABEL(lload_2),
875 INSN_LABEL(lload_3),
876 INSN_LABEL(fload_0),
877 INSN_LABEL(fload_1),
878 INSN_LABEL(fload_2),
879 INSN_LABEL(fload_3),
880 INSN_LABEL(dload_0),
881 INSN_LABEL(dload_1),
882 INSN_LABEL(dload_2),
883 INSN_LABEL(dload_3),
884 INSN_LABEL(aload_0),
885 INSN_LABEL(aload_1),
886 INSN_LABEL(aload_2),
887 INSN_LABEL(aload_3),
888 INSN_LABEL(iaload),
889 INSN_LABEL(laload),
890 INSN_LABEL(faload),
891 INSN_LABEL(daload),
892 INSN_LABEL(aaload),
893 INSN_LABEL(baload),
894 INSN_LABEL(caload),
895 INSN_LABEL(saload),
896 INSN_LABEL(istore),
897 INSN_LABEL(lstore),
898 INSN_LABEL(fstore),
899 INSN_LABEL(dstore),
900 INSN_LABEL(astore),
901 INSN_LABEL(istore_0),
902 INSN_LABEL(istore_1),
903 INSN_LABEL(istore_2),
904 INSN_LABEL(istore_3),
905 INSN_LABEL(lstore_0),
906 INSN_LABEL(lstore_1),
907 INSN_LABEL(lstore_2),
908 INSN_LABEL(lstore_3),
909 INSN_LABEL(fstore_0),
910 INSN_LABEL(fstore_1),
911 INSN_LABEL(fstore_2),
912 INSN_LABEL(fstore_3),
913 INSN_LABEL(dstore_0),
914 INSN_LABEL(dstore_1),
915 INSN_LABEL(dstore_2),
916 INSN_LABEL(dstore_3),
917 INSN_LABEL(astore_0),
918 INSN_LABEL(astore_1),
919 INSN_LABEL(astore_2),
920 INSN_LABEL(astore_3),
921 INSN_LABEL(iastore),
922 INSN_LABEL(lastore),
923 INSN_LABEL(fastore),
924 INSN_LABEL(dastore),
925 INSN_LABEL(aastore),
926 INSN_LABEL(bastore),
927 INSN_LABEL(castore),
928 INSN_LABEL(sastore),
929 INSN_LABEL(pop),
930 INSN_LABEL(pop2),
931 INSN_LABEL(dup),
932 INSN_LABEL(dup_x1),
933 INSN_LABEL(dup_x2),
934 INSN_LABEL(dup2),
935 INSN_LABEL(dup2_x1),
936 INSN_LABEL(dup2_x2),
937 INSN_LABEL(swap),
938 INSN_LABEL(iadd),
939 INSN_LABEL(ladd),
940 INSN_LABEL(fadd),
941 INSN_LABEL(dadd),
942 INSN_LABEL(isub),
943 INSN_LABEL(lsub),
944 INSN_LABEL(fsub),
945 INSN_LABEL(dsub),
946 INSN_LABEL(imul),
947 INSN_LABEL(lmul),
948 INSN_LABEL(fmul),
949 INSN_LABEL(dmul),
950 INSN_LABEL(idiv),
951 INSN_LABEL(ldiv),
952 INSN_LABEL(fdiv),
953 INSN_LABEL(ddiv),
954 INSN_LABEL(irem),
955 INSN_LABEL(lrem),
956 INSN_LABEL(frem),
957 INSN_LABEL(drem),
958 INSN_LABEL(ineg),
959 INSN_LABEL(lneg),
960 INSN_LABEL(fneg),
961 INSN_LABEL(dneg),
962 INSN_LABEL(ishl),
963 INSN_LABEL(lshl),
964 INSN_LABEL(ishr),
965 INSN_LABEL(lshr),
966 INSN_LABEL(iushr),
967 INSN_LABEL(lushr),
968 INSN_LABEL(iand),
969 INSN_LABEL(land),
970 INSN_LABEL(ior),
971 INSN_LABEL(lor),
972 INSN_LABEL(ixor),
973 INSN_LABEL(lxor),
974 INSN_LABEL(iinc),
975 INSN_LABEL(i2l),
976 INSN_LABEL(i2f),
977 INSN_LABEL(i2d),
978 INSN_LABEL(l2i),
979 INSN_LABEL(l2f),
980 INSN_LABEL(l2d),
981 INSN_LABEL(f2i),
982 INSN_LABEL(f2l),
983 INSN_LABEL(f2d),
984 INSN_LABEL(d2i),
985 INSN_LABEL(d2l),
986 INSN_LABEL(d2f),
987 INSN_LABEL(i2b),
988 INSN_LABEL(i2c),
989 INSN_LABEL(i2s),
990 INSN_LABEL(lcmp),
991 INSN_LABEL(fcmpl),
992 INSN_LABEL(fcmpg),
993 INSN_LABEL(dcmpl),
994 INSN_LABEL(dcmpg),
995 INSN_LABEL(ifeq),
996 INSN_LABEL(ifne),
997 INSN_LABEL(iflt),
998 INSN_LABEL(ifge),
999 INSN_LABEL(ifgt),
1000 INSN_LABEL(ifle),
1001 INSN_LABEL(if_icmpeq),
1002 INSN_LABEL(if_icmpne),
1003 INSN_LABEL(if_icmplt),
1004 INSN_LABEL(if_icmpge),
1005 INSN_LABEL(if_icmpgt),
1006 INSN_LABEL(if_icmple),
1007 INSN_LABEL(if_acmpeq),
1008 INSN_LABEL(if_acmpne),
1009 INSN_LABEL(goto),
1010 INSN_LABEL(jsr),
1011 INSN_LABEL(ret),
1012 INSN_LABEL(tableswitch),
1013 INSN_LABEL(lookupswitch),
1014 INSN_LABEL(ireturn),
1015 INSN_LABEL(lreturn),
1016 INSN_LABEL(freturn),
1017 INSN_LABEL(dreturn),
1018 INSN_LABEL(areturn),
1019 INSN_LABEL(return),
1020 INSN_LABEL(getstatic),
1021 INSN_LABEL(putstatic),
1022 INSN_LABEL(getfield),
1023 INSN_LABEL(putfield),
1024 INSN_LABEL(invokevirtual),
1025 INSN_LABEL(invokespecial),
1026 INSN_LABEL(invokestatic),
1027 INSN_LABEL(invokeinterface),
1028 0, /* Unused. */
1029 INSN_LABEL(new),
1030 INSN_LABEL(newarray),
1031 INSN_LABEL(anewarray),
1032 INSN_LABEL(arraylength),
1033 INSN_LABEL(athrow),
1034 INSN_LABEL(checkcast),
1035 INSN_LABEL(instanceof),
1036 INSN_LABEL(monitorenter),
1037 INSN_LABEL(monitorexit),
1038 #ifdef DIRECT_THREADED
1039 0, // wide
1040 #else
1041 INSN_LABEL(wide),
1042 #endif
1043 INSN_LABEL(multianewarray),
1044 INSN_LABEL(ifnull),
1045 INSN_LABEL(ifnonnull),
1046 INSN_LABEL(goto_w),
1047 INSN_LABEL(jsr_w),
1051 pc_t pc;
1053 #ifdef DIRECT_THREADED
1055 #define NEXT_INSN goto *((pc++)->insn)
1056 #define INTVAL() ((pc++)->int_val)
1057 #define AVAL() ((pc++)->datum)
1059 #define GET1S() INTVAL ()
1060 #define GET2S() INTVAL ()
1061 #define GET1U() INTVAL ()
1062 #define GET2U() INTVAL ()
1063 #define AVAL1U() AVAL ()
1064 #define AVAL2U() AVAL ()
1065 #define AVAL2UP() AVAL ()
1066 #define SKIP_GOTO ++pc
1067 #define GOTO_VAL() (insn_slot *) pc->datum
1068 #define PCVAL(unionval) unionval.p
1069 #define AMPAMP(label) &&label
1071 // Compile if we must. NOTE: Double-check locking.
1072 if (prepared == NULL)
1074 _Jv_MutexLock (&compile_mutex);
1075 if (prepared == NULL)
1076 compile (insn_target);
1077 _Jv_MutexUnlock (&compile_mutex);
1079 pc = (insn_slot *) prepared;
1081 #else
1083 #define NEXT_INSN goto *(insn_target[*pc++])
1085 #define GET1S() get1s (pc++)
1086 #define GET2S() (pc += 2, get2s (pc- 2))
1087 #define GET1U() get1u (pc++)
1088 #define GET2U() (pc += 2, get2u (pc - 2))
1089 #define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
1090 #define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
1091 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1092 #define SKIP_GOTO pc += 2
1093 #define GOTO_VAL() pc - 1 + get2s (pc)
1094 #define PCVAL(unionval) unionval.i
1095 #define AMPAMP(label) NULL
1097 pc = bytecode ();
1099 #endif /* DIRECT_THREADED */
1101 #define TAKE_GOTO pc = GOTO_VAL ()
1105 // We keep nop around. It is used if we're interpreting the
1106 // bytecodes and not doing direct threading.
1107 insn_nop:
1108 NEXT_INSN;
1110 /* The first few instructions here are ordered according to their
1111 frequency, in the hope that this will improve code locality a
1112 little. */
1114 insn_aload_0: // 0x2a
1115 LOADA (0);
1116 NEXT_INSN;
1118 insn_iload: // 0x15
1119 LOADI (GET1U ());
1120 NEXT_INSN;
1122 insn_iload_1: // 0x1b
1123 LOADI (1);
1124 NEXT_INSN;
1126 insn_invokevirtual: // 0xb6
1128 int index = GET2U ();
1130 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1131 * value already is resolved. If we want to clutter up the
1132 * code here to gain a little performance, then we can check
1133 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1134 * directly. For now, I don't think it is worth it. */
1136 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
1137 index)).rmethod;
1139 sp -= rmeth->stack_item_count;
1140 // We don't use NULLCHECK here because we can't rely on that
1141 // working if the method is final. So instead we do an
1142 // explicit test.
1143 if (! sp[0].o)
1144 throw new java::lang::NullPointerException;
1146 if (rmeth->vtable_index == -1)
1148 // final methods do not appear in the vtable,
1149 // if it does not appear in the superclass.
1150 fun = (void (*)()) rmeth->method->ncode;
1152 else
1154 jobject rcv = sp[0].o;
1155 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1156 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1159 #ifdef DIRECT_THREADED
1160 // Rewrite instruction so that we use a faster pre-resolved
1161 // method.
1162 pc[-2].insn = &&invokevirtual_resolved;
1163 pc[-1].datum = rmeth;
1164 #endif /* DIRECT_THREADED */
1166 goto perform_invoke;
1168 #ifdef DIRECT_THREADED
1169 invokevirtual_resolved:
1171 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1172 sp -= rmeth->stack_item_count;
1173 // We don't use NULLCHECK here because we can't rely on that
1174 // working if the method is final. So instead we do an
1175 // explicit test.
1176 if (! sp[0].o)
1177 throw new java::lang::NullPointerException;
1179 if (rmeth->vtable_index == -1)
1181 // final methods do not appear in the vtable,
1182 // if it does not appear in the superclass.
1183 fun = (void (*)()) rmeth->method->ncode;
1185 else
1187 jobject rcv = sp[0].o;
1188 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1189 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1192 goto perform_invoke;
1193 #endif /* DIRECT_THREADED */
1195 perform_invoke:
1197 /* here goes the magic again... */
1198 ffi_cif *cif = &rmeth->cif;
1199 ffi_raw *raw = (ffi_raw*) sp;
1201 _Jv_value rvalue;
1203 #if FFI_NATIVE_RAW_API
1204 /* We assume that this is only implemented if it's correct */
1205 /* to use it here. On a 64 bit machine, it never is. */
1206 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1207 #else
1208 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1209 #endif
1211 int rtype = cif->rtype->type;
1213 /* the likelyhood of object, int, or void return is very high,
1214 * so those are checked before the switch */
1215 if (rtype == FFI_TYPE_POINTER)
1217 PUSHA (rvalue.object_value);
1219 else if (rtype == FFI_TYPE_SINT32)
1221 PUSHI (rvalue.int_value);
1223 else if (rtype == FFI_TYPE_VOID)
1225 /* skip */
1227 else
1229 switch (rtype)
1231 case FFI_TYPE_SINT8:
1232 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1233 break;
1235 case FFI_TYPE_SINT16:
1236 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1237 break;
1239 case FFI_TYPE_UINT16:
1240 PUSHI (rvalue.int_value & 0xffff);
1241 break;
1243 case FFI_TYPE_FLOAT:
1244 PUSHF (rvalue.float_value);
1245 break;
1247 case FFI_TYPE_DOUBLE:
1248 PUSHD (rvalue.double_value);
1249 break;
1251 case FFI_TYPE_SINT64:
1252 PUSHL (rvalue.long_value);
1253 break;
1255 default:
1256 throw_internal_error ("unknown return type in invokeXXX");
1260 NEXT_INSN;
1262 insn_aconst_null:
1263 PUSHA (NULL);
1264 NEXT_INSN;
1266 insn_iconst_m1:
1267 PUSHI (-1);
1268 NEXT_INSN;
1270 insn_iconst_0:
1271 PUSHI (0);
1272 NEXT_INSN;
1274 insn_iconst_1:
1275 PUSHI (1);
1276 NEXT_INSN;
1278 insn_iconst_2:
1279 PUSHI (2);
1280 NEXT_INSN;
1282 insn_iconst_3:
1283 PUSHI (3);
1284 NEXT_INSN;
1286 insn_iconst_4:
1287 PUSHI (4);
1288 NEXT_INSN;
1290 insn_iconst_5:
1291 PUSHI (5);
1292 NEXT_INSN;
1294 insn_lconst_0:
1295 PUSHL (0);
1296 NEXT_INSN;
1298 insn_lconst_1:
1299 PUSHL (1);
1300 NEXT_INSN;
1302 insn_fconst_0:
1303 PUSHF (0);
1304 NEXT_INSN;
1306 insn_fconst_1:
1307 PUSHF (1);
1308 NEXT_INSN;
1310 insn_fconst_2:
1311 PUSHF (2);
1312 NEXT_INSN;
1314 insn_dconst_0:
1315 PUSHD (0);
1316 NEXT_INSN;
1318 insn_dconst_1:
1319 PUSHD (1);
1320 NEXT_INSN;
1322 insn_bipush:
1323 // For direct threaded, bipush and sipush are the same.
1324 #ifndef DIRECT_THREADED
1325 PUSHI (GET1S ());
1326 NEXT_INSN;
1327 #endif /* DIRECT_THREADED */
1328 insn_sipush:
1329 PUSHI (GET2S ());
1330 NEXT_INSN;
1332 insn_ldc:
1333 // For direct threaded, ldc and ldc_w are the same.
1334 #ifndef DIRECT_THREADED
1335 PUSHA ((jobject) AVAL1U ());
1336 NEXT_INSN;
1337 #endif /* DIRECT_THREADED */
1338 insn_ldc_w:
1339 PUSHA ((jobject) AVAL2U ());
1340 NEXT_INSN;
1342 insn_ldc2_w:
1344 void *where = AVAL2UP ();
1345 memcpy (sp, where, 2*sizeof (_Jv_word));
1346 sp += 2;
1348 NEXT_INSN;
1350 insn_lload:
1351 LOADL (GET1U ());
1352 NEXT_INSN;
1354 insn_fload:
1355 LOADF (GET1U ());
1356 NEXT_INSN;
1358 insn_dload:
1359 LOADD (GET1U ());
1360 NEXT_INSN;
1362 insn_aload:
1363 LOADA (GET1U ());
1364 NEXT_INSN;
1366 insn_iload_0:
1367 LOADI (0);
1368 NEXT_INSN;
1370 insn_iload_2:
1371 LOADI (2);
1372 NEXT_INSN;
1374 insn_iload_3:
1375 LOADI (3);
1376 NEXT_INSN;
1378 insn_lload_0:
1379 LOADL (0);
1380 NEXT_INSN;
1382 insn_lload_1:
1383 LOADL (1);
1384 NEXT_INSN;
1386 insn_lload_2:
1387 LOADL (2);
1388 NEXT_INSN;
1390 insn_lload_3:
1391 LOADL (3);
1392 NEXT_INSN;
1394 insn_fload_0:
1395 LOADF (0);
1396 NEXT_INSN;
1398 insn_fload_1:
1399 LOADF (1);
1400 NEXT_INSN;
1402 insn_fload_2:
1403 LOADF (2);
1404 NEXT_INSN;
1406 insn_fload_3:
1407 LOADF (3);
1408 NEXT_INSN;
1410 insn_dload_0:
1411 LOADD (0);
1412 NEXT_INSN;
1414 insn_dload_1:
1415 LOADD (1);
1416 NEXT_INSN;
1418 insn_dload_2:
1419 LOADD (2);
1420 NEXT_INSN;
1422 insn_dload_3:
1423 LOADD (3);
1424 NEXT_INSN;
1426 insn_aload_1:
1427 LOADA(1);
1428 NEXT_INSN;
1430 insn_aload_2:
1431 LOADA(2);
1432 NEXT_INSN;
1434 insn_aload_3:
1435 LOADA(3);
1436 NEXT_INSN;
1438 insn_iaload:
1440 jint index = POPI();
1441 jintArray arr = (jintArray) POPA();
1442 NULLARRAYCHECK (arr);
1443 ARRAYBOUNDSCHECK (arr, index);
1444 PUSHI( elements(arr)[index] );
1446 NEXT_INSN;
1448 insn_laload:
1450 jint index = POPI();
1451 jlongArray arr = (jlongArray) POPA();
1452 NULLARRAYCHECK (arr);
1453 ARRAYBOUNDSCHECK (arr, index);
1454 PUSHL( elements(arr)[index] );
1456 NEXT_INSN;
1458 insn_faload:
1460 jint index = POPI();
1461 jfloatArray arr = (jfloatArray) POPA();
1462 NULLARRAYCHECK (arr);
1463 ARRAYBOUNDSCHECK (arr, index);
1464 PUSHF( elements(arr)[index] );
1466 NEXT_INSN;
1468 insn_daload:
1470 jint index = POPI();
1471 jdoubleArray arr = (jdoubleArray) POPA();
1472 NULLARRAYCHECK (arr);
1473 ARRAYBOUNDSCHECK (arr, index);
1474 PUSHD( elements(arr)[index] );
1476 NEXT_INSN;
1478 insn_aaload:
1480 jint index = POPI();
1481 jobjectArray arr = (jobjectArray) POPA();
1482 NULLARRAYCHECK (arr);
1483 ARRAYBOUNDSCHECK (arr, index);
1484 PUSHA( elements(arr)[index] );
1486 NEXT_INSN;
1488 insn_baload:
1490 jint index = POPI();
1491 jbyteArray arr = (jbyteArray) POPA();
1492 NULLARRAYCHECK (arr);
1493 ARRAYBOUNDSCHECK (arr, index);
1494 PUSHI( elements(arr)[index] );
1496 NEXT_INSN;
1498 insn_caload:
1500 jint index = POPI();
1501 jcharArray arr = (jcharArray) POPA();
1502 NULLARRAYCHECK (arr);
1503 ARRAYBOUNDSCHECK (arr, index);
1504 PUSHI( elements(arr)[index] );
1506 NEXT_INSN;
1508 insn_saload:
1510 jint index = POPI();
1511 jshortArray arr = (jshortArray) POPA();
1512 NULLARRAYCHECK (arr);
1513 ARRAYBOUNDSCHECK (arr, index);
1514 PUSHI( elements(arr)[index] );
1516 NEXT_INSN;
1518 insn_istore:
1519 STOREI (GET1U ());
1520 NEXT_INSN;
1522 insn_lstore:
1523 STOREL (GET1U ());
1524 NEXT_INSN;
1526 insn_fstore:
1527 STOREF (GET1U ());
1528 NEXT_INSN;
1530 insn_dstore:
1531 STORED (GET1U ());
1532 NEXT_INSN;
1534 insn_astore:
1535 STOREA (GET1U ());
1536 NEXT_INSN;
1538 insn_istore_0:
1539 STOREI (0);
1540 NEXT_INSN;
1542 insn_istore_1:
1543 STOREI (1);
1544 NEXT_INSN;
1546 insn_istore_2:
1547 STOREI (2);
1548 NEXT_INSN;
1550 insn_istore_3:
1551 STOREI (3);
1552 NEXT_INSN;
1554 insn_lstore_0:
1555 STOREL (0);
1556 NEXT_INSN;
1558 insn_lstore_1:
1559 STOREL (1);
1560 NEXT_INSN;
1562 insn_lstore_2:
1563 STOREL (2);
1564 NEXT_INSN;
1566 insn_lstore_3:
1567 STOREL (3);
1568 NEXT_INSN;
1570 insn_fstore_0:
1571 STOREF (0);
1572 NEXT_INSN;
1574 insn_fstore_1:
1575 STOREF (1);
1576 NEXT_INSN;
1578 insn_fstore_2:
1579 STOREF (2);
1580 NEXT_INSN;
1582 insn_fstore_3:
1583 STOREF (3);
1584 NEXT_INSN;
1586 insn_dstore_0:
1587 STORED (0);
1588 NEXT_INSN;
1590 insn_dstore_1:
1591 STORED (1);
1592 NEXT_INSN;
1594 insn_dstore_2:
1595 STORED (2);
1596 NEXT_INSN;
1598 insn_dstore_3:
1599 STORED (3);
1600 NEXT_INSN;
1602 insn_astore_0:
1603 STOREA(0);
1604 NEXT_INSN;
1606 insn_astore_1:
1607 STOREA(1);
1608 NEXT_INSN;
1610 insn_astore_2:
1611 STOREA(2);
1612 NEXT_INSN;
1614 insn_astore_3:
1615 STOREA(3);
1616 NEXT_INSN;
1618 insn_iastore:
1620 jint value = POPI();
1621 jint index = POPI();
1622 jintArray arr = (jintArray) POPA();
1623 NULLARRAYCHECK (arr);
1624 ARRAYBOUNDSCHECK (arr, index);
1625 elements(arr)[index] = value;
1627 NEXT_INSN;
1629 insn_lastore:
1631 jlong value = POPL();
1632 jint index = POPI();
1633 jlongArray arr = (jlongArray) POPA();
1634 NULLARRAYCHECK (arr);
1635 ARRAYBOUNDSCHECK (arr, index);
1636 elements(arr)[index] = value;
1638 NEXT_INSN;
1640 insn_fastore:
1642 jfloat value = POPF();
1643 jint index = POPI();
1644 jfloatArray arr = (jfloatArray) POPA();
1645 NULLARRAYCHECK (arr);
1646 ARRAYBOUNDSCHECK (arr, index);
1647 elements(arr)[index] = value;
1649 NEXT_INSN;
1651 insn_dastore:
1653 jdouble value = POPD();
1654 jint index = POPI();
1655 jdoubleArray arr = (jdoubleArray) POPA();
1656 NULLARRAYCHECK (arr);
1657 ARRAYBOUNDSCHECK (arr, index);
1658 elements(arr)[index] = value;
1660 NEXT_INSN;
1662 insn_aastore:
1664 jobject value = POPA();
1665 jint index = POPI();
1666 jobjectArray arr = (jobjectArray) POPA();
1667 NULLARRAYCHECK (arr);
1668 ARRAYBOUNDSCHECK (arr, index);
1669 _Jv_CheckArrayStore (arr, value);
1670 elements(arr)[index] = value;
1672 NEXT_INSN;
1674 insn_bastore:
1676 jbyte value = (jbyte) POPI();
1677 jint index = POPI();
1678 jbyteArray arr = (jbyteArray) POPA();
1679 NULLARRAYCHECK (arr);
1680 ARRAYBOUNDSCHECK (arr, index);
1681 elements(arr)[index] = value;
1683 NEXT_INSN;
1685 insn_castore:
1687 jchar value = (jchar) POPI();
1688 jint index = POPI();
1689 jcharArray arr = (jcharArray) POPA();
1690 NULLARRAYCHECK (arr);
1691 ARRAYBOUNDSCHECK (arr, index);
1692 elements(arr)[index] = value;
1694 NEXT_INSN;
1696 insn_sastore:
1698 jshort value = (jshort) POPI();
1699 jint index = POPI();
1700 jshortArray arr = (jshortArray) POPA();
1701 NULLARRAYCHECK (arr);
1702 ARRAYBOUNDSCHECK (arr, index);
1703 elements(arr)[index] = value;
1705 NEXT_INSN;
1707 insn_pop:
1708 sp -= 1;
1709 NEXT_INSN;
1711 insn_pop2:
1712 sp -= 2;
1713 NEXT_INSN;
1715 insn_dup:
1716 sp[0] = sp[-1];
1717 sp += 1;
1718 NEXT_INSN;
1720 insn_dup_x1:
1721 dupx (sp, 1, 1); sp+=1;
1722 NEXT_INSN;
1724 insn_dup_x2:
1725 dupx (sp, 1, 2); sp+=1;
1726 NEXT_INSN;
1728 insn_dup2:
1729 sp[0] = sp[-2];
1730 sp[1] = sp[-1];
1731 sp += 2;
1732 NEXT_INSN;
1734 insn_dup2_x1:
1735 dupx (sp, 2, 1); sp+=2;
1736 NEXT_INSN;
1738 insn_dup2_x2:
1739 dupx (sp, 2, 2); sp+=2;
1740 NEXT_INSN;
1742 insn_swap:
1744 jobject tmp1 = POPA();
1745 jobject tmp2 = POPA();
1746 PUSHA (tmp1);
1747 PUSHA (tmp2);
1749 NEXT_INSN;
1751 insn_iadd:
1752 BINOPI(+);
1753 NEXT_INSN;
1755 insn_ladd:
1756 BINOPL(+);
1757 NEXT_INSN;
1759 insn_fadd:
1760 BINOPF(+);
1761 NEXT_INSN;
1763 insn_dadd:
1764 BINOPD(+);
1765 NEXT_INSN;
1767 insn_isub:
1768 BINOPI(-);
1769 NEXT_INSN;
1771 insn_lsub:
1772 BINOPL(-);
1773 NEXT_INSN;
1775 insn_fsub:
1776 BINOPF(-);
1777 NEXT_INSN;
1779 insn_dsub:
1780 BINOPD(-);
1781 NEXT_INSN;
1783 insn_imul:
1784 BINOPI(*);
1785 NEXT_INSN;
1787 insn_lmul:
1788 BINOPL(*);
1789 NEXT_INSN;
1791 insn_fmul:
1792 BINOPF(*);
1793 NEXT_INSN;
1795 insn_dmul:
1796 BINOPD(*);
1797 NEXT_INSN;
1799 insn_idiv:
1801 jint value2 = POPI();
1802 jint value1 = POPI();
1803 jint res = _Jv_divI (value1, value2);
1804 PUSHI (res);
1806 NEXT_INSN;
1808 insn_ldiv:
1810 jlong value2 = POPL();
1811 jlong value1 = POPL();
1812 jlong res = _Jv_divJ (value1, value2);
1813 PUSHL (res);
1815 NEXT_INSN;
1817 insn_fdiv:
1819 jfloat value2 = POPF();
1820 jfloat value1 = POPF();
1821 jfloat res = value1 / value2;
1822 PUSHF (res);
1824 NEXT_INSN;
1826 insn_ddiv:
1828 jdouble value2 = POPD();
1829 jdouble value1 = POPD();
1830 jdouble res = value1 / value2;
1831 PUSHD (res);
1833 NEXT_INSN;
1835 insn_irem:
1837 jint value2 = POPI();
1838 jint value1 = POPI();
1839 jint res = _Jv_remI (value1, value2);
1840 PUSHI (res);
1842 NEXT_INSN;
1844 insn_lrem:
1846 jlong value2 = POPL();
1847 jlong value1 = POPL();
1848 jlong res = _Jv_remJ (value1, value2);
1849 PUSHL (res);
1851 NEXT_INSN;
1853 insn_frem:
1855 jfloat value2 = POPF();
1856 jfloat value1 = POPF();
1857 jfloat res = __ieee754_fmod (value1, value2);
1858 PUSHF (res);
1860 NEXT_INSN;
1862 insn_drem:
1864 jdouble value2 = POPD();
1865 jdouble value1 = POPD();
1866 jdouble res = __ieee754_fmod (value1, value2);
1867 PUSHD (res);
1869 NEXT_INSN;
1871 insn_ineg:
1873 jint value = POPI();
1874 PUSHI (value * -1);
1876 NEXT_INSN;
1878 insn_lneg:
1880 jlong value = POPL();
1881 PUSHL (value * -1);
1883 NEXT_INSN;
1885 insn_fneg:
1887 jfloat value = POPF();
1888 PUSHF (value * -1);
1890 NEXT_INSN;
1892 insn_dneg:
1894 jdouble value = POPD();
1895 PUSHD (value * -1);
1897 NEXT_INSN;
1899 insn_ishl:
1901 jint shift = (POPI() & 0x1f);
1902 jint value = POPI();
1903 PUSHI (value << shift);
1905 NEXT_INSN;
1907 insn_lshl:
1909 jint shift = (POPI() & 0x3f);
1910 jlong value = POPL();
1911 PUSHL (value << shift);
1913 NEXT_INSN;
1915 insn_ishr:
1917 jint shift = (POPI() & 0x1f);
1918 jint value = POPI();
1919 PUSHI (value >> shift);
1921 NEXT_INSN;
1923 insn_lshr:
1925 jint shift = (POPI() & 0x3f);
1926 jlong value = POPL();
1927 PUSHL (value >> shift);
1929 NEXT_INSN;
1931 insn_iushr:
1933 jint shift = (POPI() & 0x1f);
1934 _Jv_uint value = (_Jv_uint) POPI();
1935 PUSHI ((jint) (value >> shift));
1937 NEXT_INSN;
1939 insn_lushr:
1941 jint shift = (POPI() & 0x3f);
1942 _Jv_ulong value = (_Jv_ulong) POPL();
1943 PUSHL ((jlong) (value >> shift));
1945 NEXT_INSN;
1947 insn_iand:
1948 BINOPI (&);
1949 NEXT_INSN;
1951 insn_land:
1952 BINOPL (&);
1953 NEXT_INSN;
1955 insn_ior:
1956 BINOPI (|);
1957 NEXT_INSN;
1959 insn_lor:
1960 BINOPL (|);
1961 NEXT_INSN;
1963 insn_ixor:
1964 BINOPI (^);
1965 NEXT_INSN;
1967 insn_lxor:
1968 BINOPL (^);
1969 NEXT_INSN;
1971 insn_iinc:
1973 jint index = GET1U ();
1974 jint amount = GET1S ();
1975 locals[index].i += amount;
1977 NEXT_INSN;
1979 insn_i2l:
1980 {jlong value = POPI(); PUSHL (value);}
1981 NEXT_INSN;
1983 insn_i2f:
1984 {jfloat value = POPI(); PUSHF (value);}
1985 NEXT_INSN;
1987 insn_i2d:
1988 {jdouble value = POPI(); PUSHD (value);}
1989 NEXT_INSN;
1991 insn_l2i:
1992 {jint value = POPL(); PUSHI (value);}
1993 NEXT_INSN;
1995 insn_l2f:
1996 {jfloat value = POPL(); PUSHF (value);}
1997 NEXT_INSN;
1999 insn_l2d:
2000 {jdouble value = POPL(); PUSHD (value);}
2001 NEXT_INSN;
2003 insn_f2i:
2005 using namespace java::lang;
2006 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2007 PUSHI(value);
2009 NEXT_INSN;
2011 insn_f2l:
2013 using namespace java::lang;
2014 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2015 PUSHL(value);
2017 NEXT_INSN;
2019 insn_f2d:
2020 { jdouble value = POPF (); PUSHD(value); }
2021 NEXT_INSN;
2023 insn_d2i:
2025 using namespace java::lang;
2026 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2027 PUSHI(value);
2029 NEXT_INSN;
2031 insn_d2l:
2033 using namespace java::lang;
2034 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2035 PUSHL(value);
2037 NEXT_INSN;
2039 insn_d2f:
2040 { jfloat value = POPD (); PUSHF(value); }
2041 NEXT_INSN;
2043 insn_i2b:
2044 { jbyte value = POPI (); PUSHI(value); }
2045 NEXT_INSN;
2047 insn_i2c:
2048 { jchar value = POPI (); PUSHI(value); }
2049 NEXT_INSN;
2051 insn_i2s:
2052 { jshort value = POPI (); PUSHI(value); }
2053 NEXT_INSN;
2055 insn_lcmp:
2057 jlong value2 = POPL ();
2058 jlong value1 = POPL ();
2059 if (value1 > value2)
2060 { PUSHI (1); }
2061 else if (value1 == value2)
2062 { PUSHI (0); }
2063 else
2064 { PUSHI (-1); }
2066 NEXT_INSN;
2068 insn_fcmpl:
2069 tmpval = -1;
2070 goto fcmp;
2072 insn_fcmpg:
2073 tmpval = 1;
2075 fcmp:
2077 jfloat value2 = POPF ();
2078 jfloat value1 = POPF ();
2079 if (value1 > value2)
2080 PUSHI (1);
2081 else if (value1 == value2)
2082 PUSHI (0);
2083 else if (value1 < value2)
2084 PUSHI (-1);
2085 else
2086 PUSHI (tmpval);
2088 NEXT_INSN;
2090 insn_dcmpl:
2091 tmpval = -1;
2092 goto dcmp;
2094 insn_dcmpg:
2095 tmpval = 1;
2097 dcmp:
2099 jdouble value2 = POPD ();
2100 jdouble value1 = POPD ();
2101 if (value1 > value2)
2102 PUSHI (1);
2103 else if (value1 == value2)
2104 PUSHI (0);
2105 else if (value1 < value2)
2106 PUSHI (-1);
2107 else
2108 PUSHI (tmpval);
2110 NEXT_INSN;
2112 insn_ifeq:
2114 if (POPI() == 0)
2115 TAKE_GOTO;
2116 else
2117 SKIP_GOTO;
2119 NEXT_INSN;
2121 insn_ifne:
2123 if (POPI() != 0)
2124 TAKE_GOTO;
2125 else
2126 SKIP_GOTO;
2128 NEXT_INSN;
2130 insn_iflt:
2132 if (POPI() < 0)
2133 TAKE_GOTO;
2134 else
2135 SKIP_GOTO;
2137 NEXT_INSN;
2139 insn_ifge:
2141 if (POPI() >= 0)
2142 TAKE_GOTO;
2143 else
2144 SKIP_GOTO;
2146 NEXT_INSN;
2148 insn_ifgt:
2150 if (POPI() > 0)
2151 TAKE_GOTO;
2152 else
2153 SKIP_GOTO;
2155 NEXT_INSN;
2157 insn_ifle:
2159 if (POPI() <= 0)
2160 TAKE_GOTO;
2161 else
2162 SKIP_GOTO;
2164 NEXT_INSN;
2166 insn_if_icmpeq:
2168 jint value2 = POPI();
2169 jint value1 = POPI();
2170 if (value1 == value2)
2171 TAKE_GOTO;
2172 else
2173 SKIP_GOTO;
2175 NEXT_INSN;
2177 insn_if_icmpne:
2179 jint value2 = POPI();
2180 jint value1 = POPI();
2181 if (value1 != value2)
2182 TAKE_GOTO;
2183 else
2184 SKIP_GOTO;
2186 NEXT_INSN;
2188 insn_if_icmplt:
2190 jint value2 = POPI();
2191 jint value1 = POPI();
2192 if (value1 < value2)
2193 TAKE_GOTO;
2194 else
2195 SKIP_GOTO;
2197 NEXT_INSN;
2199 insn_if_icmpge:
2201 jint value2 = POPI();
2202 jint value1 = POPI();
2203 if (value1 >= value2)
2204 TAKE_GOTO;
2205 else
2206 SKIP_GOTO;
2208 NEXT_INSN;
2210 insn_if_icmpgt:
2212 jint value2 = POPI();
2213 jint value1 = POPI();
2214 if (value1 > value2)
2215 TAKE_GOTO;
2216 else
2217 SKIP_GOTO;
2219 NEXT_INSN;
2221 insn_if_icmple:
2223 jint value2 = POPI();
2224 jint value1 = POPI();
2225 if (value1 <= value2)
2226 TAKE_GOTO;
2227 else
2228 SKIP_GOTO;
2230 NEXT_INSN;
2232 insn_if_acmpeq:
2234 jobject value2 = POPA();
2235 jobject value1 = POPA();
2236 if (value1 == value2)
2237 TAKE_GOTO;
2238 else
2239 SKIP_GOTO;
2241 NEXT_INSN;
2243 insn_if_acmpne:
2245 jobject value2 = POPA();
2246 jobject value1 = POPA();
2247 if (value1 != value2)
2248 TAKE_GOTO;
2249 else
2250 SKIP_GOTO;
2252 NEXT_INSN;
2254 insn_goto_w:
2255 #ifndef DIRECT_THREADED
2256 // For direct threaded, goto and goto_w are the same.
2257 pc = pc - 1 + get4 (pc);
2258 NEXT_INSN;
2259 #endif /* DIRECT_THREADED */
2260 insn_goto:
2261 TAKE_GOTO;
2262 NEXT_INSN;
2264 insn_jsr_w:
2265 #ifndef DIRECT_THREADED
2266 // For direct threaded, jsr and jsr_w are the same.
2268 pc_t next = pc - 1 + get4 (pc);
2269 pc += 4;
2270 PUSHA ((jobject) pc);
2271 pc = next;
2273 NEXT_INSN;
2274 #endif /* DIRECT_THREADED */
2275 insn_jsr:
2277 pc_t next = GOTO_VAL();
2278 SKIP_GOTO;
2279 PUSHA ((jobject) pc);
2280 pc = next;
2282 NEXT_INSN;
2284 insn_ret:
2286 jint index = GET1U ();
2287 pc = (pc_t) PEEKA (index);
2289 NEXT_INSN;
2291 insn_tableswitch:
2293 #ifdef DIRECT_THREADED
2294 void *def = (pc++)->datum;
2296 int index = POPI();
2298 jint low = INTVAL ();
2299 jint high = INTVAL ();
2301 if (index < low || index > high)
2302 pc = (insn_slot *) def;
2303 else
2304 pc = (insn_slot *) ((pc + index - low)->datum);
2305 #else
2306 pc_t base_pc = pc - 1;
2307 int index = POPI ();
2309 pc_t base = (pc_t) bytecode ();
2310 while ((pc - base) % 4 != 0)
2311 ++pc;
2313 jint def = get4 (pc);
2314 jint low = get4 (pc + 4);
2315 jint high = get4 (pc + 8);
2316 if (index < low || index > high)
2317 pc = base_pc + def;
2318 else
2319 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2320 #endif /* DIRECT_THREADED */
2322 NEXT_INSN;
2324 insn_lookupswitch:
2326 #ifdef DIRECT_THREADED
2327 void *def = (pc++)->insn;
2329 int index = POPI();
2331 jint npairs = INTVAL ();
2333 int max = npairs - 1;
2334 int min = 0;
2336 // Simple binary search...
2337 while (min < max)
2339 int half = (min + max) / 2;
2340 int match = pc[2 * half].int_val;
2342 if (index == match)
2344 // Found it.
2345 pc = (insn_slot *) pc[2 * half + 1].datum;
2346 NEXT_INSN;
2348 else if (index < match)
2349 // We can use HALF - 1 here because we check again on
2350 // loop exit.
2351 max = half - 1;
2352 else
2353 // We can use HALF + 1 here because we check again on
2354 // loop exit.
2355 min = half + 1;
2357 if (index == pc[2 * min].int_val)
2358 pc = (insn_slot *) pc[2 * min + 1].datum;
2359 else
2360 pc = (insn_slot *) def;
2361 #else
2362 unsigned char *base_pc = pc-1;
2363 int index = POPI();
2365 unsigned char* base = bytecode ();
2366 while ((pc-base) % 4 != 0)
2367 ++pc;
2369 jint def = get4 (pc);
2370 jint npairs = get4 (pc+4);
2372 int max = npairs-1;
2373 int min = 0;
2375 // Simple binary search...
2376 while (min < max)
2378 int half = (min+max)/2;
2379 int match = get4 (pc+ 4*(2 + 2*half));
2381 if (index == match)
2382 min = max = half;
2383 else if (index < match)
2384 // We can use HALF - 1 here because we check again on
2385 // loop exit.
2386 max = half - 1;
2387 else
2388 // We can use HALF + 1 here because we check again on
2389 // loop exit.
2390 min = half + 1;
2393 if (index == get4 (pc+ 4*(2 + 2*min)))
2394 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2395 else
2396 pc = base_pc + def;
2397 #endif /* DIRECT_THREADED */
2399 NEXT_INSN;
2401 insn_areturn:
2402 *(jobject *) retp = POPA ();
2403 return;
2405 insn_lreturn:
2406 *(jlong *) retp = POPL ();
2407 return;
2409 insn_freturn:
2410 *(jfloat *) retp = POPF ();
2411 return;
2413 insn_dreturn:
2414 *(jdouble *) retp = POPD ();
2415 return;
2417 insn_ireturn:
2418 *(jint *) retp = POPI ();
2419 return;
2421 insn_return:
2422 return;
2424 insn_getstatic:
2426 jint fieldref_index = GET2U ();
2427 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2428 _Jv_Field *field = pool_data[fieldref_index].field;
2430 if ((field->flags & Modifier::STATIC) == 0)
2431 throw_incompatible_class_change_error
2432 (JvNewStringLatin1 ("field no longer static"));
2434 jclass type = field->type;
2436 // We rewrite the instruction once we discover what it refers
2437 // to.
2438 void *newinsn = NULL;
2439 if (type->isPrimitive ())
2441 switch (type->size_in_bytes)
2443 case 1:
2444 PUSHI (*field->u.byte_addr);
2445 newinsn = AMPAMP (getstatic_resolved_1);
2446 break;
2448 case 2:
2449 if (type == JvPrimClass (char))
2451 PUSHI (*field->u.char_addr);
2452 newinsn = AMPAMP (getstatic_resolved_char);
2454 else
2456 PUSHI (*field->u.short_addr);
2457 newinsn = AMPAMP (getstatic_resolved_short);
2459 break;
2461 case 4:
2462 PUSHI(*field->u.int_addr);
2463 newinsn = AMPAMP (getstatic_resolved_4);
2464 break;
2466 case 8:
2467 PUSHL(*field->u.long_addr);
2468 newinsn = AMPAMP (getstatic_resolved_8);
2469 break;
2472 else
2474 PUSHA(*field->u.object_addr);
2475 newinsn = AMPAMP (getstatic_resolved_obj);
2478 #ifdef DIRECT_THREADED
2479 pc[-2].insn = newinsn;
2480 pc[-1].datum = field->u.addr;
2481 #endif /* DIRECT_THREADED */
2483 NEXT_INSN;
2485 #ifdef DIRECT_THREADED
2486 getstatic_resolved_1:
2487 PUSHI (*(jbyte *) AVAL ());
2488 NEXT_INSN;
2490 getstatic_resolved_char:
2491 PUSHI (*(jchar *) AVAL ());
2492 NEXT_INSN;
2494 getstatic_resolved_short:
2495 PUSHI (*(jshort *) AVAL ());
2496 NEXT_INSN;
2498 getstatic_resolved_4:
2499 PUSHI (*(jint *) AVAL ());
2500 NEXT_INSN;
2502 getstatic_resolved_8:
2503 PUSHL (*(jlong *) AVAL ());
2504 NEXT_INSN;
2506 getstatic_resolved_obj:
2507 PUSHA (*(jobject *) AVAL ());
2508 NEXT_INSN;
2509 #endif /* DIRECT_THREADED */
2511 insn_getfield:
2513 jint fieldref_index = GET2U ();
2514 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2515 _Jv_Field *field = pool_data[fieldref_index].field;
2517 if ((field->flags & Modifier::STATIC) != 0)
2518 throw_incompatible_class_change_error
2519 (JvNewStringLatin1 ("field is static"));
2521 jclass type = field->type;
2522 jint field_offset = field->u.boffset;
2523 if (field_offset > 0xffff)
2524 throw new java::lang::VirtualMachineError;
2526 jobject obj = POPA();
2527 NULLCHECK(obj);
2529 void *newinsn = NULL;
2530 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2531 if (type->isPrimitive ())
2533 switch (type->size_in_bytes)
2535 case 1:
2536 PUSHI (val->byte_value);
2537 newinsn = AMPAMP (getfield_resolved_1);
2538 break;
2540 case 2:
2541 if (type == JvPrimClass (char))
2543 PUSHI (val->char_value);
2544 newinsn = AMPAMP (getfield_resolved_char);
2546 else
2548 PUSHI (val->short_value);
2549 newinsn = AMPAMP (getfield_resolved_short);
2551 break;
2553 case 4:
2554 PUSHI (val->int_value);
2555 newinsn = AMPAMP (getfield_resolved_4);
2556 break;
2558 case 8:
2559 PUSHL (val->long_value);
2560 newinsn = AMPAMP (getfield_resolved_8);
2561 break;
2564 else
2566 PUSHA (val->object_value);
2567 newinsn = AMPAMP (getfield_resolved_obj);
2570 #ifdef DIRECT_THREADED
2571 pc[-2].insn = newinsn;
2572 pc[-1].int_val = field_offset;
2573 #endif /* DIRECT_THREADED */
2575 NEXT_INSN;
2577 #ifdef DIRECT_THREADED
2578 getfield_resolved_1:
2580 char *obj = (char *) POPA ();
2581 NULLCHECK (obj);
2582 PUSHI (*(jbyte *) (obj + INTVAL ()));
2584 NEXT_INSN;
2586 getfield_resolved_char:
2588 char *obj = (char *) POPA ();
2589 NULLCHECK (obj);
2590 PUSHI (*(jchar *) (obj + INTVAL ()));
2592 NEXT_INSN;
2594 getfield_resolved_short:
2596 char *obj = (char *) POPA ();
2597 NULLCHECK (obj);
2598 PUSHI (*(jshort *) (obj + INTVAL ()));
2600 NEXT_INSN;
2602 getfield_resolved_4:
2604 char *obj = (char *) POPA ();
2605 NULLCHECK (obj);
2606 PUSHI (*(jint *) (obj + INTVAL ()));
2608 NEXT_INSN;
2610 getfield_resolved_8:
2612 char *obj = (char *) POPA ();
2613 NULLCHECK (obj);
2614 PUSHL (*(jlong *) (obj + INTVAL ()));
2616 NEXT_INSN;
2618 getfield_resolved_obj:
2620 char *obj = (char *) POPA ();
2621 NULLCHECK (obj);
2622 PUSHA (*(jobject *) (obj + INTVAL ()));
2624 NEXT_INSN;
2625 #endif /* DIRECT_THREADED */
2627 insn_putstatic:
2629 jint fieldref_index = GET2U ();
2630 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2631 _Jv_Field *field = pool_data[fieldref_index].field;
2633 jclass type = field->type;
2635 // ResolvePoolEntry cannot check this
2636 if ((field->flags & Modifier::STATIC) == 0)
2637 throw_incompatible_class_change_error
2638 (JvNewStringLatin1 ("field no longer static"));
2640 void *newinsn = NULL;
2641 if (type->isPrimitive ())
2643 switch (type->size_in_bytes)
2645 case 1:
2647 jint value = POPI();
2648 *field->u.byte_addr = value;
2649 newinsn = AMPAMP (putstatic_resolved_1);
2650 break;
2653 case 2:
2655 jint value = POPI();
2656 *field->u.char_addr = value;
2657 newinsn = AMPAMP (putstatic_resolved_2);
2658 break;
2661 case 4:
2663 jint value = POPI();
2664 *field->u.int_addr = value;
2665 newinsn = AMPAMP (putstatic_resolved_4);
2666 break;
2669 case 8:
2671 jlong value = POPL();
2672 *field->u.long_addr = value;
2673 newinsn = AMPAMP (putstatic_resolved_8);
2674 break;
2678 else
2680 jobject value = POPA();
2681 *field->u.object_addr = value;
2682 newinsn = AMPAMP (putstatic_resolved_obj);
2685 #ifdef DIRECT_THREADED
2686 pc[-2].insn = newinsn;
2687 pc[-1].datum = field->u.addr;
2688 #endif /* DIRECT_THREADED */
2690 NEXT_INSN;
2692 #ifdef DIRECT_THREADED
2693 putstatic_resolved_1:
2694 *(jbyte *) AVAL () = POPI ();
2695 NEXT_INSN;
2697 putstatic_resolved_2:
2698 *(jchar *) AVAL () = POPI ();
2699 NEXT_INSN;
2701 putstatic_resolved_4:
2702 *(jint *) AVAL () = POPI ();
2703 NEXT_INSN;
2705 putstatic_resolved_8:
2706 *(jlong *) AVAL () = POPL ();
2707 NEXT_INSN;
2709 putstatic_resolved_obj:
2710 *(jobject *) AVAL () = POPA ();
2711 NEXT_INSN;
2712 #endif /* DIRECT_THREADED */
2714 insn_putfield:
2716 jint fieldref_index = GET2U ();
2717 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2718 _Jv_Field *field = pool_data[fieldref_index].field;
2720 jclass type = field->type;
2722 if ((field->flags & Modifier::STATIC) != 0)
2723 throw_incompatible_class_change_error
2724 (JvNewStringLatin1 ("field is static"));
2726 jint field_offset = field->u.boffset;
2727 if (field_offset > 0xffff)
2728 throw new java::lang::VirtualMachineError;
2730 void *newinsn = NULL;
2731 if (type->isPrimitive ())
2733 switch (type->size_in_bytes)
2735 case 1:
2737 jint value = POPI();
2738 jobject obj = POPA();
2739 NULLCHECK(obj);
2740 *(jbyte*) ((char*)obj + field_offset) = value;
2741 newinsn = AMPAMP (putfield_resolved_1);
2742 break;
2745 case 2:
2747 jint value = POPI();
2748 jobject obj = POPA();
2749 NULLCHECK(obj);
2750 *(jchar*) ((char*)obj + field_offset) = value;
2751 newinsn = AMPAMP (putfield_resolved_2);
2752 break;
2755 case 4:
2757 jint value = POPI();
2758 jobject obj = POPA();
2759 NULLCHECK(obj);
2760 *(jint*) ((char*)obj + field_offset) = value;
2761 newinsn = AMPAMP (putfield_resolved_4);
2762 break;
2765 case 8:
2767 jlong value = POPL();
2768 jobject obj = POPA();
2769 NULLCHECK(obj);
2770 *(jlong*) ((char*)obj + field_offset) = value;
2771 newinsn = AMPAMP (putfield_resolved_8);
2772 break;
2776 else
2778 jobject value = POPA();
2779 jobject obj = POPA();
2780 NULLCHECK(obj);
2781 *(jobject*) ((char*)obj + field_offset) = value;
2782 newinsn = AMPAMP (putfield_resolved_obj);
2785 #ifdef DIRECT_THREADED
2786 pc[-2].insn = newinsn;
2787 pc[-1].int_val = field_offset;
2788 #endif /* DIRECT_THREADED */
2790 NEXT_INSN;
2792 #ifdef DIRECT_THREADED
2793 putfield_resolved_1:
2795 jint val = POPI ();
2796 char *obj = (char *) POPA ();
2797 NULLCHECK (obj);
2798 *(jbyte *) (obj + INTVAL ()) = val;
2800 NEXT_INSN;
2802 putfield_resolved_2:
2804 jint val = POPI ();
2805 char *obj = (char *) POPA ();
2806 NULLCHECK (obj);
2807 *(jchar *) (obj + INTVAL ()) = val;
2809 NEXT_INSN;
2811 putfield_resolved_4:
2813 jint val = POPI ();
2814 char *obj = (char *) POPA ();
2815 NULLCHECK (obj);
2816 *(jint *) (obj + INTVAL ()) = val;
2818 NEXT_INSN;
2820 putfield_resolved_8:
2822 jlong val = POPL ();
2823 char *obj = (char *) POPA ();
2824 NULLCHECK (obj);
2825 *(jlong *) (obj + INTVAL ()) = val;
2827 NEXT_INSN;
2829 putfield_resolved_obj:
2831 jobject val = POPA ();
2832 char *obj = (char *) POPA ();
2833 NULLCHECK (obj);
2834 *(jobject *) (obj + INTVAL ()) = val;
2836 NEXT_INSN;
2837 #endif /* DIRECT_THREADED */
2839 insn_invokespecial:
2841 int index = GET2U ();
2843 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
2844 index)).rmethod;
2846 sp -= rmeth->stack_item_count;
2848 // We don't use NULLCHECK here because we can't rely on that
2849 // working for <init>. So instead we do an explicit test.
2850 if (! sp[0].o)
2851 throw new java::lang::NullPointerException;
2853 fun = (void (*)()) rmeth->method->ncode;
2855 #ifdef DIRECT_THREADED
2856 // Rewrite instruction so that we use a faster pre-resolved
2857 // method.
2858 pc[-2].insn = &&invokespecial_resolved;
2859 pc[-1].datum = rmeth;
2860 #endif /* DIRECT_THREADED */
2862 goto perform_invoke;
2864 #ifdef DIRECT_THREADED
2865 invokespecial_resolved:
2867 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2868 sp -= rmeth->stack_item_count;
2869 // We don't use NULLCHECK here because we can't rely on that
2870 // working for <init>. So instead we do an explicit test.
2871 if (! sp[0].o)
2872 throw new java::lang::NullPointerException;
2873 fun = (void (*)()) rmeth->method->ncode;
2875 goto perform_invoke;
2876 #endif /* DIRECT_THREADED */
2878 insn_invokestatic:
2880 int index = GET2U ();
2882 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
2883 index)).rmethod;
2885 sp -= rmeth->stack_item_count;
2887 fun = (void (*)()) rmeth->method->ncode;
2889 #ifdef DIRECT_THREADED
2890 // Rewrite instruction so that we use a faster pre-resolved
2891 // method.
2892 pc[-2].insn = &&invokestatic_resolved;
2893 pc[-1].datum = rmeth;
2894 #endif /* DIRECT_THREADED */
2896 goto perform_invoke;
2898 #ifdef DIRECT_THREADED
2899 invokestatic_resolved:
2901 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2902 sp -= rmeth->stack_item_count;
2903 fun = (void (*)()) rmeth->method->ncode;
2905 goto perform_invoke;
2906 #endif /* DIRECT_THREADED */
2908 insn_invokeinterface:
2910 int index = GET2U ();
2912 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
2913 index)).rmethod;
2915 sp -= rmeth->stack_item_count;
2917 jobject rcv = sp[0].o;
2919 NULLCHECK (rcv);
2921 fun = (void (*)())
2922 _Jv_LookupInterfaceMethod (rcv->getClass (),
2923 rmeth->method->name,
2924 rmeth->method->signature);
2926 #ifdef DIRECT_THREADED
2927 // Rewrite instruction so that we use a faster pre-resolved
2928 // method.
2929 pc[-2].insn = &&invokeinterface_resolved;
2930 pc[-1].datum = rmeth;
2931 #else
2932 // Skip dummy bytes.
2933 pc += 2;
2934 #endif /* DIRECT_THREADED */
2936 goto perform_invoke;
2938 #ifdef DIRECT_THREADED
2939 invokeinterface_resolved:
2941 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2942 sp -= rmeth->stack_item_count;
2943 jobject rcv = sp[0].o;
2944 NULLCHECK (rcv);
2945 fun = (void (*)())
2946 _Jv_LookupInterfaceMethod (rcv->getClass (),
2947 rmeth->method->name,
2948 rmeth->method->signature);
2950 goto perform_invoke;
2951 #endif /* DIRECT_THREADED */
2953 insn_new:
2955 int index = GET2U ();
2956 jclass klass = (_Jv_Linker::resolve_pool_entry (defining_class,
2957 index)).clazz;
2958 /* VM spec, section 3.11.5 */
2959 if ((klass->getModifiers() & Modifier::ABSTRACT)
2960 || klass->isInterface())
2961 throw new java::lang::InstantiationException;
2962 jobject res = _Jv_AllocObject (klass);
2963 PUSHA (res);
2965 #ifdef DIRECT_THREADED
2966 pc[-2].insn = &&new_resolved;
2967 pc[-1].datum = klass;
2968 #endif /* DIRECT_THREADED */
2970 NEXT_INSN;
2972 #ifdef DIRECT_THREADED
2973 new_resolved:
2975 jclass klass = (jclass) AVAL ();
2976 jobject res = _Jv_AllocObject (klass);
2977 PUSHA (res);
2979 NEXT_INSN;
2980 #endif /* DIRECT_THREADED */
2982 insn_newarray:
2984 int atype = GET1U ();
2985 int size = POPI();
2986 jobject result = _Jv_NewArray (atype, size);
2987 PUSHA (result);
2989 NEXT_INSN;
2991 insn_anewarray:
2993 int index = GET2U ();
2994 jclass klass = (_Jv_Linker::resolve_pool_entry (defining_class,
2995 index)).clazz;
2996 int size = POPI();
2997 jobject result = _Jv_NewObjectArray (size, klass, 0);
2998 PUSHA (result);
3000 #ifdef DIRECT_THREADED
3001 pc[-2].insn = &&anewarray_resolved;
3002 pc[-1].datum = klass;
3003 #endif /* DIRECT_THREADED */
3005 NEXT_INSN;
3007 #ifdef DIRECT_THREADED
3008 anewarray_resolved:
3010 jclass klass = (jclass) AVAL ();
3011 int size = POPI ();
3012 jobject result = _Jv_NewObjectArray (size, klass, 0);
3013 PUSHA (result);
3015 NEXT_INSN;
3016 #endif /* DIRECT_THREADED */
3018 insn_arraylength:
3020 __JArray *arr = (__JArray*)POPA();
3021 NULLARRAYCHECK (arr);
3022 PUSHI (arr->length);
3024 NEXT_INSN;
3026 insn_athrow:
3028 jobject value = POPA();
3029 throw static_cast<jthrowable>(value);
3031 NEXT_INSN;
3033 insn_checkcast:
3035 jobject value = POPA();
3036 jint index = GET2U ();
3037 jclass to = (_Jv_Linker::resolve_pool_entry (defining_class,
3038 index)).clazz;
3040 if (value != NULL && ! to->isInstance (value))
3041 throw new java::lang::ClassCastException (to->getName());
3043 PUSHA (value);
3045 #ifdef DIRECT_THREADED
3046 pc[-2].insn = &&checkcast_resolved;
3047 pc[-1].datum = to;
3048 #endif /* DIRECT_THREADED */
3050 NEXT_INSN;
3052 #ifdef DIRECT_THREADED
3053 checkcast_resolved:
3055 jobject value = POPA ();
3056 jclass to = (jclass) AVAL ();
3057 if (value != NULL && ! to->isInstance (value))
3058 throw new java::lang::ClassCastException (to->getName());
3059 PUSHA (value);
3061 NEXT_INSN;
3062 #endif /* DIRECT_THREADED */
3064 insn_instanceof:
3066 jobject value = POPA();
3067 jint index = GET2U ();
3068 jclass to = (_Jv_Linker::resolve_pool_entry (defining_class,
3069 index)).clazz;
3070 PUSHI (to->isInstance (value));
3072 #ifdef DIRECT_THREADED
3073 pc[-2].insn = &&instanceof_resolved;
3074 pc[-1].datum = to;
3075 #endif /* DIRECT_THREADED */
3077 NEXT_INSN;
3079 #ifdef DIRECT_THREADED
3080 instanceof_resolved:
3082 jobject value = POPA ();
3083 jclass to = (jclass) AVAL ();
3084 PUSHI (to->isInstance (value));
3086 NEXT_INSN;
3087 #endif /* DIRECT_THREADED */
3089 insn_monitorenter:
3091 jobject value = POPA();
3092 NULLCHECK(value);
3093 _Jv_MonitorEnter (value);
3095 NEXT_INSN;
3097 insn_monitorexit:
3099 jobject value = POPA();
3100 NULLCHECK(value);
3101 _Jv_MonitorExit (value);
3103 NEXT_INSN;
3105 insn_ifnull:
3107 jobject val = POPA();
3108 if (val == NULL)
3109 TAKE_GOTO;
3110 else
3111 SKIP_GOTO;
3113 NEXT_INSN;
3115 insn_ifnonnull:
3117 jobject val = POPA();
3118 if (val != NULL)
3119 TAKE_GOTO;
3120 else
3121 SKIP_GOTO;
3123 NEXT_INSN;
3125 insn_multianewarray:
3127 int kind_index = GET2U ();
3128 int dim = GET1U ();
3130 jclass type
3131 = (_Jv_Linker::resolve_pool_entry (defining_class,
3132 kind_index)).clazz;
3133 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3135 for (int i = dim - 1; i >= 0; i--)
3137 sizes[i] = POPI ();
3140 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3142 PUSHA (res);
3144 NEXT_INSN;
3146 #ifndef DIRECT_THREADED
3147 insn_wide:
3149 jint the_mod_op = get1u (pc++);
3150 jint wide = get2u (pc); pc += 2;
3152 switch (the_mod_op)
3154 case op_istore:
3155 STOREI (wide);
3156 NEXT_INSN;
3158 case op_fstore:
3159 STOREF (wide);
3160 NEXT_INSN;
3162 case op_astore:
3163 STOREA (wide);
3164 NEXT_INSN;
3166 case op_lload:
3167 LOADL (wide);
3168 NEXT_INSN;
3170 case op_dload:
3171 LOADD (wide);
3172 NEXT_INSN;
3174 case op_iload:
3175 LOADI (wide);
3176 NEXT_INSN;
3178 case op_fload:
3179 LOADF (wide);
3180 NEXT_INSN;
3182 case op_aload:
3183 LOADA (wide);
3184 NEXT_INSN;
3186 case op_lstore:
3187 STOREL (wide);
3188 NEXT_INSN;
3190 case op_dstore:
3191 STORED (wide);
3192 NEXT_INSN;
3194 case op_ret:
3195 pc = (unsigned char*) PEEKA (wide);
3196 NEXT_INSN;
3198 case op_iinc:
3200 jint amount = get2s (pc); pc += 2;
3201 jint value = PEEKI (wide);
3202 POKEI (wide, value+amount);
3204 NEXT_INSN;
3206 default:
3207 throw_internal_error ("illegal bytecode modified by wide");
3211 #endif /* DIRECT_THREADED */
3213 catch (java::lang::Throwable *ex)
3215 #ifdef DIRECT_THREADED
3216 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3217 #else
3218 int logical_pc = pc - 1 - bytecode ();
3219 #endif
3220 _Jv_InterpException *exc = exceptions ();
3221 jclass exc_class = ex->getClass ();
3223 for (int i = 0; i < exc_count; i++)
3225 if (PCVAL (exc[i].start_pc) <= logical_pc
3226 && logical_pc < PCVAL (exc[i].end_pc))
3228 #ifdef DIRECT_THREADED
3229 jclass handler = (jclass) exc[i].handler_type.p;
3230 #else
3231 jclass handler = NULL;
3232 if (exc[i].handler_type.i != 0)
3233 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3234 exc[i].handler_type.i)).clazz;
3235 #endif /* DIRECT_THREADED */
3237 if (handler == NULL || handler->isAssignableFrom (exc_class))
3239 #ifdef DIRECT_THREADED
3240 pc = (insn_slot *) exc[i].handler_pc.p;
3241 #else
3242 pc = bytecode () + exc[i].handler_pc.i;
3243 #endif /* DIRECT_THREADED */
3244 sp = stack;
3245 sp++->o = ex; // Push exception.
3246 NEXT_INSN;
3251 // No handler, so re-throw.
3252 throw ex;
3256 static void
3257 throw_internal_error (char *msg)
3259 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3262 static void
3263 throw_incompatible_class_change_error (jstring msg)
3265 throw new java::lang::IncompatibleClassChangeError (msg);
3268 #ifndef HANDLE_SEGV
3269 static java::lang::NullPointerException *null_pointer_exc;
3270 static void
3271 throw_null_pointer_exception ()
3273 if (null_pointer_exc == NULL)
3274 null_pointer_exc = new java::lang::NullPointerException;
3276 throw null_pointer_exc;
3278 #endif
3280 /** Do static initialization for fields with a constant initializer */
3281 void
3282 _Jv_InitField (jobject obj, jclass klass, int index)
3284 using namespace java::lang::reflect;
3286 if (obj != 0 && klass == 0)
3287 klass = obj->getClass ();
3289 if (!_Jv_IsInterpretedClass (klass))
3290 return;
3292 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3294 _Jv_Field * field = (&klass->fields[0]) + index;
3296 if (index > klass->field_count)
3297 throw_internal_error ("field out of range");
3299 int init = iclass->field_initializers[index];
3300 if (init == 0)
3301 return;
3303 _Jv_Constants *pool = &klass->constants;
3304 int tag = pool->tags[init];
3306 if (! field->isResolved ())
3307 throw_internal_error ("initializing unresolved field");
3309 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3310 throw_internal_error ("initializing non-static field with no object");
3312 void *addr = 0;
3314 if ((field->flags & Modifier::STATIC) != 0)
3315 addr = (void*) field->u.addr;
3316 else
3317 addr = (void*) (((char*)obj) + field->u.boffset);
3319 switch (tag)
3321 case JV_CONSTANT_String:
3323 jstring str;
3324 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3325 pool->data[init].string = str;
3326 pool->tags[init] = JV_CONSTANT_ResolvedString;
3328 /* fall through */
3330 case JV_CONSTANT_ResolvedString:
3331 if (! (field->type == &java::lang::String::class$
3332 || field->type == &java::lang::Class::class$))
3333 throw_class_format_error ("string initialiser to non-string field");
3335 *(jstring*)addr = pool->data[init].string;
3336 break;
3338 case JV_CONSTANT_Integer:
3340 int value = pool->data[init].i;
3342 if (field->type == JvPrimClass (boolean))
3343 *(jboolean*)addr = (jboolean)value;
3345 else if (field->type == JvPrimClass (byte))
3346 *(jbyte*)addr = (jbyte)value;
3348 else if (field->type == JvPrimClass (char))
3349 *(jchar*)addr = (jchar)value;
3351 else if (field->type == JvPrimClass (short))
3352 *(jshort*)addr = (jshort)value;
3354 else if (field->type == JvPrimClass (int))
3355 *(jint*)addr = (jint)value;
3357 else
3358 throw_class_format_error ("erroneous field initializer");
3360 break;
3362 case JV_CONSTANT_Long:
3363 if (field->type != JvPrimClass (long))
3364 throw_class_format_error ("erroneous field initializer");
3366 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3367 break;
3369 case JV_CONSTANT_Float:
3370 if (field->type != JvPrimClass (float))
3371 throw_class_format_error ("erroneous field initializer");
3373 *(jfloat*)addr = pool->data[init].f;
3374 break;
3376 case JV_CONSTANT_Double:
3377 if (field->type != JvPrimClass (double))
3378 throw_class_format_error ("erroneous field initializer");
3380 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3381 break;
3383 default:
3384 throw_class_format_error ("erroneous field initializer");
3388 inline static unsigned char*
3389 skip_one_type (unsigned char* ptr)
3391 int ch = *ptr++;
3393 while (ch == '[')
3395 ch = *ptr++;
3398 if (ch == 'L')
3400 do { ch = *ptr++; } while (ch != ';');
3403 return ptr;
3406 static ffi_type*
3407 get_ffi_type_from_signature (unsigned char* ptr)
3409 switch (*ptr)
3411 case 'L':
3412 case '[':
3413 return &ffi_type_pointer;
3414 break;
3416 case 'Z':
3417 // On some platforms a bool is a byte, on others an int.
3418 if (sizeof (jboolean) == sizeof (jbyte))
3419 return &ffi_type_sint8;
3420 else
3422 JvAssert (sizeof (jbyte) == sizeof (jint));
3423 return &ffi_type_sint32;
3425 break;
3427 case 'B':
3428 return &ffi_type_sint8;
3429 break;
3431 case 'C':
3432 return &ffi_type_uint16;
3433 break;
3435 case 'S':
3436 return &ffi_type_sint16;
3437 break;
3439 case 'I':
3440 return &ffi_type_sint32;
3441 break;
3443 case 'J':
3444 return &ffi_type_sint64;
3445 break;
3447 case 'F':
3448 return &ffi_type_float;
3449 break;
3451 case 'D':
3452 return &ffi_type_double;
3453 break;
3455 case 'V':
3456 return &ffi_type_void;
3457 break;
3460 throw_internal_error ("unknown type in signature");
3463 /* this function yields the number of actual arguments, that is, if the
3464 * function is non-static, then one is added to the number of elements
3465 * found in the signature */
3467 int
3468 _Jv_count_arguments (_Jv_Utf8Const *signature,
3469 jboolean staticp)
3471 unsigned char *ptr = (unsigned char*) signature->chars();
3472 int arg_count = staticp ? 0 : 1;
3474 /* first, count number of arguments */
3476 // skip '('
3477 ptr++;
3479 // count args
3480 while (*ptr != ')')
3482 ptr = skip_one_type (ptr);
3483 arg_count += 1;
3486 return arg_count;
3489 /* This beast will build a cif, given the signature. Memory for
3490 * the cif itself and for the argument types must be allocated by the
3491 * caller.
3494 static int
3495 init_cif (_Jv_Utf8Const* signature,
3496 int arg_count,
3497 jboolean staticp,
3498 ffi_cif *cif,
3499 ffi_type **arg_types,
3500 ffi_type **rtype_p)
3502 unsigned char *ptr = (unsigned char*) signature->chars();
3504 int arg_index = 0; // arg number
3505 int item_count = 0; // stack-item count
3507 // setup receiver
3508 if (!staticp)
3510 arg_types[arg_index++] = &ffi_type_pointer;
3511 item_count += 1;
3514 // skip '('
3515 ptr++;
3517 // assign arg types
3518 while (*ptr != ')')
3520 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3522 if (*ptr == 'J' || *ptr == 'D')
3523 item_count += 2;
3524 else
3525 item_count += 1;
3527 ptr = skip_one_type (ptr);
3530 // skip ')'
3531 ptr++;
3532 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3534 ptr = skip_one_type (ptr);
3535 if (ptr != (unsigned char*)signature->chars() + signature->len())
3536 throw_internal_error ("did not find end of signature");
3538 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3539 arg_count, rtype, arg_types) != FFI_OK)
3540 throw_internal_error ("ffi_prep_cif failed");
3542 if (rtype_p != NULL)
3543 *rtype_p = rtype;
3545 return item_count;
3548 #if FFI_NATIVE_RAW_API
3549 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3550 # define FFI_RAW_SIZE ffi_raw_size
3551 #else
3552 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3553 # define FFI_RAW_SIZE ffi_java_raw_size
3554 #endif
3556 /* we put this one here, and not in interpret.cc because it
3557 * calls the utility routines _Jv_count_arguments
3558 * which are static to this module. The following struct defines the
3559 * layout we use for the stubs, it's only used in the ncode method. */
3561 typedef struct {
3562 ffi_raw_closure closure;
3563 ffi_cif cif;
3564 ffi_type *arg_types[0];
3565 } ncode_closure;
3567 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3569 void *
3570 _Jv_InterpMethod::ncode ()
3572 using namespace java::lang::reflect;
3574 if (self->ncode != 0)
3575 return self->ncode;
3577 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3578 int arg_count = _Jv_count_arguments (self->signature, staticp);
3580 ncode_closure *closure =
3581 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3582 + arg_count * sizeof (ffi_type*));
3584 init_cif (self->signature,
3585 arg_count,
3586 staticp,
3587 &closure->cif,
3588 &closure->arg_types[0],
3589 NULL);
3591 ffi_closure_fun fun;
3593 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3595 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3597 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3599 if (staticp)
3600 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3601 else
3602 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3604 else
3606 if (staticp)
3607 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3608 else
3609 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3612 FFI_PREP_RAW_CLOSURE (&closure->closure,
3613 &closure->cif,
3614 fun,
3615 (void*)this);
3617 self->ncode = (void*)closure;
3618 return self->ncode;
3621 void *
3622 _Jv_JNIMethod::ncode ()
3624 using namespace java::lang::reflect;
3626 if (self->ncode != 0)
3627 return self->ncode;
3629 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3630 int arg_count = _Jv_count_arguments (self->signature, staticp);
3632 ncode_closure *closure =
3633 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3634 + arg_count * sizeof (ffi_type*));
3636 ffi_type *rtype;
3637 init_cif (self->signature,
3638 arg_count,
3639 staticp,
3640 &closure->cif,
3641 &closure->arg_types[0],
3642 &rtype);
3644 ffi_closure_fun fun;
3646 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3648 // Initialize the argument types and CIF that represent the actual
3649 // underlying JNI function.
3650 int extra_args = 1;
3651 if ((self->accflags & Modifier::STATIC))
3652 ++extra_args;
3653 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3654 * sizeof (ffi_type *));
3655 int offset = 0;
3656 jni_arg_types[offset++] = &ffi_type_pointer;
3657 if ((self->accflags & Modifier::STATIC))
3658 jni_arg_types[offset++] = &ffi_type_pointer;
3659 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3660 arg_count * sizeof (ffi_type *));
3662 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3663 extra_args + arg_count, rtype,
3664 jni_arg_types) != FFI_OK)
3665 throw_internal_error ("ffi_prep_cif failed for JNI function");
3667 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3669 // FIXME: for now we assume that all native methods for
3670 // interpreted code use JNI.
3671 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3673 FFI_PREP_RAW_CLOSURE (&closure->closure,
3674 &closure->cif,
3675 fun,
3676 (void*) this);
3678 self->ncode = (void *) closure;
3679 return self->ncode;
3682 static void
3683 throw_class_format_error (jstring msg)
3685 throw (msg
3686 ? new java::lang::ClassFormatError (msg)
3687 : new java::lang::ClassFormatError);
3690 static void
3691 throw_class_format_error (char *msg)
3693 throw_class_format_error (JvNewStringLatin1 (msg));
3698 void
3699 _Jv_InterpreterEngine::do_verify (jclass klass)
3701 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3702 for (int i = 0; i < klass->method_count; i++)
3704 using namespace java::lang::reflect;
3705 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3706 _Jv_ushort accflags = klass->methods[i].accflags;
3707 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3709 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3710 _Jv_VerifyMethod (im);
3715 void
3716 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3718 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3719 for (int i = 0; i < klass->method_count; i++)
3721 // Just skip abstract methods. This is particularly important
3722 // because we don't resize the interpreted_methods array when
3723 // miranda methods are added to it.
3724 if ((klass->methods[i].accflags
3725 & java::lang::reflect::Modifier::ABSTRACT)
3726 != 0)
3727 continue;
3729 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3731 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3732 != 0)
3734 // You might think we could use a virtual `ncode' method in
3735 // the _Jv_MethodBase and unify the native and non-native
3736 // cases. Well, we can't, because we don't allocate these
3737 // objects using `new', and thus they don't get a vtable.
3738 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3739 klass->methods[i].ncode = jnim->ncode ();
3741 else if (imeth != 0) // it could be abstract
3743 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3744 klass->methods[i].ncode = im->ncode ();
3749 void
3750 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3751 int static_size)
3753 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3755 char *static_data = (char *) _Jv_AllocBytes (static_size);
3756 memset (static_data, 0, static_size);
3758 for (int i = 0; i < klass->field_count; i++)
3760 _Jv_Field *field = &klass->fields[i];
3762 if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3764 field->u.addr = static_data + field->u.boffset;
3766 if (iclass->field_initializers[i] != 0)
3768 _Jv_Linker::resolve_field (field, klass->loader);
3769 _Jv_InitField (0, klass, i);
3774 // Now we don't need the field_initializers anymore, so let the
3775 // collector get rid of it.
3776 iclass->field_initializers = 0;
3779 _Jv_ResolvedMethod *
3780 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3781 jboolean staticp, jint vtable_index)
3783 int arg_count = _Jv_count_arguments (method->signature, staticp);
3785 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3786 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3787 + arg_count*sizeof (ffi_type*));
3789 result->stack_item_count
3790 = init_cif (method->signature,
3791 arg_count,
3792 staticp,
3793 &result->cif,
3794 &result->arg_types[0],
3795 NULL);
3797 result->vtable_index = vtable_index;
3798 result->method = method;
3799 result->klass = klass;
3801 return result;
3804 void
3805 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3807 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3808 for (int i = 0; i < klass->method_count; i++)
3810 // Just skip abstract methods. This is particularly important
3811 // because we don't resize the interpreted_methods array when
3812 // miranda methods are added to it.
3813 if ((klass->methods[i].accflags
3814 & java::lang::reflect::Modifier::ABSTRACT)
3815 != 0)
3816 continue;
3817 // Miranda method additions mean that the `methods' array moves.
3818 // We cache a pointer into this array, so we have to update.
3819 iclass->interpreted_methods[i]->self = &klass->methods[i];
3823 #endif // INTERPRETER