PR libgcj/26063, PR libgcj/17978, PR libgcj/10598:
[gcc.git] / libjava / interpret.cc
1 // interpret.cc - Code for the interpreter
2
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
12
13 #include <config.h>
14 #include <platform.h>
15
16 #pragma implementation "java-interp.h"
17
18 #include <jvm.h>
19 #include <java-cpool.h>
20 #include <java-interp.h>
21 #include <java/lang/System.h>
22 #include <java/lang/String.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/StringBuffer.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/VirtualMachineError.h>
29 #include <java/lang/InternalError.h>
30 #include <java/lang/NullPointerException.h>
31 #include <java/lang/ArithmeticException.h>
32 #include <java/lang/IncompatibleClassChangeError.h>
33 #include <java/lang/InstantiationException.h>
34 #include <java/lang/Thread.h>
35 #include <java-insns.h>
36 #include <java-signal.h>
37 #include <java/lang/ClassFormatError.h>
38 #include <execution.h>
39 #include <java/lang/reflect/Modifier.h>
40
41 #ifdef INTERPRETER
42
43 // Execution engine for interpreted code.
44 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
45
46 #include <stdlib.h>
47
48 using namespace gcj;
49
50 static void throw_internal_error (char *msg)
51 __attribute__ ((__noreturn__));
52 static void throw_incompatible_class_change_error (jstring msg)
53 __attribute__ ((__noreturn__));
54 #ifndef HANDLE_SEGV
55 static void throw_null_pointer_exception ()
56 __attribute__ ((__noreturn__));
57 #endif
58
59 static void throw_class_format_error (jstring msg)
60 __attribute__ ((__noreturn__));
61 static void throw_class_format_error (char *msg)
62 __attribute__ ((__noreturn__));
63
64 #ifdef DIRECT_THREADED
65 // Lock to ensure that methods are not compiled concurrently.
66 // We could use a finer-grained lock here, however it is not safe to use
67 // the Class monitor as user code in another thread could hold it.
68 static _Jv_Mutex_t compile_mutex;
69
70 void
71 _Jv_InitInterpreter()
72 {
73 _Jv_MutexInit (&compile_mutex);
74 }
75 #else
76 void _Jv_InitInterpreter() {}
77 #endif
78
79 extern "C" double __ieee754_fmod (double,double);
80
81 static inline void dupx (_Jv_word *sp, int n, int x)
82 {
83 // first "slide" n+x elements n to the right
84 int top = n-1;
85 for (int i = 0; i < n+x; i++)
86 {
87 sp[(top-i)] = sp[(top-i)-n];
88 }
89
90 // next, copy the n top elements, n+x down
91 for (int i = 0; i < n; i++)
92 {
93 sp[top-(n+x)-i] = sp[top-i];
94 }
95 }
96
97 // Used to convert from floating types to integral types.
98 template<typename TO, typename FROM>
99 static inline TO
100 convert (FROM val, TO min, TO max)
101 {
102 TO ret;
103 if (val >= (FROM) max)
104 ret = max;
105 else if (val <= (FROM) min)
106 ret = min;
107 else if (val != val)
108 ret = 0;
109 else
110 ret = (TO) val;
111 return ret;
112 }
113
114 #define PUSHA(V) (sp++)->o = (V)
115 #define PUSHI(V) (sp++)->i = (V)
116 #define PUSHF(V) (sp++)->f = (V)
117 #if SIZEOF_VOID_P == 8
118 # define PUSHL(V) (sp->l = (V), sp += 2)
119 # define PUSHD(V) (sp->d = (V), sp += 2)
120 #else
121 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
122 (sp++)->ia[0] = w2.ia[0]; \
123 (sp++)->ia[0] = w2.ia[1]; } while (0)
124 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
125 (sp++)->ia[0] = w2.ia[0]; \
126 (sp++)->ia[0] = w2.ia[1]; } while (0)
127 #endif
128
129 #define POPA() ((--sp)->o)
130 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
131 #define POPF() ((jfloat) (--sp)->f)
132 #if SIZEOF_VOID_P == 8
133 # define POPL() (sp -= 2, (jlong) sp->l)
134 # define POPD() (sp -= 2, (jdouble) sp->d)
135 #else
136 # define POPL() ({ _Jv_word2 w2; \
137 w2.ia[1] = (--sp)->ia[0]; \
138 w2.ia[0] = (--sp)->ia[0]; w2.l; })
139 # define POPD() ({ _Jv_word2 w2; \
140 w2.ia[1] = (--sp)->ia[0]; \
141 w2.ia[0] = (--sp)->ia[0]; w2.d; })
142 #endif
143
144 #define LOADA(I) (sp++)->o = locals[I].o
145 #define LOADI(I) (sp++)->i = locals[I].i
146 #define LOADF(I) (sp++)->f = locals[I].f
147 #if SIZEOF_VOID_P == 8
148 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
149 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
150 #else
151 # define LOADL(I) do { jint __idx = (I); \
152 (sp++)->ia[0] = locals[__idx].ia[0]; \
153 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
154 } while (0)
155 # define LOADD(I) LOADL(I)
156 #endif
157
158 #define STOREA(I) locals[I].o = (--sp)->o
159 #define STOREI(I) locals[I].i = (--sp)->i
160 #define STOREF(I) locals[I].f = (--sp)->f
161 #if SIZEOF_VOID_P == 8
162 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
163 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
164 #else
165 # define STOREL(I) do { jint __idx = (I); \
166 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
167 locals[__idx].ia[0] = (--sp)->ia[0]; \
168 } while (0)
169 # define STORED(I) STOREL(I)
170 #endif
171
172 #define PEEKI(I) (locals+(I))->i
173 #define PEEKA(I) (locals+(I))->o
174
175 #define POKEI(I,V) ((locals+(I))->i = (V))
176
177
178 #define BINOPI(OP) { \
179 jint value2 = POPI(); \
180 jint value1 = POPI(); \
181 PUSHI(value1 OP value2); \
182 }
183
184 #define BINOPF(OP) { \
185 jfloat value2 = POPF(); \
186 jfloat value1 = POPF(); \
187 PUSHF(value1 OP value2); \
188 }
189
190 #define BINOPL(OP) { \
191 jlong value2 = POPL(); \
192 jlong value1 = POPL(); \
193 PUSHL(value1 OP value2); \
194 }
195
196 #define BINOPD(OP) { \
197 jdouble value2 = POPD(); \
198 jdouble value1 = POPD(); \
199 PUSHD(value1 OP value2); \
200 }
201
202 static inline jint get1s(unsigned char* loc) {
203 return *(signed char*)loc;
204 }
205
206 static inline jint get1u(unsigned char* loc) {
207 return *loc;
208 }
209
210 static inline jint get2s(unsigned char* loc) {
211 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
212 }
213
214 static inline jint get2u(unsigned char* loc) {
215 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
216 }
217
218 static jint get4(unsigned char* loc) {
219 return (((jint)(loc[0])) << 24)
220 | (((jint)(loc[1])) << 16)
221 | (((jint)(loc[2])) << 8)
222 | (((jint)(loc[3])) << 0);
223 }
224
225 #define SAVE_PC() frame_desc.pc = pc
226
227 #ifdef HANDLE_SEGV
228 #define NULLCHECK(X) SAVE_PC()
229 #define NULLARRAYCHECK(X) SAVE_PC()
230 #else
231 #define NULLCHECK(X) \
232 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
233 #define NULLARRAYCHECK(X) \
234 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
235 #endif
236
237 #define ARRAYBOUNDSCHECK(array, index) \
238 do \
239 { \
240 if (((unsigned) index) >= (unsigned) (array->length)) \
241 _Jv_ThrowBadArrayIndex (index); \
242 } \
243 while (0)
244
245 void
246 _Jv_InterpMethod::run_normal (ffi_cif *,
247 void* ret,
248 ffi_raw * args,
249 void* __this)
250 {
251 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
252 run (ret, args, _this);
253 }
254
255 void
256 _Jv_InterpMethod::run_synch_object (ffi_cif *,
257 void* ret,
258 ffi_raw * args,
259 void* __this)
260 {
261 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
262
263 jobject rcv = (jobject) args[0].ptr;
264 JvSynchronize mutex (rcv);
265
266 run (ret, args, _this);
267 }
268
269 void
270 _Jv_InterpMethod::run_class (ffi_cif *,
271 void* ret,
272 ffi_raw * args,
273 void* __this)
274 {
275 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
276 _Jv_InitClass (_this->defining_class);
277 run (ret, args, _this);
278 }
279
280 void
281 _Jv_InterpMethod::run_synch_class (ffi_cif *,
282 void* ret,
283 ffi_raw * args,
284 void* __this)
285 {
286 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
287
288 jclass sync = _this->defining_class;
289 _Jv_InitClass (sync);
290 JvSynchronize mutex (sync);
291
292 run (ret, args, _this);
293 }
294
295 #ifdef DIRECT_THREADED
296 // "Compile" a method by turning it from bytecode to direct-threaded
297 // code.
298 void
299 _Jv_InterpMethod::compile (const void * const *insn_targets)
300 {
301 insn_slot *insns = NULL;
302 int next = 0;
303 unsigned char *codestart = bytecode ();
304 unsigned char *end = codestart + code_length;
305 _Jv_word *pool_data = defining_class->constants.data;
306
307 #define SET_ONE(Field, Value) \
308 do \
309 { \
310 if (first_pass) \
311 ++next; \
312 else \
313 insns[next++].Field = Value; \
314 } \
315 while (0)
316
317 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
318 #define SET_INT(Value) SET_ONE (int_val, Value)
319 #define SET_DATUM(Value) SET_ONE (datum, Value)
320
321 // Map from bytecode PC to slot in INSNS.
322 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
323 for (int i = 0; i < code_length; ++i)
324 pc_mapping[i] = -1;
325
326 for (int i = 0; i < 2; ++i)
327 {
328 jboolean first_pass = i == 0;
329
330 if (! first_pass)
331 {
332 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
333 number_insn_slots = next;
334 next = 0;
335 }
336
337 unsigned char *pc = codestart;
338 while (pc < end)
339 {
340 int base_pc_val = pc - codestart;
341 if (first_pass)
342 pc_mapping[base_pc_val] = next;
343
344 java_opcode opcode = (java_opcode) *pc++;
345 // Just elide NOPs.
346 if (opcode == op_nop)
347 continue;
348 SET_INSN (insn_targets[opcode]);
349
350 switch (opcode)
351 {
352 case op_nop:
353 case op_aconst_null:
354 case op_iconst_m1:
355 case op_iconst_0:
356 case op_iconst_1:
357 case op_iconst_2:
358 case op_iconst_3:
359 case op_iconst_4:
360 case op_iconst_5:
361 case op_lconst_0:
362 case op_lconst_1:
363 case op_fconst_0:
364 case op_fconst_1:
365 case op_fconst_2:
366 case op_dconst_0:
367 case op_dconst_1:
368 case op_iload_0:
369 case op_iload_1:
370 case op_iload_2:
371 case op_iload_3:
372 case op_lload_0:
373 case op_lload_1:
374 case op_lload_2:
375 case op_lload_3:
376 case op_fload_0:
377 case op_fload_1:
378 case op_fload_2:
379 case op_fload_3:
380 case op_dload_0:
381 case op_dload_1:
382 case op_dload_2:
383 case op_dload_3:
384 case op_aload_0:
385 case op_aload_1:
386 case op_aload_2:
387 case op_aload_3:
388 case op_iaload:
389 case op_laload:
390 case op_faload:
391 case op_daload:
392 case op_aaload:
393 case op_baload:
394 case op_caload:
395 case op_saload:
396 case op_istore_0:
397 case op_istore_1:
398 case op_istore_2:
399 case op_istore_3:
400 case op_lstore_0:
401 case op_lstore_1:
402 case op_lstore_2:
403 case op_lstore_3:
404 case op_fstore_0:
405 case op_fstore_1:
406 case op_fstore_2:
407 case op_fstore_3:
408 case op_dstore_0:
409 case op_dstore_1:
410 case op_dstore_2:
411 case op_dstore_3:
412 case op_astore_0:
413 case op_astore_1:
414 case op_astore_2:
415 case op_astore_3:
416 case op_iastore:
417 case op_lastore:
418 case op_fastore:
419 case op_dastore:
420 case op_aastore:
421 case op_bastore:
422 case op_castore:
423 case op_sastore:
424 case op_pop:
425 case op_pop2:
426 case op_dup:
427 case op_dup_x1:
428 case op_dup_x2:
429 case op_dup2:
430 case op_dup2_x1:
431 case op_dup2_x2:
432 case op_swap:
433 case op_iadd:
434 case op_isub:
435 case op_imul:
436 case op_idiv:
437 case op_irem:
438 case op_ishl:
439 case op_ishr:
440 case op_iushr:
441 case op_iand:
442 case op_ior:
443 case op_ixor:
444 case op_ladd:
445 case op_lsub:
446 case op_lmul:
447 case op_ldiv:
448 case op_lrem:
449 case op_lshl:
450 case op_lshr:
451 case op_lushr:
452 case op_land:
453 case op_lor:
454 case op_lxor:
455 case op_fadd:
456 case op_fsub:
457 case op_fmul:
458 case op_fdiv:
459 case op_frem:
460 case op_dadd:
461 case op_dsub:
462 case op_dmul:
463 case op_ddiv:
464 case op_drem:
465 case op_ineg:
466 case op_i2b:
467 case op_i2c:
468 case op_i2s:
469 case op_lneg:
470 case op_fneg:
471 case op_dneg:
472 case op_i2l:
473 case op_i2f:
474 case op_i2d:
475 case op_l2i:
476 case op_l2f:
477 case op_l2d:
478 case op_f2i:
479 case op_f2l:
480 case op_f2d:
481 case op_d2i:
482 case op_d2l:
483 case op_d2f:
484 case op_lcmp:
485 case op_fcmpl:
486 case op_fcmpg:
487 case op_dcmpl:
488 case op_dcmpg:
489 case op_monitorenter:
490 case op_monitorexit:
491 case op_ireturn:
492 case op_lreturn:
493 case op_freturn:
494 case op_dreturn:
495 case op_areturn:
496 case op_return:
497 case op_athrow:
498 case op_arraylength:
499 // No argument, nothing else to do.
500 break;
501
502 case op_bipush:
503 SET_INT (get1s (pc));
504 ++pc;
505 break;
506
507 case op_ldc:
508 {
509 int index = get1u (pc);
510 ++pc;
511 // For an unresolved class we want to delay resolution
512 // until execution.
513 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
514 {
515 --next;
516 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
517 SET_INT (index);
518 }
519 else
520 SET_DATUM (pool_data[index].o);
521 }
522 break;
523
524 case op_ret:
525 case op_iload:
526 case op_lload:
527 case op_fload:
528 case op_dload:
529 case op_aload:
530 case op_istore:
531 case op_lstore:
532 case op_fstore:
533 case op_dstore:
534 case op_astore:
535 case op_newarray:
536 SET_INT (get1u (pc));
537 ++pc;
538 break;
539
540 case op_iinc:
541 SET_INT (get1u (pc));
542 SET_INT (get1s (pc + 1));
543 pc += 2;
544 break;
545
546 case op_ldc_w:
547 {
548 int index = get2u (pc);
549 pc += 2;
550 // For an unresolved class we want to delay resolution
551 // until execution.
552 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
553 {
554 --next;
555 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
556 SET_INT (index);
557 }
558 else
559 SET_DATUM (pool_data[index].o);
560 }
561 break;
562
563 case op_ldc2_w:
564 {
565 int index = get2u (pc);
566 pc += 2;
567 SET_DATUM (&pool_data[index]);
568 }
569 break;
570
571 case op_sipush:
572 SET_INT (get2s (pc));
573 pc += 2;
574 break;
575
576 case op_new:
577 case op_getstatic:
578 case op_getfield:
579 case op_putfield:
580 case op_putstatic:
581 case op_anewarray:
582 case op_instanceof:
583 case op_checkcast:
584 case op_invokespecial:
585 case op_invokestatic:
586 case op_invokevirtual:
587 SET_INT (get2u (pc));
588 pc += 2;
589 break;
590
591 case op_multianewarray:
592 SET_INT (get2u (pc));
593 SET_INT (get1u (pc + 2));
594 pc += 3;
595 break;
596
597 case op_jsr:
598 case op_ifeq:
599 case op_ifne:
600 case op_iflt:
601 case op_ifge:
602 case op_ifgt:
603 case op_ifle:
604 case op_if_icmpeq:
605 case op_if_icmpne:
606 case op_if_icmplt:
607 case op_if_icmpge:
608 case op_if_icmpgt:
609 case op_if_icmple:
610 case op_if_acmpeq:
611 case op_if_acmpne:
612 case op_ifnull:
613 case op_ifnonnull:
614 case op_goto:
615 {
616 int offset = get2s (pc);
617 pc += 2;
618
619 int new_pc = base_pc_val + offset;
620
621 bool orig_was_goto = opcode == op_goto;
622
623 // Thread jumps. We limit the loop count; this lets
624 // us avoid infinite loops if the bytecode contains
625 // such. `10' is arbitrary.
626 int count = 10;
627 while (codestart[new_pc] == op_goto && count-- > 0)
628 new_pc += get2s (&codestart[new_pc + 1]);
629
630 // If the jump takes us to a `return' instruction and
631 // the original branch was an unconditional goto, then
632 // we hoist the return.
633 opcode = (java_opcode) codestart[new_pc];
634 if (orig_was_goto
635 && (opcode == op_ireturn || opcode == op_lreturn
636 || opcode == op_freturn || opcode == op_dreturn
637 || opcode == op_areturn || opcode == op_return))
638 {
639 --next;
640 SET_INSN (insn_targets[opcode]);
641 }
642 else
643 SET_DATUM (&insns[pc_mapping[new_pc]]);
644 }
645 break;
646
647 case op_tableswitch:
648 {
649 while ((pc - codestart) % 4 != 0)
650 ++pc;
651
652 jint def = get4 (pc);
653 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
654 pc += 4;
655
656 int low = get4 (pc);
657 SET_INT (low);
658 pc += 4;
659 int high = get4 (pc);
660 SET_INT (high);
661 pc += 4;
662
663 for (int i = low; i <= high; ++i)
664 {
665 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
666 pc += 4;
667 }
668 }
669 break;
670
671 case op_lookupswitch:
672 {
673 while ((pc - codestart) % 4 != 0)
674 ++pc;
675
676 jint def = get4 (pc);
677 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
678 pc += 4;
679
680 jint npairs = get4 (pc);
681 pc += 4;
682 SET_INT (npairs);
683
684 while (npairs-- > 0)
685 {
686 jint match = get4 (pc);
687 jint offset = get4 (pc + 4);
688 SET_INT (match);
689 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
690 pc += 8;
691 }
692 }
693 break;
694
695 case op_invokeinterface:
696 {
697 jint index = get2u (pc);
698 pc += 2;
699 // We ignore the next two bytes.
700 pc += 2;
701 SET_INT (index);
702 }
703 break;
704
705 case op_wide:
706 {
707 opcode = (java_opcode) get1u (pc);
708 pc += 1;
709 jint val = get2u (pc);
710 pc += 2;
711
712 // We implement narrow and wide instructions using the
713 // same code in the interpreter. So we rewrite the
714 // instruction slot here.
715 if (! first_pass)
716 insns[next - 1].insn = (void *) insn_targets[opcode];
717 SET_INT (val);
718
719 if (opcode == op_iinc)
720 {
721 SET_INT (get2s (pc));
722 pc += 2;
723 }
724 }
725 break;
726
727 case op_jsr_w:
728 case op_goto_w:
729 {
730 jint offset = get4 (pc);
731 pc += 4;
732 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
733 }
734 break;
735
736 // Some "can't happen" cases that we include for
737 // error-checking purposes.
738 case op_putfield_1:
739 case op_putfield_2:
740 case op_putfield_4:
741 case op_putfield_8:
742 case op_putfield_a:
743 case op_putstatic_1:
744 case op_putstatic_2:
745 case op_putstatic_4:
746 case op_putstatic_8:
747 case op_putstatic_a:
748 case op_getfield_1:
749 case op_getfield_2s:
750 case op_getfield_2u:
751 case op_getfield_4:
752 case op_getfield_8:
753 case op_getfield_a:
754 case op_getstatic_1:
755 case op_getstatic_2s:
756 case op_getstatic_2u:
757 case op_getstatic_4:
758 case op_getstatic_8:
759 case op_getstatic_a:
760 default:
761 // Fail somehow.
762 break;
763 }
764 }
765 }
766
767 // Now update exceptions.
768 _Jv_InterpException *exc = exceptions ();
769 for (int i = 0; i < exc_count; ++i)
770 {
771 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
772 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
773 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
774 jclass handler
775 = (_Jv_Linker::resolve_pool_entry (defining_class,
776 exc[i].handler_type.i)).clazz;
777 exc[i].handler_type.p = handler;
778 }
779
780 // Translate entries in the LineNumberTable from bytecode PC's to direct
781 // threaded interpreter instruction values.
782 for (int i = 0; i < line_table_len; i++)
783 {
784 int byte_pc = line_table[i].bytecode_pc;
785 // It isn't worth throwing an exception if this table is
786 // corrupted, but at the same time we don't want a crash.
787 if (byte_pc < 0 || byte_pc >= code_length)
788 byte_pc = 0;
789 line_table[i].pc = &insns[pc_mapping[byte_pc]];
790 }
791
792 prepared = insns;
793 }
794 #endif /* DIRECT_THREADED */
795
796 /* Run the given method.
797 When args is NULL, don't run anything -- just compile it. */
798 void
799 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
800 {
801 using namespace java::lang::reflect;
802
803 // FRAME_DESC registers this particular invocation as the top-most
804 // interpreter frame. This lets the stack tracing code (for
805 // Throwable) print information about the method being interpreted
806 // rather than about the interpreter itself. FRAME_DESC has a
807 // destructor so it cleans up automatically when the interpreter
808 // returns.
809 java::lang::Thread *thread = java::lang::Thread::currentThread();
810 _Jv_InterpFrame frame_desc (meth,
811 (_Jv_InterpFrame **) &thread->interp_frame);
812
813 _Jv_word stack[meth->max_stack];
814 _Jv_word *sp = stack;
815
816 _Jv_word locals[meth->max_locals];
817
818 #define INSN_LABEL(op) &&insn_##op
819
820 static const void *const insn_target[] =
821 {
822 INSN_LABEL(nop),
823 INSN_LABEL(aconst_null),
824 INSN_LABEL(iconst_m1),
825 INSN_LABEL(iconst_0),
826 INSN_LABEL(iconst_1),
827 INSN_LABEL(iconst_2),
828 INSN_LABEL(iconst_3),
829 INSN_LABEL(iconst_4),
830 INSN_LABEL(iconst_5),
831 INSN_LABEL(lconst_0),
832 INSN_LABEL(lconst_1),
833 INSN_LABEL(fconst_0),
834 INSN_LABEL(fconst_1),
835 INSN_LABEL(fconst_2),
836 INSN_LABEL(dconst_0),
837 INSN_LABEL(dconst_1),
838 INSN_LABEL(bipush),
839 INSN_LABEL(sipush),
840 INSN_LABEL(ldc),
841 INSN_LABEL(ldc_w),
842 INSN_LABEL(ldc2_w),
843 INSN_LABEL(iload),
844 INSN_LABEL(lload),
845 INSN_LABEL(fload),
846 INSN_LABEL(dload),
847 INSN_LABEL(aload),
848 INSN_LABEL(iload_0),
849 INSN_LABEL(iload_1),
850 INSN_LABEL(iload_2),
851 INSN_LABEL(iload_3),
852 INSN_LABEL(lload_0),
853 INSN_LABEL(lload_1),
854 INSN_LABEL(lload_2),
855 INSN_LABEL(lload_3),
856 INSN_LABEL(fload_0),
857 INSN_LABEL(fload_1),
858 INSN_LABEL(fload_2),
859 INSN_LABEL(fload_3),
860 INSN_LABEL(dload_0),
861 INSN_LABEL(dload_1),
862 INSN_LABEL(dload_2),
863 INSN_LABEL(dload_3),
864 INSN_LABEL(aload_0),
865 INSN_LABEL(aload_1),
866 INSN_LABEL(aload_2),
867 INSN_LABEL(aload_3),
868 INSN_LABEL(iaload),
869 INSN_LABEL(laload),
870 INSN_LABEL(faload),
871 INSN_LABEL(daload),
872 INSN_LABEL(aaload),
873 INSN_LABEL(baload),
874 INSN_LABEL(caload),
875 INSN_LABEL(saload),
876 INSN_LABEL(istore),
877 INSN_LABEL(lstore),
878 INSN_LABEL(fstore),
879 INSN_LABEL(dstore),
880 INSN_LABEL(astore),
881 INSN_LABEL(istore_0),
882 INSN_LABEL(istore_1),
883 INSN_LABEL(istore_2),
884 INSN_LABEL(istore_3),
885 INSN_LABEL(lstore_0),
886 INSN_LABEL(lstore_1),
887 INSN_LABEL(lstore_2),
888 INSN_LABEL(lstore_3),
889 INSN_LABEL(fstore_0),
890 INSN_LABEL(fstore_1),
891 INSN_LABEL(fstore_2),
892 INSN_LABEL(fstore_3),
893 INSN_LABEL(dstore_0),
894 INSN_LABEL(dstore_1),
895 INSN_LABEL(dstore_2),
896 INSN_LABEL(dstore_3),
897 INSN_LABEL(astore_0),
898 INSN_LABEL(astore_1),
899 INSN_LABEL(astore_2),
900 INSN_LABEL(astore_3),
901 INSN_LABEL(iastore),
902 INSN_LABEL(lastore),
903 INSN_LABEL(fastore),
904 INSN_LABEL(dastore),
905 INSN_LABEL(aastore),
906 INSN_LABEL(bastore),
907 INSN_LABEL(castore),
908 INSN_LABEL(sastore),
909 INSN_LABEL(pop),
910 INSN_LABEL(pop2),
911 INSN_LABEL(dup),
912 INSN_LABEL(dup_x1),
913 INSN_LABEL(dup_x2),
914 INSN_LABEL(dup2),
915 INSN_LABEL(dup2_x1),
916 INSN_LABEL(dup2_x2),
917 INSN_LABEL(swap),
918 INSN_LABEL(iadd),
919 INSN_LABEL(ladd),
920 INSN_LABEL(fadd),
921 INSN_LABEL(dadd),
922 INSN_LABEL(isub),
923 INSN_LABEL(lsub),
924 INSN_LABEL(fsub),
925 INSN_LABEL(dsub),
926 INSN_LABEL(imul),
927 INSN_LABEL(lmul),
928 INSN_LABEL(fmul),
929 INSN_LABEL(dmul),
930 INSN_LABEL(idiv),
931 INSN_LABEL(ldiv),
932 INSN_LABEL(fdiv),
933 INSN_LABEL(ddiv),
934 INSN_LABEL(irem),
935 INSN_LABEL(lrem),
936 INSN_LABEL(frem),
937 INSN_LABEL(drem),
938 INSN_LABEL(ineg),
939 INSN_LABEL(lneg),
940 INSN_LABEL(fneg),
941 INSN_LABEL(dneg),
942 INSN_LABEL(ishl),
943 INSN_LABEL(lshl),
944 INSN_LABEL(ishr),
945 INSN_LABEL(lshr),
946 INSN_LABEL(iushr),
947 INSN_LABEL(lushr),
948 INSN_LABEL(iand),
949 INSN_LABEL(land),
950 INSN_LABEL(ior),
951 INSN_LABEL(lor),
952 INSN_LABEL(ixor),
953 INSN_LABEL(lxor),
954 INSN_LABEL(iinc),
955 INSN_LABEL(i2l),
956 INSN_LABEL(i2f),
957 INSN_LABEL(i2d),
958 INSN_LABEL(l2i),
959 INSN_LABEL(l2f),
960 INSN_LABEL(l2d),
961 INSN_LABEL(f2i),
962 INSN_LABEL(f2l),
963 INSN_LABEL(f2d),
964 INSN_LABEL(d2i),
965 INSN_LABEL(d2l),
966 INSN_LABEL(d2f),
967 INSN_LABEL(i2b),
968 INSN_LABEL(i2c),
969 INSN_LABEL(i2s),
970 INSN_LABEL(lcmp),
971 INSN_LABEL(fcmpl),
972 INSN_LABEL(fcmpg),
973 INSN_LABEL(dcmpl),
974 INSN_LABEL(dcmpg),
975 INSN_LABEL(ifeq),
976 INSN_LABEL(ifne),
977 INSN_LABEL(iflt),
978 INSN_LABEL(ifge),
979 INSN_LABEL(ifgt),
980 INSN_LABEL(ifle),
981 INSN_LABEL(if_icmpeq),
982 INSN_LABEL(if_icmpne),
983 INSN_LABEL(if_icmplt),
984 INSN_LABEL(if_icmpge),
985 INSN_LABEL(if_icmpgt),
986 INSN_LABEL(if_icmple),
987 INSN_LABEL(if_acmpeq),
988 INSN_LABEL(if_acmpne),
989 INSN_LABEL(goto),
990 INSN_LABEL(jsr),
991 INSN_LABEL(ret),
992 INSN_LABEL(tableswitch),
993 INSN_LABEL(lookupswitch),
994 INSN_LABEL(ireturn),
995 INSN_LABEL(lreturn),
996 INSN_LABEL(freturn),
997 INSN_LABEL(dreturn),
998 INSN_LABEL(areturn),
999 INSN_LABEL(return),
1000 INSN_LABEL(getstatic),
1001 INSN_LABEL(putstatic),
1002 INSN_LABEL(getfield),
1003 INSN_LABEL(putfield),
1004 INSN_LABEL(invokevirtual),
1005 INSN_LABEL(invokespecial),
1006 INSN_LABEL(invokestatic),
1007 INSN_LABEL(invokeinterface),
1008 0, /* Unused. */
1009 INSN_LABEL(new),
1010 INSN_LABEL(newarray),
1011 INSN_LABEL(anewarray),
1012 INSN_LABEL(arraylength),
1013 INSN_LABEL(athrow),
1014 INSN_LABEL(checkcast),
1015 INSN_LABEL(instanceof),
1016 INSN_LABEL(monitorenter),
1017 INSN_LABEL(monitorexit),
1018 #ifdef DIRECT_THREADED
1019 0, // wide
1020 #else
1021 INSN_LABEL(wide),
1022 #endif
1023 INSN_LABEL(multianewarray),
1024 INSN_LABEL(ifnull),
1025 INSN_LABEL(ifnonnull),
1026 INSN_LABEL(goto_w),
1027 INSN_LABEL(jsr_w),
1028 #ifdef DIRECT_THREADED
1029 INSN_LABEL (ldc_class)
1030 #else
1031 0
1032 #endif
1033 };
1034
1035 pc_t pc;
1036
1037 #ifdef DIRECT_THREADED
1038
1039 #define NEXT_INSN goto *((pc++)->insn)
1040 #define INTVAL() ((pc++)->int_val)
1041 #define AVAL() ((pc++)->datum)
1042
1043 #define GET1S() INTVAL ()
1044 #define GET2S() INTVAL ()
1045 #define GET1U() INTVAL ()
1046 #define GET2U() INTVAL ()
1047 #define AVAL1U() AVAL ()
1048 #define AVAL2U() AVAL ()
1049 #define AVAL2UP() AVAL ()
1050 #define SKIP_GOTO ++pc
1051 #define GOTO_VAL() (insn_slot *) pc->datum
1052 #define PCVAL(unionval) unionval.p
1053 #define AMPAMP(label) &&label
1054
1055 // Compile if we must. NOTE: Double-check locking.
1056 if (meth->prepared == NULL)
1057 {
1058 _Jv_MutexLock (&compile_mutex);
1059 if (meth->prepared == NULL)
1060 meth->compile (insn_target);
1061 _Jv_MutexUnlock (&compile_mutex);
1062 }
1063
1064 // If we're only compiling, stop here
1065 if (args == NULL)
1066 return;
1067
1068 pc = (insn_slot *) meth->prepared;
1069
1070 #else
1071
1072 #define NEXT_INSN goto *(insn_target[*pc++])
1073
1074 #define GET1S() get1s (pc++)
1075 #define GET2S() (pc += 2, get2s (pc- 2))
1076 #define GET1U() get1u (pc++)
1077 #define GET2U() (pc += 2, get2u (pc - 2))
1078 // Note that these could be more efficient when not handling 'ldc
1079 // class'.
1080 #define AVAL1U() \
1081 ({ int index = get1u (pc++); \
1082 resolve_pool_entry (meth->defining_class, index).o; })
1083 #define AVAL2U() \
1084 ({ int index = get2u (pc); pc += 2; \
1085 resolve_pool_entry (meth->defining_class, index).o; })
1086 // Note that we don't need to resolve the pool entry here as class
1087 // constants are never wide.
1088 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1089 #define SKIP_GOTO pc += 2
1090 #define GOTO_VAL() pc - 1 + get2s (pc)
1091 #define PCVAL(unionval) unionval.i
1092 #define AMPAMP(label) NULL
1093
1094 pc = bytecode ();
1095
1096 #endif /* DIRECT_THREADED */
1097
1098 #define TAKE_GOTO pc = GOTO_VAL ()
1099
1100 /* Go straight at it! the ffi raw format matches the internal
1101 stack representation exactly. At least, that's the idea.
1102 */
1103 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1104
1105 _Jv_word *pool_data = meth->defining_class->constants.data;
1106
1107 /* These three are temporaries for common code used by several
1108 instructions. */
1109 void (*fun)();
1110 _Jv_ResolvedMethod* rmeth;
1111 int tmpval;
1112
1113 try
1114 {
1115 // We keep nop around. It is used if we're interpreting the
1116 // bytecodes and not doing direct threading.
1117 insn_nop:
1118 NEXT_INSN;
1119
1120 /* The first few instructions here are ordered according to their
1121 frequency, in the hope that this will improve code locality a
1122 little. */
1123
1124 insn_aload_0: // 0x2a
1125 LOADA (0);
1126 NEXT_INSN;
1127
1128 insn_iload: // 0x15
1129 LOADI (GET1U ());
1130 NEXT_INSN;
1131
1132 insn_iload_1: // 0x1b
1133 LOADI (1);
1134 NEXT_INSN;
1135
1136 insn_invokevirtual: // 0xb6
1137 {
1138 int index = GET2U ();
1139
1140 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1141 * value already is resolved. If we want to clutter up the
1142 * code here to gain a little performance, then we can check
1143 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1144 * directly. For now, I don't think it is worth it. */
1145
1146 SAVE_PC();
1147 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1148 index)).rmethod;
1149
1150 sp -= rmeth->stack_item_count;
1151 // We don't use NULLCHECK here because we can't rely on that
1152 // working if the method is final. So instead we do an
1153 // explicit test.
1154 if (! sp[0].o)
1155 {
1156 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1157 throw new java::lang::NullPointerException;
1158 }
1159
1160 if (rmeth->vtable_index == -1)
1161 {
1162 // final methods do not appear in the vtable,
1163 // if it does not appear in the superclass.
1164 fun = (void (*)()) rmeth->method->ncode;
1165 }
1166 else
1167 {
1168 jobject rcv = sp[0].o;
1169 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1170 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1171 }
1172
1173 #ifdef DIRECT_THREADED
1174 // Rewrite instruction so that we use a faster pre-resolved
1175 // method.
1176 pc[-2].insn = &&invokevirtual_resolved;
1177 pc[-1].datum = rmeth;
1178 #endif /* DIRECT_THREADED */
1179 }
1180 goto perform_invoke;
1181
1182 #ifdef DIRECT_THREADED
1183 invokevirtual_resolved:
1184 {
1185 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1186 sp -= rmeth->stack_item_count;
1187 // We don't use NULLCHECK here because we can't rely on that
1188 // working if the method is final. So instead we do an
1189 // explicit test.
1190 if (! sp[0].o)
1191 {
1192 SAVE_PC();
1193 throw new java::lang::NullPointerException;
1194 }
1195
1196 if (rmeth->vtable_index == -1)
1197 {
1198 // final methods do not appear in the vtable,
1199 // if it does not appear in the superclass.
1200 fun = (void (*)()) rmeth->method->ncode;
1201 }
1202 else
1203 {
1204 jobject rcv = sp[0].o;
1205 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1206 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1207 }
1208 }
1209 goto perform_invoke;
1210 #endif /* DIRECT_THREADED */
1211
1212 perform_invoke:
1213 {
1214 SAVE_PC();
1215
1216 /* here goes the magic again... */
1217 ffi_cif *cif = &rmeth->cif;
1218 ffi_raw *raw = (ffi_raw*) sp;
1219
1220 _Jv_value rvalue;
1221
1222 #if FFI_NATIVE_RAW_API
1223 /* We assume that this is only implemented if it's correct */
1224 /* to use it here. On a 64 bit machine, it never is. */
1225 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1226 #else
1227 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1228 #endif
1229
1230 int rtype = cif->rtype->type;
1231
1232 /* the likelyhood of object, int, or void return is very high,
1233 * so those are checked before the switch */
1234 if (rtype == FFI_TYPE_POINTER)
1235 {
1236 PUSHA (rvalue.object_value);
1237 }
1238 else if (rtype == FFI_TYPE_SINT32)
1239 {
1240 PUSHI (rvalue.int_value);
1241 }
1242 else if (rtype == FFI_TYPE_VOID)
1243 {
1244 /* skip */
1245 }
1246 else
1247 {
1248 switch (rtype)
1249 {
1250 case FFI_TYPE_SINT8:
1251 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1252 break;
1253
1254 case FFI_TYPE_SINT16:
1255 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1256 break;
1257
1258 case FFI_TYPE_UINT16:
1259 PUSHI (rvalue.int_value & 0xffff);
1260 break;
1261
1262 case FFI_TYPE_FLOAT:
1263 PUSHF (rvalue.float_value);
1264 break;
1265
1266 case FFI_TYPE_DOUBLE:
1267 PUSHD (rvalue.double_value);
1268 break;
1269
1270 case FFI_TYPE_SINT64:
1271 PUSHL (rvalue.long_value);
1272 break;
1273
1274 default:
1275 throw_internal_error ("unknown return type in invokeXXX");
1276 }
1277 }
1278 }
1279 NEXT_INSN;
1280
1281 insn_aconst_null:
1282 PUSHA (NULL);
1283 NEXT_INSN;
1284
1285 insn_iconst_m1:
1286 PUSHI (-1);
1287 NEXT_INSN;
1288
1289 insn_iconst_0:
1290 PUSHI (0);
1291 NEXT_INSN;
1292
1293 insn_iconst_1:
1294 PUSHI (1);
1295 NEXT_INSN;
1296
1297 insn_iconst_2:
1298 PUSHI (2);
1299 NEXT_INSN;
1300
1301 insn_iconst_3:
1302 PUSHI (3);
1303 NEXT_INSN;
1304
1305 insn_iconst_4:
1306 PUSHI (4);
1307 NEXT_INSN;
1308
1309 insn_iconst_5:
1310 PUSHI (5);
1311 NEXT_INSN;
1312
1313 insn_lconst_0:
1314 PUSHL (0);
1315 NEXT_INSN;
1316
1317 insn_lconst_1:
1318 PUSHL (1);
1319 NEXT_INSN;
1320
1321 insn_fconst_0:
1322 PUSHF (0);
1323 NEXT_INSN;
1324
1325 insn_fconst_1:
1326 PUSHF (1);
1327 NEXT_INSN;
1328
1329 insn_fconst_2:
1330 PUSHF (2);
1331 NEXT_INSN;
1332
1333 insn_dconst_0:
1334 PUSHD (0);
1335 NEXT_INSN;
1336
1337 insn_dconst_1:
1338 PUSHD (1);
1339 NEXT_INSN;
1340
1341 insn_bipush:
1342 // For direct threaded, bipush and sipush are the same.
1343 #ifndef DIRECT_THREADED
1344 PUSHI (GET1S ());
1345 NEXT_INSN;
1346 #endif /* DIRECT_THREADED */
1347 insn_sipush:
1348 PUSHI (GET2S ());
1349 NEXT_INSN;
1350
1351 insn_ldc:
1352 // For direct threaded, ldc and ldc_w are the same.
1353 #ifndef DIRECT_THREADED
1354 PUSHA ((jobject) AVAL1U ());
1355 NEXT_INSN;
1356 #endif /* DIRECT_THREADED */
1357 insn_ldc_w:
1358 PUSHA ((jobject) AVAL2U ());
1359 NEXT_INSN;
1360
1361 #ifdef DIRECT_THREADED
1362 // For direct threaded we have a separate 'ldc class' operation.
1363 insn_ldc_class:
1364 {
1365 // We could rewrite the instruction at this point.
1366 int index = INTVAL ();
1367 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1368 index)).o;
1369 PUSHA (k);
1370 }
1371 NEXT_INSN;
1372 #endif /* DIRECT_THREADED */
1373
1374 insn_ldc2_w:
1375 {
1376 void *where = AVAL2UP ();
1377 memcpy (sp, where, 2*sizeof (_Jv_word));
1378 sp += 2;
1379 }
1380 NEXT_INSN;
1381
1382 insn_lload:
1383 LOADL (GET1U ());
1384 NEXT_INSN;
1385
1386 insn_fload:
1387 LOADF (GET1U ());
1388 NEXT_INSN;
1389
1390 insn_dload:
1391 LOADD (GET1U ());
1392 NEXT_INSN;
1393
1394 insn_aload:
1395 LOADA (GET1U ());
1396 NEXT_INSN;
1397
1398 insn_iload_0:
1399 LOADI (0);
1400 NEXT_INSN;
1401
1402 insn_iload_2:
1403 LOADI (2);
1404 NEXT_INSN;
1405
1406 insn_iload_3:
1407 LOADI (3);
1408 NEXT_INSN;
1409
1410 insn_lload_0:
1411 LOADL (0);
1412 NEXT_INSN;
1413
1414 insn_lload_1:
1415 LOADL (1);
1416 NEXT_INSN;
1417
1418 insn_lload_2:
1419 LOADL (2);
1420 NEXT_INSN;
1421
1422 insn_lload_3:
1423 LOADL (3);
1424 NEXT_INSN;
1425
1426 insn_fload_0:
1427 LOADF (0);
1428 NEXT_INSN;
1429
1430 insn_fload_1:
1431 LOADF (1);
1432 NEXT_INSN;
1433
1434 insn_fload_2:
1435 LOADF (2);
1436 NEXT_INSN;
1437
1438 insn_fload_3:
1439 LOADF (3);
1440 NEXT_INSN;
1441
1442 insn_dload_0:
1443 LOADD (0);
1444 NEXT_INSN;
1445
1446 insn_dload_1:
1447 LOADD (1);
1448 NEXT_INSN;
1449
1450 insn_dload_2:
1451 LOADD (2);
1452 NEXT_INSN;
1453
1454 insn_dload_3:
1455 LOADD (3);
1456 NEXT_INSN;
1457
1458 insn_aload_1:
1459 LOADA(1);
1460 NEXT_INSN;
1461
1462 insn_aload_2:
1463 LOADA(2);
1464 NEXT_INSN;
1465
1466 insn_aload_3:
1467 LOADA(3);
1468 NEXT_INSN;
1469
1470 insn_iaload:
1471 {
1472 jint index = POPI();
1473 jintArray arr = (jintArray) POPA();
1474 NULLARRAYCHECK (arr);
1475 ARRAYBOUNDSCHECK (arr, index);
1476 PUSHI( elements(arr)[index] );
1477 }
1478 NEXT_INSN;
1479
1480 insn_laload:
1481 {
1482 jint index = POPI();
1483 jlongArray arr = (jlongArray) POPA();
1484 NULLARRAYCHECK (arr);
1485 ARRAYBOUNDSCHECK (arr, index);
1486 PUSHL( elements(arr)[index] );
1487 }
1488 NEXT_INSN;
1489
1490 insn_faload:
1491 {
1492 jint index = POPI();
1493 jfloatArray arr = (jfloatArray) POPA();
1494 NULLARRAYCHECK (arr);
1495 ARRAYBOUNDSCHECK (arr, index);
1496 PUSHF( elements(arr)[index] );
1497 }
1498 NEXT_INSN;
1499
1500 insn_daload:
1501 {
1502 jint index = POPI();
1503 jdoubleArray arr = (jdoubleArray) POPA();
1504 NULLARRAYCHECK (arr);
1505 ARRAYBOUNDSCHECK (arr, index);
1506 PUSHD( elements(arr)[index] );
1507 }
1508 NEXT_INSN;
1509
1510 insn_aaload:
1511 {
1512 jint index = POPI();
1513 jobjectArray arr = (jobjectArray) POPA();
1514 NULLARRAYCHECK (arr);
1515 ARRAYBOUNDSCHECK (arr, index);
1516 PUSHA( elements(arr)[index] );
1517 }
1518 NEXT_INSN;
1519
1520 insn_baload:
1521 {
1522 jint index = POPI();
1523 jbyteArray arr = (jbyteArray) POPA();
1524 NULLARRAYCHECK (arr);
1525 ARRAYBOUNDSCHECK (arr, index);
1526 PUSHI( elements(arr)[index] );
1527 }
1528 NEXT_INSN;
1529
1530 insn_caload:
1531 {
1532 jint index = POPI();
1533 jcharArray arr = (jcharArray) POPA();
1534 NULLARRAYCHECK (arr);
1535 ARRAYBOUNDSCHECK (arr, index);
1536 PUSHI( elements(arr)[index] );
1537 }
1538 NEXT_INSN;
1539
1540 insn_saload:
1541 {
1542 jint index = POPI();
1543 jshortArray arr = (jshortArray) POPA();
1544 NULLARRAYCHECK (arr);
1545 ARRAYBOUNDSCHECK (arr, index);
1546 PUSHI( elements(arr)[index] );
1547 }
1548 NEXT_INSN;
1549
1550 insn_istore:
1551 STOREI (GET1U ());
1552 NEXT_INSN;
1553
1554 insn_lstore:
1555 STOREL (GET1U ());
1556 NEXT_INSN;
1557
1558 insn_fstore:
1559 STOREF (GET1U ());
1560 NEXT_INSN;
1561
1562 insn_dstore:
1563 STORED (GET1U ());
1564 NEXT_INSN;
1565
1566 insn_astore:
1567 STOREA (GET1U ());
1568 NEXT_INSN;
1569
1570 insn_istore_0:
1571 STOREI (0);
1572 NEXT_INSN;
1573
1574 insn_istore_1:
1575 STOREI (1);
1576 NEXT_INSN;
1577
1578 insn_istore_2:
1579 STOREI (2);
1580 NEXT_INSN;
1581
1582 insn_istore_3:
1583 STOREI (3);
1584 NEXT_INSN;
1585
1586 insn_lstore_0:
1587 STOREL (0);
1588 NEXT_INSN;
1589
1590 insn_lstore_1:
1591 STOREL (1);
1592 NEXT_INSN;
1593
1594 insn_lstore_2:
1595 STOREL (2);
1596 NEXT_INSN;
1597
1598 insn_lstore_3:
1599 STOREL (3);
1600 NEXT_INSN;
1601
1602 insn_fstore_0:
1603 STOREF (0);
1604 NEXT_INSN;
1605
1606 insn_fstore_1:
1607 STOREF (1);
1608 NEXT_INSN;
1609
1610 insn_fstore_2:
1611 STOREF (2);
1612 NEXT_INSN;
1613
1614 insn_fstore_3:
1615 STOREF (3);
1616 NEXT_INSN;
1617
1618 insn_dstore_0:
1619 STORED (0);
1620 NEXT_INSN;
1621
1622 insn_dstore_1:
1623 STORED (1);
1624 NEXT_INSN;
1625
1626 insn_dstore_2:
1627 STORED (2);
1628 NEXT_INSN;
1629
1630 insn_dstore_3:
1631 STORED (3);
1632 NEXT_INSN;
1633
1634 insn_astore_0:
1635 STOREA(0);
1636 NEXT_INSN;
1637
1638 insn_astore_1:
1639 STOREA(1);
1640 NEXT_INSN;
1641
1642 insn_astore_2:
1643 STOREA(2);
1644 NEXT_INSN;
1645
1646 insn_astore_3:
1647 STOREA(3);
1648 NEXT_INSN;
1649
1650 insn_iastore:
1651 {
1652 jint value = POPI();
1653 jint index = POPI();
1654 jintArray arr = (jintArray) POPA();
1655 NULLARRAYCHECK (arr);
1656 ARRAYBOUNDSCHECK (arr, index);
1657 elements(arr)[index] = value;
1658 }
1659 NEXT_INSN;
1660
1661 insn_lastore:
1662 {
1663 jlong value = POPL();
1664 jint index = POPI();
1665 jlongArray arr = (jlongArray) POPA();
1666 NULLARRAYCHECK (arr);
1667 ARRAYBOUNDSCHECK (arr, index);
1668 elements(arr)[index] = value;
1669 }
1670 NEXT_INSN;
1671
1672 insn_fastore:
1673 {
1674 jfloat value = POPF();
1675 jint index = POPI();
1676 jfloatArray arr = (jfloatArray) POPA();
1677 NULLARRAYCHECK (arr);
1678 ARRAYBOUNDSCHECK (arr, index);
1679 elements(arr)[index] = value;
1680 }
1681 NEXT_INSN;
1682
1683 insn_dastore:
1684 {
1685 jdouble value = POPD();
1686 jint index = POPI();
1687 jdoubleArray arr = (jdoubleArray) POPA();
1688 NULLARRAYCHECK (arr);
1689 ARRAYBOUNDSCHECK (arr, index);
1690 elements(arr)[index] = value;
1691 }
1692 NEXT_INSN;
1693
1694 insn_aastore:
1695 {
1696 jobject value = POPA();
1697 jint index = POPI();
1698 jobjectArray arr = (jobjectArray) POPA();
1699 NULLARRAYCHECK (arr);
1700 ARRAYBOUNDSCHECK (arr, index);
1701 _Jv_CheckArrayStore (arr, value);
1702 elements(arr)[index] = value;
1703 }
1704 NEXT_INSN;
1705
1706 insn_bastore:
1707 {
1708 jbyte value = (jbyte) POPI();
1709 jint index = POPI();
1710 jbyteArray arr = (jbyteArray) POPA();
1711 NULLARRAYCHECK (arr);
1712 ARRAYBOUNDSCHECK (arr, index);
1713 elements(arr)[index] = value;
1714 }
1715 NEXT_INSN;
1716
1717 insn_castore:
1718 {
1719 jchar value = (jchar) POPI();
1720 jint index = POPI();
1721 jcharArray arr = (jcharArray) POPA();
1722 NULLARRAYCHECK (arr);
1723 ARRAYBOUNDSCHECK (arr, index);
1724 elements(arr)[index] = value;
1725 }
1726 NEXT_INSN;
1727
1728 insn_sastore:
1729 {
1730 jshort value = (jshort) POPI();
1731 jint index = POPI();
1732 jshortArray arr = (jshortArray) POPA();
1733 NULLARRAYCHECK (arr);
1734 ARRAYBOUNDSCHECK (arr, index);
1735 elements(arr)[index] = value;
1736 }
1737 NEXT_INSN;
1738
1739 insn_pop:
1740 sp -= 1;
1741 NEXT_INSN;
1742
1743 insn_pop2:
1744 sp -= 2;
1745 NEXT_INSN;
1746
1747 insn_dup:
1748 sp[0] = sp[-1];
1749 sp += 1;
1750 NEXT_INSN;
1751
1752 insn_dup_x1:
1753 dupx (sp, 1, 1); sp+=1;
1754 NEXT_INSN;
1755
1756 insn_dup_x2:
1757 dupx (sp, 1, 2); sp+=1;
1758 NEXT_INSN;
1759
1760 insn_dup2:
1761 sp[0] = sp[-2];
1762 sp[1] = sp[-1];
1763 sp += 2;
1764 NEXT_INSN;
1765
1766 insn_dup2_x1:
1767 dupx (sp, 2, 1); sp+=2;
1768 NEXT_INSN;
1769
1770 insn_dup2_x2:
1771 dupx (sp, 2, 2); sp+=2;
1772 NEXT_INSN;
1773
1774 insn_swap:
1775 {
1776 jobject tmp1 = POPA();
1777 jobject tmp2 = POPA();
1778 PUSHA (tmp1);
1779 PUSHA (tmp2);
1780 }
1781 NEXT_INSN;
1782
1783 insn_iadd:
1784 BINOPI(+);
1785 NEXT_INSN;
1786
1787 insn_ladd:
1788 BINOPL(+);
1789 NEXT_INSN;
1790
1791 insn_fadd:
1792 BINOPF(+);
1793 NEXT_INSN;
1794
1795 insn_dadd:
1796 BINOPD(+);
1797 NEXT_INSN;
1798
1799 insn_isub:
1800 BINOPI(-);
1801 NEXT_INSN;
1802
1803 insn_lsub:
1804 BINOPL(-);
1805 NEXT_INSN;
1806
1807 insn_fsub:
1808 BINOPF(-);
1809 NEXT_INSN;
1810
1811 insn_dsub:
1812 BINOPD(-);
1813 NEXT_INSN;
1814
1815 insn_imul:
1816 BINOPI(*);
1817 NEXT_INSN;
1818
1819 insn_lmul:
1820 BINOPL(*);
1821 NEXT_INSN;
1822
1823 insn_fmul:
1824 BINOPF(*);
1825 NEXT_INSN;
1826
1827 insn_dmul:
1828 BINOPD(*);
1829 NEXT_INSN;
1830
1831 insn_idiv:
1832 {
1833 jint value2 = POPI();
1834 jint value1 = POPI();
1835 jint res = _Jv_divI (value1, value2);
1836 PUSHI (res);
1837 }
1838 NEXT_INSN;
1839
1840 insn_ldiv:
1841 {
1842 jlong value2 = POPL();
1843 jlong value1 = POPL();
1844 jlong res = _Jv_divJ (value1, value2);
1845 PUSHL (res);
1846 }
1847 NEXT_INSN;
1848
1849 insn_fdiv:
1850 {
1851 jfloat value2 = POPF();
1852 jfloat value1 = POPF();
1853 jfloat res = value1 / value2;
1854 PUSHF (res);
1855 }
1856 NEXT_INSN;
1857
1858 insn_ddiv:
1859 {
1860 jdouble value2 = POPD();
1861 jdouble value1 = POPD();
1862 jdouble res = value1 / value2;
1863 PUSHD (res);
1864 }
1865 NEXT_INSN;
1866
1867 insn_irem:
1868 {
1869 jint value2 = POPI();
1870 jint value1 = POPI();
1871 jint res = _Jv_remI (value1, value2);
1872 PUSHI (res);
1873 }
1874 NEXT_INSN;
1875
1876 insn_lrem:
1877 {
1878 jlong value2 = POPL();
1879 jlong value1 = POPL();
1880 jlong res = _Jv_remJ (value1, value2);
1881 PUSHL (res);
1882 }
1883 NEXT_INSN;
1884
1885 insn_frem:
1886 {
1887 jfloat value2 = POPF();
1888 jfloat value1 = POPF();
1889 jfloat res = __ieee754_fmod (value1, value2);
1890 PUSHF (res);
1891 }
1892 NEXT_INSN;
1893
1894 insn_drem:
1895 {
1896 jdouble value2 = POPD();
1897 jdouble value1 = POPD();
1898 jdouble res = __ieee754_fmod (value1, value2);
1899 PUSHD (res);
1900 }
1901 NEXT_INSN;
1902
1903 insn_ineg:
1904 {
1905 jint value = POPI();
1906 PUSHI (value * -1);
1907 }
1908 NEXT_INSN;
1909
1910 insn_lneg:
1911 {
1912 jlong value = POPL();
1913 PUSHL (value * -1);
1914 }
1915 NEXT_INSN;
1916
1917 insn_fneg:
1918 {
1919 jfloat value = POPF();
1920 PUSHF (value * -1);
1921 }
1922 NEXT_INSN;
1923
1924 insn_dneg:
1925 {
1926 jdouble value = POPD();
1927 PUSHD (value * -1);
1928 }
1929 NEXT_INSN;
1930
1931 insn_ishl:
1932 {
1933 jint shift = (POPI() & 0x1f);
1934 jint value = POPI();
1935 PUSHI (value << shift);
1936 }
1937 NEXT_INSN;
1938
1939 insn_lshl:
1940 {
1941 jint shift = (POPI() & 0x3f);
1942 jlong value = POPL();
1943 PUSHL (value << shift);
1944 }
1945 NEXT_INSN;
1946
1947 insn_ishr:
1948 {
1949 jint shift = (POPI() & 0x1f);
1950 jint value = POPI();
1951 PUSHI (value >> shift);
1952 }
1953 NEXT_INSN;
1954
1955 insn_lshr:
1956 {
1957 jint shift = (POPI() & 0x3f);
1958 jlong value = POPL();
1959 PUSHL (value >> shift);
1960 }
1961 NEXT_INSN;
1962
1963 insn_iushr:
1964 {
1965 jint shift = (POPI() & 0x1f);
1966 _Jv_uint value = (_Jv_uint) POPI();
1967 PUSHI ((jint) (value >> shift));
1968 }
1969 NEXT_INSN;
1970
1971 insn_lushr:
1972 {
1973 jint shift = (POPI() & 0x3f);
1974 _Jv_ulong value = (_Jv_ulong) POPL();
1975 PUSHL ((jlong) (value >> shift));
1976 }
1977 NEXT_INSN;
1978
1979 insn_iand:
1980 BINOPI (&);
1981 NEXT_INSN;
1982
1983 insn_land:
1984 BINOPL (&);
1985 NEXT_INSN;
1986
1987 insn_ior:
1988 BINOPI (|);
1989 NEXT_INSN;
1990
1991 insn_lor:
1992 BINOPL (|);
1993 NEXT_INSN;
1994
1995 insn_ixor:
1996 BINOPI (^);
1997 NEXT_INSN;
1998
1999 insn_lxor:
2000 BINOPL (^);
2001 NEXT_INSN;
2002
2003 insn_iinc:
2004 {
2005 jint index = GET1U ();
2006 jint amount = GET1S ();
2007 locals[index].i += amount;
2008 }
2009 NEXT_INSN;
2010
2011 insn_i2l:
2012 {jlong value = POPI(); PUSHL (value);}
2013 NEXT_INSN;
2014
2015 insn_i2f:
2016 {jfloat value = POPI(); PUSHF (value);}
2017 NEXT_INSN;
2018
2019 insn_i2d:
2020 {jdouble value = POPI(); PUSHD (value);}
2021 NEXT_INSN;
2022
2023 insn_l2i:
2024 {jint value = POPL(); PUSHI (value);}
2025 NEXT_INSN;
2026
2027 insn_l2f:
2028 {jfloat value = POPL(); PUSHF (value);}
2029 NEXT_INSN;
2030
2031 insn_l2d:
2032 {jdouble value = POPL(); PUSHD (value);}
2033 NEXT_INSN;
2034
2035 insn_f2i:
2036 {
2037 using namespace java::lang;
2038 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2039 PUSHI(value);
2040 }
2041 NEXT_INSN;
2042
2043 insn_f2l:
2044 {
2045 using namespace java::lang;
2046 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2047 PUSHL(value);
2048 }
2049 NEXT_INSN;
2050
2051 insn_f2d:
2052 { jdouble value = POPF (); PUSHD(value); }
2053 NEXT_INSN;
2054
2055 insn_d2i:
2056 {
2057 using namespace java::lang;
2058 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2059 PUSHI(value);
2060 }
2061 NEXT_INSN;
2062
2063 insn_d2l:
2064 {
2065 using namespace java::lang;
2066 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2067 PUSHL(value);
2068 }
2069 NEXT_INSN;
2070
2071 insn_d2f:
2072 { jfloat value = POPD (); PUSHF(value); }
2073 NEXT_INSN;
2074
2075 insn_i2b:
2076 { jbyte value = POPI (); PUSHI(value); }
2077 NEXT_INSN;
2078
2079 insn_i2c:
2080 { jchar value = POPI (); PUSHI(value); }
2081 NEXT_INSN;
2082
2083 insn_i2s:
2084 { jshort value = POPI (); PUSHI(value); }
2085 NEXT_INSN;
2086
2087 insn_lcmp:
2088 {
2089 jlong value2 = POPL ();
2090 jlong value1 = POPL ();
2091 if (value1 > value2)
2092 { PUSHI (1); }
2093 else if (value1 == value2)
2094 { PUSHI (0); }
2095 else
2096 { PUSHI (-1); }
2097 }
2098 NEXT_INSN;
2099
2100 insn_fcmpl:
2101 tmpval = -1;
2102 goto fcmp;
2103
2104 insn_fcmpg:
2105 tmpval = 1;
2106
2107 fcmp:
2108 {
2109 jfloat value2 = POPF ();
2110 jfloat value1 = POPF ();
2111 if (value1 > value2)
2112 PUSHI (1);
2113 else if (value1 == value2)
2114 PUSHI (0);
2115 else if (value1 < value2)
2116 PUSHI (-1);
2117 else
2118 PUSHI (tmpval);
2119 }
2120 NEXT_INSN;
2121
2122 insn_dcmpl:
2123 tmpval = -1;
2124 goto dcmp;
2125
2126 insn_dcmpg:
2127 tmpval = 1;
2128
2129 dcmp:
2130 {
2131 jdouble value2 = POPD ();
2132 jdouble value1 = POPD ();
2133 if (value1 > value2)
2134 PUSHI (1);
2135 else if (value1 == value2)
2136 PUSHI (0);
2137 else if (value1 < value2)
2138 PUSHI (-1);
2139 else
2140 PUSHI (tmpval);
2141 }
2142 NEXT_INSN;
2143
2144 insn_ifeq:
2145 {
2146 if (POPI() == 0)
2147 TAKE_GOTO;
2148 else
2149 SKIP_GOTO;
2150 }
2151 NEXT_INSN;
2152
2153 insn_ifne:
2154 {
2155 if (POPI() != 0)
2156 TAKE_GOTO;
2157 else
2158 SKIP_GOTO;
2159 }
2160 NEXT_INSN;
2161
2162 insn_iflt:
2163 {
2164 if (POPI() < 0)
2165 TAKE_GOTO;
2166 else
2167 SKIP_GOTO;
2168 }
2169 NEXT_INSN;
2170
2171 insn_ifge:
2172 {
2173 if (POPI() >= 0)
2174 TAKE_GOTO;
2175 else
2176 SKIP_GOTO;
2177 }
2178 NEXT_INSN;
2179
2180 insn_ifgt:
2181 {
2182 if (POPI() > 0)
2183 TAKE_GOTO;
2184 else
2185 SKIP_GOTO;
2186 }
2187 NEXT_INSN;
2188
2189 insn_ifle:
2190 {
2191 if (POPI() <= 0)
2192 TAKE_GOTO;
2193 else
2194 SKIP_GOTO;
2195 }
2196 NEXT_INSN;
2197
2198 insn_if_icmpeq:
2199 {
2200 jint value2 = POPI();
2201 jint value1 = POPI();
2202 if (value1 == value2)
2203 TAKE_GOTO;
2204 else
2205 SKIP_GOTO;
2206 }
2207 NEXT_INSN;
2208
2209 insn_if_icmpne:
2210 {
2211 jint value2 = POPI();
2212 jint value1 = POPI();
2213 if (value1 != value2)
2214 TAKE_GOTO;
2215 else
2216 SKIP_GOTO;
2217 }
2218 NEXT_INSN;
2219
2220 insn_if_icmplt:
2221 {
2222 jint value2 = POPI();
2223 jint value1 = POPI();
2224 if (value1 < value2)
2225 TAKE_GOTO;
2226 else
2227 SKIP_GOTO;
2228 }
2229 NEXT_INSN;
2230
2231 insn_if_icmpge:
2232 {
2233 jint value2 = POPI();
2234 jint value1 = POPI();
2235 if (value1 >= value2)
2236 TAKE_GOTO;
2237 else
2238 SKIP_GOTO;
2239 }
2240 NEXT_INSN;
2241
2242 insn_if_icmpgt:
2243 {
2244 jint value2 = POPI();
2245 jint value1 = POPI();
2246 if (value1 > value2)
2247 TAKE_GOTO;
2248 else
2249 SKIP_GOTO;
2250 }
2251 NEXT_INSN;
2252
2253 insn_if_icmple:
2254 {
2255 jint value2 = POPI();
2256 jint value1 = POPI();
2257 if (value1 <= value2)
2258 TAKE_GOTO;
2259 else
2260 SKIP_GOTO;
2261 }
2262 NEXT_INSN;
2263
2264 insn_if_acmpeq:
2265 {
2266 jobject value2 = POPA();
2267 jobject value1 = POPA();
2268 if (value1 == value2)
2269 TAKE_GOTO;
2270 else
2271 SKIP_GOTO;
2272 }
2273 NEXT_INSN;
2274
2275 insn_if_acmpne:
2276 {
2277 jobject value2 = POPA();
2278 jobject value1 = POPA();
2279 if (value1 != value2)
2280 TAKE_GOTO;
2281 else
2282 SKIP_GOTO;
2283 }
2284 NEXT_INSN;
2285
2286 insn_goto_w:
2287 #ifndef DIRECT_THREADED
2288 // For direct threaded, goto and goto_w are the same.
2289 pc = pc - 1 + get4 (pc);
2290 NEXT_INSN;
2291 #endif /* DIRECT_THREADED */
2292 insn_goto:
2293 TAKE_GOTO;
2294 NEXT_INSN;
2295
2296 insn_jsr_w:
2297 #ifndef DIRECT_THREADED
2298 // For direct threaded, jsr and jsr_w are the same.
2299 {
2300 pc_t next = pc - 1 + get4 (pc);
2301 pc += 4;
2302 PUSHA ((jobject) pc);
2303 pc = next;
2304 }
2305 NEXT_INSN;
2306 #endif /* DIRECT_THREADED */
2307 insn_jsr:
2308 {
2309 pc_t next = GOTO_VAL();
2310 SKIP_GOTO;
2311 PUSHA ((jobject) pc);
2312 pc = next;
2313 }
2314 NEXT_INSN;
2315
2316 insn_ret:
2317 {
2318 jint index = GET1U ();
2319 pc = (pc_t) PEEKA (index);
2320 }
2321 NEXT_INSN;
2322
2323 insn_tableswitch:
2324 {
2325 #ifdef DIRECT_THREADED
2326 void *def = (pc++)->datum;
2327
2328 int index = POPI();
2329
2330 jint low = INTVAL ();
2331 jint high = INTVAL ();
2332
2333 if (index < low || index > high)
2334 pc = (insn_slot *) def;
2335 else
2336 pc = (insn_slot *) ((pc + index - low)->datum);
2337 #else
2338 pc_t base_pc = pc - 1;
2339 int index = POPI ();
2340
2341 pc_t base = (pc_t) bytecode ();
2342 while ((pc - base) % 4 != 0)
2343 ++pc;
2344
2345 jint def = get4 (pc);
2346 jint low = get4 (pc + 4);
2347 jint high = get4 (pc + 8);
2348 if (index < low || index > high)
2349 pc = base_pc + def;
2350 else
2351 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2352 #endif /* DIRECT_THREADED */
2353 }
2354 NEXT_INSN;
2355
2356 insn_lookupswitch:
2357 {
2358 #ifdef DIRECT_THREADED
2359 void *def = (pc++)->insn;
2360
2361 int index = POPI();
2362
2363 jint npairs = INTVAL ();
2364
2365 int max = npairs - 1;
2366 int min = 0;
2367
2368 // Simple binary search...
2369 while (min < max)
2370 {
2371 int half = (min + max) / 2;
2372 int match = pc[2 * half].int_val;
2373
2374 if (index == match)
2375 {
2376 // Found it.
2377 pc = (insn_slot *) pc[2 * half + 1].datum;
2378 NEXT_INSN;
2379 }
2380 else if (index < match)
2381 // We can use HALF - 1 here because we check again on
2382 // loop exit.
2383 max = half - 1;
2384 else
2385 // We can use HALF + 1 here because we check again on
2386 // loop exit.
2387 min = half + 1;
2388 }
2389 if (index == pc[2 * min].int_val)
2390 pc = (insn_slot *) pc[2 * min + 1].datum;
2391 else
2392 pc = (insn_slot *) def;
2393 #else
2394 unsigned char *base_pc = pc-1;
2395 int index = POPI();
2396
2397 unsigned char* base = bytecode ();
2398 while ((pc-base) % 4 != 0)
2399 ++pc;
2400
2401 jint def = get4 (pc);
2402 jint npairs = get4 (pc+4);
2403
2404 int max = npairs-1;
2405 int min = 0;
2406
2407 // Simple binary search...
2408 while (min < max)
2409 {
2410 int half = (min+max)/2;
2411 int match = get4 (pc+ 4*(2 + 2*half));
2412
2413 if (index == match)
2414 min = max = half;
2415 else if (index < match)
2416 // We can use HALF - 1 here because we check again on
2417 // loop exit.
2418 max = half - 1;
2419 else
2420 // We can use HALF + 1 here because we check again on
2421 // loop exit.
2422 min = half + 1;
2423 }
2424
2425 if (index == get4 (pc+ 4*(2 + 2*min)))
2426 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2427 else
2428 pc = base_pc + def;
2429 #endif /* DIRECT_THREADED */
2430 }
2431 NEXT_INSN;
2432
2433 insn_areturn:
2434 *(jobject *) retp = POPA ();
2435 return;
2436
2437 insn_lreturn:
2438 *(jlong *) retp = POPL ();
2439 return;
2440
2441 insn_freturn:
2442 *(jfloat *) retp = POPF ();
2443 return;
2444
2445 insn_dreturn:
2446 *(jdouble *) retp = POPD ();
2447 return;
2448
2449 insn_ireturn:
2450 *(jint *) retp = POPI ();
2451 return;
2452
2453 insn_return:
2454 return;
2455
2456 insn_getstatic:
2457 {
2458 jint fieldref_index = GET2U ();
2459 SAVE_PC(); // Constant pool resolution could throw.
2460 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2461 _Jv_Field *field = pool_data[fieldref_index].field;
2462
2463 if ((field->flags & Modifier::STATIC) == 0)
2464 throw_incompatible_class_change_error
2465 (JvNewStringLatin1 ("field no longer static"));
2466
2467 jclass type = field->type;
2468
2469 // We rewrite the instruction once we discover what it refers
2470 // to.
2471 void *newinsn = NULL;
2472 if (type->isPrimitive ())
2473 {
2474 switch (type->size_in_bytes)
2475 {
2476 case 1:
2477 PUSHI (*field->u.byte_addr);
2478 newinsn = AMPAMP (getstatic_resolved_1);
2479 break;
2480
2481 case 2:
2482 if (type == JvPrimClass (char))
2483 {
2484 PUSHI (*field->u.char_addr);
2485 newinsn = AMPAMP (getstatic_resolved_char);
2486 }
2487 else
2488 {
2489 PUSHI (*field->u.short_addr);
2490 newinsn = AMPAMP (getstatic_resolved_short);
2491 }
2492 break;
2493
2494 case 4:
2495 PUSHI(*field->u.int_addr);
2496 newinsn = AMPAMP (getstatic_resolved_4);
2497 break;
2498
2499 case 8:
2500 PUSHL(*field->u.long_addr);
2501 newinsn = AMPAMP (getstatic_resolved_8);
2502 break;
2503 }
2504 }
2505 else
2506 {
2507 PUSHA(*field->u.object_addr);
2508 newinsn = AMPAMP (getstatic_resolved_obj);
2509 }
2510
2511 #ifdef DIRECT_THREADED
2512 pc[-2].insn = newinsn;
2513 pc[-1].datum = field->u.addr;
2514 #endif /* DIRECT_THREADED */
2515 }
2516 NEXT_INSN;
2517
2518 #ifdef DIRECT_THREADED
2519 getstatic_resolved_1:
2520 PUSHI (*(jbyte *) AVAL ());
2521 NEXT_INSN;
2522
2523 getstatic_resolved_char:
2524 PUSHI (*(jchar *) AVAL ());
2525 NEXT_INSN;
2526
2527 getstatic_resolved_short:
2528 PUSHI (*(jshort *) AVAL ());
2529 NEXT_INSN;
2530
2531 getstatic_resolved_4:
2532 PUSHI (*(jint *) AVAL ());
2533 NEXT_INSN;
2534
2535 getstatic_resolved_8:
2536 PUSHL (*(jlong *) AVAL ());
2537 NEXT_INSN;
2538
2539 getstatic_resolved_obj:
2540 PUSHA (*(jobject *) AVAL ());
2541 NEXT_INSN;
2542 #endif /* DIRECT_THREADED */
2543
2544 insn_getfield:
2545 {
2546 jint fieldref_index = GET2U ();
2547 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2548 _Jv_Field *field = pool_data[fieldref_index].field;
2549
2550 if ((field->flags & Modifier::STATIC) != 0)
2551 throw_incompatible_class_change_error
2552 (JvNewStringLatin1 ("field is static"));
2553
2554 jclass type = field->type;
2555 jint field_offset = field->u.boffset;
2556 if (field_offset > 0xffff)
2557 throw new java::lang::VirtualMachineError;
2558
2559 jobject obj = POPA();
2560 NULLCHECK(obj);
2561
2562 void *newinsn = NULL;
2563 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2564 if (type->isPrimitive ())
2565 {
2566 switch (type->size_in_bytes)
2567 {
2568 case 1:
2569 PUSHI (val->byte_value);
2570 newinsn = AMPAMP (getfield_resolved_1);
2571 break;
2572
2573 case 2:
2574 if (type == JvPrimClass (char))
2575 {
2576 PUSHI (val->char_value);
2577 newinsn = AMPAMP (getfield_resolved_char);
2578 }
2579 else
2580 {
2581 PUSHI (val->short_value);
2582 newinsn = AMPAMP (getfield_resolved_short);
2583 }
2584 break;
2585
2586 case 4:
2587 PUSHI (val->int_value);
2588 newinsn = AMPAMP (getfield_resolved_4);
2589 break;
2590
2591 case 8:
2592 PUSHL (val->long_value);
2593 newinsn = AMPAMP (getfield_resolved_8);
2594 break;
2595 }
2596 }
2597 else
2598 {
2599 PUSHA (val->object_value);
2600 newinsn = AMPAMP (getfield_resolved_obj);
2601 }
2602
2603 #ifdef DIRECT_THREADED
2604 pc[-2].insn = newinsn;
2605 pc[-1].int_val = field_offset;
2606 #endif /* DIRECT_THREADED */
2607 }
2608 NEXT_INSN;
2609
2610 #ifdef DIRECT_THREADED
2611 getfield_resolved_1:
2612 {
2613 char *obj = (char *) POPA ();
2614 NULLCHECK (obj);
2615 PUSHI (*(jbyte *) (obj + INTVAL ()));
2616 }
2617 NEXT_INSN;
2618
2619 getfield_resolved_char:
2620 {
2621 char *obj = (char *) POPA ();
2622 NULLCHECK (obj);
2623 PUSHI (*(jchar *) (obj + INTVAL ()));
2624 }
2625 NEXT_INSN;
2626
2627 getfield_resolved_short:
2628 {
2629 char *obj = (char *) POPA ();
2630 NULLCHECK (obj);
2631 PUSHI (*(jshort *) (obj + INTVAL ()));
2632 }
2633 NEXT_INSN;
2634
2635 getfield_resolved_4:
2636 {
2637 char *obj = (char *) POPA ();
2638 NULLCHECK (obj);
2639 PUSHI (*(jint *) (obj + INTVAL ()));
2640 }
2641 NEXT_INSN;
2642
2643 getfield_resolved_8:
2644 {
2645 char *obj = (char *) POPA ();
2646 NULLCHECK (obj);
2647 PUSHL (*(jlong *) (obj + INTVAL ()));
2648 }
2649 NEXT_INSN;
2650
2651 getfield_resolved_obj:
2652 {
2653 char *obj = (char *) POPA ();
2654 NULLCHECK (obj);
2655 PUSHA (*(jobject *) (obj + INTVAL ()));
2656 }
2657 NEXT_INSN;
2658 #endif /* DIRECT_THREADED */
2659
2660 insn_putstatic:
2661 {
2662 jint fieldref_index = GET2U ();
2663 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2664 _Jv_Field *field = pool_data[fieldref_index].field;
2665
2666 jclass type = field->type;
2667
2668 // ResolvePoolEntry cannot check this
2669 if ((field->flags & Modifier::STATIC) == 0)
2670 throw_incompatible_class_change_error
2671 (JvNewStringLatin1 ("field no longer static"));
2672
2673 void *newinsn = NULL;
2674 if (type->isPrimitive ())
2675 {
2676 switch (type->size_in_bytes)
2677 {
2678 case 1:
2679 {
2680 jint value = POPI();
2681 *field->u.byte_addr = value;
2682 newinsn = AMPAMP (putstatic_resolved_1);
2683 break;
2684 }
2685
2686 case 2:
2687 {
2688 jint value = POPI();
2689 *field->u.char_addr = value;
2690 newinsn = AMPAMP (putstatic_resolved_2);
2691 break;
2692 }
2693
2694 case 4:
2695 {
2696 jint value = POPI();
2697 *field->u.int_addr = value;
2698 newinsn = AMPAMP (putstatic_resolved_4);
2699 break;
2700 }
2701
2702 case 8:
2703 {
2704 jlong value = POPL();
2705 *field->u.long_addr = value;
2706 newinsn = AMPAMP (putstatic_resolved_8);
2707 break;
2708 }
2709 }
2710 }
2711 else
2712 {
2713 jobject value = POPA();
2714 *field->u.object_addr = value;
2715 newinsn = AMPAMP (putstatic_resolved_obj);
2716 }
2717
2718 #ifdef DIRECT_THREADED
2719 pc[-2].insn = newinsn;
2720 pc[-1].datum = field->u.addr;
2721 #endif /* DIRECT_THREADED */
2722 }
2723 NEXT_INSN;
2724
2725 #ifdef DIRECT_THREADED
2726 putstatic_resolved_1:
2727 *(jbyte *) AVAL () = POPI ();
2728 NEXT_INSN;
2729
2730 putstatic_resolved_2:
2731 *(jchar *) AVAL () = POPI ();
2732 NEXT_INSN;
2733
2734 putstatic_resolved_4:
2735 *(jint *) AVAL () = POPI ();
2736 NEXT_INSN;
2737
2738 putstatic_resolved_8:
2739 *(jlong *) AVAL () = POPL ();
2740 NEXT_INSN;
2741
2742 putstatic_resolved_obj:
2743 *(jobject *) AVAL () = POPA ();
2744 NEXT_INSN;
2745 #endif /* DIRECT_THREADED */
2746
2747 insn_putfield:
2748 {
2749 jint fieldref_index = GET2U ();
2750 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2751 _Jv_Field *field = pool_data[fieldref_index].field;
2752
2753 jclass type = field->type;
2754
2755 if ((field->flags & Modifier::STATIC) != 0)
2756 throw_incompatible_class_change_error
2757 (JvNewStringLatin1 ("field is static"));
2758
2759 jint field_offset = field->u.boffset;
2760 if (field_offset > 0xffff)
2761 throw new java::lang::VirtualMachineError;
2762
2763 void *newinsn = NULL;
2764 if (type->isPrimitive ())
2765 {
2766 switch (type->size_in_bytes)
2767 {
2768 case 1:
2769 {
2770 jint value = POPI();
2771 jobject obj = POPA();
2772 NULLCHECK(obj);
2773 *(jbyte*) ((char*)obj + field_offset) = value;
2774 newinsn = AMPAMP (putfield_resolved_1);
2775 break;
2776 }
2777
2778 case 2:
2779 {
2780 jint value = POPI();
2781 jobject obj = POPA();
2782 NULLCHECK(obj);
2783 *(jchar*) ((char*)obj + field_offset) = value;
2784 newinsn = AMPAMP (putfield_resolved_2);
2785 break;
2786 }
2787
2788 case 4:
2789 {
2790 jint value = POPI();
2791 jobject obj = POPA();
2792 NULLCHECK(obj);
2793 *(jint*) ((char*)obj + field_offset) = value;
2794 newinsn = AMPAMP (putfield_resolved_4);
2795 break;
2796 }
2797
2798 case 8:
2799 {
2800 jlong value = POPL();
2801 jobject obj = POPA();
2802 NULLCHECK(obj);
2803 *(jlong*) ((char*)obj + field_offset) = value;
2804 newinsn = AMPAMP (putfield_resolved_8);
2805 break;
2806 }
2807 }
2808 }
2809 else
2810 {
2811 jobject value = POPA();
2812 jobject obj = POPA();
2813 NULLCHECK(obj);
2814 *(jobject*) ((char*)obj + field_offset) = value;
2815 newinsn = AMPAMP (putfield_resolved_obj);
2816 }
2817
2818 #ifdef DIRECT_THREADED
2819 pc[-2].insn = newinsn;
2820 pc[-1].int_val = field_offset;
2821 #endif /* DIRECT_THREADED */
2822 }
2823 NEXT_INSN;
2824
2825 #ifdef DIRECT_THREADED
2826 putfield_resolved_1:
2827 {
2828 jint val = POPI ();
2829 char *obj = (char *) POPA ();
2830 NULLCHECK (obj);
2831 *(jbyte *) (obj + INTVAL ()) = val;
2832 }
2833 NEXT_INSN;
2834
2835 putfield_resolved_2:
2836 {
2837 jint val = POPI ();
2838 char *obj = (char *) POPA ();
2839 NULLCHECK (obj);
2840 *(jchar *) (obj + INTVAL ()) = val;
2841 }
2842 NEXT_INSN;
2843
2844 putfield_resolved_4:
2845 {
2846 jint val = POPI ();
2847 char *obj = (char *) POPA ();
2848 NULLCHECK (obj);
2849 *(jint *) (obj + INTVAL ()) = val;
2850 }
2851 NEXT_INSN;
2852
2853 putfield_resolved_8:
2854 {
2855 jlong val = POPL ();
2856 char *obj = (char *) POPA ();
2857 NULLCHECK (obj);
2858 *(jlong *) (obj + INTVAL ()) = val;
2859 }
2860 NEXT_INSN;
2861
2862 putfield_resolved_obj:
2863 {
2864 jobject val = POPA ();
2865 char *obj = (char *) POPA ();
2866 NULLCHECK (obj);
2867 *(jobject *) (obj + INTVAL ()) = val;
2868 }
2869 NEXT_INSN;
2870 #endif /* DIRECT_THREADED */
2871
2872 insn_invokespecial:
2873 {
2874 int index = GET2U ();
2875
2876 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2877 index)).rmethod;
2878
2879 sp -= rmeth->stack_item_count;
2880
2881 // We don't use NULLCHECK here because we can't rely on that
2882 // working for <init>. So instead we do an explicit test.
2883 if (! sp[0].o)
2884 {
2885 SAVE_PC();
2886 throw new java::lang::NullPointerException;
2887 }
2888
2889 fun = (void (*)()) rmeth->method->ncode;
2890
2891 #ifdef DIRECT_THREADED
2892 // Rewrite instruction so that we use a faster pre-resolved
2893 // method.
2894 pc[-2].insn = &&invokespecial_resolved;
2895 pc[-1].datum = rmeth;
2896 #endif /* DIRECT_THREADED */
2897 }
2898 goto perform_invoke;
2899
2900 #ifdef DIRECT_THREADED
2901 invokespecial_resolved:
2902 {
2903 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2904 sp -= rmeth->stack_item_count;
2905 // We don't use NULLCHECK here because we can't rely on that
2906 // working for <init>. So instead we do an explicit test.
2907 if (! sp[0].o)
2908 {
2909 SAVE_PC();
2910 throw new java::lang::NullPointerException;
2911 }
2912 fun = (void (*)()) rmeth->method->ncode;
2913 }
2914 goto perform_invoke;
2915 #endif /* DIRECT_THREADED */
2916
2917 insn_invokestatic:
2918 {
2919 int index = GET2U ();
2920
2921 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2922 index)).rmethod;
2923
2924 sp -= rmeth->stack_item_count;
2925
2926 fun = (void (*)()) rmeth->method->ncode;
2927
2928 #ifdef DIRECT_THREADED
2929 // Rewrite instruction so that we use a faster pre-resolved
2930 // method.
2931 pc[-2].insn = &&invokestatic_resolved;
2932 pc[-1].datum = rmeth;
2933 #endif /* DIRECT_THREADED */
2934 }
2935 goto perform_invoke;
2936
2937 #ifdef DIRECT_THREADED
2938 invokestatic_resolved:
2939 {
2940 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2941 sp -= rmeth->stack_item_count;
2942 fun = (void (*)()) rmeth->method->ncode;
2943 }
2944 goto perform_invoke;
2945 #endif /* DIRECT_THREADED */
2946
2947 insn_invokeinterface:
2948 {
2949 int index = GET2U ();
2950
2951 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2952 index)).rmethod;
2953
2954 sp -= rmeth->stack_item_count;
2955
2956 jobject rcv = sp[0].o;
2957
2958 NULLCHECK (rcv);
2959
2960 fun = (void (*)())
2961 _Jv_LookupInterfaceMethod (rcv->getClass (),
2962 rmeth->method->name,
2963 rmeth->method->signature);
2964
2965 #ifdef DIRECT_THREADED
2966 // Rewrite instruction so that we use a faster pre-resolved
2967 // method.
2968 pc[-2].insn = &&invokeinterface_resolved;
2969 pc[-1].datum = rmeth;
2970 #else
2971 // Skip dummy bytes.
2972 pc += 2;
2973 #endif /* DIRECT_THREADED */
2974 }
2975 goto perform_invoke;
2976
2977 #ifdef DIRECT_THREADED
2978 invokeinterface_resolved:
2979 {
2980 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2981 sp -= rmeth->stack_item_count;
2982 jobject rcv = sp[0].o;
2983 NULLCHECK (rcv);
2984 fun = (void (*)())
2985 _Jv_LookupInterfaceMethod (rcv->getClass (),
2986 rmeth->method->name,
2987 rmeth->method->signature);
2988 }
2989 goto perform_invoke;
2990 #endif /* DIRECT_THREADED */
2991
2992 insn_new:
2993 {
2994 int index = GET2U ();
2995 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2996 index)).clazz;
2997 /* VM spec, section 3.11.5 */
2998 if ((klass->getModifiers() & Modifier::ABSTRACT)
2999 || klass->isInterface())
3000 throw new java::lang::InstantiationException;
3001 jobject res = _Jv_AllocObject (klass);
3002 PUSHA (res);
3003
3004 #ifdef DIRECT_THREADED
3005 pc[-2].insn = &&new_resolved;
3006 pc[-1].datum = klass;
3007 #endif /* DIRECT_THREADED */
3008 }
3009 NEXT_INSN;
3010
3011 #ifdef DIRECT_THREADED
3012 new_resolved:
3013 {
3014 jclass klass = (jclass) AVAL ();
3015 jobject res = _Jv_AllocObject (klass);
3016 PUSHA (res);
3017 }
3018 NEXT_INSN;
3019 #endif /* DIRECT_THREADED */
3020
3021 insn_newarray:
3022 {
3023 int atype = GET1U ();
3024 int size = POPI();
3025 jobject result = _Jv_NewArray (atype, size);
3026 PUSHA (result);
3027 }
3028 NEXT_INSN;
3029
3030 insn_anewarray:
3031 {
3032 int index = GET2U ();
3033 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3034 index)).clazz;
3035 int size = POPI();
3036 jobject result = _Jv_NewObjectArray (size, klass, 0);
3037 PUSHA (result);
3038
3039 #ifdef DIRECT_THREADED
3040 pc[-2].insn = &&anewarray_resolved;
3041 pc[-1].datum = klass;
3042 #endif /* DIRECT_THREADED */
3043 }
3044 NEXT_INSN;
3045
3046 #ifdef DIRECT_THREADED
3047 anewarray_resolved:
3048 {
3049 jclass klass = (jclass) AVAL ();
3050 int size = POPI ();
3051 jobject result = _Jv_NewObjectArray (size, klass, 0);
3052 PUSHA (result);
3053 }
3054 NEXT_INSN;
3055 #endif /* DIRECT_THREADED */
3056
3057 insn_arraylength:
3058 {
3059 __JArray *arr = (__JArray*)POPA();
3060 NULLARRAYCHECK (arr);
3061 PUSHI (arr->length);
3062 }
3063 NEXT_INSN;
3064
3065 insn_athrow:
3066 {
3067 jobject value = POPA();
3068 throw static_cast<jthrowable>(value);
3069 }
3070 NEXT_INSN;
3071
3072 insn_checkcast:
3073 {
3074 SAVE_PC();
3075 jobject value = POPA();
3076 jint index = GET2U ();
3077 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3078 index)).clazz;
3079
3080 value = (jobject) _Jv_CheckCast (to, value);
3081
3082 PUSHA (value);
3083
3084 #ifdef DIRECT_THREADED
3085 pc[-2].insn = &&checkcast_resolved;
3086 pc[-1].datum = to;
3087 #endif /* DIRECT_THREADED */
3088 }
3089 NEXT_INSN;
3090
3091 #ifdef DIRECT_THREADED
3092 checkcast_resolved:
3093 {
3094 SAVE_PC();
3095 jobject value = POPA ();
3096 jclass to = (jclass) AVAL ();
3097 value = (jobject) _Jv_CheckCast (to, value);
3098 PUSHA (value);
3099 }
3100 NEXT_INSN;
3101 #endif /* DIRECT_THREADED */
3102
3103 insn_instanceof:
3104 {
3105 SAVE_PC();
3106 jobject value = POPA();
3107 jint index = GET2U ();
3108 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3109 index)).clazz;
3110 PUSHI (to->isInstance (value));
3111
3112 #ifdef DIRECT_THREADED
3113 pc[-2].insn = &&instanceof_resolved;
3114 pc[-1].datum = to;
3115 #endif /* DIRECT_THREADED */
3116 }
3117 NEXT_INSN;
3118
3119 #ifdef DIRECT_THREADED
3120 instanceof_resolved:
3121 {
3122 jobject value = POPA ();
3123 jclass to = (jclass) AVAL ();
3124 PUSHI (to->isInstance (value));
3125 }
3126 NEXT_INSN;
3127 #endif /* DIRECT_THREADED */
3128
3129 insn_monitorenter:
3130 {
3131 jobject value = POPA();
3132 NULLCHECK(value);
3133 _Jv_MonitorEnter (value);
3134 }
3135 NEXT_INSN;
3136
3137 insn_monitorexit:
3138 {
3139 jobject value = POPA();
3140 NULLCHECK(value);
3141 _Jv_MonitorExit (value);
3142 }
3143 NEXT_INSN;
3144
3145 insn_ifnull:
3146 {
3147 jobject val = POPA();
3148 if (val == NULL)
3149 TAKE_GOTO;
3150 else
3151 SKIP_GOTO;
3152 }
3153 NEXT_INSN;
3154
3155 insn_ifnonnull:
3156 {
3157 jobject val = POPA();
3158 if (val != NULL)
3159 TAKE_GOTO;
3160 else
3161 SKIP_GOTO;
3162 }
3163 NEXT_INSN;
3164
3165 insn_multianewarray:
3166 {
3167 int kind_index = GET2U ();
3168 int dim = GET1U ();
3169
3170 jclass type
3171 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3172 kind_index)).clazz;
3173 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3174
3175 for (int i = dim - 1; i >= 0; i--)
3176 {
3177 sizes[i] = POPI ();
3178 }
3179
3180 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3181
3182 PUSHA (res);
3183 }
3184 NEXT_INSN;
3185
3186 #ifndef DIRECT_THREADED
3187 insn_wide:
3188 {
3189 jint the_mod_op = get1u (pc++);
3190 jint wide = get2u (pc); pc += 2;
3191
3192 switch (the_mod_op)
3193 {
3194 case op_istore:
3195 STOREI (wide);
3196 NEXT_INSN;
3197
3198 case op_fstore:
3199 STOREF (wide);
3200 NEXT_INSN;
3201
3202 case op_astore:
3203 STOREA (wide);
3204 NEXT_INSN;
3205
3206 case op_lload:
3207 LOADL (wide);
3208 NEXT_INSN;
3209
3210 case op_dload:
3211 LOADD (wide);
3212 NEXT_INSN;
3213
3214 case op_iload:
3215 LOADI (wide);
3216 NEXT_INSN;
3217
3218 case op_fload:
3219 LOADF (wide);
3220 NEXT_INSN;
3221
3222 case op_aload:
3223 LOADA (wide);
3224 NEXT_INSN;
3225
3226 case op_lstore:
3227 STOREL (wide);
3228 NEXT_INSN;
3229
3230 case op_dstore:
3231 STORED (wide);
3232 NEXT_INSN;
3233
3234 case op_ret:
3235 pc = (unsigned char*) PEEKA (wide);
3236 NEXT_INSN;
3237
3238 case op_iinc:
3239 {
3240 jint amount = get2s (pc); pc += 2;
3241 jint value = PEEKI (wide);
3242 POKEI (wide, value+amount);
3243 }
3244 NEXT_INSN;
3245
3246 default:
3247 throw_internal_error ("illegal bytecode modified by wide");
3248 }
3249
3250 }
3251 #endif /* DIRECT_THREADED */
3252 }
3253 catch (java::lang::Throwable *ex)
3254 {
3255 #ifdef DIRECT_THREADED
3256 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3257 #else
3258 int logical_pc = pc - 1 - bytecode ();
3259 #endif
3260 _Jv_InterpException *exc = meth->exceptions ();
3261 jclass exc_class = ex->getClass ();
3262
3263 for (int i = 0; i < meth->exc_count; i++)
3264 {
3265 if (PCVAL (exc[i].start_pc) <= logical_pc
3266 && logical_pc < PCVAL (exc[i].end_pc))
3267 {
3268 #ifdef DIRECT_THREADED
3269 jclass handler = (jclass) exc[i].handler_type.p;
3270 #else
3271 jclass handler = NULL;
3272 if (exc[i].handler_type.i != 0)
3273 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3274 exc[i].handler_type.i)).clazz;
3275 #endif /* DIRECT_THREADED */
3276
3277 if (handler == NULL || handler->isAssignableFrom (exc_class))
3278 {
3279 #ifdef DIRECT_THREADED
3280 pc = (insn_slot *) exc[i].handler_pc.p;
3281 #else
3282 pc = bytecode () + exc[i].handler_pc.i;
3283 #endif /* DIRECT_THREADED */
3284 sp = stack;
3285 sp++->o = ex; // Push exception.
3286 NEXT_INSN;
3287 }
3288 }
3289 }
3290
3291 // No handler, so re-throw.
3292 throw ex;
3293 }
3294 }
3295
3296 static void
3297 throw_internal_error (char *msg)
3298 {
3299 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3300 }
3301
3302 static void
3303 throw_incompatible_class_change_error (jstring msg)
3304 {
3305 throw new java::lang::IncompatibleClassChangeError (msg);
3306 }
3307
3308 #ifndef HANDLE_SEGV
3309 static java::lang::NullPointerException *null_pointer_exc;
3310 static void
3311 throw_null_pointer_exception ()
3312 {
3313 if (null_pointer_exc == NULL)
3314 null_pointer_exc = new java::lang::NullPointerException;
3315
3316 throw null_pointer_exc;
3317 }
3318 #endif
3319
3320 /* Look up source code line number for given bytecode (or direct threaded
3321 interpreter) PC. */
3322 int
3323 _Jv_InterpMethod::get_source_line(pc_t mpc)
3324 {
3325 int line = line_table_len > 0 ? line_table[0].line : -1;
3326 for (int i = 1; i < line_table_len; i++)
3327 if (line_table[i].pc > mpc)
3328 break;
3329 else
3330 line = line_table[i].line;
3331
3332 return line;
3333 }
3334
3335 /** Do static initialization for fields with a constant initializer */
3336 void
3337 _Jv_InitField (jobject obj, jclass klass, int index)
3338 {
3339 using namespace java::lang::reflect;
3340
3341 if (obj != 0 && klass == 0)
3342 klass = obj->getClass ();
3343
3344 if (!_Jv_IsInterpretedClass (klass))
3345 return;
3346
3347 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3348
3349 _Jv_Field * field = (&klass->fields[0]) + index;
3350
3351 if (index > klass->field_count)
3352 throw_internal_error ("field out of range");
3353
3354 int init = iclass->field_initializers[index];
3355 if (init == 0)
3356 return;
3357
3358 _Jv_Constants *pool = &klass->constants;
3359 int tag = pool->tags[init];
3360
3361 if (! field->isResolved ())
3362 throw_internal_error ("initializing unresolved field");
3363
3364 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3365 throw_internal_error ("initializing non-static field with no object");
3366
3367 void *addr = 0;
3368
3369 if ((field->flags & Modifier::STATIC) != 0)
3370 addr = (void*) field->u.addr;
3371 else
3372 addr = (void*) (((char*)obj) + field->u.boffset);
3373
3374 switch (tag)
3375 {
3376 case JV_CONSTANT_String:
3377 {
3378 jstring str;
3379 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3380 pool->data[init].string = str;
3381 pool->tags[init] = JV_CONSTANT_ResolvedString;
3382 }
3383 /* fall through */
3384
3385 case JV_CONSTANT_ResolvedString:
3386 if (! (field->type == &java::lang::String::class$
3387 || field->type == &java::lang::Class::class$))
3388 throw_class_format_error ("string initialiser to non-string field");
3389
3390 *(jstring*)addr = pool->data[init].string;
3391 break;
3392
3393 case JV_CONSTANT_Integer:
3394 {
3395 int value = pool->data[init].i;
3396
3397 if (field->type == JvPrimClass (boolean))
3398 *(jboolean*)addr = (jboolean)value;
3399
3400 else if (field->type == JvPrimClass (byte))
3401 *(jbyte*)addr = (jbyte)value;
3402
3403 else if (field->type == JvPrimClass (char))
3404 *(jchar*)addr = (jchar)value;
3405
3406 else if (field->type == JvPrimClass (short))
3407 *(jshort*)addr = (jshort)value;
3408
3409 else if (field->type == JvPrimClass (int))
3410 *(jint*)addr = (jint)value;
3411
3412 else
3413 throw_class_format_error ("erroneous field initializer");
3414 }
3415 break;
3416
3417 case JV_CONSTANT_Long:
3418 if (field->type != JvPrimClass (long))
3419 throw_class_format_error ("erroneous field initializer");
3420
3421 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3422 break;
3423
3424 case JV_CONSTANT_Float:
3425 if (field->type != JvPrimClass (float))
3426 throw_class_format_error ("erroneous field initializer");
3427
3428 *(jfloat*)addr = pool->data[init].f;
3429 break;
3430
3431 case JV_CONSTANT_Double:
3432 if (field->type != JvPrimClass (double))
3433 throw_class_format_error ("erroneous field initializer");
3434
3435 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3436 break;
3437
3438 default:
3439 throw_class_format_error ("erroneous field initializer");
3440 }
3441 }
3442
3443 inline static unsigned char*
3444 skip_one_type (unsigned char* ptr)
3445 {
3446 int ch = *ptr++;
3447
3448 while (ch == '[')
3449 {
3450 ch = *ptr++;
3451 }
3452
3453 if (ch == 'L')
3454 {
3455 do { ch = *ptr++; } while (ch != ';');
3456 }
3457
3458 return ptr;
3459 }
3460
3461 static ffi_type*
3462 get_ffi_type_from_signature (unsigned char* ptr)
3463 {
3464 switch (*ptr)
3465 {
3466 case 'L':
3467 case '[':
3468 return &ffi_type_pointer;
3469 break;
3470
3471 case 'Z':
3472 // On some platforms a bool is a byte, on others an int.
3473 if (sizeof (jboolean) == sizeof (jbyte))
3474 return &ffi_type_sint8;
3475 else
3476 {
3477 JvAssert (sizeof (jbyte) == sizeof (jint));
3478 return &ffi_type_sint32;
3479 }
3480 break;
3481
3482 case 'B':
3483 return &ffi_type_sint8;
3484 break;
3485
3486 case 'C':
3487 return &ffi_type_uint16;
3488 break;
3489
3490 case 'S':
3491 return &ffi_type_sint16;
3492 break;
3493
3494 case 'I':
3495 return &ffi_type_sint32;
3496 break;
3497
3498 case 'J':
3499 return &ffi_type_sint64;
3500 break;
3501
3502 case 'F':
3503 return &ffi_type_float;
3504 break;
3505
3506 case 'D':
3507 return &ffi_type_double;
3508 break;
3509
3510 case 'V':
3511 return &ffi_type_void;
3512 break;
3513 }
3514
3515 throw_internal_error ("unknown type in signature");
3516 }
3517
3518 /* this function yields the number of actual arguments, that is, if the
3519 * function is non-static, then one is added to the number of elements
3520 * found in the signature */
3521
3522 int
3523 _Jv_count_arguments (_Jv_Utf8Const *signature,
3524 jboolean staticp)
3525 {
3526 unsigned char *ptr = (unsigned char*) signature->chars();
3527 int arg_count = staticp ? 0 : 1;
3528
3529 /* first, count number of arguments */
3530
3531 // skip '('
3532 ptr++;
3533
3534 // count args
3535 while (*ptr != ')')
3536 {
3537 ptr = skip_one_type (ptr);
3538 arg_count += 1;
3539 }
3540
3541 return arg_count;
3542 }
3543
3544 /* This beast will build a cif, given the signature. Memory for
3545 * the cif itself and for the argument types must be allocated by the
3546 * caller.
3547 */
3548
3549 static int
3550 init_cif (_Jv_Utf8Const* signature,
3551 int arg_count,
3552 jboolean staticp,
3553 ffi_cif *cif,
3554 ffi_type **arg_types,
3555 ffi_type **rtype_p)
3556 {
3557 unsigned char *ptr = (unsigned char*) signature->chars();
3558
3559 int arg_index = 0; // arg number
3560 int item_count = 0; // stack-item count
3561
3562 // setup receiver
3563 if (!staticp)
3564 {
3565 arg_types[arg_index++] = &ffi_type_pointer;
3566 item_count += 1;
3567 }
3568
3569 // skip '('
3570 ptr++;
3571
3572 // assign arg types
3573 while (*ptr != ')')
3574 {
3575 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3576
3577 if (*ptr == 'J' || *ptr == 'D')
3578 item_count += 2;
3579 else
3580 item_count += 1;
3581
3582 ptr = skip_one_type (ptr);
3583 }
3584
3585 // skip ')'
3586 ptr++;
3587 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3588
3589 ptr = skip_one_type (ptr);
3590 if (ptr != (unsigned char*)signature->chars() + signature->len())
3591 throw_internal_error ("did not find end of signature");
3592
3593 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3594 arg_count, rtype, arg_types) != FFI_OK)
3595 throw_internal_error ("ffi_prep_cif failed");
3596
3597 if (rtype_p != NULL)
3598 *rtype_p = rtype;
3599
3600 return item_count;
3601 }
3602
3603 #if FFI_NATIVE_RAW_API
3604 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3605 # define FFI_RAW_SIZE ffi_raw_size
3606 #else
3607 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3608 # define FFI_RAW_SIZE ffi_java_raw_size
3609 #endif
3610
3611 /* we put this one here, and not in interpret.cc because it
3612 * calls the utility routines _Jv_count_arguments
3613 * which are static to this module. The following struct defines the
3614 * layout we use for the stubs, it's only used in the ncode method. */
3615
3616 typedef struct {
3617 ffi_raw_closure closure;
3618 ffi_cif cif;
3619 ffi_type *arg_types[0];
3620 } ncode_closure;
3621
3622 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3623
3624 void *
3625 _Jv_InterpMethod::ncode ()
3626 {
3627 using namespace java::lang::reflect;
3628
3629 if (self->ncode != 0)
3630 return self->ncode;
3631
3632 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3633 int arg_count = _Jv_count_arguments (self->signature, staticp);
3634
3635 ncode_closure *closure =
3636 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3637 + arg_count * sizeof (ffi_type*));
3638
3639 init_cif (self->signature,
3640 arg_count,
3641 staticp,
3642 &closure->cif,
3643 &closure->arg_types[0],
3644 NULL);
3645
3646 ffi_closure_fun fun;
3647
3648 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3649
3650 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3651
3652 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3653 {
3654 if (staticp)
3655 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3656 else
3657 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3658 }
3659 else
3660 {
3661 if (staticp)
3662 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3663 else
3664 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3665 }
3666
3667 FFI_PREP_RAW_CLOSURE (&closure->closure,
3668 &closure->cif,
3669 fun,
3670 (void*)this);
3671
3672 self->ncode = (void*)closure;
3673 return self->ncode;
3674 }
3675
3676 #ifdef DIRECT_THREADED
3677 /* Find the index of the given insn in the array of insn slots
3678 for this method. Returns -1 if not found. */
3679 jlong
3680 _Jv_InterpMethod::insn_index (pc_t pc)
3681 {
3682 jlong left = 0;
3683 jlong right = number_insn_slots;
3684 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3685
3686 while (right >= 0)
3687 {
3688 jlong mid = (left + right) / 2;
3689 if (&slots[mid] == pc)
3690 return mid;
3691
3692 if (pc < &slots[mid])
3693 right = mid - 1;
3694 else
3695 left = mid + 1;
3696 }
3697
3698 return -1;
3699 }
3700 #endif // DIRECT_THREADED
3701
3702 void
3703 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
3704 jintArray& line_numbers,
3705 jlongArray& code_indices)
3706 {
3707 #ifdef DIRECT_THREADED
3708 /* For the DIRECT_THREADED case, if the method has not yet been
3709 * compiled, the linetable will change to insn slots instead of
3710 * bytecode PCs. It is probably easiest, in this case, to simply
3711 * compile the method and guarantee that we are using insn
3712 * slots.
3713 */
3714 _Jv_CompileMethod (this);
3715
3716 if (line_table_len > 0)
3717 {
3718 start = 0;
3719 end = number_insn_slots;
3720 line_numbers = JvNewIntArray (line_table_len);
3721 code_indices = JvNewLongArray (line_table_len);
3722
3723 jint* lines = elements (line_numbers);
3724 jlong* indices = elements (code_indices);
3725 for (int i = 0; i < line_table_len; ++i)
3726 {
3727 lines[i] = line_table[i].line;
3728 indices[i] = insn_index (line_table[i].pc);
3729 }
3730 }
3731 #else // !DIRECT_THREADED
3732 if (line_table_len > 0)
3733 {
3734 start = 0;
3735 end = code_length;
3736 line_numbers = JvNewIntArray (line_table_len);
3737 code_indices = JvNewLongArray (line_table_len);
3738
3739 jint* lines = elements (line_numbers);
3740 jlong* indices = elements (code_indices);
3741 for (int i = 0; i < line_table_len; ++i)
3742 {
3743 lines[i] = line_table[i].line;
3744 indices[i] = (jlong) line_table[i].bytecode_pc;
3745 }
3746 }
3747 #endif // !DIRECT_THREADED
3748 }
3749
3750 void *
3751 _Jv_JNIMethod::ncode ()
3752 {
3753 using namespace java::lang::reflect;
3754
3755 if (self->ncode != 0)
3756 return self->ncode;
3757
3758 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3759 int arg_count = _Jv_count_arguments (self->signature, staticp);
3760
3761 ncode_closure *closure =
3762 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3763 + arg_count * sizeof (ffi_type*));
3764
3765 ffi_type *rtype;
3766 init_cif (self->signature,
3767 arg_count,
3768 staticp,
3769 &closure->cif,
3770 &closure->arg_types[0],
3771 &rtype);
3772
3773 ffi_closure_fun fun;
3774
3775 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3776
3777 // Initialize the argument types and CIF that represent the actual
3778 // underlying JNI function.
3779 int extra_args = 1;
3780 if ((self->accflags & Modifier::STATIC))
3781 ++extra_args;
3782 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3783 * sizeof (ffi_type *));
3784 int offset = 0;
3785 jni_arg_types[offset++] = &ffi_type_pointer;
3786 if ((self->accflags & Modifier::STATIC))
3787 jni_arg_types[offset++] = &ffi_type_pointer;
3788 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3789 arg_count * sizeof (ffi_type *));
3790
3791 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3792 extra_args + arg_count, rtype,
3793 jni_arg_types) != FFI_OK)
3794 throw_internal_error ("ffi_prep_cif failed for JNI function");
3795
3796 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3797
3798 // FIXME: for now we assume that all native methods for
3799 // interpreted code use JNI.
3800 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3801
3802 FFI_PREP_RAW_CLOSURE (&closure->closure,
3803 &closure->cif,
3804 fun,
3805 (void*) this);
3806
3807 self->ncode = (void *) closure;
3808 return self->ncode;
3809 }
3810
3811 static void
3812 throw_class_format_error (jstring msg)
3813 {
3814 throw (msg
3815 ? new java::lang::ClassFormatError (msg)
3816 : new java::lang::ClassFormatError);
3817 }
3818
3819 static void
3820 throw_class_format_error (char *msg)
3821 {
3822 throw_class_format_error (JvNewStringLatin1 (msg));
3823 }
3824
3825 \f
3826
3827 void
3828 _Jv_InterpreterEngine::do_verify (jclass klass)
3829 {
3830 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3831 for (int i = 0; i < klass->method_count; i++)
3832 {
3833 using namespace java::lang::reflect;
3834 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3835 _Jv_ushort accflags = klass->methods[i].accflags;
3836 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3837 {
3838 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3839 _Jv_VerifyMethod (im);
3840 }
3841 }
3842 }
3843
3844 void
3845 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3846 {
3847 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3848 for (int i = 0; i < klass->method_count; i++)
3849 {
3850 // Just skip abstract methods. This is particularly important
3851 // because we don't resize the interpreted_methods array when
3852 // miranda methods are added to it.
3853 if ((klass->methods[i].accflags
3854 & java::lang::reflect::Modifier::ABSTRACT)
3855 != 0)
3856 continue;
3857
3858 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3859
3860 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3861 != 0)
3862 {
3863 // You might think we could use a virtual `ncode' method in
3864 // the _Jv_MethodBase and unify the native and non-native
3865 // cases. Well, we can't, because we don't allocate these
3866 // objects using `new', and thus they don't get a vtable.
3867 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3868 klass->methods[i].ncode = jnim->ncode ();
3869 }
3870 else if (imeth != 0) // it could be abstract
3871 {
3872 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3873 klass->methods[i].ncode = im->ncode ();
3874 }
3875 }
3876 }
3877
3878 void
3879 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3880 int pointer_size,
3881 int other_size)
3882 {
3883 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3884
3885 // Splitting the allocations here lets us scan reference fields and
3886 // avoid scanning non-reference fields.
3887 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
3888 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
3889
3890 for (int i = 0; i < klass->field_count; i++)
3891 {
3892 _Jv_Field *field = &klass->fields[i];
3893
3894 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3895 continue;
3896
3897 char *base = field->isRef() ? reference_fields : non_reference_fields;
3898 field->u.addr = base + field->u.boffset;
3899
3900 if (iclass->field_initializers[i] != 0)
3901 {
3902 _Jv_Linker::resolve_field (field, klass->loader);
3903 _Jv_InitField (0, klass, i);
3904 }
3905 }
3906
3907 // Now we don't need the field_initializers anymore, so let the
3908 // collector get rid of it.
3909 iclass->field_initializers = 0;
3910 }
3911
3912 _Jv_ResolvedMethod *
3913 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3914 jboolean staticp, jint vtable_index)
3915 {
3916 int arg_count = _Jv_count_arguments (method->signature, staticp);
3917
3918 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3919 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3920 + arg_count*sizeof (ffi_type*));
3921
3922 result->stack_item_count
3923 = init_cif (method->signature,
3924 arg_count,
3925 staticp,
3926 &result->cif,
3927 &result->arg_types[0],
3928 NULL);
3929
3930 result->vtable_index = vtable_index;
3931 result->method = method;
3932 result->klass = klass;
3933
3934 return result;
3935 }
3936
3937 void
3938 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3939 {
3940 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3941 for (int i = 0; i < klass->method_count; i++)
3942 {
3943 // Just skip abstract methods. This is particularly important
3944 // because we don't resize the interpreted_methods array when
3945 // miranda methods are added to it.
3946 if ((klass->methods[i].accflags
3947 & java::lang::reflect::Modifier::ABSTRACT)
3948 != 0)
3949 continue;
3950 // Miranda method additions mean that the `methods' array moves.
3951 // We cache a pointer into this array, so we have to update.
3952 iclass->interpreted_methods[i]->self = &klass->methods[i];
3953 }
3954 }
3955
3956 #ifdef DIRECT_THREADED
3957 void
3958 _Jv_CompileMethod (_Jv_InterpMethod* method)
3959 {
3960 if (method->prepared == NULL)
3961 _Jv_InterpMethod::run (NULL, NULL, method);
3962 }
3963 #endif // DIRECT_THREADED
3964
3965 #endif // INTERPRETER