* gcse.c (hoist_code): Rewrite to only get list of dominated
[official-gcc.git] / libjava / interpret.cc
blobe5c40cff15039de6651f36c6dd582925d9d99ed9
1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002 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>
15 // Define this to get the direct-threaded interpreter. If undefined,
16 // we revert to a basic bytecode interpreter. The former is faster
17 // but uses more memory.
18 #define DIRECT_THREADED
20 #pragma implementation "java-interp.h"
22 #include <jvm.h>
23 #include <java-cpool.h>
24 #include <java-interp.h>
25 // #include <java/lang/fdlibm.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-insns.h>
40 #include <java-signal.h>
42 #ifdef INTERPRETER
44 #include <stdlib.h>
46 using namespace gcj;
48 static void throw_internal_error (char *msg)
49 __attribute__ ((__noreturn__));
50 static void throw_incompatible_class_change_error (jstring msg)
51 __attribute__ ((__noreturn__));
52 #ifndef HANDLE_SEGV
53 static void throw_null_pointer_exception ()
54 __attribute__ ((__noreturn__));
55 #endif
57 extern "C" double __ieee754_fmod (double,double);
59 // This represents a single slot in the "compiled" form of the
60 // bytecode.
61 union insn_slot
63 // Address of code.
64 void *insn;
65 // An integer value used by an instruction.
66 jint int_val;
67 // A pointer value used by an instruction.
68 void *datum;
71 // The type of the PC depends on whether we're doing direct threading
72 // or a more ordinary bytecode interpreter.
73 #ifdef DIRECT_THREADED
74 typedef insn_slot *pc_t;
75 #else
76 typedef unsigned char *pc_t;
77 #endif
79 static inline void dupx (_Jv_word *sp, int n, int x)
81 // first "slide" n+x elements n to the right
82 int top = n-1;
83 for (int i = 0; i < n+x; i++)
85 sp[(top-i)] = sp[(top-i)-n];
88 // next, copy the n top elements, n+x down
89 for (int i = 0; i < n; i++)
91 sp[top-(n+x)-i] = sp[top-i];
96 // Used to convert from floating types to integral types.
97 template<typename TO, typename FROM>
98 static inline TO
99 convert (FROM val, TO min, TO max)
101 TO ret;
102 if (val >= (FROM) max)
103 ret = max;
104 else if (val <= (FROM) min)
105 ret = min;
106 else if (val != val)
107 ret = 0;
108 else
109 ret = (TO) val;
110 return ret;
113 #define PUSHA(V) (sp++)->o = (V)
114 #define PUSHI(V) (sp++)->i = (V)
115 #define PUSHF(V) (sp++)->f = (V)
116 #if SIZEOF_VOID_P == 8
117 # define PUSHL(V) (sp->l = (V), sp += 2)
118 # define PUSHD(V) (sp->d = (V), sp += 2)
119 #else
120 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
121 (sp++)->ia[0] = w2.ia[0]; \
122 (sp++)->ia[0] = w2.ia[1]; } while (0)
123 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
124 (sp++)->ia[0] = w2.ia[0]; \
125 (sp++)->ia[0] = w2.ia[1]; } while (0)
126 #endif
128 #define POPA() ((--sp)->o)
129 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
130 #define POPF() ((jfloat) (--sp)->f)
131 #if SIZEOF_VOID_P == 8
132 # define POPL() (sp -= 2, (jlong) sp->l)
133 # define POPD() (sp -= 2, (jdouble) sp->d)
134 #else
135 # define POPL() ({ _Jv_word2 w2; \
136 w2.ia[1] = (--sp)->ia[0]; \
137 w2.ia[0] = (--sp)->ia[0]; w2.l; })
138 # define POPD() ({ _Jv_word2 w2; \
139 w2.ia[1] = (--sp)->ia[0]; \
140 w2.ia[0] = (--sp)->ia[0]; w2.d; })
141 #endif
143 #define LOADA(I) (sp++)->o = locals[I].o
144 #define LOADI(I) (sp++)->i = locals[I].i
145 #define LOADF(I) (sp++)->f = locals[I].f
146 #if SIZEOF_VOID_P == 8
147 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
148 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
149 #else
150 # define LOADL(I) do { jint __idx = (I); \
151 (sp++)->ia[0] = locals[__idx].ia[0]; \
152 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
153 } while (0)
154 # define LOADD(I) LOADL(I)
155 #endif
157 #define STOREA(I) locals[I].o = (--sp)->o
158 #define STOREI(I) locals[I].i = (--sp)->i
159 #define STOREF(I) locals[I].f = (--sp)->f
160 #if SIZEOF_VOID_P == 8
161 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
162 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
163 #else
164 # define STOREL(I) do { jint __idx = (I); \
165 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
166 locals[__idx].ia[0] = (--sp)->ia[0]; \
167 } while (0)
168 # define STORED(I) STOREL(I)
169 #endif
171 #define PEEKI(I) (locals+(I))->i
172 #define PEEKA(I) (locals+(I))->o
174 #define POKEI(I,V) ((locals+(I))->i = (V))
177 #define BINOPI(OP) { \
178 jint value2 = POPI(); \
179 jint value1 = POPI(); \
180 PUSHI(value1 OP value2); \
183 #define BINOPF(OP) { \
184 jfloat value2 = POPF(); \
185 jfloat value1 = POPF(); \
186 PUSHF(value1 OP value2); \
189 #define BINOPL(OP) { \
190 jlong value2 = POPL(); \
191 jlong value1 = POPL(); \
192 PUSHL(value1 OP value2); \
195 #define BINOPD(OP) { \
196 jdouble value2 = POPD(); \
197 jdouble value1 = POPD(); \
198 PUSHD(value1 OP value2); \
201 static inline jint get1s(unsigned char* loc) {
202 return *(signed char*)loc;
205 static inline jint get1u(unsigned char* loc) {
206 return *loc;
209 static inline jint get2s(unsigned char* loc) {
210 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
213 static inline jint get2u(unsigned char* loc) {
214 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
217 static jint get4(unsigned char* loc) {
218 return (((jint)(loc[0])) << 24)
219 | (((jint)(loc[1])) << 16)
220 | (((jint)(loc[2])) << 8)
221 | (((jint)(loc[3])) << 0);
225 #ifdef HANDLE_SEGV
226 #define NULLCHECK(X)
227 #define NULLARRAYCHECK(X)
228 #else
229 #define NULLCHECK(X) \
230 do { if ((X)==NULL) throw_null_pointer_exception (); } while (0)
231 #define NULLARRAYCHECK(X) \
232 do { if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
233 #endif
235 #define ARRAYBOUNDSCHECK(array, index) \
236 do \
238 if (((unsigned) index) >= (unsigned) (array->length)) \
239 _Jv_ThrowBadArrayIndex (index); \
241 while (0)
243 void _Jv_InterpMethod::run_normal (ffi_cif *,
244 void* ret,
245 ffi_raw * args,
246 void* __this)
248 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
249 _this->run (ret, args);
252 void _Jv_InterpMethod::run_synch_object (ffi_cif *,
253 void* ret,
254 ffi_raw * args,
255 void* __this)
257 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
259 jobject rcv = (jobject) args[0].ptr;
260 JvSynchronize mutex (rcv);
262 _this->run (ret, args);
265 void _Jv_InterpMethod::run_synch_class (ffi_cif *,
266 void* ret,
267 ffi_raw * args,
268 void* __this)
270 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
272 jclass sync = _this->defining_class;
273 JvSynchronize mutex (sync);
275 _this->run (ret, args);
278 #ifdef DIRECT_THREADED
279 // "Compile" a method by turning it from bytecode to direct-threaded
280 // code.
281 void
282 _Jv_InterpMethod::compile (const void * const *insn_targets)
284 insn_slot *insns = NULL;
285 int next = 0;
286 unsigned char *codestart = bytecode ();
287 unsigned char *end = codestart + code_length;
288 _Jv_word *pool_data = defining_class->constants.data;
290 #define SET_ONE(Field, Value) \
291 do \
293 if (first_pass) \
294 ++next; \
295 else \
296 insns[next++].Field = Value; \
298 while (0)
300 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
301 #define SET_INT(Value) SET_ONE (int_val, Value)
302 #define SET_DATUM(Value) SET_ONE (datum, Value)
304 // Map from bytecode PC to slot in INSNS.
305 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
306 for (int i = 0; i < code_length; ++i)
307 pc_mapping[i] = -1;
309 for (int i = 0; i < 2; ++i)
311 jboolean first_pass = i == 0;
313 if (! first_pass)
315 insns = (insn_slot *) _Jv_Malloc (sizeof (insn_slot) * next);
316 next = 0;
319 unsigned char *pc = codestart;
320 while (pc < end)
322 int base_pc_val = pc - codestart;
323 if (first_pass)
324 pc_mapping[base_pc_val] = next;
326 java_opcode opcode = (java_opcode) *pc++;
327 // Just elide NOPs.
328 if (opcode == op_nop)
329 continue;
330 SET_INSN (insn_targets[opcode]);
332 switch (opcode)
334 case op_nop:
335 case op_aconst_null:
336 case op_iconst_m1:
337 case op_iconst_0:
338 case op_iconst_1:
339 case op_iconst_2:
340 case op_iconst_3:
341 case op_iconst_4:
342 case op_iconst_5:
343 case op_lconst_0:
344 case op_lconst_1:
345 case op_fconst_0:
346 case op_fconst_1:
347 case op_fconst_2:
348 case op_dconst_0:
349 case op_dconst_1:
350 case op_iload_0:
351 case op_iload_1:
352 case op_iload_2:
353 case op_iload_3:
354 case op_lload_0:
355 case op_lload_1:
356 case op_lload_2:
357 case op_lload_3:
358 case op_fload_0:
359 case op_fload_1:
360 case op_fload_2:
361 case op_fload_3:
362 case op_dload_0:
363 case op_dload_1:
364 case op_dload_2:
365 case op_dload_3:
366 case op_aload_0:
367 case op_aload_1:
368 case op_aload_2:
369 case op_aload_3:
370 case op_iaload:
371 case op_laload:
372 case op_faload:
373 case op_daload:
374 case op_aaload:
375 case op_baload:
376 case op_caload:
377 case op_saload:
378 case op_istore_0:
379 case op_istore_1:
380 case op_istore_2:
381 case op_istore_3:
382 case op_lstore_0:
383 case op_lstore_1:
384 case op_lstore_2:
385 case op_lstore_3:
386 case op_fstore_0:
387 case op_fstore_1:
388 case op_fstore_2:
389 case op_fstore_3:
390 case op_dstore_0:
391 case op_dstore_1:
392 case op_dstore_2:
393 case op_dstore_3:
394 case op_astore_0:
395 case op_astore_1:
396 case op_astore_2:
397 case op_astore_3:
398 case op_iastore:
399 case op_lastore:
400 case op_fastore:
401 case op_dastore:
402 case op_aastore:
403 case op_bastore:
404 case op_castore:
405 case op_sastore:
406 case op_pop:
407 case op_pop2:
408 case op_dup:
409 case op_dup_x1:
410 case op_dup_x2:
411 case op_dup2:
412 case op_dup2_x1:
413 case op_dup2_x2:
414 case op_swap:
415 case op_iadd:
416 case op_isub:
417 case op_imul:
418 case op_idiv:
419 case op_irem:
420 case op_ishl:
421 case op_ishr:
422 case op_iushr:
423 case op_iand:
424 case op_ior:
425 case op_ixor:
426 case op_ladd:
427 case op_lsub:
428 case op_lmul:
429 case op_ldiv:
430 case op_lrem:
431 case op_lshl:
432 case op_lshr:
433 case op_lushr:
434 case op_land:
435 case op_lor:
436 case op_lxor:
437 case op_fadd:
438 case op_fsub:
439 case op_fmul:
440 case op_fdiv:
441 case op_frem:
442 case op_dadd:
443 case op_dsub:
444 case op_dmul:
445 case op_ddiv:
446 case op_drem:
447 case op_ineg:
448 case op_i2b:
449 case op_i2c:
450 case op_i2s:
451 case op_lneg:
452 case op_fneg:
453 case op_dneg:
454 case op_i2l:
455 case op_i2f:
456 case op_i2d:
457 case op_l2i:
458 case op_l2f:
459 case op_l2d:
460 case op_f2i:
461 case op_f2l:
462 case op_f2d:
463 case op_d2i:
464 case op_d2l:
465 case op_d2f:
466 case op_lcmp:
467 case op_fcmpl:
468 case op_fcmpg:
469 case op_dcmpl:
470 case op_dcmpg:
471 case op_monitorenter:
472 case op_monitorexit:
473 case op_ireturn:
474 case op_lreturn:
475 case op_freturn:
476 case op_dreturn:
477 case op_areturn:
478 case op_return:
479 case op_athrow:
480 case op_arraylength:
481 // No argument, nothing else to do.
482 break;
484 case op_bipush:
485 SET_INT (get1s (pc));
486 ++pc;
487 break;
489 case op_ldc:
491 int index = get1u (pc);
492 ++pc;
493 SET_DATUM (pool_data[index].o);
495 break;
497 case op_ret:
498 case op_iload:
499 case op_lload:
500 case op_fload:
501 case op_dload:
502 case op_aload:
503 case op_istore:
504 case op_lstore:
505 case op_fstore:
506 case op_dstore:
507 case op_astore:
508 case op_newarray:
509 SET_INT (get1u (pc));
510 ++pc;
511 break;
513 case op_iinc:
514 SET_INT (get1u (pc));
515 SET_INT (get1s (pc + 1));
516 pc += 2;
517 break;
519 case op_ldc_w:
521 int index = get2u (pc);
522 pc += 2;
523 SET_DATUM (pool_data[index].o);
525 break;
527 case op_ldc2_w:
529 int index = get2u (pc);
530 pc += 2;
531 SET_DATUM (&pool_data[index]);
533 break;
535 case op_sipush:
536 SET_INT (get2s (pc));
537 pc += 2;
538 break;
540 case op_new:
541 case op_getstatic:
542 case op_getfield:
543 case op_putfield:
544 case op_putstatic:
545 case op_anewarray:
546 case op_instanceof:
547 case op_checkcast:
548 case op_invokespecial:
549 case op_invokestatic:
550 case op_invokevirtual:
551 SET_INT (get2u (pc));
552 pc += 2;
553 break;
555 case op_multianewarray:
556 SET_INT (get2u (pc));
557 SET_INT (get1u (pc + 2));
558 pc += 3;
559 break;
561 case op_jsr:
562 case op_ifeq:
563 case op_ifne:
564 case op_iflt:
565 case op_ifge:
566 case op_ifgt:
567 case op_ifle:
568 case op_if_icmpeq:
569 case op_if_icmpne:
570 case op_if_icmplt:
571 case op_if_icmpge:
572 case op_if_icmpgt:
573 case op_if_icmple:
574 case op_if_acmpeq:
575 case op_if_acmpne:
576 case op_ifnull:
577 case op_ifnonnull:
578 case op_goto:
580 int offset = get2s (pc);
581 pc += 2;
583 int new_pc = base_pc_val + offset;
585 bool orig_was_goto = opcode == op_goto;
587 // Thread jumps. We limit the loop count; this lets
588 // us avoid infinite loops if the bytecode contains
589 // such. `10' is arbitrary.
590 int count = 10;
591 while (codestart[new_pc] == op_goto && count-- > 0)
592 new_pc += get2s (&codestart[new_pc + 1]);
594 // If the jump takes us to a `return' instruction and
595 // the original branch was an unconditional goto, then
596 // we hoist the return.
597 opcode = (java_opcode) codestart[new_pc];
598 if (orig_was_goto
599 && (opcode == op_ireturn || opcode == op_lreturn
600 || opcode == op_freturn || opcode == op_dreturn
601 || opcode == op_areturn || opcode == op_return))
603 --next;
604 SET_INSN (insn_targets[opcode]);
606 else
607 SET_DATUM (&insns[pc_mapping[new_pc]]);
609 break;
611 case op_tableswitch:
613 while ((pc - codestart) % 4 != 0)
614 ++pc;
616 jint def = get4 (pc);
617 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
618 pc += 4;
620 int low = get4 (pc);
621 SET_INT (low);
622 pc += 4;
623 int high = get4 (pc);
624 SET_INT (high);
625 pc += 4;
627 for (int i = low; i <= high; ++i)
629 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
630 pc += 4;
633 break;
635 case op_lookupswitch:
637 while ((pc - codestart) % 4 != 0)
638 ++pc;
640 jint def = get4 (pc);
641 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
642 pc += 4;
644 jint npairs = get4 (pc);
645 pc += 4;
646 SET_INT (npairs);
648 while (npairs-- > 0)
650 jint match = get4 (pc);
651 jint offset = get4 (pc + 4);
652 SET_INT (match);
653 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
654 pc += 8;
657 break;
659 case op_invokeinterface:
661 jint index = get2u (pc);
662 pc += 2;
663 // We ignore the next two bytes.
664 pc += 2;
665 SET_INT (index);
667 break;
669 case op_wide:
671 opcode = (java_opcode) get1u (pc);
672 pc += 1;
673 jint val = get2u (pc);
674 pc += 2;
676 // We implement narrow and wide instructions using the
677 // same code in the interpreter. So we rewrite the
678 // instruction slot here.
679 if (! first_pass)
680 insns[next - 1].insn = (void *) insn_targets[opcode];
681 SET_INT (val);
683 if (opcode == op_iinc)
685 SET_INT (get2s (pc));
686 pc += 2;
689 break;
691 case op_jsr_w:
692 case op_goto_w:
694 jint offset = get4 (pc);
695 pc += 4;
696 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
698 break;
700 // Some "can't happen" cases that we include for
701 // error-checking purposes.
702 case op_putfield_1:
703 case op_putfield_2:
704 case op_putfield_4:
705 case op_putfield_8:
706 case op_putfield_a:
707 case op_putstatic_1:
708 case op_putstatic_2:
709 case op_putstatic_4:
710 case op_putstatic_8:
711 case op_putstatic_a:
712 case op_getfield_1:
713 case op_getfield_2s:
714 case op_getfield_2u:
715 case op_getfield_4:
716 case op_getfield_8:
717 case op_getfield_a:
718 case op_getstatic_1:
719 case op_getstatic_2s:
720 case op_getstatic_2u:
721 case op_getstatic_4:
722 case op_getstatic_8:
723 case op_getstatic_a:
724 default:
725 // Fail somehow.
726 break;
731 // Now update exceptions.
732 _Jv_InterpException *exc = exceptions ();
733 for (int i = 0; i < exc_count; ++i)
735 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
736 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
737 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
738 jclass handler = (_Jv_ResolvePoolEntry (defining_class,
739 exc[i].handler_type.i)).clazz;
740 exc[i].handler_type.p = handler;
743 prepared = insns;
745 #endif /* DIRECT_THREADED */
747 void
748 _Jv_InterpMethod::run (void *retp, ffi_raw *args)
750 using namespace java::lang::reflect;
752 _Jv_word stack[max_stack];
753 _Jv_word *sp = stack;
755 _Jv_word locals[max_locals];
757 /* Go straight at it! the ffi raw format matches the internal
758 stack representation exactly. At least, that's the idea.
760 memcpy ((void*) locals, (void*) args, args_raw_size);
762 _Jv_word *pool_data = defining_class->constants.data;
764 /* These three are temporaries for common code used by several
765 instructions. */
766 void (*fun)();
767 _Jv_ResolvedMethod* rmeth;
768 int tmpval;
770 #define INSN_LABEL(op) &&insn_##op
772 static const void *const insn_target[] =
774 INSN_LABEL(nop),
775 INSN_LABEL(aconst_null),
776 INSN_LABEL(iconst_m1),
777 INSN_LABEL(iconst_0),
778 INSN_LABEL(iconst_1),
779 INSN_LABEL(iconst_2),
780 INSN_LABEL(iconst_3),
781 INSN_LABEL(iconst_4),
782 INSN_LABEL(iconst_5),
783 INSN_LABEL(lconst_0),
784 INSN_LABEL(lconst_1),
785 INSN_LABEL(fconst_0),
786 INSN_LABEL(fconst_1),
787 INSN_LABEL(fconst_2),
788 INSN_LABEL(dconst_0),
789 INSN_LABEL(dconst_1),
790 INSN_LABEL(bipush),
791 INSN_LABEL(sipush),
792 INSN_LABEL(ldc),
793 INSN_LABEL(ldc_w),
794 INSN_LABEL(ldc2_w),
795 INSN_LABEL(iload),
796 INSN_LABEL(lload),
797 INSN_LABEL(fload),
798 INSN_LABEL(dload),
799 INSN_LABEL(aload),
800 INSN_LABEL(iload_0),
801 INSN_LABEL(iload_1),
802 INSN_LABEL(iload_2),
803 INSN_LABEL(iload_3),
804 INSN_LABEL(lload_0),
805 INSN_LABEL(lload_1),
806 INSN_LABEL(lload_2),
807 INSN_LABEL(lload_3),
808 INSN_LABEL(fload_0),
809 INSN_LABEL(fload_1),
810 INSN_LABEL(fload_2),
811 INSN_LABEL(fload_3),
812 INSN_LABEL(dload_0),
813 INSN_LABEL(dload_1),
814 INSN_LABEL(dload_2),
815 INSN_LABEL(dload_3),
816 INSN_LABEL(aload_0),
817 INSN_LABEL(aload_1),
818 INSN_LABEL(aload_2),
819 INSN_LABEL(aload_3),
820 INSN_LABEL(iaload),
821 INSN_LABEL(laload),
822 INSN_LABEL(faload),
823 INSN_LABEL(daload),
824 INSN_LABEL(aaload),
825 INSN_LABEL(baload),
826 INSN_LABEL(caload),
827 INSN_LABEL(saload),
828 INSN_LABEL(istore),
829 INSN_LABEL(lstore),
830 INSN_LABEL(fstore),
831 INSN_LABEL(dstore),
832 INSN_LABEL(astore),
833 INSN_LABEL(istore_0),
834 INSN_LABEL(istore_1),
835 INSN_LABEL(istore_2),
836 INSN_LABEL(istore_3),
837 INSN_LABEL(lstore_0),
838 INSN_LABEL(lstore_1),
839 INSN_LABEL(lstore_2),
840 INSN_LABEL(lstore_3),
841 INSN_LABEL(fstore_0),
842 INSN_LABEL(fstore_1),
843 INSN_LABEL(fstore_2),
844 INSN_LABEL(fstore_3),
845 INSN_LABEL(dstore_0),
846 INSN_LABEL(dstore_1),
847 INSN_LABEL(dstore_2),
848 INSN_LABEL(dstore_3),
849 INSN_LABEL(astore_0),
850 INSN_LABEL(astore_1),
851 INSN_LABEL(astore_2),
852 INSN_LABEL(astore_3),
853 INSN_LABEL(iastore),
854 INSN_LABEL(lastore),
855 INSN_LABEL(fastore),
856 INSN_LABEL(dastore),
857 INSN_LABEL(aastore),
858 INSN_LABEL(bastore),
859 INSN_LABEL(castore),
860 INSN_LABEL(sastore),
861 INSN_LABEL(pop),
862 INSN_LABEL(pop2),
863 INSN_LABEL(dup),
864 INSN_LABEL(dup_x1),
865 INSN_LABEL(dup_x2),
866 INSN_LABEL(dup2),
867 INSN_LABEL(dup2_x1),
868 INSN_LABEL(dup2_x2),
869 INSN_LABEL(swap),
870 INSN_LABEL(iadd),
871 INSN_LABEL(ladd),
872 INSN_LABEL(fadd),
873 INSN_LABEL(dadd),
874 INSN_LABEL(isub),
875 INSN_LABEL(lsub),
876 INSN_LABEL(fsub),
877 INSN_LABEL(dsub),
878 INSN_LABEL(imul),
879 INSN_LABEL(lmul),
880 INSN_LABEL(fmul),
881 INSN_LABEL(dmul),
882 INSN_LABEL(idiv),
883 INSN_LABEL(ldiv),
884 INSN_LABEL(fdiv),
885 INSN_LABEL(ddiv),
886 INSN_LABEL(irem),
887 INSN_LABEL(lrem),
888 INSN_LABEL(frem),
889 INSN_LABEL(drem),
890 INSN_LABEL(ineg),
891 INSN_LABEL(lneg),
892 INSN_LABEL(fneg),
893 INSN_LABEL(dneg),
894 INSN_LABEL(ishl),
895 INSN_LABEL(lshl),
896 INSN_LABEL(ishr),
897 INSN_LABEL(lshr),
898 INSN_LABEL(iushr),
899 INSN_LABEL(lushr),
900 INSN_LABEL(iand),
901 INSN_LABEL(land),
902 INSN_LABEL(ior),
903 INSN_LABEL(lor),
904 INSN_LABEL(ixor),
905 INSN_LABEL(lxor),
906 INSN_LABEL(iinc),
907 INSN_LABEL(i2l),
908 INSN_LABEL(i2f),
909 INSN_LABEL(i2d),
910 INSN_LABEL(l2i),
911 INSN_LABEL(l2f),
912 INSN_LABEL(l2d),
913 INSN_LABEL(f2i),
914 INSN_LABEL(f2l),
915 INSN_LABEL(f2d),
916 INSN_LABEL(d2i),
917 INSN_LABEL(d2l),
918 INSN_LABEL(d2f),
919 INSN_LABEL(i2b),
920 INSN_LABEL(i2c),
921 INSN_LABEL(i2s),
922 INSN_LABEL(lcmp),
923 INSN_LABEL(fcmpl),
924 INSN_LABEL(fcmpg),
925 INSN_LABEL(dcmpl),
926 INSN_LABEL(dcmpg),
927 INSN_LABEL(ifeq),
928 INSN_LABEL(ifne),
929 INSN_LABEL(iflt),
930 INSN_LABEL(ifge),
931 INSN_LABEL(ifgt),
932 INSN_LABEL(ifle),
933 INSN_LABEL(if_icmpeq),
934 INSN_LABEL(if_icmpne),
935 INSN_LABEL(if_icmplt),
936 INSN_LABEL(if_icmpge),
937 INSN_LABEL(if_icmpgt),
938 INSN_LABEL(if_icmple),
939 INSN_LABEL(if_acmpeq),
940 INSN_LABEL(if_acmpne),
941 INSN_LABEL(goto),
942 INSN_LABEL(jsr),
943 INSN_LABEL(ret),
944 INSN_LABEL(tableswitch),
945 INSN_LABEL(lookupswitch),
946 INSN_LABEL(ireturn),
947 INSN_LABEL(lreturn),
948 INSN_LABEL(freturn),
949 INSN_LABEL(dreturn),
950 INSN_LABEL(areturn),
951 INSN_LABEL(return),
952 INSN_LABEL(getstatic),
953 INSN_LABEL(putstatic),
954 INSN_LABEL(getfield),
955 INSN_LABEL(putfield),
956 INSN_LABEL(invokevirtual),
957 INSN_LABEL(invokespecial),
958 INSN_LABEL(invokestatic),
959 INSN_LABEL(invokeinterface),
960 0, /* Unused. */
961 INSN_LABEL(new),
962 INSN_LABEL(newarray),
963 INSN_LABEL(anewarray),
964 INSN_LABEL(arraylength),
965 INSN_LABEL(athrow),
966 INSN_LABEL(checkcast),
967 INSN_LABEL(instanceof),
968 INSN_LABEL(monitorenter),
969 INSN_LABEL(monitorexit),
970 #ifdef DIRECT_THREADED
971 0, // wide
972 #else
973 INSN_LABEL(wide),
974 #endif
975 INSN_LABEL(multianewarray),
976 INSN_LABEL(ifnull),
977 INSN_LABEL(ifnonnull),
978 INSN_LABEL(goto_w),
979 INSN_LABEL(jsr_w),
983 pc_t pc;
985 #ifdef DIRECT_THREADED
987 #define NEXT_INSN goto *((pc++)->insn)
988 #define INTVAL() ((pc++)->int_val)
989 #define AVAL() ((pc++)->datum)
991 #define GET1S() INTVAL ()
992 #define GET2S() INTVAL ()
993 #define GET1U() INTVAL ()
994 #define GET2U() INTVAL ()
995 #define AVAL1U() AVAL ()
996 #define AVAL2U() AVAL ()
997 #define AVAL2UP() AVAL ()
998 #define SKIP_GOTO ++pc
999 #define GOTO_VAL() (insn_slot *) pc->datum
1000 #define PCVAL(unionval) unionval.p
1001 #define AMPAMP(label) &&label
1003 // Compile if we must.
1004 if (prepared == NULL)
1005 compile (insn_target);
1006 pc = (insn_slot *) prepared;
1008 #else
1010 #define NEXT_INSN goto *(insn_target[*pc++])
1012 #define GET1S() get1s (pc++)
1013 #define GET2S() (pc += 2, get2s (pc- 2))
1014 #define GET1U() get1u (pc++)
1015 #define GET2U() (pc += 2, get2u (pc - 2))
1016 #define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
1017 #define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
1018 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1019 #define SKIP_GOTO pc += 2
1020 #define GOTO_VAL() pc - 1 + get2s (pc)
1021 #define PCVAL(unionval) unionval.i
1022 #define AMPAMP(label) NULL
1024 pc = bytecode ();
1026 #endif /* DIRECT_THREADED */
1028 #define TAKE_GOTO pc = GOTO_VAL ()
1032 // We keep nop around. It is used if we're interpreting the
1033 // bytecodes and not doing direct threading.
1034 insn_nop:
1035 NEXT_INSN;
1037 /* The first few instructions here are ordered according to their
1038 frequency, in the hope that this will improve code locality a
1039 little. */
1041 insn_aload_0: // 0x2a
1042 LOADA (0);
1043 NEXT_INSN;
1045 insn_iload: // 0x15
1046 LOADI (GET1U ());
1047 NEXT_INSN;
1049 insn_iload_1: // 0x1b
1050 LOADI (1);
1051 NEXT_INSN;
1053 insn_invokevirtual: // 0xb6
1055 int index = GET2U ();
1057 /* _Jv_ResolvePoolEntry returns immediately if the value already
1058 * is resolved. If we want to clutter up the code here to gain
1059 * a little performance, then we can check the corresponding bit
1060 * JV_CONSTANT_ResolvedFlag in the tag directly. For now, I
1061 * don't think it is worth it. */
1063 rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
1065 sp -= rmeth->stack_item_count;
1066 // We don't use NULLCHECK here because we can't rely on that
1067 // working if the method is final. So instead we do an
1068 // explicit test.
1069 if (! sp[0].o)
1070 throw new java::lang::NullPointerException;
1072 if (rmeth->vtable_index == -1)
1074 // final methods do not appear in the vtable,
1075 // if it does not appear in the superclass.
1076 fun = (void (*)()) rmeth->method->ncode;
1078 else
1080 jobject rcv = sp[0].o;
1081 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1082 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1085 #ifdef DIRECT_THREADED
1086 // Rewrite instruction so that we use a faster pre-resolved
1087 // method.
1088 pc[-2].insn = &&invokevirtual_resolved;
1089 pc[-1].datum = rmeth;
1090 #endif /* DIRECT_THREADED */
1092 goto perform_invoke;
1094 #ifdef DIRECT_THREADED
1095 invokevirtual_resolved:
1097 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1098 sp -= rmeth->stack_item_count;
1099 // We don't use NULLCHECK here because we can't rely on that
1100 // working if the method is final. So instead we do an
1101 // explicit test.
1102 if (! sp[0].o)
1103 throw new java::lang::NullPointerException;
1105 if (rmeth->vtable_index == -1)
1107 // final methods do not appear in the vtable,
1108 // if it does not appear in the superclass.
1109 fun = (void (*)()) rmeth->method->ncode;
1111 else
1113 jobject rcv = sp[0].o;
1114 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1115 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1118 goto perform_invoke;
1119 #endif /* DIRECT_THREADED */
1121 perform_invoke:
1123 /* here goes the magic again... */
1124 ffi_cif *cif = &rmeth->cif;
1125 ffi_raw *raw = (ffi_raw*) sp;
1127 jdouble rvalue;
1129 #if FFI_NATIVE_RAW_API
1130 /* We assume that this is only implemented if it's correct */
1131 /* to use it here. On a 64 bit machine, it never is. */
1132 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1133 #else
1134 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1135 #endif
1137 int rtype = cif->rtype->type;
1139 /* the likelyhood of object, int, or void return is very high,
1140 * so those are checked before the switch */
1141 if (rtype == FFI_TYPE_POINTER)
1143 PUSHA (*(jobject*)&rvalue);
1145 else if (rtype == FFI_TYPE_SINT32)
1147 PUSHI (*(jint*)&rvalue);
1149 else if (rtype == FFI_TYPE_VOID)
1151 /* skip */
1153 else
1155 switch (rtype)
1157 case FFI_TYPE_SINT8:
1159 jbyte value = (*(jint*)&rvalue) & 0xff;
1160 PUSHI (value);
1162 break;
1164 case FFI_TYPE_SINT16:
1166 jshort value = (*(jint*)&rvalue) & 0xffff;
1167 PUSHI (value);
1169 break;
1171 case FFI_TYPE_UINT16:
1173 jint value = (*(jint*)&rvalue) & 0xffff;
1174 PUSHI (value);
1176 break;
1178 case FFI_TYPE_FLOAT:
1179 PUSHF (*(jfloat*)&rvalue);
1180 break;
1182 case FFI_TYPE_DOUBLE:
1183 PUSHD (rvalue);
1184 break;
1186 case FFI_TYPE_SINT64:
1187 PUSHL (*(jlong*)&rvalue);
1188 break;
1190 default:
1191 throw_internal_error ("unknown return type in invokeXXX");
1195 NEXT_INSN;
1197 insn_aconst_null:
1198 PUSHA (NULL);
1199 NEXT_INSN;
1201 insn_iconst_m1:
1202 PUSHI (-1);
1203 NEXT_INSN;
1205 insn_iconst_0:
1206 PUSHI (0);
1207 NEXT_INSN;
1209 insn_iconst_1:
1210 PUSHI (1);
1211 NEXT_INSN;
1213 insn_iconst_2:
1214 PUSHI (2);
1215 NEXT_INSN;
1217 insn_iconst_3:
1218 PUSHI (3);
1219 NEXT_INSN;
1221 insn_iconst_4:
1222 PUSHI (4);
1223 NEXT_INSN;
1225 insn_iconst_5:
1226 PUSHI (5);
1227 NEXT_INSN;
1229 insn_lconst_0:
1230 PUSHL (0);
1231 NEXT_INSN;
1233 insn_lconst_1:
1234 PUSHL (1);
1235 NEXT_INSN;
1237 insn_fconst_0:
1238 PUSHF (0);
1239 NEXT_INSN;
1241 insn_fconst_1:
1242 PUSHF (1);
1243 NEXT_INSN;
1245 insn_fconst_2:
1246 PUSHF (2);
1247 NEXT_INSN;
1249 insn_dconst_0:
1250 PUSHD (0);
1251 NEXT_INSN;
1253 insn_dconst_1:
1254 PUSHD (1);
1255 NEXT_INSN;
1257 insn_bipush:
1258 // For direct threaded, bipush and sipush are the same.
1259 #ifndef DIRECT_THREADED
1260 PUSHI (GET1S ());
1261 NEXT_INSN;
1262 #endif /* DIRECT_THREADED */
1263 insn_sipush:
1264 PUSHI (GET2S ());
1265 NEXT_INSN;
1267 insn_ldc:
1268 // For direct threaded, ldc and ldc_w are the same.
1269 #ifndef DIRECT_THREADED
1270 PUSHA ((jobject) AVAL1U ());
1271 NEXT_INSN;
1272 #endif /* DIRECT_THREADED */
1273 insn_ldc_w:
1274 PUSHA ((jobject) AVAL2U ());
1275 NEXT_INSN;
1277 insn_ldc2_w:
1279 void *where = AVAL2UP ();
1280 memcpy (sp, where, 2*sizeof (_Jv_word));
1281 sp += 2;
1283 NEXT_INSN;
1285 insn_lload:
1286 LOADL (GET1U ());
1287 NEXT_INSN;
1289 insn_fload:
1290 LOADF (GET1U ());
1291 NEXT_INSN;
1293 insn_dload:
1294 LOADD (GET1U ());
1295 NEXT_INSN;
1297 insn_aload:
1298 LOADA (GET1U ());
1299 NEXT_INSN;
1301 insn_iload_0:
1302 LOADI (0);
1303 NEXT_INSN;
1305 insn_iload_2:
1306 LOADI (2);
1307 NEXT_INSN;
1309 insn_iload_3:
1310 LOADI (3);
1311 NEXT_INSN;
1313 insn_lload_0:
1314 LOADL (0);
1315 NEXT_INSN;
1317 insn_lload_1:
1318 LOADL (1);
1319 NEXT_INSN;
1321 insn_lload_2:
1322 LOADL (2);
1323 NEXT_INSN;
1325 insn_lload_3:
1326 LOADL (3);
1327 NEXT_INSN;
1329 insn_fload_0:
1330 LOADF (0);
1331 NEXT_INSN;
1333 insn_fload_1:
1334 LOADF (1);
1335 NEXT_INSN;
1337 insn_fload_2:
1338 LOADF (2);
1339 NEXT_INSN;
1341 insn_fload_3:
1342 LOADF (3);
1343 NEXT_INSN;
1345 insn_dload_0:
1346 LOADD (0);
1347 NEXT_INSN;
1349 insn_dload_1:
1350 LOADD (1);
1351 NEXT_INSN;
1353 insn_dload_2:
1354 LOADD (2);
1355 NEXT_INSN;
1357 insn_dload_3:
1358 LOADD (3);
1359 NEXT_INSN;
1361 insn_aload_1:
1362 LOADA(1);
1363 NEXT_INSN;
1365 insn_aload_2:
1366 LOADA(2);
1367 NEXT_INSN;
1369 insn_aload_3:
1370 LOADA(3);
1371 NEXT_INSN;
1373 insn_iaload:
1375 jint index = POPI();
1376 jintArray arr = (jintArray) POPA();
1377 NULLARRAYCHECK (arr);
1378 ARRAYBOUNDSCHECK (arr, index);
1379 PUSHI( elements(arr)[index] );
1381 NEXT_INSN;
1383 insn_laload:
1385 jint index = POPI();
1386 jlongArray arr = (jlongArray) POPA();
1387 NULLARRAYCHECK (arr);
1388 ARRAYBOUNDSCHECK (arr, index);
1389 PUSHL( elements(arr)[index] );
1391 NEXT_INSN;
1393 insn_faload:
1395 jint index = POPI();
1396 jfloatArray arr = (jfloatArray) POPA();
1397 NULLARRAYCHECK (arr);
1398 ARRAYBOUNDSCHECK (arr, index);
1399 PUSHF( elements(arr)[index] );
1401 NEXT_INSN;
1403 insn_daload:
1405 jint index = POPI();
1406 jdoubleArray arr = (jdoubleArray) POPA();
1407 NULLARRAYCHECK (arr);
1408 ARRAYBOUNDSCHECK (arr, index);
1409 PUSHD( elements(arr)[index] );
1411 NEXT_INSN;
1413 insn_aaload:
1415 jint index = POPI();
1416 jobjectArray arr = (jobjectArray) POPA();
1417 NULLARRAYCHECK (arr);
1418 ARRAYBOUNDSCHECK (arr, index);
1419 PUSHA( elements(arr)[index] );
1421 NEXT_INSN;
1423 insn_baload:
1425 jint index = POPI();
1426 jbyteArray arr = (jbyteArray) POPA();
1427 NULLARRAYCHECK (arr);
1428 ARRAYBOUNDSCHECK (arr, index);
1429 PUSHI( elements(arr)[index] );
1431 NEXT_INSN;
1433 insn_caload:
1435 jint index = POPI();
1436 jcharArray arr = (jcharArray) POPA();
1437 NULLARRAYCHECK (arr);
1438 ARRAYBOUNDSCHECK (arr, index);
1439 PUSHI( elements(arr)[index] );
1441 NEXT_INSN;
1443 insn_saload:
1445 jint index = POPI();
1446 jshortArray arr = (jshortArray) POPA();
1447 NULLARRAYCHECK (arr);
1448 ARRAYBOUNDSCHECK (arr, index);
1449 PUSHI( elements(arr)[index] );
1451 NEXT_INSN;
1453 insn_istore:
1454 STOREI (GET1U ());
1455 NEXT_INSN;
1457 insn_lstore:
1458 STOREL (GET1U ());
1459 NEXT_INSN;
1461 insn_fstore:
1462 STOREF (GET1U ());
1463 NEXT_INSN;
1465 insn_dstore:
1466 STORED (GET1U ());
1467 NEXT_INSN;
1469 insn_astore:
1470 STOREA (GET1U ());
1471 NEXT_INSN;
1473 insn_istore_0:
1474 STOREI (0);
1475 NEXT_INSN;
1477 insn_istore_1:
1478 STOREI (1);
1479 NEXT_INSN;
1481 insn_istore_2:
1482 STOREI (2);
1483 NEXT_INSN;
1485 insn_istore_3:
1486 STOREI (3);
1487 NEXT_INSN;
1489 insn_lstore_0:
1490 STOREL (0);
1491 NEXT_INSN;
1493 insn_lstore_1:
1494 STOREL (1);
1495 NEXT_INSN;
1497 insn_lstore_2:
1498 STOREL (2);
1499 NEXT_INSN;
1501 insn_lstore_3:
1502 STOREL (3);
1503 NEXT_INSN;
1505 insn_fstore_0:
1506 STOREF (0);
1507 NEXT_INSN;
1509 insn_fstore_1:
1510 STOREF (1);
1511 NEXT_INSN;
1513 insn_fstore_2:
1514 STOREF (2);
1515 NEXT_INSN;
1517 insn_fstore_3:
1518 STOREF (3);
1519 NEXT_INSN;
1521 insn_dstore_0:
1522 STORED (0);
1523 NEXT_INSN;
1525 insn_dstore_1:
1526 STORED (1);
1527 NEXT_INSN;
1529 insn_dstore_2:
1530 STORED (2);
1531 NEXT_INSN;
1533 insn_dstore_3:
1534 STORED (3);
1535 NEXT_INSN;
1537 insn_astore_0:
1538 STOREA(0);
1539 NEXT_INSN;
1541 insn_astore_1:
1542 STOREA(1);
1543 NEXT_INSN;
1545 insn_astore_2:
1546 STOREA(2);
1547 NEXT_INSN;
1549 insn_astore_3:
1550 STOREA(3);
1551 NEXT_INSN;
1553 insn_iastore:
1555 jint value = POPI();
1556 jint index = POPI();
1557 jintArray arr = (jintArray) POPA();
1558 NULLARRAYCHECK (arr);
1559 ARRAYBOUNDSCHECK (arr, index);
1560 elements(arr)[index] = value;
1562 NEXT_INSN;
1564 insn_lastore:
1566 jlong value = POPL();
1567 jint index = POPI();
1568 jlongArray arr = (jlongArray) POPA();
1569 NULLARRAYCHECK (arr);
1570 ARRAYBOUNDSCHECK (arr, index);
1571 elements(arr)[index] = value;
1573 NEXT_INSN;
1575 insn_fastore:
1577 jfloat value = POPF();
1578 jint index = POPI();
1579 jfloatArray arr = (jfloatArray) POPA();
1580 NULLARRAYCHECK (arr);
1581 ARRAYBOUNDSCHECK (arr, index);
1582 elements(arr)[index] = value;
1584 NEXT_INSN;
1586 insn_dastore:
1588 jdouble value = POPD();
1589 jint index = POPI();
1590 jdoubleArray arr = (jdoubleArray) POPA();
1591 NULLARRAYCHECK (arr);
1592 ARRAYBOUNDSCHECK (arr, index);
1593 elements(arr)[index] = value;
1595 NEXT_INSN;
1597 insn_aastore:
1599 jobject value = POPA();
1600 jint index = POPI();
1601 jobjectArray arr = (jobjectArray) POPA();
1602 NULLARRAYCHECK (arr);
1603 ARRAYBOUNDSCHECK (arr, index);
1604 _Jv_CheckArrayStore (arr, value);
1605 elements(arr)[index] = value;
1607 NEXT_INSN;
1609 insn_bastore:
1611 jbyte value = (jbyte) POPI();
1612 jint index = POPI();
1613 jbyteArray arr = (jbyteArray) POPA();
1614 NULLARRAYCHECK (arr);
1615 ARRAYBOUNDSCHECK (arr, index);
1616 elements(arr)[index] = value;
1618 NEXT_INSN;
1620 insn_castore:
1622 jchar value = (jchar) POPI();
1623 jint index = POPI();
1624 jcharArray arr = (jcharArray) POPA();
1625 NULLARRAYCHECK (arr);
1626 ARRAYBOUNDSCHECK (arr, index);
1627 elements(arr)[index] = value;
1629 NEXT_INSN;
1631 insn_sastore:
1633 jshort value = (jshort) POPI();
1634 jint index = POPI();
1635 jshortArray arr = (jshortArray) POPA();
1636 NULLARRAYCHECK (arr);
1637 ARRAYBOUNDSCHECK (arr, index);
1638 elements(arr)[index] = value;
1640 NEXT_INSN;
1642 insn_pop:
1643 sp -= 1;
1644 NEXT_INSN;
1646 insn_pop2:
1647 sp -= 2;
1648 NEXT_INSN;
1650 insn_dup:
1651 sp[0] = sp[-1];
1652 sp += 1;
1653 NEXT_INSN;
1655 insn_dup_x1:
1656 dupx (sp, 1, 1); sp+=1;
1657 NEXT_INSN;
1659 insn_dup_x2:
1660 dupx (sp, 1, 2); sp+=1;
1661 NEXT_INSN;
1663 insn_dup2:
1664 sp[0] = sp[-2];
1665 sp[1] = sp[-1];
1666 sp += 2;
1667 NEXT_INSN;
1669 insn_dup2_x1:
1670 dupx (sp, 2, 1); sp+=2;
1671 NEXT_INSN;
1673 insn_dup2_x2:
1674 dupx (sp, 2, 2); sp+=2;
1675 NEXT_INSN;
1677 insn_swap:
1679 jobject tmp1 = POPA();
1680 jobject tmp2 = POPA();
1681 PUSHA (tmp1);
1682 PUSHA (tmp2);
1684 NEXT_INSN;
1686 insn_iadd:
1687 BINOPI(+);
1688 NEXT_INSN;
1690 insn_ladd:
1691 BINOPL(+);
1692 NEXT_INSN;
1694 insn_fadd:
1695 BINOPF(+);
1696 NEXT_INSN;
1698 insn_dadd:
1699 BINOPD(+);
1700 NEXT_INSN;
1702 insn_isub:
1703 BINOPI(-);
1704 NEXT_INSN;
1706 insn_lsub:
1707 BINOPL(-);
1708 NEXT_INSN;
1710 insn_fsub:
1711 BINOPF(-);
1712 NEXT_INSN;
1714 insn_dsub:
1715 BINOPD(-);
1716 NEXT_INSN;
1718 insn_imul:
1719 BINOPI(*);
1720 NEXT_INSN;
1722 insn_lmul:
1723 BINOPL(*);
1724 NEXT_INSN;
1726 insn_fmul:
1727 BINOPF(*);
1728 NEXT_INSN;
1730 insn_dmul:
1731 BINOPD(*);
1732 NEXT_INSN;
1734 insn_idiv:
1736 jint value2 = POPI();
1737 jint value1 = POPI();
1738 jint res = _Jv_divI (value1, value2);
1739 PUSHI (res);
1741 NEXT_INSN;
1743 insn_ldiv:
1745 jlong value2 = POPL();
1746 jlong value1 = POPL();
1747 jlong res = _Jv_divJ (value1, value2);
1748 PUSHL (res);
1750 NEXT_INSN;
1752 insn_fdiv:
1754 jfloat value2 = POPF();
1755 jfloat value1 = POPF();
1756 jfloat res = value1 / value2;
1757 PUSHF (res);
1759 NEXT_INSN;
1761 insn_ddiv:
1763 jdouble value2 = POPD();
1764 jdouble value1 = POPD();
1765 jdouble res = value1 / value2;
1766 PUSHD (res);
1768 NEXT_INSN;
1770 insn_irem:
1772 jint value2 = POPI();
1773 jint value1 = POPI();
1774 jint res = _Jv_remI (value1, value2);
1775 PUSHI (res);
1777 NEXT_INSN;
1779 insn_lrem:
1781 jlong value2 = POPL();
1782 jlong value1 = POPL();
1783 jlong res = _Jv_remJ (value1, value2);
1784 PUSHL (res);
1786 NEXT_INSN;
1788 insn_frem:
1790 jfloat value2 = POPF();
1791 jfloat value1 = POPF();
1792 jfloat res = __ieee754_fmod (value1, value2);
1793 PUSHF (res);
1795 NEXT_INSN;
1797 insn_drem:
1799 jdouble value2 = POPD();
1800 jdouble value1 = POPD();
1801 jdouble res = __ieee754_fmod (value1, value2);
1802 PUSHD (res);
1804 NEXT_INSN;
1806 insn_ineg:
1808 jint value = POPI();
1809 PUSHI (value * -1);
1811 NEXT_INSN;
1813 insn_lneg:
1815 jlong value = POPL();
1816 PUSHL (value * -1);
1818 NEXT_INSN;
1820 insn_fneg:
1822 jfloat value = POPF();
1823 PUSHF (value * -1);
1825 NEXT_INSN;
1827 insn_dneg:
1829 jdouble value = POPD();
1830 PUSHD (value * -1);
1832 NEXT_INSN;
1834 insn_ishl:
1836 jint shift = (POPI() & 0x1f);
1837 jint value = POPI();
1838 PUSHI (value << shift);
1840 NEXT_INSN;
1842 insn_lshl:
1844 jint shift = (POPI() & 0x3f);
1845 jlong value = POPL();
1846 PUSHL (value << shift);
1848 NEXT_INSN;
1850 insn_ishr:
1852 jint shift = (POPI() & 0x1f);
1853 jint value = POPI();
1854 PUSHI (value >> shift);
1856 NEXT_INSN;
1858 insn_lshr:
1860 jint shift = (POPI() & 0x3f);
1861 jlong value = POPL();
1862 PUSHL (value >> shift);
1864 NEXT_INSN;
1866 insn_iushr:
1868 jint shift = (POPI() & 0x1f);
1869 unsigned long value = POPI();
1870 PUSHI ((jint) (value >> shift));
1872 NEXT_INSN;
1874 insn_lushr:
1876 jint shift = (POPI() & 0x3f);
1877 UINT64 value = (UINT64) POPL();
1878 PUSHL ((value >> shift));
1880 NEXT_INSN;
1882 insn_iand:
1883 BINOPI (&);
1884 NEXT_INSN;
1886 insn_land:
1887 BINOPL (&);
1888 NEXT_INSN;
1890 insn_ior:
1891 BINOPI (|);
1892 NEXT_INSN;
1894 insn_lor:
1895 BINOPL (|);
1896 NEXT_INSN;
1898 insn_ixor:
1899 BINOPI (^);
1900 NEXT_INSN;
1902 insn_lxor:
1903 BINOPL (^);
1904 NEXT_INSN;
1906 insn_iinc:
1908 jint index = GET1U ();
1909 jint amount = GET1S ();
1910 locals[index].i += amount;
1912 NEXT_INSN;
1914 insn_i2l:
1915 {jlong value = POPI(); PUSHL (value);}
1916 NEXT_INSN;
1918 insn_i2f:
1919 {jfloat value = POPI(); PUSHF (value);}
1920 NEXT_INSN;
1922 insn_i2d:
1923 {jdouble value = POPI(); PUSHD (value);}
1924 NEXT_INSN;
1926 insn_l2i:
1927 {jint value = POPL(); PUSHI (value);}
1928 NEXT_INSN;
1930 insn_l2f:
1931 {jfloat value = POPL(); PUSHF (value);}
1932 NEXT_INSN;
1934 insn_l2d:
1935 {jdouble value = POPL(); PUSHD (value);}
1936 NEXT_INSN;
1938 insn_f2i:
1940 using namespace java::lang;
1941 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1942 PUSHI(value);
1944 NEXT_INSN;
1946 insn_f2l:
1948 using namespace java::lang;
1949 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
1950 PUSHL(value);
1952 NEXT_INSN;
1954 insn_f2d:
1955 { jdouble value = POPF (); PUSHD(value); }
1956 NEXT_INSN;
1958 insn_d2i:
1960 using namespace java::lang;
1961 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1962 PUSHI(value);
1964 NEXT_INSN;
1966 insn_d2l:
1968 using namespace java::lang;
1969 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
1970 PUSHL(value);
1972 NEXT_INSN;
1974 insn_d2f:
1975 { jfloat value = POPD (); PUSHF(value); }
1976 NEXT_INSN;
1978 insn_i2b:
1979 { jbyte value = POPI (); PUSHI(value); }
1980 NEXT_INSN;
1982 insn_i2c:
1983 { jchar value = POPI (); PUSHI(value); }
1984 NEXT_INSN;
1986 insn_i2s:
1987 { jshort value = POPI (); PUSHI(value); }
1988 NEXT_INSN;
1990 insn_lcmp:
1992 jlong value2 = POPL ();
1993 jlong value1 = POPL ();
1994 if (value1 > value2)
1995 { PUSHI (1); }
1996 else if (value1 == value2)
1997 { PUSHI (0); }
1998 else
1999 { PUSHI (-1); }
2001 NEXT_INSN;
2003 insn_fcmpl:
2004 tmpval = -1;
2005 goto fcmp;
2007 insn_fcmpg:
2008 tmpval = 1;
2010 fcmp:
2012 jfloat value2 = POPF ();
2013 jfloat value1 = POPF ();
2014 if (value1 > value2)
2015 PUSHI (1);
2016 else if (value1 == value2)
2017 PUSHI (0);
2018 else if (value1 < value2)
2019 PUSHI (-1);
2020 else
2021 PUSHI (tmpval);
2023 NEXT_INSN;
2025 insn_dcmpl:
2026 tmpval = 1;
2027 goto dcmp;
2029 insn_dcmpg:
2030 tmpval = -1;
2032 dcmp:
2034 jdouble value2 = POPD ();
2035 jdouble value1 = POPD ();
2036 if (value1 > value2)
2037 PUSHI (1);
2038 else if (value1 == value2)
2039 PUSHI (0);
2040 else if (value1 < value2)
2041 PUSHI (-1);
2042 else
2043 PUSHI (tmpval);
2045 NEXT_INSN;
2047 insn_ifeq:
2049 if (POPI() == 0)
2050 TAKE_GOTO;
2051 else
2052 SKIP_GOTO;
2054 NEXT_INSN;
2056 insn_ifne:
2058 if (POPI() != 0)
2059 TAKE_GOTO;
2060 else
2061 SKIP_GOTO;
2063 NEXT_INSN;
2065 insn_iflt:
2067 if (POPI() < 0)
2068 TAKE_GOTO;
2069 else
2070 SKIP_GOTO;
2072 NEXT_INSN;
2074 insn_ifge:
2076 if (POPI() >= 0)
2077 TAKE_GOTO;
2078 else
2079 SKIP_GOTO;
2081 NEXT_INSN;
2083 insn_ifgt:
2085 if (POPI() > 0)
2086 TAKE_GOTO;
2087 else
2088 SKIP_GOTO;
2090 NEXT_INSN;
2092 insn_ifle:
2094 if (POPI() <= 0)
2095 TAKE_GOTO;
2096 else
2097 SKIP_GOTO;
2099 NEXT_INSN;
2101 insn_if_icmpeq:
2103 jint value2 = POPI();
2104 jint value1 = POPI();
2105 if (value1 == value2)
2106 TAKE_GOTO;
2107 else
2108 SKIP_GOTO;
2110 NEXT_INSN;
2112 insn_if_icmpne:
2114 jint value2 = POPI();
2115 jint value1 = POPI();
2116 if (value1 != value2)
2117 TAKE_GOTO;
2118 else
2119 SKIP_GOTO;
2121 NEXT_INSN;
2123 insn_if_icmplt:
2125 jint value2 = POPI();
2126 jint value1 = POPI();
2127 if (value1 < value2)
2128 TAKE_GOTO;
2129 else
2130 SKIP_GOTO;
2132 NEXT_INSN;
2134 insn_if_icmpge:
2136 jint value2 = POPI();
2137 jint value1 = POPI();
2138 if (value1 >= value2)
2139 TAKE_GOTO;
2140 else
2141 SKIP_GOTO;
2143 NEXT_INSN;
2145 insn_if_icmpgt:
2147 jint value2 = POPI();
2148 jint value1 = POPI();
2149 if (value1 > value2)
2150 TAKE_GOTO;
2151 else
2152 SKIP_GOTO;
2154 NEXT_INSN;
2156 insn_if_icmple:
2158 jint value2 = POPI();
2159 jint value1 = POPI();
2160 if (value1 <= value2)
2161 TAKE_GOTO;
2162 else
2163 SKIP_GOTO;
2165 NEXT_INSN;
2167 insn_if_acmpeq:
2169 jobject value2 = POPA();
2170 jobject value1 = POPA();
2171 if (value1 == value2)
2172 TAKE_GOTO;
2173 else
2174 SKIP_GOTO;
2176 NEXT_INSN;
2178 insn_if_acmpne:
2180 jobject value2 = POPA();
2181 jobject value1 = POPA();
2182 if (value1 != value2)
2183 TAKE_GOTO;
2184 else
2185 SKIP_GOTO;
2187 NEXT_INSN;
2189 insn_goto_w:
2190 #ifndef DIRECT_THREADED
2191 // For direct threaded, goto and goto_w are the same.
2192 pc = pc - 1 + get4 (pc);
2193 NEXT_INSN;
2194 #endif /* DIRECT_THREADED */
2195 insn_goto:
2196 TAKE_GOTO;
2197 NEXT_INSN;
2199 insn_jsr_w:
2200 #ifndef DIRECT_THREADED
2201 // For direct threaded, jsr and jsr_w are the same.
2203 pc_t next = pc - 1 + get4 (pc);
2204 pc += 4;
2205 PUSHA ((jobject) pc);
2206 pc = next;
2208 NEXT_INSN;
2209 #endif /* DIRECT_THREADED */
2210 insn_jsr:
2212 pc_t next = GOTO_VAL();
2213 SKIP_GOTO;
2214 PUSHA ((jobject) pc);
2215 pc = next;
2217 NEXT_INSN;
2219 insn_ret:
2221 jint index = GET1U ();
2222 pc = (pc_t) PEEKA (index);
2224 NEXT_INSN;
2226 insn_tableswitch:
2228 #ifdef DIRECT_THREADED
2229 void *def = (pc++)->datum;
2231 int index = POPI();
2233 jint low = INTVAL ();
2234 jint high = INTVAL ();
2236 if (index < low || index > high)
2237 pc = (insn_slot *) def;
2238 else
2239 pc = (insn_slot *) ((pc + index - low)->datum);
2240 #else
2241 pc_t base_pc = pc - 1;
2242 int index = POPI ();
2244 pc_t base = (pc_t) bytecode ();
2245 while ((pc - base) % 4 != 0)
2246 ++pc;
2248 jint def = get4 (pc);
2249 jint low = get4 (pc + 4);
2250 jint high = get4 (pc + 8);
2251 if (index < low || index > high)
2252 pc = base_pc + def;
2253 else
2254 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2255 #endif /* DIRECT_THREADED */
2257 NEXT_INSN;
2259 insn_lookupswitch:
2261 #ifdef DIRECT_THREADED
2262 void *def = (pc++)->insn;
2264 int index = POPI();
2266 jint npairs = INTVAL ();
2268 int max = npairs - 1;
2269 int min = 0;
2271 // Simple binary search...
2272 while (min < max)
2274 int half = (min + max) / 2;
2275 int match = pc[2 * half].int_val;
2277 if (index == match)
2279 // Found it.
2280 pc = (insn_slot *) pc[2 * half + 1].datum;
2281 NEXT_INSN;
2283 else if (index < match)
2284 // We can use HALF - 1 here because we check again on
2285 // loop exit.
2286 max = half - 1;
2287 else
2288 // We can use HALF + 1 here because we check again on
2289 // loop exit.
2290 min = half + 1;
2292 if (index == pc[2 * min].int_val)
2293 pc = (insn_slot *) pc[2 * min + 1].datum;
2294 else
2295 pc = (insn_slot *) def;
2296 #else
2297 unsigned char *base_pc = pc-1;
2298 int index = POPI();
2300 unsigned char* base = bytecode ();
2301 while ((pc-base) % 4 != 0)
2302 ++pc;
2304 jint def = get4 (pc);
2305 jint npairs = get4 (pc+4);
2307 int max = npairs-1;
2308 int min = 0;
2310 // Simple binary search...
2311 while (min < max)
2313 int half = (min+max)/2;
2314 int match = get4 (pc+ 4*(2 + 2*half));
2316 if (index == match)
2317 min = max = half;
2318 else if (index < match)
2319 // We can use HALF - 1 here because we check again on
2320 // loop exit.
2321 max = half - 1;
2322 else
2323 // We can use HALF + 1 here because we check again on
2324 // loop exit.
2325 min = half + 1;
2328 if (index == get4 (pc+ 4*(2 + 2*min)))
2329 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2330 else
2331 pc = base_pc + def;
2332 #endif /* DIRECT_THREADED */
2334 NEXT_INSN;
2336 insn_areturn:
2337 *(jobject *) retp = POPA ();
2338 return;
2340 insn_lreturn:
2341 *(jlong *) retp = POPL ();
2342 return;
2344 insn_freturn:
2345 *(jfloat *) retp = POPF ();
2346 return;
2348 insn_dreturn:
2349 *(jdouble *) retp = POPD ();
2350 return;
2352 insn_ireturn:
2353 *(jint *) retp = POPI ();
2354 return;
2356 insn_return:
2357 return;
2359 insn_getstatic:
2361 jint fieldref_index = GET2U ();
2362 _Jv_ResolvePoolEntry (defining_class, fieldref_index);
2363 _Jv_Field *field = pool_data[fieldref_index].field;
2365 if ((field->flags & Modifier::STATIC) == 0)
2366 throw_incompatible_class_change_error
2367 (JvNewStringLatin1 ("field no longer static"));
2369 jclass type = field->type;
2371 // We rewrite the instruction once we discover what it refers
2372 // to.
2373 void *newinsn = NULL;
2374 if (type->isPrimitive ())
2376 switch (type->size_in_bytes)
2378 case 1:
2379 PUSHI (*(jbyte*) (field->u.addr));
2380 newinsn = AMPAMP (getstatic_resolved_1);
2381 break;
2383 case 2:
2384 if (type == JvPrimClass (char))
2386 PUSHI(*(jchar*) (field->u.addr));
2387 newinsn = AMPAMP (getstatic_resolved_char);
2389 else
2391 PUSHI(*(jshort*) (field->u.addr));
2392 newinsn = AMPAMP (getstatic_resolved_short);
2394 break;
2396 case 4:
2397 PUSHI(*(jint*) (field->u.addr));
2398 newinsn = AMPAMP (getstatic_resolved_4);
2399 break;
2401 case 8:
2402 PUSHL(*(jlong*) (field->u.addr));
2403 newinsn = AMPAMP (getstatic_resolved_8);
2404 break;
2407 else
2409 PUSHA(*(jobject*) (field->u.addr));
2410 newinsn = AMPAMP (getstatic_resolved_obj);
2413 #ifdef DIRECT_THREADED
2414 pc[-2].insn = newinsn;
2415 pc[-1].datum = field->u.addr;
2416 #endif /* DIRECT_THREADED */
2418 NEXT_INSN;
2420 #ifdef DIRECT_THREADED
2421 getstatic_resolved_1:
2422 PUSHI (*(jbyte *) AVAL ());
2423 NEXT_INSN;
2425 getstatic_resolved_char:
2426 PUSHI (*(jchar *) AVAL ());
2427 NEXT_INSN;
2429 getstatic_resolved_short:
2430 PUSHI (*(jshort *) AVAL ());
2431 NEXT_INSN;
2433 getstatic_resolved_4:
2434 PUSHI (*(jint *) AVAL ());
2435 NEXT_INSN;
2437 getstatic_resolved_8:
2438 PUSHL (*(jlong *) AVAL ());
2439 NEXT_INSN;
2441 getstatic_resolved_obj:
2442 PUSHA (*(jobject *) AVAL ());
2443 NEXT_INSN;
2444 #endif /* DIRECT_THREADED */
2446 insn_getfield:
2448 jint fieldref_index = GET2U ();
2449 _Jv_ResolvePoolEntry (defining_class, fieldref_index);
2450 _Jv_Field *field = pool_data[fieldref_index].field;
2452 if ((field->flags & Modifier::STATIC) != 0)
2453 throw_incompatible_class_change_error
2454 (JvNewStringLatin1 ("field is static"));
2456 jclass type = field->type;
2457 jint field_offset = field->u.boffset;
2458 if (field_offset > 0xffff)
2459 throw new java::lang::VirtualMachineError;
2461 jobject obj = POPA();
2462 NULLCHECK(obj);
2464 void *newinsn = NULL;
2465 if (type->isPrimitive ())
2467 switch (type->size_in_bytes)
2469 case 1:
2470 PUSHI (*(jbyte*) ((char*)obj + field_offset));
2471 newinsn = AMPAMP (getfield_resolved_1);
2472 break;
2474 case 2:
2475 if (type == JvPrimClass (char))
2477 PUSHI (*(jchar*) ((char*)obj + field_offset));
2478 newinsn = AMPAMP (getfield_resolved_char);
2480 else
2482 PUSHI (*(jshort*) ((char*)obj + field_offset));
2483 newinsn = AMPAMP (getfield_resolved_short);
2485 break;
2487 case 4:
2488 PUSHI (*(jint*) ((char*)obj + field_offset));
2489 newinsn = AMPAMP (getfield_resolved_4);
2490 break;
2492 case 8:
2493 PUSHL(*(jlong*) ((char*)obj + field_offset));
2494 newinsn = AMPAMP (getfield_resolved_8);
2495 break;
2498 else
2500 PUSHA(*(jobject*) ((char*)obj + field_offset));
2501 newinsn = AMPAMP (getfield_resolved_obj);
2504 #ifdef DIRECT_THREADED
2505 pc[-2].insn = newinsn;
2506 pc[-1].int_val = field_offset;
2507 #endif /* DIRECT_THREADED */
2509 NEXT_INSN;
2511 #ifdef DIRECT_THREADED
2512 getfield_resolved_1:
2514 char *obj = (char *) POPA ();
2515 NULLCHECK (obj);
2516 PUSHI (*(jbyte *) (obj + INTVAL ()));
2518 NEXT_INSN;
2520 getfield_resolved_char:
2522 char *obj = (char *) POPA ();
2523 NULLCHECK (obj);
2524 PUSHI (*(jchar *) (obj + INTVAL ()));
2526 NEXT_INSN;
2528 getfield_resolved_short:
2530 char *obj = (char *) POPA ();
2531 NULLCHECK (obj);
2532 PUSHI (*(jshort *) (obj + INTVAL ()));
2534 NEXT_INSN;
2536 getfield_resolved_4:
2538 char *obj = (char *) POPA ();
2539 NULLCHECK (obj);
2540 PUSHI (*(jint *) (obj + INTVAL ()));
2542 NEXT_INSN;
2544 getfield_resolved_8:
2546 char *obj = (char *) POPA ();
2547 NULLCHECK (obj);
2548 PUSHL (*(jlong *) (obj + INTVAL ()));
2550 NEXT_INSN;
2552 getfield_resolved_obj:
2554 char *obj = (char *) POPA ();
2555 NULLCHECK (obj);
2556 PUSHA (*(jobject *) (obj + INTVAL ()));
2558 NEXT_INSN;
2559 #endif /* DIRECT_THREADED */
2561 insn_putstatic:
2563 jint fieldref_index = GET2U ();
2564 _Jv_ResolvePoolEntry (defining_class, fieldref_index);
2565 _Jv_Field *field = pool_data[fieldref_index].field;
2567 jclass type = field->type;
2569 // ResolvePoolEntry cannot check this
2570 if ((field->flags & Modifier::STATIC) == 0)
2571 throw_incompatible_class_change_error
2572 (JvNewStringLatin1 ("field no longer static"));
2574 void *newinsn = NULL;
2575 if (type->isPrimitive ())
2577 switch (type->size_in_bytes)
2579 case 1:
2581 jint value = POPI();
2582 *(jbyte*) (field->u.addr) = value;
2583 newinsn = AMPAMP (putstatic_resolved_1);
2584 break;
2587 case 2:
2589 jint value = POPI();
2590 *(jchar*) (field->u.addr) = value;
2591 newinsn = AMPAMP (putstatic_resolved_2);
2592 break;
2595 case 4:
2597 jint value = POPI();
2598 *(jint*) (field->u.addr) = value;
2599 newinsn = AMPAMP (putstatic_resolved_4);
2600 break;
2603 case 8:
2605 jlong value = POPL();
2606 *(jlong*) (field->u.addr) = value;
2607 newinsn = AMPAMP (putstatic_resolved_8);
2608 break;
2612 else
2614 jobject value = POPA();
2615 *(jobject*) (field->u.addr) = value;
2616 newinsn = AMPAMP (putstatic_resolved_obj);
2619 #ifdef DIRECT_THREADED
2620 pc[-2].insn = newinsn;
2621 pc[-1].datum = field->u.addr;
2622 #endif /* DIRECT_THREADED */
2624 NEXT_INSN;
2626 #ifdef DIRECT_THREADED
2627 putstatic_resolved_1:
2628 *(jbyte *) AVAL () = POPI ();
2629 NEXT_INSN;
2631 putstatic_resolved_2:
2632 *(jchar *) AVAL () = POPI ();
2633 NEXT_INSN;
2635 putstatic_resolved_4:
2636 *(jint *) AVAL () = POPI ();
2637 NEXT_INSN;
2639 putstatic_resolved_8:
2640 *(jlong *) AVAL () = POPL ();
2641 NEXT_INSN;
2643 putstatic_resolved_obj:
2644 *(jobject *) AVAL () = POPA ();
2645 NEXT_INSN;
2646 #endif /* DIRECT_THREADED */
2648 insn_putfield:
2650 jint fieldref_index = GET2U ();
2651 _Jv_ResolvePoolEntry (defining_class, fieldref_index);
2652 _Jv_Field *field = pool_data[fieldref_index].field;
2654 jclass type = field->type;
2656 if ((field->flags & Modifier::STATIC) != 0)
2657 throw_incompatible_class_change_error
2658 (JvNewStringLatin1 ("field is static"));
2660 jint field_offset = field->u.boffset;
2661 if (field_offset > 0xffff)
2662 throw new java::lang::VirtualMachineError;
2664 void *newinsn = NULL;
2665 if (type->isPrimitive ())
2667 switch (type->size_in_bytes)
2669 case 1:
2671 jint value = POPI();
2672 jobject obj = POPA();
2673 NULLCHECK(obj);
2674 *(jbyte*) ((char*)obj + field_offset) = value;
2675 newinsn = AMPAMP (putfield_resolved_1);
2676 break;
2679 case 2:
2681 jint value = POPI();
2682 jobject obj = POPA();
2683 NULLCHECK(obj);
2684 *(jchar*) ((char*)obj + field_offset) = value;
2685 newinsn = AMPAMP (putfield_resolved_2);
2686 break;
2689 case 4:
2691 jint value = POPI();
2692 jobject obj = POPA();
2693 NULLCHECK(obj);
2694 *(jint*) ((char*)obj + field_offset) = value;
2695 newinsn = AMPAMP (putfield_resolved_4);
2696 break;
2699 case 8:
2701 jlong value = POPL();
2702 jobject obj = POPA();
2703 NULLCHECK(obj);
2704 *(jlong*) ((char*)obj + field_offset) = value;
2705 newinsn = AMPAMP (putfield_resolved_8);
2706 break;
2710 else
2712 jobject value = POPA();
2713 jobject obj = POPA();
2714 NULLCHECK(obj);
2715 *(jobject*) ((char*)obj + field_offset) = value;
2716 newinsn = AMPAMP (putfield_resolved_obj);
2719 #ifdef DIRECT_THREADED
2720 pc[-2].insn = newinsn;
2721 pc[-1].int_val = field_offset;
2722 #endif /* DIRECT_THREADED */
2724 NEXT_INSN;
2726 #ifdef DIRECT_THREADED
2727 putfield_resolved_1:
2729 jint val = POPI ();
2730 char *obj = (char *) POPA ();
2731 NULLCHECK (obj);
2732 *(jbyte *) (obj + INTVAL ()) = val;
2734 NEXT_INSN;
2736 putfield_resolved_2:
2738 jint val = POPI ();
2739 char *obj = (char *) POPA ();
2740 NULLCHECK (obj);
2741 *(jchar *) (obj + INTVAL ()) = val;
2743 NEXT_INSN;
2745 putfield_resolved_4:
2747 jint val = POPI ();
2748 char *obj = (char *) POPA ();
2749 NULLCHECK (obj);
2750 *(jint *) (obj + INTVAL ()) = val;
2752 NEXT_INSN;
2754 putfield_resolved_8:
2756 jlong val = POPL ();
2757 char *obj = (char *) POPA ();
2758 NULLCHECK (obj);
2759 *(jlong *) (obj + INTVAL ()) = val;
2761 NEXT_INSN;
2763 putfield_resolved_obj:
2765 jobject val = POPA ();
2766 char *obj = (char *) POPA ();
2767 NULLCHECK (obj);
2768 *(jobject *) (obj + INTVAL ()) = val;
2770 NEXT_INSN;
2771 #endif /* DIRECT_THREADED */
2773 insn_invokespecial:
2775 int index = GET2U ();
2777 rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
2779 sp -= rmeth->stack_item_count;
2781 NULLCHECK (sp[0].o);
2783 fun = (void (*)()) rmeth->method->ncode;
2785 #ifdef DIRECT_THREADED
2786 // Rewrite instruction so that we use a faster pre-resolved
2787 // method.
2788 pc[-2].insn = &&invokespecial_resolved;
2789 pc[-1].datum = rmeth;
2790 #endif /* DIRECT_THREADED */
2792 goto perform_invoke;
2794 #ifdef DIRECT_THREADED
2795 invokespecial_resolved:
2797 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2798 sp -= rmeth->stack_item_count;
2799 NULLCHECK (sp[0].o);
2800 fun = (void (*)()) rmeth->method->ncode;
2802 goto perform_invoke;
2803 #endif /* DIRECT_THREADED */
2805 insn_invokestatic:
2807 int index = GET2U ();
2809 rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
2811 sp -= rmeth->stack_item_count;
2813 _Jv_InitClass (rmeth->klass);
2814 fun = (void (*)()) rmeth->method->ncode;
2816 #ifdef DIRECT_THREADED
2817 // Rewrite instruction so that we use a faster pre-resolved
2818 // method.
2819 pc[-2].insn = &&invokestatic_resolved;
2820 pc[-1].datum = rmeth;
2821 #endif /* DIRECT_THREADED */
2823 goto perform_invoke;
2825 #ifdef DIRECT_THREADED
2826 invokestatic_resolved:
2828 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2829 sp -= rmeth->stack_item_count;
2830 fun = (void (*)()) rmeth->method->ncode;
2832 goto perform_invoke;
2833 #endif /* DIRECT_THREADED */
2835 insn_invokeinterface:
2837 int index = GET2U ();
2839 rmeth = (_Jv_ResolvePoolEntry (defining_class, index)).rmethod;
2841 sp -= rmeth->stack_item_count;
2843 jobject rcv = sp[0].o;
2845 NULLCHECK (rcv);
2847 fun = (void (*)())
2848 _Jv_LookupInterfaceMethod (rcv->getClass (),
2849 rmeth->method->name,
2850 rmeth->method->signature);
2852 #ifdef DIRECT_THREADED
2853 // Rewrite instruction so that we use a faster pre-resolved
2854 // method.
2855 pc[-2].insn = &&invokeinterface_resolved;
2856 pc[-1].datum = rmeth;
2857 #else
2858 // Skip dummy bytes.
2859 pc += 2;
2860 #endif /* DIRECT_THREADED */
2862 goto perform_invoke;
2864 #ifdef DIRECT_THREADED
2865 invokeinterface_resolved:
2867 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2868 sp -= rmeth->stack_item_count;
2869 jobject rcv = sp[0].o;
2870 NULLCHECK (rcv);
2871 fun = (void (*)())
2872 _Jv_LookupInterfaceMethod (rcv->getClass (),
2873 rmeth->method->name,
2874 rmeth->method->signature);
2876 goto perform_invoke;
2877 #endif /* DIRECT_THREADED */
2879 insn_new:
2881 int index = GET2U ();
2882 jclass klass = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
2883 _Jv_InitClass (klass);
2884 jobject res = _Jv_AllocObject (klass, klass->size_in_bytes);
2885 PUSHA (res);
2887 #ifdef DIRECT_THREADED
2888 pc[-2].insn = &&new_resolved;
2889 pc[-1].datum = klass;
2890 #endif /* DIRECT_THREADED */
2892 NEXT_INSN;
2894 #ifdef DIRECT_THREADED
2895 new_resolved:
2897 jclass klass = (jclass) AVAL ();
2898 jobject res = _Jv_AllocObject (klass, klass->size_in_bytes);
2899 PUSHA (res);
2901 NEXT_INSN;
2902 #endif /* DIRECT_THREADED */
2904 insn_newarray:
2906 int atype = GET1U ();
2907 int size = POPI();
2908 jobject result = _Jv_NewArray (atype, size);
2909 PUSHA (result);
2911 NEXT_INSN;
2913 insn_anewarray:
2915 int index = GET2U ();
2916 jclass klass = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
2917 int size = POPI();
2918 _Jv_InitClass (klass);
2919 jobject result = _Jv_NewObjectArray (size, klass, 0);
2920 PUSHA (result);
2922 #ifdef DIRECT_THREADED
2923 pc[-2].insn = &&anewarray_resolved;
2924 pc[-1].datum = klass;
2925 #endif /* DIRECT_THREADED */
2927 NEXT_INSN;
2929 #ifdef DIRECT_THREADED
2930 anewarray_resolved:
2932 jclass klass = (jclass) AVAL ();
2933 int size = POPI ();
2934 jobject result = _Jv_NewObjectArray (size, klass, 0);
2935 PUSHA (result);
2937 NEXT_INSN;
2938 #endif /* DIRECT_THREADED */
2940 insn_arraylength:
2942 __JArray *arr = (__JArray*)POPA();
2943 NULLARRAYCHECK (arr);
2944 PUSHI (arr->length);
2946 NEXT_INSN;
2948 insn_athrow:
2950 jobject value = POPA();
2951 throw static_cast<jthrowable>(value);
2953 NEXT_INSN;
2955 insn_checkcast:
2957 jobject value = POPA();
2958 jint index = GET2U ();
2959 jclass to = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
2961 if (value != NULL && ! to->isInstance (value))
2962 throw new java::lang::ClassCastException (to->getName());
2964 PUSHA (value);
2966 #ifdef DIRECT_THREADED
2967 pc[-2].insn = &&checkcast_resolved;
2968 pc[-1].datum = to;
2969 #endif /* DIRECT_THREADED */
2971 NEXT_INSN;
2973 #ifdef DIRECT_THREADED
2974 checkcast_resolved:
2976 jobject value = POPA ();
2977 jclass to = (jclass) AVAL ();
2978 if (value != NULL && ! to->isInstance (value))
2979 throw new java::lang::ClassCastException (to->getName());
2980 PUSHA (value);
2982 NEXT_INSN;
2983 #endif /* DIRECT_THREADED */
2985 insn_instanceof:
2987 jobject value = POPA();
2988 jint index = GET2U ();
2989 jclass to = (_Jv_ResolvePoolEntry (defining_class, index)).clazz;
2990 PUSHI (to->isInstance (value));
2992 #ifdef DIRECT_THREADED
2993 pc[-2].insn = &&instanceof_resolved;
2994 pc[-1].datum = to;
2995 #endif /* DIRECT_THREADED */
2997 NEXT_INSN;
2999 #ifdef DIRECT_THREADED
3000 instanceof_resolved:
3002 jobject value = POPA ();
3003 jclass to = (jclass) AVAL ();
3004 PUSHI (to->isInstance (value));
3006 NEXT_INSN;
3007 #endif /* DIRECT_THREADED */
3009 insn_monitorenter:
3011 jobject value = POPA();
3012 NULLCHECK(value);
3013 _Jv_MonitorEnter (value);
3015 NEXT_INSN;
3017 insn_monitorexit:
3019 jobject value = POPA();
3020 NULLCHECK(value);
3021 _Jv_MonitorExit (value);
3023 NEXT_INSN;
3025 insn_ifnull:
3027 jobject val = POPA();
3028 if (val == NULL)
3029 TAKE_GOTO;
3030 else
3031 SKIP_GOTO;
3033 NEXT_INSN;
3035 insn_ifnonnull:
3037 jobject val = POPA();
3038 if (val != NULL)
3039 TAKE_GOTO;
3040 else
3041 SKIP_GOTO;
3043 NEXT_INSN;
3045 insn_multianewarray:
3047 int kind_index = GET2U ();
3048 int dim = GET1U ();
3050 jclass type
3051 = (_Jv_ResolvePoolEntry (defining_class, kind_index)).clazz;
3052 _Jv_InitClass (type);
3053 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3055 for (int i = dim - 1; i >= 0; i--)
3057 sizes[i] = POPI ();
3060 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3062 PUSHA (res);
3064 NEXT_INSN;
3066 #ifndef DIRECT_THREADED
3067 insn_wide:
3069 jint the_mod_op = get1u (pc++);
3070 jint wide = get2u (pc); pc += 2;
3072 switch (the_mod_op)
3074 case op_istore:
3075 STOREI (wide);
3076 NEXT_INSN;
3078 case op_fstore:
3079 STOREF (wide);
3080 NEXT_INSN;
3082 case op_astore:
3083 STOREA (wide);
3084 NEXT_INSN;
3086 case op_lload:
3087 LOADL (wide);
3088 NEXT_INSN;
3090 case op_dload:
3091 LOADD (wide);
3092 NEXT_INSN;
3094 case op_iload:
3095 LOADI (wide);
3096 NEXT_INSN;
3098 case op_aload:
3099 LOADA (wide);
3100 NEXT_INSN;
3102 case op_lstore:
3103 STOREL (wide);
3104 NEXT_INSN;
3106 case op_dstore:
3107 STORED (wide);
3108 NEXT_INSN;
3110 case op_ret:
3111 pc = (unsigned char*) PEEKA (wide);
3112 NEXT_INSN;
3114 case op_iinc:
3116 jint amount = get2s (pc); pc += 2;
3117 jint value = PEEKI (wide);
3118 POKEI (wide, value+amount);
3120 NEXT_INSN;
3122 default:
3123 throw_internal_error ("illegal bytecode modified by wide");
3127 #endif /* DIRECT_THREADED */
3129 catch (java::lang::Throwable *ex)
3131 #ifdef DIRECT_THREADED
3132 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3133 #else
3134 int logical_pc = pc - 1 - bytecode ();
3135 #endif
3136 _Jv_InterpException *exc = exceptions ();
3137 jclass exc_class = ex->getClass ();
3139 for (int i = 0; i < exc_count; i++)
3141 if (PCVAL (exc[i].start_pc) <= logical_pc
3142 && logical_pc < PCVAL (exc[i].end_pc))
3144 #ifdef DIRECT_THREADED
3145 jclass handler = (jclass) exc[i].handler_type.p;
3146 #else
3147 jclass handler = NULL;
3148 if (exc[i].handler_type.i != 0)
3149 handler = (_Jv_ResolvePoolEntry (defining_class,
3150 exc[i].handler_type.i)).clazz;
3151 #endif /* DIRECT_THREADED */
3153 if (handler == NULL || handler->isAssignableFrom (exc_class))
3155 #ifdef DIRECT_THREADED
3156 pc = (insn_slot *) exc[i].handler_pc.p;
3157 #else
3158 pc = bytecode () + exc[i].handler_pc.i;
3159 #endif /* DIRECT_THREADED */
3160 sp = stack;
3161 sp++->o = ex; // Push exception.
3162 NEXT_INSN;
3167 // No handler, so re-throw.
3168 throw ex;
3172 static void
3173 throw_internal_error (char *msg)
3175 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3178 static void
3179 throw_incompatible_class_change_error (jstring msg)
3181 throw new java::lang::IncompatibleClassChangeError (msg);
3184 #ifndef HANDLE_SEGV
3185 static java::lang::NullPointerException *null_pointer_exc;
3186 static void
3187 throw_null_pointer_exception ()
3189 if (null_pointer_exc == NULL)
3190 null_pointer_exc = new java::lang::NullPointerException;
3192 throw null_pointer_exc;
3194 #endif
3196 #endif // INTERPRETER