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/InternalError.h>
29 #include <java/lang/NullPointerException.h>
30 #include <java/lang/ArithmeticException.h>
31 #include <java/lang/IncompatibleClassChangeError.h>
32 #include <java/lang/InstantiationException.h>
33 #include <java/lang/Thread.h>
34 #include <java-insns.h>
35 #include <java-signal.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <execution.h>
38 #include <java/lang/reflect/Modifier.h>
42 // Execution engine for interpreted code.
43 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
49 static void throw_internal_error (const char *msg)
50 __attribute__ ((__noreturn__));
51 static void throw_incompatible_class_change_error (jstring msg)
52 __attribute__ ((__noreturn__));
53 static void throw_null_pointer_exception ()
54 __attribute__ ((__noreturn__));
56 static void throw_class_format_error (jstring msg)
57 __attribute__ ((__noreturn__));
58 static void throw_class_format_error (const char *msg)
59 __attribute__ ((__noreturn__));
61 #ifdef DIRECT_THREADED
62 // Lock to ensure that methods are not compiled concurrently.
63 // We could use a finer-grained lock here, however it is not safe to use
64 // the Class monitor as user code in another thread could hold it.
65 static _Jv_Mutex_t compile_mutex;
70 _Jv_MutexInit (&compile_mutex);
73 void _Jv_InitInterpreter() {}
76 extern "C" double __ieee754_fmod (double,double);
78 static inline void dupx (_Jv_word *sp, int n, int x)
80 // first "slide" n+x elements n to the right
82 for (int i = 0; i < n+x; i++)
84 sp[(top-i)] = sp[(top-i)-n];
87 // next, copy the n top elements, n+x down
88 for (int i = 0; i < n; i++)
90 sp[top-(n+x)-i] = sp[top-i];
94 // Used to convert from floating types to integral types.
95 template<typename TO, typename FROM>
97 convert (FROM val, TO min, TO max)
100 if (val >= (FROM) max)
102 else if (val <= (FROM) min)
111 #define PUSHA(V) (sp++)->o = (V)
112 #define PUSHI(V) (sp++)->i = (V)
113 #define PUSHF(V) (sp++)->f = (V)
114 #if SIZEOF_VOID_P == 8
115 # define PUSHL(V) (sp->l = (V), sp += 2)
116 # define PUSHD(V) (sp->d = (V), sp += 2)
118 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
119 (sp++)->ia[0] = w2.ia[0]; \
120 (sp++)->ia[0] = w2.ia[1]; } while (0)
121 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
122 (sp++)->ia[0] = w2.ia[0]; \
123 (sp++)->ia[0] = w2.ia[1]; } while (0)
126 #define POPA() ((--sp)->o)
127 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
128 #define POPF() ((jfloat) (--sp)->f)
129 #if SIZEOF_VOID_P == 8
130 # define POPL() (sp -= 2, (jlong) sp->l)
131 # define POPD() (sp -= 2, (jdouble) sp->d)
133 # define POPL() ({ _Jv_word2 w2; \
134 w2.ia[1] = (--sp)->ia[0]; \
135 w2.ia[0] = (--sp)->ia[0]; w2.l; })
136 # define POPD() ({ _Jv_word2 w2; \
137 w2.ia[1] = (--sp)->ia[0]; \
138 w2.ia[0] = (--sp)->ia[0]; w2.d; })
141 #define LOADA(I) (sp++)->o = locals[I].o
142 #define LOADI(I) (sp++)->i = locals[I].i
143 #define LOADF(I) (sp++)->f = locals[I].f
144 #if SIZEOF_VOID_P == 8
145 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
146 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
148 # define LOADL(I) do { jint __idx = (I); \
149 (sp++)->ia[0] = locals[__idx].ia[0]; \
150 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
152 # define LOADD(I) LOADL(I)
155 #define STOREA(I) locals[I].o = (--sp)->o
156 #define STOREI(I) locals[I].i = (--sp)->i
157 #define STOREF(I) locals[I].f = (--sp)->f
158 #if SIZEOF_VOID_P == 8
159 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
160 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
162 # define STOREL(I) do { jint __idx = (I); \
163 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
164 locals[__idx].ia[0] = (--sp)->ia[0]; \
166 # define STORED(I) STOREL(I)
169 #define PEEKI(I) (locals+(I))->i
170 #define PEEKA(I) (locals+(I))->o
172 #define POKEI(I,V) ((locals+(I))->i = (V))
175 #define BINOPI(OP) { \
176 jint value2 = POPI(); \
177 jint value1 = POPI(); \
178 PUSHI(value1 OP value2); \
181 #define BINOPF(OP) { \
182 jfloat value2 = POPF(); \
183 jfloat value1 = POPF(); \
184 PUSHF(value1 OP value2); \
187 #define BINOPL(OP) { \
188 jlong value2 = POPL(); \
189 jlong value1 = POPL(); \
190 PUSHL(value1 OP value2); \
193 #define BINOPD(OP) { \
194 jdouble value2 = POPD(); \
195 jdouble value1 = POPD(); \
196 PUSHD(value1 OP value2); \
199 static inline jint get1s(unsigned char* loc) {
200 return *(signed char*)loc;
203 static inline jint get1u(unsigned char* loc) {
207 static inline jint get2s(unsigned char* loc) {
208 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
211 static inline jint get2u(unsigned char* loc) {
212 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
215 static jint get4(unsigned char* loc) {
216 return (((jint)(loc[0])) << 24)
217 | (((jint)(loc[1])) << 16)
218 | (((jint)(loc[2])) << 8)
219 | (((jint)(loc[3])) << 0);
222 #define SAVE_PC() frame_desc.pc = pc
224 // We used to define this conditionally, depending on HANDLE_SEGV.
225 // However, that runs into a problem if a chunk in low memory is
226 // mapped and we try to look at a field near the end of a large
227 // object. See PR 26858 for details. It is, most likely, relatively
228 // inexpensive to simply do this check always.
229 #define NULLCHECK(X) \
230 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
232 // Note that we can still conditionally define NULLARRAYCHECK, since
233 // we know that all uses of an array will first reference the length
234 // field, which is first -- and thus will trigger a SEGV.
236 #define NULLARRAYCHECK(X) SAVE_PC()
238 #define NULLARRAYCHECK(X) \
239 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
242 #define ARRAYBOUNDSCHECK(array, index) \
245 if (((unsigned) index) >= (unsigned) (array->length)) \
246 _Jv_ThrowBadArrayIndex (index); \
251 _Jv_InterpMethod::run_normal (ffi_cif *,
256 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
257 run (ret, args, _this);
261 _Jv_InterpMethod::run_synch_object (ffi_cif *,
266 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
268 jobject rcv = (jobject) args[0].ptr;
269 JvSynchronize mutex (rcv);
271 run (ret, args, _this);
275 _Jv_InterpMethod::run_class (ffi_cif *,
280 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
281 _Jv_InitClass (_this->defining_class);
282 run (ret, args, _this);
286 _Jv_InterpMethod::run_synch_class (ffi_cif *,
291 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
293 jclass sync = _this->defining_class;
294 _Jv_InitClass (sync);
295 JvSynchronize mutex (sync);
297 run (ret, args, _this);
300 #ifdef DIRECT_THREADED
301 // "Compile" a method by turning it from bytecode to direct-threaded
304 _Jv_InterpMethod::compile (const void * const *insn_targets)
306 insn_slot *insns = NULL;
308 unsigned char *codestart = bytecode ();
309 unsigned char *end = codestart + code_length;
310 _Jv_word *pool_data = defining_class->constants.data;
312 #define SET_ONE(Field, Value) \
318 insns[next++].Field = Value; \
322 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
323 #define SET_INT(Value) SET_ONE (int_val, Value)
324 #define SET_DATUM(Value) SET_ONE (datum, Value)
326 // Map from bytecode PC to slot in INSNS.
327 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
328 for (int i = 0; i < code_length; ++i)
331 for (int i = 0; i < 2; ++i)
333 jboolean first_pass = i == 0;
337 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
338 number_insn_slots = next;
342 unsigned char *pc = codestart;
345 int base_pc_val = pc - codestart;
347 pc_mapping[base_pc_val] = next;
349 java_opcode opcode = (java_opcode) *pc++;
351 if (opcode == op_nop)
353 SET_INSN (insn_targets[opcode]);
494 case op_monitorenter:
504 // No argument, nothing else to do.
508 SET_INT (get1s (pc));
514 int index = get1u (pc);
516 // For an unresolved class we want to delay resolution
518 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
521 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
525 SET_DATUM (pool_data[index].o);
541 SET_INT (get1u (pc));
546 SET_INT (get1u (pc));
547 SET_INT (get1s (pc + 1));
553 int index = get2u (pc);
555 // For an unresolved class we want to delay resolution
557 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
560 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
564 SET_DATUM (pool_data[index].o);
570 int index = get2u (pc);
572 SET_DATUM (&pool_data[index]);
577 SET_INT (get2s (pc));
589 case op_invokespecial:
590 case op_invokestatic:
591 case op_invokevirtual:
592 SET_INT (get2u (pc));
596 case op_multianewarray:
597 SET_INT (get2u (pc));
598 SET_INT (get1u (pc + 2));
621 int offset = get2s (pc);
624 int new_pc = base_pc_val + offset;
626 bool orig_was_goto = opcode == op_goto;
628 // Thread jumps. We limit the loop count; this lets
629 // us avoid infinite loops if the bytecode contains
630 // such. `10' is arbitrary.
632 while (codestart[new_pc] == op_goto && count-- > 0)
633 new_pc += get2s (&codestart[new_pc + 1]);
635 // If the jump takes us to a `return' instruction and
636 // the original branch was an unconditional goto, then
637 // we hoist the return.
638 opcode = (java_opcode) codestart[new_pc];
640 && (opcode == op_ireturn || opcode == op_lreturn
641 || opcode == op_freturn || opcode == op_dreturn
642 || opcode == op_areturn || opcode == op_return))
645 SET_INSN (insn_targets[opcode]);
648 SET_DATUM (&insns[pc_mapping[new_pc]]);
654 while ((pc - codestart) % 4 != 0)
657 jint def = get4 (pc);
658 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
664 int high = get4 (pc);
668 for (int i = low; i <= high; ++i)
670 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
676 case op_lookupswitch:
678 while ((pc - codestart) % 4 != 0)
681 jint def = get4 (pc);
682 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
685 jint npairs = get4 (pc);
691 jint match = get4 (pc);
692 jint offset = get4 (pc + 4);
694 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
700 case op_invokeinterface:
702 jint index = get2u (pc);
704 // We ignore the next two bytes.
712 opcode = (java_opcode) get1u (pc);
714 jint val = get2u (pc);
717 // We implement narrow and wide instructions using the
718 // same code in the interpreter. So we rewrite the
719 // instruction slot here.
721 insns[next - 1].insn = (void *) insn_targets[opcode];
724 if (opcode == op_iinc)
726 SET_INT (get2s (pc));
735 jint offset = get4 (pc);
737 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
741 // Some "can't happen" cases that we include for
742 // error-checking purposes.
760 case op_getstatic_2s:
761 case op_getstatic_2u:
772 // Now update exceptions.
773 _Jv_InterpException *exc = exceptions ();
774 for (int i = 0; i < exc_count; ++i)
776 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
777 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
778 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
780 = (_Jv_Linker::resolve_pool_entry (defining_class,
781 exc[i].handler_type.i)).clazz;
782 exc[i].handler_type.p = handler;
785 // Translate entries in the LineNumberTable from bytecode PC's to direct
786 // threaded interpreter instruction values.
787 for (int i = 0; i < line_table_len; i++)
789 int byte_pc = line_table[i].bytecode_pc;
790 // It isn't worth throwing an exception if this table is
791 // corrupted, but at the same time we don't want a crash.
792 if (byte_pc < 0 || byte_pc >= code_length)
794 line_table[i].pc = &insns[pc_mapping[byte_pc]];
799 #endif /* DIRECT_THREADED */
801 /* Run the given method.
802 When args is NULL, don't run anything -- just compile it. */
804 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
806 using namespace java::lang::reflect;
808 // FRAME_DESC registers this particular invocation as the top-most
809 // interpreter frame. This lets the stack tracing code (for
810 // Throwable) print information about the method being interpreted
811 // rather than about the interpreter itself. FRAME_DESC has a
812 // destructor so it cleans up automatically when the interpreter
814 java::lang::Thread *thread = java::lang::Thread::currentThread();
815 _Jv_InterpFrame frame_desc (meth, thread);
817 _Jv_word stack[meth->max_stack];
818 _Jv_word *sp = stack;
820 _Jv_word locals[meth->max_locals];
822 #define INSN_LABEL(op) &&insn_##op
824 static const void *const insn_target[] =
827 INSN_LABEL(aconst_null),
828 INSN_LABEL(iconst_m1),
829 INSN_LABEL(iconst_0),
830 INSN_LABEL(iconst_1),
831 INSN_LABEL(iconst_2),
832 INSN_LABEL(iconst_3),
833 INSN_LABEL(iconst_4),
834 INSN_LABEL(iconst_5),
835 INSN_LABEL(lconst_0),
836 INSN_LABEL(lconst_1),
837 INSN_LABEL(fconst_0),
838 INSN_LABEL(fconst_1),
839 INSN_LABEL(fconst_2),
840 INSN_LABEL(dconst_0),
841 INSN_LABEL(dconst_1),
885 INSN_LABEL(istore_0),
886 INSN_LABEL(istore_1),
887 INSN_LABEL(istore_2),
888 INSN_LABEL(istore_3),
889 INSN_LABEL(lstore_0),
890 INSN_LABEL(lstore_1),
891 INSN_LABEL(lstore_2),
892 INSN_LABEL(lstore_3),
893 INSN_LABEL(fstore_0),
894 INSN_LABEL(fstore_1),
895 INSN_LABEL(fstore_2),
896 INSN_LABEL(fstore_3),
897 INSN_LABEL(dstore_0),
898 INSN_LABEL(dstore_1),
899 INSN_LABEL(dstore_2),
900 INSN_LABEL(dstore_3),
901 INSN_LABEL(astore_0),
902 INSN_LABEL(astore_1),
903 INSN_LABEL(astore_2),
904 INSN_LABEL(astore_3),
985 INSN_LABEL(if_icmpeq),
986 INSN_LABEL(if_icmpne),
987 INSN_LABEL(if_icmplt),
988 INSN_LABEL(if_icmpge),
989 INSN_LABEL(if_icmpgt),
990 INSN_LABEL(if_icmple),
991 INSN_LABEL(if_acmpeq),
992 INSN_LABEL(if_acmpne),
996 INSN_LABEL(tableswitch),
997 INSN_LABEL(lookupswitch),
1000 INSN_LABEL(freturn),
1001 INSN_LABEL(dreturn),
1002 INSN_LABEL(areturn),
1004 INSN_LABEL(getstatic),
1005 INSN_LABEL(putstatic),
1006 INSN_LABEL(getfield),
1007 INSN_LABEL(putfield),
1008 INSN_LABEL(invokevirtual),
1009 INSN_LABEL(invokespecial),
1010 INSN_LABEL(invokestatic),
1011 INSN_LABEL(invokeinterface),
1014 INSN_LABEL(newarray),
1015 INSN_LABEL(anewarray),
1016 INSN_LABEL(arraylength),
1018 INSN_LABEL(checkcast),
1019 INSN_LABEL(instanceof),
1020 INSN_LABEL(monitorenter),
1021 INSN_LABEL(monitorexit),
1022 #ifdef DIRECT_THREADED
1027 INSN_LABEL(multianewarray),
1029 INSN_LABEL(ifnonnull),
1032 #ifdef DIRECT_THREADED
1033 INSN_LABEL (ldc_class)
1041 #ifdef DIRECT_THREADED
1043 #define NEXT_INSN goto *((pc++)->insn)
1044 #define INTVAL() ((pc++)->int_val)
1045 #define AVAL() ((pc++)->datum)
1047 #define GET1S() INTVAL ()
1048 #define GET2S() INTVAL ()
1049 #define GET1U() INTVAL ()
1050 #define GET2U() INTVAL ()
1051 #define AVAL1U() AVAL ()
1052 #define AVAL2U() AVAL ()
1053 #define AVAL2UP() AVAL ()
1054 #define SKIP_GOTO ++pc
1055 #define GOTO_VAL() (insn_slot *) pc->datum
1056 #define PCVAL(unionval) unionval.p
1057 #define AMPAMP(label) &&label
1059 // Compile if we must. NOTE: Double-check locking.
1060 if (meth->prepared == NULL)
1062 _Jv_MutexLock (&compile_mutex);
1063 if (meth->prepared == NULL)
1064 meth->compile (insn_target);
1065 _Jv_MutexUnlock (&compile_mutex);
1068 // If we're only compiling, stop here
1072 pc = (insn_slot *) meth->prepared;
1076 #define NEXT_INSN goto *(insn_target[*pc++])
1078 #define GET1S() get1s (pc++)
1079 #define GET2S() (pc += 2, get2s (pc- 2))
1080 #define GET1U() get1u (pc++)
1081 #define GET2U() (pc += 2, get2u (pc - 2))
1082 // Note that these could be more efficient when not handling 'ldc
1085 ({ int index = get1u (pc++); \
1086 resolve_pool_entry (meth->defining_class, index).o; })
1088 ({ int index = get2u (pc); pc += 2; \
1089 resolve_pool_entry (meth->defining_class, index).o; })
1090 // Note that we don't need to resolve the pool entry here as class
1091 // constants are never wide.
1092 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1093 #define SKIP_GOTO pc += 2
1094 #define GOTO_VAL() pc - 1 + get2s (pc)
1095 #define PCVAL(unionval) unionval.i
1096 #define AMPAMP(label) NULL
1100 #endif /* DIRECT_THREADED */
1102 #define TAKE_GOTO pc = GOTO_VAL ()
1104 /* Go straight at it! the ffi raw format matches the internal
1105 stack representation exactly. At least, that's the idea.
1107 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1109 _Jv_word *pool_data = meth->defining_class->constants.data;
1111 /* These three are temporaries for common code used by several
1114 _Jv_ResolvedMethod* rmeth;
1119 // We keep nop around. It is used if we're interpreting the
1120 // bytecodes and not doing direct threading.
1124 /* The first few instructions here are ordered according to their
1125 frequency, in the hope that this will improve code locality a
1128 insn_aload_0: // 0x2a
1136 insn_iload_1: // 0x1b
1140 insn_invokevirtual: // 0xb6
1142 int index = GET2U ();
1144 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1145 * value already is resolved. If we want to clutter up the
1146 * code here to gain a little performance, then we can check
1147 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1148 * directly. For now, I don't think it is worth it. */
1150 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1153 sp -= rmeth->stack_item_count;
1155 if (rmeth->method->accflags & Modifier::FINAL)
1157 // We can't rely on NULLCHECK working if the method is final.
1160 throw_null_pointer_exception ();
1162 // Final methods might not appear in the vtable.
1163 fun = (void (*)()) rmeth->method->ncode;
1167 NULLCHECK (sp[0].o);
1168 jobject rcv = sp[0].o;
1169 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1170 fun = (void (*)()) table->get_method (rmeth->method->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;
1188 if (rmeth->method->accflags & Modifier::FINAL)
1190 // We can't rely on NULLCHECK working if the method is final.
1193 throw_null_pointer_exception ();
1195 // Final methods might not appear in the vtable.
1196 fun = (void (*)()) rmeth->method->ncode;
1200 jobject rcv = sp[0].o;
1201 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1202 fun = (void (*)()) table->get_method (rmeth->method->index);
1205 goto perform_invoke;
1206 #endif /* DIRECT_THREADED */
1212 /* here goes the magic again... */
1213 ffi_cif *cif = &rmeth->cif;
1214 ffi_raw *raw = (ffi_raw*) sp;
1218 #if FFI_NATIVE_RAW_API
1219 /* We assume that this is only implemented if it's correct */
1220 /* to use it here. On a 64 bit machine, it never is. */
1221 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1223 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1226 int rtype = cif->rtype->type;
1228 /* the likelyhood of object, int, or void return is very high,
1229 * so those are checked before the switch */
1230 if (rtype == FFI_TYPE_POINTER)
1232 PUSHA (rvalue.object_value);
1234 else if (rtype == FFI_TYPE_SINT32)
1236 PUSHI (rvalue.int_value);
1238 else if (rtype == FFI_TYPE_VOID)
1246 case FFI_TYPE_SINT8:
1247 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1250 case FFI_TYPE_SINT16:
1251 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1254 case FFI_TYPE_UINT16:
1255 PUSHI (rvalue.int_value & 0xffff);
1258 case FFI_TYPE_FLOAT:
1259 PUSHF (rvalue.float_value);
1262 case FFI_TYPE_DOUBLE:
1263 PUSHD (rvalue.double_value);
1266 case FFI_TYPE_SINT64:
1267 PUSHL (rvalue.long_value);
1271 throw_internal_error ("unknown return type in invokeXXX");
1338 // For direct threaded, bipush and sipush are the same.
1339 #ifndef DIRECT_THREADED
1342 #endif /* DIRECT_THREADED */
1348 // For direct threaded, ldc and ldc_w are the same.
1349 #ifndef DIRECT_THREADED
1350 PUSHA ((jobject) AVAL1U ());
1352 #endif /* DIRECT_THREADED */
1354 PUSHA ((jobject) AVAL2U ());
1357 #ifdef DIRECT_THREADED
1358 // For direct threaded we have a separate 'ldc class' operation.
1361 // We could rewrite the instruction at this point.
1362 int index = INTVAL ();
1363 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1368 #endif /* DIRECT_THREADED */
1372 void *where = AVAL2UP ();
1373 memcpy (sp, where, 2*sizeof (_Jv_word));
1468 jint index = POPI();
1469 jintArray arr = (jintArray) POPA();
1470 NULLARRAYCHECK (arr);
1471 ARRAYBOUNDSCHECK (arr, index);
1472 PUSHI( elements(arr)[index] );
1478 jint index = POPI();
1479 jlongArray arr = (jlongArray) POPA();
1480 NULLARRAYCHECK (arr);
1481 ARRAYBOUNDSCHECK (arr, index);
1482 PUSHL( elements(arr)[index] );
1488 jint index = POPI();
1489 jfloatArray arr = (jfloatArray) POPA();
1490 NULLARRAYCHECK (arr);
1491 ARRAYBOUNDSCHECK (arr, index);
1492 PUSHF( elements(arr)[index] );
1498 jint index = POPI();
1499 jdoubleArray arr = (jdoubleArray) POPA();
1500 NULLARRAYCHECK (arr);
1501 ARRAYBOUNDSCHECK (arr, index);
1502 PUSHD( elements(arr)[index] );
1508 jint index = POPI();
1509 jobjectArray arr = (jobjectArray) POPA();
1510 NULLARRAYCHECK (arr);
1511 ARRAYBOUNDSCHECK (arr, index);
1512 PUSHA( elements(arr)[index] );
1518 jint index = POPI();
1519 jbyteArray arr = (jbyteArray) POPA();
1520 NULLARRAYCHECK (arr);
1521 ARRAYBOUNDSCHECK (arr, index);
1522 PUSHI( elements(arr)[index] );
1528 jint index = POPI();
1529 jcharArray arr = (jcharArray) POPA();
1530 NULLARRAYCHECK (arr);
1531 ARRAYBOUNDSCHECK (arr, index);
1532 PUSHI( elements(arr)[index] );
1538 jint index = POPI();
1539 jshortArray arr = (jshortArray) POPA();
1540 NULLARRAYCHECK (arr);
1541 ARRAYBOUNDSCHECK (arr, index);
1542 PUSHI( elements(arr)[index] );
1648 jint value = POPI();
1649 jint index = POPI();
1650 jintArray arr = (jintArray) POPA();
1651 NULLARRAYCHECK (arr);
1652 ARRAYBOUNDSCHECK (arr, index);
1653 elements(arr)[index] = value;
1659 jlong value = POPL();
1660 jint index = POPI();
1661 jlongArray arr = (jlongArray) POPA();
1662 NULLARRAYCHECK (arr);
1663 ARRAYBOUNDSCHECK (arr, index);
1664 elements(arr)[index] = value;
1670 jfloat value = POPF();
1671 jint index = POPI();
1672 jfloatArray arr = (jfloatArray) POPA();
1673 NULLARRAYCHECK (arr);
1674 ARRAYBOUNDSCHECK (arr, index);
1675 elements(arr)[index] = value;
1681 jdouble value = POPD();
1682 jint index = POPI();
1683 jdoubleArray arr = (jdoubleArray) POPA();
1684 NULLARRAYCHECK (arr);
1685 ARRAYBOUNDSCHECK (arr, index);
1686 elements(arr)[index] = value;
1692 jobject value = POPA();
1693 jint index = POPI();
1694 jobjectArray arr = (jobjectArray) POPA();
1695 NULLARRAYCHECK (arr);
1696 ARRAYBOUNDSCHECK (arr, index);
1697 _Jv_CheckArrayStore (arr, value);
1698 elements(arr)[index] = value;
1704 jbyte value = (jbyte) POPI();
1705 jint index = POPI();
1706 jbyteArray arr = (jbyteArray) POPA();
1707 NULLARRAYCHECK (arr);
1708 ARRAYBOUNDSCHECK (arr, index);
1709 elements(arr)[index] = value;
1715 jchar value = (jchar) POPI();
1716 jint index = POPI();
1717 jcharArray arr = (jcharArray) POPA();
1718 NULLARRAYCHECK (arr);
1719 ARRAYBOUNDSCHECK (arr, index);
1720 elements(arr)[index] = value;
1726 jshort value = (jshort) POPI();
1727 jint index = POPI();
1728 jshortArray arr = (jshortArray) POPA();
1729 NULLARRAYCHECK (arr);
1730 ARRAYBOUNDSCHECK (arr, index);
1731 elements(arr)[index] = value;
1749 dupx (sp, 1, 1); sp+=1;
1753 dupx (sp, 1, 2); sp+=1;
1763 dupx (sp, 2, 1); sp+=2;
1767 dupx (sp, 2, 2); sp+=2;
1772 jobject tmp1 = POPA();
1773 jobject tmp2 = POPA();
1829 jint value2 = POPI();
1830 jint value1 = POPI();
1831 jint res = _Jv_divI (value1, value2);
1838 jlong value2 = POPL();
1839 jlong value1 = POPL();
1840 jlong res = _Jv_divJ (value1, value2);
1847 jfloat value2 = POPF();
1848 jfloat value1 = POPF();
1849 jfloat res = value1 / value2;
1856 jdouble value2 = POPD();
1857 jdouble value1 = POPD();
1858 jdouble res = value1 / value2;
1865 jint value2 = POPI();
1866 jint value1 = POPI();
1867 jint res = _Jv_remI (value1, value2);
1874 jlong value2 = POPL();
1875 jlong value1 = POPL();
1876 jlong res = _Jv_remJ (value1, value2);
1883 jfloat value2 = POPF();
1884 jfloat value1 = POPF();
1885 jfloat res = __ieee754_fmod (value1, value2);
1892 jdouble value2 = POPD();
1893 jdouble value1 = POPD();
1894 jdouble res = __ieee754_fmod (value1, value2);
1901 jint value = POPI();
1908 jlong value = POPL();
1915 jfloat value = POPF();
1922 jdouble value = POPD();
1929 jint shift = (POPI() & 0x1f);
1930 jint value = POPI();
1931 PUSHI (value << shift);
1937 jint shift = (POPI() & 0x3f);
1938 jlong value = POPL();
1939 PUSHL (value << shift);
1945 jint shift = (POPI() & 0x1f);
1946 jint value = POPI();
1947 PUSHI (value >> shift);
1953 jint shift = (POPI() & 0x3f);
1954 jlong value = POPL();
1955 PUSHL (value >> shift);
1961 jint shift = (POPI() & 0x1f);
1962 _Jv_uint value = (_Jv_uint) POPI();
1963 PUSHI ((jint) (value >> shift));
1969 jint shift = (POPI() & 0x3f);
1970 _Jv_ulong value = (_Jv_ulong) POPL();
1971 PUSHL ((jlong) (value >> shift));
2001 jint index = GET1U ();
2002 jint amount = GET1S ();
2003 locals[index].i += amount;
2008 {jlong value = POPI(); PUSHL (value);}
2012 {jfloat value = POPI(); PUSHF (value);}
2016 {jdouble value = POPI(); PUSHD (value);}
2020 {jint value = POPL(); PUSHI (value);}
2024 {jfloat value = POPL(); PUSHF (value);}
2028 {jdouble value = POPL(); PUSHD (value);}
2033 using namespace java::lang;
2034 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2041 using namespace java::lang;
2042 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2048 { jdouble value = POPF (); PUSHD(value); }
2053 using namespace java::lang;
2054 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2061 using namespace java::lang;
2062 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2068 { jfloat value = POPD (); PUSHF(value); }
2072 { jbyte value = POPI (); PUSHI(value); }
2076 { jchar value = POPI (); PUSHI(value); }
2080 { jshort value = POPI (); PUSHI(value); }
2085 jlong value2 = POPL ();
2086 jlong value1 = POPL ();
2087 if (value1 > value2)
2089 else if (value1 == value2)
2105 jfloat value2 = POPF ();
2106 jfloat value1 = POPF ();
2107 if (value1 > value2)
2109 else if (value1 == value2)
2111 else if (value1 < value2)
2127 jdouble value2 = POPD ();
2128 jdouble value1 = POPD ();
2129 if (value1 > value2)
2131 else if (value1 == value2)
2133 else if (value1 < value2)
2196 jint value2 = POPI();
2197 jint value1 = POPI();
2198 if (value1 == value2)
2207 jint value2 = POPI();
2208 jint value1 = POPI();
2209 if (value1 != value2)
2218 jint value2 = POPI();
2219 jint value1 = POPI();
2220 if (value1 < value2)
2229 jint value2 = POPI();
2230 jint value1 = POPI();
2231 if (value1 >= value2)
2240 jint value2 = POPI();
2241 jint value1 = POPI();
2242 if (value1 > value2)
2251 jint value2 = POPI();
2252 jint value1 = POPI();
2253 if (value1 <= value2)
2262 jobject value2 = POPA();
2263 jobject value1 = POPA();
2264 if (value1 == value2)
2273 jobject value2 = POPA();
2274 jobject value1 = POPA();
2275 if (value1 != value2)
2283 #ifndef DIRECT_THREADED
2284 // For direct threaded, goto and goto_w are the same.
2285 pc = pc - 1 + get4 (pc);
2287 #endif /* DIRECT_THREADED */
2293 #ifndef DIRECT_THREADED
2294 // For direct threaded, jsr and jsr_w are the same.
2296 pc_t next = pc - 1 + get4 (pc);
2298 PUSHA ((jobject) pc);
2302 #endif /* DIRECT_THREADED */
2305 pc_t next = GOTO_VAL();
2307 PUSHA ((jobject) pc);
2314 jint index = GET1U ();
2315 pc = (pc_t) PEEKA (index);
2321 #ifdef DIRECT_THREADED
2322 void *def = (pc++)->datum;
2326 jint low = INTVAL ();
2327 jint high = INTVAL ();
2329 if (index < low || index > high)
2330 pc = (insn_slot *) def;
2332 pc = (insn_slot *) ((pc + index - low)->datum);
2334 pc_t base_pc = pc - 1;
2335 int index = POPI ();
2337 pc_t base = (pc_t) bytecode ();
2338 while ((pc - base) % 4 != 0)
2341 jint def = get4 (pc);
2342 jint low = get4 (pc + 4);
2343 jint high = get4 (pc + 8);
2344 if (index < low || index > high)
2347 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2348 #endif /* DIRECT_THREADED */
2354 #ifdef DIRECT_THREADED
2355 void *def = (pc++)->insn;
2359 jint npairs = INTVAL ();
2361 int max = npairs - 1;
2364 // Simple binary search...
2367 int half = (min + max) / 2;
2368 int match = pc[2 * half].int_val;
2373 pc = (insn_slot *) pc[2 * half + 1].datum;
2376 else if (index < match)
2377 // We can use HALF - 1 here because we check again on
2381 // We can use HALF + 1 here because we check again on
2385 if (index == pc[2 * min].int_val)
2386 pc = (insn_slot *) pc[2 * min + 1].datum;
2388 pc = (insn_slot *) def;
2390 unsigned char *base_pc = pc-1;
2393 unsigned char* base = bytecode ();
2394 while ((pc-base) % 4 != 0)
2397 jint def = get4 (pc);
2398 jint npairs = get4 (pc+4);
2403 // Simple binary search...
2406 int half = (min+max)/2;
2407 int match = get4 (pc+ 4*(2 + 2*half));
2411 else if (index < match)
2412 // We can use HALF - 1 here because we check again on
2416 // We can use HALF + 1 here because we check again on
2421 if (index == get4 (pc+ 4*(2 + 2*min)))
2422 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2425 #endif /* DIRECT_THREADED */
2430 *(jobject *) retp = POPA ();
2434 *(jlong *) retp = POPL ();
2438 *(jfloat *) retp = POPF ();
2442 *(jdouble *) retp = POPD ();
2446 *(jint *) retp = POPI ();
2454 jint fieldref_index = GET2U ();
2455 SAVE_PC(); // Constant pool resolution could throw.
2456 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2457 _Jv_Field *field = pool_data[fieldref_index].field;
2459 if ((field->flags & Modifier::STATIC) == 0)
2460 throw_incompatible_class_change_error
2461 (JvNewStringLatin1 ("field no longer static"));
2463 jclass type = field->type;
2465 // We rewrite the instruction once we discover what it refers
2467 void *newinsn = NULL;
2468 if (type->isPrimitive ())
2470 switch (type->size_in_bytes)
2473 PUSHI (*field->u.byte_addr);
2474 newinsn = AMPAMP (getstatic_resolved_1);
2478 if (type == JvPrimClass (char))
2480 PUSHI (*field->u.char_addr);
2481 newinsn = AMPAMP (getstatic_resolved_char);
2485 PUSHI (*field->u.short_addr);
2486 newinsn = AMPAMP (getstatic_resolved_short);
2491 PUSHI(*field->u.int_addr);
2492 newinsn = AMPAMP (getstatic_resolved_4);
2496 PUSHL(*field->u.long_addr);
2497 newinsn = AMPAMP (getstatic_resolved_8);
2503 PUSHA(*field->u.object_addr);
2504 newinsn = AMPAMP (getstatic_resolved_obj);
2507 #ifdef DIRECT_THREADED
2508 pc[-2].insn = newinsn;
2509 pc[-1].datum = field->u.addr;
2510 #endif /* DIRECT_THREADED */
2514 #ifdef DIRECT_THREADED
2515 getstatic_resolved_1:
2516 PUSHI (*(jbyte *) AVAL ());
2519 getstatic_resolved_char:
2520 PUSHI (*(jchar *) AVAL ());
2523 getstatic_resolved_short:
2524 PUSHI (*(jshort *) AVAL ());
2527 getstatic_resolved_4:
2528 PUSHI (*(jint *) AVAL ());
2531 getstatic_resolved_8:
2532 PUSHL (*(jlong *) AVAL ());
2535 getstatic_resolved_obj:
2536 PUSHA (*(jobject *) AVAL ());
2538 #endif /* DIRECT_THREADED */
2542 jint fieldref_index = GET2U ();
2543 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2544 _Jv_Field *field = pool_data[fieldref_index].field;
2546 if ((field->flags & Modifier::STATIC) != 0)
2547 throw_incompatible_class_change_error
2548 (JvNewStringLatin1 ("field is static"));
2550 jclass type = field->type;
2551 jint field_offset = field->u.boffset;
2553 jobject obj = POPA();
2556 void *newinsn = NULL;
2557 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2558 if (type->isPrimitive ())
2560 switch (type->size_in_bytes)
2563 PUSHI (val->byte_value);
2564 newinsn = AMPAMP (getfield_resolved_1);
2568 if (type == JvPrimClass (char))
2570 PUSHI (val->char_value);
2571 newinsn = AMPAMP (getfield_resolved_char);
2575 PUSHI (val->short_value);
2576 newinsn = AMPAMP (getfield_resolved_short);
2581 PUSHI (val->int_value);
2582 newinsn = AMPAMP (getfield_resolved_4);
2586 PUSHL (val->long_value);
2587 newinsn = AMPAMP (getfield_resolved_8);
2593 PUSHA (val->object_value);
2594 newinsn = AMPAMP (getfield_resolved_obj);
2597 #ifdef DIRECT_THREADED
2598 pc[-2].insn = newinsn;
2599 pc[-1].int_val = field_offset;
2600 #endif /* DIRECT_THREADED */
2604 #ifdef DIRECT_THREADED
2605 getfield_resolved_1:
2607 char *obj = (char *) POPA ();
2609 PUSHI (*(jbyte *) (obj + INTVAL ()));
2613 getfield_resolved_char:
2615 char *obj = (char *) POPA ();
2617 PUSHI (*(jchar *) (obj + INTVAL ()));
2621 getfield_resolved_short:
2623 char *obj = (char *) POPA ();
2625 PUSHI (*(jshort *) (obj + INTVAL ()));
2629 getfield_resolved_4:
2631 char *obj = (char *) POPA ();
2633 PUSHI (*(jint *) (obj + INTVAL ()));
2637 getfield_resolved_8:
2639 char *obj = (char *) POPA ();
2641 PUSHL (*(jlong *) (obj + INTVAL ()));
2645 getfield_resolved_obj:
2647 char *obj = (char *) POPA ();
2649 PUSHA (*(jobject *) (obj + INTVAL ()));
2652 #endif /* DIRECT_THREADED */
2656 jint fieldref_index = GET2U ();
2657 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2658 _Jv_Field *field = pool_data[fieldref_index].field;
2660 jclass type = field->type;
2662 // ResolvePoolEntry cannot check this
2663 if ((field->flags & Modifier::STATIC) == 0)
2664 throw_incompatible_class_change_error
2665 (JvNewStringLatin1 ("field no longer static"));
2667 void *newinsn = NULL;
2668 if (type->isPrimitive ())
2670 switch (type->size_in_bytes)
2674 jint value = POPI();
2675 *field->u.byte_addr = value;
2676 newinsn = AMPAMP (putstatic_resolved_1);
2682 jint value = POPI();
2683 *field->u.char_addr = value;
2684 newinsn = AMPAMP (putstatic_resolved_2);
2690 jint value = POPI();
2691 *field->u.int_addr = value;
2692 newinsn = AMPAMP (putstatic_resolved_4);
2698 jlong value = POPL();
2699 *field->u.long_addr = value;
2700 newinsn = AMPAMP (putstatic_resolved_8);
2707 jobject value = POPA();
2708 *field->u.object_addr = value;
2709 newinsn = AMPAMP (putstatic_resolved_obj);
2712 #ifdef DIRECT_THREADED
2713 pc[-2].insn = newinsn;
2714 pc[-1].datum = field->u.addr;
2715 #endif /* DIRECT_THREADED */
2719 #ifdef DIRECT_THREADED
2720 putstatic_resolved_1:
2721 *(jbyte *) AVAL () = POPI ();
2724 putstatic_resolved_2:
2725 *(jchar *) AVAL () = POPI ();
2728 putstatic_resolved_4:
2729 *(jint *) AVAL () = POPI ();
2732 putstatic_resolved_8:
2733 *(jlong *) AVAL () = POPL ();
2736 putstatic_resolved_obj:
2737 *(jobject *) AVAL () = POPA ();
2739 #endif /* DIRECT_THREADED */
2743 jint fieldref_index = GET2U ();
2744 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2745 _Jv_Field *field = pool_data[fieldref_index].field;
2747 jclass type = field->type;
2749 if ((field->flags & Modifier::STATIC) != 0)
2750 throw_incompatible_class_change_error
2751 (JvNewStringLatin1 ("field is static"));
2753 jint field_offset = field->u.boffset;
2755 void *newinsn = NULL;
2756 if (type->isPrimitive ())
2758 switch (type->size_in_bytes)
2762 jint value = POPI();
2763 jobject obj = POPA();
2765 *(jbyte*) ((char*)obj + field_offset) = value;
2766 newinsn = AMPAMP (putfield_resolved_1);
2772 jint value = POPI();
2773 jobject obj = POPA();
2775 *(jchar*) ((char*)obj + field_offset) = value;
2776 newinsn = AMPAMP (putfield_resolved_2);
2782 jint value = POPI();
2783 jobject obj = POPA();
2785 *(jint*) ((char*)obj + field_offset) = value;
2786 newinsn = AMPAMP (putfield_resolved_4);
2792 jlong value = POPL();
2793 jobject obj = POPA();
2795 *(jlong*) ((char*)obj + field_offset) = value;
2796 newinsn = AMPAMP (putfield_resolved_8);
2803 jobject value = POPA();
2804 jobject obj = POPA();
2806 *(jobject*) ((char*)obj + field_offset) = value;
2807 newinsn = AMPAMP (putfield_resolved_obj);
2810 #ifdef DIRECT_THREADED
2811 pc[-2].insn = newinsn;
2812 pc[-1].int_val = field_offset;
2813 #endif /* DIRECT_THREADED */
2817 #ifdef DIRECT_THREADED
2818 putfield_resolved_1:
2821 char *obj = (char *) POPA ();
2823 *(jbyte *) (obj + INTVAL ()) = val;
2827 putfield_resolved_2:
2830 char *obj = (char *) POPA ();
2832 *(jchar *) (obj + INTVAL ()) = val;
2836 putfield_resolved_4:
2839 char *obj = (char *) POPA ();
2841 *(jint *) (obj + INTVAL ()) = val;
2845 putfield_resolved_8:
2847 jlong val = POPL ();
2848 char *obj = (char *) POPA ();
2850 *(jlong *) (obj + INTVAL ()) = val;
2854 putfield_resolved_obj:
2856 jobject val = POPA ();
2857 char *obj = (char *) POPA ();
2859 *(jobject *) (obj + INTVAL ()) = val;
2862 #endif /* DIRECT_THREADED */
2866 int index = GET2U ();
2868 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2871 sp -= rmeth->stack_item_count;
2873 // We don't use NULLCHECK here because we can't rely on that
2874 // working for <init>. So instead we do an explicit test.
2878 throw_null_pointer_exception ();
2881 fun = (void (*)()) rmeth->method->ncode;
2883 #ifdef DIRECT_THREADED
2884 // Rewrite instruction so that we use a faster pre-resolved
2886 pc[-2].insn = &&invokespecial_resolved;
2887 pc[-1].datum = rmeth;
2888 #endif /* DIRECT_THREADED */
2890 goto perform_invoke;
2892 #ifdef DIRECT_THREADED
2893 invokespecial_resolved:
2895 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2896 sp -= rmeth->stack_item_count;
2897 // We don't use NULLCHECK here because we can't rely on that
2898 // working for <init>. So instead we do an explicit test.
2902 throw_null_pointer_exception ();
2904 fun = (void (*)()) rmeth->method->ncode;
2906 goto perform_invoke;
2907 #endif /* DIRECT_THREADED */
2911 int index = GET2U ();
2913 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2916 sp -= rmeth->stack_item_count;
2918 fun = (void (*)()) rmeth->method->ncode;
2920 #ifdef DIRECT_THREADED
2921 // Rewrite instruction so that we use a faster pre-resolved
2923 pc[-2].insn = &&invokestatic_resolved;
2924 pc[-1].datum = rmeth;
2925 #endif /* DIRECT_THREADED */
2927 goto perform_invoke;
2929 #ifdef DIRECT_THREADED
2930 invokestatic_resolved:
2932 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2933 sp -= rmeth->stack_item_count;
2934 fun = (void (*)()) rmeth->method->ncode;
2936 goto perform_invoke;
2937 #endif /* DIRECT_THREADED */
2939 insn_invokeinterface:
2941 int index = GET2U ();
2943 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2946 sp -= rmeth->stack_item_count;
2948 jobject rcv = sp[0].o;
2953 _Jv_LookupInterfaceMethod (rcv->getClass (),
2954 rmeth->method->name,
2955 rmeth->method->signature);
2957 #ifdef DIRECT_THREADED
2958 // Rewrite instruction so that we use a faster pre-resolved
2960 pc[-2].insn = &&invokeinterface_resolved;
2961 pc[-1].datum = rmeth;
2963 // Skip dummy bytes.
2965 #endif /* DIRECT_THREADED */
2967 goto perform_invoke;
2969 #ifdef DIRECT_THREADED
2970 invokeinterface_resolved:
2972 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2973 sp -= rmeth->stack_item_count;
2974 jobject rcv = sp[0].o;
2977 _Jv_LookupInterfaceMethod (rcv->getClass (),
2978 rmeth->method->name,
2979 rmeth->method->signature);
2981 goto perform_invoke;
2982 #endif /* DIRECT_THREADED */
2986 int index = GET2U ();
2987 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2989 /* VM spec, section 3.11.5 */
2990 if ((klass->getModifiers() & Modifier::ABSTRACT)
2991 || klass->isInterface())
2992 throw new java::lang::InstantiationException;
2993 jobject res = _Jv_AllocObject (klass);
2996 #ifdef DIRECT_THREADED
2997 pc[-2].insn = &&new_resolved;
2998 pc[-1].datum = klass;
2999 #endif /* DIRECT_THREADED */
3003 #ifdef DIRECT_THREADED
3006 jclass klass = (jclass) AVAL ();
3007 jobject res = _Jv_AllocObject (klass);
3011 #endif /* DIRECT_THREADED */
3015 int atype = GET1U ();
3017 jobject result = _Jv_NewArray (atype, size);
3024 int index = GET2U ();
3025 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3028 jobject result = _Jv_NewObjectArray (size, klass, 0);
3031 #ifdef DIRECT_THREADED
3032 pc[-2].insn = &&anewarray_resolved;
3033 pc[-1].datum = klass;
3034 #endif /* DIRECT_THREADED */
3038 #ifdef DIRECT_THREADED
3041 jclass klass = (jclass) AVAL ();
3043 jobject result = _Jv_NewObjectArray (size, klass, 0);
3047 #endif /* DIRECT_THREADED */
3051 __JArray *arr = (__JArray*)POPA();
3052 NULLARRAYCHECK (arr);
3053 PUSHI (arr->length);
3059 jobject value = POPA();
3060 throw static_cast<jthrowable>(value);
3067 jobject value = POPA();
3068 jint index = GET2U ();
3069 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3072 value = (jobject) _Jv_CheckCast (to, value);
3076 #ifdef DIRECT_THREADED
3077 pc[-2].insn = &&checkcast_resolved;
3079 #endif /* DIRECT_THREADED */
3083 #ifdef DIRECT_THREADED
3087 jobject value = POPA ();
3088 jclass to = (jclass) AVAL ();
3089 value = (jobject) _Jv_CheckCast (to, value);
3093 #endif /* DIRECT_THREADED */
3098 jobject value = POPA();
3099 jint index = GET2U ();
3100 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3102 PUSHI (to->isInstance (value));
3104 #ifdef DIRECT_THREADED
3105 pc[-2].insn = &&instanceof_resolved;
3107 #endif /* DIRECT_THREADED */
3111 #ifdef DIRECT_THREADED
3112 instanceof_resolved:
3114 jobject value = POPA ();
3115 jclass to = (jclass) AVAL ();
3116 PUSHI (to->isInstance (value));
3119 #endif /* DIRECT_THREADED */
3123 jobject value = POPA();
3125 _Jv_MonitorEnter (value);
3131 jobject value = POPA();
3133 _Jv_MonitorExit (value);
3139 jobject val = POPA();
3149 jobject val = POPA();
3157 insn_multianewarray:
3159 int kind_index = GET2U ();
3163 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3165 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3167 for (int i = dim - 1; i >= 0; i--)
3172 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3178 #ifndef DIRECT_THREADED
3181 jint the_mod_op = get1u (pc++);
3182 jint wide = get2u (pc); pc += 2;
3227 pc = (unsigned char*) PEEKA (wide);
3232 jint amount = get2s (pc); pc += 2;
3233 jint value = PEEKI (wide);
3234 POKEI (wide, value+amount);
3239 throw_internal_error ("illegal bytecode modified by wide");
3243 #endif /* DIRECT_THREADED */
3245 catch (java::lang::Throwable *ex)
3247 #ifdef DIRECT_THREADED
3248 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3250 int logical_pc = pc - 1 - bytecode ();
3252 _Jv_InterpException *exc = meth->exceptions ();
3253 jclass exc_class = ex->getClass ();
3255 for (int i = 0; i < meth->exc_count; i++)
3257 if (PCVAL (exc[i].start_pc) <= logical_pc
3258 && logical_pc < PCVAL (exc[i].end_pc))
3260 #ifdef DIRECT_THREADED
3261 jclass handler = (jclass) exc[i].handler_type.p;
3263 jclass handler = NULL;
3264 if (exc[i].handler_type.i != 0)
3265 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3266 exc[i].handler_type.i)).clazz;
3267 #endif /* DIRECT_THREADED */
3269 if (handler == NULL || handler->isAssignableFrom (exc_class))
3271 #ifdef DIRECT_THREADED
3272 pc = (insn_slot *) exc[i].handler_pc.p;
3274 pc = bytecode () + exc[i].handler_pc.i;
3275 #endif /* DIRECT_THREADED */
3277 sp++->o = ex; // Push exception.
3283 // No handler, so re-throw.
3289 throw_internal_error (const char *msg)
3291 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3295 throw_incompatible_class_change_error (jstring msg)
3297 throw new java::lang::IncompatibleClassChangeError (msg);
3301 throw_null_pointer_exception ()
3303 throw new java::lang::NullPointerException;
3306 /* Look up source code line number for given bytecode (or direct threaded
3309 _Jv_InterpMethod::get_source_line(pc_t mpc)
3311 int line = line_table_len > 0 ? line_table[0].line : -1;
3312 for (int i = 1; i < line_table_len; i++)
3313 if (line_table[i].pc > mpc)
3316 line = line_table[i].line;
3321 /** Do static initialization for fields with a constant initializer */
3323 _Jv_InitField (jobject obj, jclass klass, int index)
3325 using namespace java::lang::reflect;
3327 if (obj != 0 && klass == 0)
3328 klass = obj->getClass ();
3330 if (!_Jv_IsInterpretedClass (klass))
3333 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3335 _Jv_Field * field = (&klass->fields[0]) + index;
3337 if (index > klass->field_count)
3338 throw_internal_error ("field out of range");
3340 int init = iclass->field_initializers[index];
3344 _Jv_Constants *pool = &klass->constants;
3345 int tag = pool->tags[init];
3347 if (! field->isResolved ())
3348 throw_internal_error ("initializing unresolved field");
3350 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3351 throw_internal_error ("initializing non-static field with no object");
3355 if ((field->flags & Modifier::STATIC) != 0)
3356 addr = (void*) field->u.addr;
3358 addr = (void*) (((char*)obj) + field->u.boffset);
3362 case JV_CONSTANT_String:
3365 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3366 pool->data[init].string = str;
3367 pool->tags[init] = JV_CONSTANT_ResolvedString;
3371 case JV_CONSTANT_ResolvedString:
3372 if (! (field->type == &java::lang::String::class$
3373 || field->type == &java::lang::Class::class$))
3374 throw_class_format_error ("string initialiser to non-string field");
3376 *(jstring*)addr = pool->data[init].string;
3379 case JV_CONSTANT_Integer:
3381 int value = pool->data[init].i;
3383 if (field->type == JvPrimClass (boolean))
3384 *(jboolean*)addr = (jboolean)value;
3386 else if (field->type == JvPrimClass (byte))
3387 *(jbyte*)addr = (jbyte)value;
3389 else if (field->type == JvPrimClass (char))
3390 *(jchar*)addr = (jchar)value;
3392 else if (field->type == JvPrimClass (short))
3393 *(jshort*)addr = (jshort)value;
3395 else if (field->type == JvPrimClass (int))
3396 *(jint*)addr = (jint)value;
3399 throw_class_format_error ("erroneous field initializer");
3403 case JV_CONSTANT_Long:
3404 if (field->type != JvPrimClass (long))
3405 throw_class_format_error ("erroneous field initializer");
3407 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3410 case JV_CONSTANT_Float:
3411 if (field->type != JvPrimClass (float))
3412 throw_class_format_error ("erroneous field initializer");
3414 *(jfloat*)addr = pool->data[init].f;
3417 case JV_CONSTANT_Double:
3418 if (field->type != JvPrimClass (double))
3419 throw_class_format_error ("erroneous field initializer");
3421 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3425 throw_class_format_error ("erroneous field initializer");
3429 inline static unsigned char*
3430 skip_one_type (unsigned char* ptr)
3441 do { ch = *ptr++; } while (ch != ';');
3448 get_ffi_type_from_signature (unsigned char* ptr)
3454 return &ffi_type_pointer;
3458 // On some platforms a bool is a byte, on others an int.
3459 if (sizeof (jboolean) == sizeof (jbyte))
3460 return &ffi_type_sint8;
3463 JvAssert (sizeof (jbyte) == sizeof (jint));
3464 return &ffi_type_sint32;
3469 return &ffi_type_sint8;
3473 return &ffi_type_uint16;
3477 return &ffi_type_sint16;
3481 return &ffi_type_sint32;
3485 return &ffi_type_sint64;
3489 return &ffi_type_float;
3493 return &ffi_type_double;
3497 return &ffi_type_void;
3501 throw_internal_error ("unknown type in signature");
3504 /* this function yields the number of actual arguments, that is, if the
3505 * function is non-static, then one is added to the number of elements
3506 * found in the signature */
3509 _Jv_count_arguments (_Jv_Utf8Const *signature,
3512 unsigned char *ptr = (unsigned char*) signature->chars();
3513 int arg_count = staticp ? 0 : 1;
3515 /* first, count number of arguments */
3523 ptr = skip_one_type (ptr);
3530 /* This beast will build a cif, given the signature. Memory for
3531 * the cif itself and for the argument types must be allocated by the
3536 init_cif (_Jv_Utf8Const* signature,
3540 ffi_type **arg_types,
3543 unsigned char *ptr = (unsigned char*) signature->chars();
3545 int arg_index = 0; // arg number
3546 int item_count = 0; // stack-item count
3551 arg_types[arg_index++] = &ffi_type_pointer;
3561 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3563 if (*ptr == 'J' || *ptr == 'D')
3568 ptr = skip_one_type (ptr);
3573 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3575 ptr = skip_one_type (ptr);
3576 if (ptr != (unsigned char*)signature->chars() + signature->len())
3577 throw_internal_error ("did not find end of signature");
3579 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3580 arg_count, rtype, arg_types) != FFI_OK)
3581 throw_internal_error ("ffi_prep_cif failed");
3583 if (rtype_p != NULL)
3589 #if FFI_NATIVE_RAW_API
3590 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3591 # define FFI_RAW_SIZE ffi_raw_size
3593 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3594 # define FFI_RAW_SIZE ffi_java_raw_size
3597 /* we put this one here, and not in interpret.cc because it
3598 * calls the utility routines _Jv_count_arguments
3599 * which are static to this module. The following struct defines the
3600 * layout we use for the stubs, it's only used in the ncode method. */
3603 ffi_raw_closure closure;
3605 ffi_type *arg_types[0];
3608 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3611 _Jv_InterpMethod::ncode ()
3613 using namespace java::lang::reflect;
3615 if (self->ncode != 0)
3618 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3619 int arg_count = _Jv_count_arguments (self->signature, staticp);
3621 ncode_closure *closure =
3622 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3623 + arg_count * sizeof (ffi_type*));
3625 init_cif (self->signature,
3629 &closure->arg_types[0],
3632 ffi_closure_fun fun;
3634 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3636 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3638 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3641 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3643 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3648 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3650 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3653 FFI_PREP_RAW_CLOSURE (&closure->closure,
3658 self->ncode = (void*)closure;
3662 #ifdef DIRECT_THREADED
3663 /* Find the index of the given insn in the array of insn slots
3664 for this method. Returns -1 if not found. */
3666 _Jv_InterpMethod::insn_index (pc_t pc)
3669 jlong right = number_insn_slots;
3670 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3674 jlong mid = (left + right) / 2;
3675 if (&slots[mid] == pc)
3678 if (pc < &slots[mid])
3686 #endif // DIRECT_THREADED
3689 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
3690 jintArray& line_numbers,
3691 jlongArray& code_indices)
3693 #ifdef DIRECT_THREADED
3694 /* For the DIRECT_THREADED case, if the method has not yet been
3695 * compiled, the linetable will change to insn slots instead of
3696 * bytecode PCs. It is probably easiest, in this case, to simply
3697 * compile the method and guarantee that we are using insn
3700 _Jv_CompileMethod (this);
3702 if (line_table_len > 0)
3705 end = number_insn_slots;
3706 line_numbers = JvNewIntArray (line_table_len);
3707 code_indices = JvNewLongArray (line_table_len);
3709 jint* lines = elements (line_numbers);
3710 jlong* indices = elements (code_indices);
3711 for (int i = 0; i < line_table_len; ++i)
3713 lines[i] = line_table[i].line;
3714 indices[i] = insn_index (line_table[i].pc);
3717 #else // !DIRECT_THREADED
3718 if (line_table_len > 0)
3722 line_numbers = JvNewIntArray (line_table_len);
3723 code_indices = JvNewLongArray (line_table_len);
3725 jint* lines = elements (line_numbers);
3726 jlong* indices = elements (code_indices);
3727 for (int i = 0; i < line_table_len; ++i)
3729 lines[i] = line_table[i].line;
3730 indices[i] = (jlong) line_table[i].bytecode_pc;
3733 #endif // !DIRECT_THREADED
3737 _Jv_JNIMethod::ncode ()
3739 using namespace java::lang::reflect;
3741 if (self->ncode != 0)
3744 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3745 int arg_count = _Jv_count_arguments (self->signature, staticp);
3747 ncode_closure *closure =
3748 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3749 + arg_count * sizeof (ffi_type*));
3752 init_cif (self->signature,
3756 &closure->arg_types[0],
3759 ffi_closure_fun fun;
3761 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3763 // Initialize the argument types and CIF that represent the actual
3764 // underlying JNI function.
3766 if ((self->accflags & Modifier::STATIC))
3768 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3769 * sizeof (ffi_type *));
3771 jni_arg_types[offset++] = &ffi_type_pointer;
3772 if ((self->accflags & Modifier::STATIC))
3773 jni_arg_types[offset++] = &ffi_type_pointer;
3774 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3775 arg_count * sizeof (ffi_type *));
3777 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3778 extra_args + arg_count, rtype,
3779 jni_arg_types) != FFI_OK)
3780 throw_internal_error ("ffi_prep_cif failed for JNI function");
3782 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3784 // FIXME: for now we assume that all native methods for
3785 // interpreted code use JNI.
3786 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3788 FFI_PREP_RAW_CLOSURE (&closure->closure,
3793 self->ncode = (void *) closure;
3798 throw_class_format_error (jstring msg)
3801 ? new java::lang::ClassFormatError (msg)
3802 : new java::lang::ClassFormatError);
3806 throw_class_format_error (const char *msg)
3808 throw_class_format_error (JvNewStringLatin1 (msg));
3814 _Jv_InterpreterEngine::do_verify (jclass klass)
3816 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3817 for (int i = 0; i < klass->method_count; i++)
3819 using namespace java::lang::reflect;
3820 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3821 _Jv_ushort accflags = klass->methods[i].accflags;
3822 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3824 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3825 _Jv_VerifyMethod (im);
3831 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3833 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3834 for (int i = 0; i < klass->method_count; i++)
3836 // Just skip abstract methods. This is particularly important
3837 // because we don't resize the interpreted_methods array when
3838 // miranda methods are added to it.
3839 if ((klass->methods[i].accflags
3840 & java::lang::reflect::Modifier::ABSTRACT)
3844 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3846 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3849 // You might think we could use a virtual `ncode' method in
3850 // the _Jv_MethodBase and unify the native and non-native
3851 // cases. Well, we can't, because we don't allocate these
3852 // objects using `new', and thus they don't get a vtable.
3853 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3854 klass->methods[i].ncode = jnim->ncode ();
3856 else if (imeth != 0) // it could be abstract
3858 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3859 klass->methods[i].ncode = im->ncode ();
3865 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3869 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3871 // Splitting the allocations here lets us scan reference fields and
3872 // avoid scanning non-reference fields. How reference fields are
3873 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
3874 // means that this memory will be scanned conservatively (same
3875 // difference, since we know all the contents here are pointers).
3876 // Then we put pointers into this memory into the 'fields'
3877 // structure. Most of these are interior pointers, which is ok (but
3878 // even so the pointer to the first reference field will be used and
3879 // that is not an interior pointer). The 'fields' array is also
3880 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
3881 // be scanned. A pointer to this array is held by Class and thus
3882 // seen by the collector.
3883 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
3884 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
3886 for (int i = 0; i < klass->field_count; i++)
3888 _Jv_Field *field = &klass->fields[i];
3890 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3893 char *base = field->isRef() ? reference_fields : non_reference_fields;
3894 field->u.addr = base + field->u.boffset;
3896 if (iclass->field_initializers[i] != 0)
3898 _Jv_Linker::resolve_field (field, klass->loader);
3899 _Jv_InitField (0, klass, i);
3903 // Now we don't need the field_initializers anymore, so let the
3904 // collector get rid of it.
3905 iclass->field_initializers = 0;
3908 _Jv_ResolvedMethod *
3909 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3912 int arg_count = _Jv_count_arguments (method->signature, staticp);
3914 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3915 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3916 + arg_count*sizeof (ffi_type*));
3918 result->stack_item_count
3919 = init_cif (method->signature,
3923 &result->arg_types[0],
3926 result->method = method;
3927 result->klass = klass;
3933 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3935 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3936 for (int i = 0; i < klass->method_count; i++)
3938 // Just skip abstract methods. This is particularly important
3939 // because we don't resize the interpreted_methods array when
3940 // miranda methods are added to it.
3941 if ((klass->methods[i].accflags
3942 & java::lang::reflect::Modifier::ABSTRACT)
3945 // Miranda method additions mean that the `methods' array moves.
3946 // We cache a pointer into this array, so we have to update.
3947 iclass->interpreted_methods[i]->self = &klass->methods[i];
3951 #ifdef DIRECT_THREADED
3953 _Jv_CompileMethod (_Jv_InterpMethod* method)
3955 if (method->prepared == NULL)
3956 _Jv_InterpMethod::run (NULL, NULL, method);
3958 #endif // DIRECT_THREADED
3960 #endif // INTERPRETER