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]];
779 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
780 // during compilation.
782 = (_Jv_Linker::resolve_pool_entry (defining_class,
783 exc[i].handler_type.i)).clazz;
784 exc[i].handler_type.p = handler;
787 // Translate entries in the LineNumberTable from bytecode PC's to direct
788 // threaded interpreter instruction values.
789 for (int i = 0; i < line_table_len; i++)
791 int byte_pc = line_table[i].bytecode_pc;
792 // It isn't worth throwing an exception if this table is
793 // corrupted, but at the same time we don't want a crash.
794 if (byte_pc < 0 || byte_pc >= code_length)
796 line_table[i].pc = &insns[pc_mapping[byte_pc]];
801 #endif /* DIRECT_THREADED */
803 /* Run the given method.
804 When args is NULL, don't run anything -- just compile it. */
806 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
808 using namespace java::lang::reflect;
810 // FRAME_DESC registers this particular invocation as the top-most
811 // interpreter frame. This lets the stack tracing code (for
812 // Throwable) print information about the method being interpreted
813 // rather than about the interpreter itself. FRAME_DESC has a
814 // destructor so it cleans up automatically when the interpreter
816 java::lang::Thread *thread = java::lang::Thread::currentThread();
817 _Jv_InterpFrame frame_desc (meth, thread);
819 _Jv_word stack[meth->max_stack];
820 _Jv_word *sp = stack;
822 _Jv_word locals[meth->max_locals];
824 #define INSN_LABEL(op) &&insn_##op
826 static const void *const insn_target[] =
829 INSN_LABEL(aconst_null),
830 INSN_LABEL(iconst_m1),
831 INSN_LABEL(iconst_0),
832 INSN_LABEL(iconst_1),
833 INSN_LABEL(iconst_2),
834 INSN_LABEL(iconst_3),
835 INSN_LABEL(iconst_4),
836 INSN_LABEL(iconst_5),
837 INSN_LABEL(lconst_0),
838 INSN_LABEL(lconst_1),
839 INSN_LABEL(fconst_0),
840 INSN_LABEL(fconst_1),
841 INSN_LABEL(fconst_2),
842 INSN_LABEL(dconst_0),
843 INSN_LABEL(dconst_1),
887 INSN_LABEL(istore_0),
888 INSN_LABEL(istore_1),
889 INSN_LABEL(istore_2),
890 INSN_LABEL(istore_3),
891 INSN_LABEL(lstore_0),
892 INSN_LABEL(lstore_1),
893 INSN_LABEL(lstore_2),
894 INSN_LABEL(lstore_3),
895 INSN_LABEL(fstore_0),
896 INSN_LABEL(fstore_1),
897 INSN_LABEL(fstore_2),
898 INSN_LABEL(fstore_3),
899 INSN_LABEL(dstore_0),
900 INSN_LABEL(dstore_1),
901 INSN_LABEL(dstore_2),
902 INSN_LABEL(dstore_3),
903 INSN_LABEL(astore_0),
904 INSN_LABEL(astore_1),
905 INSN_LABEL(astore_2),
906 INSN_LABEL(astore_3),
987 INSN_LABEL(if_icmpeq),
988 INSN_LABEL(if_icmpne),
989 INSN_LABEL(if_icmplt),
990 INSN_LABEL(if_icmpge),
991 INSN_LABEL(if_icmpgt),
992 INSN_LABEL(if_icmple),
993 INSN_LABEL(if_acmpeq),
994 INSN_LABEL(if_acmpne),
998 INSN_LABEL(tableswitch),
999 INSN_LABEL(lookupswitch),
1000 INSN_LABEL(ireturn),
1001 INSN_LABEL(lreturn),
1002 INSN_LABEL(freturn),
1003 INSN_LABEL(dreturn),
1004 INSN_LABEL(areturn),
1006 INSN_LABEL(getstatic),
1007 INSN_LABEL(putstatic),
1008 INSN_LABEL(getfield),
1009 INSN_LABEL(putfield),
1010 INSN_LABEL(invokevirtual),
1011 INSN_LABEL(invokespecial),
1012 INSN_LABEL(invokestatic),
1013 INSN_LABEL(invokeinterface),
1016 INSN_LABEL(newarray),
1017 INSN_LABEL(anewarray),
1018 INSN_LABEL(arraylength),
1020 INSN_LABEL(checkcast),
1021 INSN_LABEL(instanceof),
1022 INSN_LABEL(monitorenter),
1023 INSN_LABEL(monitorexit),
1024 #ifdef DIRECT_THREADED
1029 INSN_LABEL(multianewarray),
1031 INSN_LABEL(ifnonnull),
1034 #ifdef DIRECT_THREADED
1035 INSN_LABEL (ldc_class)
1043 #ifdef DIRECT_THREADED
1045 #define NEXT_INSN goto *((pc++)->insn)
1046 #define INTVAL() ((pc++)->int_val)
1047 #define AVAL() ((pc++)->datum)
1049 #define GET1S() INTVAL ()
1050 #define GET2S() INTVAL ()
1051 #define GET1U() INTVAL ()
1052 #define GET2U() INTVAL ()
1053 #define AVAL1U() AVAL ()
1054 #define AVAL2U() AVAL ()
1055 #define AVAL2UP() AVAL ()
1056 #define SKIP_GOTO ++pc
1057 #define GOTO_VAL() (insn_slot *) pc->datum
1058 #define PCVAL(unionval) unionval.p
1059 #define AMPAMP(label) &&label
1061 // Compile if we must. NOTE: Double-check locking.
1062 if (meth->prepared == NULL)
1064 _Jv_MutexLock (&compile_mutex);
1065 if (meth->prepared == NULL)
1066 meth->compile (insn_target);
1067 _Jv_MutexUnlock (&compile_mutex);
1070 // If we're only compiling, stop here
1074 pc = (insn_slot *) meth->prepared;
1078 #define NEXT_INSN goto *(insn_target[*pc++])
1080 #define GET1S() get1s (pc++)
1081 #define GET2S() (pc += 2, get2s (pc- 2))
1082 #define GET1U() get1u (pc++)
1083 #define GET2U() (pc += 2, get2u (pc - 2))
1084 // Note that these could be more efficient when not handling 'ldc
1087 ({ int index = get1u (pc++); \
1088 resolve_pool_entry (meth->defining_class, index).o; })
1090 ({ int index = get2u (pc); pc += 2; \
1091 resolve_pool_entry (meth->defining_class, index).o; })
1092 // Note that we don't need to resolve the pool entry here as class
1093 // constants are never wide.
1094 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1095 #define SKIP_GOTO pc += 2
1096 #define GOTO_VAL() pc - 1 + get2s (pc)
1097 #define PCVAL(unionval) unionval.i
1098 #define AMPAMP(label) NULL
1102 #endif /* DIRECT_THREADED */
1104 #define TAKE_GOTO pc = GOTO_VAL ()
1106 /* Go straight at it! the ffi raw format matches the internal
1107 stack representation exactly. At least, that's the idea.
1109 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
1111 _Jv_word *pool_data = meth->defining_class->constants.data;
1113 /* These three are temporaries for common code used by several
1116 _Jv_ResolvedMethod* rmeth;
1121 // We keep nop around. It is used if we're interpreting the
1122 // bytecodes and not doing direct threading.
1126 /* The first few instructions here are ordered according to their
1127 frequency, in the hope that this will improve code locality a
1130 insn_aload_0: // 0x2a
1138 insn_iload_1: // 0x1b
1142 insn_invokevirtual: // 0xb6
1145 int index = GET2U ();
1147 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1148 * value already is resolved. If we want to clutter up the
1149 * code here to gain a little performance, then we can check
1150 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1151 * directly. For now, I don't think it is worth it. */
1153 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1156 sp -= rmeth->stack_item_count;
1158 if (rmeth->method->accflags & Modifier::FINAL)
1160 // We can't rely on NULLCHECK working if the method is final.
1162 throw_null_pointer_exception ();
1164 // Final methods might not appear in the vtable.
1165 fun = (void (*)()) rmeth->method->ncode;
1169 NULLCHECK (sp[0].o);
1170 jobject rcv = sp[0].o;
1171 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1172 fun = (void (*)()) table->get_method (rmeth->method->index);
1175 #ifdef DIRECT_THREADED
1176 // Rewrite instruction so that we use a faster pre-resolved
1178 pc[-2].insn = &&invokevirtual_resolved;
1179 pc[-1].datum = rmeth;
1180 #endif /* DIRECT_THREADED */
1182 goto perform_invoke;
1184 #ifdef DIRECT_THREADED
1185 invokevirtual_resolved:
1188 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1189 sp -= rmeth->stack_item_count;
1191 if (rmeth->method->accflags & Modifier::FINAL)
1193 // We can't rely on NULLCHECK working if the method is final.
1195 throw_null_pointer_exception ();
1197 // Final methods might not appear in the vtable.
1198 fun = (void (*)()) rmeth->method->ncode;
1202 jobject rcv = sp[0].o;
1203 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1204 fun = (void (*)()) table->get_method (rmeth->method->index);
1207 goto perform_invoke;
1208 #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.
1362 // We could rewrite the instruction at this point.
1363 int index = INTVAL ();
1364 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1369 #endif /* DIRECT_THREADED */
1373 void *where = AVAL2UP ();
1374 memcpy (sp, where, 2*sizeof (_Jv_word));
1469 jint index = POPI();
1470 jintArray arr = (jintArray) POPA();
1471 NULLARRAYCHECK (arr);
1472 ARRAYBOUNDSCHECK (arr, index);
1473 PUSHI( elements(arr)[index] );
1479 jint index = POPI();
1480 jlongArray arr = (jlongArray) POPA();
1481 NULLARRAYCHECK (arr);
1482 ARRAYBOUNDSCHECK (arr, index);
1483 PUSHL( elements(arr)[index] );
1489 jint index = POPI();
1490 jfloatArray arr = (jfloatArray) POPA();
1491 NULLARRAYCHECK (arr);
1492 ARRAYBOUNDSCHECK (arr, index);
1493 PUSHF( elements(arr)[index] );
1499 jint index = POPI();
1500 jdoubleArray arr = (jdoubleArray) POPA();
1501 NULLARRAYCHECK (arr);
1502 ARRAYBOUNDSCHECK (arr, index);
1503 PUSHD( elements(arr)[index] );
1509 jint index = POPI();
1510 jobjectArray arr = (jobjectArray) POPA();
1511 NULLARRAYCHECK (arr);
1512 ARRAYBOUNDSCHECK (arr, index);
1513 PUSHA( elements(arr)[index] );
1519 jint index = POPI();
1520 jbyteArray arr = (jbyteArray) POPA();
1521 NULLARRAYCHECK (arr);
1522 ARRAYBOUNDSCHECK (arr, index);
1523 PUSHI( elements(arr)[index] );
1529 jint index = POPI();
1530 jcharArray arr = (jcharArray) POPA();
1531 NULLARRAYCHECK (arr);
1532 ARRAYBOUNDSCHECK (arr, index);
1533 PUSHI( elements(arr)[index] );
1539 jint index = POPI();
1540 jshortArray arr = (jshortArray) POPA();
1541 NULLARRAYCHECK (arr);
1542 ARRAYBOUNDSCHECK (arr, index);
1543 PUSHI( elements(arr)[index] );
1649 jint value = POPI();
1650 jint index = POPI();
1651 jintArray arr = (jintArray) POPA();
1652 NULLARRAYCHECK (arr);
1653 ARRAYBOUNDSCHECK (arr, index);
1654 elements(arr)[index] = value;
1660 jlong value = POPL();
1661 jint index = POPI();
1662 jlongArray arr = (jlongArray) POPA();
1663 NULLARRAYCHECK (arr);
1664 ARRAYBOUNDSCHECK (arr, index);
1665 elements(arr)[index] = value;
1671 jfloat value = POPF();
1672 jint index = POPI();
1673 jfloatArray arr = (jfloatArray) POPA();
1674 NULLARRAYCHECK (arr);
1675 ARRAYBOUNDSCHECK (arr, index);
1676 elements(arr)[index] = value;
1682 jdouble value = POPD();
1683 jint index = POPI();
1684 jdoubleArray arr = (jdoubleArray) POPA();
1685 NULLARRAYCHECK (arr);
1686 ARRAYBOUNDSCHECK (arr, index);
1687 elements(arr)[index] = value;
1693 jobject value = POPA();
1694 jint index = POPI();
1695 jobjectArray arr = (jobjectArray) POPA();
1696 NULLARRAYCHECK (arr);
1697 ARRAYBOUNDSCHECK (arr, index);
1698 _Jv_CheckArrayStore (arr, value);
1699 elements(arr)[index] = value;
1705 jbyte value = (jbyte) POPI();
1706 jint index = POPI();
1707 jbyteArray arr = (jbyteArray) POPA();
1708 NULLARRAYCHECK (arr);
1709 ARRAYBOUNDSCHECK (arr, index);
1710 elements(arr)[index] = value;
1716 jchar value = (jchar) POPI();
1717 jint index = POPI();
1718 jcharArray arr = (jcharArray) POPA();
1719 NULLARRAYCHECK (arr);
1720 ARRAYBOUNDSCHECK (arr, index);
1721 elements(arr)[index] = value;
1727 jshort value = (jshort) POPI();
1728 jint index = POPI();
1729 jshortArray arr = (jshortArray) POPA();
1730 NULLARRAYCHECK (arr);
1731 ARRAYBOUNDSCHECK (arr, index);
1732 elements(arr)[index] = value;
1750 dupx (sp, 1, 1); sp+=1;
1754 dupx (sp, 1, 2); sp+=1;
1764 dupx (sp, 2, 1); sp+=2;
1768 dupx (sp, 2, 2); sp+=2;
1773 jobject tmp1 = POPA();
1774 jobject tmp2 = POPA();
1831 jint value2 = POPI();
1832 jint value1 = POPI();
1833 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;
1869 jint value2 = POPI();
1870 jint value1 = POPI();
1871 jint res = _Jv_remI (value1, value2);
1879 jlong value2 = POPL();
1880 jlong value1 = POPL();
1881 jlong res = _Jv_remJ (value1, value2);
1888 jfloat value2 = POPF();
1889 jfloat value1 = POPF();
1890 jfloat res = __ieee754_fmod (value1, value2);
1897 jdouble value2 = POPD();
1898 jdouble value1 = POPD();
1899 jdouble res = __ieee754_fmod (value1, value2);
1906 jint value = POPI();
1913 jlong value = POPL();
1920 jfloat value = POPF();
1927 jdouble value = POPD();
1934 jint shift = (POPI() & 0x1f);
1935 jint value = POPI();
1936 PUSHI (value << shift);
1942 jint shift = (POPI() & 0x3f);
1943 jlong value = POPL();
1944 PUSHL (value << shift);
1950 jint shift = (POPI() & 0x1f);
1951 jint value = POPI();
1952 PUSHI (value >> shift);
1958 jint shift = (POPI() & 0x3f);
1959 jlong value = POPL();
1960 PUSHL (value >> shift);
1966 jint shift = (POPI() & 0x1f);
1967 _Jv_uint value = (_Jv_uint) POPI();
1968 PUSHI ((jint) (value >> shift));
1974 jint shift = (POPI() & 0x3f);
1975 _Jv_ulong value = (_Jv_ulong) POPL();
1976 PUSHL ((jlong) (value >> shift));
2006 jint index = GET1U ();
2007 jint amount = GET1S ();
2008 locals[index].i += amount;
2013 {jlong value = POPI(); PUSHL (value);}
2017 {jfloat value = POPI(); PUSHF (value);}
2021 {jdouble value = POPI(); PUSHD (value);}
2025 {jint value = POPL(); PUSHI (value);}
2029 {jfloat value = POPL(); PUSHF (value);}
2033 {jdouble value = POPL(); PUSHD (value);}
2038 using namespace java::lang;
2039 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2046 using namespace java::lang;
2047 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2053 { jdouble value = POPF (); PUSHD(value); }
2058 using namespace java::lang;
2059 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2066 using namespace java::lang;
2067 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2073 { jfloat value = POPD (); PUSHF(value); }
2077 { jbyte value = POPI (); PUSHI(value); }
2081 { jchar value = POPI (); PUSHI(value); }
2085 { jshort value = POPI (); PUSHI(value); }
2090 jlong value2 = POPL ();
2091 jlong value1 = POPL ();
2092 if (value1 > value2)
2094 else if (value1 == value2)
2110 jfloat value2 = POPF ();
2111 jfloat value1 = POPF ();
2112 if (value1 > value2)
2114 else if (value1 == value2)
2116 else if (value1 < value2)
2132 jdouble value2 = POPD ();
2133 jdouble value1 = POPD ();
2134 if (value1 > value2)
2136 else if (value1 == value2)
2138 else if (value1 < value2)
2201 jint value2 = POPI();
2202 jint value1 = POPI();
2203 if (value1 == value2)
2212 jint value2 = POPI();
2213 jint value1 = POPI();
2214 if (value1 != value2)
2223 jint value2 = POPI();
2224 jint value1 = POPI();
2225 if (value1 < value2)
2234 jint value2 = POPI();
2235 jint value1 = POPI();
2236 if (value1 >= value2)
2245 jint value2 = POPI();
2246 jint value1 = POPI();
2247 if (value1 > value2)
2256 jint value2 = POPI();
2257 jint value1 = POPI();
2258 if (value1 <= value2)
2267 jobject value2 = POPA();
2268 jobject value1 = POPA();
2269 if (value1 == value2)
2278 jobject value2 = POPA();
2279 jobject value1 = POPA();
2280 if (value1 != value2)
2288 #ifndef DIRECT_THREADED
2289 // For direct threaded, goto and goto_w are the same.
2290 pc = pc - 1 + get4 (pc);
2292 #endif /* DIRECT_THREADED */
2298 #ifndef DIRECT_THREADED
2299 // For direct threaded, jsr and jsr_w are the same.
2301 pc_t next = pc - 1 + get4 (pc);
2303 PUSHA ((jobject) pc);
2307 #endif /* DIRECT_THREADED */
2310 pc_t next = GOTO_VAL();
2312 PUSHA ((jobject) pc);
2319 jint index = GET1U ();
2320 pc = (pc_t) PEEKA (index);
2326 #ifdef DIRECT_THREADED
2327 void *def = (pc++)->datum;
2331 jint low = INTVAL ();
2332 jint high = INTVAL ();
2334 if (index < low || index > high)
2335 pc = (insn_slot *) def;
2337 pc = (insn_slot *) ((pc + index - low)->datum);
2339 pc_t base_pc = pc - 1;
2340 int index = POPI ();
2342 pc_t base = (pc_t) bytecode ();
2343 while ((pc - base) % 4 != 0)
2346 jint def = get4 (pc);
2347 jint low = get4 (pc + 4);
2348 jint high = get4 (pc + 8);
2349 if (index < low || index > high)
2352 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2353 #endif /* DIRECT_THREADED */
2359 #ifdef DIRECT_THREADED
2360 void *def = (pc++)->insn;
2364 jint npairs = INTVAL ();
2366 int max = npairs - 1;
2369 // Simple binary search...
2372 int half = (min + max) / 2;
2373 int match = pc[2 * half].int_val;
2378 pc = (insn_slot *) pc[2 * half + 1].datum;
2381 else if (index < match)
2382 // We can use HALF - 1 here because we check again on
2386 // We can use HALF + 1 here because we check again on
2390 if (index == pc[2 * min].int_val)
2391 pc = (insn_slot *) pc[2 * min + 1].datum;
2393 pc = (insn_slot *) def;
2395 unsigned char *base_pc = pc-1;
2398 unsigned char* base = bytecode ();
2399 while ((pc-base) % 4 != 0)
2402 jint def = get4 (pc);
2403 jint npairs = get4 (pc+4);
2408 // Simple binary search...
2411 int half = (min+max)/2;
2412 int match = get4 (pc+ 4*(2 + 2*half));
2416 else if (index < match)
2417 // We can use HALF - 1 here because we check again on
2421 // We can use HALF + 1 here because we check again on
2426 if (index == get4 (pc+ 4*(2 + 2*min)))
2427 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2430 #endif /* DIRECT_THREADED */
2435 *(jobject *) retp = POPA ();
2439 *(jlong *) retp = POPL ();
2443 *(jfloat *) retp = POPF ();
2447 *(jdouble *) retp = POPD ();
2451 *(jint *) retp = POPI ();
2459 jint fieldref_index = GET2U ();
2460 SAVE_PC(); // Constant pool resolution could throw.
2461 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2462 _Jv_Field *field = pool_data[fieldref_index].field;
2464 if ((field->flags & Modifier::STATIC) == 0)
2465 throw_incompatible_class_change_error
2466 (JvNewStringLatin1 ("field no longer static"));
2468 jclass type = field->type;
2470 // We rewrite the instruction once we discover what it refers
2472 void *newinsn = NULL;
2473 if (type->isPrimitive ())
2475 switch (type->size_in_bytes)
2478 PUSHI (*field->u.byte_addr);
2479 newinsn = AMPAMP (getstatic_resolved_1);
2483 if (type == JvPrimClass (char))
2485 PUSHI (*field->u.char_addr);
2486 newinsn = AMPAMP (getstatic_resolved_char);
2490 PUSHI (*field->u.short_addr);
2491 newinsn = AMPAMP (getstatic_resolved_short);
2496 PUSHI(*field->u.int_addr);
2497 newinsn = AMPAMP (getstatic_resolved_4);
2501 PUSHL(*field->u.long_addr);
2502 newinsn = AMPAMP (getstatic_resolved_8);
2508 PUSHA(*field->u.object_addr);
2509 newinsn = AMPAMP (getstatic_resolved_obj);
2512 #ifdef DIRECT_THREADED
2513 pc[-2].insn = newinsn;
2514 pc[-1].datum = field->u.addr;
2515 #endif /* DIRECT_THREADED */
2519 #ifdef DIRECT_THREADED
2520 getstatic_resolved_1:
2521 PUSHI (*(jbyte *) AVAL ());
2524 getstatic_resolved_char:
2525 PUSHI (*(jchar *) AVAL ());
2528 getstatic_resolved_short:
2529 PUSHI (*(jshort *) AVAL ());
2532 getstatic_resolved_4:
2533 PUSHI (*(jint *) AVAL ());
2536 getstatic_resolved_8:
2537 PUSHL (*(jlong *) AVAL ());
2540 getstatic_resolved_obj:
2541 PUSHA (*(jobject *) AVAL ());
2543 #endif /* DIRECT_THREADED */
2548 jint fieldref_index = GET2U ();
2549 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2550 _Jv_Field *field = pool_data[fieldref_index].field;
2552 if ((field->flags & Modifier::STATIC) != 0)
2553 throw_incompatible_class_change_error
2554 (JvNewStringLatin1 ("field is static"));
2556 jclass type = field->type;
2557 jint field_offset = field->u.boffset;
2559 jobject obj = POPA();
2562 void *newinsn = NULL;
2563 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2564 if (type->isPrimitive ())
2566 switch (type->size_in_bytes)
2569 PUSHI (val->byte_value);
2570 newinsn = AMPAMP (getfield_resolved_1);
2574 if (type == JvPrimClass (char))
2576 PUSHI (val->char_value);
2577 newinsn = AMPAMP (getfield_resolved_char);
2581 PUSHI (val->short_value);
2582 newinsn = AMPAMP (getfield_resolved_short);
2587 PUSHI (val->int_value);
2588 newinsn = AMPAMP (getfield_resolved_4);
2592 PUSHL (val->long_value);
2593 newinsn = AMPAMP (getfield_resolved_8);
2599 PUSHA (val->object_value);
2600 newinsn = AMPAMP (getfield_resolved_obj);
2603 #ifdef DIRECT_THREADED
2604 pc[-2].insn = newinsn;
2605 pc[-1].int_val = field_offset;
2606 #endif /* DIRECT_THREADED */
2610 #ifdef DIRECT_THREADED
2611 getfield_resolved_1:
2613 char *obj = (char *) POPA ();
2615 PUSHI (*(jbyte *) (obj + INTVAL ()));
2619 getfield_resolved_char:
2621 char *obj = (char *) POPA ();
2623 PUSHI (*(jchar *) (obj + INTVAL ()));
2627 getfield_resolved_short:
2629 char *obj = (char *) POPA ();
2631 PUSHI (*(jshort *) (obj + INTVAL ()));
2635 getfield_resolved_4:
2637 char *obj = (char *) POPA ();
2639 PUSHI (*(jint *) (obj + INTVAL ()));
2643 getfield_resolved_8:
2645 char *obj = (char *) POPA ();
2647 PUSHL (*(jlong *) (obj + INTVAL ()));
2651 getfield_resolved_obj:
2653 char *obj = (char *) POPA ();
2655 PUSHA (*(jobject *) (obj + INTVAL ()));
2658 #endif /* DIRECT_THREADED */
2663 jint fieldref_index = GET2U ();
2664 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2665 _Jv_Field *field = pool_data[fieldref_index].field;
2667 jclass type = field->type;
2669 // ResolvePoolEntry cannot check this
2670 if ((field->flags & Modifier::STATIC) == 0)
2671 throw_incompatible_class_change_error
2672 (JvNewStringLatin1 ("field no longer static"));
2674 void *newinsn = NULL;
2675 if (type->isPrimitive ())
2677 switch (type->size_in_bytes)
2681 jint value = POPI();
2682 *field->u.byte_addr = value;
2683 newinsn = AMPAMP (putstatic_resolved_1);
2689 jint value = POPI();
2690 *field->u.char_addr = value;
2691 newinsn = AMPAMP (putstatic_resolved_2);
2697 jint value = POPI();
2698 *field->u.int_addr = value;
2699 newinsn = AMPAMP (putstatic_resolved_4);
2705 jlong value = POPL();
2706 *field->u.long_addr = value;
2707 newinsn = AMPAMP (putstatic_resolved_8);
2714 jobject value = POPA();
2715 *field->u.object_addr = value;
2716 newinsn = AMPAMP (putstatic_resolved_obj);
2719 #ifdef DIRECT_THREADED
2720 pc[-2].insn = newinsn;
2721 pc[-1].datum = field->u.addr;
2722 #endif /* DIRECT_THREADED */
2726 #ifdef DIRECT_THREADED
2727 putstatic_resolved_1:
2728 *(jbyte *) AVAL () = POPI ();
2731 putstatic_resolved_2:
2732 *(jchar *) AVAL () = POPI ();
2735 putstatic_resolved_4:
2736 *(jint *) AVAL () = POPI ();
2739 putstatic_resolved_8:
2740 *(jlong *) AVAL () = POPL ();
2743 putstatic_resolved_obj:
2744 *(jobject *) AVAL () = POPA ();
2746 #endif /* DIRECT_THREADED */
2751 jint fieldref_index = GET2U ();
2752 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2753 _Jv_Field *field = pool_data[fieldref_index].field;
2755 jclass type = field->type;
2757 if ((field->flags & Modifier::STATIC) != 0)
2758 throw_incompatible_class_change_error
2759 (JvNewStringLatin1 ("field is static"));
2761 jint field_offset = field->u.boffset;
2763 void *newinsn = NULL;
2764 if (type->isPrimitive ())
2766 switch (type->size_in_bytes)
2770 jint value = POPI();
2771 jobject obj = POPA();
2773 *(jbyte*) ((char*)obj + field_offset) = value;
2774 newinsn = AMPAMP (putfield_resolved_1);
2780 jint value = POPI();
2781 jobject obj = POPA();
2783 *(jchar*) ((char*)obj + field_offset) = value;
2784 newinsn = AMPAMP (putfield_resolved_2);
2790 jint value = POPI();
2791 jobject obj = POPA();
2793 *(jint*) ((char*)obj + field_offset) = value;
2794 newinsn = AMPAMP (putfield_resolved_4);
2800 jlong value = POPL();
2801 jobject obj = POPA();
2803 *(jlong*) ((char*)obj + field_offset) = value;
2804 newinsn = AMPAMP (putfield_resolved_8);
2811 jobject value = POPA();
2812 jobject obj = POPA();
2814 *(jobject*) ((char*)obj + field_offset) = value;
2815 newinsn = AMPAMP (putfield_resolved_obj);
2818 #ifdef DIRECT_THREADED
2819 pc[-2].insn = newinsn;
2820 pc[-1].int_val = field_offset;
2821 #endif /* DIRECT_THREADED */
2825 #ifdef DIRECT_THREADED
2826 putfield_resolved_1:
2829 char *obj = (char *) POPA ();
2831 *(jbyte *) (obj + INTVAL ()) = val;
2835 putfield_resolved_2:
2838 char *obj = (char *) POPA ();
2840 *(jchar *) (obj + INTVAL ()) = val;
2844 putfield_resolved_4:
2847 char *obj = (char *) POPA ();
2849 *(jint *) (obj + INTVAL ()) = val;
2853 putfield_resolved_8:
2855 jlong val = POPL ();
2856 char *obj = (char *) POPA ();
2858 *(jlong *) (obj + INTVAL ()) = val;
2862 putfield_resolved_obj:
2864 jobject val = POPA ();
2865 char *obj = (char *) POPA ();
2867 *(jobject *) (obj + INTVAL ()) = val;
2870 #endif /* DIRECT_THREADED */
2875 int index = GET2U ();
2877 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2880 sp -= rmeth->stack_item_count;
2882 // We don't use NULLCHECK here because we can't rely on that
2883 // working for <init>. So instead we do an explicit test.
2887 throw_null_pointer_exception ();
2890 fun = (void (*)()) rmeth->method->ncode;
2892 #ifdef DIRECT_THREADED
2893 // Rewrite instruction so that we use a faster pre-resolved
2895 pc[-2].insn = &&invokespecial_resolved;
2896 pc[-1].datum = rmeth;
2897 #endif /* DIRECT_THREADED */
2899 goto perform_invoke;
2901 #ifdef DIRECT_THREADED
2902 invokespecial_resolved:
2905 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2906 sp -= rmeth->stack_item_count;
2907 // We don't use NULLCHECK here because we can't rely on that
2908 // working for <init>. So instead we do an explicit test.
2911 throw_null_pointer_exception ();
2913 fun = (void (*)()) rmeth->method->ncode;
2915 goto perform_invoke;
2916 #endif /* DIRECT_THREADED */
2921 int index = GET2U ();
2923 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2926 sp -= rmeth->stack_item_count;
2928 fun = (void (*)()) rmeth->method->ncode;
2930 #ifdef DIRECT_THREADED
2931 // Rewrite instruction so that we use a faster pre-resolved
2933 pc[-2].insn = &&invokestatic_resolved;
2934 pc[-1].datum = rmeth;
2935 #endif /* DIRECT_THREADED */
2937 goto perform_invoke;
2939 #ifdef DIRECT_THREADED
2940 invokestatic_resolved:
2943 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2944 sp -= rmeth->stack_item_count;
2945 fun = (void (*)()) rmeth->method->ncode;
2947 goto perform_invoke;
2948 #endif /* DIRECT_THREADED */
2950 insn_invokeinterface:
2953 int index = GET2U ();
2955 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2958 sp -= rmeth->stack_item_count;
2960 jobject rcv = sp[0].o;
2965 _Jv_LookupInterfaceMethod (rcv->getClass (),
2966 rmeth->method->name,
2967 rmeth->method->signature);
2969 #ifdef DIRECT_THREADED
2970 // Rewrite instruction so that we use a faster pre-resolved
2972 pc[-2].insn = &&invokeinterface_resolved;
2973 pc[-1].datum = rmeth;
2975 // Skip dummy bytes.
2977 #endif /* DIRECT_THREADED */
2979 goto perform_invoke;
2981 #ifdef DIRECT_THREADED
2982 invokeinterface_resolved:
2985 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2986 sp -= rmeth->stack_item_count;
2987 jobject rcv = sp[0].o;
2990 _Jv_LookupInterfaceMethod (rcv->getClass (),
2991 rmeth->method->name,
2992 rmeth->method->signature);
2994 goto perform_invoke;
2995 #endif /* DIRECT_THREADED */
3000 int index = GET2U ();
3001 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3003 /* VM spec, section 3.11.5 */
3004 if ((klass->getModifiers() & Modifier::ABSTRACT)
3005 || klass->isInterface())
3006 throw new java::lang::InstantiationException;
3007 jobject res = _Jv_AllocObject (klass);
3010 #ifdef DIRECT_THREADED
3011 pc[-2].insn = &&new_resolved;
3012 pc[-1].datum = klass;
3013 #endif /* DIRECT_THREADED */
3017 #ifdef DIRECT_THREADED
3020 jclass klass = (jclass) AVAL ();
3021 jobject res = _Jv_AllocObject (klass);
3025 #endif /* DIRECT_THREADED */
3029 int atype = GET1U ();
3031 jobject result = _Jv_NewArray (atype, size);
3039 int index = GET2U ();
3040 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3043 jobject result = _Jv_NewObjectArray (size, klass, 0);
3046 #ifdef DIRECT_THREADED
3047 pc[-2].insn = &&anewarray_resolved;
3048 pc[-1].datum = klass;
3049 #endif /* DIRECT_THREADED */
3053 #ifdef DIRECT_THREADED
3056 jclass klass = (jclass) AVAL ();
3058 jobject result = _Jv_NewObjectArray (size, klass, 0);
3062 #endif /* DIRECT_THREADED */
3066 __JArray *arr = (__JArray*)POPA();
3067 NULLARRAYCHECK (arr);
3068 PUSHI (arr->length);
3074 jobject value = POPA();
3075 throw static_cast<jthrowable>(value);
3082 jobject value = POPA();
3083 jint index = GET2U ();
3084 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3087 value = (jobject) _Jv_CheckCast (to, value);
3091 #ifdef DIRECT_THREADED
3092 pc[-2].insn = &&checkcast_resolved;
3094 #endif /* DIRECT_THREADED */
3098 #ifdef DIRECT_THREADED
3102 jobject value = POPA ();
3103 jclass to = (jclass) AVAL ();
3104 value = (jobject) _Jv_CheckCast (to, value);
3108 #endif /* DIRECT_THREADED */
3113 jobject value = POPA();
3114 jint index = GET2U ();
3115 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3117 PUSHI (to->isInstance (value));
3119 #ifdef DIRECT_THREADED
3120 pc[-2].insn = &&instanceof_resolved;
3122 #endif /* DIRECT_THREADED */
3126 #ifdef DIRECT_THREADED
3127 instanceof_resolved:
3129 jobject value = POPA ();
3130 jclass to = (jclass) AVAL ();
3131 PUSHI (to->isInstance (value));
3134 #endif /* DIRECT_THREADED */
3138 jobject value = POPA();
3140 _Jv_MonitorEnter (value);
3146 jobject value = POPA();
3148 _Jv_MonitorExit (value);
3154 jobject val = POPA();
3164 jobject val = POPA();
3172 insn_multianewarray:
3175 int kind_index = GET2U ();
3179 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3181 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3183 for (int i = dim - 1; i >= 0; i--)
3188 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3194 #ifndef DIRECT_THREADED
3197 jint the_mod_op = get1u (pc++);
3198 jint wide = get2u (pc); pc += 2;
3243 pc = (unsigned char*) PEEKA (wide);
3248 jint amount = get2s (pc); pc += 2;
3249 jint value = PEEKI (wide);
3250 POKEI (wide, value+amount);
3255 throw_internal_error ("illegal bytecode modified by wide");
3259 #endif /* DIRECT_THREADED */
3261 catch (java::lang::Throwable *ex)
3263 #ifdef DIRECT_THREADED
3264 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3266 int logical_pc = pc - 1 - bytecode ();
3268 _Jv_InterpException *exc = meth->exceptions ();
3269 jclass exc_class = ex->getClass ();
3271 for (int i = 0; i < meth->exc_count; i++)
3273 if (PCVAL (exc[i].start_pc) <= logical_pc
3274 && logical_pc < PCVAL (exc[i].end_pc))
3276 #ifdef DIRECT_THREADED
3277 jclass handler = (jclass) exc[i].handler_type.p;
3279 jclass handler = NULL;
3280 if (exc[i].handler_type.i != 0)
3281 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3282 exc[i].handler_type.i)).clazz;
3283 #endif /* DIRECT_THREADED */
3285 if (handler == NULL || handler->isAssignableFrom (exc_class))
3287 #ifdef DIRECT_THREADED
3288 pc = (insn_slot *) exc[i].handler_pc.p;
3290 pc = bytecode () + exc[i].handler_pc.i;
3291 #endif /* DIRECT_THREADED */
3293 sp++->o = ex; // Push exception.
3299 // No handler, so re-throw.
3305 throw_internal_error (const char *msg)
3307 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3311 throw_incompatible_class_change_error (jstring msg)
3313 throw new java::lang::IncompatibleClassChangeError (msg);
3317 throw_null_pointer_exception ()
3319 throw new java::lang::NullPointerException;
3322 /* Look up source code line number for given bytecode (or direct threaded
3325 _Jv_InterpMethod::get_source_line(pc_t mpc)
3327 int line = line_table_len > 0 ? line_table[0].line : -1;
3328 for (int i = 1; i < line_table_len; i++)
3329 if (line_table[i].pc > mpc)
3332 line = line_table[i].line;
3337 /** Do static initialization for fields with a constant initializer */
3339 _Jv_InitField (jobject obj, jclass klass, int index)
3341 using namespace java::lang::reflect;
3343 if (obj != 0 && klass == 0)
3344 klass = obj->getClass ();
3346 if (!_Jv_IsInterpretedClass (klass))
3349 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3351 _Jv_Field * field = (&klass->fields[0]) + index;
3353 if (index > klass->field_count)
3354 throw_internal_error ("field out of range");
3356 int init = iclass->field_initializers[index];
3360 _Jv_Constants *pool = &klass->constants;
3361 int tag = pool->tags[init];
3363 if (! field->isResolved ())
3364 throw_internal_error ("initializing unresolved field");
3366 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3367 throw_internal_error ("initializing non-static field with no object");
3371 if ((field->flags & Modifier::STATIC) != 0)
3372 addr = (void*) field->u.addr;
3374 addr = (void*) (((char*)obj) + field->u.boffset);
3378 case JV_CONSTANT_String:
3381 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3382 pool->data[init].string = str;
3383 pool->tags[init] = JV_CONSTANT_ResolvedString;
3387 case JV_CONSTANT_ResolvedString:
3388 if (! (field->type == &java::lang::String::class$
3389 || field->type == &java::lang::Class::class$))
3390 throw_class_format_error ("string initialiser to non-string field");
3392 *(jstring*)addr = pool->data[init].string;
3395 case JV_CONSTANT_Integer:
3397 int value = pool->data[init].i;
3399 if (field->type == JvPrimClass (boolean))
3400 *(jboolean*)addr = (jboolean)value;
3402 else if (field->type == JvPrimClass (byte))
3403 *(jbyte*)addr = (jbyte)value;
3405 else if (field->type == JvPrimClass (char))
3406 *(jchar*)addr = (jchar)value;
3408 else if (field->type == JvPrimClass (short))
3409 *(jshort*)addr = (jshort)value;
3411 else if (field->type == JvPrimClass (int))
3412 *(jint*)addr = (jint)value;
3415 throw_class_format_error ("erroneous field initializer");
3419 case JV_CONSTANT_Long:
3420 if (field->type != JvPrimClass (long))
3421 throw_class_format_error ("erroneous field initializer");
3423 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3426 case JV_CONSTANT_Float:
3427 if (field->type != JvPrimClass (float))
3428 throw_class_format_error ("erroneous field initializer");
3430 *(jfloat*)addr = pool->data[init].f;
3433 case JV_CONSTANT_Double:
3434 if (field->type != JvPrimClass (double))
3435 throw_class_format_error ("erroneous field initializer");
3437 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3441 throw_class_format_error ("erroneous field initializer");
3445 inline static unsigned char*
3446 skip_one_type (unsigned char* ptr)
3457 do { ch = *ptr++; } while (ch != ';');
3464 get_ffi_type_from_signature (unsigned char* ptr)
3470 return &ffi_type_pointer;
3474 // On some platforms a bool is a byte, on others an int.
3475 if (sizeof (jboolean) == sizeof (jbyte))
3476 return &ffi_type_sint8;
3479 JvAssert (sizeof (jbyte) == sizeof (jint));
3480 return &ffi_type_sint32;
3485 return &ffi_type_sint8;
3489 return &ffi_type_uint16;
3493 return &ffi_type_sint16;
3497 return &ffi_type_sint32;
3501 return &ffi_type_sint64;
3505 return &ffi_type_float;
3509 return &ffi_type_double;
3513 return &ffi_type_void;
3517 throw_internal_error ("unknown type in signature");
3520 /* this function yields the number of actual arguments, that is, if the
3521 * function is non-static, then one is added to the number of elements
3522 * found in the signature */
3525 _Jv_count_arguments (_Jv_Utf8Const *signature,
3528 unsigned char *ptr = (unsigned char*) signature->chars();
3529 int arg_count = staticp ? 0 : 1;
3531 /* first, count number of arguments */
3539 ptr = skip_one_type (ptr);
3546 /* This beast will build a cif, given the signature. Memory for
3547 * the cif itself and for the argument types must be allocated by the
3552 init_cif (_Jv_Utf8Const* signature,
3556 ffi_type **arg_types,
3559 unsigned char *ptr = (unsigned char*) signature->chars();
3561 int arg_index = 0; // arg number
3562 int item_count = 0; // stack-item count
3567 arg_types[arg_index++] = &ffi_type_pointer;
3577 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3579 if (*ptr == 'J' || *ptr == 'D')
3584 ptr = skip_one_type (ptr);
3589 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3591 ptr = skip_one_type (ptr);
3592 if (ptr != (unsigned char*)signature->chars() + signature->len())
3593 throw_internal_error ("did not find end of signature");
3595 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3596 arg_count, rtype, arg_types) != FFI_OK)
3597 throw_internal_error ("ffi_prep_cif failed");
3599 if (rtype_p != NULL)
3605 #if FFI_NATIVE_RAW_API
3606 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3607 # define FFI_RAW_SIZE ffi_raw_size
3609 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3610 # define FFI_RAW_SIZE ffi_java_raw_size
3613 /* we put this one here, and not in interpret.cc because it
3614 * calls the utility routines _Jv_count_arguments
3615 * which are static to this module. The following struct defines the
3616 * layout we use for the stubs, it's only used in the ncode method. */
3619 ffi_raw_closure closure;
3621 ffi_type *arg_types[0];
3624 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3627 _Jv_InterpMethod::ncode ()
3629 using namespace java::lang::reflect;
3631 if (self->ncode != 0)
3634 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3635 int arg_count = _Jv_count_arguments (self->signature, staticp);
3637 ncode_closure *closure =
3638 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3639 + arg_count * sizeof (ffi_type*));
3641 init_cif (self->signature,
3645 &closure->arg_types[0],
3648 ffi_closure_fun fun;
3650 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3652 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3654 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3657 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3659 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3664 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3666 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3669 FFI_PREP_RAW_CLOSURE (&closure->closure,
3674 self->ncode = (void*)closure;
3678 #ifdef DIRECT_THREADED
3679 /* Find the index of the given insn in the array of insn slots
3680 for this method. Returns -1 if not found. */
3682 _Jv_InterpMethod::insn_index (pc_t pc)
3685 jlong right = number_insn_slots;
3686 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
3690 jlong mid = (left + right) / 2;
3691 if (&slots[mid] == pc)
3694 if (pc < &slots[mid])
3702 #endif // DIRECT_THREADED
3705 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
3706 jintArray& line_numbers,
3707 jlongArray& code_indices)
3709 #ifdef DIRECT_THREADED
3710 /* For the DIRECT_THREADED case, if the method has not yet been
3711 * compiled, the linetable will change to insn slots instead of
3712 * bytecode PCs. It is probably easiest, in this case, to simply
3713 * compile the method and guarantee that we are using insn
3716 _Jv_CompileMethod (this);
3718 if (line_table_len > 0)
3721 end = number_insn_slots;
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] = insn_index (line_table[i].pc);
3733 #else // !DIRECT_THREADED
3734 if (line_table_len > 0)
3738 line_numbers = JvNewIntArray (line_table_len);
3739 code_indices = JvNewLongArray (line_table_len);
3741 jint* lines = elements (line_numbers);
3742 jlong* indices = elements (code_indices);
3743 for (int i = 0; i < line_table_len; ++i)
3745 lines[i] = line_table[i].line;
3746 indices[i] = (jlong) line_table[i].bytecode_pc;
3749 #endif // !DIRECT_THREADED
3753 _Jv_JNIMethod::ncode ()
3755 using namespace java::lang::reflect;
3757 if (self->ncode != 0)
3760 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3761 int arg_count = _Jv_count_arguments (self->signature, staticp);
3763 ncode_closure *closure =
3764 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3765 + arg_count * sizeof (ffi_type*));
3768 init_cif (self->signature,
3772 &closure->arg_types[0],
3775 ffi_closure_fun fun;
3777 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3779 // Initialize the argument types and CIF that represent the actual
3780 // underlying JNI function.
3782 if ((self->accflags & Modifier::STATIC))
3784 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3785 * sizeof (ffi_type *));
3787 jni_arg_types[offset++] = &ffi_type_pointer;
3788 if ((self->accflags & Modifier::STATIC))
3789 jni_arg_types[offset++] = &ffi_type_pointer;
3790 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3791 arg_count * sizeof (ffi_type *));
3793 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3794 extra_args + arg_count, rtype,
3795 jni_arg_types) != FFI_OK)
3796 throw_internal_error ("ffi_prep_cif failed for JNI function");
3798 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3800 // FIXME: for now we assume that all native methods for
3801 // interpreted code use JNI.
3802 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3804 FFI_PREP_RAW_CLOSURE (&closure->closure,
3809 self->ncode = (void *) closure;
3814 throw_class_format_error (jstring msg)
3817 ? new java::lang::ClassFormatError (msg)
3818 : new java::lang::ClassFormatError);
3822 throw_class_format_error (const char *msg)
3824 throw_class_format_error (JvNewStringLatin1 (msg));
3830 _Jv_InterpreterEngine::do_verify (jclass klass)
3832 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3833 for (int i = 0; i < klass->method_count; i++)
3835 using namespace java::lang::reflect;
3836 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3837 _Jv_ushort accflags = klass->methods[i].accflags;
3838 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3840 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3841 _Jv_VerifyMethod (im);
3847 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3849 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3850 for (int i = 0; i < klass->method_count; i++)
3852 // Just skip abstract methods. This is particularly important
3853 // because we don't resize the interpreted_methods array when
3854 // miranda methods are added to it.
3855 if ((klass->methods[i].accflags
3856 & java::lang::reflect::Modifier::ABSTRACT)
3860 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3862 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3865 // You might think we could use a virtual `ncode' method in
3866 // the _Jv_MethodBase and unify the native and non-native
3867 // cases. Well, we can't, because we don't allocate these
3868 // objects using `new', and thus they don't get a vtable.
3869 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3870 klass->methods[i].ncode = jnim->ncode ();
3872 else if (imeth != 0) // it could be abstract
3874 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3875 klass->methods[i].ncode = im->ncode ();
3881 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3885 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3887 // Splitting the allocations here lets us scan reference fields and
3888 // avoid scanning non-reference fields. How reference fields are
3889 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
3890 // means that this memory will be scanned conservatively (same
3891 // difference, since we know all the contents here are pointers).
3892 // Then we put pointers into this memory into the 'fields'
3893 // structure. Most of these are interior pointers, which is ok (but
3894 // even so the pointer to the first reference field will be used and
3895 // that is not an interior pointer). The 'fields' array is also
3896 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
3897 // be scanned. A pointer to this array is held by Class and thus
3898 // seen by the collector.
3899 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
3900 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
3902 for (int i = 0; i < klass->field_count; i++)
3904 _Jv_Field *field = &klass->fields[i];
3906 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
3909 char *base = field->isRef() ? reference_fields : non_reference_fields;
3910 field->u.addr = base + field->u.boffset;
3912 if (iclass->field_initializers[i] != 0)
3914 _Jv_Linker::resolve_field (field, klass->loader);
3915 _Jv_InitField (0, klass, i);
3919 // Now we don't need the field_initializers anymore, so let the
3920 // collector get rid of it.
3921 iclass->field_initializers = 0;
3924 _Jv_ResolvedMethod *
3925 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3928 int arg_count = _Jv_count_arguments (method->signature, staticp);
3930 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3931 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3932 + arg_count*sizeof (ffi_type*));
3934 result->stack_item_count
3935 = init_cif (method->signature,
3939 &result->arg_types[0],
3942 result->method = method;
3943 result->klass = klass;
3949 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3951 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3952 for (int i = 0; i < klass->method_count; i++)
3954 // Just skip abstract methods. This is particularly important
3955 // because we don't resize the interpreted_methods array when
3956 // miranda methods are added to it.
3957 if ((klass->methods[i].accflags
3958 & java::lang::reflect::Modifier::ABSTRACT)
3961 // Miranda method additions mean that the `methods' array moves.
3962 // We cache a pointer into this array, so we have to update.
3963 iclass->interpreted_methods[i]->self = &klass->methods[i];
3967 #ifdef DIRECT_THREADED
3969 _Jv_CompileMethod (_Jv_InterpMethod* method)
3971 if (method->prepared == NULL)
3972 _Jv_InterpMethod::run (NULL, NULL, method);
3974 #endif // DIRECT_THREADED
3976 #endif // INTERPRETER