Fix changelog date.
[official-gcc.git] / libjava / interpret.cc
blob4c547b35dd6b5f3c34bdf1107f5e9dff61036f89
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/Thread.h>
40 #include <java-insns.h>
41 #include <java-signal.h>
42 #include <java/lang/ClassFormatError.h>
43 #include <execution.h>
44 #include <java/lang/reflect/Modifier.h>
46 #ifdef INTERPRETER
48 // Execution engine for interpreted code.
49 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
51 #include <stdlib.h>
53 using namespace gcj;
55 static void throw_internal_error (char *msg)
56 __attribute__ ((__noreturn__));
57 static void throw_incompatible_class_change_error (jstring msg)
58 __attribute__ ((__noreturn__));
59 #ifndef HANDLE_SEGV
60 static void throw_null_pointer_exception ()
61 __attribute__ ((__noreturn__));
62 #endif
64 static void throw_class_format_error (jstring msg)
65 __attribute__ ((__noreturn__));
66 static void throw_class_format_error (char *msg)
67 __attribute__ ((__noreturn__));
69 #ifdef DIRECT_THREADED
70 // Lock to ensure that methods are not compiled concurrently.
71 // We could use a finer-grained lock here, however it is not safe to use
72 // the Class monitor as user code in another thread could hold it.
73 static _Jv_Mutex_t compile_mutex;
75 void
76 _Jv_InitInterpreter()
78 _Jv_MutexInit (&compile_mutex);
80 #else
81 void _Jv_InitInterpreter() {}
82 #endif
84 extern "C" double __ieee754_fmod (double,double);
86 // This represents a single slot in the "compiled" form of the
87 // bytecode.
88 union insn_slot
90 // Address of code.
91 void *insn;
92 // An integer value used by an instruction.
93 jint int_val;
94 // A pointer value used by an instruction.
95 void *datum;
98 // The type of the PC depends on whether we're doing direct threading
99 // or a more ordinary bytecode interpreter.
100 #ifdef DIRECT_THREADED
101 typedef insn_slot *pc_t;
102 #else
103 typedef unsigned char *pc_t;
104 #endif
106 static inline void dupx (_Jv_word *sp, int n, int x)
108 // first "slide" n+x elements n to the right
109 int top = n-1;
110 for (int i = 0; i < n+x; i++)
112 sp[(top-i)] = sp[(top-i)-n];
115 // next, copy the n top elements, n+x down
116 for (int i = 0; i < n; i++)
118 sp[top-(n+x)-i] = sp[top-i];
123 // Used to convert from floating types to integral types.
124 template<typename TO, typename FROM>
125 static inline TO
126 convert (FROM val, TO min, TO max)
128 TO ret;
129 if (val >= (FROM) max)
130 ret = max;
131 else if (val <= (FROM) min)
132 ret = min;
133 else if (val != val)
134 ret = 0;
135 else
136 ret = (TO) val;
137 return ret;
140 #define PUSHA(V) (sp++)->o = (V)
141 #define PUSHI(V) (sp++)->i = (V)
142 #define PUSHF(V) (sp++)->f = (V)
143 #if SIZEOF_VOID_P == 8
144 # define PUSHL(V) (sp->l = (V), sp += 2)
145 # define PUSHD(V) (sp->d = (V), sp += 2)
146 #else
147 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
148 (sp++)->ia[0] = w2.ia[0]; \
149 (sp++)->ia[0] = w2.ia[1]; } while (0)
150 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
151 (sp++)->ia[0] = w2.ia[0]; \
152 (sp++)->ia[0] = w2.ia[1]; } while (0)
153 #endif
155 #define POPA() ((--sp)->o)
156 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
157 #define POPF() ((jfloat) (--sp)->f)
158 #if SIZEOF_VOID_P == 8
159 # define POPL() (sp -= 2, (jlong) sp->l)
160 # define POPD() (sp -= 2, (jdouble) sp->d)
161 #else
162 # define POPL() ({ _Jv_word2 w2; \
163 w2.ia[1] = (--sp)->ia[0]; \
164 w2.ia[0] = (--sp)->ia[0]; w2.l; })
165 # define POPD() ({ _Jv_word2 w2; \
166 w2.ia[1] = (--sp)->ia[0]; \
167 w2.ia[0] = (--sp)->ia[0]; w2.d; })
168 #endif
170 #define LOADA(I) (sp++)->o = locals[I].o
171 #define LOADI(I) (sp++)->i = locals[I].i
172 #define LOADF(I) (sp++)->f = locals[I].f
173 #if SIZEOF_VOID_P == 8
174 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
175 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
176 #else
177 # define LOADL(I) do { jint __idx = (I); \
178 (sp++)->ia[0] = locals[__idx].ia[0]; \
179 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
180 } while (0)
181 # define LOADD(I) LOADL(I)
182 #endif
184 #define STOREA(I) locals[I].o = (--sp)->o
185 #define STOREI(I) locals[I].i = (--sp)->i
186 #define STOREF(I) locals[I].f = (--sp)->f
187 #if SIZEOF_VOID_P == 8
188 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
189 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
190 #else
191 # define STOREL(I) do { jint __idx = (I); \
192 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
193 locals[__idx].ia[0] = (--sp)->ia[0]; \
194 } while (0)
195 # define STORED(I) STOREL(I)
196 #endif
198 #define PEEKI(I) (locals+(I))->i
199 #define PEEKA(I) (locals+(I))->o
201 #define POKEI(I,V) ((locals+(I))->i = (V))
204 #define BINOPI(OP) { \
205 jint value2 = POPI(); \
206 jint value1 = POPI(); \
207 PUSHI(value1 OP value2); \
210 #define BINOPF(OP) { \
211 jfloat value2 = POPF(); \
212 jfloat value1 = POPF(); \
213 PUSHF(value1 OP value2); \
216 #define BINOPL(OP) { \
217 jlong value2 = POPL(); \
218 jlong value1 = POPL(); \
219 PUSHL(value1 OP value2); \
222 #define BINOPD(OP) { \
223 jdouble value2 = POPD(); \
224 jdouble value1 = POPD(); \
225 PUSHD(value1 OP value2); \
228 static inline jint get1s(unsigned char* loc) {
229 return *(signed char*)loc;
232 static inline jint get1u(unsigned char* loc) {
233 return *loc;
236 static inline jint get2s(unsigned char* loc) {
237 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
240 static inline jint get2u(unsigned char* loc) {
241 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
244 static jint get4(unsigned char* loc) {
245 return (((jint)(loc[0])) << 24)
246 | (((jint)(loc[1])) << 16)
247 | (((jint)(loc[2])) << 8)
248 | (((jint)(loc[3])) << 0);
252 #ifdef HANDLE_SEGV
253 #define NULLCHECK(X)
254 #define NULLARRAYCHECK(X)
255 #else
256 #define NULLCHECK(X) \
257 do { if ((X)==NULL) throw_null_pointer_exception (); } while (0)
258 #define NULLARRAYCHECK(X) \
259 do { if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
260 #endif
262 #define ARRAYBOUNDSCHECK(array, index) \
263 do \
265 if (((unsigned) index) >= (unsigned) (array->length)) \
266 _Jv_ThrowBadArrayIndex (index); \
268 while (0)
270 void
271 _Jv_InterpMethod::run_normal (ffi_cif *,
272 void* ret,
273 ffi_raw * args,
274 void* __this)
276 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
277 _this->run (ret, args);
280 void
281 _Jv_InterpMethod::run_synch_object (ffi_cif *,
282 void* ret,
283 ffi_raw * args,
284 void* __this)
286 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
288 jobject rcv = (jobject) args[0].ptr;
289 JvSynchronize mutex (rcv);
291 _this->run (ret, args);
294 void
295 _Jv_InterpMethod::run_class (ffi_cif *,
296 void* ret,
297 ffi_raw * args,
298 void* __this)
300 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
301 _Jv_InitClass (_this->defining_class);
302 _this->run (ret, args);
305 void
306 _Jv_InterpMethod::run_synch_class (ffi_cif *,
307 void* ret,
308 ffi_raw * args,
309 void* __this)
311 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
313 jclass sync = _this->defining_class;
314 _Jv_InitClass (sync);
315 JvSynchronize mutex (sync);
317 _this->run (ret, args);
320 #ifdef DIRECT_THREADED
321 // "Compile" a method by turning it from bytecode to direct-threaded
322 // code.
323 void
324 _Jv_InterpMethod::compile (const void * const *insn_targets)
326 insn_slot *insns = NULL;
327 int next = 0;
328 unsigned char *codestart = bytecode ();
329 unsigned char *end = codestart + code_length;
330 _Jv_word *pool_data = defining_class->constants.data;
332 #define SET_ONE(Field, Value) \
333 do \
335 if (first_pass) \
336 ++next; \
337 else \
338 insns[next++].Field = Value; \
340 while (0)
342 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
343 #define SET_INT(Value) SET_ONE (int_val, Value)
344 #define SET_DATUM(Value) SET_ONE (datum, Value)
346 // Map from bytecode PC to slot in INSNS.
347 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
348 for (int i = 0; i < code_length; ++i)
349 pc_mapping[i] = -1;
351 for (int i = 0; i < 2; ++i)
353 jboolean first_pass = i == 0;
355 if (! first_pass)
357 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
358 next = 0;
361 unsigned char *pc = codestart;
362 while (pc < end)
364 int base_pc_val = pc - codestart;
365 if (first_pass)
366 pc_mapping[base_pc_val] = next;
368 java_opcode opcode = (java_opcode) *pc++;
369 // Just elide NOPs.
370 if (opcode == op_nop)
371 continue;
372 SET_INSN (insn_targets[opcode]);
374 switch (opcode)
376 case op_nop:
377 case op_aconst_null:
378 case op_iconst_m1:
379 case op_iconst_0:
380 case op_iconst_1:
381 case op_iconst_2:
382 case op_iconst_3:
383 case op_iconst_4:
384 case op_iconst_5:
385 case op_lconst_0:
386 case op_lconst_1:
387 case op_fconst_0:
388 case op_fconst_1:
389 case op_fconst_2:
390 case op_dconst_0:
391 case op_dconst_1:
392 case op_iload_0:
393 case op_iload_1:
394 case op_iload_2:
395 case op_iload_3:
396 case op_lload_0:
397 case op_lload_1:
398 case op_lload_2:
399 case op_lload_3:
400 case op_fload_0:
401 case op_fload_1:
402 case op_fload_2:
403 case op_fload_3:
404 case op_dload_0:
405 case op_dload_1:
406 case op_dload_2:
407 case op_dload_3:
408 case op_aload_0:
409 case op_aload_1:
410 case op_aload_2:
411 case op_aload_3:
412 case op_iaload:
413 case op_laload:
414 case op_faload:
415 case op_daload:
416 case op_aaload:
417 case op_baload:
418 case op_caload:
419 case op_saload:
420 case op_istore_0:
421 case op_istore_1:
422 case op_istore_2:
423 case op_istore_3:
424 case op_lstore_0:
425 case op_lstore_1:
426 case op_lstore_2:
427 case op_lstore_3:
428 case op_fstore_0:
429 case op_fstore_1:
430 case op_fstore_2:
431 case op_fstore_3:
432 case op_dstore_0:
433 case op_dstore_1:
434 case op_dstore_2:
435 case op_dstore_3:
436 case op_astore_0:
437 case op_astore_1:
438 case op_astore_2:
439 case op_astore_3:
440 case op_iastore:
441 case op_lastore:
442 case op_fastore:
443 case op_dastore:
444 case op_aastore:
445 case op_bastore:
446 case op_castore:
447 case op_sastore:
448 case op_pop:
449 case op_pop2:
450 case op_dup:
451 case op_dup_x1:
452 case op_dup_x2:
453 case op_dup2:
454 case op_dup2_x1:
455 case op_dup2_x2:
456 case op_swap:
457 case op_iadd:
458 case op_isub:
459 case op_imul:
460 case op_idiv:
461 case op_irem:
462 case op_ishl:
463 case op_ishr:
464 case op_iushr:
465 case op_iand:
466 case op_ior:
467 case op_ixor:
468 case op_ladd:
469 case op_lsub:
470 case op_lmul:
471 case op_ldiv:
472 case op_lrem:
473 case op_lshl:
474 case op_lshr:
475 case op_lushr:
476 case op_land:
477 case op_lor:
478 case op_lxor:
479 case op_fadd:
480 case op_fsub:
481 case op_fmul:
482 case op_fdiv:
483 case op_frem:
484 case op_dadd:
485 case op_dsub:
486 case op_dmul:
487 case op_ddiv:
488 case op_drem:
489 case op_ineg:
490 case op_i2b:
491 case op_i2c:
492 case op_i2s:
493 case op_lneg:
494 case op_fneg:
495 case op_dneg:
496 case op_i2l:
497 case op_i2f:
498 case op_i2d:
499 case op_l2i:
500 case op_l2f:
501 case op_l2d:
502 case op_f2i:
503 case op_f2l:
504 case op_f2d:
505 case op_d2i:
506 case op_d2l:
507 case op_d2f:
508 case op_lcmp:
509 case op_fcmpl:
510 case op_fcmpg:
511 case op_dcmpl:
512 case op_dcmpg:
513 case op_monitorenter:
514 case op_monitorexit:
515 case op_ireturn:
516 case op_lreturn:
517 case op_freturn:
518 case op_dreturn:
519 case op_areturn:
520 case op_return:
521 case op_athrow:
522 case op_arraylength:
523 // No argument, nothing else to do.
524 break;
526 case op_bipush:
527 SET_INT (get1s (pc));
528 ++pc;
529 break;
531 case op_ldc:
533 int index = get1u (pc);
534 ++pc;
535 SET_DATUM (pool_data[index].o);
537 break;
539 case op_ret:
540 case op_iload:
541 case op_lload:
542 case op_fload:
543 case op_dload:
544 case op_aload:
545 case op_istore:
546 case op_lstore:
547 case op_fstore:
548 case op_dstore:
549 case op_astore:
550 case op_newarray:
551 SET_INT (get1u (pc));
552 ++pc;
553 break;
555 case op_iinc:
556 SET_INT (get1u (pc));
557 SET_INT (get1s (pc + 1));
558 pc += 2;
559 break;
561 case op_ldc_w:
563 int index = get2u (pc);
564 pc += 2;
565 SET_DATUM (pool_data[index].o);
567 break;
569 case op_ldc2_w:
571 int index = get2u (pc);
572 pc += 2;
573 SET_DATUM (&pool_data[index]);
575 break;
577 case op_sipush:
578 SET_INT (get2s (pc));
579 pc += 2;
580 break;
582 case op_new:
583 case op_getstatic:
584 case op_getfield:
585 case op_putfield:
586 case op_putstatic:
587 case op_anewarray:
588 case op_instanceof:
589 case op_checkcast:
590 case op_invokespecial:
591 case op_invokestatic:
592 case op_invokevirtual:
593 SET_INT (get2u (pc));
594 pc += 2;
595 break;
597 case op_multianewarray:
598 SET_INT (get2u (pc));
599 SET_INT (get1u (pc + 2));
600 pc += 3;
601 break;
603 case op_jsr:
604 case op_ifeq:
605 case op_ifne:
606 case op_iflt:
607 case op_ifge:
608 case op_ifgt:
609 case op_ifle:
610 case op_if_icmpeq:
611 case op_if_icmpne:
612 case op_if_icmplt:
613 case op_if_icmpge:
614 case op_if_icmpgt:
615 case op_if_icmple:
616 case op_if_acmpeq:
617 case op_if_acmpne:
618 case op_ifnull:
619 case op_ifnonnull:
620 case op_goto:
622 int offset = get2s (pc);
623 pc += 2;
625 int new_pc = base_pc_val + offset;
627 bool orig_was_goto = opcode == op_goto;
629 // Thread jumps. We limit the loop count; this lets
630 // us avoid infinite loops if the bytecode contains
631 // such. `10' is arbitrary.
632 int count = 10;
633 while (codestart[new_pc] == op_goto && count-- > 0)
634 new_pc += get2s (&codestart[new_pc + 1]);
636 // If the jump takes us to a `return' instruction and
637 // the original branch was an unconditional goto, then
638 // we hoist the return.
639 opcode = (java_opcode) codestart[new_pc];
640 if (orig_was_goto
641 && (opcode == op_ireturn || opcode == op_lreturn
642 || opcode == op_freturn || opcode == op_dreturn
643 || opcode == op_areturn || opcode == op_return))
645 --next;
646 SET_INSN (insn_targets[opcode]);
648 else
649 SET_DATUM (&insns[pc_mapping[new_pc]]);
651 break;
653 case op_tableswitch:
655 while ((pc - codestart) % 4 != 0)
656 ++pc;
658 jint def = get4 (pc);
659 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
660 pc += 4;
662 int low = get4 (pc);
663 SET_INT (low);
664 pc += 4;
665 int high = get4 (pc);
666 SET_INT (high);
667 pc += 4;
669 for (int i = low; i <= high; ++i)
671 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
672 pc += 4;
675 break;
677 case op_lookupswitch:
679 while ((pc - codestart) % 4 != 0)
680 ++pc;
682 jint def = get4 (pc);
683 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
684 pc += 4;
686 jint npairs = get4 (pc);
687 pc += 4;
688 SET_INT (npairs);
690 while (npairs-- > 0)
692 jint match = get4 (pc);
693 jint offset = get4 (pc + 4);
694 SET_INT (match);
695 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
696 pc += 8;
699 break;
701 case op_invokeinterface:
703 jint index = get2u (pc);
704 pc += 2;
705 // We ignore the next two bytes.
706 pc += 2;
707 SET_INT (index);
709 break;
711 case op_wide:
713 opcode = (java_opcode) get1u (pc);
714 pc += 1;
715 jint val = get2u (pc);
716 pc += 2;
718 // We implement narrow and wide instructions using the
719 // same code in the interpreter. So we rewrite the
720 // instruction slot here.
721 if (! first_pass)
722 insns[next - 1].insn = (void *) insn_targets[opcode];
723 SET_INT (val);
725 if (opcode == op_iinc)
727 SET_INT (get2s (pc));
728 pc += 2;
731 break;
733 case op_jsr_w:
734 case op_goto_w:
736 jint offset = get4 (pc);
737 pc += 4;
738 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
740 break;
742 // Some "can't happen" cases that we include for
743 // error-checking purposes.
744 case op_putfield_1:
745 case op_putfield_2:
746 case op_putfield_4:
747 case op_putfield_8:
748 case op_putfield_a:
749 case op_putstatic_1:
750 case op_putstatic_2:
751 case op_putstatic_4:
752 case op_putstatic_8:
753 case op_putstatic_a:
754 case op_getfield_1:
755 case op_getfield_2s:
756 case op_getfield_2u:
757 case op_getfield_4:
758 case op_getfield_8:
759 case op_getfield_a:
760 case op_getstatic_1:
761 case op_getstatic_2s:
762 case op_getstatic_2u:
763 case op_getstatic_4:
764 case op_getstatic_8:
765 case op_getstatic_a:
766 default:
767 // Fail somehow.
768 break;
773 // Now update exceptions.
774 _Jv_InterpException *exc = exceptions ();
775 for (int i = 0; i < exc_count; ++i)
777 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
778 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
779 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
780 jclass handler
781 = (_Jv_Linker::resolve_pool_entry (defining_class,
782 exc[i].handler_type.i)).clazz;
783 exc[i].handler_type.p = handler;
786 prepared = insns;
788 #endif /* DIRECT_THREADED */
790 // These exist so that the stack-tracing code can find the boundaries
791 // of the interpreter.
792 void *_Jv_StartOfInterpreter;
793 void *_Jv_EndOfInterpreter;
794 extern "C" void *_Unwind_FindEnclosingFunction (void *pc);
796 void
797 _Jv_InterpMethod::run (void *retp, ffi_raw *args)
799 using namespace java::lang::reflect;
801 // Record the address of the start of this member function in
802 // _Jv_StartOfInterpreter. Such a write to a global variable
803 // without acquiring a lock is correct iff reads and writes of words
804 // in memory are atomic, but Java requires that anyway.
805 foo:
806 if (_Jv_StartOfInterpreter == NULL)
807 _Jv_StartOfInterpreter = _Unwind_FindEnclosingFunction (&&foo);
809 // FRAME_DESC registers this particular invocation as the top-most
810 // interpreter frame. This lets the stack tracing code (for
811 // Throwable) print information about the method being interpreted
812 // rather than about the interpreter itself. FRAME_DESC has a
813 // destructor so it cleans up automatically when the interpreter
814 // returns.
815 java::lang::Thread *thread = java::lang::Thread::currentThread();
816 _Jv_MethodChain frame_desc (this,
817 (_Jv_MethodChain **) &thread->interp_frame);
819 _Jv_word stack[max_stack];
820 _Jv_word *sp = stack;
822 _Jv_word locals[max_locals];
824 /* Go straight at it! the ffi raw format matches the internal
825 stack representation exactly. At least, that's the idea.
827 memcpy ((void*) locals, (void*) args, args_raw_size);
829 _Jv_word *pool_data = defining_class->constants.data;
831 /* These three are temporaries for common code used by several
832 instructions. */
833 void (*fun)();
834 _Jv_ResolvedMethod* rmeth;
835 int tmpval;
837 #define INSN_LABEL(op) &&insn_##op
839 static const void *const insn_target[] =
841 INSN_LABEL(nop),
842 INSN_LABEL(aconst_null),
843 INSN_LABEL(iconst_m1),
844 INSN_LABEL(iconst_0),
845 INSN_LABEL(iconst_1),
846 INSN_LABEL(iconst_2),
847 INSN_LABEL(iconst_3),
848 INSN_LABEL(iconst_4),
849 INSN_LABEL(iconst_5),
850 INSN_LABEL(lconst_0),
851 INSN_LABEL(lconst_1),
852 INSN_LABEL(fconst_0),
853 INSN_LABEL(fconst_1),
854 INSN_LABEL(fconst_2),
855 INSN_LABEL(dconst_0),
856 INSN_LABEL(dconst_1),
857 INSN_LABEL(bipush),
858 INSN_LABEL(sipush),
859 INSN_LABEL(ldc),
860 INSN_LABEL(ldc_w),
861 INSN_LABEL(ldc2_w),
862 INSN_LABEL(iload),
863 INSN_LABEL(lload),
864 INSN_LABEL(fload),
865 INSN_LABEL(dload),
866 INSN_LABEL(aload),
867 INSN_LABEL(iload_0),
868 INSN_LABEL(iload_1),
869 INSN_LABEL(iload_2),
870 INSN_LABEL(iload_3),
871 INSN_LABEL(lload_0),
872 INSN_LABEL(lload_1),
873 INSN_LABEL(lload_2),
874 INSN_LABEL(lload_3),
875 INSN_LABEL(fload_0),
876 INSN_LABEL(fload_1),
877 INSN_LABEL(fload_2),
878 INSN_LABEL(fload_3),
879 INSN_LABEL(dload_0),
880 INSN_LABEL(dload_1),
881 INSN_LABEL(dload_2),
882 INSN_LABEL(dload_3),
883 INSN_LABEL(aload_0),
884 INSN_LABEL(aload_1),
885 INSN_LABEL(aload_2),
886 INSN_LABEL(aload_3),
887 INSN_LABEL(iaload),
888 INSN_LABEL(laload),
889 INSN_LABEL(faload),
890 INSN_LABEL(daload),
891 INSN_LABEL(aaload),
892 INSN_LABEL(baload),
893 INSN_LABEL(caload),
894 INSN_LABEL(saload),
895 INSN_LABEL(istore),
896 INSN_LABEL(lstore),
897 INSN_LABEL(fstore),
898 INSN_LABEL(dstore),
899 INSN_LABEL(astore),
900 INSN_LABEL(istore_0),
901 INSN_LABEL(istore_1),
902 INSN_LABEL(istore_2),
903 INSN_LABEL(istore_3),
904 INSN_LABEL(lstore_0),
905 INSN_LABEL(lstore_1),
906 INSN_LABEL(lstore_2),
907 INSN_LABEL(lstore_3),
908 INSN_LABEL(fstore_0),
909 INSN_LABEL(fstore_1),
910 INSN_LABEL(fstore_2),
911 INSN_LABEL(fstore_3),
912 INSN_LABEL(dstore_0),
913 INSN_LABEL(dstore_1),
914 INSN_LABEL(dstore_2),
915 INSN_LABEL(dstore_3),
916 INSN_LABEL(astore_0),
917 INSN_LABEL(astore_1),
918 INSN_LABEL(astore_2),
919 INSN_LABEL(astore_3),
920 INSN_LABEL(iastore),
921 INSN_LABEL(lastore),
922 INSN_LABEL(fastore),
923 INSN_LABEL(dastore),
924 INSN_LABEL(aastore),
925 INSN_LABEL(bastore),
926 INSN_LABEL(castore),
927 INSN_LABEL(sastore),
928 INSN_LABEL(pop),
929 INSN_LABEL(pop2),
930 INSN_LABEL(dup),
931 INSN_LABEL(dup_x1),
932 INSN_LABEL(dup_x2),
933 INSN_LABEL(dup2),
934 INSN_LABEL(dup2_x1),
935 INSN_LABEL(dup2_x2),
936 INSN_LABEL(swap),
937 INSN_LABEL(iadd),
938 INSN_LABEL(ladd),
939 INSN_LABEL(fadd),
940 INSN_LABEL(dadd),
941 INSN_LABEL(isub),
942 INSN_LABEL(lsub),
943 INSN_LABEL(fsub),
944 INSN_LABEL(dsub),
945 INSN_LABEL(imul),
946 INSN_LABEL(lmul),
947 INSN_LABEL(fmul),
948 INSN_LABEL(dmul),
949 INSN_LABEL(idiv),
950 INSN_LABEL(ldiv),
951 INSN_LABEL(fdiv),
952 INSN_LABEL(ddiv),
953 INSN_LABEL(irem),
954 INSN_LABEL(lrem),
955 INSN_LABEL(frem),
956 INSN_LABEL(drem),
957 INSN_LABEL(ineg),
958 INSN_LABEL(lneg),
959 INSN_LABEL(fneg),
960 INSN_LABEL(dneg),
961 INSN_LABEL(ishl),
962 INSN_LABEL(lshl),
963 INSN_LABEL(ishr),
964 INSN_LABEL(lshr),
965 INSN_LABEL(iushr),
966 INSN_LABEL(lushr),
967 INSN_LABEL(iand),
968 INSN_LABEL(land),
969 INSN_LABEL(ior),
970 INSN_LABEL(lor),
971 INSN_LABEL(ixor),
972 INSN_LABEL(lxor),
973 INSN_LABEL(iinc),
974 INSN_LABEL(i2l),
975 INSN_LABEL(i2f),
976 INSN_LABEL(i2d),
977 INSN_LABEL(l2i),
978 INSN_LABEL(l2f),
979 INSN_LABEL(l2d),
980 INSN_LABEL(f2i),
981 INSN_LABEL(f2l),
982 INSN_LABEL(f2d),
983 INSN_LABEL(d2i),
984 INSN_LABEL(d2l),
985 INSN_LABEL(d2f),
986 INSN_LABEL(i2b),
987 INSN_LABEL(i2c),
988 INSN_LABEL(i2s),
989 INSN_LABEL(lcmp),
990 INSN_LABEL(fcmpl),
991 INSN_LABEL(fcmpg),
992 INSN_LABEL(dcmpl),
993 INSN_LABEL(dcmpg),
994 INSN_LABEL(ifeq),
995 INSN_LABEL(ifne),
996 INSN_LABEL(iflt),
997 INSN_LABEL(ifge),
998 INSN_LABEL(ifgt),
999 INSN_LABEL(ifle),
1000 INSN_LABEL(if_icmpeq),
1001 INSN_LABEL(if_icmpne),
1002 INSN_LABEL(if_icmplt),
1003 INSN_LABEL(if_icmpge),
1004 INSN_LABEL(if_icmpgt),
1005 INSN_LABEL(if_icmple),
1006 INSN_LABEL(if_acmpeq),
1007 INSN_LABEL(if_acmpne),
1008 INSN_LABEL(goto),
1009 INSN_LABEL(jsr),
1010 INSN_LABEL(ret),
1011 INSN_LABEL(tableswitch),
1012 INSN_LABEL(lookupswitch),
1013 INSN_LABEL(ireturn),
1014 INSN_LABEL(lreturn),
1015 INSN_LABEL(freturn),
1016 INSN_LABEL(dreturn),
1017 INSN_LABEL(areturn),
1018 INSN_LABEL(return),
1019 INSN_LABEL(getstatic),
1020 INSN_LABEL(putstatic),
1021 INSN_LABEL(getfield),
1022 INSN_LABEL(putfield),
1023 INSN_LABEL(invokevirtual),
1024 INSN_LABEL(invokespecial),
1025 INSN_LABEL(invokestatic),
1026 INSN_LABEL(invokeinterface),
1027 0, /* Unused. */
1028 INSN_LABEL(new),
1029 INSN_LABEL(newarray),
1030 INSN_LABEL(anewarray),
1031 INSN_LABEL(arraylength),
1032 INSN_LABEL(athrow),
1033 INSN_LABEL(checkcast),
1034 INSN_LABEL(instanceof),
1035 INSN_LABEL(monitorenter),
1036 INSN_LABEL(monitorexit),
1037 #ifdef DIRECT_THREADED
1038 0, // wide
1039 #else
1040 INSN_LABEL(wide),
1041 #endif
1042 INSN_LABEL(multianewarray),
1043 INSN_LABEL(ifnull),
1044 INSN_LABEL(ifnonnull),
1045 INSN_LABEL(goto_w),
1046 INSN_LABEL(jsr_w),
1050 pc_t pc;
1052 #ifdef DIRECT_THREADED
1054 #define NEXT_INSN goto *((pc++)->insn)
1055 #define INTVAL() ((pc++)->int_val)
1056 #define AVAL() ((pc++)->datum)
1058 #define GET1S() INTVAL ()
1059 #define GET2S() INTVAL ()
1060 #define GET1U() INTVAL ()
1061 #define GET2U() INTVAL ()
1062 #define AVAL1U() AVAL ()
1063 #define AVAL2U() AVAL ()
1064 #define AVAL2UP() AVAL ()
1065 #define SKIP_GOTO ++pc
1066 #define GOTO_VAL() (insn_slot *) pc->datum
1067 #define PCVAL(unionval) unionval.p
1068 #define AMPAMP(label) &&label
1070 // Compile if we must. NOTE: Double-check locking.
1071 if (prepared == NULL)
1073 _Jv_MutexLock (&compile_mutex);
1074 if (prepared == NULL)
1075 compile (insn_target);
1076 _Jv_MutexUnlock (&compile_mutex);
1078 pc = (insn_slot *) prepared;
1080 #else
1082 #define NEXT_INSN goto *(insn_target[*pc++])
1084 #define GET1S() get1s (pc++)
1085 #define GET2S() (pc += 2, get2s (pc- 2))
1086 #define GET1U() get1u (pc++)
1087 #define GET2U() (pc += 2, get2u (pc - 2))
1088 #define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
1089 #define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
1090 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1091 #define SKIP_GOTO pc += 2
1092 #define GOTO_VAL() pc - 1 + get2s (pc)
1093 #define PCVAL(unionval) unionval.i
1094 #define AMPAMP(label) NULL
1096 pc = bytecode ();
1098 #endif /* DIRECT_THREADED */
1100 #define TAKE_GOTO pc = GOTO_VAL ()
1104 // We keep nop around. It is used if we're interpreting the
1105 // bytecodes and not doing direct threading.
1106 insn_nop:
1107 NEXT_INSN;
1109 /* The first few instructions here are ordered according to their
1110 frequency, in the hope that this will improve code locality a
1111 little. */
1113 insn_aload_0: // 0x2a
1114 LOADA (0);
1115 NEXT_INSN;
1117 insn_iload: // 0x15
1118 LOADI (GET1U ());
1119 NEXT_INSN;
1121 insn_iload_1: // 0x1b
1122 LOADI (1);
1123 NEXT_INSN;
1125 insn_invokevirtual: // 0xb6
1127 int index = GET2U ();
1129 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1130 * value already is resolved. If we want to clutter up the
1131 * code here to gain a little performance, then we can check
1132 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1133 * directly. For now, I don't think it is worth it. */
1135 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
1136 index)).rmethod;
1138 sp -= rmeth->stack_item_count;
1139 // We don't use NULLCHECK here because we can't rely on that
1140 // working if the method is final. So instead we do an
1141 // explicit test.
1142 if (! sp[0].o)
1143 throw new java::lang::NullPointerException;
1145 if (rmeth->vtable_index == -1)
1147 // final methods do not appear in the vtable,
1148 // if it does not appear in the superclass.
1149 fun = (void (*)()) rmeth->method->ncode;
1151 else
1153 jobject rcv = sp[0].o;
1154 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1155 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1158 #ifdef DIRECT_THREADED
1159 // Rewrite instruction so that we use a faster pre-resolved
1160 // method.
1161 pc[-2].insn = &&invokevirtual_resolved;
1162 pc[-1].datum = rmeth;
1163 #endif /* DIRECT_THREADED */
1165 goto perform_invoke;
1167 #ifdef DIRECT_THREADED
1168 invokevirtual_resolved:
1170 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1171 sp -= rmeth->stack_item_count;
1172 // We don't use NULLCHECK here because we can't rely on that
1173 // working if the method is final. So instead we do an
1174 // explicit test.
1175 if (! sp[0].o)
1176 throw new java::lang::NullPointerException;
1178 if (rmeth->vtable_index == -1)
1180 // final methods do not appear in the vtable,
1181 // if it does not appear in the superclass.
1182 fun = (void (*)()) rmeth->method->ncode;
1184 else
1186 jobject rcv = sp[0].o;
1187 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1188 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1191 goto perform_invoke;
1192 #endif /* DIRECT_THREADED */
1194 perform_invoke:
1196 /* here goes the magic again... */
1197 ffi_cif *cif = &rmeth->cif;
1198 ffi_raw *raw = (ffi_raw*) sp;
1200 _Jv_value rvalue;
1202 #if FFI_NATIVE_RAW_API
1203 /* We assume that this is only implemented if it's correct */
1204 /* to use it here. On a 64 bit machine, it never is. */
1205 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1206 #else
1207 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1208 #endif
1210 int rtype = cif->rtype->type;
1212 /* the likelyhood of object, int, or void return is very high,
1213 * so those are checked before the switch */
1214 if (rtype == FFI_TYPE_POINTER)
1216 PUSHA (rvalue.object_value);
1218 else if (rtype == FFI_TYPE_SINT32)
1220 PUSHI (rvalue.int_value);
1222 else if (rtype == FFI_TYPE_VOID)
1224 /* skip */
1226 else
1228 switch (rtype)
1230 case FFI_TYPE_SINT8:
1231 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1232 break;
1234 case FFI_TYPE_SINT16:
1235 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1236 break;
1238 case FFI_TYPE_UINT16:
1239 PUSHI (rvalue.int_value & 0xffff);
1240 break;
1242 case FFI_TYPE_FLOAT:
1243 PUSHF (rvalue.float_value);
1244 break;
1246 case FFI_TYPE_DOUBLE:
1247 PUSHD (rvalue.double_value);
1248 break;
1250 case FFI_TYPE_SINT64:
1251 PUSHL (rvalue.long_value);
1252 break;
1254 default:
1255 throw_internal_error ("unknown return type in invokeXXX");
1259 NEXT_INSN;
1261 insn_aconst_null:
1262 PUSHA (NULL);
1263 NEXT_INSN;
1265 insn_iconst_m1:
1266 PUSHI (-1);
1267 NEXT_INSN;
1269 insn_iconst_0:
1270 PUSHI (0);
1271 NEXT_INSN;
1273 insn_iconst_1:
1274 PUSHI (1);
1275 NEXT_INSN;
1277 insn_iconst_2:
1278 PUSHI (2);
1279 NEXT_INSN;
1281 insn_iconst_3:
1282 PUSHI (3);
1283 NEXT_INSN;
1285 insn_iconst_4:
1286 PUSHI (4);
1287 NEXT_INSN;
1289 insn_iconst_5:
1290 PUSHI (5);
1291 NEXT_INSN;
1293 insn_lconst_0:
1294 PUSHL (0);
1295 NEXT_INSN;
1297 insn_lconst_1:
1298 PUSHL (1);
1299 NEXT_INSN;
1301 insn_fconst_0:
1302 PUSHF (0);
1303 NEXT_INSN;
1305 insn_fconst_1:
1306 PUSHF (1);
1307 NEXT_INSN;
1309 insn_fconst_2:
1310 PUSHF (2);
1311 NEXT_INSN;
1313 insn_dconst_0:
1314 PUSHD (0);
1315 NEXT_INSN;
1317 insn_dconst_1:
1318 PUSHD (1);
1319 NEXT_INSN;
1321 insn_bipush:
1322 // For direct threaded, bipush and sipush are the same.
1323 #ifndef DIRECT_THREADED
1324 PUSHI (GET1S ());
1325 NEXT_INSN;
1326 #endif /* DIRECT_THREADED */
1327 insn_sipush:
1328 PUSHI (GET2S ());
1329 NEXT_INSN;
1331 insn_ldc:
1332 // For direct threaded, ldc and ldc_w are the same.
1333 #ifndef DIRECT_THREADED
1334 PUSHA ((jobject) AVAL1U ());
1335 NEXT_INSN;
1336 #endif /* DIRECT_THREADED */
1337 insn_ldc_w:
1338 PUSHA ((jobject) AVAL2U ());
1339 NEXT_INSN;
1341 insn_ldc2_w:
1343 void *where = AVAL2UP ();
1344 memcpy (sp, where, 2*sizeof (_Jv_word));
1345 sp += 2;
1347 NEXT_INSN;
1349 insn_lload:
1350 LOADL (GET1U ());
1351 NEXT_INSN;
1353 insn_fload:
1354 LOADF (GET1U ());
1355 NEXT_INSN;
1357 insn_dload:
1358 LOADD (GET1U ());
1359 NEXT_INSN;
1361 insn_aload:
1362 LOADA (GET1U ());
1363 NEXT_INSN;
1365 insn_iload_0:
1366 LOADI (0);
1367 NEXT_INSN;
1369 insn_iload_2:
1370 LOADI (2);
1371 NEXT_INSN;
1373 insn_iload_3:
1374 LOADI (3);
1375 NEXT_INSN;
1377 insn_lload_0:
1378 LOADL (0);
1379 NEXT_INSN;
1381 insn_lload_1:
1382 LOADL (1);
1383 NEXT_INSN;
1385 insn_lload_2:
1386 LOADL (2);
1387 NEXT_INSN;
1389 insn_lload_3:
1390 LOADL (3);
1391 NEXT_INSN;
1393 insn_fload_0:
1394 LOADF (0);
1395 NEXT_INSN;
1397 insn_fload_1:
1398 LOADF (1);
1399 NEXT_INSN;
1401 insn_fload_2:
1402 LOADF (2);
1403 NEXT_INSN;
1405 insn_fload_3:
1406 LOADF (3);
1407 NEXT_INSN;
1409 insn_dload_0:
1410 LOADD (0);
1411 NEXT_INSN;
1413 insn_dload_1:
1414 LOADD (1);
1415 NEXT_INSN;
1417 insn_dload_2:
1418 LOADD (2);
1419 NEXT_INSN;
1421 insn_dload_3:
1422 LOADD (3);
1423 NEXT_INSN;
1425 insn_aload_1:
1426 LOADA(1);
1427 NEXT_INSN;
1429 insn_aload_2:
1430 LOADA(2);
1431 NEXT_INSN;
1433 insn_aload_3:
1434 LOADA(3);
1435 NEXT_INSN;
1437 insn_iaload:
1439 jint index = POPI();
1440 jintArray arr = (jintArray) POPA();
1441 NULLARRAYCHECK (arr);
1442 ARRAYBOUNDSCHECK (arr, index);
1443 PUSHI( elements(arr)[index] );
1445 NEXT_INSN;
1447 insn_laload:
1449 jint index = POPI();
1450 jlongArray arr = (jlongArray) POPA();
1451 NULLARRAYCHECK (arr);
1452 ARRAYBOUNDSCHECK (arr, index);
1453 PUSHL( elements(arr)[index] );
1455 NEXT_INSN;
1457 insn_faload:
1459 jint index = POPI();
1460 jfloatArray arr = (jfloatArray) POPA();
1461 NULLARRAYCHECK (arr);
1462 ARRAYBOUNDSCHECK (arr, index);
1463 PUSHF( elements(arr)[index] );
1465 NEXT_INSN;
1467 insn_daload:
1469 jint index = POPI();
1470 jdoubleArray arr = (jdoubleArray) POPA();
1471 NULLARRAYCHECK (arr);
1472 ARRAYBOUNDSCHECK (arr, index);
1473 PUSHD( elements(arr)[index] );
1475 NEXT_INSN;
1477 insn_aaload:
1479 jint index = POPI();
1480 jobjectArray arr = (jobjectArray) POPA();
1481 NULLARRAYCHECK (arr);
1482 ARRAYBOUNDSCHECK (arr, index);
1483 PUSHA( elements(arr)[index] );
1485 NEXT_INSN;
1487 insn_baload:
1489 jint index = POPI();
1490 jbyteArray arr = (jbyteArray) POPA();
1491 NULLARRAYCHECK (arr);
1492 ARRAYBOUNDSCHECK (arr, index);
1493 PUSHI( elements(arr)[index] );
1495 NEXT_INSN;
1497 insn_caload:
1499 jint index = POPI();
1500 jcharArray arr = (jcharArray) POPA();
1501 NULLARRAYCHECK (arr);
1502 ARRAYBOUNDSCHECK (arr, index);
1503 PUSHI( elements(arr)[index] );
1505 NEXT_INSN;
1507 insn_saload:
1509 jint index = POPI();
1510 jshortArray arr = (jshortArray) POPA();
1511 NULLARRAYCHECK (arr);
1512 ARRAYBOUNDSCHECK (arr, index);
1513 PUSHI( elements(arr)[index] );
1515 NEXT_INSN;
1517 insn_istore:
1518 STOREI (GET1U ());
1519 NEXT_INSN;
1521 insn_lstore:
1522 STOREL (GET1U ());
1523 NEXT_INSN;
1525 insn_fstore:
1526 STOREF (GET1U ());
1527 NEXT_INSN;
1529 insn_dstore:
1530 STORED (GET1U ());
1531 NEXT_INSN;
1533 insn_astore:
1534 STOREA (GET1U ());
1535 NEXT_INSN;
1537 insn_istore_0:
1538 STOREI (0);
1539 NEXT_INSN;
1541 insn_istore_1:
1542 STOREI (1);
1543 NEXT_INSN;
1545 insn_istore_2:
1546 STOREI (2);
1547 NEXT_INSN;
1549 insn_istore_3:
1550 STOREI (3);
1551 NEXT_INSN;
1553 insn_lstore_0:
1554 STOREL (0);
1555 NEXT_INSN;
1557 insn_lstore_1:
1558 STOREL (1);
1559 NEXT_INSN;
1561 insn_lstore_2:
1562 STOREL (2);
1563 NEXT_INSN;
1565 insn_lstore_3:
1566 STOREL (3);
1567 NEXT_INSN;
1569 insn_fstore_0:
1570 STOREF (0);
1571 NEXT_INSN;
1573 insn_fstore_1:
1574 STOREF (1);
1575 NEXT_INSN;
1577 insn_fstore_2:
1578 STOREF (2);
1579 NEXT_INSN;
1581 insn_fstore_3:
1582 STOREF (3);
1583 NEXT_INSN;
1585 insn_dstore_0:
1586 STORED (0);
1587 NEXT_INSN;
1589 insn_dstore_1:
1590 STORED (1);
1591 NEXT_INSN;
1593 insn_dstore_2:
1594 STORED (2);
1595 NEXT_INSN;
1597 insn_dstore_3:
1598 STORED (3);
1599 NEXT_INSN;
1601 insn_astore_0:
1602 STOREA(0);
1603 NEXT_INSN;
1605 insn_astore_1:
1606 STOREA(1);
1607 NEXT_INSN;
1609 insn_astore_2:
1610 STOREA(2);
1611 NEXT_INSN;
1613 insn_astore_3:
1614 STOREA(3);
1615 NEXT_INSN;
1617 insn_iastore:
1619 jint value = POPI();
1620 jint index = POPI();
1621 jintArray arr = (jintArray) POPA();
1622 NULLARRAYCHECK (arr);
1623 ARRAYBOUNDSCHECK (arr, index);
1624 elements(arr)[index] = value;
1626 NEXT_INSN;
1628 insn_lastore:
1630 jlong value = POPL();
1631 jint index = POPI();
1632 jlongArray arr = (jlongArray) POPA();
1633 NULLARRAYCHECK (arr);
1634 ARRAYBOUNDSCHECK (arr, index);
1635 elements(arr)[index] = value;
1637 NEXT_INSN;
1639 insn_fastore:
1641 jfloat value = POPF();
1642 jint index = POPI();
1643 jfloatArray arr = (jfloatArray) POPA();
1644 NULLARRAYCHECK (arr);
1645 ARRAYBOUNDSCHECK (arr, index);
1646 elements(arr)[index] = value;
1648 NEXT_INSN;
1650 insn_dastore:
1652 jdouble value = POPD();
1653 jint index = POPI();
1654 jdoubleArray arr = (jdoubleArray) POPA();
1655 NULLARRAYCHECK (arr);
1656 ARRAYBOUNDSCHECK (arr, index);
1657 elements(arr)[index] = value;
1659 NEXT_INSN;
1661 insn_aastore:
1663 jobject value = POPA();
1664 jint index = POPI();
1665 jobjectArray arr = (jobjectArray) POPA();
1666 NULLARRAYCHECK (arr);
1667 ARRAYBOUNDSCHECK (arr, index);
1668 _Jv_CheckArrayStore (arr, value);
1669 elements(arr)[index] = value;
1671 NEXT_INSN;
1673 insn_bastore:
1675 jbyte value = (jbyte) POPI();
1676 jint index = POPI();
1677 jbyteArray arr = (jbyteArray) POPA();
1678 NULLARRAYCHECK (arr);
1679 ARRAYBOUNDSCHECK (arr, index);
1680 elements(arr)[index] = value;
1682 NEXT_INSN;
1684 insn_castore:
1686 jchar value = (jchar) POPI();
1687 jint index = POPI();
1688 jcharArray arr = (jcharArray) POPA();
1689 NULLARRAYCHECK (arr);
1690 ARRAYBOUNDSCHECK (arr, index);
1691 elements(arr)[index] = value;
1693 NEXT_INSN;
1695 insn_sastore:
1697 jshort value = (jshort) POPI();
1698 jint index = POPI();
1699 jshortArray arr = (jshortArray) POPA();
1700 NULLARRAYCHECK (arr);
1701 ARRAYBOUNDSCHECK (arr, index);
1702 elements(arr)[index] = value;
1704 NEXT_INSN;
1706 insn_pop:
1707 sp -= 1;
1708 NEXT_INSN;
1710 insn_pop2:
1711 sp -= 2;
1712 NEXT_INSN;
1714 insn_dup:
1715 sp[0] = sp[-1];
1716 sp += 1;
1717 NEXT_INSN;
1719 insn_dup_x1:
1720 dupx (sp, 1, 1); sp+=1;
1721 NEXT_INSN;
1723 insn_dup_x2:
1724 dupx (sp, 1, 2); sp+=1;
1725 NEXT_INSN;
1727 insn_dup2:
1728 sp[0] = sp[-2];
1729 sp[1] = sp[-1];
1730 sp += 2;
1731 NEXT_INSN;
1733 insn_dup2_x1:
1734 dupx (sp, 2, 1); sp+=2;
1735 NEXT_INSN;
1737 insn_dup2_x2:
1738 dupx (sp, 2, 2); sp+=2;
1739 NEXT_INSN;
1741 insn_swap:
1743 jobject tmp1 = POPA();
1744 jobject tmp2 = POPA();
1745 PUSHA (tmp1);
1746 PUSHA (tmp2);
1748 NEXT_INSN;
1750 insn_iadd:
1751 BINOPI(+);
1752 NEXT_INSN;
1754 insn_ladd:
1755 BINOPL(+);
1756 NEXT_INSN;
1758 insn_fadd:
1759 BINOPF(+);
1760 NEXT_INSN;
1762 insn_dadd:
1763 BINOPD(+);
1764 NEXT_INSN;
1766 insn_isub:
1767 BINOPI(-);
1768 NEXT_INSN;
1770 insn_lsub:
1771 BINOPL(-);
1772 NEXT_INSN;
1774 insn_fsub:
1775 BINOPF(-);
1776 NEXT_INSN;
1778 insn_dsub:
1779 BINOPD(-);
1780 NEXT_INSN;
1782 insn_imul:
1783 BINOPI(*);
1784 NEXT_INSN;
1786 insn_lmul:
1787 BINOPL(*);
1788 NEXT_INSN;
1790 insn_fmul:
1791 BINOPF(*);
1792 NEXT_INSN;
1794 insn_dmul:
1795 BINOPD(*);
1796 NEXT_INSN;
1798 insn_idiv:
1800 jint value2 = POPI();
1801 jint value1 = POPI();
1802 jint res = _Jv_divI (value1, value2);
1803 PUSHI (res);
1805 NEXT_INSN;
1807 insn_ldiv:
1809 jlong value2 = POPL();
1810 jlong value1 = POPL();
1811 jlong res = _Jv_divJ (value1, value2);
1812 PUSHL (res);
1814 NEXT_INSN;
1816 insn_fdiv:
1818 jfloat value2 = POPF();
1819 jfloat value1 = POPF();
1820 jfloat res = value1 / value2;
1821 PUSHF (res);
1823 NEXT_INSN;
1825 insn_ddiv:
1827 jdouble value2 = POPD();
1828 jdouble value1 = POPD();
1829 jdouble res = value1 / value2;
1830 PUSHD (res);
1832 NEXT_INSN;
1834 insn_irem:
1836 jint value2 = POPI();
1837 jint value1 = POPI();
1838 jint res = _Jv_remI (value1, value2);
1839 PUSHI (res);
1841 NEXT_INSN;
1843 insn_lrem:
1845 jlong value2 = POPL();
1846 jlong value1 = POPL();
1847 jlong res = _Jv_remJ (value1, value2);
1848 PUSHL (res);
1850 NEXT_INSN;
1852 insn_frem:
1854 jfloat value2 = POPF();
1855 jfloat value1 = POPF();
1856 jfloat res = __ieee754_fmod (value1, value2);
1857 PUSHF (res);
1859 NEXT_INSN;
1861 insn_drem:
1863 jdouble value2 = POPD();
1864 jdouble value1 = POPD();
1865 jdouble res = __ieee754_fmod (value1, value2);
1866 PUSHD (res);
1868 NEXT_INSN;
1870 insn_ineg:
1872 jint value = POPI();
1873 PUSHI (value * -1);
1875 NEXT_INSN;
1877 insn_lneg:
1879 jlong value = POPL();
1880 PUSHL (value * -1);
1882 NEXT_INSN;
1884 insn_fneg:
1886 jfloat value = POPF();
1887 PUSHF (value * -1);
1889 NEXT_INSN;
1891 insn_dneg:
1893 jdouble value = POPD();
1894 PUSHD (value * -1);
1896 NEXT_INSN;
1898 insn_ishl:
1900 jint shift = (POPI() & 0x1f);
1901 jint value = POPI();
1902 PUSHI (value << shift);
1904 NEXT_INSN;
1906 insn_lshl:
1908 jint shift = (POPI() & 0x3f);
1909 jlong value = POPL();
1910 PUSHL (value << shift);
1912 NEXT_INSN;
1914 insn_ishr:
1916 jint shift = (POPI() & 0x1f);
1917 jint value = POPI();
1918 PUSHI (value >> shift);
1920 NEXT_INSN;
1922 insn_lshr:
1924 jint shift = (POPI() & 0x3f);
1925 jlong value = POPL();
1926 PUSHL (value >> shift);
1928 NEXT_INSN;
1930 insn_iushr:
1932 jint shift = (POPI() & 0x1f);
1933 _Jv_uint value = (_Jv_uint) POPI();
1934 PUSHI ((jint) (value >> shift));
1936 NEXT_INSN;
1938 insn_lushr:
1940 jint shift = (POPI() & 0x3f);
1941 _Jv_ulong value = (_Jv_ulong) POPL();
1942 PUSHL ((jlong) (value >> shift));
1944 NEXT_INSN;
1946 insn_iand:
1947 BINOPI (&);
1948 NEXT_INSN;
1950 insn_land:
1951 BINOPL (&);
1952 NEXT_INSN;
1954 insn_ior:
1955 BINOPI (|);
1956 NEXT_INSN;
1958 insn_lor:
1959 BINOPL (|);
1960 NEXT_INSN;
1962 insn_ixor:
1963 BINOPI (^);
1964 NEXT_INSN;
1966 insn_lxor:
1967 BINOPL (^);
1968 NEXT_INSN;
1970 insn_iinc:
1972 jint index = GET1U ();
1973 jint amount = GET1S ();
1974 locals[index].i += amount;
1976 NEXT_INSN;
1978 insn_i2l:
1979 {jlong value = POPI(); PUSHL (value);}
1980 NEXT_INSN;
1982 insn_i2f:
1983 {jfloat value = POPI(); PUSHF (value);}
1984 NEXT_INSN;
1986 insn_i2d:
1987 {jdouble value = POPI(); PUSHD (value);}
1988 NEXT_INSN;
1990 insn_l2i:
1991 {jint value = POPL(); PUSHI (value);}
1992 NEXT_INSN;
1994 insn_l2f:
1995 {jfloat value = POPL(); PUSHF (value);}
1996 NEXT_INSN;
1998 insn_l2d:
1999 {jdouble value = POPL(); PUSHD (value);}
2000 NEXT_INSN;
2002 insn_f2i:
2004 using namespace java::lang;
2005 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2006 PUSHI(value);
2008 NEXT_INSN;
2010 insn_f2l:
2012 using namespace java::lang;
2013 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2014 PUSHL(value);
2016 NEXT_INSN;
2018 insn_f2d:
2019 { jdouble value = POPF (); PUSHD(value); }
2020 NEXT_INSN;
2022 insn_d2i:
2024 using namespace java::lang;
2025 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2026 PUSHI(value);
2028 NEXT_INSN;
2030 insn_d2l:
2032 using namespace java::lang;
2033 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2034 PUSHL(value);
2036 NEXT_INSN;
2038 insn_d2f:
2039 { jfloat value = POPD (); PUSHF(value); }
2040 NEXT_INSN;
2042 insn_i2b:
2043 { jbyte value = POPI (); PUSHI(value); }
2044 NEXT_INSN;
2046 insn_i2c:
2047 { jchar value = POPI (); PUSHI(value); }
2048 NEXT_INSN;
2050 insn_i2s:
2051 { jshort value = POPI (); PUSHI(value); }
2052 NEXT_INSN;
2054 insn_lcmp:
2056 jlong value2 = POPL ();
2057 jlong value1 = POPL ();
2058 if (value1 > value2)
2059 { PUSHI (1); }
2060 else if (value1 == value2)
2061 { PUSHI (0); }
2062 else
2063 { PUSHI (-1); }
2065 NEXT_INSN;
2067 insn_fcmpl:
2068 tmpval = -1;
2069 goto fcmp;
2071 insn_fcmpg:
2072 tmpval = 1;
2074 fcmp:
2076 jfloat value2 = POPF ();
2077 jfloat value1 = POPF ();
2078 if (value1 > value2)
2079 PUSHI (1);
2080 else if (value1 == value2)
2081 PUSHI (0);
2082 else if (value1 < value2)
2083 PUSHI (-1);
2084 else
2085 PUSHI (tmpval);
2087 NEXT_INSN;
2089 insn_dcmpl:
2090 tmpval = -1;
2091 goto dcmp;
2093 insn_dcmpg:
2094 tmpval = 1;
2096 dcmp:
2098 jdouble value2 = POPD ();
2099 jdouble value1 = POPD ();
2100 if (value1 > value2)
2101 PUSHI (1);
2102 else if (value1 == value2)
2103 PUSHI (0);
2104 else if (value1 < value2)
2105 PUSHI (-1);
2106 else
2107 PUSHI (tmpval);
2109 NEXT_INSN;
2111 insn_ifeq:
2113 if (POPI() == 0)
2114 TAKE_GOTO;
2115 else
2116 SKIP_GOTO;
2118 NEXT_INSN;
2120 insn_ifne:
2122 if (POPI() != 0)
2123 TAKE_GOTO;
2124 else
2125 SKIP_GOTO;
2127 NEXT_INSN;
2129 insn_iflt:
2131 if (POPI() < 0)
2132 TAKE_GOTO;
2133 else
2134 SKIP_GOTO;
2136 NEXT_INSN;
2138 insn_ifge:
2140 if (POPI() >= 0)
2141 TAKE_GOTO;
2142 else
2143 SKIP_GOTO;
2145 NEXT_INSN;
2147 insn_ifgt:
2149 if (POPI() > 0)
2150 TAKE_GOTO;
2151 else
2152 SKIP_GOTO;
2154 NEXT_INSN;
2156 insn_ifle:
2158 if (POPI() <= 0)
2159 TAKE_GOTO;
2160 else
2161 SKIP_GOTO;
2163 NEXT_INSN;
2165 insn_if_icmpeq:
2167 jint value2 = POPI();
2168 jint value1 = POPI();
2169 if (value1 == value2)
2170 TAKE_GOTO;
2171 else
2172 SKIP_GOTO;
2174 NEXT_INSN;
2176 insn_if_icmpne:
2178 jint value2 = POPI();
2179 jint value1 = POPI();
2180 if (value1 != value2)
2181 TAKE_GOTO;
2182 else
2183 SKIP_GOTO;
2185 NEXT_INSN;
2187 insn_if_icmplt:
2189 jint value2 = POPI();
2190 jint value1 = POPI();
2191 if (value1 < value2)
2192 TAKE_GOTO;
2193 else
2194 SKIP_GOTO;
2196 NEXT_INSN;
2198 insn_if_icmpge:
2200 jint value2 = POPI();
2201 jint value1 = POPI();
2202 if (value1 >= value2)
2203 TAKE_GOTO;
2204 else
2205 SKIP_GOTO;
2207 NEXT_INSN;
2209 insn_if_icmpgt:
2211 jint value2 = POPI();
2212 jint value1 = POPI();
2213 if (value1 > value2)
2214 TAKE_GOTO;
2215 else
2216 SKIP_GOTO;
2218 NEXT_INSN;
2220 insn_if_icmple:
2222 jint value2 = POPI();
2223 jint value1 = POPI();
2224 if (value1 <= value2)
2225 TAKE_GOTO;
2226 else
2227 SKIP_GOTO;
2229 NEXT_INSN;
2231 insn_if_acmpeq:
2233 jobject value2 = POPA();
2234 jobject value1 = POPA();
2235 if (value1 == value2)
2236 TAKE_GOTO;
2237 else
2238 SKIP_GOTO;
2240 NEXT_INSN;
2242 insn_if_acmpne:
2244 jobject value2 = POPA();
2245 jobject value1 = POPA();
2246 if (value1 != value2)
2247 TAKE_GOTO;
2248 else
2249 SKIP_GOTO;
2251 NEXT_INSN;
2253 insn_goto_w:
2254 #ifndef DIRECT_THREADED
2255 // For direct threaded, goto and goto_w are the same.
2256 pc = pc - 1 + get4 (pc);
2257 NEXT_INSN;
2258 #endif /* DIRECT_THREADED */
2259 insn_goto:
2260 TAKE_GOTO;
2261 NEXT_INSN;
2263 insn_jsr_w:
2264 #ifndef DIRECT_THREADED
2265 // For direct threaded, jsr and jsr_w are the same.
2267 pc_t next = pc - 1 + get4 (pc);
2268 pc += 4;
2269 PUSHA ((jobject) pc);
2270 pc = next;
2272 NEXT_INSN;
2273 #endif /* DIRECT_THREADED */
2274 insn_jsr:
2276 pc_t next = GOTO_VAL();
2277 SKIP_GOTO;
2278 PUSHA ((jobject) pc);
2279 pc = next;
2281 NEXT_INSN;
2283 insn_ret:
2285 jint index = GET1U ();
2286 pc = (pc_t) PEEKA (index);
2288 NEXT_INSN;
2290 insn_tableswitch:
2292 #ifdef DIRECT_THREADED
2293 void *def = (pc++)->datum;
2295 int index = POPI();
2297 jint low = INTVAL ();
2298 jint high = INTVAL ();
2300 if (index < low || index > high)
2301 pc = (insn_slot *) def;
2302 else
2303 pc = (insn_slot *) ((pc + index - low)->datum);
2304 #else
2305 pc_t base_pc = pc - 1;
2306 int index = POPI ();
2308 pc_t base = (pc_t) bytecode ();
2309 while ((pc - base) % 4 != 0)
2310 ++pc;
2312 jint def = get4 (pc);
2313 jint low = get4 (pc + 4);
2314 jint high = get4 (pc + 8);
2315 if (index < low || index > high)
2316 pc = base_pc + def;
2317 else
2318 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2319 #endif /* DIRECT_THREADED */
2321 NEXT_INSN;
2323 insn_lookupswitch:
2325 #ifdef DIRECT_THREADED
2326 void *def = (pc++)->insn;
2328 int index = POPI();
2330 jint npairs = INTVAL ();
2332 int max = npairs - 1;
2333 int min = 0;
2335 // Simple binary search...
2336 while (min < max)
2338 int half = (min + max) / 2;
2339 int match = pc[2 * half].int_val;
2341 if (index == match)
2343 // Found it.
2344 pc = (insn_slot *) pc[2 * half + 1].datum;
2345 NEXT_INSN;
2347 else if (index < match)
2348 // We can use HALF - 1 here because we check again on
2349 // loop exit.
2350 max = half - 1;
2351 else
2352 // We can use HALF + 1 here because we check again on
2353 // loop exit.
2354 min = half + 1;
2356 if (index == pc[2 * min].int_val)
2357 pc = (insn_slot *) pc[2 * min + 1].datum;
2358 else
2359 pc = (insn_slot *) def;
2360 #else
2361 unsigned char *base_pc = pc-1;
2362 int index = POPI();
2364 unsigned char* base = bytecode ();
2365 while ((pc-base) % 4 != 0)
2366 ++pc;
2368 jint def = get4 (pc);
2369 jint npairs = get4 (pc+4);
2371 int max = npairs-1;
2372 int min = 0;
2374 // Simple binary search...
2375 while (min < max)
2377 int half = (min+max)/2;
2378 int match = get4 (pc+ 4*(2 + 2*half));
2380 if (index == match)
2381 min = max = half;
2382 else if (index < match)
2383 // We can use HALF - 1 here because we check again on
2384 // loop exit.
2385 max = half - 1;
2386 else
2387 // We can use HALF + 1 here because we check again on
2388 // loop exit.
2389 min = half + 1;
2392 if (index == get4 (pc+ 4*(2 + 2*min)))
2393 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2394 else
2395 pc = base_pc + def;
2396 #endif /* DIRECT_THREADED */
2398 NEXT_INSN;
2400 insn_areturn:
2401 *(jobject *) retp = POPA ();
2402 return;
2404 insn_lreturn:
2405 *(jlong *) retp = POPL ();
2406 return;
2408 insn_freturn:
2409 *(jfloat *) retp = POPF ();
2410 return;
2412 insn_dreturn:
2413 *(jdouble *) retp = POPD ();
2414 return;
2416 insn_ireturn:
2417 *(jint *) retp = POPI ();
2418 return;
2420 insn_return:
2421 return;
2423 insn_getstatic:
2425 jint fieldref_index = GET2U ();
2426 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2427 _Jv_Field *field = pool_data[fieldref_index].field;
2429 if ((field->flags & Modifier::STATIC) == 0)
2430 throw_incompatible_class_change_error
2431 (JvNewStringLatin1 ("field no longer static"));
2433 jclass type = field->type;
2435 // We rewrite the instruction once we discover what it refers
2436 // to.
2437 void *newinsn = NULL;
2438 if (type->isPrimitive ())
2440 switch (type->size_in_bytes)
2442 case 1:
2443 PUSHI (*field->u.byte_addr);
2444 newinsn = AMPAMP (getstatic_resolved_1);
2445 break;
2447 case 2:
2448 if (type == JvPrimClass (char))
2450 PUSHI (*field->u.char_addr);
2451 newinsn = AMPAMP (getstatic_resolved_char);
2453 else
2455 PUSHI (*field->u.short_addr);
2456 newinsn = AMPAMP (getstatic_resolved_short);
2458 break;
2460 case 4:
2461 PUSHI(*field->u.int_addr);
2462 newinsn = AMPAMP (getstatic_resolved_4);
2463 break;
2465 case 8:
2466 PUSHL(*field->u.long_addr);
2467 newinsn = AMPAMP (getstatic_resolved_8);
2468 break;
2471 else
2473 PUSHA(*field->u.object_addr);
2474 newinsn = AMPAMP (getstatic_resolved_obj);
2477 #ifdef DIRECT_THREADED
2478 pc[-2].insn = newinsn;
2479 pc[-1].datum = field->u.addr;
2480 #endif /* DIRECT_THREADED */
2482 NEXT_INSN;
2484 #ifdef DIRECT_THREADED
2485 getstatic_resolved_1:
2486 PUSHI (*(jbyte *) AVAL ());
2487 NEXT_INSN;
2489 getstatic_resolved_char:
2490 PUSHI (*(jchar *) AVAL ());
2491 NEXT_INSN;
2493 getstatic_resolved_short:
2494 PUSHI (*(jshort *) AVAL ());
2495 NEXT_INSN;
2497 getstatic_resolved_4:
2498 PUSHI (*(jint *) AVAL ());
2499 NEXT_INSN;
2501 getstatic_resolved_8:
2502 PUSHL (*(jlong *) AVAL ());
2503 NEXT_INSN;
2505 getstatic_resolved_obj:
2506 PUSHA (*(jobject *) AVAL ());
2507 NEXT_INSN;
2508 #endif /* DIRECT_THREADED */
2510 insn_getfield:
2512 jint fieldref_index = GET2U ();
2513 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2514 _Jv_Field *field = pool_data[fieldref_index].field;
2516 if ((field->flags & Modifier::STATIC) != 0)
2517 throw_incompatible_class_change_error
2518 (JvNewStringLatin1 ("field is static"));
2520 jclass type = field->type;
2521 jint field_offset = field->u.boffset;
2522 if (field_offset > 0xffff)
2523 throw new java::lang::VirtualMachineError;
2525 jobject obj = POPA();
2526 NULLCHECK(obj);
2528 void *newinsn = NULL;
2529 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2530 if (type->isPrimitive ())
2532 switch (type->size_in_bytes)
2534 case 1:
2535 PUSHI (val->byte_value);
2536 newinsn = AMPAMP (getfield_resolved_1);
2537 break;
2539 case 2:
2540 if (type == JvPrimClass (char))
2542 PUSHI (val->char_value);
2543 newinsn = AMPAMP (getfield_resolved_char);
2545 else
2547 PUSHI (val->short_value);
2548 newinsn = AMPAMP (getfield_resolved_short);
2550 break;
2552 case 4:
2553 PUSHI (val->int_value);
2554 newinsn = AMPAMP (getfield_resolved_4);
2555 break;
2557 case 8:
2558 PUSHL (val->long_value);
2559 newinsn = AMPAMP (getfield_resolved_8);
2560 break;
2563 else
2565 PUSHA (val->object_value);
2566 newinsn = AMPAMP (getfield_resolved_obj);
2569 #ifdef DIRECT_THREADED
2570 pc[-2].insn = newinsn;
2571 pc[-1].int_val = field_offset;
2572 #endif /* DIRECT_THREADED */
2574 NEXT_INSN;
2576 #ifdef DIRECT_THREADED
2577 getfield_resolved_1:
2579 char *obj = (char *) POPA ();
2580 NULLCHECK (obj);
2581 PUSHI (*(jbyte *) (obj + INTVAL ()));
2583 NEXT_INSN;
2585 getfield_resolved_char:
2587 char *obj = (char *) POPA ();
2588 NULLCHECK (obj);
2589 PUSHI (*(jchar *) (obj + INTVAL ()));
2591 NEXT_INSN;
2593 getfield_resolved_short:
2595 char *obj = (char *) POPA ();
2596 NULLCHECK (obj);
2597 PUSHI (*(jshort *) (obj + INTVAL ()));
2599 NEXT_INSN;
2601 getfield_resolved_4:
2603 char *obj = (char *) POPA ();
2604 NULLCHECK (obj);
2605 PUSHI (*(jint *) (obj + INTVAL ()));
2607 NEXT_INSN;
2609 getfield_resolved_8:
2611 char *obj = (char *) POPA ();
2612 NULLCHECK (obj);
2613 PUSHL (*(jlong *) (obj + INTVAL ()));
2615 NEXT_INSN;
2617 getfield_resolved_obj:
2619 char *obj = (char *) POPA ();
2620 NULLCHECK (obj);
2621 PUSHA (*(jobject *) (obj + INTVAL ()));
2623 NEXT_INSN;
2624 #endif /* DIRECT_THREADED */
2626 insn_putstatic:
2628 jint fieldref_index = GET2U ();
2629 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2630 _Jv_Field *field = pool_data[fieldref_index].field;
2632 jclass type = field->type;
2634 // ResolvePoolEntry cannot check this
2635 if ((field->flags & Modifier::STATIC) == 0)
2636 throw_incompatible_class_change_error
2637 (JvNewStringLatin1 ("field no longer static"));
2639 void *newinsn = NULL;
2640 if (type->isPrimitive ())
2642 switch (type->size_in_bytes)
2644 case 1:
2646 jint value = POPI();
2647 *field->u.byte_addr = value;
2648 newinsn = AMPAMP (putstatic_resolved_1);
2649 break;
2652 case 2:
2654 jint value = POPI();
2655 *field->u.char_addr = value;
2656 newinsn = AMPAMP (putstatic_resolved_2);
2657 break;
2660 case 4:
2662 jint value = POPI();
2663 *field->u.int_addr = value;
2664 newinsn = AMPAMP (putstatic_resolved_4);
2665 break;
2668 case 8:
2670 jlong value = POPL();
2671 *field->u.long_addr = value;
2672 newinsn = AMPAMP (putstatic_resolved_8);
2673 break;
2677 else
2679 jobject value = POPA();
2680 *field->u.object_addr = value;
2681 newinsn = AMPAMP (putstatic_resolved_obj);
2684 #ifdef DIRECT_THREADED
2685 pc[-2].insn = newinsn;
2686 pc[-1].datum = field->u.addr;
2687 #endif /* DIRECT_THREADED */
2689 NEXT_INSN;
2691 #ifdef DIRECT_THREADED
2692 putstatic_resolved_1:
2693 *(jbyte *) AVAL () = POPI ();
2694 NEXT_INSN;
2696 putstatic_resolved_2:
2697 *(jchar *) AVAL () = POPI ();
2698 NEXT_INSN;
2700 putstatic_resolved_4:
2701 *(jint *) AVAL () = POPI ();
2702 NEXT_INSN;
2704 putstatic_resolved_8:
2705 *(jlong *) AVAL () = POPL ();
2706 NEXT_INSN;
2708 putstatic_resolved_obj:
2709 *(jobject *) AVAL () = POPA ();
2710 NEXT_INSN;
2711 #endif /* DIRECT_THREADED */
2713 insn_putfield:
2715 jint fieldref_index = GET2U ();
2716 _Jv_Linker::resolve_pool_entry (defining_class, fieldref_index);
2717 _Jv_Field *field = pool_data[fieldref_index].field;
2719 jclass type = field->type;
2721 if ((field->flags & Modifier::STATIC) != 0)
2722 throw_incompatible_class_change_error
2723 (JvNewStringLatin1 ("field is static"));
2725 jint field_offset = field->u.boffset;
2726 if (field_offset > 0xffff)
2727 throw new java::lang::VirtualMachineError;
2729 void *newinsn = NULL;
2730 if (type->isPrimitive ())
2732 switch (type->size_in_bytes)
2734 case 1:
2736 jint value = POPI();
2737 jobject obj = POPA();
2738 NULLCHECK(obj);
2739 *(jbyte*) ((char*)obj + field_offset) = value;
2740 newinsn = AMPAMP (putfield_resolved_1);
2741 break;
2744 case 2:
2746 jint value = POPI();
2747 jobject obj = POPA();
2748 NULLCHECK(obj);
2749 *(jchar*) ((char*)obj + field_offset) = value;
2750 newinsn = AMPAMP (putfield_resolved_2);
2751 break;
2754 case 4:
2756 jint value = POPI();
2757 jobject obj = POPA();
2758 NULLCHECK(obj);
2759 *(jint*) ((char*)obj + field_offset) = value;
2760 newinsn = AMPAMP (putfield_resolved_4);
2761 break;
2764 case 8:
2766 jlong value = POPL();
2767 jobject obj = POPA();
2768 NULLCHECK(obj);
2769 *(jlong*) ((char*)obj + field_offset) = value;
2770 newinsn = AMPAMP (putfield_resolved_8);
2771 break;
2775 else
2777 jobject value = POPA();
2778 jobject obj = POPA();
2779 NULLCHECK(obj);
2780 *(jobject*) ((char*)obj + field_offset) = value;
2781 newinsn = AMPAMP (putfield_resolved_obj);
2784 #ifdef DIRECT_THREADED
2785 pc[-2].insn = newinsn;
2786 pc[-1].int_val = field_offset;
2787 #endif /* DIRECT_THREADED */
2789 NEXT_INSN;
2791 #ifdef DIRECT_THREADED
2792 putfield_resolved_1:
2794 jint val = POPI ();
2795 char *obj = (char *) POPA ();
2796 NULLCHECK (obj);
2797 *(jbyte *) (obj + INTVAL ()) = val;
2799 NEXT_INSN;
2801 putfield_resolved_2:
2803 jint val = POPI ();
2804 char *obj = (char *) POPA ();
2805 NULLCHECK (obj);
2806 *(jchar *) (obj + INTVAL ()) = val;
2808 NEXT_INSN;
2810 putfield_resolved_4:
2812 jint val = POPI ();
2813 char *obj = (char *) POPA ();
2814 NULLCHECK (obj);
2815 *(jint *) (obj + INTVAL ()) = val;
2817 NEXT_INSN;
2819 putfield_resolved_8:
2821 jlong val = POPL ();
2822 char *obj = (char *) POPA ();
2823 NULLCHECK (obj);
2824 *(jlong *) (obj + INTVAL ()) = val;
2826 NEXT_INSN;
2828 putfield_resolved_obj:
2830 jobject val = POPA ();
2831 char *obj = (char *) POPA ();
2832 NULLCHECK (obj);
2833 *(jobject *) (obj + INTVAL ()) = val;
2835 NEXT_INSN;
2836 #endif /* DIRECT_THREADED */
2838 insn_invokespecial:
2840 int index = GET2U ();
2842 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
2843 index)).rmethod;
2845 sp -= rmeth->stack_item_count;
2847 // We don't use NULLCHECK here because we can't rely on that
2848 // working for <init>. So instead we do an explicit test.
2849 if (! sp[0].o)
2850 throw new java::lang::NullPointerException;
2852 fun = (void (*)()) rmeth->method->ncode;
2854 #ifdef DIRECT_THREADED
2855 // Rewrite instruction so that we use a faster pre-resolved
2856 // method.
2857 pc[-2].insn = &&invokespecial_resolved;
2858 pc[-1].datum = rmeth;
2859 #endif /* DIRECT_THREADED */
2861 goto perform_invoke;
2863 #ifdef DIRECT_THREADED
2864 invokespecial_resolved:
2866 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2867 sp -= rmeth->stack_item_count;
2868 // We don't use NULLCHECK here because we can't rely on that
2869 // working for <init>. So instead we do an explicit test.
2870 if (! sp[0].o)
2871 throw new java::lang::NullPointerException;
2872 fun = (void (*)()) rmeth->method->ncode;
2874 goto perform_invoke;
2875 #endif /* DIRECT_THREADED */
2877 insn_invokestatic:
2879 int index = GET2U ();
2881 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
2882 index)).rmethod;
2884 sp -= rmeth->stack_item_count;
2886 fun = (void (*)()) rmeth->method->ncode;
2888 #ifdef DIRECT_THREADED
2889 // Rewrite instruction so that we use a faster pre-resolved
2890 // method.
2891 pc[-2].insn = &&invokestatic_resolved;
2892 pc[-1].datum = rmeth;
2893 #endif /* DIRECT_THREADED */
2895 goto perform_invoke;
2897 #ifdef DIRECT_THREADED
2898 invokestatic_resolved:
2900 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2901 sp -= rmeth->stack_item_count;
2902 fun = (void (*)()) rmeth->method->ncode;
2904 goto perform_invoke;
2905 #endif /* DIRECT_THREADED */
2907 insn_invokeinterface:
2909 int index = GET2U ();
2911 rmeth = (_Jv_Linker::resolve_pool_entry (defining_class,
2912 index)).rmethod;
2914 sp -= rmeth->stack_item_count;
2916 jobject rcv = sp[0].o;
2918 NULLCHECK (rcv);
2920 fun = (void (*)())
2921 _Jv_LookupInterfaceMethod (rcv->getClass (),
2922 rmeth->method->name,
2923 rmeth->method->signature);
2925 #ifdef DIRECT_THREADED
2926 // Rewrite instruction so that we use a faster pre-resolved
2927 // method.
2928 pc[-2].insn = &&invokeinterface_resolved;
2929 pc[-1].datum = rmeth;
2930 #else
2931 // Skip dummy bytes.
2932 pc += 2;
2933 #endif /* DIRECT_THREADED */
2935 goto perform_invoke;
2937 #ifdef DIRECT_THREADED
2938 invokeinterface_resolved:
2940 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2941 sp -= rmeth->stack_item_count;
2942 jobject rcv = sp[0].o;
2943 NULLCHECK (rcv);
2944 fun = (void (*)())
2945 _Jv_LookupInterfaceMethod (rcv->getClass (),
2946 rmeth->method->name,
2947 rmeth->method->signature);
2949 goto perform_invoke;
2950 #endif /* DIRECT_THREADED */
2952 insn_new:
2954 int index = GET2U ();
2955 jclass klass = (_Jv_Linker::resolve_pool_entry (defining_class,
2956 index)).clazz;
2957 jobject res = _Jv_AllocObject (klass);
2958 PUSHA (res);
2960 #ifdef DIRECT_THREADED
2961 pc[-2].insn = &&new_resolved;
2962 pc[-1].datum = klass;
2963 #endif /* DIRECT_THREADED */
2965 NEXT_INSN;
2967 #ifdef DIRECT_THREADED
2968 new_resolved:
2970 jclass klass = (jclass) AVAL ();
2971 jobject res = _Jv_AllocObject (klass);
2972 PUSHA (res);
2974 NEXT_INSN;
2975 #endif /* DIRECT_THREADED */
2977 insn_newarray:
2979 int atype = GET1U ();
2980 int size = POPI();
2981 jobject result = _Jv_NewArray (atype, size);
2982 PUSHA (result);
2984 NEXT_INSN;
2986 insn_anewarray:
2988 int index = GET2U ();
2989 jclass klass = (_Jv_Linker::resolve_pool_entry (defining_class,
2990 index)).clazz;
2991 int size = POPI();
2992 jobject result = _Jv_NewObjectArray (size, klass, 0);
2993 PUSHA (result);
2995 #ifdef DIRECT_THREADED
2996 pc[-2].insn = &&anewarray_resolved;
2997 pc[-1].datum = klass;
2998 #endif /* DIRECT_THREADED */
3000 NEXT_INSN;
3002 #ifdef DIRECT_THREADED
3003 anewarray_resolved:
3005 jclass klass = (jclass) AVAL ();
3006 int size = POPI ();
3007 jobject result = _Jv_NewObjectArray (size, klass, 0);
3008 PUSHA (result);
3010 NEXT_INSN;
3011 #endif /* DIRECT_THREADED */
3013 insn_arraylength:
3015 __JArray *arr = (__JArray*)POPA();
3016 NULLARRAYCHECK (arr);
3017 PUSHI (arr->length);
3019 NEXT_INSN;
3021 insn_athrow:
3023 jobject value = POPA();
3024 throw static_cast<jthrowable>(value);
3026 NEXT_INSN;
3028 insn_checkcast:
3030 jobject value = POPA();
3031 jint index = GET2U ();
3032 jclass to = (_Jv_Linker::resolve_pool_entry (defining_class,
3033 index)).clazz;
3035 if (value != NULL && ! to->isInstance (value))
3036 throw new java::lang::ClassCastException (to->getName());
3038 PUSHA (value);
3040 #ifdef DIRECT_THREADED
3041 pc[-2].insn = &&checkcast_resolved;
3042 pc[-1].datum = to;
3043 #endif /* DIRECT_THREADED */
3045 NEXT_INSN;
3047 #ifdef DIRECT_THREADED
3048 checkcast_resolved:
3050 jobject value = POPA ();
3051 jclass to = (jclass) AVAL ();
3052 if (value != NULL && ! to->isInstance (value))
3053 throw new java::lang::ClassCastException (to->getName());
3054 PUSHA (value);
3056 NEXT_INSN;
3057 #endif /* DIRECT_THREADED */
3059 insn_instanceof:
3061 jobject value = POPA();
3062 jint index = GET2U ();
3063 jclass to = (_Jv_Linker::resolve_pool_entry (defining_class,
3064 index)).clazz;
3065 PUSHI (to->isInstance (value));
3067 #ifdef DIRECT_THREADED
3068 pc[-2].insn = &&instanceof_resolved;
3069 pc[-1].datum = to;
3070 #endif /* DIRECT_THREADED */
3072 NEXT_INSN;
3074 #ifdef DIRECT_THREADED
3075 instanceof_resolved:
3077 jobject value = POPA ();
3078 jclass to = (jclass) AVAL ();
3079 PUSHI (to->isInstance (value));
3081 NEXT_INSN;
3082 #endif /* DIRECT_THREADED */
3084 insn_monitorenter:
3086 jobject value = POPA();
3087 NULLCHECK(value);
3088 _Jv_MonitorEnter (value);
3090 NEXT_INSN;
3092 insn_monitorexit:
3094 jobject value = POPA();
3095 NULLCHECK(value);
3096 _Jv_MonitorExit (value);
3098 NEXT_INSN;
3100 insn_ifnull:
3102 jobject val = POPA();
3103 if (val == NULL)
3104 TAKE_GOTO;
3105 else
3106 SKIP_GOTO;
3108 NEXT_INSN;
3110 insn_ifnonnull:
3112 jobject val = POPA();
3113 if (val != NULL)
3114 TAKE_GOTO;
3115 else
3116 SKIP_GOTO;
3118 NEXT_INSN;
3120 insn_multianewarray:
3122 int kind_index = GET2U ();
3123 int dim = GET1U ();
3125 jclass type
3126 = (_Jv_Linker::resolve_pool_entry (defining_class,
3127 kind_index)).clazz;
3128 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3130 for (int i = dim - 1; i >= 0; i--)
3132 sizes[i] = POPI ();
3135 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3137 PUSHA (res);
3139 NEXT_INSN;
3141 #ifndef DIRECT_THREADED
3142 insn_wide:
3144 jint the_mod_op = get1u (pc++);
3145 jint wide = get2u (pc); pc += 2;
3147 switch (the_mod_op)
3149 case op_istore:
3150 STOREI (wide);
3151 NEXT_INSN;
3153 case op_fstore:
3154 STOREF (wide);
3155 NEXT_INSN;
3157 case op_astore:
3158 STOREA (wide);
3159 NEXT_INSN;
3161 case op_lload:
3162 LOADL (wide);
3163 NEXT_INSN;
3165 case op_dload:
3166 LOADD (wide);
3167 NEXT_INSN;
3169 case op_iload:
3170 LOADI (wide);
3171 NEXT_INSN;
3173 case op_fload:
3174 LOADF (wide);
3175 NEXT_INSN;
3177 case op_aload:
3178 LOADA (wide);
3179 NEXT_INSN;
3181 case op_lstore:
3182 STOREL (wide);
3183 NEXT_INSN;
3185 case op_dstore:
3186 STORED (wide);
3187 NEXT_INSN;
3189 case op_ret:
3190 pc = (unsigned char*) PEEKA (wide);
3191 NEXT_INSN;
3193 case op_iinc:
3195 jint amount = get2s (pc); pc += 2;
3196 jint value = PEEKI (wide);
3197 POKEI (wide, value+amount);
3199 NEXT_INSN;
3201 default:
3202 throw_internal_error ("illegal bytecode modified by wide");
3206 #endif /* DIRECT_THREADED */
3208 catch (java::lang::Throwable *ex)
3210 #ifdef DIRECT_THREADED
3211 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3212 #else
3213 int logical_pc = pc - 1 - bytecode ();
3214 #endif
3215 _Jv_InterpException *exc = exceptions ();
3216 jclass exc_class = ex->getClass ();
3218 for (int i = 0; i < exc_count; i++)
3220 if (PCVAL (exc[i].start_pc) <= logical_pc
3221 && logical_pc < PCVAL (exc[i].end_pc))
3223 #ifdef DIRECT_THREADED
3224 jclass handler = (jclass) exc[i].handler_type.p;
3225 #else
3226 jclass handler = NULL;
3227 if (exc[i].handler_type.i != 0)
3228 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3229 exc[i].handler_type.i)).clazz;
3230 #endif /* DIRECT_THREADED */
3232 if (handler == NULL || handler->isAssignableFrom (exc_class))
3234 #ifdef DIRECT_THREADED
3235 pc = (insn_slot *) exc[i].handler_pc.p;
3236 #else
3237 pc = bytecode () + exc[i].handler_pc.i;
3238 #endif /* DIRECT_THREADED */
3239 sp = stack;
3240 sp++->o = ex; // Push exception.
3241 NEXT_INSN;
3246 // No handler, so re-throw.
3247 throw ex;
3251 static void
3252 throw_internal_error (char *msg)
3254 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3257 static void
3258 throw_incompatible_class_change_error (jstring msg)
3260 throw new java::lang::IncompatibleClassChangeError (msg);
3263 #ifndef HANDLE_SEGV
3264 static java::lang::NullPointerException *null_pointer_exc;
3265 static void
3266 throw_null_pointer_exception ()
3268 if (null_pointer_exc == NULL)
3269 null_pointer_exc = new java::lang::NullPointerException;
3271 throw null_pointer_exc;
3273 #endif
3275 /** Do static initialization for fields with a constant initializer */
3276 void
3277 _Jv_InitField (jobject obj, jclass klass, int index)
3279 using namespace java::lang::reflect;
3281 if (obj != 0 && klass == 0)
3282 klass = obj->getClass ();
3284 if (!_Jv_IsInterpretedClass (klass))
3285 return;
3287 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3289 _Jv_Field * field = (&klass->fields[0]) + index;
3291 if (index > klass->field_count)
3292 throw_internal_error ("field out of range");
3294 int init = iclass->field_initializers[index];
3295 if (init == 0)
3296 return;
3298 _Jv_Constants *pool = &klass->constants;
3299 int tag = pool->tags[init];
3301 if (! field->isResolved ())
3302 throw_internal_error ("initializing unresolved field");
3304 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3305 throw_internal_error ("initializing non-static field with no object");
3307 void *addr = 0;
3309 if ((field->flags & Modifier::STATIC) != 0)
3310 addr = (void*) field->u.addr;
3311 else
3312 addr = (void*) (((char*)obj) + field->u.boffset);
3314 switch (tag)
3316 case JV_CONSTANT_String:
3318 jstring str;
3319 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3320 pool->data[init].string = str;
3321 pool->tags[init] = JV_CONSTANT_ResolvedString;
3323 /* fall through */
3325 case JV_CONSTANT_ResolvedString:
3326 if (! (field->type == &java::lang::String::class$
3327 || field->type == &java::lang::Class::class$))
3328 throw_class_format_error ("string initialiser to non-string field");
3330 *(jstring*)addr = pool->data[init].string;
3331 break;
3333 case JV_CONSTANT_Integer:
3335 int value = pool->data[init].i;
3337 if (field->type == JvPrimClass (boolean))
3338 *(jboolean*)addr = (jboolean)value;
3340 else if (field->type == JvPrimClass (byte))
3341 *(jbyte*)addr = (jbyte)value;
3343 else if (field->type == JvPrimClass (char))
3344 *(jchar*)addr = (jchar)value;
3346 else if (field->type == JvPrimClass (short))
3347 *(jshort*)addr = (jshort)value;
3349 else if (field->type == JvPrimClass (int))
3350 *(jint*)addr = (jint)value;
3352 else
3353 throw_class_format_error ("erroneous field initializer");
3355 break;
3357 case JV_CONSTANT_Long:
3358 if (field->type != JvPrimClass (long))
3359 throw_class_format_error ("erroneous field initializer");
3361 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3362 break;
3364 case JV_CONSTANT_Float:
3365 if (field->type != JvPrimClass (float))
3366 throw_class_format_error ("erroneous field initializer");
3368 *(jfloat*)addr = pool->data[init].f;
3369 break;
3371 case JV_CONSTANT_Double:
3372 if (field->type != JvPrimClass (double))
3373 throw_class_format_error ("erroneous field initializer");
3375 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3376 break;
3378 default:
3379 throw_class_format_error ("erroneous field initializer");
3383 inline static unsigned char*
3384 skip_one_type (unsigned char* ptr)
3386 int ch = *ptr++;
3388 while (ch == '[')
3390 ch = *ptr++;
3393 if (ch == 'L')
3395 do { ch = *ptr++; } while (ch != ';');
3398 return ptr;
3401 static ffi_type*
3402 get_ffi_type_from_signature (unsigned char* ptr)
3404 switch (*ptr)
3406 case 'L':
3407 case '[':
3408 return &ffi_type_pointer;
3409 break;
3411 case 'Z':
3412 // On some platforms a bool is a byte, on others an int.
3413 if (sizeof (jboolean) == sizeof (jbyte))
3414 return &ffi_type_sint8;
3415 else
3417 JvAssert (sizeof (jbyte) == sizeof (jint));
3418 return &ffi_type_sint32;
3420 break;
3422 case 'B':
3423 return &ffi_type_sint8;
3424 break;
3426 case 'C':
3427 return &ffi_type_uint16;
3428 break;
3430 case 'S':
3431 return &ffi_type_sint16;
3432 break;
3434 case 'I':
3435 return &ffi_type_sint32;
3436 break;
3438 case 'J':
3439 return &ffi_type_sint64;
3440 break;
3442 case 'F':
3443 return &ffi_type_float;
3444 break;
3446 case 'D':
3447 return &ffi_type_double;
3448 break;
3450 case 'V':
3451 return &ffi_type_void;
3452 break;
3455 throw_internal_error ("unknown type in signature");
3458 /* this function yields the number of actual arguments, that is, if the
3459 * function is non-static, then one is added to the number of elements
3460 * found in the signature */
3462 int
3463 _Jv_count_arguments (_Jv_Utf8Const *signature,
3464 jboolean staticp)
3466 unsigned char *ptr = (unsigned char*) signature->chars();
3467 int arg_count = staticp ? 0 : 1;
3469 /* first, count number of arguments */
3471 // skip '('
3472 ptr++;
3474 // count args
3475 while (*ptr != ')')
3477 ptr = skip_one_type (ptr);
3478 arg_count += 1;
3481 return arg_count;
3484 /* This beast will build a cif, given the signature. Memory for
3485 * the cif itself and for the argument types must be allocated by the
3486 * caller.
3489 static int
3490 init_cif (_Jv_Utf8Const* signature,
3491 int arg_count,
3492 jboolean staticp,
3493 ffi_cif *cif,
3494 ffi_type **arg_types,
3495 ffi_type **rtype_p)
3497 unsigned char *ptr = (unsigned char*) signature->chars();
3499 int arg_index = 0; // arg number
3500 int item_count = 0; // stack-item count
3502 // setup receiver
3503 if (!staticp)
3505 arg_types[arg_index++] = &ffi_type_pointer;
3506 item_count += 1;
3509 // skip '('
3510 ptr++;
3512 // assign arg types
3513 while (*ptr != ')')
3515 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3517 if (*ptr == 'J' || *ptr == 'D')
3518 item_count += 2;
3519 else
3520 item_count += 1;
3522 ptr = skip_one_type (ptr);
3525 // skip ')'
3526 ptr++;
3527 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3529 ptr = skip_one_type (ptr);
3530 if (ptr != (unsigned char*)signature->chars() + signature->len())
3531 throw_internal_error ("did not find end of signature");
3533 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3534 arg_count, rtype, arg_types) != FFI_OK)
3535 throw_internal_error ("ffi_prep_cif failed");
3537 if (rtype_p != NULL)
3538 *rtype_p = rtype;
3540 return item_count;
3543 #if FFI_NATIVE_RAW_API
3544 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3545 # define FFI_RAW_SIZE ffi_raw_size
3546 #else
3547 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3548 # define FFI_RAW_SIZE ffi_java_raw_size
3549 #endif
3551 /* we put this one here, and not in interpret.cc because it
3552 * calls the utility routines _Jv_count_arguments
3553 * which are static to this module. The following struct defines the
3554 * layout we use for the stubs, it's only used in the ncode method. */
3556 typedef struct {
3557 ffi_raw_closure closure;
3558 ffi_cif cif;
3559 ffi_type *arg_types[0];
3560 } ncode_closure;
3562 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3564 void *
3565 _Jv_InterpMethod::ncode ()
3567 using namespace java::lang::reflect;
3569 if (self->ncode != 0)
3570 return self->ncode;
3572 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3573 int arg_count = _Jv_count_arguments (self->signature, staticp);
3575 ncode_closure *closure =
3576 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3577 + arg_count * sizeof (ffi_type*));
3579 init_cif (self->signature,
3580 arg_count,
3581 staticp,
3582 &closure->cif,
3583 &closure->arg_types[0],
3584 NULL);
3586 ffi_closure_fun fun;
3588 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3590 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3592 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3594 if (staticp)
3595 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3596 else
3597 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3599 else
3601 if (staticp)
3602 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3603 else
3604 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3607 FFI_PREP_RAW_CLOSURE (&closure->closure,
3608 &closure->cif,
3609 fun,
3610 (void*)this);
3612 self->ncode = (void*)closure;
3613 return self->ncode;
3616 void *
3617 _Jv_JNIMethod::ncode ()
3619 using namespace java::lang::reflect;
3621 if (self->ncode != 0)
3622 return self->ncode;
3624 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3625 int arg_count = _Jv_count_arguments (self->signature, staticp);
3627 ncode_closure *closure =
3628 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3629 + arg_count * sizeof (ffi_type*));
3631 ffi_type *rtype;
3632 init_cif (self->signature,
3633 arg_count,
3634 staticp,
3635 &closure->cif,
3636 &closure->arg_types[0],
3637 &rtype);
3639 ffi_closure_fun fun;
3641 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3643 // Initialize the argument types and CIF that represent the actual
3644 // underlying JNI function.
3645 int extra_args = 1;
3646 if ((self->accflags & Modifier::STATIC))
3647 ++extra_args;
3648 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3649 * sizeof (ffi_type *));
3650 int offset = 0;
3651 jni_arg_types[offset++] = &ffi_type_pointer;
3652 if ((self->accflags & Modifier::STATIC))
3653 jni_arg_types[offset++] = &ffi_type_pointer;
3654 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3655 arg_count * sizeof (ffi_type *));
3657 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3658 extra_args + arg_count, rtype,
3659 jni_arg_types) != FFI_OK)
3660 throw_internal_error ("ffi_prep_cif failed for JNI function");
3662 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3664 // FIXME: for now we assume that all native methods for
3665 // interpreted code use JNI.
3666 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3668 FFI_PREP_RAW_CLOSURE (&closure->closure,
3669 &closure->cif,
3670 fun,
3671 (void*) this);
3673 self->ncode = (void *) closure;
3674 return self->ncode;
3677 static void
3678 throw_class_format_error (jstring msg)
3680 throw (msg
3681 ? new java::lang::ClassFormatError (msg)
3682 : new java::lang::ClassFormatError);
3685 static void
3686 throw_class_format_error (char *msg)
3688 throw_class_format_error (JvNewStringLatin1 (msg));
3693 void
3694 _Jv_InterpreterEngine::do_verify (jclass klass)
3696 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3697 for (int i = 0; i < klass->method_count; i++)
3699 using namespace java::lang::reflect;
3700 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3701 _Jv_ushort accflags = klass->methods[i].accflags;
3702 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3704 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3705 _Jv_VerifyMethod (im);
3710 void
3711 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3713 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3714 for (int i = 0; i < klass->method_count; i++)
3716 // Just skip abstract methods. This is particularly important
3717 // because we don't resize the interpreted_methods array when
3718 // miranda methods are added to it.
3719 if ((klass->methods[i].accflags
3720 & java::lang::reflect::Modifier::ABSTRACT)
3721 != 0)
3722 continue;
3724 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3726 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3727 != 0)
3729 // You might think we could use a virtual `ncode' method in
3730 // the _Jv_MethodBase and unify the native and non-native
3731 // cases. Well, we can't, because we don't allocate these
3732 // objects using `new', and thus they don't get a vtable.
3733 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3734 klass->methods[i].ncode = jnim->ncode ();
3736 else if (imeth != 0) // it could be abstract
3738 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3739 klass->methods[i].ncode = im->ncode ();
3744 void
3745 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3746 int static_size)
3748 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3750 char *static_data = (char *) _Jv_AllocBytes (static_size);
3751 memset (static_data, 0, static_size);
3753 for (int i = 0; i < klass->field_count; i++)
3755 _Jv_Field *field = &klass->fields[i];
3757 if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3759 field->u.addr = static_data + field->u.boffset;
3761 if (iclass->field_initializers[i] != 0)
3763 _Jv_Linker::resolve_field (field, klass->loader);
3764 _Jv_InitField (0, klass, i);
3769 // Now we don't need the field_initializers anymore, so let the
3770 // collector get rid of it.
3771 iclass->field_initializers = 0;
3774 _Jv_ResolvedMethod *
3775 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3776 jboolean staticp, jint vtable_index)
3778 int arg_count = _Jv_count_arguments (method->signature, staticp);
3780 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3781 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3782 + arg_count*sizeof (ffi_type*));
3784 result->stack_item_count
3785 = init_cif (method->signature,
3786 arg_count,
3787 staticp,
3788 &result->cif,
3789 &result->arg_types[0],
3790 NULL);
3792 result->vtable_index = vtable_index;
3793 result->method = method;
3794 result->klass = klass;
3796 return result;
3799 void
3800 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3802 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3803 for (int i = 0; i < klass->method_count; i++)
3805 // Just skip abstract methods. This is particularly important
3806 // because we don't resize the interpreted_methods array when
3807 // miranda methods are added to it.
3808 if ((klass->methods[i].accflags
3809 & java::lang::reflect::Modifier::ABSTRACT)
3810 != 0)
3811 continue;
3812 // Miranda method additions mean that the `methods' array moves.
3813 // We cache a pointer into this array, so we have to update.
3814 iclass->interpreted_methods[i]->self = &klass->methods[i];
3818 #endif // INTERPRETER