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
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
16 #pragma implementation "java-interp.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>
43 // Execution engine for interpreted code.
44 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
50 static void throw_internal_error (char *msg)
51 __attribute__ ((__noreturn__));
52 static void throw_incompatible_class_change_error (jstring msg)
53 __attribute__ ((__noreturn__));
55 static void throw_null_pointer_exception ()
56 __attribute__ ((__noreturn__));
59 static void throw_class_format_error (jstring msg)
60 __attribute__ ((__noreturn__));
61 static void throw_class_format_error (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;
73 _Jv_MutexInit (&compile_mutex);
76 void _Jv_InitInterpreter() {}
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
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>
100 convert (FROM val, TO min, TO max)
103 if (val >= (FROM) max)
105 else if (val <= (FROM) min)
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)
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)
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)
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; })
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)
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]; \
155 # define LOADD(I) LOADL(I)
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)
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]; \
169 # define STORED(I) STOREL(I)
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) {
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
228 #define NULLCHECK(X) SAVE_PC()
229 #define NULLARRAYCHECK(X) SAVE_PC()
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)
237 #define ARRAYBOUNDSCHECK(array, index) \
240 if (((unsigned) index) >= (unsigned) (array->length)) \
241 _Jv_ThrowBadArrayIndex (index); \
246 _Jv_InterpMethod::run_normal (ffi_cif *,
251 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
252 run (ret, args, _this);
256 _Jv_InterpMethod::run_synch_object (ffi_cif *,
261 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
263 jobject rcv = (jobject) args[0].ptr;
264 JvSynchronize mutex (rcv);
266 run (ret, args, _this);
270 _Jv_InterpMethod::run_class (ffi_cif *,
275 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
276 _Jv_InitClass (_this->defining_class);
277 run (ret, args, _this);
281 _Jv_InterpMethod::run_synch_class (ffi_cif *,
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
299 _Jv_InterpMethod::compile (const void * const *insn_targets)
301 insn_slot *insns = NULL;
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) \
313 insns[next++].Field = Value; \
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)
326 for (int i = 0; i < 2; ++i)
328 jboolean first_pass = i == 0;
332 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
333 number_insn_slots = next;
337 unsigned char *pc = codestart;
340 int base_pc_val = pc - codestart;
342 pc_mapping[base_pc_val] = next;
344 java_opcode opcode = (java_opcode) *pc++;
346 if (opcode == op_nop)
348 SET_INSN (insn_targets[opcode]);
489 case op_monitorenter:
499 // No argument, nothing else to do.
503 SET_INT (get1s (pc));
509 int index = get1u (pc);
511 // For an unresolved class we want to delay resolution
513 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
516 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
520 SET_DATUM (pool_data[index].o);
536 SET_INT (get1u (pc));
541 SET_INT (get1u (pc));
542 SET_INT (get1s (pc + 1));
548 int index = get2u (pc);
550 // For an unresolved class we want to delay resolution
552 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
555 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
559 SET_DATUM (pool_data[index].o);
565 int index = get2u (pc);
567 SET_DATUM (&pool_data[index]);
572 SET_INT (get2s (pc));
584 case op_invokespecial:
585 case op_invokestatic:
586 case op_invokevirtual:
587 SET_INT (get2u (pc));
591 case op_multianewarray:
592 SET_INT (get2u (pc));
593 SET_INT (get1u (pc + 2));
616 int offset = get2s (pc);
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.
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];
635 && (opcode == op_ireturn || opcode == op_lreturn
636 || opcode == op_freturn || opcode == op_dreturn
637 || opcode == op_areturn || opcode == op_return))
640 SET_INSN (insn_targets[opcode]);
643 SET_DATUM (&insns[pc_mapping[new_pc]]);
649 while ((pc - codestart) % 4 != 0)
652 jint def = get4 (pc);
653 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
659 int high = get4 (pc);
663 for (int i = low; i <= high; ++i)
665 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
671 case op_lookupswitch:
673 while ((pc - codestart) % 4 != 0)
676 jint def = get4 (pc);
677 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
680 jint npairs = get4 (pc);
686 jint match = get4 (pc);
687 jint offset = get4 (pc + 4);
689 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
695 case op_invokeinterface:
697 jint index = get2u (pc);
699 // We ignore the next two bytes.
707 opcode = (java_opcode) get1u (pc);
709 jint val = get2u (pc);
712 // We implement narrow and wide instructions using the
713 // same code in the interpreter. So we rewrite the
714 // instruction slot here.
716 insns[next - 1].insn = (void *) insn_targets[opcode];
719 if (opcode == op_iinc)
721 SET_INT (get2s (pc));
730 jint offset = get4 (pc);
732 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
736 // Some "can't happen" cases that we include for
737 // error-checking purposes.
755 case op_getstatic_2s:
756 case op_getstatic_2u:
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]];
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)
789 line_table[i].pc = &insns[pc_mapping[byte_pc]];
794 #endif /* DIRECT_THREADED */
796 /* Run the given method.
797 When args is NULL, don't run anything -- just compile it. */
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
809 java::lang::Thread *thread = java::lang::Thread::currentThread();
810 _Jv_InterpFrame frame_desc (meth,
811 (_Jv_InterpFrame **) &thread->interp_frame);
813 _Jv_word stack[meth->max_stack];
814 _Jv_word *sp = stack;
816 _Jv_word locals[meth->max_locals];
818 #define INSN_LABEL(op) &&insn_##op
820 static const void *const insn_target[] =
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),
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),
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),
992 INSN_LABEL(tableswitch),
993 INSN_LABEL(lookupswitch),
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),
1010 INSN_LABEL(newarray),
1011 INSN_LABEL(anewarray),
1012 INSN_LABEL(arraylength),
1014 INSN_LABEL(checkcast),
1015 INSN_LABEL(instanceof),
1016 INSN_LABEL(monitorenter),
1017 INSN_LABEL(monitorexit),
1018 #ifdef DIRECT_THREADED
1023 INSN_LABEL(multianewarray),
1025 INSN_LABEL(ifnonnull),
1028 #ifdef DIRECT_THREADED
1029 INSN_LABEL (ldc_class)
1037 #ifdef DIRECT_THREADED
1039 #define NEXT_INSN goto *((pc++)->insn)
1040 #define INTVAL() ((pc++)->int_val)
1041 #define AVAL() ((pc++)->datum)
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
1055 // Compile if we must. NOTE: Double-check locking.
1056 if (meth->prepared == NULL)
1058 _Jv_MutexLock (&compile_mutex);
1059 if (meth->prepared == NULL)
1060 meth->compile (insn_target);
1061 _Jv_MutexUnlock (&compile_mutex);
1064 // If we're only compiling, stop here
1068 pc = (insn_slot *) meth->prepared;
1072 #define NEXT_INSN goto *(insn_target[*pc++])
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
1081 ({ int index = get1u (pc++); \
1082 resolve_pool_entry (meth->defining_class, index).o; })
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
1096 #endif /* DIRECT_THREADED */
1098 #define TAKE_GOTO pc = GOTO_VAL ()
1100 /* Go straight at it! the ffi raw format matches the internal
1101 stack representation exactly. At least, that's the idea.
1103 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1105 _Jv_word *pool_data = meth->defining_class->constants.data;
1107 /* These three are temporaries for common code used by several
1110 _Jv_ResolvedMethod* rmeth;
1115 // We keep nop around. It is used if we're interpreting the
1116 // bytecodes and not doing direct threading.
1120 /* The first few instructions here are ordered according to their
1121 frequency, in the hope that this will improve code locality a
1124 insn_aload_0: // 0x2a
1132 insn_iload_1: // 0x1b
1136 insn_invokevirtual: // 0xb6
1138 int index = GET2U ();
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. */
1147 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
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
1156 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1157 throw new java::lang::NullPointerException;
1160 if (rmeth->vtable_index == -1)
1162 // final methods do not appear in the vtable,
1163 // if it does not appear in the superclass.
1164 fun = (void (*)()) rmeth->method->ncode;
1168 jobject rcv = sp[0].o;
1169 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1170 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1173 #ifdef DIRECT_THREADED
1174 // Rewrite instruction so that we use a faster pre-resolved
1176 pc[-2].insn = &&invokevirtual_resolved;
1177 pc[-1].datum = rmeth;
1178 #endif /* DIRECT_THREADED */
1180 goto perform_invoke;
1182 #ifdef DIRECT_THREADED
1183 invokevirtual_resolved:
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
1193 throw new java::lang::NullPointerException;
1196 if (rmeth->vtable_index == -1)
1198 // final methods do not appear in the vtable,
1199 // if it does not appear in the superclass.
1200 fun = (void (*)()) rmeth->method->ncode;
1204 jobject rcv = sp[0].o;
1205 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1206 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1209 goto perform_invoke;
1210 #endif /* DIRECT_THREADED */
1216 /* here goes the magic again... */
1217 ffi_cif *cif = &rmeth->cif;
1218 ffi_raw *raw = (ffi_raw*) sp;
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);
1227 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1230 int rtype = cif->rtype->type;
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)
1236 PUSHA (rvalue.object_value);
1238 else if (rtype == FFI_TYPE_SINT32)
1240 PUSHI (rvalue.int_value);
1242 else if (rtype == FFI_TYPE_VOID)
1250 case FFI_TYPE_SINT8:
1251 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1254 case FFI_TYPE_SINT16:
1255 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1258 case FFI_TYPE_UINT16:
1259 PUSHI (rvalue.int_value & 0xffff);
1262 case FFI_TYPE_FLOAT:
1263 PUSHF (rvalue.float_value);
1266 case FFI_TYPE_DOUBLE:
1267 PUSHD (rvalue.double_value);
1270 case FFI_TYPE_SINT64:
1271 PUSHL (rvalue.long_value);
1275 throw_internal_error ("unknown return type in invokeXXX");
1342 // For direct threaded, bipush and sipush are the same.
1343 #ifndef DIRECT_THREADED
1346 #endif /* DIRECT_THREADED */
1352 // For direct threaded, ldc and ldc_w are the same.
1353 #ifndef DIRECT_THREADED
1354 PUSHA ((jobject) AVAL1U ());
1356 #endif /* DIRECT_THREADED */
1358 PUSHA ((jobject) AVAL2U ());
1361 #ifdef DIRECT_THREADED
1362 // For direct threaded we have a separate 'ldc class' operation.
1365 // We could rewrite the instruction at this point.
1366 int index = INTVAL ();
1367 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1372 #endif /* DIRECT_THREADED */
1376 void *where = AVAL2UP ();
1377 memcpy (sp, where, 2*sizeof (_Jv_word));
1472 jint index = POPI();
1473 jintArray arr = (jintArray) POPA();
1474 NULLARRAYCHECK (arr);
1475 ARRAYBOUNDSCHECK (arr, index);
1476 PUSHI( elements(arr)[index] );
1482 jint index = POPI();
1483 jlongArray arr = (jlongArray) POPA();
1484 NULLARRAYCHECK (arr);
1485 ARRAYBOUNDSCHECK (arr, index);
1486 PUSHL( elements(arr)[index] );
1492 jint index = POPI();
1493 jfloatArray arr = (jfloatArray) POPA();
1494 NULLARRAYCHECK (arr);
1495 ARRAYBOUNDSCHECK (arr, index);
1496 PUSHF( elements(arr)[index] );
1502 jint index = POPI();
1503 jdoubleArray arr = (jdoubleArray) POPA();
1504 NULLARRAYCHECK (arr);
1505 ARRAYBOUNDSCHECK (arr, index);
1506 PUSHD( elements(arr)[index] );
1512 jint index = POPI();
1513 jobjectArray arr = (jobjectArray) POPA();
1514 NULLARRAYCHECK (arr);
1515 ARRAYBOUNDSCHECK (arr, index);
1516 PUSHA( elements(arr)[index] );
1522 jint index = POPI();
1523 jbyteArray arr = (jbyteArray) POPA();
1524 NULLARRAYCHECK (arr);
1525 ARRAYBOUNDSCHECK (arr, index);
1526 PUSHI( elements(arr)[index] );
1532 jint index = POPI();
1533 jcharArray arr = (jcharArray) POPA();
1534 NULLARRAYCHECK (arr);
1535 ARRAYBOUNDSCHECK (arr, index);
1536 PUSHI( elements(arr)[index] );
1542 jint index = POPI();
1543 jshortArray arr = (jshortArray) POPA();
1544 NULLARRAYCHECK (arr);
1545 ARRAYBOUNDSCHECK (arr, index);
1546 PUSHI( elements(arr)[index] );
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;
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;
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;
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;
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;
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;
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;
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;
1753 dupx (sp, 1, 1); sp+=1;
1757 dupx (sp, 1, 2); sp+=1;
1767 dupx (sp, 2, 1); sp+=2;
1771 dupx (sp, 2, 2); sp+=2;
1776 jobject tmp1 = POPA();
1777 jobject tmp2 = POPA();
1833 jint value2 = POPI();
1834 jint value1 = POPI();
1835 jint res = _Jv_divI (value1, value2);
1842 jlong value2 = POPL();
1843 jlong value1 = POPL();
1844 jlong res = _Jv_divJ (value1, value2);
1851 jfloat value2 = POPF();
1852 jfloat value1 = POPF();
1853 jfloat res = value1 / value2;
1860 jdouble value2 = POPD();
1861 jdouble value1 = POPD();
1862 jdouble res = value1 / value2;
1869 jint value2 = POPI();
1870 jint value1 = POPI();
1871 jint res = _Jv_remI (value1, value2);
1878 jlong value2 = POPL();
1879 jlong value1 = POPL();
1880 jlong res = _Jv_remJ (value1, value2);
1887 jfloat value2 = POPF();
1888 jfloat value1 = POPF();
1889 jfloat res = __ieee754_fmod (value1, value2);
1896 jdouble value2 = POPD();
1897 jdouble value1 = POPD();
1898 jdouble res = __ieee754_fmod (value1, value2);
1905 jint value = POPI();
1912 jlong value = POPL();
1919 jfloat value = POPF();
1926 jdouble value = POPD();
1933 jint shift = (POPI() & 0x1f);
1934 jint value = POPI();
1935 PUSHI (value << shift);
1941 jint shift = (POPI() & 0x3f);
1942 jlong value = POPL();
1943 PUSHL (value << shift);
1949 jint shift = (POPI() & 0x1f);
1950 jint value = POPI();
1951 PUSHI (value >> shift);
1957 jint shift = (POPI() & 0x3f);
1958 jlong value = POPL();
1959 PUSHL (value >> shift);
1965 jint shift = (POPI() & 0x1f);
1966 _Jv_uint value = (_Jv_uint) POPI();
1967 PUSHI ((jint) (value >> shift));
1973 jint shift = (POPI() & 0x3f);
1974 _Jv_ulong value = (_Jv_ulong) POPL();
1975 PUSHL ((jlong) (value >> shift));
2005 jint index = GET1U ();
2006 jint amount = GET1S ();
2007 locals[index].i += amount;
2012 {jlong value = POPI(); PUSHL (value);}
2016 {jfloat value = POPI(); PUSHF (value);}
2020 {jdouble value = POPI(); PUSHD (value);}
2024 {jint value = POPL(); PUSHI (value);}
2028 {jfloat value = POPL(); PUSHF (value);}
2032 {jdouble value = POPL(); PUSHD (value);}
2037 using namespace java::lang;
2038 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2045 using namespace java::lang;
2046 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2052 { jdouble value = POPF (); PUSHD(value); }
2057 using namespace java::lang;
2058 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2065 using namespace java::lang;
2066 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2072 { jfloat value = POPD (); PUSHF(value); }
2076 { jbyte value = POPI (); PUSHI(value); }
2080 { jchar value = POPI (); PUSHI(value); }
2084 { jshort value = POPI (); PUSHI(value); }
2089 jlong value2 = POPL ();
2090 jlong value1 = POPL ();
2091 if (value1 > value2)
2093 else if (value1 == value2)
2109 jfloat value2 = POPF ();
2110 jfloat value1 = POPF ();
2111 if (value1 > value2)
2113 else if (value1 == value2)
2115 else if (value1 < value2)
2131 jdouble value2 = POPD ();
2132 jdouble value1 = POPD ();
2133 if (value1 > value2)
2135 else if (value1 == value2)
2137 else if (value1 < value2)
2200 jint value2 = POPI();
2201 jint value1 = POPI();
2202 if (value1 == value2)
2211 jint value2 = POPI();
2212 jint value1 = POPI();
2213 if (value1 != value2)
2222 jint value2 = POPI();
2223 jint value1 = POPI();
2224 if (value1 < value2)
2233 jint value2 = POPI();
2234 jint value1 = POPI();
2235 if (value1 >= value2)
2244 jint value2 = POPI();
2245 jint value1 = POPI();
2246 if (value1 > value2)
2255 jint value2 = POPI();
2256 jint value1 = POPI();
2257 if (value1 <= value2)
2266 jobject value2 = POPA();
2267 jobject value1 = POPA();
2268 if (value1 == value2)
2277 jobject value2 = POPA();
2278 jobject value1 = POPA();
2279 if (value1 != value2)
2287 #ifndef DIRECT_THREADED
2288 // For direct threaded, goto and goto_w are the same.
2289 pc = pc - 1 + get4 (pc);
2291 #endif /* DIRECT_THREADED */
2297 #ifndef DIRECT_THREADED
2298 // For direct threaded, jsr and jsr_w are the same.
2300 pc_t next = pc - 1 + get4 (pc);
2302 PUSHA ((jobject) pc);
2306 #endif /* DIRECT_THREADED */
2309 pc_t next = GOTO_VAL();
2311 PUSHA ((jobject) pc);
2318 jint index = GET1U ();
2319 pc = (pc_t) PEEKA (index);
2325 #ifdef DIRECT_THREADED
2326 void *def = (pc++)->datum;
2330 jint low = INTVAL ();
2331 jint high = INTVAL ();
2333 if (index < low || index > high)
2334 pc = (insn_slot *) def;
2336 pc = (insn_slot *) ((pc + index - low)->datum);
2338 pc_t base_pc = pc - 1;
2339 int index = POPI ();
2341 pc_t base = (pc_t) bytecode ();
2342 while ((pc - base) % 4 != 0)
2345 jint def = get4 (pc);
2346 jint low = get4 (pc + 4);
2347 jint high = get4 (pc + 8);
2348 if (index < low || index > high)
2351 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2352 #endif /* DIRECT_THREADED */
2358 #ifdef DIRECT_THREADED
2359 void *def = (pc++)->insn;
2363 jint npairs = INTVAL ();
2365 int max = npairs - 1;
2368 // Simple binary search...
2371 int half = (min + max) / 2;
2372 int match = pc[2 * half].int_val;
2377 pc = (insn_slot *) pc[2 * half + 1].datum;
2380 else if (index < match)
2381 // We can use HALF - 1 here because we check again on
2385 // We can use HALF + 1 here because we check again on
2389 if (index == pc[2 * min].int_val)
2390 pc = (insn_slot *) pc[2 * min + 1].datum;
2392 pc = (insn_slot *) def;
2394 unsigned char *base_pc = pc-1;
2397 unsigned char* base = bytecode ();
2398 while ((pc-base) % 4 != 0)
2401 jint def = get4 (pc);
2402 jint npairs = get4 (pc+4);
2407 // Simple binary search...
2410 int half = (min+max)/2;
2411 int match = get4 (pc+ 4*(2 + 2*half));
2415 else if (index < match)
2416 // We can use HALF - 1 here because we check again on
2420 // We can use HALF + 1 here because we check again on
2425 if (index == get4 (pc+ 4*(2 + 2*min)))
2426 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2429 #endif /* DIRECT_THREADED */
2434 *(jobject *) retp = POPA ();
2438 *(jlong *) retp = POPL ();
2442 *(jfloat *) retp = POPF ();
2446 *(jdouble *) retp = POPD ();
2450 *(jint *) retp = POPI ();
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;
2463 if ((field->flags & Modifier::STATIC) == 0)
2464 throw_incompatible_class_change_error
2465 (JvNewStringLatin1 ("field no longer static"));
2467 jclass type = field->type;
2469 // We rewrite the instruction once we discover what it refers
2471 void *newinsn = NULL;
2472 if (type->isPrimitive ())
2474 switch (type->size_in_bytes)
2477 PUSHI (*field->u.byte_addr);
2478 newinsn = AMPAMP (getstatic_resolved_1);
2482 if (type == JvPrimClass (char))
2484 PUSHI (*field->u.char_addr);
2485 newinsn = AMPAMP (getstatic_resolved_char);
2489 PUSHI (*field->u.short_addr);
2490 newinsn = AMPAMP (getstatic_resolved_short);
2495 PUSHI(*field->u.int_addr);
2496 newinsn = AMPAMP (getstatic_resolved_4);
2500 PUSHL(*field->u.long_addr);
2501 newinsn = AMPAMP (getstatic_resolved_8);
2507 PUSHA(*field->u.object_addr);
2508 newinsn = AMPAMP (getstatic_resolved_obj);
2511 #ifdef DIRECT_THREADED
2512 pc[-2].insn = newinsn;
2513 pc[-1].datum = field->u.addr;
2514 #endif /* DIRECT_THREADED */
2518 #ifdef DIRECT_THREADED
2519 getstatic_resolved_1:
2520 PUSHI (*(jbyte *) AVAL ());
2523 getstatic_resolved_char:
2524 PUSHI (*(jchar *) AVAL ());
2527 getstatic_resolved_short:
2528 PUSHI (*(jshort *) AVAL ());
2531 getstatic_resolved_4:
2532 PUSHI (*(jint *) AVAL ());
2535 getstatic_resolved_8:
2536 PUSHL (*(jlong *) AVAL ());
2539 getstatic_resolved_obj:
2540 PUSHA (*(jobject *) AVAL ());
2542 #endif /* DIRECT_THREADED */
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;
2550 if ((field->flags & Modifier::STATIC) != 0)
2551 throw_incompatible_class_change_error
2552 (JvNewStringLatin1 ("field is static"));
2554 jclass type = field->type;
2555 jint field_offset = field->u.boffset;
2556 if (field_offset > 0xffff)
2557 throw new java::lang::VirtualMachineError;
2559 jobject obj = POPA();
2562 void *newinsn = NULL;
2563 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2564 if (type->isPrimitive ())
2566 switch (type->size_in_bytes)
2569 PUSHI (val->byte_value);
2570 newinsn = AMPAMP (getfield_resolved_1);
2574 if (type == JvPrimClass (char))
2576 PUSHI (val->char_value);
2577 newinsn = AMPAMP (getfield_resolved_char);
2581 PUSHI (val->short_value);
2582 newinsn = AMPAMP (getfield_resolved_short);
2587 PUSHI (val->int_value);
2588 newinsn = AMPAMP (getfield_resolved_4);
2592 PUSHL (val->long_value);
2593 newinsn = AMPAMP (getfield_resolved_8);
2599 PUSHA (val->object_value);
2600 newinsn = AMPAMP (getfield_resolved_obj);
2603 #ifdef DIRECT_THREADED
2604 pc[-2].insn = newinsn;
2605 pc[-1].int_val = field_offset;
2606 #endif /* DIRECT_THREADED */
2610 #ifdef DIRECT_THREADED
2611 getfield_resolved_1:
2613 char *obj = (char *) POPA ();
2615 PUSHI (*(jbyte *) (obj + INTVAL ()));
2619 getfield_resolved_char:
2621 char *obj = (char *) POPA ();
2623 PUSHI (*(jchar *) (obj + INTVAL ()));
2627 getfield_resolved_short:
2629 char *obj = (char *) POPA ();
2631 PUSHI (*(jshort *) (obj + INTVAL ()));
2635 getfield_resolved_4:
2637 char *obj = (char *) POPA ();
2639 PUSHI (*(jint *) (obj + INTVAL ()));
2643 getfield_resolved_8:
2645 char *obj = (char *) POPA ();
2647 PUSHL (*(jlong *) (obj + INTVAL ()));
2651 getfield_resolved_obj:
2653 char *obj = (char *) POPA ();
2655 PUSHA (*(jobject *) (obj + INTVAL ()));
2658 #endif /* DIRECT_THREADED */
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;
2666 jclass type = field->type;
2668 // ResolvePoolEntry cannot check this
2669 if ((field->flags & Modifier::STATIC) == 0)
2670 throw_incompatible_class_change_error
2671 (JvNewStringLatin1 ("field no longer static"));
2673 void *newinsn = NULL;
2674 if (type->isPrimitive ())
2676 switch (type->size_in_bytes)
2680 jint value = POPI();
2681 *field->u.byte_addr = value;
2682 newinsn = AMPAMP (putstatic_resolved_1);
2688 jint value = POPI();
2689 *field->u.char_addr = value;
2690 newinsn = AMPAMP (putstatic_resolved_2);
2696 jint value = POPI();
2697 *field->u.int_addr = value;
2698 newinsn = AMPAMP (putstatic_resolved_4);
2704 jlong value = POPL();
2705 *field->u.long_addr = value;
2706 newinsn = AMPAMP (putstatic_resolved_8);
2713 jobject value = POPA();
2714 *field->u.object_addr = value;
2715 newinsn = AMPAMP (putstatic_resolved_obj);
2718 #ifdef DIRECT_THREADED
2719 pc[-2].insn = newinsn;
2720 pc[-1].datum = field->u.addr;
2721 #endif /* DIRECT_THREADED */
2725 #ifdef DIRECT_THREADED
2726 putstatic_resolved_1:
2727 *(jbyte *) AVAL () = POPI ();
2730 putstatic_resolved_2:
2731 *(jchar *) AVAL () = POPI ();
2734 putstatic_resolved_4:
2735 *(jint *) AVAL () = POPI ();
2738 putstatic_resolved_8:
2739 *(jlong *) AVAL () = POPL ();
2742 putstatic_resolved_obj:
2743 *(jobject *) AVAL () = POPA ();
2745 #endif /* DIRECT_THREADED */
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;
2753 jclass type = field->type;
2755 if ((field->flags & Modifier::STATIC) != 0)
2756 throw_incompatible_class_change_error
2757 (JvNewStringLatin1 ("field is static"));
2759 jint field_offset = field->u.boffset;
2760 if (field_offset > 0xffff)
2761 throw new java::lang::VirtualMachineError;
2763 void *newinsn = NULL;
2764 if (type->isPrimitive ())
2766 switch (type->size_in_bytes)
2770 jint value = POPI();
2771 jobject obj = POPA();
2773 *(jbyte*) ((char*)obj + field_offset) = value;
2774 newinsn = AMPAMP (putfield_resolved_1);
2780 jint value = POPI();
2781 jobject obj = POPA();
2783 *(jchar*) ((char*)obj + field_offset) = value;
2784 newinsn = AMPAMP (putfield_resolved_2);
2790 jint value = POPI();
2791 jobject obj = POPA();
2793 *(jint*) ((char*)obj + field_offset) = value;
2794 newinsn = AMPAMP (putfield_resolved_4);
2800 jlong value = POPL();
2801 jobject obj = POPA();
2803 *(jlong*) ((char*)obj + field_offset) = value;
2804 newinsn = AMPAMP (putfield_resolved_8);
2811 jobject value = POPA();
2812 jobject obj = POPA();
2814 *(jobject*) ((char*)obj + field_offset) = value;
2815 newinsn = AMPAMP (putfield_resolved_obj);
2818 #ifdef DIRECT_THREADED
2819 pc[-2].insn = newinsn;
2820 pc[-1].int_val = field_offset;
2821 #endif /* DIRECT_THREADED */
2825 #ifdef DIRECT_THREADED
2826 putfield_resolved_1:
2829 char *obj = (char *) POPA ();
2831 *(jbyte *) (obj + INTVAL ()) = val;
2835 putfield_resolved_2:
2838 char *obj = (char *) POPA ();
2840 *(jchar *) (obj + INTVAL ()) = val;
2844 putfield_resolved_4:
2847 char *obj = (char *) POPA ();
2849 *(jint *) (obj + INTVAL ()) = val;
2853 putfield_resolved_8:
2855 jlong val = POPL ();
2856 char *obj = (char *) POPA ();
2858 *(jlong *) (obj + INTVAL ()) = val;
2862 putfield_resolved_obj:
2864 jobject val = POPA ();
2865 char *obj = (char *) POPA ();
2867 *(jobject *) (obj + INTVAL ()) = val;
2870 #endif /* DIRECT_THREADED */
2874 int index = GET2U ();
2876 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2879 sp -= rmeth->stack_item_count;
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.
2886 throw new java::lang::NullPointerException;
2889 fun = (void (*)()) rmeth->method->ncode;
2891 #ifdef DIRECT_THREADED
2892 // Rewrite instruction so that we use a faster pre-resolved
2894 pc[-2].insn = &&invokespecial_resolved;
2895 pc[-1].datum = rmeth;
2896 #endif /* DIRECT_THREADED */
2898 goto perform_invoke;
2900 #ifdef DIRECT_THREADED
2901 invokespecial_resolved:
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.
2910 throw new java::lang::NullPointerException;
2912 fun = (void (*)()) rmeth->method->ncode;
2914 goto perform_invoke;
2915 #endif /* DIRECT_THREADED */
2919 int index = GET2U ();
2921 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2924 sp -= rmeth->stack_item_count;
2926 fun = (void (*)()) rmeth->method->ncode;
2928 #ifdef DIRECT_THREADED
2929 // Rewrite instruction so that we use a faster pre-resolved
2931 pc[-2].insn = &&invokestatic_resolved;
2932 pc[-1].datum = rmeth;
2933 #endif /* DIRECT_THREADED */
2935 goto perform_invoke;
2937 #ifdef DIRECT_THREADED
2938 invokestatic_resolved:
2940 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2941 sp -= rmeth->stack_item_count;
2942 fun = (void (*)()) rmeth->method->ncode;
2944 goto perform_invoke;
2945 #endif /* DIRECT_THREADED */
2947 insn_invokeinterface:
2949 int index = GET2U ();
2951 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2954 sp -= rmeth->stack_item_count;
2956 jobject rcv = sp[0].o;
2961 _Jv_LookupInterfaceMethod (rcv->getClass (),
2962 rmeth->method->name,
2963 rmeth->method->signature);
2965 #ifdef DIRECT_THREADED
2966 // Rewrite instruction so that we use a faster pre-resolved
2968 pc[-2].insn = &&invokeinterface_resolved;
2969 pc[-1].datum = rmeth;
2971 // Skip dummy bytes.
2973 #endif /* DIRECT_THREADED */
2975 goto perform_invoke;
2977 #ifdef DIRECT_THREADED
2978 invokeinterface_resolved:
2980 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2981 sp -= rmeth->stack_item_count;
2982 jobject rcv = sp[0].o;
2985 _Jv_LookupInterfaceMethod (rcv->getClass (),
2986 rmeth->method->name,
2987 rmeth->method->signature);
2989 goto perform_invoke;
2990 #endif /* DIRECT_THREADED */
2994 int index = GET2U ();
2995 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
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);
3004 #ifdef DIRECT_THREADED
3005 pc[-2].insn = &&new_resolved;
3006 pc[-1].datum = klass;
3007 #endif /* DIRECT_THREADED */
3011 #ifdef DIRECT_THREADED
3014 jclass klass = (jclass) AVAL ();
3015 jobject res = _Jv_AllocObject (klass);
3019 #endif /* DIRECT_THREADED */
3023 int atype = GET1U ();
3025 jobject result = _Jv_NewArray (atype, size);
3032 int index = GET2U ();
3033 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3036 jobject result = _Jv_NewObjectArray (size, klass, 0);
3039 #ifdef DIRECT_THREADED
3040 pc[-2].insn = &&anewarray_resolved;
3041 pc[-1].datum = klass;
3042 #endif /* DIRECT_THREADED */
3046 #ifdef DIRECT_THREADED
3049 jclass klass = (jclass) AVAL ();
3051 jobject result = _Jv_NewObjectArray (size, klass, 0);
3055 #endif /* DIRECT_THREADED */
3059 __JArray *arr = (__JArray*)POPA();
3060 NULLARRAYCHECK (arr);
3061 PUSHI (arr->length);
3067 jobject value = POPA();
3068 throw static_cast<jthrowable>(value);
3075 jobject value = POPA();
3076 jint index = GET2U ();
3077 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3080 value = (jobject) _Jv_CheckCast (to, value);
3084 #ifdef DIRECT_THREADED
3085 pc[-2].insn = &&checkcast_resolved;
3087 #endif /* DIRECT_THREADED */
3091 #ifdef DIRECT_THREADED
3095 jobject value = POPA ();
3096 jclass to = (jclass) AVAL ();
3097 value = (jobject) _Jv_CheckCast (to, value);
3101 #endif /* DIRECT_THREADED */
3106 jobject value = POPA();
3107 jint index = GET2U ();
3108 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3110 PUSHI (to->isInstance (value));
3112 #ifdef DIRECT_THREADED
3113 pc[-2].insn = &&instanceof_resolved;
3115 #endif /* DIRECT_THREADED */
3119 #ifdef DIRECT_THREADED
3120 instanceof_resolved:
3122 jobject value = POPA ();
3123 jclass to = (jclass) AVAL ();
3124 PUSHI (to->isInstance (value));
3127 #endif /* DIRECT_THREADED */
3131 jobject value = POPA();
3133 _Jv_MonitorEnter (value);
3139 jobject value = POPA();
3141 _Jv_MonitorExit (value);
3147 jobject val = POPA();
3157 jobject val = POPA();
3165 insn_multianewarray:
3167 int kind_index = GET2U ();
3171 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3173 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3175 for (int i = dim - 1; i >= 0; i--)
3180 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3186 #ifndef DIRECT_THREADED
3189 jint the_mod_op = get1u (pc++);
3190 jint wide = get2u (pc); pc += 2;
3235 pc = (unsigned char*) PEEKA (wide);
3240 jint amount = get2s (pc); pc += 2;
3241 jint value = PEEKI (wide);
3242 POKEI (wide, value+amount);
3247 throw_internal_error ("illegal bytecode modified by wide");
3251 #endif /* DIRECT_THREADED */
3253 catch (java::lang::Throwable *ex)
3255 #ifdef DIRECT_THREADED
3256 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3258 int logical_pc = pc - 1 - bytecode ();
3260 _Jv_InterpException *exc = meth->exceptions ();
3261 jclass exc_class = ex->getClass ();
3263 for (int i = 0; i < meth->exc_count; i++)
3265 if (PCVAL (exc[i].start_pc) <= logical_pc
3266 && logical_pc < PCVAL (exc[i].end_pc))
3268 #ifdef DIRECT_THREADED
3269 jclass handler = (jclass) exc[i].handler_type.p;
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 */
3277 if (handler == NULL || handler->isAssignableFrom (exc_class))
3279 #ifdef DIRECT_THREADED
3280 pc = (insn_slot *) exc[i].handler_pc.p;
3282 pc = bytecode () + exc[i].handler_pc.i;
3283 #endif /* DIRECT_THREADED */
3285 sp++->o = ex; // Push exception.
3291 // No handler, so re-throw.
3297 throw_internal_error (char *msg)
3299 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3303 throw_incompatible_class_change_error (jstring msg)
3305 throw new java::lang::IncompatibleClassChangeError (msg);
3309 static java::lang::NullPointerException *null_pointer_exc;
3311 throw_null_pointer_exception ()
3313 if (null_pointer_exc == NULL)
3314 null_pointer_exc = new java::lang::NullPointerException;
3316 throw null_pointer_exc;
3320 /* Look up source code line number for given bytecode (or direct threaded
3323 _Jv_InterpMethod::get_source_line(pc_t mpc)
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)
3330 line = line_table[i].line;
3335 /** Do static initialization for fields with a constant initializer */
3337 _Jv_InitField (jobject obj, jclass klass, int index)
3339 using namespace java::lang::reflect;
3341 if (obj != 0 && klass == 0)
3342 klass = obj->getClass ();
3344 if (!_Jv_IsInterpretedClass (klass))
3347 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3349 _Jv_Field * field = (&klass->fields[0]) + index;
3351 if (index > klass->field_count)
3352 throw_internal_error ("field out of range");
3354 int init = iclass->field_initializers[index];
3358 _Jv_Constants *pool = &klass->constants;
3359 int tag = pool->tags[init];
3361 if (! field->isResolved ())
3362 throw_internal_error ("initializing unresolved field");
3364 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3365 throw_internal_error ("initializing non-static field with no object");
3369 if ((field->flags & Modifier::STATIC) != 0)
3370 addr = (void*) field->u.addr;
3372 addr = (void*) (((char*)obj) + field->u.boffset);
3376 case JV_CONSTANT_String:
3379 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3380 pool->data[init].string = str;
3381 pool->tags[init] = JV_CONSTANT_ResolvedString;
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");
3390 *(jstring*)addr = pool->data[init].string;
3393 case JV_CONSTANT_Integer:
3395 int value = pool->data[init].i;
3397 if (field->type == JvPrimClass (boolean))
3398 *(jboolean*)addr = (jboolean)value;
3400 else if (field->type == JvPrimClass (byte))
3401 *(jbyte*)addr = (jbyte)value;
3403 else if (field->type == JvPrimClass (char))
3404 *(jchar*)addr = (jchar)value;
3406 else if (field->type == JvPrimClass (short))
3407 *(jshort*)addr = (jshort)value;
3409 else if (field->type == JvPrimClass (int))
3410 *(jint*)addr = (jint)value;
3413 throw_class_format_error ("erroneous field initializer");
3417 case JV_CONSTANT_Long:
3418 if (field->type != JvPrimClass (long))
3419 throw_class_format_error ("erroneous field initializer");
3421 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3424 case JV_CONSTANT_Float:
3425 if (field->type != JvPrimClass (float))
3426 throw_class_format_error ("erroneous field initializer");
3428 *(jfloat*)addr = pool->data[init].f;
3431 case JV_CONSTANT_Double:
3432 if (field->type != JvPrimClass (double))
3433 throw_class_format_error ("erroneous field initializer");
3435 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3439 throw_class_format_error ("erroneous field initializer");
3443 inline static unsigned char*
3444 skip_one_type (unsigned char* ptr)
3455 do { ch = *ptr++; } while (ch != ';');
3462 get_ffi_type_from_signature (unsigned char* ptr)
3468 return &ffi_type_pointer;
3472 // On some platforms a bool is a byte, on others an int.
3473 if (sizeof (jboolean) == sizeof (jbyte))
3474 return &ffi_type_sint8;
3477 JvAssert (sizeof (jbyte) == sizeof (jint));
3478 return &ffi_type_sint32;
3483 return &ffi_type_sint8;
3487 return &ffi_type_uint16;
3491 return &ffi_type_sint16;
3495 return &ffi_type_sint32;
3499 return &ffi_type_sint64;
3503 return &ffi_type_float;
3507 return &ffi_type_double;
3511 return &ffi_type_void;
3515 throw_internal_error ("unknown type in signature");
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 */
3523 _Jv_count_arguments (_Jv_Utf8Const *signature,
3526 unsigned char *ptr = (unsigned char*) signature->chars();
3527 int arg_count = staticp ? 0 : 1;
3529 /* first, count number of arguments */
3537 ptr = skip_one_type (ptr);
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
3550 init_cif (_Jv_Utf8Const* signature,
3554 ffi_type **arg_types,
3557 unsigned char *ptr = (unsigned char*) signature->chars();
3559 int arg_index = 0; // arg number
3560 int item_count = 0; // stack-item count
3565 arg_types[arg_index++] = &ffi_type_pointer;
3575 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3577 if (*ptr == 'J' || *ptr == 'D')
3582 ptr = skip_one_type (ptr);
3587 ffi_type *rtype = get_ffi_type_from_signature (ptr);
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");
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");
3597 if (rtype_p != NULL)
3603 #if FFI_NATIVE_RAW_API
3604 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3605 # define FFI_RAW_SIZE ffi_raw_size
3607 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3608 # define FFI_RAW_SIZE ffi_java_raw_size
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. */
3617 ffi_raw_closure closure;
3619 ffi_type *arg_types[0];
3622 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3625 _Jv_InterpMethod::ncode ()
3627 using namespace java::lang::reflect;
3629 if (self->ncode != 0)
3632 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3633 int arg_count = _Jv_count_arguments (self->signature, staticp);
3635 ncode_closure *closure =
3636 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3637 + arg_count * sizeof (ffi_type*));
3639 init_cif (self->signature,
3643 &closure->arg_types[0],
3646 ffi_closure_fun fun;
3648 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3650 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3652 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3655 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3657 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3662 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3664 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3667 FFI_PREP_RAW_CLOSURE (&closure->closure,
3672 self->ncode = (void*)closure;
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. */
3680 _Jv_InterpMethod::insn_index (pc_t pc)
3683 jlong right = number_insn_slots;
3684 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3688 jlong mid = (left + right) / 2;
3689 if (&slots[mid] == pc)
3692 if (pc < &slots[mid])
3700 #endif // DIRECT_THREADED
3703 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
3704 jintArray& line_numbers,
3705 jlongArray& code_indices)
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
3714 _Jv_CompileMethod (this);
3716 if (line_table_len > 0)
3719 end = number_insn_slots;
3720 line_numbers = JvNewIntArray (line_table_len);
3721 code_indices = JvNewLongArray (line_table_len);
3723 jint* lines = elements (line_numbers);
3724 jlong* indices = elements (code_indices);
3725 for (int i = 0; i < line_table_len; ++i)
3727 lines[i] = line_table[i].line;
3728 indices[i] = insn_index (line_table[i].pc);
3731 #else // !DIRECT_THREADED
3732 if (line_table_len > 0)
3736 line_numbers = JvNewIntArray (line_table_len);
3737 code_indices = JvNewLongArray (line_table_len);
3739 jint* lines = elements (line_numbers);
3740 jlong* indices = elements (code_indices);
3741 for (int i = 0; i < line_table_len; ++i)
3743 lines[i] = line_table[i].line;
3744 indices[i] = (jlong) line_table[i].bytecode_pc;
3747 #endif // !DIRECT_THREADED
3751 _Jv_JNIMethod::ncode ()
3753 using namespace java::lang::reflect;
3755 if (self->ncode != 0)
3758 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3759 int arg_count = _Jv_count_arguments (self->signature, staticp);
3761 ncode_closure *closure =
3762 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3763 + arg_count * sizeof (ffi_type*));
3766 init_cif (self->signature,
3770 &closure->arg_types[0],
3773 ffi_closure_fun fun;
3775 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3777 // Initialize the argument types and CIF that represent the actual
3778 // underlying JNI function.
3780 if ((self->accflags & Modifier::STATIC))
3782 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3783 * sizeof (ffi_type *));
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 *));
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");
3796 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3798 // FIXME: for now we assume that all native methods for
3799 // interpreted code use JNI.
3800 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3802 FFI_PREP_RAW_CLOSURE (&closure->closure,
3807 self->ncode = (void *) closure;
3812 throw_class_format_error (jstring msg)
3815 ? new java::lang::ClassFormatError (msg)
3816 : new java::lang::ClassFormatError);
3820 throw_class_format_error (char *msg)
3822 throw_class_format_error (JvNewStringLatin1 (msg));
3828 _Jv_InterpreterEngine::do_verify (jclass klass)
3830 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3831 for (int i = 0; i < klass->method_count; i++)
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)
3838 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3839 _Jv_VerifyMethod (im);
3845 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3847 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3848 for (int i = 0; i < klass->method_count; i++)
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)
3858 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3860 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
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 ();
3870 else if (imeth != 0) // it could be abstract
3872 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3873 klass->methods[i].ncode = im->ncode ();
3879 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3883 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
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);
3890 for (int i = 0; i < klass->field_count; i++)
3892 _Jv_Field *field = &klass->fields[i];
3894 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3897 char *base = field->isRef() ? reference_fields : non_reference_fields;
3898 field->u.addr = base + field->u.boffset;
3900 if (iclass->field_initializers[i] != 0)
3902 _Jv_Linker::resolve_field (field, klass->loader);
3903 _Jv_InitField (0, klass, i);
3907 // Now we don't need the field_initializers anymore, so let the
3908 // collector get rid of it.
3909 iclass->field_initializers = 0;
3912 _Jv_ResolvedMethod *
3913 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3914 jboolean staticp, jint vtable_index)
3916 int arg_count = _Jv_count_arguments (method->signature, staticp);
3918 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3919 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3920 + arg_count*sizeof (ffi_type*));
3922 result->stack_item_count
3923 = init_cif (method->signature,
3927 &result->arg_types[0],
3930 result->vtable_index = vtable_index;
3931 result->method = method;
3932 result->klass = klass;
3938 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3940 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3941 for (int i = 0; i < klass->method_count; i++)
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)
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];
3956 #ifdef DIRECT_THREADED
3958 _Jv_CompileMethod (_Jv_InterpMethod* method)
3960 if (method->prepared == NULL)
3961 _Jv_InterpMethod::run (NULL, NULL, method);
3963 #endif // DIRECT_THREADED
3965 #endif // INTERPRETER