1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
16 #pragma implementation "java-interp.h"
19 #include <java-cpool.h>
20 #include <java-interp.h>
21 #include <java/lang/System.h>
22 #include <java/lang/String.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/StringBuffer.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/VirtualMachineError.h>
29 #include <java/lang/InternalError.h>
30 #include <java/lang/NullPointerException.h>
31 #include <java/lang/ArithmeticException.h>
32 #include <java/lang/IncompatibleClassChangeError.h>
33 #include <java/lang/InstantiationException.h>
34 #include <java/lang/Thread.h>
35 #include <java-insns.h>
36 #include <java-signal.h>
37 #include <java/lang/ClassFormatError.h>
38 #include <execution.h>
39 #include <java/lang/reflect/Modifier.h>
43 // Execution engine for interpreted code.
44 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
50 static void throw_internal_error (char *msg)
51 __attribute__ ((__noreturn__));
52 static void throw_incompatible_class_change_error (jstring msg)
53 __attribute__ ((__noreturn__));
55 static void throw_null_pointer_exception ()
56 __attribute__ ((__noreturn__));
59 static void throw_class_format_error (jstring msg)
60 __attribute__ ((__noreturn__));
61 static void throw_class_format_error (char *msg)
62 __attribute__ ((__noreturn__));
64 #ifdef DIRECT_THREADED
65 // Lock to ensure that methods are not compiled concurrently.
66 // We could use a finer-grained lock here, however it is not safe to use
67 // the Class monitor as user code in another thread could hold it.
68 static _Jv_Mutex_t compile_mutex;
73 _Jv_MutexInit (&compile_mutex);
76 void _Jv_InitInterpreter() {}
79 extern "C" double __ieee754_fmod (double,double);
81 static inline void dupx (_Jv_word *sp, int n, int x)
83 // first "slide" n+x elements n to the right
85 for (int i = 0; i < n+x; i++)
87 sp[(top-i)] = sp[(top-i)-n];
90 // next, copy the n top elements, n+x down
91 for (int i = 0; i < n; i++)
93 sp[top-(n+x)-i] = sp[top-i];
97 // Used to convert from floating types to integral types.
98 template<typename TO, typename FROM>
100 convert (FROM val, TO min, TO max)
103 if (val >= (FROM) max)
105 else if (val <= (FROM) min)
114 #define PUSHA(V) (sp++)->o = (V)
115 #define PUSHI(V) (sp++)->i = (V)
116 #define PUSHF(V) (sp++)->f = (V)
117 #if SIZEOF_VOID_P == 8
118 # define PUSHL(V) (sp->l = (V), sp += 2)
119 # define PUSHD(V) (sp->d = (V), sp += 2)
121 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
122 (sp++)->ia[0] = w2.ia[0]; \
123 (sp++)->ia[0] = w2.ia[1]; } while (0)
124 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
125 (sp++)->ia[0] = w2.ia[0]; \
126 (sp++)->ia[0] = w2.ia[1]; } while (0)
129 #define POPA() ((--sp)->o)
130 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
131 #define POPF() ((jfloat) (--sp)->f)
132 #if SIZEOF_VOID_P == 8
133 # define POPL() (sp -= 2, (jlong) sp->l)
134 # define POPD() (sp -= 2, (jdouble) sp->d)
136 # define POPL() ({ _Jv_word2 w2; \
137 w2.ia[1] = (--sp)->ia[0]; \
138 w2.ia[0] = (--sp)->ia[0]; w2.l; })
139 # define POPD() ({ _Jv_word2 w2; \
140 w2.ia[1] = (--sp)->ia[0]; \
141 w2.ia[0] = (--sp)->ia[0]; w2.d; })
144 #define LOADA(I) (sp++)->o = locals[I].o
145 #define LOADI(I) (sp++)->i = locals[I].i
146 #define LOADF(I) (sp++)->f = locals[I].f
147 #if SIZEOF_VOID_P == 8
148 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
149 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
151 # define LOADL(I) do { jint __idx = (I); \
152 (sp++)->ia[0] = locals[__idx].ia[0]; \
153 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
155 # define LOADD(I) LOADL(I)
158 #define STOREA(I) locals[I].o = (--sp)->o
159 #define STOREI(I) locals[I].i = (--sp)->i
160 #define STOREF(I) locals[I].f = (--sp)->f
161 #if SIZEOF_VOID_P == 8
162 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
163 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
165 # define STOREL(I) do { jint __idx = (I); \
166 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
167 locals[__idx].ia[0] = (--sp)->ia[0]; \
169 # define STORED(I) STOREL(I)
172 #define PEEKI(I) (locals+(I))->i
173 #define PEEKA(I) (locals+(I))->o
175 #define POKEI(I,V) ((locals+(I))->i = (V))
178 #define BINOPI(OP) { \
179 jint value2 = POPI(); \
180 jint value1 = POPI(); \
181 PUSHI(value1 OP value2); \
184 #define BINOPF(OP) { \
185 jfloat value2 = POPF(); \
186 jfloat value1 = POPF(); \
187 PUSHF(value1 OP value2); \
190 #define BINOPL(OP) { \
191 jlong value2 = POPL(); \
192 jlong value1 = POPL(); \
193 PUSHL(value1 OP value2); \
196 #define BINOPD(OP) { \
197 jdouble value2 = POPD(); \
198 jdouble value1 = POPD(); \
199 PUSHD(value1 OP value2); \
202 static inline jint get1s(unsigned char* loc) {
203 return *(signed char*)loc;
206 static inline jint get1u(unsigned char* loc) {
210 static inline jint get2s(unsigned char* loc) {
211 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
214 static inline jint get2u(unsigned char* loc) {
215 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
218 static jint get4(unsigned char* loc) {
219 return (((jint)(loc[0])) << 24)
220 | (((jint)(loc[1])) << 16)
221 | (((jint)(loc[2])) << 8)
222 | (((jint)(loc[3])) << 0);
225 #define SAVE_PC() frame_desc.pc = pc
228 #define NULLCHECK(X) SAVE_PC()
229 #define NULLARRAYCHECK(X) SAVE_PC()
231 #define NULLCHECK(X) \
232 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
233 #define NULLARRAYCHECK(X) \
234 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
237 #define ARRAYBOUNDSCHECK(array, index) \
240 if (((unsigned) index) >= (unsigned) (array->length)) \
241 _Jv_ThrowBadArrayIndex (index); \
246 _Jv_InterpMethod::run_normal (ffi_cif *,
251 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
252 run (ret, args, _this);
256 _Jv_InterpMethod::run_synch_object (ffi_cif *,
261 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
263 jobject rcv = (jobject) args[0].ptr;
264 JvSynchronize mutex (rcv);
266 run (ret, args, _this);
270 _Jv_InterpMethod::run_class (ffi_cif *,
275 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
276 _Jv_InitClass (_this->defining_class);
277 run (ret, args, _this);
281 _Jv_InterpMethod::run_synch_class (ffi_cif *,
286 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
288 jclass sync = _this->defining_class;
289 _Jv_InitClass (sync);
290 JvSynchronize mutex (sync);
292 run (ret, args, _this);
295 #ifdef DIRECT_THREADED
296 // "Compile" a method by turning it from bytecode to direct-threaded
299 _Jv_InterpMethod::compile (const void * const *insn_targets)
301 insn_slot *insns = NULL;
303 unsigned char *codestart = bytecode ();
304 unsigned char *end = codestart + code_length;
305 _Jv_word *pool_data = defining_class->constants.data;
307 #define SET_ONE(Field, Value) \
313 insns[next++].Field = Value; \
317 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
318 #define SET_INT(Value) SET_ONE (int_val, Value)
319 #define SET_DATUM(Value) SET_ONE (datum, Value)
321 // Map from bytecode PC to slot in INSNS.
322 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
323 for (int i = 0; i < code_length; ++i)
326 for (int i = 0; i < 2; ++i)
328 jboolean first_pass = i == 0;
332 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
336 unsigned char *pc = codestart;
339 int base_pc_val = pc - codestart;
341 pc_mapping[base_pc_val] = next;
343 java_opcode opcode = (java_opcode) *pc++;
345 if (opcode == op_nop)
347 SET_INSN (insn_targets[opcode]);
488 case op_monitorenter:
498 // No argument, nothing else to do.
502 SET_INT (get1s (pc));
508 int index = get1u (pc);
510 // For an unresolved class we want to delay resolution
512 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
515 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
519 SET_DATUM (pool_data[index].o);
535 SET_INT (get1u (pc));
540 SET_INT (get1u (pc));
541 SET_INT (get1s (pc + 1));
547 int index = get2u (pc);
549 // For an unresolved class we want to delay resolution
551 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
554 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
558 SET_DATUM (pool_data[index].o);
564 int index = get2u (pc);
566 SET_DATUM (&pool_data[index]);
571 SET_INT (get2s (pc));
583 case op_invokespecial:
584 case op_invokestatic:
585 case op_invokevirtual:
586 SET_INT (get2u (pc));
590 case op_multianewarray:
591 SET_INT (get2u (pc));
592 SET_INT (get1u (pc + 2));
615 int offset = get2s (pc);
618 int new_pc = base_pc_val + offset;
620 bool orig_was_goto = opcode == op_goto;
622 // Thread jumps. We limit the loop count; this lets
623 // us avoid infinite loops if the bytecode contains
624 // such. `10' is arbitrary.
626 while (codestart[new_pc] == op_goto && count-- > 0)
627 new_pc += get2s (&codestart[new_pc + 1]);
629 // If the jump takes us to a `return' instruction and
630 // the original branch was an unconditional goto, then
631 // we hoist the return.
632 opcode = (java_opcode) codestart[new_pc];
634 && (opcode == op_ireturn || opcode == op_lreturn
635 || opcode == op_freturn || opcode == op_dreturn
636 || opcode == op_areturn || opcode == op_return))
639 SET_INSN (insn_targets[opcode]);
642 SET_DATUM (&insns[pc_mapping[new_pc]]);
648 while ((pc - codestart) % 4 != 0)
651 jint def = get4 (pc);
652 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
658 int high = get4 (pc);
662 for (int i = low; i <= high; ++i)
664 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
670 case op_lookupswitch:
672 while ((pc - codestart) % 4 != 0)
675 jint def = get4 (pc);
676 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
679 jint npairs = get4 (pc);
685 jint match = get4 (pc);
686 jint offset = get4 (pc + 4);
688 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
694 case op_invokeinterface:
696 jint index = get2u (pc);
698 // We ignore the next two bytes.
706 opcode = (java_opcode) get1u (pc);
708 jint val = get2u (pc);
711 // We implement narrow and wide instructions using the
712 // same code in the interpreter. So we rewrite the
713 // instruction slot here.
715 insns[next - 1].insn = (void *) insn_targets[opcode];
718 if (opcode == op_iinc)
720 SET_INT (get2s (pc));
729 jint offset = get4 (pc);
731 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
735 // Some "can't happen" cases that we include for
736 // error-checking purposes.
754 case op_getstatic_2s:
755 case op_getstatic_2u:
766 // Now update exceptions.
767 _Jv_InterpException *exc = exceptions ();
768 for (int i = 0; i < exc_count; ++i)
770 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
771 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
772 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
774 = (_Jv_Linker::resolve_pool_entry (defining_class,
775 exc[i].handler_type.i)).clazz;
776 exc[i].handler_type.p = handler;
779 // Translate entries in the LineNumberTable from bytecode PC's to direct
780 // threaded interpreter instruction values.
781 for (int i = 0; i < line_table_len; i++)
783 int byte_pc = line_table[i].bytecode_pc;
784 // It isn't worth throwing an exception if this table is
785 // corrupted, but at the same time we don't want a crash.
786 if (byte_pc < 0 || byte_pc >= code_length)
788 line_table[i].pc = &insns[pc_mapping[byte_pc]];
793 #endif /* DIRECT_THREADED */
796 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
798 using namespace java::lang::reflect;
800 // FRAME_DESC registers this particular invocation as the top-most
801 // interpreter frame. This lets the stack tracing code (for
802 // Throwable) print information about the method being interpreted
803 // rather than about the interpreter itself. FRAME_DESC has a
804 // destructor so it cleans up automatically when the interpreter
806 java::lang::Thread *thread = java::lang::Thread::currentThread();
807 _Jv_InterpFrame frame_desc (meth,
808 (_Jv_InterpFrame **) &thread->interp_frame);
810 _Jv_word stack[meth->max_stack];
811 _Jv_word *sp = stack;
813 _Jv_word locals[meth->max_locals];
815 /* Go straight at it! the ffi raw format matches the internal
816 stack representation exactly. At least, that's the idea.
818 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
820 _Jv_word *pool_data = meth->defining_class->constants.data;
822 /* These three are temporaries for common code used by several
825 _Jv_ResolvedMethod* rmeth;
828 #define INSN_LABEL(op) &&insn_##op
830 static const void *const insn_target[] =
833 INSN_LABEL(aconst_null),
834 INSN_LABEL(iconst_m1),
835 INSN_LABEL(iconst_0),
836 INSN_LABEL(iconst_1),
837 INSN_LABEL(iconst_2),
838 INSN_LABEL(iconst_3),
839 INSN_LABEL(iconst_4),
840 INSN_LABEL(iconst_5),
841 INSN_LABEL(lconst_0),
842 INSN_LABEL(lconst_1),
843 INSN_LABEL(fconst_0),
844 INSN_LABEL(fconst_1),
845 INSN_LABEL(fconst_2),
846 INSN_LABEL(dconst_0),
847 INSN_LABEL(dconst_1),
891 INSN_LABEL(istore_0),
892 INSN_LABEL(istore_1),
893 INSN_LABEL(istore_2),
894 INSN_LABEL(istore_3),
895 INSN_LABEL(lstore_0),
896 INSN_LABEL(lstore_1),
897 INSN_LABEL(lstore_2),
898 INSN_LABEL(lstore_3),
899 INSN_LABEL(fstore_0),
900 INSN_LABEL(fstore_1),
901 INSN_LABEL(fstore_2),
902 INSN_LABEL(fstore_3),
903 INSN_LABEL(dstore_0),
904 INSN_LABEL(dstore_1),
905 INSN_LABEL(dstore_2),
906 INSN_LABEL(dstore_3),
907 INSN_LABEL(astore_0),
908 INSN_LABEL(astore_1),
909 INSN_LABEL(astore_2),
910 INSN_LABEL(astore_3),
991 INSN_LABEL(if_icmpeq),
992 INSN_LABEL(if_icmpne),
993 INSN_LABEL(if_icmplt),
994 INSN_LABEL(if_icmpge),
995 INSN_LABEL(if_icmpgt),
996 INSN_LABEL(if_icmple),
997 INSN_LABEL(if_acmpeq),
998 INSN_LABEL(if_acmpne),
1002 INSN_LABEL(tableswitch),
1003 INSN_LABEL(lookupswitch),
1004 INSN_LABEL(ireturn),
1005 INSN_LABEL(lreturn),
1006 INSN_LABEL(freturn),
1007 INSN_LABEL(dreturn),
1008 INSN_LABEL(areturn),
1010 INSN_LABEL(getstatic),
1011 INSN_LABEL(putstatic),
1012 INSN_LABEL(getfield),
1013 INSN_LABEL(putfield),
1014 INSN_LABEL(invokevirtual),
1015 INSN_LABEL(invokespecial),
1016 INSN_LABEL(invokestatic),
1017 INSN_LABEL(invokeinterface),
1020 INSN_LABEL(newarray),
1021 INSN_LABEL(anewarray),
1022 INSN_LABEL(arraylength),
1024 INSN_LABEL(checkcast),
1025 INSN_LABEL(instanceof),
1026 INSN_LABEL(monitorenter),
1027 INSN_LABEL(monitorexit),
1028 #ifdef DIRECT_THREADED
1033 INSN_LABEL(multianewarray),
1035 INSN_LABEL(ifnonnull),
1038 #ifdef DIRECT_THREADED
1039 INSN_LABEL (ldc_class)
1047 #ifdef DIRECT_THREADED
1049 #define NEXT_INSN goto *((pc++)->insn)
1050 #define INTVAL() ((pc++)->int_val)
1051 #define AVAL() ((pc++)->datum)
1053 #define GET1S() INTVAL ()
1054 #define GET2S() INTVAL ()
1055 #define GET1U() INTVAL ()
1056 #define GET2U() INTVAL ()
1057 #define AVAL1U() AVAL ()
1058 #define AVAL2U() AVAL ()
1059 #define AVAL2UP() AVAL ()
1060 #define SKIP_GOTO ++pc
1061 #define GOTO_VAL() (insn_slot *) pc->datum
1062 #define PCVAL(unionval) unionval.p
1063 #define AMPAMP(label) &&label
1065 // Compile if we must. NOTE: Double-check locking.
1066 if (meth->prepared == NULL)
1068 _Jv_MutexLock (&compile_mutex);
1069 if (meth->prepared == NULL)
1070 meth->compile (insn_target);
1071 _Jv_MutexUnlock (&compile_mutex);
1073 pc = (insn_slot *) meth->prepared;
1077 #define NEXT_INSN goto *(insn_target[*pc++])
1079 #define GET1S() get1s (pc++)
1080 #define GET2S() (pc += 2, get2s (pc- 2))
1081 #define GET1U() get1u (pc++)
1082 #define GET2U() (pc += 2, get2u (pc - 2))
1083 // Note that these could be more efficient when not handling 'ldc
1086 ({ int index = get1u (pc++); \
1087 resolve_pool_entry (meth->defining_class, index).o; })
1089 ({ int index = get2u (pc); pc += 2; \
1090 resolve_pool_entry (meth->defining_class, index).o; })
1091 // Note that we don't need to resolve the pool entry here as class
1092 // constants are never wide.
1093 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1094 #define SKIP_GOTO pc += 2
1095 #define GOTO_VAL() pc - 1 + get2s (pc)
1096 #define PCVAL(unionval) unionval.i
1097 #define AMPAMP(label) NULL
1101 #endif /* DIRECT_THREADED */
1103 #define TAKE_GOTO pc = GOTO_VAL ()
1107 // We keep nop around. It is used if we're interpreting the
1108 // bytecodes and not doing direct threading.
1112 /* The first few instructions here are ordered according to their
1113 frequency, in the hope that this will improve code locality a
1116 insn_aload_0: // 0x2a
1124 insn_iload_1: // 0x1b
1128 insn_invokevirtual: // 0xb6
1130 int index = GET2U ();
1132 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1133 * value already is resolved. If we want to clutter up the
1134 * code here to gain a little performance, then we can check
1135 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1136 * directly. For now, I don't think it is worth it. */
1139 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1142 sp -= rmeth->stack_item_count;
1143 // We don't use NULLCHECK here because we can't rely on that
1144 // working if the method is final. So instead we do an
1148 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1149 throw new java::lang::NullPointerException;
1152 if (rmeth->vtable_index == -1)
1154 // final methods do not appear in the vtable,
1155 // if it does not appear in the superclass.
1156 fun = (void (*)()) rmeth->method->ncode;
1160 jobject rcv = sp[0].o;
1161 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1162 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1165 #ifdef DIRECT_THREADED
1166 // Rewrite instruction so that we use a faster pre-resolved
1168 pc[-2].insn = &&invokevirtual_resolved;
1169 pc[-1].datum = rmeth;
1170 #endif /* DIRECT_THREADED */
1172 goto perform_invoke;
1174 #ifdef DIRECT_THREADED
1175 invokevirtual_resolved:
1177 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1178 sp -= rmeth->stack_item_count;
1179 // We don't use NULLCHECK here because we can't rely on that
1180 // working if the method is final. So instead we do an
1185 throw new java::lang::NullPointerException;
1188 if (rmeth->vtable_index == -1)
1190 // final methods do not appear in the vtable,
1191 // if it does not appear in the superclass.
1192 fun = (void (*)()) rmeth->method->ncode;
1196 jobject rcv = sp[0].o;
1197 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1198 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1201 goto perform_invoke;
1202 #endif /* DIRECT_THREADED */
1208 /* here goes the magic again... */
1209 ffi_cif *cif = &rmeth->cif;
1210 ffi_raw *raw = (ffi_raw*) sp;
1214 #if FFI_NATIVE_RAW_API
1215 /* We assume that this is only implemented if it's correct */
1216 /* to use it here. On a 64 bit machine, it never is. */
1217 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1219 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1222 int rtype = cif->rtype->type;
1224 /* the likelyhood of object, int, or void return is very high,
1225 * so those are checked before the switch */
1226 if (rtype == FFI_TYPE_POINTER)
1228 PUSHA (rvalue.object_value);
1230 else if (rtype == FFI_TYPE_SINT32)
1232 PUSHI (rvalue.int_value);
1234 else if (rtype == FFI_TYPE_VOID)
1242 case FFI_TYPE_SINT8:
1243 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1246 case FFI_TYPE_SINT16:
1247 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1250 case FFI_TYPE_UINT16:
1251 PUSHI (rvalue.int_value & 0xffff);
1254 case FFI_TYPE_FLOAT:
1255 PUSHF (rvalue.float_value);
1258 case FFI_TYPE_DOUBLE:
1259 PUSHD (rvalue.double_value);
1262 case FFI_TYPE_SINT64:
1263 PUSHL (rvalue.long_value);
1267 throw_internal_error ("unknown return type in invokeXXX");
1334 // For direct threaded, bipush and sipush are the same.
1335 #ifndef DIRECT_THREADED
1338 #endif /* DIRECT_THREADED */
1344 // For direct threaded, ldc and ldc_w are the same.
1345 #ifndef DIRECT_THREADED
1346 PUSHA ((jobject) AVAL1U ());
1348 #endif /* DIRECT_THREADED */
1350 PUSHA ((jobject) AVAL2U ());
1353 #ifdef DIRECT_THREADED
1354 // For direct threaded we have a separate 'ldc class' operation.
1357 // We could rewrite the instruction at this point.
1358 int index = INTVAL ();
1359 jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1364 #endif /* DIRECT_THREADED */
1368 void *where = AVAL2UP ();
1369 memcpy (sp, where, 2*sizeof (_Jv_word));
1464 jint index = POPI();
1465 jintArray arr = (jintArray) POPA();
1466 NULLARRAYCHECK (arr);
1467 ARRAYBOUNDSCHECK (arr, index);
1468 PUSHI( elements(arr)[index] );
1474 jint index = POPI();
1475 jlongArray arr = (jlongArray) POPA();
1476 NULLARRAYCHECK (arr);
1477 ARRAYBOUNDSCHECK (arr, index);
1478 PUSHL( elements(arr)[index] );
1484 jint index = POPI();
1485 jfloatArray arr = (jfloatArray) POPA();
1486 NULLARRAYCHECK (arr);
1487 ARRAYBOUNDSCHECK (arr, index);
1488 PUSHF( elements(arr)[index] );
1494 jint index = POPI();
1495 jdoubleArray arr = (jdoubleArray) POPA();
1496 NULLARRAYCHECK (arr);
1497 ARRAYBOUNDSCHECK (arr, index);
1498 PUSHD( elements(arr)[index] );
1504 jint index = POPI();
1505 jobjectArray arr = (jobjectArray) POPA();
1506 NULLARRAYCHECK (arr);
1507 ARRAYBOUNDSCHECK (arr, index);
1508 PUSHA( elements(arr)[index] );
1514 jint index = POPI();
1515 jbyteArray arr = (jbyteArray) POPA();
1516 NULLARRAYCHECK (arr);
1517 ARRAYBOUNDSCHECK (arr, index);
1518 PUSHI( elements(arr)[index] );
1524 jint index = POPI();
1525 jcharArray arr = (jcharArray) POPA();
1526 NULLARRAYCHECK (arr);
1527 ARRAYBOUNDSCHECK (arr, index);
1528 PUSHI( elements(arr)[index] );
1534 jint index = POPI();
1535 jshortArray arr = (jshortArray) POPA();
1536 NULLARRAYCHECK (arr);
1537 ARRAYBOUNDSCHECK (arr, index);
1538 PUSHI( elements(arr)[index] );
1644 jint value = POPI();
1645 jint index = POPI();
1646 jintArray arr = (jintArray) POPA();
1647 NULLARRAYCHECK (arr);
1648 ARRAYBOUNDSCHECK (arr, index);
1649 elements(arr)[index] = value;
1655 jlong value = POPL();
1656 jint index = POPI();
1657 jlongArray arr = (jlongArray) POPA();
1658 NULLARRAYCHECK (arr);
1659 ARRAYBOUNDSCHECK (arr, index);
1660 elements(arr)[index] = value;
1666 jfloat value = POPF();
1667 jint index = POPI();
1668 jfloatArray arr = (jfloatArray) POPA();
1669 NULLARRAYCHECK (arr);
1670 ARRAYBOUNDSCHECK (arr, index);
1671 elements(arr)[index] = value;
1677 jdouble value = POPD();
1678 jint index = POPI();
1679 jdoubleArray arr = (jdoubleArray) POPA();
1680 NULLARRAYCHECK (arr);
1681 ARRAYBOUNDSCHECK (arr, index);
1682 elements(arr)[index] = value;
1688 jobject value = POPA();
1689 jint index = POPI();
1690 jobjectArray arr = (jobjectArray) POPA();
1691 NULLARRAYCHECK (arr);
1692 ARRAYBOUNDSCHECK (arr, index);
1693 _Jv_CheckArrayStore (arr, value);
1694 elements(arr)[index] = value;
1700 jbyte value = (jbyte) POPI();
1701 jint index = POPI();
1702 jbyteArray arr = (jbyteArray) POPA();
1703 NULLARRAYCHECK (arr);
1704 ARRAYBOUNDSCHECK (arr, index);
1705 elements(arr)[index] = value;
1711 jchar value = (jchar) POPI();
1712 jint index = POPI();
1713 jcharArray arr = (jcharArray) POPA();
1714 NULLARRAYCHECK (arr);
1715 ARRAYBOUNDSCHECK (arr, index);
1716 elements(arr)[index] = value;
1722 jshort value = (jshort) POPI();
1723 jint index = POPI();
1724 jshortArray arr = (jshortArray) POPA();
1725 NULLARRAYCHECK (arr);
1726 ARRAYBOUNDSCHECK (arr, index);
1727 elements(arr)[index] = value;
1745 dupx (sp, 1, 1); sp+=1;
1749 dupx (sp, 1, 2); sp+=1;
1759 dupx (sp, 2, 1); sp+=2;
1763 dupx (sp, 2, 2); sp+=2;
1768 jobject tmp1 = POPA();
1769 jobject tmp2 = POPA();
1825 jint value2 = POPI();
1826 jint value1 = POPI();
1827 jint res = _Jv_divI (value1, value2);
1834 jlong value2 = POPL();
1835 jlong value1 = POPL();
1836 jlong res = _Jv_divJ (value1, value2);
1843 jfloat value2 = POPF();
1844 jfloat value1 = POPF();
1845 jfloat res = value1 / value2;
1852 jdouble value2 = POPD();
1853 jdouble value1 = POPD();
1854 jdouble res = value1 / value2;
1861 jint value2 = POPI();
1862 jint value1 = POPI();
1863 jint res = _Jv_remI (value1, value2);
1870 jlong value2 = POPL();
1871 jlong value1 = POPL();
1872 jlong res = _Jv_remJ (value1, value2);
1879 jfloat value2 = POPF();
1880 jfloat value1 = POPF();
1881 jfloat res = __ieee754_fmod (value1, value2);
1888 jdouble value2 = POPD();
1889 jdouble value1 = POPD();
1890 jdouble res = __ieee754_fmod (value1, value2);
1897 jint value = POPI();
1904 jlong value = POPL();
1911 jfloat value = POPF();
1918 jdouble value = POPD();
1925 jint shift = (POPI() & 0x1f);
1926 jint value = POPI();
1927 PUSHI (value << shift);
1933 jint shift = (POPI() & 0x3f);
1934 jlong value = POPL();
1935 PUSHL (value << shift);
1941 jint shift = (POPI() & 0x1f);
1942 jint value = POPI();
1943 PUSHI (value >> shift);
1949 jint shift = (POPI() & 0x3f);
1950 jlong value = POPL();
1951 PUSHL (value >> shift);
1957 jint shift = (POPI() & 0x1f);
1958 _Jv_uint value = (_Jv_uint) POPI();
1959 PUSHI ((jint) (value >> shift));
1965 jint shift = (POPI() & 0x3f);
1966 _Jv_ulong value = (_Jv_ulong) POPL();
1967 PUSHL ((jlong) (value >> shift));
1997 jint index = GET1U ();
1998 jint amount = GET1S ();
1999 locals[index].i += amount;
2004 {jlong value = POPI(); PUSHL (value);}
2008 {jfloat value = POPI(); PUSHF (value);}
2012 {jdouble value = POPI(); PUSHD (value);}
2016 {jint value = POPL(); PUSHI (value);}
2020 {jfloat value = POPL(); PUSHF (value);}
2024 {jdouble value = POPL(); PUSHD (value);}
2029 using namespace java::lang;
2030 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2037 using namespace java::lang;
2038 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2044 { jdouble value = POPF (); PUSHD(value); }
2049 using namespace java::lang;
2050 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2057 using namespace java::lang;
2058 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2064 { jfloat value = POPD (); PUSHF(value); }
2068 { jbyte value = POPI (); PUSHI(value); }
2072 { jchar value = POPI (); PUSHI(value); }
2076 { jshort value = POPI (); PUSHI(value); }
2081 jlong value2 = POPL ();
2082 jlong value1 = POPL ();
2083 if (value1 > value2)
2085 else if (value1 == value2)
2101 jfloat value2 = POPF ();
2102 jfloat value1 = POPF ();
2103 if (value1 > value2)
2105 else if (value1 == value2)
2107 else if (value1 < value2)
2123 jdouble value2 = POPD ();
2124 jdouble value1 = POPD ();
2125 if (value1 > value2)
2127 else if (value1 == value2)
2129 else if (value1 < value2)
2192 jint value2 = POPI();
2193 jint value1 = POPI();
2194 if (value1 == value2)
2203 jint value2 = POPI();
2204 jint value1 = POPI();
2205 if (value1 != value2)
2214 jint value2 = POPI();
2215 jint value1 = POPI();
2216 if (value1 < value2)
2225 jint value2 = POPI();
2226 jint value1 = POPI();
2227 if (value1 >= value2)
2236 jint value2 = POPI();
2237 jint value1 = POPI();
2238 if (value1 > value2)
2247 jint value2 = POPI();
2248 jint value1 = POPI();
2249 if (value1 <= value2)
2258 jobject value2 = POPA();
2259 jobject value1 = POPA();
2260 if (value1 == value2)
2269 jobject value2 = POPA();
2270 jobject value1 = POPA();
2271 if (value1 != value2)
2279 #ifndef DIRECT_THREADED
2280 // For direct threaded, goto and goto_w are the same.
2281 pc = pc - 1 + get4 (pc);
2283 #endif /* DIRECT_THREADED */
2289 #ifndef DIRECT_THREADED
2290 // For direct threaded, jsr and jsr_w are the same.
2292 pc_t next = pc - 1 + get4 (pc);
2294 PUSHA ((jobject) pc);
2298 #endif /* DIRECT_THREADED */
2301 pc_t next = GOTO_VAL();
2303 PUSHA ((jobject) pc);
2310 jint index = GET1U ();
2311 pc = (pc_t) PEEKA (index);
2317 #ifdef DIRECT_THREADED
2318 void *def = (pc++)->datum;
2322 jint low = INTVAL ();
2323 jint high = INTVAL ();
2325 if (index < low || index > high)
2326 pc = (insn_slot *) def;
2328 pc = (insn_slot *) ((pc + index - low)->datum);
2330 pc_t base_pc = pc - 1;
2331 int index = POPI ();
2333 pc_t base = (pc_t) bytecode ();
2334 while ((pc - base) % 4 != 0)
2337 jint def = get4 (pc);
2338 jint low = get4 (pc + 4);
2339 jint high = get4 (pc + 8);
2340 if (index < low || index > high)
2343 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2344 #endif /* DIRECT_THREADED */
2350 #ifdef DIRECT_THREADED
2351 void *def = (pc++)->insn;
2355 jint npairs = INTVAL ();
2357 int max = npairs - 1;
2360 // Simple binary search...
2363 int half = (min + max) / 2;
2364 int match = pc[2 * half].int_val;
2369 pc = (insn_slot *) pc[2 * half + 1].datum;
2372 else if (index < match)
2373 // We can use HALF - 1 here because we check again on
2377 // We can use HALF + 1 here because we check again on
2381 if (index == pc[2 * min].int_val)
2382 pc = (insn_slot *) pc[2 * min + 1].datum;
2384 pc = (insn_slot *) def;
2386 unsigned char *base_pc = pc-1;
2389 unsigned char* base = bytecode ();
2390 while ((pc-base) % 4 != 0)
2393 jint def = get4 (pc);
2394 jint npairs = get4 (pc+4);
2399 // Simple binary search...
2402 int half = (min+max)/2;
2403 int match = get4 (pc+ 4*(2 + 2*half));
2407 else if (index < match)
2408 // We can use HALF - 1 here because we check again on
2412 // We can use HALF + 1 here because we check again on
2417 if (index == get4 (pc+ 4*(2 + 2*min)))
2418 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2421 #endif /* DIRECT_THREADED */
2426 *(jobject *) retp = POPA ();
2430 *(jlong *) retp = POPL ();
2434 *(jfloat *) retp = POPF ();
2438 *(jdouble *) retp = POPD ();
2442 *(jint *) retp = POPI ();
2450 jint fieldref_index = GET2U ();
2451 SAVE_PC(); // Constant pool resolution could throw.
2452 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2453 _Jv_Field *field = pool_data[fieldref_index].field;
2455 if ((field->flags & Modifier::STATIC) == 0)
2456 throw_incompatible_class_change_error
2457 (JvNewStringLatin1 ("field no longer static"));
2459 jclass type = field->type;
2461 // We rewrite the instruction once we discover what it refers
2463 void *newinsn = NULL;
2464 if (type->isPrimitive ())
2466 switch (type->size_in_bytes)
2469 PUSHI (*field->u.byte_addr);
2470 newinsn = AMPAMP (getstatic_resolved_1);
2474 if (type == JvPrimClass (char))
2476 PUSHI (*field->u.char_addr);
2477 newinsn = AMPAMP (getstatic_resolved_char);
2481 PUSHI (*field->u.short_addr);
2482 newinsn = AMPAMP (getstatic_resolved_short);
2487 PUSHI(*field->u.int_addr);
2488 newinsn = AMPAMP (getstatic_resolved_4);
2492 PUSHL(*field->u.long_addr);
2493 newinsn = AMPAMP (getstatic_resolved_8);
2499 PUSHA(*field->u.object_addr);
2500 newinsn = AMPAMP (getstatic_resolved_obj);
2503 #ifdef DIRECT_THREADED
2504 pc[-2].insn = newinsn;
2505 pc[-1].datum = field->u.addr;
2506 #endif /* DIRECT_THREADED */
2510 #ifdef DIRECT_THREADED
2511 getstatic_resolved_1:
2512 PUSHI (*(jbyte *) AVAL ());
2515 getstatic_resolved_char:
2516 PUSHI (*(jchar *) AVAL ());
2519 getstatic_resolved_short:
2520 PUSHI (*(jshort *) AVAL ());
2523 getstatic_resolved_4:
2524 PUSHI (*(jint *) AVAL ());
2527 getstatic_resolved_8:
2528 PUSHL (*(jlong *) AVAL ());
2531 getstatic_resolved_obj:
2532 PUSHA (*(jobject *) AVAL ());
2534 #endif /* DIRECT_THREADED */
2538 jint fieldref_index = GET2U ();
2539 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2540 _Jv_Field *field = pool_data[fieldref_index].field;
2542 if ((field->flags & Modifier::STATIC) != 0)
2543 throw_incompatible_class_change_error
2544 (JvNewStringLatin1 ("field is static"));
2546 jclass type = field->type;
2547 jint field_offset = field->u.boffset;
2548 if (field_offset > 0xffff)
2549 throw new java::lang::VirtualMachineError;
2551 jobject obj = POPA();
2554 void *newinsn = NULL;
2555 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2556 if (type->isPrimitive ())
2558 switch (type->size_in_bytes)
2561 PUSHI (val->byte_value);
2562 newinsn = AMPAMP (getfield_resolved_1);
2566 if (type == JvPrimClass (char))
2568 PUSHI (val->char_value);
2569 newinsn = AMPAMP (getfield_resolved_char);
2573 PUSHI (val->short_value);
2574 newinsn = AMPAMP (getfield_resolved_short);
2579 PUSHI (val->int_value);
2580 newinsn = AMPAMP (getfield_resolved_4);
2584 PUSHL (val->long_value);
2585 newinsn = AMPAMP (getfield_resolved_8);
2591 PUSHA (val->object_value);
2592 newinsn = AMPAMP (getfield_resolved_obj);
2595 #ifdef DIRECT_THREADED
2596 pc[-2].insn = newinsn;
2597 pc[-1].int_val = field_offset;
2598 #endif /* DIRECT_THREADED */
2602 #ifdef DIRECT_THREADED
2603 getfield_resolved_1:
2605 char *obj = (char *) POPA ();
2607 PUSHI (*(jbyte *) (obj + INTVAL ()));
2611 getfield_resolved_char:
2613 char *obj = (char *) POPA ();
2615 PUSHI (*(jchar *) (obj + INTVAL ()));
2619 getfield_resolved_short:
2621 char *obj = (char *) POPA ();
2623 PUSHI (*(jshort *) (obj + INTVAL ()));
2627 getfield_resolved_4:
2629 char *obj = (char *) POPA ();
2631 PUSHI (*(jint *) (obj + INTVAL ()));
2635 getfield_resolved_8:
2637 char *obj = (char *) POPA ();
2639 PUSHL (*(jlong *) (obj + INTVAL ()));
2643 getfield_resolved_obj:
2645 char *obj = (char *) POPA ();
2647 PUSHA (*(jobject *) (obj + INTVAL ()));
2650 #endif /* DIRECT_THREADED */
2654 jint fieldref_index = GET2U ();
2655 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2656 _Jv_Field *field = pool_data[fieldref_index].field;
2658 jclass type = field->type;
2660 // ResolvePoolEntry cannot check this
2661 if ((field->flags & Modifier::STATIC) == 0)
2662 throw_incompatible_class_change_error
2663 (JvNewStringLatin1 ("field no longer static"));
2665 void *newinsn = NULL;
2666 if (type->isPrimitive ())
2668 switch (type->size_in_bytes)
2672 jint value = POPI();
2673 *field->u.byte_addr = value;
2674 newinsn = AMPAMP (putstatic_resolved_1);
2680 jint value = POPI();
2681 *field->u.char_addr = value;
2682 newinsn = AMPAMP (putstatic_resolved_2);
2688 jint value = POPI();
2689 *field->u.int_addr = value;
2690 newinsn = AMPAMP (putstatic_resolved_4);
2696 jlong value = POPL();
2697 *field->u.long_addr = value;
2698 newinsn = AMPAMP (putstatic_resolved_8);
2705 jobject value = POPA();
2706 *field->u.object_addr = value;
2707 newinsn = AMPAMP (putstatic_resolved_obj);
2710 #ifdef DIRECT_THREADED
2711 pc[-2].insn = newinsn;
2712 pc[-1].datum = field->u.addr;
2713 #endif /* DIRECT_THREADED */
2717 #ifdef DIRECT_THREADED
2718 putstatic_resolved_1:
2719 *(jbyte *) AVAL () = POPI ();
2722 putstatic_resolved_2:
2723 *(jchar *) AVAL () = POPI ();
2726 putstatic_resolved_4:
2727 *(jint *) AVAL () = POPI ();
2730 putstatic_resolved_8:
2731 *(jlong *) AVAL () = POPL ();
2734 putstatic_resolved_obj:
2735 *(jobject *) AVAL () = POPA ();
2737 #endif /* DIRECT_THREADED */
2741 jint fieldref_index = GET2U ();
2742 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2743 _Jv_Field *field = pool_data[fieldref_index].field;
2745 jclass type = field->type;
2747 if ((field->flags & Modifier::STATIC) != 0)
2748 throw_incompatible_class_change_error
2749 (JvNewStringLatin1 ("field is static"));
2751 jint field_offset = field->u.boffset;
2752 if (field_offset > 0xffff)
2753 throw new java::lang::VirtualMachineError;
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 new java::lang::NullPointerException;
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 new java::lang::NullPointerException;
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 (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 static java::lang::NullPointerException *null_pointer_exc;
3303 throw_null_pointer_exception ()
3305 if (null_pointer_exc == NULL)
3306 null_pointer_exc = new java::lang::NullPointerException;
3308 throw null_pointer_exc;
3312 /* Look up source code line number for given bytecode (or direct threaded
3315 _Jv_InterpMethod::get_source_line(pc_t mpc)
3317 int line = line_table_len > 0 ? line_table[0].line : -1;
3318 for (int i = 1; i < line_table_len; i++)
3319 if (line_table[i].pc > mpc)
3322 line = line_table[i].line;
3327 /** Do static initialization for fields with a constant initializer */
3329 _Jv_InitField (jobject obj, jclass klass, int index)
3331 using namespace java::lang::reflect;
3333 if (obj != 0 && klass == 0)
3334 klass = obj->getClass ();
3336 if (!_Jv_IsInterpretedClass (klass))
3339 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3341 _Jv_Field * field = (&klass->fields[0]) + index;
3343 if (index > klass->field_count)
3344 throw_internal_error ("field out of range");
3346 int init = iclass->field_initializers[index];
3350 _Jv_Constants *pool = &klass->constants;
3351 int tag = pool->tags[init];
3353 if (! field->isResolved ())
3354 throw_internal_error ("initializing unresolved field");
3356 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3357 throw_internal_error ("initializing non-static field with no object");
3361 if ((field->flags & Modifier::STATIC) != 0)
3362 addr = (void*) field->u.addr;
3364 addr = (void*) (((char*)obj) + field->u.boffset);
3368 case JV_CONSTANT_String:
3371 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3372 pool->data[init].string = str;
3373 pool->tags[init] = JV_CONSTANT_ResolvedString;
3377 case JV_CONSTANT_ResolvedString:
3378 if (! (field->type == &java::lang::String::class$
3379 || field->type == &java::lang::Class::class$))
3380 throw_class_format_error ("string initialiser to non-string field");
3382 *(jstring*)addr = pool->data[init].string;
3385 case JV_CONSTANT_Integer:
3387 int value = pool->data[init].i;
3389 if (field->type == JvPrimClass (boolean))
3390 *(jboolean*)addr = (jboolean)value;
3392 else if (field->type == JvPrimClass (byte))
3393 *(jbyte*)addr = (jbyte)value;
3395 else if (field->type == JvPrimClass (char))
3396 *(jchar*)addr = (jchar)value;
3398 else if (field->type == JvPrimClass (short))
3399 *(jshort*)addr = (jshort)value;
3401 else if (field->type == JvPrimClass (int))
3402 *(jint*)addr = (jint)value;
3405 throw_class_format_error ("erroneous field initializer");
3409 case JV_CONSTANT_Long:
3410 if (field->type != JvPrimClass (long))
3411 throw_class_format_error ("erroneous field initializer");
3413 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3416 case JV_CONSTANT_Float:
3417 if (field->type != JvPrimClass (float))
3418 throw_class_format_error ("erroneous field initializer");
3420 *(jfloat*)addr = pool->data[init].f;
3423 case JV_CONSTANT_Double:
3424 if (field->type != JvPrimClass (double))
3425 throw_class_format_error ("erroneous field initializer");
3427 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3431 throw_class_format_error ("erroneous field initializer");
3435 inline static unsigned char*
3436 skip_one_type (unsigned char* ptr)
3447 do { ch = *ptr++; } while (ch != ';');
3454 get_ffi_type_from_signature (unsigned char* ptr)
3460 return &ffi_type_pointer;
3464 // On some platforms a bool is a byte, on others an int.
3465 if (sizeof (jboolean) == sizeof (jbyte))
3466 return &ffi_type_sint8;
3469 JvAssert (sizeof (jbyte) == sizeof (jint));
3470 return &ffi_type_sint32;
3475 return &ffi_type_sint8;
3479 return &ffi_type_uint16;
3483 return &ffi_type_sint16;
3487 return &ffi_type_sint32;
3491 return &ffi_type_sint64;
3495 return &ffi_type_float;
3499 return &ffi_type_double;
3503 return &ffi_type_void;
3507 throw_internal_error ("unknown type in signature");
3510 /* this function yields the number of actual arguments, that is, if the
3511 * function is non-static, then one is added to the number of elements
3512 * found in the signature */
3515 _Jv_count_arguments (_Jv_Utf8Const *signature,
3518 unsigned char *ptr = (unsigned char*) signature->chars();
3519 int arg_count = staticp ? 0 : 1;
3521 /* first, count number of arguments */
3529 ptr = skip_one_type (ptr);
3536 /* This beast will build a cif, given the signature. Memory for
3537 * the cif itself and for the argument types must be allocated by the
3542 init_cif (_Jv_Utf8Const* signature,
3546 ffi_type **arg_types,
3549 unsigned char *ptr = (unsigned char*) signature->chars();
3551 int arg_index = 0; // arg number
3552 int item_count = 0; // stack-item count
3557 arg_types[arg_index++] = &ffi_type_pointer;
3567 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3569 if (*ptr == 'J' || *ptr == 'D')
3574 ptr = skip_one_type (ptr);
3579 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3581 ptr = skip_one_type (ptr);
3582 if (ptr != (unsigned char*)signature->chars() + signature->len())
3583 throw_internal_error ("did not find end of signature");
3585 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3586 arg_count, rtype, arg_types) != FFI_OK)
3587 throw_internal_error ("ffi_prep_cif failed");
3589 if (rtype_p != NULL)
3595 #if FFI_NATIVE_RAW_API
3596 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3597 # define FFI_RAW_SIZE ffi_raw_size
3599 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3600 # define FFI_RAW_SIZE ffi_java_raw_size
3603 /* we put this one here, and not in interpret.cc because it
3604 * calls the utility routines _Jv_count_arguments
3605 * which are static to this module. The following struct defines the
3606 * layout we use for the stubs, it's only used in the ncode method. */
3609 ffi_raw_closure closure;
3611 ffi_type *arg_types[0];
3614 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3617 _Jv_InterpMethod::ncode ()
3619 using namespace java::lang::reflect;
3621 if (self->ncode != 0)
3624 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3625 int arg_count = _Jv_count_arguments (self->signature, staticp);
3627 ncode_closure *closure =
3628 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3629 + arg_count * sizeof (ffi_type*));
3631 init_cif (self->signature,
3635 &closure->arg_types[0],
3638 ffi_closure_fun fun;
3640 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3642 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3644 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3647 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3649 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3654 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3656 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3659 FFI_PREP_RAW_CLOSURE (&closure->closure,
3664 self->ncode = (void*)closure;
3669 _Jv_JNIMethod::ncode ()
3671 using namespace java::lang::reflect;
3673 if (self->ncode != 0)
3676 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3677 int arg_count = _Jv_count_arguments (self->signature, staticp);
3679 ncode_closure *closure =
3680 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3681 + arg_count * sizeof (ffi_type*));
3684 init_cif (self->signature,
3688 &closure->arg_types[0],
3691 ffi_closure_fun fun;
3693 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3695 // Initialize the argument types and CIF that represent the actual
3696 // underlying JNI function.
3698 if ((self->accflags & Modifier::STATIC))
3700 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3701 * sizeof (ffi_type *));
3703 jni_arg_types[offset++] = &ffi_type_pointer;
3704 if ((self->accflags & Modifier::STATIC))
3705 jni_arg_types[offset++] = &ffi_type_pointer;
3706 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3707 arg_count * sizeof (ffi_type *));
3709 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3710 extra_args + arg_count, rtype,
3711 jni_arg_types) != FFI_OK)
3712 throw_internal_error ("ffi_prep_cif failed for JNI function");
3714 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3716 // FIXME: for now we assume that all native methods for
3717 // interpreted code use JNI.
3718 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3720 FFI_PREP_RAW_CLOSURE (&closure->closure,
3725 self->ncode = (void *) closure;
3730 throw_class_format_error (jstring msg)
3733 ? new java::lang::ClassFormatError (msg)
3734 : new java::lang::ClassFormatError);
3738 throw_class_format_error (char *msg)
3740 throw_class_format_error (JvNewStringLatin1 (msg));
3746 _Jv_InterpreterEngine::do_verify (jclass klass)
3748 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3749 for (int i = 0; i < klass->method_count; i++)
3751 using namespace java::lang::reflect;
3752 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3753 _Jv_ushort accflags = klass->methods[i].accflags;
3754 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3756 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3757 _Jv_VerifyMethod (im);
3763 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3765 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3766 for (int i = 0; i < klass->method_count; i++)
3768 // Just skip abstract methods. This is particularly important
3769 // because we don't resize the interpreted_methods array when
3770 // miranda methods are added to it.
3771 if ((klass->methods[i].accflags
3772 & java::lang::reflect::Modifier::ABSTRACT)
3776 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3778 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3781 // You might think we could use a virtual `ncode' method in
3782 // the _Jv_MethodBase and unify the native and non-native
3783 // cases. Well, we can't, because we don't allocate these
3784 // objects using `new', and thus they don't get a vtable.
3785 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3786 klass->methods[i].ncode = jnim->ncode ();
3788 else if (imeth != 0) // it could be abstract
3790 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3791 klass->methods[i].ncode = im->ncode ();
3797 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3800 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3802 char *static_data = (char *) _Jv_AllocBytes (static_size);
3804 for (int i = 0; i < klass->field_count; i++)
3806 _Jv_Field *field = &klass->fields[i];
3808 if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3810 field->u.addr = static_data + field->u.boffset;
3812 if (iclass->field_initializers[i] != 0)
3814 _Jv_Linker::resolve_field (field, klass->loader);
3815 _Jv_InitField (0, klass, i);
3820 // Now we don't need the field_initializers anymore, so let the
3821 // collector get rid of it.
3822 iclass->field_initializers = 0;
3825 _Jv_ResolvedMethod *
3826 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3827 jboolean staticp, jint vtable_index)
3829 int arg_count = _Jv_count_arguments (method->signature, staticp);
3831 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3832 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3833 + arg_count*sizeof (ffi_type*));
3835 result->stack_item_count
3836 = init_cif (method->signature,
3840 &result->arg_types[0],
3843 result->vtable_index = vtable_index;
3844 result->method = method;
3845 result->klass = klass;
3851 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3853 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3854 for (int i = 0; i < klass->method_count; i++)
3856 // Just skip abstract methods. This is particularly important
3857 // because we don't resize the interpreted_methods array when
3858 // miranda methods are added to it.
3859 if ((klass->methods[i].accflags
3860 & java::lang::reflect::Modifier::ABSTRACT)
3863 // Miranda method additions mean that the `methods' array moves.
3864 // We cache a pointer into this array, so we have to update.
3865 iclass->interpreted_methods[i]->self = &klass->methods[i];
3869 #endif // INTERPRETER