1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 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/ClassCastException.h>
29 #include <java/lang/VirtualMachineError.h>
30 #include <java/lang/InternalError.h>
31 #include <java/lang/NullPointerException.h>
32 #include <java/lang/ArithmeticException.h>
33 #include <java/lang/IncompatibleClassChangeError.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 SET_DATUM (pool_data[index].o);
526 SET_INT (get1u (pc));
531 SET_INT (get1u (pc));
532 SET_INT (get1s (pc + 1));
538 int index = get2u (pc);
540 SET_DATUM (pool_data[index].o);
546 int index = get2u (pc);
548 SET_DATUM (&pool_data[index]);
553 SET_INT (get2s (pc));
565 case op_invokespecial:
566 case op_invokestatic:
567 case op_invokevirtual:
568 SET_INT (get2u (pc));
572 case op_multianewarray:
573 SET_INT (get2u (pc));
574 SET_INT (get1u (pc + 2));
597 int offset = get2s (pc);
600 int new_pc = base_pc_val + offset;
602 bool orig_was_goto = opcode == op_goto;
604 // Thread jumps. We limit the loop count; this lets
605 // us avoid infinite loops if the bytecode contains
606 // such. `10' is arbitrary.
608 while (codestart[new_pc] == op_goto && count-- > 0)
609 new_pc += get2s (&codestart[new_pc + 1]);
611 // If the jump takes us to a `return' instruction and
612 // the original branch was an unconditional goto, then
613 // we hoist the return.
614 opcode = (java_opcode) codestart[new_pc];
616 && (opcode == op_ireturn || opcode == op_lreturn
617 || opcode == op_freturn || opcode == op_dreturn
618 || opcode == op_areturn || opcode == op_return))
621 SET_INSN (insn_targets[opcode]);
624 SET_DATUM (&insns[pc_mapping[new_pc]]);
630 while ((pc - codestart) % 4 != 0)
633 jint def = get4 (pc);
634 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
640 int high = get4 (pc);
644 for (int i = low; i <= high; ++i)
646 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
652 case op_lookupswitch:
654 while ((pc - codestart) % 4 != 0)
657 jint def = get4 (pc);
658 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
661 jint npairs = get4 (pc);
667 jint match = get4 (pc);
668 jint offset = get4 (pc + 4);
670 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
676 case op_invokeinterface:
678 jint index = get2u (pc);
680 // We ignore the next two bytes.
688 opcode = (java_opcode) get1u (pc);
690 jint val = get2u (pc);
693 // We implement narrow and wide instructions using the
694 // same code in the interpreter. So we rewrite the
695 // instruction slot here.
697 insns[next - 1].insn = (void *) insn_targets[opcode];
700 if (opcode == op_iinc)
702 SET_INT (get2s (pc));
711 jint offset = get4 (pc);
713 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
717 // Some "can't happen" cases that we include for
718 // error-checking purposes.
736 case op_getstatic_2s:
737 case op_getstatic_2u:
748 // Now update exceptions.
749 _Jv_InterpException *exc = exceptions ();
750 for (int i = 0; i < exc_count; ++i)
752 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
753 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
754 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
756 = (_Jv_Linker::resolve_pool_entry (defining_class,
757 exc[i].handler_type.i)).clazz;
758 exc[i].handler_type.p = handler;
761 // Translate entries in the LineNumberTable from bytecode PC's to direct
762 // threaded interpreter instruction values.
763 for (int i = 0; i < line_table_len; i++)
765 int byte_pc = line_table[i].bytecode_pc;
766 line_table[i].pc = &insns[pc_mapping[byte_pc]];
771 #endif /* DIRECT_THREADED */
774 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
776 using namespace java::lang::reflect;
778 // FRAME_DESC registers this particular invocation as the top-most
779 // interpreter frame. This lets the stack tracing code (for
780 // Throwable) print information about the method being interpreted
781 // rather than about the interpreter itself. FRAME_DESC has a
782 // destructor so it cleans up automatically when the interpreter
784 java::lang::Thread *thread = java::lang::Thread::currentThread();
785 _Jv_InterpFrame frame_desc (meth,
786 (_Jv_InterpFrame **) &thread->interp_frame);
788 _Jv_word stack[meth->max_stack];
789 _Jv_word *sp = stack;
791 _Jv_word locals[meth->max_locals];
793 /* Go straight at it! the ffi raw format matches the internal
794 stack representation exactly. At least, that's the idea.
796 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
798 _Jv_word *pool_data = meth->defining_class->constants.data;
800 /* These three are temporaries for common code used by several
803 _Jv_ResolvedMethod* rmeth;
806 #define INSN_LABEL(op) &&insn_##op
808 static const void *const insn_target[] =
811 INSN_LABEL(aconst_null),
812 INSN_LABEL(iconst_m1),
813 INSN_LABEL(iconst_0),
814 INSN_LABEL(iconst_1),
815 INSN_LABEL(iconst_2),
816 INSN_LABEL(iconst_3),
817 INSN_LABEL(iconst_4),
818 INSN_LABEL(iconst_5),
819 INSN_LABEL(lconst_0),
820 INSN_LABEL(lconst_1),
821 INSN_LABEL(fconst_0),
822 INSN_LABEL(fconst_1),
823 INSN_LABEL(fconst_2),
824 INSN_LABEL(dconst_0),
825 INSN_LABEL(dconst_1),
869 INSN_LABEL(istore_0),
870 INSN_LABEL(istore_1),
871 INSN_LABEL(istore_2),
872 INSN_LABEL(istore_3),
873 INSN_LABEL(lstore_0),
874 INSN_LABEL(lstore_1),
875 INSN_LABEL(lstore_2),
876 INSN_LABEL(lstore_3),
877 INSN_LABEL(fstore_0),
878 INSN_LABEL(fstore_1),
879 INSN_LABEL(fstore_2),
880 INSN_LABEL(fstore_3),
881 INSN_LABEL(dstore_0),
882 INSN_LABEL(dstore_1),
883 INSN_LABEL(dstore_2),
884 INSN_LABEL(dstore_3),
885 INSN_LABEL(astore_0),
886 INSN_LABEL(astore_1),
887 INSN_LABEL(astore_2),
888 INSN_LABEL(astore_3),
969 INSN_LABEL(if_icmpeq),
970 INSN_LABEL(if_icmpne),
971 INSN_LABEL(if_icmplt),
972 INSN_LABEL(if_icmpge),
973 INSN_LABEL(if_icmpgt),
974 INSN_LABEL(if_icmple),
975 INSN_LABEL(if_acmpeq),
976 INSN_LABEL(if_acmpne),
980 INSN_LABEL(tableswitch),
981 INSN_LABEL(lookupswitch),
988 INSN_LABEL(getstatic),
989 INSN_LABEL(putstatic),
990 INSN_LABEL(getfield),
991 INSN_LABEL(putfield),
992 INSN_LABEL(invokevirtual),
993 INSN_LABEL(invokespecial),
994 INSN_LABEL(invokestatic),
995 INSN_LABEL(invokeinterface),
998 INSN_LABEL(newarray),
999 INSN_LABEL(anewarray),
1000 INSN_LABEL(arraylength),
1002 INSN_LABEL(checkcast),
1003 INSN_LABEL(instanceof),
1004 INSN_LABEL(monitorenter),
1005 INSN_LABEL(monitorexit),
1006 #ifdef DIRECT_THREADED
1011 INSN_LABEL(multianewarray),
1013 INSN_LABEL(ifnonnull),
1021 #ifdef DIRECT_THREADED
1023 #define NEXT_INSN goto *((pc++)->insn)
1024 #define INTVAL() ((pc++)->int_val)
1025 #define AVAL() ((pc++)->datum)
1027 #define GET1S() INTVAL ()
1028 #define GET2S() INTVAL ()
1029 #define GET1U() INTVAL ()
1030 #define GET2U() INTVAL ()
1031 #define AVAL1U() AVAL ()
1032 #define AVAL2U() AVAL ()
1033 #define AVAL2UP() AVAL ()
1034 #define SKIP_GOTO ++pc
1035 #define GOTO_VAL() (insn_slot *) pc->datum
1036 #define PCVAL(unionval) unionval.p
1037 #define AMPAMP(label) &&label
1039 // Compile if we must. NOTE: Double-check locking.
1040 if (meth->prepared == NULL)
1042 _Jv_MutexLock (&compile_mutex);
1043 if (meth->prepared == NULL)
1044 meth->compile (insn_target);
1045 _Jv_MutexUnlock (&compile_mutex);
1047 pc = (insn_slot *) meth->prepared;
1051 #define NEXT_INSN goto *(insn_target[*pc++])
1053 #define GET1S() get1s (pc++)
1054 #define GET2S() (pc += 2, get2s (pc- 2))
1055 #define GET1U() get1u (pc++)
1056 #define GET2U() (pc += 2, get2u (pc - 2))
1057 #define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
1058 #define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
1059 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1060 #define SKIP_GOTO pc += 2
1061 #define GOTO_VAL() pc - 1 + get2s (pc)
1062 #define PCVAL(unionval) unionval.i
1063 #define AMPAMP(label) NULL
1067 #endif /* DIRECT_THREADED */
1069 #define TAKE_GOTO pc = GOTO_VAL ()
1073 // We keep nop around. It is used if we're interpreting the
1074 // bytecodes and not doing direct threading.
1078 /* The first few instructions here are ordered according to their
1079 frequency, in the hope that this will improve code locality a
1082 insn_aload_0: // 0x2a
1090 insn_iload_1: // 0x1b
1094 insn_invokevirtual: // 0xb6
1096 int index = GET2U ();
1098 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1099 * value already is resolved. If we want to clutter up the
1100 * code here to gain a little performance, then we can check
1101 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1102 * directly. For now, I don't think it is worth it. */
1105 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1108 sp -= rmeth->stack_item_count;
1109 // We don't use NULLCHECK here because we can't rely on that
1110 // working if the method is final. So instead we do an
1114 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1115 throw new java::lang::NullPointerException;
1118 if (rmeth->vtable_index == -1)
1120 // final methods do not appear in the vtable,
1121 // if it does not appear in the superclass.
1122 fun = (void (*)()) rmeth->method->ncode;
1126 jobject rcv = sp[0].o;
1127 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1128 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1131 #ifdef DIRECT_THREADED
1132 // Rewrite instruction so that we use a faster pre-resolved
1134 pc[-2].insn = &&invokevirtual_resolved;
1135 pc[-1].datum = rmeth;
1136 #endif /* DIRECT_THREADED */
1138 goto perform_invoke;
1140 #ifdef DIRECT_THREADED
1141 invokevirtual_resolved:
1143 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1144 sp -= rmeth->stack_item_count;
1145 // We don't use NULLCHECK here because we can't rely on that
1146 // working if the method is final. So instead we do an
1151 throw new java::lang::NullPointerException;
1154 if (rmeth->vtable_index == -1)
1156 // final methods do not appear in the vtable,
1157 // if it does not appear in the superclass.
1158 fun = (void (*)()) rmeth->method->ncode;
1162 jobject rcv = sp[0].o;
1163 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1164 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1167 goto perform_invoke;
1168 #endif /* DIRECT_THREADED */
1174 /* here goes the magic again... */
1175 ffi_cif *cif = &rmeth->cif;
1176 ffi_raw *raw = (ffi_raw*) sp;
1180 #if FFI_NATIVE_RAW_API
1181 /* We assume that this is only implemented if it's correct */
1182 /* to use it here. On a 64 bit machine, it never is. */
1183 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1185 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1188 int rtype = cif->rtype->type;
1190 /* the likelyhood of object, int, or void return is very high,
1191 * so those are checked before the switch */
1192 if (rtype == FFI_TYPE_POINTER)
1194 PUSHA (rvalue.object_value);
1196 else if (rtype == FFI_TYPE_SINT32)
1198 PUSHI (rvalue.int_value);
1200 else if (rtype == FFI_TYPE_VOID)
1208 case FFI_TYPE_SINT8:
1209 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1212 case FFI_TYPE_SINT16:
1213 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1216 case FFI_TYPE_UINT16:
1217 PUSHI (rvalue.int_value & 0xffff);
1220 case FFI_TYPE_FLOAT:
1221 PUSHF (rvalue.float_value);
1224 case FFI_TYPE_DOUBLE:
1225 PUSHD (rvalue.double_value);
1228 case FFI_TYPE_SINT64:
1229 PUSHL (rvalue.long_value);
1233 throw_internal_error ("unknown return type in invokeXXX");
1300 // For direct threaded, bipush and sipush are the same.
1301 #ifndef DIRECT_THREADED
1304 #endif /* DIRECT_THREADED */
1310 // For direct threaded, ldc and ldc_w are the same.
1311 #ifndef DIRECT_THREADED
1312 PUSHA ((jobject) AVAL1U ());
1314 #endif /* DIRECT_THREADED */
1316 PUSHA ((jobject) AVAL2U ());
1321 void *where = AVAL2UP ();
1322 memcpy (sp, where, 2*sizeof (_Jv_word));
1417 jint index = POPI();
1418 jintArray arr = (jintArray) POPA();
1419 NULLARRAYCHECK (arr);
1420 ARRAYBOUNDSCHECK (arr, index);
1421 PUSHI( elements(arr)[index] );
1427 jint index = POPI();
1428 jlongArray arr = (jlongArray) POPA();
1429 NULLARRAYCHECK (arr);
1430 ARRAYBOUNDSCHECK (arr, index);
1431 PUSHL( elements(arr)[index] );
1437 jint index = POPI();
1438 jfloatArray arr = (jfloatArray) POPA();
1439 NULLARRAYCHECK (arr);
1440 ARRAYBOUNDSCHECK (arr, index);
1441 PUSHF( elements(arr)[index] );
1447 jint index = POPI();
1448 jdoubleArray arr = (jdoubleArray) POPA();
1449 NULLARRAYCHECK (arr);
1450 ARRAYBOUNDSCHECK (arr, index);
1451 PUSHD( elements(arr)[index] );
1457 jint index = POPI();
1458 jobjectArray arr = (jobjectArray) POPA();
1459 NULLARRAYCHECK (arr);
1460 ARRAYBOUNDSCHECK (arr, index);
1461 PUSHA( elements(arr)[index] );
1467 jint index = POPI();
1468 jbyteArray arr = (jbyteArray) POPA();
1469 NULLARRAYCHECK (arr);
1470 ARRAYBOUNDSCHECK (arr, index);
1471 PUSHI( elements(arr)[index] );
1477 jint index = POPI();
1478 jcharArray arr = (jcharArray) POPA();
1479 NULLARRAYCHECK (arr);
1480 ARRAYBOUNDSCHECK (arr, index);
1481 PUSHI( elements(arr)[index] );
1487 jint index = POPI();
1488 jshortArray arr = (jshortArray) POPA();
1489 NULLARRAYCHECK (arr);
1490 ARRAYBOUNDSCHECK (arr, index);
1491 PUSHI( elements(arr)[index] );
1597 jint value = POPI();
1598 jint index = POPI();
1599 jintArray arr = (jintArray) POPA();
1600 NULLARRAYCHECK (arr);
1601 ARRAYBOUNDSCHECK (arr, index);
1602 elements(arr)[index] = value;
1608 jlong value = POPL();
1609 jint index = POPI();
1610 jlongArray arr = (jlongArray) POPA();
1611 NULLARRAYCHECK (arr);
1612 ARRAYBOUNDSCHECK (arr, index);
1613 elements(arr)[index] = value;
1619 jfloat value = POPF();
1620 jint index = POPI();
1621 jfloatArray arr = (jfloatArray) POPA();
1622 NULLARRAYCHECK (arr);
1623 ARRAYBOUNDSCHECK (arr, index);
1624 elements(arr)[index] = value;
1630 jdouble value = POPD();
1631 jint index = POPI();
1632 jdoubleArray arr = (jdoubleArray) POPA();
1633 NULLARRAYCHECK (arr);
1634 ARRAYBOUNDSCHECK (arr, index);
1635 elements(arr)[index] = value;
1641 jobject value = POPA();
1642 jint index = POPI();
1643 jobjectArray arr = (jobjectArray) POPA();
1644 NULLARRAYCHECK (arr);
1645 ARRAYBOUNDSCHECK (arr, index);
1646 _Jv_CheckArrayStore (arr, value);
1647 elements(arr)[index] = value;
1653 jbyte value = (jbyte) POPI();
1654 jint index = POPI();
1655 jbyteArray arr = (jbyteArray) POPA();
1656 NULLARRAYCHECK (arr);
1657 ARRAYBOUNDSCHECK (arr, index);
1658 elements(arr)[index] = value;
1664 jchar value = (jchar) POPI();
1665 jint index = POPI();
1666 jcharArray arr = (jcharArray) POPA();
1667 NULLARRAYCHECK (arr);
1668 ARRAYBOUNDSCHECK (arr, index);
1669 elements(arr)[index] = value;
1675 jshort value = (jshort) POPI();
1676 jint index = POPI();
1677 jshortArray arr = (jshortArray) POPA();
1678 NULLARRAYCHECK (arr);
1679 ARRAYBOUNDSCHECK (arr, index);
1680 elements(arr)[index] = value;
1698 dupx (sp, 1, 1); sp+=1;
1702 dupx (sp, 1, 2); sp+=1;
1712 dupx (sp, 2, 1); sp+=2;
1716 dupx (sp, 2, 2); sp+=2;
1721 jobject tmp1 = POPA();
1722 jobject tmp2 = POPA();
1778 jint value2 = POPI();
1779 jint value1 = POPI();
1780 jint res = _Jv_divI (value1, value2);
1787 jlong value2 = POPL();
1788 jlong value1 = POPL();
1789 jlong res = _Jv_divJ (value1, value2);
1796 jfloat value2 = POPF();
1797 jfloat value1 = POPF();
1798 jfloat res = value1 / value2;
1805 jdouble value2 = POPD();
1806 jdouble value1 = POPD();
1807 jdouble res = value1 / value2;
1814 jint value2 = POPI();
1815 jint value1 = POPI();
1816 jint res = _Jv_remI (value1, value2);
1823 jlong value2 = POPL();
1824 jlong value1 = POPL();
1825 jlong res = _Jv_remJ (value1, value2);
1832 jfloat value2 = POPF();
1833 jfloat value1 = POPF();
1834 jfloat res = __ieee754_fmod (value1, value2);
1841 jdouble value2 = POPD();
1842 jdouble value1 = POPD();
1843 jdouble res = __ieee754_fmod (value1, value2);
1850 jint value = POPI();
1857 jlong value = POPL();
1864 jfloat value = POPF();
1871 jdouble value = POPD();
1878 jint shift = (POPI() & 0x1f);
1879 jint value = POPI();
1880 PUSHI (value << shift);
1886 jint shift = (POPI() & 0x3f);
1887 jlong value = POPL();
1888 PUSHL (value << shift);
1894 jint shift = (POPI() & 0x1f);
1895 jint value = POPI();
1896 PUSHI (value >> shift);
1902 jint shift = (POPI() & 0x3f);
1903 jlong value = POPL();
1904 PUSHL (value >> shift);
1910 jint shift = (POPI() & 0x1f);
1911 _Jv_uint value = (_Jv_uint) POPI();
1912 PUSHI ((jint) (value >> shift));
1918 jint shift = (POPI() & 0x3f);
1919 _Jv_ulong value = (_Jv_ulong) POPL();
1920 PUSHL ((jlong) (value >> shift));
1950 jint index = GET1U ();
1951 jint amount = GET1S ();
1952 locals[index].i += amount;
1957 {jlong value = POPI(); PUSHL (value);}
1961 {jfloat value = POPI(); PUSHF (value);}
1965 {jdouble value = POPI(); PUSHD (value);}
1969 {jint value = POPL(); PUSHI (value);}
1973 {jfloat value = POPL(); PUSHF (value);}
1977 {jdouble value = POPL(); PUSHD (value);}
1982 using namespace java::lang;
1983 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1990 using namespace java::lang;
1991 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
1997 { jdouble value = POPF (); PUSHD(value); }
2002 using namespace java::lang;
2003 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2010 using namespace java::lang;
2011 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2017 { jfloat value = POPD (); PUSHF(value); }
2021 { jbyte value = POPI (); PUSHI(value); }
2025 { jchar value = POPI (); PUSHI(value); }
2029 { jshort value = POPI (); PUSHI(value); }
2034 jlong value2 = POPL ();
2035 jlong value1 = POPL ();
2036 if (value1 > value2)
2038 else if (value1 == value2)
2054 jfloat value2 = POPF ();
2055 jfloat value1 = POPF ();
2056 if (value1 > value2)
2058 else if (value1 == value2)
2060 else if (value1 < value2)
2076 jdouble value2 = POPD ();
2077 jdouble value1 = POPD ();
2078 if (value1 > value2)
2080 else if (value1 == value2)
2082 else if (value1 < value2)
2145 jint value2 = POPI();
2146 jint value1 = POPI();
2147 if (value1 == value2)
2156 jint value2 = POPI();
2157 jint value1 = POPI();
2158 if (value1 != value2)
2167 jint value2 = POPI();
2168 jint value1 = POPI();
2169 if (value1 < value2)
2178 jint value2 = POPI();
2179 jint value1 = POPI();
2180 if (value1 >= value2)
2189 jint value2 = POPI();
2190 jint value1 = POPI();
2191 if (value1 > value2)
2200 jint value2 = POPI();
2201 jint value1 = POPI();
2202 if (value1 <= value2)
2211 jobject value2 = POPA();
2212 jobject value1 = POPA();
2213 if (value1 == value2)
2222 jobject value2 = POPA();
2223 jobject value1 = POPA();
2224 if (value1 != value2)
2232 #ifndef DIRECT_THREADED
2233 // For direct threaded, goto and goto_w are the same.
2234 pc = pc - 1 + get4 (pc);
2236 #endif /* DIRECT_THREADED */
2242 #ifndef DIRECT_THREADED
2243 // For direct threaded, jsr and jsr_w are the same.
2245 pc_t next = pc - 1 + get4 (pc);
2247 PUSHA ((jobject) pc);
2251 #endif /* DIRECT_THREADED */
2254 pc_t next = GOTO_VAL();
2256 PUSHA ((jobject) pc);
2263 jint index = GET1U ();
2264 pc = (pc_t) PEEKA (index);
2270 #ifdef DIRECT_THREADED
2271 void *def = (pc++)->datum;
2275 jint low = INTVAL ();
2276 jint high = INTVAL ();
2278 if (index < low || index > high)
2279 pc = (insn_slot *) def;
2281 pc = (insn_slot *) ((pc + index - low)->datum);
2283 pc_t base_pc = pc - 1;
2284 int index = POPI ();
2286 pc_t base = (pc_t) bytecode ();
2287 while ((pc - base) % 4 != 0)
2290 jint def = get4 (pc);
2291 jint low = get4 (pc + 4);
2292 jint high = get4 (pc + 8);
2293 if (index < low || index > high)
2296 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2297 #endif /* DIRECT_THREADED */
2303 #ifdef DIRECT_THREADED
2304 void *def = (pc++)->insn;
2308 jint npairs = INTVAL ();
2310 int max = npairs - 1;
2313 // Simple binary search...
2316 int half = (min + max) / 2;
2317 int match = pc[2 * half].int_val;
2322 pc = (insn_slot *) pc[2 * half + 1].datum;
2325 else if (index < match)
2326 // We can use HALF - 1 here because we check again on
2330 // We can use HALF + 1 here because we check again on
2334 if (index == pc[2 * min].int_val)
2335 pc = (insn_slot *) pc[2 * min + 1].datum;
2337 pc = (insn_slot *) def;
2339 unsigned char *base_pc = pc-1;
2342 unsigned char* base = bytecode ();
2343 while ((pc-base) % 4 != 0)
2346 jint def = get4 (pc);
2347 jint npairs = get4 (pc+4);
2352 // Simple binary search...
2355 int half = (min+max)/2;
2356 int match = get4 (pc+ 4*(2 + 2*half));
2360 else if (index < match)
2361 // We can use HALF - 1 here because we check again on
2365 // We can use HALF + 1 here because we check again on
2370 if (index == get4 (pc+ 4*(2 + 2*min)))
2371 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2374 #endif /* DIRECT_THREADED */
2379 *(jobject *) retp = POPA ();
2383 *(jlong *) retp = POPL ();
2387 *(jfloat *) retp = POPF ();
2391 *(jdouble *) retp = POPD ();
2395 *(jint *) retp = POPI ();
2403 jint fieldref_index = GET2U ();
2404 SAVE_PC(); // Constant pool resolution could throw.
2405 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2406 _Jv_Field *field = pool_data[fieldref_index].field;
2408 if ((field->flags & Modifier::STATIC) == 0)
2409 throw_incompatible_class_change_error
2410 (JvNewStringLatin1 ("field no longer static"));
2412 jclass type = field->type;
2414 // We rewrite the instruction once we discover what it refers
2416 void *newinsn = NULL;
2417 if (type->isPrimitive ())
2419 switch (type->size_in_bytes)
2422 PUSHI (*field->u.byte_addr);
2423 newinsn = AMPAMP (getstatic_resolved_1);
2427 if (type == JvPrimClass (char))
2429 PUSHI (*field->u.char_addr);
2430 newinsn = AMPAMP (getstatic_resolved_char);
2434 PUSHI (*field->u.short_addr);
2435 newinsn = AMPAMP (getstatic_resolved_short);
2440 PUSHI(*field->u.int_addr);
2441 newinsn = AMPAMP (getstatic_resolved_4);
2445 PUSHL(*field->u.long_addr);
2446 newinsn = AMPAMP (getstatic_resolved_8);
2452 PUSHA(*field->u.object_addr);
2453 newinsn = AMPAMP (getstatic_resolved_obj);
2456 #ifdef DIRECT_THREADED
2457 pc[-2].insn = newinsn;
2458 pc[-1].datum = field->u.addr;
2459 #endif /* DIRECT_THREADED */
2463 #ifdef DIRECT_THREADED
2464 getstatic_resolved_1:
2465 PUSHI (*(jbyte *) AVAL ());
2468 getstatic_resolved_char:
2469 PUSHI (*(jchar *) AVAL ());
2472 getstatic_resolved_short:
2473 PUSHI (*(jshort *) AVAL ());
2476 getstatic_resolved_4:
2477 PUSHI (*(jint *) AVAL ());
2480 getstatic_resolved_8:
2481 PUSHL (*(jlong *) AVAL ());
2484 getstatic_resolved_obj:
2485 PUSHA (*(jobject *) AVAL ());
2487 #endif /* DIRECT_THREADED */
2491 jint fieldref_index = GET2U ();
2492 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2493 _Jv_Field *field = pool_data[fieldref_index].field;
2495 if ((field->flags & Modifier::STATIC) != 0)
2496 throw_incompatible_class_change_error
2497 (JvNewStringLatin1 ("field is static"));
2499 jclass type = field->type;
2500 jint field_offset = field->u.boffset;
2501 if (field_offset > 0xffff)
2502 throw new java::lang::VirtualMachineError;
2504 jobject obj = POPA();
2507 void *newinsn = NULL;
2508 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2509 if (type->isPrimitive ())
2511 switch (type->size_in_bytes)
2514 PUSHI (val->byte_value);
2515 newinsn = AMPAMP (getfield_resolved_1);
2519 if (type == JvPrimClass (char))
2521 PUSHI (val->char_value);
2522 newinsn = AMPAMP (getfield_resolved_char);
2526 PUSHI (val->short_value);
2527 newinsn = AMPAMP (getfield_resolved_short);
2532 PUSHI (val->int_value);
2533 newinsn = AMPAMP (getfield_resolved_4);
2537 PUSHL (val->long_value);
2538 newinsn = AMPAMP (getfield_resolved_8);
2544 PUSHA (val->object_value);
2545 newinsn = AMPAMP (getfield_resolved_obj);
2548 #ifdef DIRECT_THREADED
2549 pc[-2].insn = newinsn;
2550 pc[-1].int_val = field_offset;
2551 #endif /* DIRECT_THREADED */
2555 #ifdef DIRECT_THREADED
2556 getfield_resolved_1:
2558 char *obj = (char *) POPA ();
2560 PUSHI (*(jbyte *) (obj + INTVAL ()));
2564 getfield_resolved_char:
2566 char *obj = (char *) POPA ();
2568 PUSHI (*(jchar *) (obj + INTVAL ()));
2572 getfield_resolved_short:
2574 char *obj = (char *) POPA ();
2576 PUSHI (*(jshort *) (obj + INTVAL ()));
2580 getfield_resolved_4:
2582 char *obj = (char *) POPA ();
2584 PUSHI (*(jint *) (obj + INTVAL ()));
2588 getfield_resolved_8:
2590 char *obj = (char *) POPA ();
2592 PUSHL (*(jlong *) (obj + INTVAL ()));
2596 getfield_resolved_obj:
2598 char *obj = (char *) POPA ();
2600 PUSHA (*(jobject *) (obj + INTVAL ()));
2603 #endif /* DIRECT_THREADED */
2607 jint fieldref_index = GET2U ();
2608 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2609 _Jv_Field *field = pool_data[fieldref_index].field;
2611 jclass type = field->type;
2613 // ResolvePoolEntry cannot check this
2614 if ((field->flags & Modifier::STATIC) == 0)
2615 throw_incompatible_class_change_error
2616 (JvNewStringLatin1 ("field no longer static"));
2618 void *newinsn = NULL;
2619 if (type->isPrimitive ())
2621 switch (type->size_in_bytes)
2625 jint value = POPI();
2626 *field->u.byte_addr = value;
2627 newinsn = AMPAMP (putstatic_resolved_1);
2633 jint value = POPI();
2634 *field->u.char_addr = value;
2635 newinsn = AMPAMP (putstatic_resolved_2);
2641 jint value = POPI();
2642 *field->u.int_addr = value;
2643 newinsn = AMPAMP (putstatic_resolved_4);
2649 jlong value = POPL();
2650 *field->u.long_addr = value;
2651 newinsn = AMPAMP (putstatic_resolved_8);
2658 jobject value = POPA();
2659 *field->u.object_addr = value;
2660 newinsn = AMPAMP (putstatic_resolved_obj);
2663 #ifdef DIRECT_THREADED
2664 pc[-2].insn = newinsn;
2665 pc[-1].datum = field->u.addr;
2666 #endif /* DIRECT_THREADED */
2670 #ifdef DIRECT_THREADED
2671 putstatic_resolved_1:
2672 *(jbyte *) AVAL () = POPI ();
2675 putstatic_resolved_2:
2676 *(jchar *) AVAL () = POPI ();
2679 putstatic_resolved_4:
2680 *(jint *) AVAL () = POPI ();
2683 putstatic_resolved_8:
2684 *(jlong *) AVAL () = POPL ();
2687 putstatic_resolved_obj:
2688 *(jobject *) AVAL () = POPA ();
2690 #endif /* DIRECT_THREADED */
2694 jint fieldref_index = GET2U ();
2695 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2696 _Jv_Field *field = pool_data[fieldref_index].field;
2698 jclass type = field->type;
2700 if ((field->flags & Modifier::STATIC) != 0)
2701 throw_incompatible_class_change_error
2702 (JvNewStringLatin1 ("field is static"));
2704 jint field_offset = field->u.boffset;
2705 if (field_offset > 0xffff)
2706 throw new java::lang::VirtualMachineError;
2708 void *newinsn = NULL;
2709 if (type->isPrimitive ())
2711 switch (type->size_in_bytes)
2715 jint value = POPI();
2716 jobject obj = POPA();
2718 *(jbyte*) ((char*)obj + field_offset) = value;
2719 newinsn = AMPAMP (putfield_resolved_1);
2725 jint value = POPI();
2726 jobject obj = POPA();
2728 *(jchar*) ((char*)obj + field_offset) = value;
2729 newinsn = AMPAMP (putfield_resolved_2);
2735 jint value = POPI();
2736 jobject obj = POPA();
2738 *(jint*) ((char*)obj + field_offset) = value;
2739 newinsn = AMPAMP (putfield_resolved_4);
2745 jlong value = POPL();
2746 jobject obj = POPA();
2748 *(jlong*) ((char*)obj + field_offset) = value;
2749 newinsn = AMPAMP (putfield_resolved_8);
2756 jobject value = POPA();
2757 jobject obj = POPA();
2759 *(jobject*) ((char*)obj + field_offset) = value;
2760 newinsn = AMPAMP (putfield_resolved_obj);
2763 #ifdef DIRECT_THREADED
2764 pc[-2].insn = newinsn;
2765 pc[-1].int_val = field_offset;
2766 #endif /* DIRECT_THREADED */
2770 #ifdef DIRECT_THREADED
2771 putfield_resolved_1:
2774 char *obj = (char *) POPA ();
2776 *(jbyte *) (obj + INTVAL ()) = val;
2780 putfield_resolved_2:
2783 char *obj = (char *) POPA ();
2785 *(jchar *) (obj + INTVAL ()) = val;
2789 putfield_resolved_4:
2792 char *obj = (char *) POPA ();
2794 *(jint *) (obj + INTVAL ()) = val;
2798 putfield_resolved_8:
2800 jlong val = POPL ();
2801 char *obj = (char *) POPA ();
2803 *(jlong *) (obj + INTVAL ()) = val;
2807 putfield_resolved_obj:
2809 jobject val = POPA ();
2810 char *obj = (char *) POPA ();
2812 *(jobject *) (obj + INTVAL ()) = val;
2815 #endif /* DIRECT_THREADED */
2819 int index = GET2U ();
2821 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2824 sp -= rmeth->stack_item_count;
2826 // We don't use NULLCHECK here because we can't rely on that
2827 // working for <init>. So instead we do an explicit test.
2831 throw new java::lang::NullPointerException;
2834 fun = (void (*)()) rmeth->method->ncode;
2836 #ifdef DIRECT_THREADED
2837 // Rewrite instruction so that we use a faster pre-resolved
2839 pc[-2].insn = &&invokespecial_resolved;
2840 pc[-1].datum = rmeth;
2841 #endif /* DIRECT_THREADED */
2843 goto perform_invoke;
2845 #ifdef DIRECT_THREADED
2846 invokespecial_resolved:
2848 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2849 sp -= rmeth->stack_item_count;
2850 // We don't use NULLCHECK here because we can't rely on that
2851 // working for <init>. So instead we do an explicit test.
2855 throw new java::lang::NullPointerException;
2857 fun = (void (*)()) rmeth->method->ncode;
2859 goto perform_invoke;
2860 #endif /* DIRECT_THREADED */
2864 int index = GET2U ();
2866 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2869 sp -= rmeth->stack_item_count;
2871 fun = (void (*)()) rmeth->method->ncode;
2873 #ifdef DIRECT_THREADED
2874 // Rewrite instruction so that we use a faster pre-resolved
2876 pc[-2].insn = &&invokestatic_resolved;
2877 pc[-1].datum = rmeth;
2878 #endif /* DIRECT_THREADED */
2880 goto perform_invoke;
2882 #ifdef DIRECT_THREADED
2883 invokestatic_resolved:
2885 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2886 sp -= rmeth->stack_item_count;
2887 fun = (void (*)()) rmeth->method->ncode;
2889 goto perform_invoke;
2890 #endif /* DIRECT_THREADED */
2892 insn_invokeinterface:
2894 int index = GET2U ();
2896 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2899 sp -= rmeth->stack_item_count;
2901 jobject rcv = sp[0].o;
2906 _Jv_LookupInterfaceMethod (rcv->getClass (),
2907 rmeth->method->name,
2908 rmeth->method->signature);
2910 #ifdef DIRECT_THREADED
2911 // Rewrite instruction so that we use a faster pre-resolved
2913 pc[-2].insn = &&invokeinterface_resolved;
2914 pc[-1].datum = rmeth;
2916 // Skip dummy bytes.
2918 #endif /* DIRECT_THREADED */
2920 goto perform_invoke;
2922 #ifdef DIRECT_THREADED
2923 invokeinterface_resolved:
2925 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2926 sp -= rmeth->stack_item_count;
2927 jobject rcv = sp[0].o;
2930 _Jv_LookupInterfaceMethod (rcv->getClass (),
2931 rmeth->method->name,
2932 rmeth->method->signature);
2934 goto perform_invoke;
2935 #endif /* DIRECT_THREADED */
2939 int index = GET2U ();
2940 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2942 jobject res = _Jv_AllocObject (klass);
2945 #ifdef DIRECT_THREADED
2946 pc[-2].insn = &&new_resolved;
2947 pc[-1].datum = klass;
2948 #endif /* DIRECT_THREADED */
2952 #ifdef DIRECT_THREADED
2955 jclass klass = (jclass) AVAL ();
2956 jobject res = _Jv_AllocObject (klass);
2960 #endif /* DIRECT_THREADED */
2964 int atype = GET1U ();
2966 jobject result = _Jv_NewArray (atype, size);
2973 int index = GET2U ();
2974 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2977 jobject result = _Jv_NewObjectArray (size, klass, 0);
2980 #ifdef DIRECT_THREADED
2981 pc[-2].insn = &&anewarray_resolved;
2982 pc[-1].datum = klass;
2983 #endif /* DIRECT_THREADED */
2987 #ifdef DIRECT_THREADED
2990 jclass klass = (jclass) AVAL ();
2992 jobject result = _Jv_NewObjectArray (size, klass, 0);
2996 #endif /* DIRECT_THREADED */
3000 __JArray *arr = (__JArray*)POPA();
3001 NULLARRAYCHECK (arr);
3002 PUSHI (arr->length);
3008 jobject value = POPA();
3009 throw static_cast<jthrowable>(value);
3016 jobject value = POPA();
3017 jint index = GET2U ();
3018 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3021 if (value != NULL && ! to->isInstance (value))
3022 throw new java::lang::ClassCastException (to->getName());
3026 #ifdef DIRECT_THREADED
3027 pc[-2].insn = &&checkcast_resolved;
3029 #endif /* DIRECT_THREADED */
3033 #ifdef DIRECT_THREADED
3037 jobject value = POPA ();
3038 jclass to = (jclass) AVAL ();
3039 if (value != NULL && ! to->isInstance (value))
3040 throw new java::lang::ClassCastException (to->getName());
3044 #endif /* DIRECT_THREADED */
3049 jobject value = POPA();
3050 jint index = GET2U ();
3051 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3053 PUSHI (to->isInstance (value));
3055 #ifdef DIRECT_THREADED
3056 pc[-2].insn = &&instanceof_resolved;
3058 #endif /* DIRECT_THREADED */
3062 #ifdef DIRECT_THREADED
3063 instanceof_resolved:
3065 jobject value = POPA ();
3066 jclass to = (jclass) AVAL ();
3067 PUSHI (to->isInstance (value));
3070 #endif /* DIRECT_THREADED */
3074 jobject value = POPA();
3076 _Jv_MonitorEnter (value);
3082 jobject value = POPA();
3084 _Jv_MonitorExit (value);
3090 jobject val = POPA();
3100 jobject val = POPA();
3108 insn_multianewarray:
3110 int kind_index = GET2U ();
3114 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3116 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3118 for (int i = dim - 1; i >= 0; i--)
3123 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3129 #ifndef DIRECT_THREADED
3132 jint the_mod_op = get1u (pc++);
3133 jint wide = get2u (pc); pc += 2;
3178 pc = (unsigned char*) PEEKA (wide);
3183 jint amount = get2s (pc); pc += 2;
3184 jint value = PEEKI (wide);
3185 POKEI (wide, value+amount);
3190 throw_internal_error ("illegal bytecode modified by wide");
3194 #endif /* DIRECT_THREADED */
3196 catch (java::lang::Throwable *ex)
3198 #ifdef DIRECT_THREADED
3199 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3201 int logical_pc = pc - 1 - bytecode ();
3203 _Jv_InterpException *exc = meth->exceptions ();
3204 jclass exc_class = ex->getClass ();
3206 for (int i = 0; i < meth->exc_count; i++)
3208 if (PCVAL (exc[i].start_pc) <= logical_pc
3209 && logical_pc < PCVAL (exc[i].end_pc))
3211 #ifdef DIRECT_THREADED
3212 jclass handler = (jclass) exc[i].handler_type.p;
3214 jclass handler = NULL;
3215 if (exc[i].handler_type.i != 0)
3216 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3217 exc[i].handler_type.i)).clazz;
3218 #endif /* DIRECT_THREADED */
3220 if (handler == NULL || handler->isAssignableFrom (exc_class))
3222 #ifdef DIRECT_THREADED
3223 pc = (insn_slot *) exc[i].handler_pc.p;
3225 pc = bytecode () + exc[i].handler_pc.i;
3226 #endif /* DIRECT_THREADED */
3228 sp++->o = ex; // Push exception.
3234 // No handler, so re-throw.
3240 throw_internal_error (char *msg)
3242 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3246 throw_incompatible_class_change_error (jstring msg)
3248 throw new java::lang::IncompatibleClassChangeError (msg);
3252 static java::lang::NullPointerException *null_pointer_exc;
3254 throw_null_pointer_exception ()
3256 if (null_pointer_exc == NULL)
3257 null_pointer_exc = new java::lang::NullPointerException;
3259 throw null_pointer_exc;
3263 /* Look up source code line number for given bytecode (or direct threaded
3266 _Jv_InterpMethod::get_source_line(pc_t mpc)
3268 int line = line_table_len > 0 ? line_table[0].line : -1;
3269 for (int i = 1; i < line_table_len; i++)
3270 if (line_table[i].pc > mpc)
3273 line = line_table[i].line;
3278 /** Do static initialization for fields with a constant initializer */
3280 _Jv_InitField (jobject obj, jclass klass, int index)
3282 using namespace java::lang::reflect;
3284 if (obj != 0 && klass == 0)
3285 klass = obj->getClass ();
3287 if (!_Jv_IsInterpretedClass (klass))
3290 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3292 _Jv_Field * field = (&klass->fields[0]) + index;
3294 if (index > klass->field_count)
3295 throw_internal_error ("field out of range");
3297 int init = iclass->field_initializers[index];
3301 _Jv_Constants *pool = &klass->constants;
3302 int tag = pool->tags[init];
3304 if (! field->isResolved ())
3305 throw_internal_error ("initializing unresolved field");
3307 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3308 throw_internal_error ("initializing non-static field with no object");
3312 if ((field->flags & Modifier::STATIC) != 0)
3313 addr = (void*) field->u.addr;
3315 addr = (void*) (((char*)obj) + field->u.boffset);
3319 case JV_CONSTANT_String:
3322 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3323 pool->data[init].string = str;
3324 pool->tags[init] = JV_CONSTANT_ResolvedString;
3328 case JV_CONSTANT_ResolvedString:
3329 if (! (field->type == &java::lang::String::class$
3330 || field->type == &java::lang::Class::class$))
3331 throw_class_format_error ("string initialiser to non-string field");
3333 *(jstring*)addr = pool->data[init].string;
3336 case JV_CONSTANT_Integer:
3338 int value = pool->data[init].i;
3340 if (field->type == JvPrimClass (boolean))
3341 *(jboolean*)addr = (jboolean)value;
3343 else if (field->type == JvPrimClass (byte))
3344 *(jbyte*)addr = (jbyte)value;
3346 else if (field->type == JvPrimClass (char))
3347 *(jchar*)addr = (jchar)value;
3349 else if (field->type == JvPrimClass (short))
3350 *(jshort*)addr = (jshort)value;
3352 else if (field->type == JvPrimClass (int))
3353 *(jint*)addr = (jint)value;
3356 throw_class_format_error ("erroneous field initializer");
3360 case JV_CONSTANT_Long:
3361 if (field->type != JvPrimClass (long))
3362 throw_class_format_error ("erroneous field initializer");
3364 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3367 case JV_CONSTANT_Float:
3368 if (field->type != JvPrimClass (float))
3369 throw_class_format_error ("erroneous field initializer");
3371 *(jfloat*)addr = pool->data[init].f;
3374 case JV_CONSTANT_Double:
3375 if (field->type != JvPrimClass (double))
3376 throw_class_format_error ("erroneous field initializer");
3378 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3382 throw_class_format_error ("erroneous field initializer");
3386 inline static unsigned char*
3387 skip_one_type (unsigned char* ptr)
3398 do { ch = *ptr++; } while (ch != ';');
3405 get_ffi_type_from_signature (unsigned char* ptr)
3411 return &ffi_type_pointer;
3415 // On some platforms a bool is a byte, on others an int.
3416 if (sizeof (jboolean) == sizeof (jbyte))
3417 return &ffi_type_sint8;
3420 JvAssert (sizeof (jbyte) == sizeof (jint));
3421 return &ffi_type_sint32;
3426 return &ffi_type_sint8;
3430 return &ffi_type_uint16;
3434 return &ffi_type_sint16;
3438 return &ffi_type_sint32;
3442 return &ffi_type_sint64;
3446 return &ffi_type_float;
3450 return &ffi_type_double;
3454 return &ffi_type_void;
3458 throw_internal_error ("unknown type in signature");
3461 /* this function yields the number of actual arguments, that is, if the
3462 * function is non-static, then one is added to the number of elements
3463 * found in the signature */
3466 _Jv_count_arguments (_Jv_Utf8Const *signature,
3469 unsigned char *ptr = (unsigned char*) signature->chars();
3470 int arg_count = staticp ? 0 : 1;
3472 /* first, count number of arguments */
3480 ptr = skip_one_type (ptr);
3487 /* This beast will build a cif, given the signature. Memory for
3488 * the cif itself and for the argument types must be allocated by the
3493 init_cif (_Jv_Utf8Const* signature,
3497 ffi_type **arg_types,
3500 unsigned char *ptr = (unsigned char*) signature->chars();
3502 int arg_index = 0; // arg number
3503 int item_count = 0; // stack-item count
3508 arg_types[arg_index++] = &ffi_type_pointer;
3518 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3520 if (*ptr == 'J' || *ptr == 'D')
3525 ptr = skip_one_type (ptr);
3530 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3532 ptr = skip_one_type (ptr);
3533 if (ptr != (unsigned char*)signature->chars() + signature->len())
3534 throw_internal_error ("did not find end of signature");
3536 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3537 arg_count, rtype, arg_types) != FFI_OK)
3538 throw_internal_error ("ffi_prep_cif failed");
3540 if (rtype_p != NULL)
3546 #if FFI_NATIVE_RAW_API
3547 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3548 # define FFI_RAW_SIZE ffi_raw_size
3550 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3551 # define FFI_RAW_SIZE ffi_java_raw_size
3554 /* we put this one here, and not in interpret.cc because it
3555 * calls the utility routines _Jv_count_arguments
3556 * which are static to this module. The following struct defines the
3557 * layout we use for the stubs, it's only used in the ncode method. */
3560 ffi_raw_closure closure;
3562 ffi_type *arg_types[0];
3565 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3568 _Jv_InterpMethod::ncode ()
3570 using namespace java::lang::reflect;
3572 if (self->ncode != 0)
3575 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3576 int arg_count = _Jv_count_arguments (self->signature, staticp);
3578 ncode_closure *closure =
3579 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3580 + arg_count * sizeof (ffi_type*));
3582 init_cif (self->signature,
3586 &closure->arg_types[0],
3589 ffi_closure_fun fun;
3591 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3593 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3595 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3598 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3600 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3605 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3607 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3610 FFI_PREP_RAW_CLOSURE (&closure->closure,
3615 self->ncode = (void*)closure;
3620 _Jv_JNIMethod::ncode ()
3622 using namespace java::lang::reflect;
3624 if (self->ncode != 0)
3627 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3628 int arg_count = _Jv_count_arguments (self->signature, staticp);
3630 ncode_closure *closure =
3631 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3632 + arg_count * sizeof (ffi_type*));
3635 init_cif (self->signature,
3639 &closure->arg_types[0],
3642 ffi_closure_fun fun;
3644 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3646 // Initialize the argument types and CIF that represent the actual
3647 // underlying JNI function.
3649 if ((self->accflags & Modifier::STATIC))
3651 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3652 * sizeof (ffi_type *));
3654 jni_arg_types[offset++] = &ffi_type_pointer;
3655 if ((self->accflags & Modifier::STATIC))
3656 jni_arg_types[offset++] = &ffi_type_pointer;
3657 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3658 arg_count * sizeof (ffi_type *));
3660 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3661 extra_args + arg_count, rtype,
3662 jni_arg_types) != FFI_OK)
3663 throw_internal_error ("ffi_prep_cif failed for JNI function");
3665 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3667 // FIXME: for now we assume that all native methods for
3668 // interpreted code use JNI.
3669 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3671 FFI_PREP_RAW_CLOSURE (&closure->closure,
3676 self->ncode = (void *) closure;
3681 throw_class_format_error (jstring msg)
3684 ? new java::lang::ClassFormatError (msg)
3685 : new java::lang::ClassFormatError);
3689 throw_class_format_error (char *msg)
3691 throw_class_format_error (JvNewStringLatin1 (msg));
3697 _Jv_InterpreterEngine::do_verify (jclass klass)
3699 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3700 for (int i = 0; i < klass->method_count; i++)
3702 using namespace java::lang::reflect;
3703 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3704 _Jv_ushort accflags = klass->methods[i].accflags;
3705 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3707 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3708 _Jv_VerifyMethod (im);
3714 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3716 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3717 for (int i = 0; i < klass->method_count; i++)
3719 // Just skip abstract methods. This is particularly important
3720 // because we don't resize the interpreted_methods array when
3721 // miranda methods are added to it.
3722 if ((klass->methods[i].accflags
3723 & java::lang::reflect::Modifier::ABSTRACT)
3727 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3729 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3732 // You might think we could use a virtual `ncode' method in
3733 // the _Jv_MethodBase and unify the native and non-native
3734 // cases. Well, we can't, because we don't allocate these
3735 // objects using `new', and thus they don't get a vtable.
3736 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3737 klass->methods[i].ncode = jnim->ncode ();
3739 else if (imeth != 0) // it could be abstract
3741 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3742 klass->methods[i].ncode = im->ncode ();
3748 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3751 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3753 char *static_data = (char *) _Jv_AllocBytes (static_size);
3754 memset (static_data, 0, static_size);
3756 for (int i = 0; i < klass->field_count; i++)
3758 _Jv_Field *field = &klass->fields[i];
3760 if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3762 field->u.addr = static_data + field->u.boffset;
3764 if (iclass->field_initializers[i] != 0)
3766 _Jv_Linker::resolve_field (field, klass->loader);
3767 _Jv_InitField (0, klass, i);
3772 // Now we don't need the field_initializers anymore, so let the
3773 // collector get rid of it.
3774 iclass->field_initializers = 0;
3777 _Jv_ResolvedMethod *
3778 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3779 jboolean staticp, jint vtable_index)
3781 int arg_count = _Jv_count_arguments (method->signature, staticp);
3783 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3784 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3785 + arg_count*sizeof (ffi_type*));
3787 result->stack_item_count
3788 = init_cif (method->signature,
3792 &result->arg_types[0],
3795 result->vtable_index = vtable_index;
3796 result->method = method;
3797 result->klass = klass;
3803 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3805 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3806 for (int i = 0; i < klass->method_count; i++)
3808 // Just skip abstract methods. This is particularly important
3809 // because we don't resize the interpreted_methods array when
3810 // miranda methods are added to it.
3811 if ((klass->methods[i].accflags
3812 & java::lang::reflect::Modifier::ABSTRACT)
3815 // Miranda method additions mean that the `methods' array moves.
3816 // We cache a pointer into this array, so we have to update.
3817 iclass->interpreted_methods[i]->self = &klass->methods[i];
3821 #endif // INTERPRETER