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 (const 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 (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;
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, 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[] =
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),
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),
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),
991 INSN_LABEL(tableswitch),
992 INSN_LABEL(lookupswitch),
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),
1009 INSN_LABEL(newarray),
1010 INSN_LABEL(anewarray),
1011 INSN_LABEL(arraylength),
1013 INSN_LABEL(checkcast),
1014 INSN_LABEL(instanceof),
1015 INSN_LABEL(monitorenter),
1016 INSN_LABEL(monitorexit),
1017 #ifdef DIRECT_THREADED
1022 INSN_LABEL(multianewarray),
1024 INSN_LABEL(ifnonnull),
1027 #ifdef DIRECT_THREADED
1028 INSN_LABEL (ldc_class)
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
1067 pc = (insn_slot *) meth->prepared;
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
1080 ({ int index = get1u (pc++); \
1081 resolve_pool_entry (meth->defining_class, index).o; })
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
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
1109 _Jv_ResolvedMethod* rmeth;
1114 // We keep nop around. It is used if we're interpreting the
1115 // bytecodes and not doing direct threading.
1119 /* The first few instructions here are ordered according to their
1120 frequency, in the hope that this will improve code locality a
1123 insn_aload_0: // 0x2a
1131 insn_iload_1: // 0x1b
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. */
1146 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
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
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;
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
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
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;
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 */
1215 /* here goes the magic again... */
1216 ffi_cif *cif = &rmeth->cif;
1217 ffi_raw *raw = (ffi_raw*) sp;
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);
1226 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
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)
1249 case FFI_TYPE_SINT8:
1250 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1253 case FFI_TYPE_SINT16:
1254 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1257 case FFI_TYPE_UINT16:
1258 PUSHI (rvalue.int_value & 0xffff);
1261 case FFI_TYPE_FLOAT:
1262 PUSHF (rvalue.float_value);
1265 case FFI_TYPE_DOUBLE:
1266 PUSHD (rvalue.double_value);
1269 case FFI_TYPE_SINT64:
1270 PUSHL (rvalue.long_value);
1274 throw_internal_error ("unknown return type in invokeXXX");
1341 // For direct threaded, bipush and sipush are the same.
1342 #ifndef DIRECT_THREADED
1345 #endif /* DIRECT_THREADED */
1351 // For direct threaded, ldc and ldc_w are the same.
1352 #ifndef DIRECT_THREADED
1353 PUSHA ((jobject) AVAL1U ());
1355 #endif /* DIRECT_THREADED */
1357 PUSHA ((jobject) AVAL2U ());
1360 #ifdef DIRECT_THREADED
1361 // For direct threaded we have a separate 'ldc class' operation.
1364 // We could rewrite the instruction at this point.
1365 int index = INTVAL ();
1366 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1371 #endif /* DIRECT_THREADED */
1375 void *where = AVAL2UP ();
1376 memcpy (sp, where, 2*sizeof (_Jv_word));
1471 jint index = POPI();
1472 jintArray arr = (jintArray) POPA();
1473 NULLARRAYCHECK (arr);
1474 ARRAYBOUNDSCHECK (arr, index);
1475 PUSHI( elements(arr)[index] );
1481 jint index = POPI();
1482 jlongArray arr = (jlongArray) POPA();
1483 NULLARRAYCHECK (arr);
1484 ARRAYBOUNDSCHECK (arr, index);
1485 PUSHL( elements(arr)[index] );
1491 jint index = POPI();
1492 jfloatArray arr = (jfloatArray) POPA();
1493 NULLARRAYCHECK (arr);
1494 ARRAYBOUNDSCHECK (arr, index);
1495 PUSHF( elements(arr)[index] );
1501 jint index = POPI();
1502 jdoubleArray arr = (jdoubleArray) POPA();
1503 NULLARRAYCHECK (arr);
1504 ARRAYBOUNDSCHECK (arr, index);
1505 PUSHD( elements(arr)[index] );
1511 jint index = POPI();
1512 jobjectArray arr = (jobjectArray) POPA();
1513 NULLARRAYCHECK (arr);
1514 ARRAYBOUNDSCHECK (arr, index);
1515 PUSHA( elements(arr)[index] );
1521 jint index = POPI();
1522 jbyteArray arr = (jbyteArray) POPA();
1523 NULLARRAYCHECK (arr);
1524 ARRAYBOUNDSCHECK (arr, index);
1525 PUSHI( elements(arr)[index] );
1531 jint index = POPI();
1532 jcharArray arr = (jcharArray) POPA();
1533 NULLARRAYCHECK (arr);
1534 ARRAYBOUNDSCHECK (arr, index);
1535 PUSHI( elements(arr)[index] );
1541 jint index = POPI();
1542 jshortArray arr = (jshortArray) POPA();
1543 NULLARRAYCHECK (arr);
1544 ARRAYBOUNDSCHECK (arr, index);
1545 PUSHI( elements(arr)[index] );
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;
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;
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;
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;
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;
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;
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;
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;
1752 dupx (sp, 1, 1); sp+=1;
1756 dupx (sp, 1, 2); sp+=1;
1766 dupx (sp, 2, 1); sp+=2;
1770 dupx (sp, 2, 2); sp+=2;
1775 jobject tmp1 = POPA();
1776 jobject tmp2 = POPA();
1832 jint value2 = POPI();
1833 jint value1 = POPI();
1834 jint res = _Jv_divI (value1, value2);
1841 jlong value2 = POPL();
1842 jlong value1 = POPL();
1843 jlong res = _Jv_divJ (value1, value2);
1850 jfloat value2 = POPF();
1851 jfloat value1 = POPF();
1852 jfloat res = value1 / value2;
1859 jdouble value2 = POPD();
1860 jdouble value1 = POPD();
1861 jdouble res = value1 / value2;
1868 jint value2 = POPI();
1869 jint value1 = POPI();
1870 jint res = _Jv_remI (value1, value2);
1877 jlong value2 = POPL();
1878 jlong value1 = POPL();
1879 jlong res = _Jv_remJ (value1, value2);
1886 jfloat value2 = POPF();
1887 jfloat value1 = POPF();
1888 jfloat res = __ieee754_fmod (value1, value2);
1895 jdouble value2 = POPD();
1896 jdouble value1 = POPD();
1897 jdouble res = __ieee754_fmod (value1, value2);
1904 jint value = POPI();
1911 jlong value = POPL();
1918 jfloat value = POPF();
1925 jdouble value = POPD();
1932 jint shift = (POPI() & 0x1f);
1933 jint value = POPI();
1934 PUSHI (value << shift);
1940 jint shift = (POPI() & 0x3f);
1941 jlong value = POPL();
1942 PUSHL (value << shift);
1948 jint shift = (POPI() & 0x1f);
1949 jint value = POPI();
1950 PUSHI (value >> shift);
1956 jint shift = (POPI() & 0x3f);
1957 jlong value = POPL();
1958 PUSHL (value >> shift);
1964 jint shift = (POPI() & 0x1f);
1965 _Jv_uint value = (_Jv_uint) POPI();
1966 PUSHI ((jint) (value >> shift));
1972 jint shift = (POPI() & 0x3f);
1973 _Jv_ulong value = (_Jv_ulong) POPL();
1974 PUSHL ((jlong) (value >> shift));
2004 jint index = GET1U ();
2005 jint amount = GET1S ();
2006 locals[index].i += amount;
2011 {jlong value = POPI(); PUSHL (value);}
2015 {jfloat value = POPI(); PUSHF (value);}
2019 {jdouble value = POPI(); PUSHD (value);}
2023 {jint value = POPL(); PUSHI (value);}
2027 {jfloat value = POPL(); PUSHF (value);}
2031 {jdouble value = POPL(); PUSHD (value);}
2036 using namespace java::lang;
2037 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2044 using namespace java::lang;
2045 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2051 { jdouble value = POPF (); PUSHD(value); }
2056 using namespace java::lang;
2057 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2064 using namespace java::lang;
2065 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2071 { jfloat value = POPD (); PUSHF(value); }
2075 { jbyte value = POPI (); PUSHI(value); }
2079 { jchar value = POPI (); PUSHI(value); }
2083 { jshort value = POPI (); PUSHI(value); }
2088 jlong value2 = POPL ();
2089 jlong value1 = POPL ();
2090 if (value1 > value2)
2092 else if (value1 == value2)
2108 jfloat value2 = POPF ();
2109 jfloat value1 = POPF ();
2110 if (value1 > value2)
2112 else if (value1 == value2)
2114 else if (value1 < value2)
2130 jdouble value2 = POPD ();
2131 jdouble value1 = POPD ();
2132 if (value1 > value2)
2134 else if (value1 == value2)
2136 else if (value1 < value2)
2199 jint value2 = POPI();
2200 jint value1 = POPI();
2201 if (value1 == value2)
2210 jint value2 = POPI();
2211 jint value1 = POPI();
2212 if (value1 != value2)
2221 jint value2 = POPI();
2222 jint value1 = POPI();
2223 if (value1 < value2)
2232 jint value2 = POPI();
2233 jint value1 = POPI();
2234 if (value1 >= value2)
2243 jint value2 = POPI();
2244 jint value1 = POPI();
2245 if (value1 > value2)
2254 jint value2 = POPI();
2255 jint value1 = POPI();
2256 if (value1 <= value2)
2265 jobject value2 = POPA();
2266 jobject value1 = POPA();
2267 if (value1 == value2)
2276 jobject value2 = POPA();
2277 jobject value1 = POPA();
2278 if (value1 != value2)
2286 #ifndef DIRECT_THREADED
2287 // For direct threaded, goto and goto_w are the same.
2288 pc = pc - 1 + get4 (pc);
2290 #endif /* DIRECT_THREADED */
2296 #ifndef DIRECT_THREADED
2297 // For direct threaded, jsr and jsr_w are the same.
2299 pc_t next = pc - 1 + get4 (pc);
2301 PUSHA ((jobject) pc);
2305 #endif /* DIRECT_THREADED */
2308 pc_t next = GOTO_VAL();
2310 PUSHA ((jobject) pc);
2317 jint index = GET1U ();
2318 pc = (pc_t) PEEKA (index);
2324 #ifdef DIRECT_THREADED
2325 void *def = (pc++)->datum;
2329 jint low = INTVAL ();
2330 jint high = INTVAL ();
2332 if (index < low || index > high)
2333 pc = (insn_slot *) def;
2335 pc = (insn_slot *) ((pc + index - low)->datum);
2337 pc_t base_pc = pc - 1;
2338 int index = POPI ();
2340 pc_t base = (pc_t) bytecode ();
2341 while ((pc - base) % 4 != 0)
2344 jint def = get4 (pc);
2345 jint low = get4 (pc + 4);
2346 jint high = get4 (pc + 8);
2347 if (index < low || index > high)
2350 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2351 #endif /* DIRECT_THREADED */
2357 #ifdef DIRECT_THREADED
2358 void *def = (pc++)->insn;
2362 jint npairs = INTVAL ();
2364 int max = npairs - 1;
2367 // Simple binary search...
2370 int half = (min + max) / 2;
2371 int match = pc[2 * half].int_val;
2376 pc = (insn_slot *) pc[2 * half + 1].datum;
2379 else if (index < match)
2380 // We can use HALF - 1 here because we check again on
2384 // We can use HALF + 1 here because we check again on
2388 if (index == pc[2 * min].int_val)
2389 pc = (insn_slot *) pc[2 * min + 1].datum;
2391 pc = (insn_slot *) def;
2393 unsigned char *base_pc = pc-1;
2396 unsigned char* base = bytecode ();
2397 while ((pc-base) % 4 != 0)
2400 jint def = get4 (pc);
2401 jint npairs = get4 (pc+4);
2406 // Simple binary search...
2409 int half = (min+max)/2;
2410 int match = get4 (pc+ 4*(2 + 2*half));
2414 else if (index < match)
2415 // We can use HALF - 1 here because we check again on
2419 // We can use HALF + 1 here because we check again on
2424 if (index == get4 (pc+ 4*(2 + 2*min)))
2425 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2428 #endif /* DIRECT_THREADED */
2433 *(jobject *) retp = POPA ();
2437 *(jlong *) retp = POPL ();
2441 *(jfloat *) retp = POPF ();
2445 *(jdouble *) retp = POPD ();
2449 *(jint *) retp = POPI ();
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
2470 void *newinsn = NULL;
2471 if (type->isPrimitive ())
2473 switch (type->size_in_bytes)
2476 PUSHI (*field->u.byte_addr);
2477 newinsn = AMPAMP (getstatic_resolved_1);
2481 if (type == JvPrimClass (char))
2483 PUSHI (*field->u.char_addr);
2484 newinsn = AMPAMP (getstatic_resolved_char);
2488 PUSHI (*field->u.short_addr);
2489 newinsn = AMPAMP (getstatic_resolved_short);
2494 PUSHI(*field->u.int_addr);
2495 newinsn = AMPAMP (getstatic_resolved_4);
2499 PUSHL(*field->u.long_addr);
2500 newinsn = AMPAMP (getstatic_resolved_8);
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 */
2517 #ifdef DIRECT_THREADED
2518 getstatic_resolved_1:
2519 PUSHI (*(jbyte *) AVAL ());
2522 getstatic_resolved_char:
2523 PUSHI (*(jchar *) AVAL ());
2526 getstatic_resolved_short:
2527 PUSHI (*(jshort *) AVAL ());
2530 getstatic_resolved_4:
2531 PUSHI (*(jint *) AVAL ());
2534 getstatic_resolved_8:
2535 PUSHL (*(jlong *) AVAL ());
2538 getstatic_resolved_obj:
2539 PUSHA (*(jobject *) AVAL ());
2541 #endif /* DIRECT_THREADED */
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();
2561 void *newinsn = NULL;
2562 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2563 if (type->isPrimitive ())
2565 switch (type->size_in_bytes)
2568 PUSHI (val->byte_value);
2569 newinsn = AMPAMP (getfield_resolved_1);
2573 if (type == JvPrimClass (char))
2575 PUSHI (val->char_value);
2576 newinsn = AMPAMP (getfield_resolved_char);
2580 PUSHI (val->short_value);
2581 newinsn = AMPAMP (getfield_resolved_short);
2586 PUSHI (val->int_value);
2587 newinsn = AMPAMP (getfield_resolved_4);
2591 PUSHL (val->long_value);
2592 newinsn = AMPAMP (getfield_resolved_8);
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 */
2609 #ifdef DIRECT_THREADED
2610 getfield_resolved_1:
2612 char *obj = (char *) POPA ();
2614 PUSHI (*(jbyte *) (obj + INTVAL ()));
2618 getfield_resolved_char:
2620 char *obj = (char *) POPA ();
2622 PUSHI (*(jchar *) (obj + INTVAL ()));
2626 getfield_resolved_short:
2628 char *obj = (char *) POPA ();
2630 PUSHI (*(jshort *) (obj + INTVAL ()));
2634 getfield_resolved_4:
2636 char *obj = (char *) POPA ();
2638 PUSHI (*(jint *) (obj + INTVAL ()));
2642 getfield_resolved_8:
2644 char *obj = (char *) POPA ();
2646 PUSHL (*(jlong *) (obj + INTVAL ()));
2650 getfield_resolved_obj:
2652 char *obj = (char *) POPA ();
2654 PUSHA (*(jobject *) (obj + INTVAL ()));
2657 #endif /* DIRECT_THREADED */
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)
2679 jint value = POPI();
2680 *field->u.byte_addr = value;
2681 newinsn = AMPAMP (putstatic_resolved_1);
2687 jint value = POPI();
2688 *field->u.char_addr = value;
2689 newinsn = AMPAMP (putstatic_resolved_2);
2695 jint value = POPI();
2696 *field->u.int_addr = value;
2697 newinsn = AMPAMP (putstatic_resolved_4);
2703 jlong value = POPL();
2704 *field->u.long_addr = value;
2705 newinsn = AMPAMP (putstatic_resolved_8);
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 */
2724 #ifdef DIRECT_THREADED
2725 putstatic_resolved_1:
2726 *(jbyte *) AVAL () = POPI ();
2729 putstatic_resolved_2:
2730 *(jchar *) AVAL () = POPI ();
2733 putstatic_resolved_4:
2734 *(jint *) AVAL () = POPI ();
2737 putstatic_resolved_8:
2738 *(jlong *) AVAL () = POPL ();
2741 putstatic_resolved_obj:
2742 *(jobject *) AVAL () = POPA ();
2744 #endif /* DIRECT_THREADED */
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)
2769 jint value = POPI();
2770 jobject obj = POPA();
2772 *(jbyte*) ((char*)obj + field_offset) = value;
2773 newinsn = AMPAMP (putfield_resolved_1);
2779 jint value = POPI();
2780 jobject obj = POPA();
2782 *(jchar*) ((char*)obj + field_offset) = value;
2783 newinsn = AMPAMP (putfield_resolved_2);
2789 jint value = POPI();
2790 jobject obj = POPA();
2792 *(jint*) ((char*)obj + field_offset) = value;
2793 newinsn = AMPAMP (putfield_resolved_4);
2799 jlong value = POPL();
2800 jobject obj = POPA();
2802 *(jlong*) ((char*)obj + field_offset) = value;
2803 newinsn = AMPAMP (putfield_resolved_8);
2810 jobject value = POPA();
2811 jobject obj = POPA();
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 */
2824 #ifdef DIRECT_THREADED
2825 putfield_resolved_1:
2828 char *obj = (char *) POPA ();
2830 *(jbyte *) (obj + INTVAL ()) = val;
2834 putfield_resolved_2:
2837 char *obj = (char *) POPA ();
2839 *(jchar *) (obj + INTVAL ()) = val;
2843 putfield_resolved_4:
2846 char *obj = (char *) POPA ();
2848 *(jint *) (obj + INTVAL ()) = val;
2852 putfield_resolved_8:
2854 jlong val = POPL ();
2855 char *obj = (char *) POPA ();
2857 *(jlong *) (obj + INTVAL ()) = val;
2861 putfield_resolved_obj:
2863 jobject val = POPA ();
2864 char *obj = (char *) POPA ();
2866 *(jobject *) (obj + INTVAL ()) = val;
2869 #endif /* DIRECT_THREADED */
2873 int index = GET2U ();
2875 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
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.
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
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.
2909 throw new java::lang::NullPointerException;
2911 fun = (void (*)()) rmeth->method->ncode;
2913 goto perform_invoke;
2914 #endif /* DIRECT_THREADED */
2918 int index = GET2U ();
2920 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
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
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,
2953 sp -= rmeth->stack_item_count;
2955 jobject rcv = sp[0].o;
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
2967 pc[-2].insn = &&invokeinterface_resolved;
2968 pc[-1].datum = rmeth;
2970 // Skip dummy bytes.
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;
2984 _Jv_LookupInterfaceMethod (rcv->getClass (),
2985 rmeth->method->name,
2986 rmeth->method->signature);
2988 goto perform_invoke;
2989 #endif /* DIRECT_THREADED */
2993 int index = GET2U ();
2994 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
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);
3003 #ifdef DIRECT_THREADED
3004 pc[-2].insn = &&new_resolved;
3005 pc[-1].datum = klass;
3006 #endif /* DIRECT_THREADED */
3010 #ifdef DIRECT_THREADED
3013 jclass klass = (jclass) AVAL ();
3014 jobject res = _Jv_AllocObject (klass);
3018 #endif /* DIRECT_THREADED */
3022 int atype = GET1U ();
3024 jobject result = _Jv_NewArray (atype, size);
3031 int index = GET2U ();
3032 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3035 jobject result = _Jv_NewObjectArray (size, klass, 0);
3038 #ifdef DIRECT_THREADED
3039 pc[-2].insn = &&anewarray_resolved;
3040 pc[-1].datum = klass;
3041 #endif /* DIRECT_THREADED */
3045 #ifdef DIRECT_THREADED
3048 jclass klass = (jclass) AVAL ();
3050 jobject result = _Jv_NewObjectArray (size, klass, 0);
3054 #endif /* DIRECT_THREADED */
3058 __JArray *arr = (__JArray*)POPA();
3059 NULLARRAYCHECK (arr);
3060 PUSHI (arr->length);
3066 jobject value = POPA();
3067 throw static_cast<jthrowable>(value);
3074 jobject value = POPA();
3075 jint index = GET2U ();
3076 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3079 value = (jobject) _Jv_CheckCast (to, value);
3083 #ifdef DIRECT_THREADED
3084 pc[-2].insn = &&checkcast_resolved;
3086 #endif /* DIRECT_THREADED */
3090 #ifdef DIRECT_THREADED
3094 jobject value = POPA ();
3095 jclass to = (jclass) AVAL ();
3096 value = (jobject) _Jv_CheckCast (to, value);
3100 #endif /* DIRECT_THREADED */
3105 jobject value = POPA();
3106 jint index = GET2U ();
3107 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3109 PUSHI (to->isInstance (value));
3111 #ifdef DIRECT_THREADED
3112 pc[-2].insn = &&instanceof_resolved;
3114 #endif /* DIRECT_THREADED */
3118 #ifdef DIRECT_THREADED
3119 instanceof_resolved:
3121 jobject value = POPA ();
3122 jclass to = (jclass) AVAL ();
3123 PUSHI (to->isInstance (value));
3126 #endif /* DIRECT_THREADED */
3130 jobject value = POPA();
3132 _Jv_MonitorEnter (value);
3138 jobject value = POPA();
3140 _Jv_MonitorExit (value);
3146 jobject val = POPA();
3156 jobject val = POPA();
3164 insn_multianewarray:
3166 int kind_index = GET2U ();
3170 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3172 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3174 for (int i = dim - 1; i >= 0; i--)
3179 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3185 #ifndef DIRECT_THREADED
3188 jint the_mod_op = get1u (pc++);
3189 jint wide = get2u (pc); pc += 2;
3234 pc = (unsigned char*) PEEKA (wide);
3239 jint amount = get2s (pc); pc += 2;
3240 jint value = PEEKI (wide);
3241 POKEI (wide, value+amount);
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);
3257 int logical_pc = pc - 1 - bytecode ();
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;
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;
3281 pc = bytecode () + exc[i].handler_pc.i;
3282 #endif /* DIRECT_THREADED */
3284 sp++->o = ex; // Push exception.
3290 // No handler, so re-throw.
3296 throw_internal_error (const char *msg)
3298 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3302 throw_incompatible_class_change_error (jstring msg)
3304 throw new java::lang::IncompatibleClassChangeError (msg);
3308 static java::lang::NullPointerException *null_pointer_exc;
3310 throw_null_pointer_exception ()
3312 if (null_pointer_exc == NULL)
3313 null_pointer_exc = new java::lang::NullPointerException;
3315 throw null_pointer_exc;
3319 /* Look up source code line number for given bytecode (or direct threaded
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)
3329 line = line_table[i].line;
3334 /** Do static initialization for fields with a constant initializer */
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))
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];
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");
3368 if ((field->flags & Modifier::STATIC) != 0)
3369 addr = (void*) field->u.addr;
3371 addr = (void*) (((char*)obj) + field->u.boffset);
3375 case JV_CONSTANT_String:
3378 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3379 pool->data[init].string = str;
3380 pool->tags[init] = JV_CONSTANT_ResolvedString;
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;
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;
3412 throw_class_format_error ("erroneous field initializer");
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]);
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;
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]);
3438 throw_class_format_error ("erroneous field initializer");
3442 inline static unsigned char*
3443 skip_one_type (unsigned char* ptr)
3454 do { ch = *ptr++; } while (ch != ';');
3461 get_ffi_type_from_signature (unsigned char* ptr)
3467 return &ffi_type_pointer;
3471 // On some platforms a bool is a byte, on others an int.
3472 if (sizeof (jboolean) == sizeof (jbyte))
3473 return &ffi_type_sint8;
3476 JvAssert (sizeof (jbyte) == sizeof (jint));
3477 return &ffi_type_sint32;
3482 return &ffi_type_sint8;
3486 return &ffi_type_uint16;
3490 return &ffi_type_sint16;
3494 return &ffi_type_sint32;
3498 return &ffi_type_sint64;
3502 return &ffi_type_float;
3506 return &ffi_type_double;
3510 return &ffi_type_void;
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 */
3522 _Jv_count_arguments (_Jv_Utf8Const *signature,
3525 unsigned char *ptr = (unsigned char*) signature->chars();
3526 int arg_count = staticp ? 0 : 1;
3528 /* first, count number of arguments */
3536 ptr = skip_one_type (ptr);
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
3549 init_cif (_Jv_Utf8Const* signature,
3553 ffi_type **arg_types,
3556 unsigned char *ptr = (unsigned char*) signature->chars();
3558 int arg_index = 0; // arg number
3559 int item_count = 0; // stack-item count
3564 arg_types[arg_index++] = &ffi_type_pointer;
3574 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3576 if (*ptr == 'J' || *ptr == 'D')
3581 ptr = skip_one_type (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)
3602 #if FFI_NATIVE_RAW_API
3603 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3604 # define FFI_RAW_SIZE ffi_raw_size
3606 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3607 # define FFI_RAW_SIZE ffi_java_raw_size
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. */
3616 ffi_raw_closure closure;
3618 ffi_type *arg_types[0];
3621 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3624 _Jv_InterpMethod::ncode ()
3626 using namespace java::lang::reflect;
3628 if (self->ncode != 0)
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,
3642 &closure->arg_types[0],
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)
3654 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3656 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3661 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3663 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3666 FFI_PREP_RAW_CLOSURE (&closure->closure,
3671 self->ncode = (void*)closure;
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. */
3679 _Jv_InterpMethod::insn_index (pc_t pc)
3682 jlong right = number_insn_slots;
3683 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3687 jlong mid = (left + right) / 2;
3688 if (&slots[mid] == pc)
3691 if (pc < &slots[mid])
3699 #endif // DIRECT_THREADED
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
3713 _Jv_CompileMethod (this);
3715 if (line_table_len > 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)
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
3750 _Jv_JNIMethod::ncode ()
3752 using namespace java::lang::reflect;
3754 if (self->ncode != 0)
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*));
3765 init_cif (self->signature,
3769 &closure->arg_types[0],
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.
3779 if ((self->accflags & Modifier::STATIC))
3781 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3782 * sizeof (ffi_type *));
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,
3806 self->ncode = (void *) closure;
3811 throw_class_format_error (jstring msg)
3814 ? new java::lang::ClassFormatError (msg)
3815 : new java::lang::ClassFormatError);
3819 throw_class_format_error (const char *msg)
3821 throw_class_format_error (JvNewStringLatin1 (msg));
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);
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)
3857 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3859 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
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 ();
3878 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
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. How reference fields are
3886 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
3887 // means that this memory will be scanned conservatively (same
3888 // difference, since we know all the contents here are pointers).
3889 // Then we put pointers into this memory into the 'fields'
3890 // structure. Most of these are interior pointers, which is ok (but
3891 // even so the pointer to the first reference field will be used and
3892 // that is not an interior pointer). The 'fields' array is also
3893 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
3894 // be scanned. A pointer to this array is held by Class and thus
3895 // seen by the collector.
3896 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
3897 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
3899 for (int i = 0; i < klass->field_count; i++)
3901 _Jv_Field *field = &klass->fields[i];
3903 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3906 char *base = field->isRef() ? reference_fields : non_reference_fields;
3907 field->u.addr = base + field->u.boffset;
3909 if (iclass->field_initializers[i] != 0)
3911 _Jv_Linker::resolve_field (field, klass->loader);
3912 _Jv_InitField (0, klass, i);
3916 // Now we don't need the field_initializers anymore, so let the
3917 // collector get rid of it.
3918 iclass->field_initializers = 0;
3921 _Jv_ResolvedMethod *
3922 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3923 jboolean staticp, jint vtable_index)
3925 int arg_count = _Jv_count_arguments (method->signature, staticp);
3927 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3928 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3929 + arg_count*sizeof (ffi_type*));
3931 result->stack_item_count
3932 = init_cif (method->signature,
3936 &result->arg_types[0],
3939 result->vtable_index = vtable_index;
3940 result->method = method;
3941 result->klass = klass;
3947 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3949 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3950 for (int i = 0; i < klass->method_count; i++)
3952 // Just skip abstract methods. This is particularly important
3953 // because we don't resize the interpreted_methods array when
3954 // miranda methods are added to it.
3955 if ((klass->methods[i].accflags
3956 & java::lang::reflect::Modifier::ABSTRACT)
3959 // Miranda method additions mean that the `methods' array moves.
3960 // We cache a pointer into this array, so we have to update.
3961 iclass->interpreted_methods[i]->self = &klass->methods[i];
3965 #ifdef DIRECT_THREADED
3967 _Jv_CompileMethod (_Jv_InterpMethod* method)
3969 if (method->prepared == NULL)
3970 _Jv_InterpMethod::run (NULL, NULL, method);
3972 #endif // DIRECT_THREADED
3974 #endif // INTERPRETER