Merge from mainline
[official-gcc.git] / libjava / interpret.cc
blobddb469788f0c116051c9064c34bbce409dec8121
1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
13 #include <config.h>
14 #include <platform.h>
16 #pragma implementation "java-interp.h"
18 #include <jvm.h>
19 #include <java-cpool.h>
20 #include <java-interp.h>
21 #include <java/lang/System.h>
22 #include <java/lang/String.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/StringBuffer.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/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>
41 #ifdef INTERPRETER
43 // Execution engine for interpreted code.
44 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
46 #include <stdlib.h>
48 using namespace gcj;
50 static void throw_internal_error (const 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
59 static void throw_class_format_error (jstring msg)
60 __attribute__ ((__noreturn__));
61 static void throw_class_format_error (const char *msg)
62 __attribute__ ((__noreturn__));
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;
70 void
71 _Jv_InitInterpreter()
73 _Jv_MutexInit (&compile_mutex);
75 #else
76 void _Jv_InitInterpreter() {}
77 #endif
79 extern "C" double __ieee754_fmod (double,double);
81 static inline void dupx (_Jv_word *sp, int n, int x)
83 // first "slide" n+x elements n to the right
84 int top = n-1;
85 for (int i = 0; i < n+x; i++)
87 sp[(top-i)] = sp[(top-i)-n];
90 // next, copy the n top elements, n+x down
91 for (int i = 0; i < n; i++)
93 sp[top-(n+x)-i] = sp[top-i];
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)
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;
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
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
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
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
172 #define PEEKI(I) (locals+(I))->i
173 #define PEEKA(I) (locals+(I))->o
175 #define POKEI(I,V) ((locals+(I))->i = (V))
178 #define BINOPI(OP) { \
179 jint value2 = POPI(); \
180 jint value1 = POPI(); \
181 PUSHI(value1 OP value2); \
184 #define BINOPF(OP) { \
185 jfloat value2 = POPF(); \
186 jfloat value1 = POPF(); \
187 PUSHF(value1 OP value2); \
190 #define BINOPL(OP) { \
191 jlong value2 = POPL(); \
192 jlong value1 = POPL(); \
193 PUSHL(value1 OP value2); \
196 #define BINOPD(OP) { \
197 jdouble value2 = POPD(); \
198 jdouble value1 = POPD(); \
199 PUSHD(value1 OP value2); \
202 static inline jint get1s(unsigned char* loc) {
203 return *(signed char*)loc;
206 static inline jint get1u(unsigned char* loc) {
207 return *loc;
210 static inline jint get2s(unsigned char* loc) {
211 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
214 static inline jint get2u(unsigned char* loc) {
215 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
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);
225 #define SAVE_PC() frame_desc.pc = pc
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
237 #define ARRAYBOUNDSCHECK(array, index) \
238 do \
240 if (((unsigned) index) >= (unsigned) (array->length)) \
241 _Jv_ThrowBadArrayIndex (index); \
243 while (0)
245 void
246 _Jv_InterpMethod::run_normal (ffi_cif *,
247 void* ret,
248 ffi_raw * args,
249 void* __this)
251 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
252 run (ret, args, _this);
255 void
256 _Jv_InterpMethod::run_synch_object (ffi_cif *,
257 void* ret,
258 ffi_raw * args,
259 void* __this)
261 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
263 jobject rcv = (jobject) args[0].ptr;
264 JvSynchronize mutex (rcv);
266 run (ret, args, _this);
269 void
270 _Jv_InterpMethod::run_class (ffi_cif *,
271 void* ret,
272 ffi_raw * args,
273 void* __this)
275 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
276 _Jv_InitClass (_this->defining_class);
277 run (ret, args, _this);
280 void
281 _Jv_InterpMethod::run_synch_class (ffi_cif *,
282 void* ret,
283 ffi_raw * args,
284 void* __this)
286 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
288 jclass sync = _this->defining_class;
289 _Jv_InitClass (sync);
290 JvSynchronize mutex (sync);
292 run (ret, args, _this);
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)
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;
307 #define SET_ONE(Field, Value) \
308 do \
310 if (first_pass) \
311 ++next; \
312 else \
313 insns[next++].Field = Value; \
315 while (0)
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)
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;
326 for (int i = 0; i < 2; ++i)
328 jboolean first_pass = i == 0;
330 if (! first_pass)
332 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
333 number_insn_slots = next;
334 next = 0;
337 unsigned char *pc = codestart;
338 while (pc < end)
340 int base_pc_val = pc - codestart;
341 if (first_pass)
342 pc_mapping[base_pc_val] = next;
344 java_opcode opcode = (java_opcode) *pc++;
345 // Just elide NOPs.
346 if (opcode == op_nop)
347 continue;
348 SET_INSN (insn_targets[opcode]);
350 switch (opcode)
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;
502 case op_bipush:
503 SET_INT (get1s (pc));
504 ++pc;
505 break;
507 case op_ldc:
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)
515 --next;
516 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
517 SET_INT (index);
519 else
520 SET_DATUM (pool_data[index].o);
522 break;
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;
540 case op_iinc:
541 SET_INT (get1u (pc));
542 SET_INT (get1s (pc + 1));
543 pc += 2;
544 break;
546 case op_ldc_w:
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)
554 --next;
555 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
556 SET_INT (index);
558 else
559 SET_DATUM (pool_data[index].o);
561 break;
563 case op_ldc2_w:
565 int index = get2u (pc);
566 pc += 2;
567 SET_DATUM (&pool_data[index]);
569 break;
571 case op_sipush:
572 SET_INT (get2s (pc));
573 pc += 2;
574 break;
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;
591 case op_multianewarray:
592 SET_INT (get2u (pc));
593 SET_INT (get1u (pc + 2));
594 pc += 3;
595 break;
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:
616 int offset = get2s (pc);
617 pc += 2;
619 int new_pc = base_pc_val + offset;
621 bool orig_was_goto = opcode == op_goto;
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]);
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))
639 --next;
640 SET_INSN (insn_targets[opcode]);
642 else
643 SET_DATUM (&insns[pc_mapping[new_pc]]);
645 break;
647 case op_tableswitch:
649 while ((pc - codestart) % 4 != 0)
650 ++pc;
652 jint def = get4 (pc);
653 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
654 pc += 4;
656 int low = get4 (pc);
657 SET_INT (low);
658 pc += 4;
659 int high = get4 (pc);
660 SET_INT (high);
661 pc += 4;
663 for (int i = low; i <= high; ++i)
665 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
666 pc += 4;
669 break;
671 case op_lookupswitch:
673 while ((pc - codestart) % 4 != 0)
674 ++pc;
676 jint def = get4 (pc);
677 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
678 pc += 4;
680 jint npairs = get4 (pc);
681 pc += 4;
682 SET_INT (npairs);
684 while (npairs-- > 0)
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;
693 break;
695 case op_invokeinterface:
697 jint index = get2u (pc);
698 pc += 2;
699 // We ignore the next two bytes.
700 pc += 2;
701 SET_INT (index);
703 break;
705 case op_wide:
707 opcode = (java_opcode) get1u (pc);
708 pc += 1;
709 jint val = get2u (pc);
710 pc += 2;
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);
719 if (opcode == op_iinc)
721 SET_INT (get2s (pc));
722 pc += 2;
725 break;
727 case op_jsr_w:
728 case op_goto_w:
730 jint offset = get4 (pc);
731 pc += 4;
732 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
734 break;
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;
767 // Now update exceptions.
768 _Jv_InterpException *exc = exceptions ();
769 for (int i = 0; i < exc_count; ++i)
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;
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++)
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]];
792 prepared = insns;
794 #endif /* DIRECT_THREADED */
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)
801 using namespace java::lang::reflect;
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, thread);
812 _Jv_word stack[meth->max_stack];
813 _Jv_word *sp = stack;
815 _Jv_word locals[meth->max_locals];
817 #define INSN_LABEL(op) &&insn_##op
819 static const void *const insn_target[] =
821 INSN_LABEL(nop),
822 INSN_LABEL(aconst_null),
823 INSN_LABEL(iconst_m1),
824 INSN_LABEL(iconst_0),
825 INSN_LABEL(iconst_1),
826 INSN_LABEL(iconst_2),
827 INSN_LABEL(iconst_3),
828 INSN_LABEL(iconst_4),
829 INSN_LABEL(iconst_5),
830 INSN_LABEL(lconst_0),
831 INSN_LABEL(lconst_1),
832 INSN_LABEL(fconst_0),
833 INSN_LABEL(fconst_1),
834 INSN_LABEL(fconst_2),
835 INSN_LABEL(dconst_0),
836 INSN_LABEL(dconst_1),
837 INSN_LABEL(bipush),
838 INSN_LABEL(sipush),
839 INSN_LABEL(ldc),
840 INSN_LABEL(ldc_w),
841 INSN_LABEL(ldc2_w),
842 INSN_LABEL(iload),
843 INSN_LABEL(lload),
844 INSN_LABEL(fload),
845 INSN_LABEL(dload),
846 INSN_LABEL(aload),
847 INSN_LABEL(iload_0),
848 INSN_LABEL(iload_1),
849 INSN_LABEL(iload_2),
850 INSN_LABEL(iload_3),
851 INSN_LABEL(lload_0),
852 INSN_LABEL(lload_1),
853 INSN_LABEL(lload_2),
854 INSN_LABEL(lload_3),
855 INSN_LABEL(fload_0),
856 INSN_LABEL(fload_1),
857 INSN_LABEL(fload_2),
858 INSN_LABEL(fload_3),
859 INSN_LABEL(dload_0),
860 INSN_LABEL(dload_1),
861 INSN_LABEL(dload_2),
862 INSN_LABEL(dload_3),
863 INSN_LABEL(aload_0),
864 INSN_LABEL(aload_1),
865 INSN_LABEL(aload_2),
866 INSN_LABEL(aload_3),
867 INSN_LABEL(iaload),
868 INSN_LABEL(laload),
869 INSN_LABEL(faload),
870 INSN_LABEL(daload),
871 INSN_LABEL(aaload),
872 INSN_LABEL(baload),
873 INSN_LABEL(caload),
874 INSN_LABEL(saload),
875 INSN_LABEL(istore),
876 INSN_LABEL(lstore),
877 INSN_LABEL(fstore),
878 INSN_LABEL(dstore),
879 INSN_LABEL(astore),
880 INSN_LABEL(istore_0),
881 INSN_LABEL(istore_1),
882 INSN_LABEL(istore_2),
883 INSN_LABEL(istore_3),
884 INSN_LABEL(lstore_0),
885 INSN_LABEL(lstore_1),
886 INSN_LABEL(lstore_2),
887 INSN_LABEL(lstore_3),
888 INSN_LABEL(fstore_0),
889 INSN_LABEL(fstore_1),
890 INSN_LABEL(fstore_2),
891 INSN_LABEL(fstore_3),
892 INSN_LABEL(dstore_0),
893 INSN_LABEL(dstore_1),
894 INSN_LABEL(dstore_2),
895 INSN_LABEL(dstore_3),
896 INSN_LABEL(astore_0),
897 INSN_LABEL(astore_1),
898 INSN_LABEL(astore_2),
899 INSN_LABEL(astore_3),
900 INSN_LABEL(iastore),
901 INSN_LABEL(lastore),
902 INSN_LABEL(fastore),
903 INSN_LABEL(dastore),
904 INSN_LABEL(aastore),
905 INSN_LABEL(bastore),
906 INSN_LABEL(castore),
907 INSN_LABEL(sastore),
908 INSN_LABEL(pop),
909 INSN_LABEL(pop2),
910 INSN_LABEL(dup),
911 INSN_LABEL(dup_x1),
912 INSN_LABEL(dup_x2),
913 INSN_LABEL(dup2),
914 INSN_LABEL(dup2_x1),
915 INSN_LABEL(dup2_x2),
916 INSN_LABEL(swap),
917 INSN_LABEL(iadd),
918 INSN_LABEL(ladd),
919 INSN_LABEL(fadd),
920 INSN_LABEL(dadd),
921 INSN_LABEL(isub),
922 INSN_LABEL(lsub),
923 INSN_LABEL(fsub),
924 INSN_LABEL(dsub),
925 INSN_LABEL(imul),
926 INSN_LABEL(lmul),
927 INSN_LABEL(fmul),
928 INSN_LABEL(dmul),
929 INSN_LABEL(idiv),
930 INSN_LABEL(ldiv),
931 INSN_LABEL(fdiv),
932 INSN_LABEL(ddiv),
933 INSN_LABEL(irem),
934 INSN_LABEL(lrem),
935 INSN_LABEL(frem),
936 INSN_LABEL(drem),
937 INSN_LABEL(ineg),
938 INSN_LABEL(lneg),
939 INSN_LABEL(fneg),
940 INSN_LABEL(dneg),
941 INSN_LABEL(ishl),
942 INSN_LABEL(lshl),
943 INSN_LABEL(ishr),
944 INSN_LABEL(lshr),
945 INSN_LABEL(iushr),
946 INSN_LABEL(lushr),
947 INSN_LABEL(iand),
948 INSN_LABEL(land),
949 INSN_LABEL(ior),
950 INSN_LABEL(lor),
951 INSN_LABEL(ixor),
952 INSN_LABEL(lxor),
953 INSN_LABEL(iinc),
954 INSN_LABEL(i2l),
955 INSN_LABEL(i2f),
956 INSN_LABEL(i2d),
957 INSN_LABEL(l2i),
958 INSN_LABEL(l2f),
959 INSN_LABEL(l2d),
960 INSN_LABEL(f2i),
961 INSN_LABEL(f2l),
962 INSN_LABEL(f2d),
963 INSN_LABEL(d2i),
964 INSN_LABEL(d2l),
965 INSN_LABEL(d2f),
966 INSN_LABEL(i2b),
967 INSN_LABEL(i2c),
968 INSN_LABEL(i2s),
969 INSN_LABEL(lcmp),
970 INSN_LABEL(fcmpl),
971 INSN_LABEL(fcmpg),
972 INSN_LABEL(dcmpl),
973 INSN_LABEL(dcmpg),
974 INSN_LABEL(ifeq),
975 INSN_LABEL(ifne),
976 INSN_LABEL(iflt),
977 INSN_LABEL(ifge),
978 INSN_LABEL(ifgt),
979 INSN_LABEL(ifle),
980 INSN_LABEL(if_icmpeq),
981 INSN_LABEL(if_icmpne),
982 INSN_LABEL(if_icmplt),
983 INSN_LABEL(if_icmpge),
984 INSN_LABEL(if_icmpgt),
985 INSN_LABEL(if_icmple),
986 INSN_LABEL(if_acmpeq),
987 INSN_LABEL(if_acmpne),
988 INSN_LABEL(goto),
989 INSN_LABEL(jsr),
990 INSN_LABEL(ret),
991 INSN_LABEL(tableswitch),
992 INSN_LABEL(lookupswitch),
993 INSN_LABEL(ireturn),
994 INSN_LABEL(lreturn),
995 INSN_LABEL(freturn),
996 INSN_LABEL(dreturn),
997 INSN_LABEL(areturn),
998 INSN_LABEL(return),
999 INSN_LABEL(getstatic),
1000 INSN_LABEL(putstatic),
1001 INSN_LABEL(getfield),
1002 INSN_LABEL(putfield),
1003 INSN_LABEL(invokevirtual),
1004 INSN_LABEL(invokespecial),
1005 INSN_LABEL(invokestatic),
1006 INSN_LABEL(invokeinterface),
1007 0, /* Unused. */
1008 INSN_LABEL(new),
1009 INSN_LABEL(newarray),
1010 INSN_LABEL(anewarray),
1011 INSN_LABEL(arraylength),
1012 INSN_LABEL(athrow),
1013 INSN_LABEL(checkcast),
1014 INSN_LABEL(instanceof),
1015 INSN_LABEL(monitorenter),
1016 INSN_LABEL(monitorexit),
1017 #ifdef DIRECT_THREADED
1018 0, // wide
1019 #else
1020 INSN_LABEL(wide),
1021 #endif
1022 INSN_LABEL(multianewarray),
1023 INSN_LABEL(ifnull),
1024 INSN_LABEL(ifnonnull),
1025 INSN_LABEL(goto_w),
1026 INSN_LABEL(jsr_w),
1027 #ifdef DIRECT_THREADED
1028 INSN_LABEL (ldc_class)
1029 #else
1031 #endif
1034 pc_t pc;
1036 #ifdef DIRECT_THREADED
1038 #define NEXT_INSN goto *((pc++)->insn)
1039 #define INTVAL() ((pc++)->int_val)
1040 #define AVAL() ((pc++)->datum)
1042 #define GET1S() INTVAL ()
1043 #define GET2S() INTVAL ()
1044 #define GET1U() INTVAL ()
1045 #define GET2U() INTVAL ()
1046 #define AVAL1U() AVAL ()
1047 #define AVAL2U() AVAL ()
1048 #define AVAL2UP() AVAL ()
1049 #define SKIP_GOTO ++pc
1050 #define GOTO_VAL() (insn_slot *) pc->datum
1051 #define PCVAL(unionval) unionval.p
1052 #define AMPAMP(label) &&label
1054 // Compile if we must. NOTE: Double-check locking.
1055 if (meth->prepared == NULL)
1057 _Jv_MutexLock (&compile_mutex);
1058 if (meth->prepared == NULL)
1059 meth->compile (insn_target);
1060 _Jv_MutexUnlock (&compile_mutex);
1063 // If we're only compiling, stop here
1064 if (args == NULL)
1065 return;
1067 pc = (insn_slot *) meth->prepared;
1069 #else
1071 #define NEXT_INSN goto *(insn_target[*pc++])
1073 #define GET1S() get1s (pc++)
1074 #define GET2S() (pc += 2, get2s (pc- 2))
1075 #define GET1U() get1u (pc++)
1076 #define GET2U() (pc += 2, get2u (pc - 2))
1077 // Note that these could be more efficient when not handling 'ldc
1078 // class'.
1079 #define AVAL1U() \
1080 ({ int index = get1u (pc++); \
1081 resolve_pool_entry (meth->defining_class, index).o; })
1082 #define AVAL2U() \
1083 ({ int index = get2u (pc); pc += 2; \
1084 resolve_pool_entry (meth->defining_class, index).o; })
1085 // Note that we don't need to resolve the pool entry here as class
1086 // constants are never wide.
1087 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1088 #define SKIP_GOTO pc += 2
1089 #define GOTO_VAL() pc - 1 + get2s (pc)
1090 #define PCVAL(unionval) unionval.i
1091 #define AMPAMP(label) NULL
1093 pc = bytecode ();
1095 #endif /* DIRECT_THREADED */
1097 #define TAKE_GOTO pc = GOTO_VAL ()
1099 /* Go straight at it! the ffi raw format matches the internal
1100 stack representation exactly. At least, that's the idea.
1102 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1104 _Jv_word *pool_data = meth->defining_class->constants.data;
1106 /* These three are temporaries for common code used by several
1107 instructions. */
1108 void (*fun)();
1109 _Jv_ResolvedMethod* rmeth;
1110 int tmpval;
1114 // We keep nop around. It is used if we're interpreting the
1115 // bytecodes and not doing direct threading.
1116 insn_nop:
1117 NEXT_INSN;
1119 /* The first few instructions here are ordered according to their
1120 frequency, in the hope that this will improve code locality a
1121 little. */
1123 insn_aload_0: // 0x2a
1124 LOADA (0);
1125 NEXT_INSN;
1127 insn_iload: // 0x15
1128 LOADI (GET1U ());
1129 NEXT_INSN;
1131 insn_iload_1: // 0x1b
1132 LOADI (1);
1133 NEXT_INSN;
1135 insn_invokevirtual: // 0xb6
1137 int index = GET2U ();
1139 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1140 * value already is resolved. If we want to clutter up the
1141 * code here to gain a little performance, then we can check
1142 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1143 * directly. For now, I don't think it is worth it. */
1145 SAVE_PC();
1146 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1147 index)).rmethod;
1149 sp -= rmeth->stack_item_count;
1150 // We don't use NULLCHECK here because we can't rely on that
1151 // working if the method is final. So instead we do an
1152 // explicit test.
1153 if (! sp[0].o)
1155 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1156 throw new java::lang::NullPointerException;
1159 if (rmeth->vtable_index == -1)
1161 // final methods do not appear in the vtable,
1162 // if it does not appear in the superclass.
1163 fun = (void (*)()) rmeth->method->ncode;
1165 else
1167 jobject rcv = sp[0].o;
1168 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1169 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1172 #ifdef DIRECT_THREADED
1173 // Rewrite instruction so that we use a faster pre-resolved
1174 // method.
1175 pc[-2].insn = &&invokevirtual_resolved;
1176 pc[-1].datum = rmeth;
1177 #endif /* DIRECT_THREADED */
1179 goto perform_invoke;
1181 #ifdef DIRECT_THREADED
1182 invokevirtual_resolved:
1184 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1185 sp -= rmeth->stack_item_count;
1186 // We don't use NULLCHECK here because we can't rely on that
1187 // working if the method is final. So instead we do an
1188 // explicit test.
1189 if (! sp[0].o)
1191 SAVE_PC();
1192 throw new java::lang::NullPointerException;
1195 if (rmeth->vtable_index == -1)
1197 // final methods do not appear in the vtable,
1198 // if it does not appear in the superclass.
1199 fun = (void (*)()) rmeth->method->ncode;
1201 else
1203 jobject rcv = sp[0].o;
1204 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1205 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1208 goto perform_invoke;
1209 #endif /* DIRECT_THREADED */
1211 perform_invoke:
1213 SAVE_PC();
1215 /* here goes the magic again... */
1216 ffi_cif *cif = &rmeth->cif;
1217 ffi_raw *raw = (ffi_raw*) sp;
1219 _Jv_value rvalue;
1221 #if FFI_NATIVE_RAW_API
1222 /* We assume that this is only implemented if it's correct */
1223 /* to use it here. On a 64 bit machine, it never is. */
1224 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1225 #else
1226 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1227 #endif
1229 int rtype = cif->rtype->type;
1231 /* the likelyhood of object, int, or void return is very high,
1232 * so those are checked before the switch */
1233 if (rtype == FFI_TYPE_POINTER)
1235 PUSHA (rvalue.object_value);
1237 else if (rtype == FFI_TYPE_SINT32)
1239 PUSHI (rvalue.int_value);
1241 else if (rtype == FFI_TYPE_VOID)
1243 /* skip */
1245 else
1247 switch (rtype)
1249 case FFI_TYPE_SINT8:
1250 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1251 break;
1253 case FFI_TYPE_SINT16:
1254 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1255 break;
1257 case FFI_TYPE_UINT16:
1258 PUSHI (rvalue.int_value & 0xffff);
1259 break;
1261 case FFI_TYPE_FLOAT:
1262 PUSHF (rvalue.float_value);
1263 break;
1265 case FFI_TYPE_DOUBLE:
1266 PUSHD (rvalue.double_value);
1267 break;
1269 case FFI_TYPE_SINT64:
1270 PUSHL (rvalue.long_value);
1271 break;
1273 default:
1274 throw_internal_error ("unknown return type in invokeXXX");
1278 NEXT_INSN;
1280 insn_aconst_null:
1281 PUSHA (NULL);
1282 NEXT_INSN;
1284 insn_iconst_m1:
1285 PUSHI (-1);
1286 NEXT_INSN;
1288 insn_iconst_0:
1289 PUSHI (0);
1290 NEXT_INSN;
1292 insn_iconst_1:
1293 PUSHI (1);
1294 NEXT_INSN;
1296 insn_iconst_2:
1297 PUSHI (2);
1298 NEXT_INSN;
1300 insn_iconst_3:
1301 PUSHI (3);
1302 NEXT_INSN;
1304 insn_iconst_4:
1305 PUSHI (4);
1306 NEXT_INSN;
1308 insn_iconst_5:
1309 PUSHI (5);
1310 NEXT_INSN;
1312 insn_lconst_0:
1313 PUSHL (0);
1314 NEXT_INSN;
1316 insn_lconst_1:
1317 PUSHL (1);
1318 NEXT_INSN;
1320 insn_fconst_0:
1321 PUSHF (0);
1322 NEXT_INSN;
1324 insn_fconst_1:
1325 PUSHF (1);
1326 NEXT_INSN;
1328 insn_fconst_2:
1329 PUSHF (2);
1330 NEXT_INSN;
1332 insn_dconst_0:
1333 PUSHD (0);
1334 NEXT_INSN;
1336 insn_dconst_1:
1337 PUSHD (1);
1338 NEXT_INSN;
1340 insn_bipush:
1341 // For direct threaded, bipush and sipush are the same.
1342 #ifndef DIRECT_THREADED
1343 PUSHI (GET1S ());
1344 NEXT_INSN;
1345 #endif /* DIRECT_THREADED */
1346 insn_sipush:
1347 PUSHI (GET2S ());
1348 NEXT_INSN;
1350 insn_ldc:
1351 // For direct threaded, ldc and ldc_w are the same.
1352 #ifndef DIRECT_THREADED
1353 PUSHA ((jobject) AVAL1U ());
1354 NEXT_INSN;
1355 #endif /* DIRECT_THREADED */
1356 insn_ldc_w:
1357 PUSHA ((jobject) AVAL2U ());
1358 NEXT_INSN;
1360 #ifdef DIRECT_THREADED
1361 // For direct threaded we have a separate 'ldc class' operation.
1362 insn_ldc_class:
1364 // We could rewrite the instruction at this point.
1365 int index = INTVAL ();
1366 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1367 index)).o;
1368 PUSHA (k);
1370 NEXT_INSN;
1371 #endif /* DIRECT_THREADED */
1373 insn_ldc2_w:
1375 void *where = AVAL2UP ();
1376 memcpy (sp, where, 2*sizeof (_Jv_word));
1377 sp += 2;
1379 NEXT_INSN;
1381 insn_lload:
1382 LOADL (GET1U ());
1383 NEXT_INSN;
1385 insn_fload:
1386 LOADF (GET1U ());
1387 NEXT_INSN;
1389 insn_dload:
1390 LOADD (GET1U ());
1391 NEXT_INSN;
1393 insn_aload:
1394 LOADA (GET1U ());
1395 NEXT_INSN;
1397 insn_iload_0:
1398 LOADI (0);
1399 NEXT_INSN;
1401 insn_iload_2:
1402 LOADI (2);
1403 NEXT_INSN;
1405 insn_iload_3:
1406 LOADI (3);
1407 NEXT_INSN;
1409 insn_lload_0:
1410 LOADL (0);
1411 NEXT_INSN;
1413 insn_lload_1:
1414 LOADL (1);
1415 NEXT_INSN;
1417 insn_lload_2:
1418 LOADL (2);
1419 NEXT_INSN;
1421 insn_lload_3:
1422 LOADL (3);
1423 NEXT_INSN;
1425 insn_fload_0:
1426 LOADF (0);
1427 NEXT_INSN;
1429 insn_fload_1:
1430 LOADF (1);
1431 NEXT_INSN;
1433 insn_fload_2:
1434 LOADF (2);
1435 NEXT_INSN;
1437 insn_fload_3:
1438 LOADF (3);
1439 NEXT_INSN;
1441 insn_dload_0:
1442 LOADD (0);
1443 NEXT_INSN;
1445 insn_dload_1:
1446 LOADD (1);
1447 NEXT_INSN;
1449 insn_dload_2:
1450 LOADD (2);
1451 NEXT_INSN;
1453 insn_dload_3:
1454 LOADD (3);
1455 NEXT_INSN;
1457 insn_aload_1:
1458 LOADA(1);
1459 NEXT_INSN;
1461 insn_aload_2:
1462 LOADA(2);
1463 NEXT_INSN;
1465 insn_aload_3:
1466 LOADA(3);
1467 NEXT_INSN;
1469 insn_iaload:
1471 jint index = POPI();
1472 jintArray arr = (jintArray) POPA();
1473 NULLARRAYCHECK (arr);
1474 ARRAYBOUNDSCHECK (arr, index);
1475 PUSHI( elements(arr)[index] );
1477 NEXT_INSN;
1479 insn_laload:
1481 jint index = POPI();
1482 jlongArray arr = (jlongArray) POPA();
1483 NULLARRAYCHECK (arr);
1484 ARRAYBOUNDSCHECK (arr, index);
1485 PUSHL( elements(arr)[index] );
1487 NEXT_INSN;
1489 insn_faload:
1491 jint index = POPI();
1492 jfloatArray arr = (jfloatArray) POPA();
1493 NULLARRAYCHECK (arr);
1494 ARRAYBOUNDSCHECK (arr, index);
1495 PUSHF( elements(arr)[index] );
1497 NEXT_INSN;
1499 insn_daload:
1501 jint index = POPI();
1502 jdoubleArray arr = (jdoubleArray) POPA();
1503 NULLARRAYCHECK (arr);
1504 ARRAYBOUNDSCHECK (arr, index);
1505 PUSHD( elements(arr)[index] );
1507 NEXT_INSN;
1509 insn_aaload:
1511 jint index = POPI();
1512 jobjectArray arr = (jobjectArray) POPA();
1513 NULLARRAYCHECK (arr);
1514 ARRAYBOUNDSCHECK (arr, index);
1515 PUSHA( elements(arr)[index] );
1517 NEXT_INSN;
1519 insn_baload:
1521 jint index = POPI();
1522 jbyteArray arr = (jbyteArray) POPA();
1523 NULLARRAYCHECK (arr);
1524 ARRAYBOUNDSCHECK (arr, index);
1525 PUSHI( elements(arr)[index] );
1527 NEXT_INSN;
1529 insn_caload:
1531 jint index = POPI();
1532 jcharArray arr = (jcharArray) POPA();
1533 NULLARRAYCHECK (arr);
1534 ARRAYBOUNDSCHECK (arr, index);
1535 PUSHI( elements(arr)[index] );
1537 NEXT_INSN;
1539 insn_saload:
1541 jint index = POPI();
1542 jshortArray arr = (jshortArray) POPA();
1543 NULLARRAYCHECK (arr);
1544 ARRAYBOUNDSCHECK (arr, index);
1545 PUSHI( elements(arr)[index] );
1547 NEXT_INSN;
1549 insn_istore:
1550 STOREI (GET1U ());
1551 NEXT_INSN;
1553 insn_lstore:
1554 STOREL (GET1U ());
1555 NEXT_INSN;
1557 insn_fstore:
1558 STOREF (GET1U ());
1559 NEXT_INSN;
1561 insn_dstore:
1562 STORED (GET1U ());
1563 NEXT_INSN;
1565 insn_astore:
1566 STOREA (GET1U ());
1567 NEXT_INSN;
1569 insn_istore_0:
1570 STOREI (0);
1571 NEXT_INSN;
1573 insn_istore_1:
1574 STOREI (1);
1575 NEXT_INSN;
1577 insn_istore_2:
1578 STOREI (2);
1579 NEXT_INSN;
1581 insn_istore_3:
1582 STOREI (3);
1583 NEXT_INSN;
1585 insn_lstore_0:
1586 STOREL (0);
1587 NEXT_INSN;
1589 insn_lstore_1:
1590 STOREL (1);
1591 NEXT_INSN;
1593 insn_lstore_2:
1594 STOREL (2);
1595 NEXT_INSN;
1597 insn_lstore_3:
1598 STOREL (3);
1599 NEXT_INSN;
1601 insn_fstore_0:
1602 STOREF (0);
1603 NEXT_INSN;
1605 insn_fstore_1:
1606 STOREF (1);
1607 NEXT_INSN;
1609 insn_fstore_2:
1610 STOREF (2);
1611 NEXT_INSN;
1613 insn_fstore_3:
1614 STOREF (3);
1615 NEXT_INSN;
1617 insn_dstore_0:
1618 STORED (0);
1619 NEXT_INSN;
1621 insn_dstore_1:
1622 STORED (1);
1623 NEXT_INSN;
1625 insn_dstore_2:
1626 STORED (2);
1627 NEXT_INSN;
1629 insn_dstore_3:
1630 STORED (3);
1631 NEXT_INSN;
1633 insn_astore_0:
1634 STOREA(0);
1635 NEXT_INSN;
1637 insn_astore_1:
1638 STOREA(1);
1639 NEXT_INSN;
1641 insn_astore_2:
1642 STOREA(2);
1643 NEXT_INSN;
1645 insn_astore_3:
1646 STOREA(3);
1647 NEXT_INSN;
1649 insn_iastore:
1651 jint value = POPI();
1652 jint index = POPI();
1653 jintArray arr = (jintArray) POPA();
1654 NULLARRAYCHECK (arr);
1655 ARRAYBOUNDSCHECK (arr, index);
1656 elements(arr)[index] = value;
1658 NEXT_INSN;
1660 insn_lastore:
1662 jlong value = POPL();
1663 jint index = POPI();
1664 jlongArray arr = (jlongArray) POPA();
1665 NULLARRAYCHECK (arr);
1666 ARRAYBOUNDSCHECK (arr, index);
1667 elements(arr)[index] = value;
1669 NEXT_INSN;
1671 insn_fastore:
1673 jfloat value = POPF();
1674 jint index = POPI();
1675 jfloatArray arr = (jfloatArray) POPA();
1676 NULLARRAYCHECK (arr);
1677 ARRAYBOUNDSCHECK (arr, index);
1678 elements(arr)[index] = value;
1680 NEXT_INSN;
1682 insn_dastore:
1684 jdouble value = POPD();
1685 jint index = POPI();
1686 jdoubleArray arr = (jdoubleArray) POPA();
1687 NULLARRAYCHECK (arr);
1688 ARRAYBOUNDSCHECK (arr, index);
1689 elements(arr)[index] = value;
1691 NEXT_INSN;
1693 insn_aastore:
1695 jobject value = POPA();
1696 jint index = POPI();
1697 jobjectArray arr = (jobjectArray) POPA();
1698 NULLARRAYCHECK (arr);
1699 ARRAYBOUNDSCHECK (arr, index);
1700 _Jv_CheckArrayStore (arr, value);
1701 elements(arr)[index] = value;
1703 NEXT_INSN;
1705 insn_bastore:
1707 jbyte value = (jbyte) POPI();
1708 jint index = POPI();
1709 jbyteArray arr = (jbyteArray) POPA();
1710 NULLARRAYCHECK (arr);
1711 ARRAYBOUNDSCHECK (arr, index);
1712 elements(arr)[index] = value;
1714 NEXT_INSN;
1716 insn_castore:
1718 jchar value = (jchar) POPI();
1719 jint index = POPI();
1720 jcharArray arr = (jcharArray) POPA();
1721 NULLARRAYCHECK (arr);
1722 ARRAYBOUNDSCHECK (arr, index);
1723 elements(arr)[index] = value;
1725 NEXT_INSN;
1727 insn_sastore:
1729 jshort value = (jshort) POPI();
1730 jint index = POPI();
1731 jshortArray arr = (jshortArray) POPA();
1732 NULLARRAYCHECK (arr);
1733 ARRAYBOUNDSCHECK (arr, index);
1734 elements(arr)[index] = value;
1736 NEXT_INSN;
1738 insn_pop:
1739 sp -= 1;
1740 NEXT_INSN;
1742 insn_pop2:
1743 sp -= 2;
1744 NEXT_INSN;
1746 insn_dup:
1747 sp[0] = sp[-1];
1748 sp += 1;
1749 NEXT_INSN;
1751 insn_dup_x1:
1752 dupx (sp, 1, 1); sp+=1;
1753 NEXT_INSN;
1755 insn_dup_x2:
1756 dupx (sp, 1, 2); sp+=1;
1757 NEXT_INSN;
1759 insn_dup2:
1760 sp[0] = sp[-2];
1761 sp[1] = sp[-1];
1762 sp += 2;
1763 NEXT_INSN;
1765 insn_dup2_x1:
1766 dupx (sp, 2, 1); sp+=2;
1767 NEXT_INSN;
1769 insn_dup2_x2:
1770 dupx (sp, 2, 2); sp+=2;
1771 NEXT_INSN;
1773 insn_swap:
1775 jobject tmp1 = POPA();
1776 jobject tmp2 = POPA();
1777 PUSHA (tmp1);
1778 PUSHA (tmp2);
1780 NEXT_INSN;
1782 insn_iadd:
1783 BINOPI(+);
1784 NEXT_INSN;
1786 insn_ladd:
1787 BINOPL(+);
1788 NEXT_INSN;
1790 insn_fadd:
1791 BINOPF(+);
1792 NEXT_INSN;
1794 insn_dadd:
1795 BINOPD(+);
1796 NEXT_INSN;
1798 insn_isub:
1799 BINOPI(-);
1800 NEXT_INSN;
1802 insn_lsub:
1803 BINOPL(-);
1804 NEXT_INSN;
1806 insn_fsub:
1807 BINOPF(-);
1808 NEXT_INSN;
1810 insn_dsub:
1811 BINOPD(-);
1812 NEXT_INSN;
1814 insn_imul:
1815 BINOPI(*);
1816 NEXT_INSN;
1818 insn_lmul:
1819 BINOPL(*);
1820 NEXT_INSN;
1822 insn_fmul:
1823 BINOPF(*);
1824 NEXT_INSN;
1826 insn_dmul:
1827 BINOPD(*);
1828 NEXT_INSN;
1830 insn_idiv:
1832 jint value2 = POPI();
1833 jint value1 = POPI();
1834 jint res = _Jv_divI (value1, value2);
1835 PUSHI (res);
1837 NEXT_INSN;
1839 insn_ldiv:
1841 jlong value2 = POPL();
1842 jlong value1 = POPL();
1843 jlong res = _Jv_divJ (value1, value2);
1844 PUSHL (res);
1846 NEXT_INSN;
1848 insn_fdiv:
1850 jfloat value2 = POPF();
1851 jfloat value1 = POPF();
1852 jfloat res = value1 / value2;
1853 PUSHF (res);
1855 NEXT_INSN;
1857 insn_ddiv:
1859 jdouble value2 = POPD();
1860 jdouble value1 = POPD();
1861 jdouble res = value1 / value2;
1862 PUSHD (res);
1864 NEXT_INSN;
1866 insn_irem:
1868 jint value2 = POPI();
1869 jint value1 = POPI();
1870 jint res = _Jv_remI (value1, value2);
1871 PUSHI (res);
1873 NEXT_INSN;
1875 insn_lrem:
1877 jlong value2 = POPL();
1878 jlong value1 = POPL();
1879 jlong res = _Jv_remJ (value1, value2);
1880 PUSHL (res);
1882 NEXT_INSN;
1884 insn_frem:
1886 jfloat value2 = POPF();
1887 jfloat value1 = POPF();
1888 jfloat res = __ieee754_fmod (value1, value2);
1889 PUSHF (res);
1891 NEXT_INSN;
1893 insn_drem:
1895 jdouble value2 = POPD();
1896 jdouble value1 = POPD();
1897 jdouble res = __ieee754_fmod (value1, value2);
1898 PUSHD (res);
1900 NEXT_INSN;
1902 insn_ineg:
1904 jint value = POPI();
1905 PUSHI (value * -1);
1907 NEXT_INSN;
1909 insn_lneg:
1911 jlong value = POPL();
1912 PUSHL (value * -1);
1914 NEXT_INSN;
1916 insn_fneg:
1918 jfloat value = POPF();
1919 PUSHF (value * -1);
1921 NEXT_INSN;
1923 insn_dneg:
1925 jdouble value = POPD();
1926 PUSHD (value * -1);
1928 NEXT_INSN;
1930 insn_ishl:
1932 jint shift = (POPI() & 0x1f);
1933 jint value = POPI();
1934 PUSHI (value << shift);
1936 NEXT_INSN;
1938 insn_lshl:
1940 jint shift = (POPI() & 0x3f);
1941 jlong value = POPL();
1942 PUSHL (value << shift);
1944 NEXT_INSN;
1946 insn_ishr:
1948 jint shift = (POPI() & 0x1f);
1949 jint value = POPI();
1950 PUSHI (value >> shift);
1952 NEXT_INSN;
1954 insn_lshr:
1956 jint shift = (POPI() & 0x3f);
1957 jlong value = POPL();
1958 PUSHL (value >> shift);
1960 NEXT_INSN;
1962 insn_iushr:
1964 jint shift = (POPI() & 0x1f);
1965 _Jv_uint value = (_Jv_uint) POPI();
1966 PUSHI ((jint) (value >> shift));
1968 NEXT_INSN;
1970 insn_lushr:
1972 jint shift = (POPI() & 0x3f);
1973 _Jv_ulong value = (_Jv_ulong) POPL();
1974 PUSHL ((jlong) (value >> shift));
1976 NEXT_INSN;
1978 insn_iand:
1979 BINOPI (&);
1980 NEXT_INSN;
1982 insn_land:
1983 BINOPL (&);
1984 NEXT_INSN;
1986 insn_ior:
1987 BINOPI (|);
1988 NEXT_INSN;
1990 insn_lor:
1991 BINOPL (|);
1992 NEXT_INSN;
1994 insn_ixor:
1995 BINOPI (^);
1996 NEXT_INSN;
1998 insn_lxor:
1999 BINOPL (^);
2000 NEXT_INSN;
2002 insn_iinc:
2004 jint index = GET1U ();
2005 jint amount = GET1S ();
2006 locals[index].i += amount;
2008 NEXT_INSN;
2010 insn_i2l:
2011 {jlong value = POPI(); PUSHL (value);}
2012 NEXT_INSN;
2014 insn_i2f:
2015 {jfloat value = POPI(); PUSHF (value);}
2016 NEXT_INSN;
2018 insn_i2d:
2019 {jdouble value = POPI(); PUSHD (value);}
2020 NEXT_INSN;
2022 insn_l2i:
2023 {jint value = POPL(); PUSHI (value);}
2024 NEXT_INSN;
2026 insn_l2f:
2027 {jfloat value = POPL(); PUSHF (value);}
2028 NEXT_INSN;
2030 insn_l2d:
2031 {jdouble value = POPL(); PUSHD (value);}
2032 NEXT_INSN;
2034 insn_f2i:
2036 using namespace java::lang;
2037 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2038 PUSHI(value);
2040 NEXT_INSN;
2042 insn_f2l:
2044 using namespace java::lang;
2045 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2046 PUSHL(value);
2048 NEXT_INSN;
2050 insn_f2d:
2051 { jdouble value = POPF (); PUSHD(value); }
2052 NEXT_INSN;
2054 insn_d2i:
2056 using namespace java::lang;
2057 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2058 PUSHI(value);
2060 NEXT_INSN;
2062 insn_d2l:
2064 using namespace java::lang;
2065 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2066 PUSHL(value);
2068 NEXT_INSN;
2070 insn_d2f:
2071 { jfloat value = POPD (); PUSHF(value); }
2072 NEXT_INSN;
2074 insn_i2b:
2075 { jbyte value = POPI (); PUSHI(value); }
2076 NEXT_INSN;
2078 insn_i2c:
2079 { jchar value = POPI (); PUSHI(value); }
2080 NEXT_INSN;
2082 insn_i2s:
2083 { jshort value = POPI (); PUSHI(value); }
2084 NEXT_INSN;
2086 insn_lcmp:
2088 jlong value2 = POPL ();
2089 jlong value1 = POPL ();
2090 if (value1 > value2)
2091 { PUSHI (1); }
2092 else if (value1 == value2)
2093 { PUSHI (0); }
2094 else
2095 { PUSHI (-1); }
2097 NEXT_INSN;
2099 insn_fcmpl:
2100 tmpval = -1;
2101 goto fcmp;
2103 insn_fcmpg:
2104 tmpval = 1;
2106 fcmp:
2108 jfloat value2 = POPF ();
2109 jfloat value1 = POPF ();
2110 if (value1 > value2)
2111 PUSHI (1);
2112 else if (value1 == value2)
2113 PUSHI (0);
2114 else if (value1 < value2)
2115 PUSHI (-1);
2116 else
2117 PUSHI (tmpval);
2119 NEXT_INSN;
2121 insn_dcmpl:
2122 tmpval = -1;
2123 goto dcmp;
2125 insn_dcmpg:
2126 tmpval = 1;
2128 dcmp:
2130 jdouble value2 = POPD ();
2131 jdouble value1 = POPD ();
2132 if (value1 > value2)
2133 PUSHI (1);
2134 else if (value1 == value2)
2135 PUSHI (0);
2136 else if (value1 < value2)
2137 PUSHI (-1);
2138 else
2139 PUSHI (tmpval);
2141 NEXT_INSN;
2143 insn_ifeq:
2145 if (POPI() == 0)
2146 TAKE_GOTO;
2147 else
2148 SKIP_GOTO;
2150 NEXT_INSN;
2152 insn_ifne:
2154 if (POPI() != 0)
2155 TAKE_GOTO;
2156 else
2157 SKIP_GOTO;
2159 NEXT_INSN;
2161 insn_iflt:
2163 if (POPI() < 0)
2164 TAKE_GOTO;
2165 else
2166 SKIP_GOTO;
2168 NEXT_INSN;
2170 insn_ifge:
2172 if (POPI() >= 0)
2173 TAKE_GOTO;
2174 else
2175 SKIP_GOTO;
2177 NEXT_INSN;
2179 insn_ifgt:
2181 if (POPI() > 0)
2182 TAKE_GOTO;
2183 else
2184 SKIP_GOTO;
2186 NEXT_INSN;
2188 insn_ifle:
2190 if (POPI() <= 0)
2191 TAKE_GOTO;
2192 else
2193 SKIP_GOTO;
2195 NEXT_INSN;
2197 insn_if_icmpeq:
2199 jint value2 = POPI();
2200 jint value1 = POPI();
2201 if (value1 == value2)
2202 TAKE_GOTO;
2203 else
2204 SKIP_GOTO;
2206 NEXT_INSN;
2208 insn_if_icmpne:
2210 jint value2 = POPI();
2211 jint value1 = POPI();
2212 if (value1 != value2)
2213 TAKE_GOTO;
2214 else
2215 SKIP_GOTO;
2217 NEXT_INSN;
2219 insn_if_icmplt:
2221 jint value2 = POPI();
2222 jint value1 = POPI();
2223 if (value1 < value2)
2224 TAKE_GOTO;
2225 else
2226 SKIP_GOTO;
2228 NEXT_INSN;
2230 insn_if_icmpge:
2232 jint value2 = POPI();
2233 jint value1 = POPI();
2234 if (value1 >= value2)
2235 TAKE_GOTO;
2236 else
2237 SKIP_GOTO;
2239 NEXT_INSN;
2241 insn_if_icmpgt:
2243 jint value2 = POPI();
2244 jint value1 = POPI();
2245 if (value1 > value2)
2246 TAKE_GOTO;
2247 else
2248 SKIP_GOTO;
2250 NEXT_INSN;
2252 insn_if_icmple:
2254 jint value2 = POPI();
2255 jint value1 = POPI();
2256 if (value1 <= value2)
2257 TAKE_GOTO;
2258 else
2259 SKIP_GOTO;
2261 NEXT_INSN;
2263 insn_if_acmpeq:
2265 jobject value2 = POPA();
2266 jobject value1 = POPA();
2267 if (value1 == value2)
2268 TAKE_GOTO;
2269 else
2270 SKIP_GOTO;
2272 NEXT_INSN;
2274 insn_if_acmpne:
2276 jobject value2 = POPA();
2277 jobject value1 = POPA();
2278 if (value1 != value2)
2279 TAKE_GOTO;
2280 else
2281 SKIP_GOTO;
2283 NEXT_INSN;
2285 insn_goto_w:
2286 #ifndef DIRECT_THREADED
2287 // For direct threaded, goto and goto_w are the same.
2288 pc = pc - 1 + get4 (pc);
2289 NEXT_INSN;
2290 #endif /* DIRECT_THREADED */
2291 insn_goto:
2292 TAKE_GOTO;
2293 NEXT_INSN;
2295 insn_jsr_w:
2296 #ifndef DIRECT_THREADED
2297 // For direct threaded, jsr and jsr_w are the same.
2299 pc_t next = pc - 1 + get4 (pc);
2300 pc += 4;
2301 PUSHA ((jobject) pc);
2302 pc = next;
2304 NEXT_INSN;
2305 #endif /* DIRECT_THREADED */
2306 insn_jsr:
2308 pc_t next = GOTO_VAL();
2309 SKIP_GOTO;
2310 PUSHA ((jobject) pc);
2311 pc = next;
2313 NEXT_INSN;
2315 insn_ret:
2317 jint index = GET1U ();
2318 pc = (pc_t) PEEKA (index);
2320 NEXT_INSN;
2322 insn_tableswitch:
2324 #ifdef DIRECT_THREADED
2325 void *def = (pc++)->datum;
2327 int index = POPI();
2329 jint low = INTVAL ();
2330 jint high = INTVAL ();
2332 if (index < low || index > high)
2333 pc = (insn_slot *) def;
2334 else
2335 pc = (insn_slot *) ((pc + index - low)->datum);
2336 #else
2337 pc_t base_pc = pc - 1;
2338 int index = POPI ();
2340 pc_t base = (pc_t) bytecode ();
2341 while ((pc - base) % 4 != 0)
2342 ++pc;
2344 jint def = get4 (pc);
2345 jint low = get4 (pc + 4);
2346 jint high = get4 (pc + 8);
2347 if (index < low || index > high)
2348 pc = base_pc + def;
2349 else
2350 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2351 #endif /* DIRECT_THREADED */
2353 NEXT_INSN;
2355 insn_lookupswitch:
2357 #ifdef DIRECT_THREADED
2358 void *def = (pc++)->insn;
2360 int index = POPI();
2362 jint npairs = INTVAL ();
2364 int max = npairs - 1;
2365 int min = 0;
2367 // Simple binary search...
2368 while (min < max)
2370 int half = (min + max) / 2;
2371 int match = pc[2 * half].int_val;
2373 if (index == match)
2375 // Found it.
2376 pc = (insn_slot *) pc[2 * half + 1].datum;
2377 NEXT_INSN;
2379 else if (index < match)
2380 // We can use HALF - 1 here because we check again on
2381 // loop exit.
2382 max = half - 1;
2383 else
2384 // We can use HALF + 1 here because we check again on
2385 // loop exit.
2386 min = half + 1;
2388 if (index == pc[2 * min].int_val)
2389 pc = (insn_slot *) pc[2 * min + 1].datum;
2390 else
2391 pc = (insn_slot *) def;
2392 #else
2393 unsigned char *base_pc = pc-1;
2394 int index = POPI();
2396 unsigned char* base = bytecode ();
2397 while ((pc-base) % 4 != 0)
2398 ++pc;
2400 jint def = get4 (pc);
2401 jint npairs = get4 (pc+4);
2403 int max = npairs-1;
2404 int min = 0;
2406 // Simple binary search...
2407 while (min < max)
2409 int half = (min+max)/2;
2410 int match = get4 (pc+ 4*(2 + 2*half));
2412 if (index == match)
2413 min = max = half;
2414 else if (index < match)
2415 // We can use HALF - 1 here because we check again on
2416 // loop exit.
2417 max = half - 1;
2418 else
2419 // We can use HALF + 1 here because we check again on
2420 // loop exit.
2421 min = half + 1;
2424 if (index == get4 (pc+ 4*(2 + 2*min)))
2425 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2426 else
2427 pc = base_pc + def;
2428 #endif /* DIRECT_THREADED */
2430 NEXT_INSN;
2432 insn_areturn:
2433 *(jobject *) retp = POPA ();
2434 return;
2436 insn_lreturn:
2437 *(jlong *) retp = POPL ();
2438 return;
2440 insn_freturn:
2441 *(jfloat *) retp = POPF ();
2442 return;
2444 insn_dreturn:
2445 *(jdouble *) retp = POPD ();
2446 return;
2448 insn_ireturn:
2449 *(jint *) retp = POPI ();
2450 return;
2452 insn_return:
2453 return;
2455 insn_getstatic:
2457 jint fieldref_index = GET2U ();
2458 SAVE_PC(); // Constant pool resolution could throw.
2459 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2460 _Jv_Field *field = pool_data[fieldref_index].field;
2462 if ((field->flags & Modifier::STATIC) == 0)
2463 throw_incompatible_class_change_error
2464 (JvNewStringLatin1 ("field no longer static"));
2466 jclass type = field->type;
2468 // We rewrite the instruction once we discover what it refers
2469 // to.
2470 void *newinsn = NULL;
2471 if (type->isPrimitive ())
2473 switch (type->size_in_bytes)
2475 case 1:
2476 PUSHI (*field->u.byte_addr);
2477 newinsn = AMPAMP (getstatic_resolved_1);
2478 break;
2480 case 2:
2481 if (type == JvPrimClass (char))
2483 PUSHI (*field->u.char_addr);
2484 newinsn = AMPAMP (getstatic_resolved_char);
2486 else
2488 PUSHI (*field->u.short_addr);
2489 newinsn = AMPAMP (getstatic_resolved_short);
2491 break;
2493 case 4:
2494 PUSHI(*field->u.int_addr);
2495 newinsn = AMPAMP (getstatic_resolved_4);
2496 break;
2498 case 8:
2499 PUSHL(*field->u.long_addr);
2500 newinsn = AMPAMP (getstatic_resolved_8);
2501 break;
2504 else
2506 PUSHA(*field->u.object_addr);
2507 newinsn = AMPAMP (getstatic_resolved_obj);
2510 #ifdef DIRECT_THREADED
2511 pc[-2].insn = newinsn;
2512 pc[-1].datum = field->u.addr;
2513 #endif /* DIRECT_THREADED */
2515 NEXT_INSN;
2517 #ifdef DIRECT_THREADED
2518 getstatic_resolved_1:
2519 PUSHI (*(jbyte *) AVAL ());
2520 NEXT_INSN;
2522 getstatic_resolved_char:
2523 PUSHI (*(jchar *) AVAL ());
2524 NEXT_INSN;
2526 getstatic_resolved_short:
2527 PUSHI (*(jshort *) AVAL ());
2528 NEXT_INSN;
2530 getstatic_resolved_4:
2531 PUSHI (*(jint *) AVAL ());
2532 NEXT_INSN;
2534 getstatic_resolved_8:
2535 PUSHL (*(jlong *) AVAL ());
2536 NEXT_INSN;
2538 getstatic_resolved_obj:
2539 PUSHA (*(jobject *) AVAL ());
2540 NEXT_INSN;
2541 #endif /* DIRECT_THREADED */
2543 insn_getfield:
2545 jint fieldref_index = GET2U ();
2546 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2547 _Jv_Field *field = pool_data[fieldref_index].field;
2549 if ((field->flags & Modifier::STATIC) != 0)
2550 throw_incompatible_class_change_error
2551 (JvNewStringLatin1 ("field is static"));
2553 jclass type = field->type;
2554 jint field_offset = field->u.boffset;
2555 if (field_offset > 0xffff)
2556 throw new java::lang::VirtualMachineError;
2558 jobject obj = POPA();
2559 NULLCHECK(obj);
2561 void *newinsn = NULL;
2562 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2563 if (type->isPrimitive ())
2565 switch (type->size_in_bytes)
2567 case 1:
2568 PUSHI (val->byte_value);
2569 newinsn = AMPAMP (getfield_resolved_1);
2570 break;
2572 case 2:
2573 if (type == JvPrimClass (char))
2575 PUSHI (val->char_value);
2576 newinsn = AMPAMP (getfield_resolved_char);
2578 else
2580 PUSHI (val->short_value);
2581 newinsn = AMPAMP (getfield_resolved_short);
2583 break;
2585 case 4:
2586 PUSHI (val->int_value);
2587 newinsn = AMPAMP (getfield_resolved_4);
2588 break;
2590 case 8:
2591 PUSHL (val->long_value);
2592 newinsn = AMPAMP (getfield_resolved_8);
2593 break;
2596 else
2598 PUSHA (val->object_value);
2599 newinsn = AMPAMP (getfield_resolved_obj);
2602 #ifdef DIRECT_THREADED
2603 pc[-2].insn = newinsn;
2604 pc[-1].int_val = field_offset;
2605 #endif /* DIRECT_THREADED */
2607 NEXT_INSN;
2609 #ifdef DIRECT_THREADED
2610 getfield_resolved_1:
2612 char *obj = (char *) POPA ();
2613 NULLCHECK (obj);
2614 PUSHI (*(jbyte *) (obj + INTVAL ()));
2616 NEXT_INSN;
2618 getfield_resolved_char:
2620 char *obj = (char *) POPA ();
2621 NULLCHECK (obj);
2622 PUSHI (*(jchar *) (obj + INTVAL ()));
2624 NEXT_INSN;
2626 getfield_resolved_short:
2628 char *obj = (char *) POPA ();
2629 NULLCHECK (obj);
2630 PUSHI (*(jshort *) (obj + INTVAL ()));
2632 NEXT_INSN;
2634 getfield_resolved_4:
2636 char *obj = (char *) POPA ();
2637 NULLCHECK (obj);
2638 PUSHI (*(jint *) (obj + INTVAL ()));
2640 NEXT_INSN;
2642 getfield_resolved_8:
2644 char *obj = (char *) POPA ();
2645 NULLCHECK (obj);
2646 PUSHL (*(jlong *) (obj + INTVAL ()));
2648 NEXT_INSN;
2650 getfield_resolved_obj:
2652 char *obj = (char *) POPA ();
2653 NULLCHECK (obj);
2654 PUSHA (*(jobject *) (obj + INTVAL ()));
2656 NEXT_INSN;
2657 #endif /* DIRECT_THREADED */
2659 insn_putstatic:
2661 jint fieldref_index = GET2U ();
2662 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2663 _Jv_Field *field = pool_data[fieldref_index].field;
2665 jclass type = field->type;
2667 // ResolvePoolEntry cannot check this
2668 if ((field->flags & Modifier::STATIC) == 0)
2669 throw_incompatible_class_change_error
2670 (JvNewStringLatin1 ("field no longer static"));
2672 void *newinsn = NULL;
2673 if (type->isPrimitive ())
2675 switch (type->size_in_bytes)
2677 case 1:
2679 jint value = POPI();
2680 *field->u.byte_addr = value;
2681 newinsn = AMPAMP (putstatic_resolved_1);
2682 break;
2685 case 2:
2687 jint value = POPI();
2688 *field->u.char_addr = value;
2689 newinsn = AMPAMP (putstatic_resolved_2);
2690 break;
2693 case 4:
2695 jint value = POPI();
2696 *field->u.int_addr = value;
2697 newinsn = AMPAMP (putstatic_resolved_4);
2698 break;
2701 case 8:
2703 jlong value = POPL();
2704 *field->u.long_addr = value;
2705 newinsn = AMPAMP (putstatic_resolved_8);
2706 break;
2710 else
2712 jobject value = POPA();
2713 *field->u.object_addr = value;
2714 newinsn = AMPAMP (putstatic_resolved_obj);
2717 #ifdef DIRECT_THREADED
2718 pc[-2].insn = newinsn;
2719 pc[-1].datum = field->u.addr;
2720 #endif /* DIRECT_THREADED */
2722 NEXT_INSN;
2724 #ifdef DIRECT_THREADED
2725 putstatic_resolved_1:
2726 *(jbyte *) AVAL () = POPI ();
2727 NEXT_INSN;
2729 putstatic_resolved_2:
2730 *(jchar *) AVAL () = POPI ();
2731 NEXT_INSN;
2733 putstatic_resolved_4:
2734 *(jint *) AVAL () = POPI ();
2735 NEXT_INSN;
2737 putstatic_resolved_8:
2738 *(jlong *) AVAL () = POPL ();
2739 NEXT_INSN;
2741 putstatic_resolved_obj:
2742 *(jobject *) AVAL () = POPA ();
2743 NEXT_INSN;
2744 #endif /* DIRECT_THREADED */
2746 insn_putfield:
2748 jint fieldref_index = GET2U ();
2749 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2750 _Jv_Field *field = pool_data[fieldref_index].field;
2752 jclass type = field->type;
2754 if ((field->flags & Modifier::STATIC) != 0)
2755 throw_incompatible_class_change_error
2756 (JvNewStringLatin1 ("field is static"));
2758 jint field_offset = field->u.boffset;
2759 if (field_offset > 0xffff)
2760 throw new java::lang::VirtualMachineError;
2762 void *newinsn = NULL;
2763 if (type->isPrimitive ())
2765 switch (type->size_in_bytes)
2767 case 1:
2769 jint value = POPI();
2770 jobject obj = POPA();
2771 NULLCHECK(obj);
2772 *(jbyte*) ((char*)obj + field_offset) = value;
2773 newinsn = AMPAMP (putfield_resolved_1);
2774 break;
2777 case 2:
2779 jint value = POPI();
2780 jobject obj = POPA();
2781 NULLCHECK(obj);
2782 *(jchar*) ((char*)obj + field_offset) = value;
2783 newinsn = AMPAMP (putfield_resolved_2);
2784 break;
2787 case 4:
2789 jint value = POPI();
2790 jobject obj = POPA();
2791 NULLCHECK(obj);
2792 *(jint*) ((char*)obj + field_offset) = value;
2793 newinsn = AMPAMP (putfield_resolved_4);
2794 break;
2797 case 8:
2799 jlong value = POPL();
2800 jobject obj = POPA();
2801 NULLCHECK(obj);
2802 *(jlong*) ((char*)obj + field_offset) = value;
2803 newinsn = AMPAMP (putfield_resolved_8);
2804 break;
2808 else
2810 jobject value = POPA();
2811 jobject obj = POPA();
2812 NULLCHECK(obj);
2813 *(jobject*) ((char*)obj + field_offset) = value;
2814 newinsn = AMPAMP (putfield_resolved_obj);
2817 #ifdef DIRECT_THREADED
2818 pc[-2].insn = newinsn;
2819 pc[-1].int_val = field_offset;
2820 #endif /* DIRECT_THREADED */
2822 NEXT_INSN;
2824 #ifdef DIRECT_THREADED
2825 putfield_resolved_1:
2827 jint val = POPI ();
2828 char *obj = (char *) POPA ();
2829 NULLCHECK (obj);
2830 *(jbyte *) (obj + INTVAL ()) = val;
2832 NEXT_INSN;
2834 putfield_resolved_2:
2836 jint val = POPI ();
2837 char *obj = (char *) POPA ();
2838 NULLCHECK (obj);
2839 *(jchar *) (obj + INTVAL ()) = val;
2841 NEXT_INSN;
2843 putfield_resolved_4:
2845 jint val = POPI ();
2846 char *obj = (char *) POPA ();
2847 NULLCHECK (obj);
2848 *(jint *) (obj + INTVAL ()) = val;
2850 NEXT_INSN;
2852 putfield_resolved_8:
2854 jlong val = POPL ();
2855 char *obj = (char *) POPA ();
2856 NULLCHECK (obj);
2857 *(jlong *) (obj + INTVAL ()) = val;
2859 NEXT_INSN;
2861 putfield_resolved_obj:
2863 jobject val = POPA ();
2864 char *obj = (char *) POPA ();
2865 NULLCHECK (obj);
2866 *(jobject *) (obj + INTVAL ()) = val;
2868 NEXT_INSN;
2869 #endif /* DIRECT_THREADED */
2871 insn_invokespecial:
2873 int index = GET2U ();
2875 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2876 index)).rmethod;
2878 sp -= rmeth->stack_item_count;
2880 // We don't use NULLCHECK here because we can't rely on that
2881 // working for <init>. So instead we do an explicit test.
2882 if (! sp[0].o)
2884 SAVE_PC();
2885 throw new java::lang::NullPointerException;
2888 fun = (void (*)()) rmeth->method->ncode;
2890 #ifdef DIRECT_THREADED
2891 // Rewrite instruction so that we use a faster pre-resolved
2892 // method.
2893 pc[-2].insn = &&invokespecial_resolved;
2894 pc[-1].datum = rmeth;
2895 #endif /* DIRECT_THREADED */
2897 goto perform_invoke;
2899 #ifdef DIRECT_THREADED
2900 invokespecial_resolved:
2902 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2903 sp -= rmeth->stack_item_count;
2904 // We don't use NULLCHECK here because we can't rely on that
2905 // working for <init>. So instead we do an explicit test.
2906 if (! sp[0].o)
2908 SAVE_PC();
2909 throw new java::lang::NullPointerException;
2911 fun = (void (*)()) rmeth->method->ncode;
2913 goto perform_invoke;
2914 #endif /* DIRECT_THREADED */
2916 insn_invokestatic:
2918 int index = GET2U ();
2920 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2921 index)).rmethod;
2923 sp -= rmeth->stack_item_count;
2925 fun = (void (*)()) rmeth->method->ncode;
2927 #ifdef DIRECT_THREADED
2928 // Rewrite instruction so that we use a faster pre-resolved
2929 // method.
2930 pc[-2].insn = &&invokestatic_resolved;
2931 pc[-1].datum = rmeth;
2932 #endif /* DIRECT_THREADED */
2934 goto perform_invoke;
2936 #ifdef DIRECT_THREADED
2937 invokestatic_resolved:
2939 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2940 sp -= rmeth->stack_item_count;
2941 fun = (void (*)()) rmeth->method->ncode;
2943 goto perform_invoke;
2944 #endif /* DIRECT_THREADED */
2946 insn_invokeinterface:
2948 int index = GET2U ();
2950 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2951 index)).rmethod;
2953 sp -= rmeth->stack_item_count;
2955 jobject rcv = sp[0].o;
2957 NULLCHECK (rcv);
2959 fun = (void (*)())
2960 _Jv_LookupInterfaceMethod (rcv->getClass (),
2961 rmeth->method->name,
2962 rmeth->method->signature);
2964 #ifdef DIRECT_THREADED
2965 // Rewrite instruction so that we use a faster pre-resolved
2966 // method.
2967 pc[-2].insn = &&invokeinterface_resolved;
2968 pc[-1].datum = rmeth;
2969 #else
2970 // Skip dummy bytes.
2971 pc += 2;
2972 #endif /* DIRECT_THREADED */
2974 goto perform_invoke;
2976 #ifdef DIRECT_THREADED
2977 invokeinterface_resolved:
2979 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2980 sp -= rmeth->stack_item_count;
2981 jobject rcv = sp[0].o;
2982 NULLCHECK (rcv);
2983 fun = (void (*)())
2984 _Jv_LookupInterfaceMethod (rcv->getClass (),
2985 rmeth->method->name,
2986 rmeth->method->signature);
2988 goto perform_invoke;
2989 #endif /* DIRECT_THREADED */
2991 insn_new:
2993 int index = GET2U ();
2994 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2995 index)).clazz;
2996 /* VM spec, section 3.11.5 */
2997 if ((klass->getModifiers() & Modifier::ABSTRACT)
2998 || klass->isInterface())
2999 throw new java::lang::InstantiationException;
3000 jobject res = _Jv_AllocObject (klass);
3001 PUSHA (res);
3003 #ifdef DIRECT_THREADED
3004 pc[-2].insn = &&new_resolved;
3005 pc[-1].datum = klass;
3006 #endif /* DIRECT_THREADED */
3008 NEXT_INSN;
3010 #ifdef DIRECT_THREADED
3011 new_resolved:
3013 jclass klass = (jclass) AVAL ();
3014 jobject res = _Jv_AllocObject (klass);
3015 PUSHA (res);
3017 NEXT_INSN;
3018 #endif /* DIRECT_THREADED */
3020 insn_newarray:
3022 int atype = GET1U ();
3023 int size = POPI();
3024 jobject result = _Jv_NewArray (atype, size);
3025 PUSHA (result);
3027 NEXT_INSN;
3029 insn_anewarray:
3031 int index = GET2U ();
3032 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3033 index)).clazz;
3034 int size = POPI();
3035 jobject result = _Jv_NewObjectArray (size, klass, 0);
3036 PUSHA (result);
3038 #ifdef DIRECT_THREADED
3039 pc[-2].insn = &&anewarray_resolved;
3040 pc[-1].datum = klass;
3041 #endif /* DIRECT_THREADED */
3043 NEXT_INSN;
3045 #ifdef DIRECT_THREADED
3046 anewarray_resolved:
3048 jclass klass = (jclass) AVAL ();
3049 int size = POPI ();
3050 jobject result = _Jv_NewObjectArray (size, klass, 0);
3051 PUSHA (result);
3053 NEXT_INSN;
3054 #endif /* DIRECT_THREADED */
3056 insn_arraylength:
3058 __JArray *arr = (__JArray*)POPA();
3059 NULLARRAYCHECK (arr);
3060 PUSHI (arr->length);
3062 NEXT_INSN;
3064 insn_athrow:
3066 jobject value = POPA();
3067 throw static_cast<jthrowable>(value);
3069 NEXT_INSN;
3071 insn_checkcast:
3073 SAVE_PC();
3074 jobject value = POPA();
3075 jint index = GET2U ();
3076 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3077 index)).clazz;
3079 value = (jobject) _Jv_CheckCast (to, value);
3081 PUSHA (value);
3083 #ifdef DIRECT_THREADED
3084 pc[-2].insn = &&checkcast_resolved;
3085 pc[-1].datum = to;
3086 #endif /* DIRECT_THREADED */
3088 NEXT_INSN;
3090 #ifdef DIRECT_THREADED
3091 checkcast_resolved:
3093 SAVE_PC();
3094 jobject value = POPA ();
3095 jclass to = (jclass) AVAL ();
3096 value = (jobject) _Jv_CheckCast (to, value);
3097 PUSHA (value);
3099 NEXT_INSN;
3100 #endif /* DIRECT_THREADED */
3102 insn_instanceof:
3104 SAVE_PC();
3105 jobject value = POPA();
3106 jint index = GET2U ();
3107 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3108 index)).clazz;
3109 PUSHI (to->isInstance (value));
3111 #ifdef DIRECT_THREADED
3112 pc[-2].insn = &&instanceof_resolved;
3113 pc[-1].datum = to;
3114 #endif /* DIRECT_THREADED */
3116 NEXT_INSN;
3118 #ifdef DIRECT_THREADED
3119 instanceof_resolved:
3121 jobject value = POPA ();
3122 jclass to = (jclass) AVAL ();
3123 PUSHI (to->isInstance (value));
3125 NEXT_INSN;
3126 #endif /* DIRECT_THREADED */
3128 insn_monitorenter:
3130 jobject value = POPA();
3131 NULLCHECK(value);
3132 _Jv_MonitorEnter (value);
3134 NEXT_INSN;
3136 insn_monitorexit:
3138 jobject value = POPA();
3139 NULLCHECK(value);
3140 _Jv_MonitorExit (value);
3142 NEXT_INSN;
3144 insn_ifnull:
3146 jobject val = POPA();
3147 if (val == NULL)
3148 TAKE_GOTO;
3149 else
3150 SKIP_GOTO;
3152 NEXT_INSN;
3154 insn_ifnonnull:
3156 jobject val = POPA();
3157 if (val != NULL)
3158 TAKE_GOTO;
3159 else
3160 SKIP_GOTO;
3162 NEXT_INSN;
3164 insn_multianewarray:
3166 int kind_index = GET2U ();
3167 int dim = GET1U ();
3169 jclass type
3170 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3171 kind_index)).clazz;
3172 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3174 for (int i = dim - 1; i >= 0; i--)
3176 sizes[i] = POPI ();
3179 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3181 PUSHA (res);
3183 NEXT_INSN;
3185 #ifndef DIRECT_THREADED
3186 insn_wide:
3188 jint the_mod_op = get1u (pc++);
3189 jint wide = get2u (pc); pc += 2;
3191 switch (the_mod_op)
3193 case op_istore:
3194 STOREI (wide);
3195 NEXT_INSN;
3197 case op_fstore:
3198 STOREF (wide);
3199 NEXT_INSN;
3201 case op_astore:
3202 STOREA (wide);
3203 NEXT_INSN;
3205 case op_lload:
3206 LOADL (wide);
3207 NEXT_INSN;
3209 case op_dload:
3210 LOADD (wide);
3211 NEXT_INSN;
3213 case op_iload:
3214 LOADI (wide);
3215 NEXT_INSN;
3217 case op_fload:
3218 LOADF (wide);
3219 NEXT_INSN;
3221 case op_aload:
3222 LOADA (wide);
3223 NEXT_INSN;
3225 case op_lstore:
3226 STOREL (wide);
3227 NEXT_INSN;
3229 case op_dstore:
3230 STORED (wide);
3231 NEXT_INSN;
3233 case op_ret:
3234 pc = (unsigned char*) PEEKA (wide);
3235 NEXT_INSN;
3237 case op_iinc:
3239 jint amount = get2s (pc); pc += 2;
3240 jint value = PEEKI (wide);
3241 POKEI (wide, value+amount);
3243 NEXT_INSN;
3245 default:
3246 throw_internal_error ("illegal bytecode modified by wide");
3250 #endif /* DIRECT_THREADED */
3252 catch (java::lang::Throwable *ex)
3254 #ifdef DIRECT_THREADED
3255 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3256 #else
3257 int logical_pc = pc - 1 - bytecode ();
3258 #endif
3259 _Jv_InterpException *exc = meth->exceptions ();
3260 jclass exc_class = ex->getClass ();
3262 for (int i = 0; i < meth->exc_count; i++)
3264 if (PCVAL (exc[i].start_pc) <= logical_pc
3265 && logical_pc < PCVAL (exc[i].end_pc))
3267 #ifdef DIRECT_THREADED
3268 jclass handler = (jclass) exc[i].handler_type.p;
3269 #else
3270 jclass handler = NULL;
3271 if (exc[i].handler_type.i != 0)
3272 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3273 exc[i].handler_type.i)).clazz;
3274 #endif /* DIRECT_THREADED */
3276 if (handler == NULL || handler->isAssignableFrom (exc_class))
3278 #ifdef DIRECT_THREADED
3279 pc = (insn_slot *) exc[i].handler_pc.p;
3280 #else
3281 pc = bytecode () + exc[i].handler_pc.i;
3282 #endif /* DIRECT_THREADED */
3283 sp = stack;
3284 sp++->o = ex; // Push exception.
3285 NEXT_INSN;
3290 // No handler, so re-throw.
3291 throw ex;
3295 static void
3296 throw_internal_error (const char *msg)
3298 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3301 static void
3302 throw_incompatible_class_change_error (jstring msg)
3304 throw new java::lang::IncompatibleClassChangeError (msg);
3307 #ifndef HANDLE_SEGV
3308 static java::lang::NullPointerException *null_pointer_exc;
3309 static void
3310 throw_null_pointer_exception ()
3312 if (null_pointer_exc == NULL)
3313 null_pointer_exc = new java::lang::NullPointerException;
3315 throw null_pointer_exc;
3317 #endif
3319 /* Look up source code line number for given bytecode (or direct threaded
3320 interpreter) PC. */
3322 _Jv_InterpMethod::get_source_line(pc_t mpc)
3324 int line = line_table_len > 0 ? line_table[0].line : -1;
3325 for (int i = 1; i < line_table_len; i++)
3326 if (line_table[i].pc > mpc)
3327 break;
3328 else
3329 line = line_table[i].line;
3331 return line;
3334 /** Do static initialization for fields with a constant initializer */
3335 void
3336 _Jv_InitField (jobject obj, jclass klass, int index)
3338 using namespace java::lang::reflect;
3340 if (obj != 0 && klass == 0)
3341 klass = obj->getClass ();
3343 if (!_Jv_IsInterpretedClass (klass))
3344 return;
3346 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3348 _Jv_Field * field = (&klass->fields[0]) + index;
3350 if (index > klass->field_count)
3351 throw_internal_error ("field out of range");
3353 int init = iclass->field_initializers[index];
3354 if (init == 0)
3355 return;
3357 _Jv_Constants *pool = &klass->constants;
3358 int tag = pool->tags[init];
3360 if (! field->isResolved ())
3361 throw_internal_error ("initializing unresolved field");
3363 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3364 throw_internal_error ("initializing non-static field with no object");
3366 void *addr = 0;
3368 if ((field->flags & Modifier::STATIC) != 0)
3369 addr = (void*) field->u.addr;
3370 else
3371 addr = (void*) (((char*)obj) + field->u.boffset);
3373 switch (tag)
3375 case JV_CONSTANT_String:
3377 jstring str;
3378 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3379 pool->data[init].string = str;
3380 pool->tags[init] = JV_CONSTANT_ResolvedString;
3382 /* fall through */
3384 case JV_CONSTANT_ResolvedString:
3385 if (! (field->type == &java::lang::String::class$
3386 || field->type == &java::lang::Class::class$))
3387 throw_class_format_error ("string initialiser to non-string field");
3389 *(jstring*)addr = pool->data[init].string;
3390 break;
3392 case JV_CONSTANT_Integer:
3394 int value = pool->data[init].i;
3396 if (field->type == JvPrimClass (boolean))
3397 *(jboolean*)addr = (jboolean)value;
3399 else if (field->type == JvPrimClass (byte))
3400 *(jbyte*)addr = (jbyte)value;
3402 else if (field->type == JvPrimClass (char))
3403 *(jchar*)addr = (jchar)value;
3405 else if (field->type == JvPrimClass (short))
3406 *(jshort*)addr = (jshort)value;
3408 else if (field->type == JvPrimClass (int))
3409 *(jint*)addr = (jint)value;
3411 else
3412 throw_class_format_error ("erroneous field initializer");
3414 break;
3416 case JV_CONSTANT_Long:
3417 if (field->type != JvPrimClass (long))
3418 throw_class_format_error ("erroneous field initializer");
3420 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3421 break;
3423 case JV_CONSTANT_Float:
3424 if (field->type != JvPrimClass (float))
3425 throw_class_format_error ("erroneous field initializer");
3427 *(jfloat*)addr = pool->data[init].f;
3428 break;
3430 case JV_CONSTANT_Double:
3431 if (field->type != JvPrimClass (double))
3432 throw_class_format_error ("erroneous field initializer");
3434 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3435 break;
3437 default:
3438 throw_class_format_error ("erroneous field initializer");
3442 inline static unsigned char*
3443 skip_one_type (unsigned char* ptr)
3445 int ch = *ptr++;
3447 while (ch == '[')
3449 ch = *ptr++;
3452 if (ch == 'L')
3454 do { ch = *ptr++; } while (ch != ';');
3457 return ptr;
3460 static ffi_type*
3461 get_ffi_type_from_signature (unsigned char* ptr)
3463 switch (*ptr)
3465 case 'L':
3466 case '[':
3467 return &ffi_type_pointer;
3468 break;
3470 case 'Z':
3471 // On some platforms a bool is a byte, on others an int.
3472 if (sizeof (jboolean) == sizeof (jbyte))
3473 return &ffi_type_sint8;
3474 else
3476 JvAssert (sizeof (jbyte) == sizeof (jint));
3477 return &ffi_type_sint32;
3479 break;
3481 case 'B':
3482 return &ffi_type_sint8;
3483 break;
3485 case 'C':
3486 return &ffi_type_uint16;
3487 break;
3489 case 'S':
3490 return &ffi_type_sint16;
3491 break;
3493 case 'I':
3494 return &ffi_type_sint32;
3495 break;
3497 case 'J':
3498 return &ffi_type_sint64;
3499 break;
3501 case 'F':
3502 return &ffi_type_float;
3503 break;
3505 case 'D':
3506 return &ffi_type_double;
3507 break;
3509 case 'V':
3510 return &ffi_type_void;
3511 break;
3514 throw_internal_error ("unknown type in signature");
3517 /* this function yields the number of actual arguments, that is, if the
3518 * function is non-static, then one is added to the number of elements
3519 * found in the signature */
3521 int
3522 _Jv_count_arguments (_Jv_Utf8Const *signature,
3523 jboolean staticp)
3525 unsigned char *ptr = (unsigned char*) signature->chars();
3526 int arg_count = staticp ? 0 : 1;
3528 /* first, count number of arguments */
3530 // skip '('
3531 ptr++;
3533 // count args
3534 while (*ptr != ')')
3536 ptr = skip_one_type (ptr);
3537 arg_count += 1;
3540 return arg_count;
3543 /* This beast will build a cif, given the signature. Memory for
3544 * the cif itself and for the argument types must be allocated by the
3545 * caller.
3548 static int
3549 init_cif (_Jv_Utf8Const* signature,
3550 int arg_count,
3551 jboolean staticp,
3552 ffi_cif *cif,
3553 ffi_type **arg_types,
3554 ffi_type **rtype_p)
3556 unsigned char *ptr = (unsigned char*) signature->chars();
3558 int arg_index = 0; // arg number
3559 int item_count = 0; // stack-item count
3561 // setup receiver
3562 if (!staticp)
3564 arg_types[arg_index++] = &ffi_type_pointer;
3565 item_count += 1;
3568 // skip '('
3569 ptr++;
3571 // assign arg types
3572 while (*ptr != ')')
3574 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3576 if (*ptr == 'J' || *ptr == 'D')
3577 item_count += 2;
3578 else
3579 item_count += 1;
3581 ptr = skip_one_type (ptr);
3584 // skip ')'
3585 ptr++;
3586 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3588 ptr = skip_one_type (ptr);
3589 if (ptr != (unsigned char*)signature->chars() + signature->len())
3590 throw_internal_error ("did not find end of signature");
3592 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3593 arg_count, rtype, arg_types) != FFI_OK)
3594 throw_internal_error ("ffi_prep_cif failed");
3596 if (rtype_p != NULL)
3597 *rtype_p = rtype;
3599 return item_count;
3602 #if FFI_NATIVE_RAW_API
3603 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3604 # define FFI_RAW_SIZE ffi_raw_size
3605 #else
3606 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3607 # define FFI_RAW_SIZE ffi_java_raw_size
3608 #endif
3610 /* we put this one here, and not in interpret.cc because it
3611 * calls the utility routines _Jv_count_arguments
3612 * which are static to this module. The following struct defines the
3613 * layout we use for the stubs, it's only used in the ncode method. */
3615 typedef struct {
3616 ffi_raw_closure closure;
3617 ffi_cif cif;
3618 ffi_type *arg_types[0];
3619 } ncode_closure;
3621 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3623 void *
3624 _Jv_InterpMethod::ncode ()
3626 using namespace java::lang::reflect;
3628 if (self->ncode != 0)
3629 return self->ncode;
3631 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3632 int arg_count = _Jv_count_arguments (self->signature, staticp);
3634 ncode_closure *closure =
3635 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3636 + arg_count * sizeof (ffi_type*));
3638 init_cif (self->signature,
3639 arg_count,
3640 staticp,
3641 &closure->cif,
3642 &closure->arg_types[0],
3643 NULL);
3645 ffi_closure_fun fun;
3647 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3649 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3651 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3653 if (staticp)
3654 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3655 else
3656 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3658 else
3660 if (staticp)
3661 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3662 else
3663 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3666 FFI_PREP_RAW_CLOSURE (&closure->closure,
3667 &closure->cif,
3668 fun,
3669 (void*)this);
3671 self->ncode = (void*)closure;
3672 return self->ncode;
3675 #ifdef DIRECT_THREADED
3676 /* Find the index of the given insn in the array of insn slots
3677 for this method. Returns -1 if not found. */
3678 jlong
3679 _Jv_InterpMethod::insn_index (pc_t pc)
3681 jlong left = 0;
3682 jlong right = number_insn_slots;
3683 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3685 while (right >= 0)
3687 jlong mid = (left + right) / 2;
3688 if (&slots[mid] == pc)
3689 return mid;
3691 if (pc < &slots[mid])
3692 right = mid - 1;
3693 else
3694 left = mid + 1;
3697 return -1;
3699 #endif // DIRECT_THREADED
3701 void
3702 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
3703 jintArray& line_numbers,
3704 jlongArray& code_indices)
3706 #ifdef DIRECT_THREADED
3707 /* For the DIRECT_THREADED case, if the method has not yet been
3708 * compiled, the linetable will change to insn slots instead of
3709 * bytecode PCs. It is probably easiest, in this case, to simply
3710 * compile the method and guarantee that we are using insn
3711 * slots.
3713 _Jv_CompileMethod (this);
3715 if (line_table_len > 0)
3717 start = 0;
3718 end = number_insn_slots;
3719 line_numbers = JvNewIntArray (line_table_len);
3720 code_indices = JvNewLongArray (line_table_len);
3722 jint* lines = elements (line_numbers);
3723 jlong* indices = elements (code_indices);
3724 for (int i = 0; i < line_table_len; ++i)
3726 lines[i] = line_table[i].line;
3727 indices[i] = insn_index (line_table[i].pc);
3730 #else // !DIRECT_THREADED
3731 if (line_table_len > 0)
3733 start = 0;
3734 end = code_length;
3735 line_numbers = JvNewIntArray (line_table_len);
3736 code_indices = JvNewLongArray (line_table_len);
3738 jint* lines = elements (line_numbers);
3739 jlong* indices = elements (code_indices);
3740 for (int i = 0; i < line_table_len; ++i)
3742 lines[i] = line_table[i].line;
3743 indices[i] = (jlong) line_table[i].bytecode_pc;
3746 #endif // !DIRECT_THREADED
3749 void *
3750 _Jv_JNIMethod::ncode ()
3752 using namespace java::lang::reflect;
3754 if (self->ncode != 0)
3755 return self->ncode;
3757 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3758 int arg_count = _Jv_count_arguments (self->signature, staticp);
3760 ncode_closure *closure =
3761 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3762 + arg_count * sizeof (ffi_type*));
3764 ffi_type *rtype;
3765 init_cif (self->signature,
3766 arg_count,
3767 staticp,
3768 &closure->cif,
3769 &closure->arg_types[0],
3770 &rtype);
3772 ffi_closure_fun fun;
3774 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3776 // Initialize the argument types and CIF that represent the actual
3777 // underlying JNI function.
3778 int extra_args = 1;
3779 if ((self->accflags & Modifier::STATIC))
3780 ++extra_args;
3781 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3782 * sizeof (ffi_type *));
3783 int offset = 0;
3784 jni_arg_types[offset++] = &ffi_type_pointer;
3785 if ((self->accflags & Modifier::STATIC))
3786 jni_arg_types[offset++] = &ffi_type_pointer;
3787 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3788 arg_count * sizeof (ffi_type *));
3790 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3791 extra_args + arg_count, rtype,
3792 jni_arg_types) != FFI_OK)
3793 throw_internal_error ("ffi_prep_cif failed for JNI function");
3795 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3797 // FIXME: for now we assume that all native methods for
3798 // interpreted code use JNI.
3799 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3801 FFI_PREP_RAW_CLOSURE (&closure->closure,
3802 &closure->cif,
3803 fun,
3804 (void*) this);
3806 self->ncode = (void *) closure;
3807 return self->ncode;
3810 static void
3811 throw_class_format_error (jstring msg)
3813 throw (msg
3814 ? new java::lang::ClassFormatError (msg)
3815 : new java::lang::ClassFormatError);
3818 static void
3819 throw_class_format_error (const char *msg)
3821 throw_class_format_error (JvNewStringLatin1 (msg));
3826 void
3827 _Jv_InterpreterEngine::do_verify (jclass klass)
3829 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3830 for (int i = 0; i < klass->method_count; i++)
3832 using namespace java::lang::reflect;
3833 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3834 _Jv_ushort accflags = klass->methods[i].accflags;
3835 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3837 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3838 _Jv_VerifyMethod (im);
3843 void
3844 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3846 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3847 for (int i = 0; i < klass->method_count; i++)
3849 // Just skip abstract methods. This is particularly important
3850 // because we don't resize the interpreted_methods array when
3851 // miranda methods are added to it.
3852 if ((klass->methods[i].accflags
3853 & java::lang::reflect::Modifier::ABSTRACT)
3854 != 0)
3855 continue;
3857 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3859 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3860 != 0)
3862 // You might think we could use a virtual `ncode' method in
3863 // the _Jv_MethodBase and unify the native and non-native
3864 // cases. Well, we can't, because we don't allocate these
3865 // objects using `new', and thus they don't get a vtable.
3866 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3867 klass->methods[i].ncode = jnim->ncode ();
3869 else if (imeth != 0) // it could be abstract
3871 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3872 klass->methods[i].ncode = im->ncode ();
3877 void
3878 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3879 int pointer_size,
3880 int other_size)
3882 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3884 // Splitting the allocations here lets us scan reference fields and
3885 // avoid scanning non-reference fields.
3886 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
3887 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
3889 for (int i = 0; i < klass->field_count; i++)
3891 _Jv_Field *field = &klass->fields[i];
3893 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3894 continue;
3896 char *base = field->isRef() ? reference_fields : non_reference_fields;
3897 field->u.addr = base + field->u.boffset;
3899 if (iclass->field_initializers[i] != 0)
3901 _Jv_Linker::resolve_field (field, klass->loader);
3902 _Jv_InitField (0, klass, i);
3906 // Now we don't need the field_initializers anymore, so let the
3907 // collector get rid of it.
3908 iclass->field_initializers = 0;
3911 _Jv_ResolvedMethod *
3912 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3913 jboolean staticp, jint vtable_index)
3915 int arg_count = _Jv_count_arguments (method->signature, staticp);
3917 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3918 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3919 + arg_count*sizeof (ffi_type*));
3921 result->stack_item_count
3922 = init_cif (method->signature,
3923 arg_count,
3924 staticp,
3925 &result->cif,
3926 &result->arg_types[0],
3927 NULL);
3929 result->vtable_index = vtable_index;
3930 result->method = method;
3931 result->klass = klass;
3933 return result;
3936 void
3937 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3939 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3940 for (int i = 0; i < klass->method_count; i++)
3942 // Just skip abstract methods. This is particularly important
3943 // because we don't resize the interpreted_methods array when
3944 // miranda methods are added to it.
3945 if ((klass->methods[i].accflags
3946 & java::lang::reflect::Modifier::ABSTRACT)
3947 != 0)
3948 continue;
3949 // Miranda method additions mean that the `methods' array moves.
3950 // We cache a pointer into this array, so we have to update.
3951 iclass->interpreted_methods[i]->self = &klass->methods[i];
3955 #ifdef DIRECT_THREADED
3956 void
3957 _Jv_CompileMethod (_Jv_InterpMethod* method)
3959 if (method->prepared == NULL)
3960 _Jv_InterpMethod::run (NULL, NULL, method);
3962 #endif // DIRECT_THREADED
3964 #endif // INTERPRETER