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 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 // It isn't worth throwing an exception if this table is
767 // corrupted, but at the same time we don't want a crash.
768 if (byte_pc < 0 || byte_pc >= code_length)
770 line_table[i].pc = &insns[pc_mapping[byte_pc]];
775 #endif /* DIRECT_THREADED */
778 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
780 using namespace java::lang::reflect;
782 // FRAME_DESC registers this particular invocation as the top-most
783 // interpreter frame. This lets the stack tracing code (for
784 // Throwable) print information about the method being interpreted
785 // rather than about the interpreter itself. FRAME_DESC has a
786 // destructor so it cleans up automatically when the interpreter
788 java::lang::Thread *thread = java::lang::Thread::currentThread();
789 _Jv_InterpFrame frame_desc (meth,
790 (_Jv_InterpFrame **) &thread->interp_frame);
792 _Jv_word stack[meth->max_stack];
793 _Jv_word *sp = stack;
795 _Jv_word locals[meth->max_locals];
797 /* Go straight at it! the ffi raw format matches the internal
798 stack representation exactly. At least, that's the idea.
800 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
802 _Jv_word *pool_data = meth->defining_class->constants.data;
804 /* These three are temporaries for common code used by several
807 _Jv_ResolvedMethod* rmeth;
810 #define INSN_LABEL(op) &&insn_##op
812 static const void *const insn_target[] =
815 INSN_LABEL(aconst_null),
816 INSN_LABEL(iconst_m1),
817 INSN_LABEL(iconst_0),
818 INSN_LABEL(iconst_1),
819 INSN_LABEL(iconst_2),
820 INSN_LABEL(iconst_3),
821 INSN_LABEL(iconst_4),
822 INSN_LABEL(iconst_5),
823 INSN_LABEL(lconst_0),
824 INSN_LABEL(lconst_1),
825 INSN_LABEL(fconst_0),
826 INSN_LABEL(fconst_1),
827 INSN_LABEL(fconst_2),
828 INSN_LABEL(dconst_0),
829 INSN_LABEL(dconst_1),
873 INSN_LABEL(istore_0),
874 INSN_LABEL(istore_1),
875 INSN_LABEL(istore_2),
876 INSN_LABEL(istore_3),
877 INSN_LABEL(lstore_0),
878 INSN_LABEL(lstore_1),
879 INSN_LABEL(lstore_2),
880 INSN_LABEL(lstore_3),
881 INSN_LABEL(fstore_0),
882 INSN_LABEL(fstore_1),
883 INSN_LABEL(fstore_2),
884 INSN_LABEL(fstore_3),
885 INSN_LABEL(dstore_0),
886 INSN_LABEL(dstore_1),
887 INSN_LABEL(dstore_2),
888 INSN_LABEL(dstore_3),
889 INSN_LABEL(astore_0),
890 INSN_LABEL(astore_1),
891 INSN_LABEL(astore_2),
892 INSN_LABEL(astore_3),
973 INSN_LABEL(if_icmpeq),
974 INSN_LABEL(if_icmpne),
975 INSN_LABEL(if_icmplt),
976 INSN_LABEL(if_icmpge),
977 INSN_LABEL(if_icmpgt),
978 INSN_LABEL(if_icmple),
979 INSN_LABEL(if_acmpeq),
980 INSN_LABEL(if_acmpne),
984 INSN_LABEL(tableswitch),
985 INSN_LABEL(lookupswitch),
992 INSN_LABEL(getstatic),
993 INSN_LABEL(putstatic),
994 INSN_LABEL(getfield),
995 INSN_LABEL(putfield),
996 INSN_LABEL(invokevirtual),
997 INSN_LABEL(invokespecial),
998 INSN_LABEL(invokestatic),
999 INSN_LABEL(invokeinterface),
1002 INSN_LABEL(newarray),
1003 INSN_LABEL(anewarray),
1004 INSN_LABEL(arraylength),
1006 INSN_LABEL(checkcast),
1007 INSN_LABEL(instanceof),
1008 INSN_LABEL(monitorenter),
1009 INSN_LABEL(monitorexit),
1010 #ifdef DIRECT_THREADED
1015 INSN_LABEL(multianewarray),
1017 INSN_LABEL(ifnonnull),
1025 #ifdef DIRECT_THREADED
1027 #define NEXT_INSN goto *((pc++)->insn)
1028 #define INTVAL() ((pc++)->int_val)
1029 #define AVAL() ((pc++)->datum)
1031 #define GET1S() INTVAL ()
1032 #define GET2S() INTVAL ()
1033 #define GET1U() INTVAL ()
1034 #define GET2U() INTVAL ()
1035 #define AVAL1U() AVAL ()
1036 #define AVAL2U() AVAL ()
1037 #define AVAL2UP() AVAL ()
1038 #define SKIP_GOTO ++pc
1039 #define GOTO_VAL() (insn_slot *) pc->datum
1040 #define PCVAL(unionval) unionval.p
1041 #define AMPAMP(label) &&label
1043 // Compile if we must. NOTE: Double-check locking.
1044 if (meth->prepared == NULL)
1046 _Jv_MutexLock (&compile_mutex);
1047 if (meth->prepared == NULL)
1048 meth->compile (insn_target);
1049 _Jv_MutexUnlock (&compile_mutex);
1051 pc = (insn_slot *) meth->prepared;
1055 #define NEXT_INSN goto *(insn_target[*pc++])
1057 #define GET1S() get1s (pc++)
1058 #define GET2S() (pc += 2, get2s (pc- 2))
1059 #define GET1U() get1u (pc++)
1060 #define GET2U() (pc += 2, get2u (pc - 2))
1061 #define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
1062 #define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
1063 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1064 #define SKIP_GOTO pc += 2
1065 #define GOTO_VAL() pc - 1 + get2s (pc)
1066 #define PCVAL(unionval) unionval.i
1067 #define AMPAMP(label) NULL
1071 #endif /* DIRECT_THREADED */
1073 #define TAKE_GOTO pc = GOTO_VAL ()
1077 // We keep nop around. It is used if we're interpreting the
1078 // bytecodes and not doing direct threading.
1082 /* The first few instructions here are ordered according to their
1083 frequency, in the hope that this will improve code locality a
1086 insn_aload_0: // 0x2a
1094 insn_iload_1: // 0x1b
1098 insn_invokevirtual: // 0xb6
1100 int index = GET2U ();
1102 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1103 * value already is resolved. If we want to clutter up the
1104 * code here to gain a little performance, then we can check
1105 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1106 * directly. For now, I don't think it is worth it. */
1109 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1112 sp -= rmeth->stack_item_count;
1113 // We don't use NULLCHECK here because we can't rely on that
1114 // working if the method is final. So instead we do an
1118 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1119 throw new java::lang::NullPointerException;
1122 if (rmeth->vtable_index == -1)
1124 // final methods do not appear in the vtable,
1125 // if it does not appear in the superclass.
1126 fun = (void (*)()) rmeth->method->ncode;
1130 jobject rcv = sp[0].o;
1131 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1132 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1135 #ifdef DIRECT_THREADED
1136 // Rewrite instruction so that we use a faster pre-resolved
1138 pc[-2].insn = &&invokevirtual_resolved;
1139 pc[-1].datum = rmeth;
1140 #endif /* DIRECT_THREADED */
1142 goto perform_invoke;
1144 #ifdef DIRECT_THREADED
1145 invokevirtual_resolved:
1147 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1148 sp -= rmeth->stack_item_count;
1149 // We don't use NULLCHECK here because we can't rely on that
1150 // working if the method is final. So instead we do an
1155 throw new java::lang::NullPointerException;
1158 if (rmeth->vtable_index == -1)
1160 // final methods do not appear in the vtable,
1161 // if it does not appear in the superclass.
1162 fun = (void (*)()) rmeth->method->ncode;
1166 jobject rcv = sp[0].o;
1167 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1168 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1171 goto perform_invoke;
1172 #endif /* DIRECT_THREADED */
1178 /* here goes the magic again... */
1179 ffi_cif *cif = &rmeth->cif;
1180 ffi_raw *raw = (ffi_raw*) sp;
1184 #if FFI_NATIVE_RAW_API
1185 /* We assume that this is only implemented if it's correct */
1186 /* to use it here. On a 64 bit machine, it never is. */
1187 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1189 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1192 int rtype = cif->rtype->type;
1194 /* the likelyhood of object, int, or void return is very high,
1195 * so those are checked before the switch */
1196 if (rtype == FFI_TYPE_POINTER)
1198 PUSHA (rvalue.object_value);
1200 else if (rtype == FFI_TYPE_SINT32)
1202 PUSHI (rvalue.int_value);
1204 else if (rtype == FFI_TYPE_VOID)
1212 case FFI_TYPE_SINT8:
1213 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1216 case FFI_TYPE_SINT16:
1217 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1220 case FFI_TYPE_UINT16:
1221 PUSHI (rvalue.int_value & 0xffff);
1224 case FFI_TYPE_FLOAT:
1225 PUSHF (rvalue.float_value);
1228 case FFI_TYPE_DOUBLE:
1229 PUSHD (rvalue.double_value);
1232 case FFI_TYPE_SINT64:
1233 PUSHL (rvalue.long_value);
1237 throw_internal_error ("unknown return type in invokeXXX");
1304 // For direct threaded, bipush and sipush are the same.
1305 #ifndef DIRECT_THREADED
1308 #endif /* DIRECT_THREADED */
1314 // For direct threaded, ldc and ldc_w are the same.
1315 #ifndef DIRECT_THREADED
1316 PUSHA ((jobject) AVAL1U ());
1318 #endif /* DIRECT_THREADED */
1320 PUSHA ((jobject) AVAL2U ());
1325 void *where = AVAL2UP ();
1326 memcpy (sp, where, 2*sizeof (_Jv_word));
1421 jint index = POPI();
1422 jintArray arr = (jintArray) POPA();
1423 NULLARRAYCHECK (arr);
1424 ARRAYBOUNDSCHECK (arr, index);
1425 PUSHI( elements(arr)[index] );
1431 jint index = POPI();
1432 jlongArray arr = (jlongArray) POPA();
1433 NULLARRAYCHECK (arr);
1434 ARRAYBOUNDSCHECK (arr, index);
1435 PUSHL( elements(arr)[index] );
1441 jint index = POPI();
1442 jfloatArray arr = (jfloatArray) POPA();
1443 NULLARRAYCHECK (arr);
1444 ARRAYBOUNDSCHECK (arr, index);
1445 PUSHF( elements(arr)[index] );
1451 jint index = POPI();
1452 jdoubleArray arr = (jdoubleArray) POPA();
1453 NULLARRAYCHECK (arr);
1454 ARRAYBOUNDSCHECK (arr, index);
1455 PUSHD( elements(arr)[index] );
1461 jint index = POPI();
1462 jobjectArray arr = (jobjectArray) POPA();
1463 NULLARRAYCHECK (arr);
1464 ARRAYBOUNDSCHECK (arr, index);
1465 PUSHA( elements(arr)[index] );
1471 jint index = POPI();
1472 jbyteArray arr = (jbyteArray) POPA();
1473 NULLARRAYCHECK (arr);
1474 ARRAYBOUNDSCHECK (arr, index);
1475 PUSHI( elements(arr)[index] );
1481 jint index = POPI();
1482 jcharArray arr = (jcharArray) POPA();
1483 NULLARRAYCHECK (arr);
1484 ARRAYBOUNDSCHECK (arr, index);
1485 PUSHI( elements(arr)[index] );
1491 jint index = POPI();
1492 jshortArray arr = (jshortArray) POPA();
1493 NULLARRAYCHECK (arr);
1494 ARRAYBOUNDSCHECK (arr, index);
1495 PUSHI( elements(arr)[index] );
1601 jint value = POPI();
1602 jint index = POPI();
1603 jintArray arr = (jintArray) POPA();
1604 NULLARRAYCHECK (arr);
1605 ARRAYBOUNDSCHECK (arr, index);
1606 elements(arr)[index] = value;
1612 jlong value = POPL();
1613 jint index = POPI();
1614 jlongArray arr = (jlongArray) POPA();
1615 NULLARRAYCHECK (arr);
1616 ARRAYBOUNDSCHECK (arr, index);
1617 elements(arr)[index] = value;
1623 jfloat value = POPF();
1624 jint index = POPI();
1625 jfloatArray arr = (jfloatArray) POPA();
1626 NULLARRAYCHECK (arr);
1627 ARRAYBOUNDSCHECK (arr, index);
1628 elements(arr)[index] = value;
1634 jdouble value = POPD();
1635 jint index = POPI();
1636 jdoubleArray arr = (jdoubleArray) POPA();
1637 NULLARRAYCHECK (arr);
1638 ARRAYBOUNDSCHECK (arr, index);
1639 elements(arr)[index] = value;
1645 jobject value = POPA();
1646 jint index = POPI();
1647 jobjectArray arr = (jobjectArray) POPA();
1648 NULLARRAYCHECK (arr);
1649 ARRAYBOUNDSCHECK (arr, index);
1650 _Jv_CheckArrayStore (arr, value);
1651 elements(arr)[index] = value;
1657 jbyte value = (jbyte) POPI();
1658 jint index = POPI();
1659 jbyteArray arr = (jbyteArray) POPA();
1660 NULLARRAYCHECK (arr);
1661 ARRAYBOUNDSCHECK (arr, index);
1662 elements(arr)[index] = value;
1668 jchar value = (jchar) POPI();
1669 jint index = POPI();
1670 jcharArray arr = (jcharArray) POPA();
1671 NULLARRAYCHECK (arr);
1672 ARRAYBOUNDSCHECK (arr, index);
1673 elements(arr)[index] = value;
1679 jshort value = (jshort) POPI();
1680 jint index = POPI();
1681 jshortArray arr = (jshortArray) POPA();
1682 NULLARRAYCHECK (arr);
1683 ARRAYBOUNDSCHECK (arr, index);
1684 elements(arr)[index] = value;
1702 dupx (sp, 1, 1); sp+=1;
1706 dupx (sp, 1, 2); sp+=1;
1716 dupx (sp, 2, 1); sp+=2;
1720 dupx (sp, 2, 2); sp+=2;
1725 jobject tmp1 = POPA();
1726 jobject tmp2 = POPA();
1782 jint value2 = POPI();
1783 jint value1 = POPI();
1784 jint res = _Jv_divI (value1, value2);
1791 jlong value2 = POPL();
1792 jlong value1 = POPL();
1793 jlong res = _Jv_divJ (value1, value2);
1800 jfloat value2 = POPF();
1801 jfloat value1 = POPF();
1802 jfloat res = value1 / value2;
1809 jdouble value2 = POPD();
1810 jdouble value1 = POPD();
1811 jdouble res = value1 / value2;
1818 jint value2 = POPI();
1819 jint value1 = POPI();
1820 jint res = _Jv_remI (value1, value2);
1827 jlong value2 = POPL();
1828 jlong value1 = POPL();
1829 jlong res = _Jv_remJ (value1, value2);
1836 jfloat value2 = POPF();
1837 jfloat value1 = POPF();
1838 jfloat res = __ieee754_fmod (value1, value2);
1845 jdouble value2 = POPD();
1846 jdouble value1 = POPD();
1847 jdouble res = __ieee754_fmod (value1, value2);
1854 jint value = POPI();
1861 jlong value = POPL();
1868 jfloat value = POPF();
1875 jdouble value = POPD();
1882 jint shift = (POPI() & 0x1f);
1883 jint value = POPI();
1884 PUSHI (value << shift);
1890 jint shift = (POPI() & 0x3f);
1891 jlong value = POPL();
1892 PUSHL (value << shift);
1898 jint shift = (POPI() & 0x1f);
1899 jint value = POPI();
1900 PUSHI (value >> shift);
1906 jint shift = (POPI() & 0x3f);
1907 jlong value = POPL();
1908 PUSHL (value >> shift);
1914 jint shift = (POPI() & 0x1f);
1915 _Jv_uint value = (_Jv_uint) POPI();
1916 PUSHI ((jint) (value >> shift));
1922 jint shift = (POPI() & 0x3f);
1923 _Jv_ulong value = (_Jv_ulong) POPL();
1924 PUSHL ((jlong) (value >> shift));
1954 jint index = GET1U ();
1955 jint amount = GET1S ();
1956 locals[index].i += amount;
1961 {jlong value = POPI(); PUSHL (value);}
1965 {jfloat value = POPI(); PUSHF (value);}
1969 {jdouble value = POPI(); PUSHD (value);}
1973 {jint value = POPL(); PUSHI (value);}
1977 {jfloat value = POPL(); PUSHF (value);}
1981 {jdouble value = POPL(); PUSHD (value);}
1986 using namespace java::lang;
1987 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1994 using namespace java::lang;
1995 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2001 { jdouble value = POPF (); PUSHD(value); }
2006 using namespace java::lang;
2007 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2014 using namespace java::lang;
2015 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2021 { jfloat value = POPD (); PUSHF(value); }
2025 { jbyte value = POPI (); PUSHI(value); }
2029 { jchar value = POPI (); PUSHI(value); }
2033 { jshort value = POPI (); PUSHI(value); }
2038 jlong value2 = POPL ();
2039 jlong value1 = POPL ();
2040 if (value1 > value2)
2042 else if (value1 == value2)
2058 jfloat value2 = POPF ();
2059 jfloat value1 = POPF ();
2060 if (value1 > value2)
2062 else if (value1 == value2)
2064 else if (value1 < value2)
2080 jdouble value2 = POPD ();
2081 jdouble value1 = POPD ();
2082 if (value1 > value2)
2084 else if (value1 == value2)
2086 else if (value1 < value2)
2149 jint value2 = POPI();
2150 jint value1 = POPI();
2151 if (value1 == value2)
2160 jint value2 = POPI();
2161 jint value1 = POPI();
2162 if (value1 != value2)
2171 jint value2 = POPI();
2172 jint value1 = POPI();
2173 if (value1 < value2)
2182 jint value2 = POPI();
2183 jint value1 = POPI();
2184 if (value1 >= value2)
2193 jint value2 = POPI();
2194 jint value1 = POPI();
2195 if (value1 > value2)
2204 jint value2 = POPI();
2205 jint value1 = POPI();
2206 if (value1 <= value2)
2215 jobject value2 = POPA();
2216 jobject value1 = POPA();
2217 if (value1 == value2)
2226 jobject value2 = POPA();
2227 jobject value1 = POPA();
2228 if (value1 != value2)
2236 #ifndef DIRECT_THREADED
2237 // For direct threaded, goto and goto_w are the same.
2238 pc = pc - 1 + get4 (pc);
2240 #endif /* DIRECT_THREADED */
2246 #ifndef DIRECT_THREADED
2247 // For direct threaded, jsr and jsr_w are the same.
2249 pc_t next = pc - 1 + get4 (pc);
2251 PUSHA ((jobject) pc);
2255 #endif /* DIRECT_THREADED */
2258 pc_t next = GOTO_VAL();
2260 PUSHA ((jobject) pc);
2267 jint index = GET1U ();
2268 pc = (pc_t) PEEKA (index);
2274 #ifdef DIRECT_THREADED
2275 void *def = (pc++)->datum;
2279 jint low = INTVAL ();
2280 jint high = INTVAL ();
2282 if (index < low || index > high)
2283 pc = (insn_slot *) def;
2285 pc = (insn_slot *) ((pc + index - low)->datum);
2287 pc_t base_pc = pc - 1;
2288 int index = POPI ();
2290 pc_t base = (pc_t) bytecode ();
2291 while ((pc - base) % 4 != 0)
2294 jint def = get4 (pc);
2295 jint low = get4 (pc + 4);
2296 jint high = get4 (pc + 8);
2297 if (index < low || index > high)
2300 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2301 #endif /* DIRECT_THREADED */
2307 #ifdef DIRECT_THREADED
2308 void *def = (pc++)->insn;
2312 jint npairs = INTVAL ();
2314 int max = npairs - 1;
2317 // Simple binary search...
2320 int half = (min + max) / 2;
2321 int match = pc[2 * half].int_val;
2326 pc = (insn_slot *) pc[2 * half + 1].datum;
2329 else if (index < match)
2330 // We can use HALF - 1 here because we check again on
2334 // We can use HALF + 1 here because we check again on
2338 if (index == pc[2 * min].int_val)
2339 pc = (insn_slot *) pc[2 * min + 1].datum;
2341 pc = (insn_slot *) def;
2343 unsigned char *base_pc = pc-1;
2346 unsigned char* base = bytecode ();
2347 while ((pc-base) % 4 != 0)
2350 jint def = get4 (pc);
2351 jint npairs = get4 (pc+4);
2356 // Simple binary search...
2359 int half = (min+max)/2;
2360 int match = get4 (pc+ 4*(2 + 2*half));
2364 else if (index < match)
2365 // We can use HALF - 1 here because we check again on
2369 // We can use HALF + 1 here because we check again on
2374 if (index == get4 (pc+ 4*(2 + 2*min)))
2375 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2378 #endif /* DIRECT_THREADED */
2383 *(jobject *) retp = POPA ();
2387 *(jlong *) retp = POPL ();
2391 *(jfloat *) retp = POPF ();
2395 *(jdouble *) retp = POPD ();
2399 *(jint *) retp = POPI ();
2407 jint fieldref_index = GET2U ();
2408 SAVE_PC(); // Constant pool resolution could throw.
2409 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2410 _Jv_Field *field = pool_data[fieldref_index].field;
2412 if ((field->flags & Modifier::STATIC) == 0)
2413 throw_incompatible_class_change_error
2414 (JvNewStringLatin1 ("field no longer static"));
2416 jclass type = field->type;
2418 // We rewrite the instruction once we discover what it refers
2420 void *newinsn = NULL;
2421 if (type->isPrimitive ())
2423 switch (type->size_in_bytes)
2426 PUSHI (*field->u.byte_addr);
2427 newinsn = AMPAMP (getstatic_resolved_1);
2431 if (type == JvPrimClass (char))
2433 PUSHI (*field->u.char_addr);
2434 newinsn = AMPAMP (getstatic_resolved_char);
2438 PUSHI (*field->u.short_addr);
2439 newinsn = AMPAMP (getstatic_resolved_short);
2444 PUSHI(*field->u.int_addr);
2445 newinsn = AMPAMP (getstatic_resolved_4);
2449 PUSHL(*field->u.long_addr);
2450 newinsn = AMPAMP (getstatic_resolved_8);
2456 PUSHA(*field->u.object_addr);
2457 newinsn = AMPAMP (getstatic_resolved_obj);
2460 #ifdef DIRECT_THREADED
2461 pc[-2].insn = newinsn;
2462 pc[-1].datum = field->u.addr;
2463 #endif /* DIRECT_THREADED */
2467 #ifdef DIRECT_THREADED
2468 getstatic_resolved_1:
2469 PUSHI (*(jbyte *) AVAL ());
2472 getstatic_resolved_char:
2473 PUSHI (*(jchar *) AVAL ());
2476 getstatic_resolved_short:
2477 PUSHI (*(jshort *) AVAL ());
2480 getstatic_resolved_4:
2481 PUSHI (*(jint *) AVAL ());
2484 getstatic_resolved_8:
2485 PUSHL (*(jlong *) AVAL ());
2488 getstatic_resolved_obj:
2489 PUSHA (*(jobject *) AVAL ());
2491 #endif /* DIRECT_THREADED */
2495 jint fieldref_index = GET2U ();
2496 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2497 _Jv_Field *field = pool_data[fieldref_index].field;
2499 if ((field->flags & Modifier::STATIC) != 0)
2500 throw_incompatible_class_change_error
2501 (JvNewStringLatin1 ("field is static"));
2503 jclass type = field->type;
2504 jint field_offset = field->u.boffset;
2505 if (field_offset > 0xffff)
2506 throw new java::lang::VirtualMachineError;
2508 jobject obj = POPA();
2511 void *newinsn = NULL;
2512 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2513 if (type->isPrimitive ())
2515 switch (type->size_in_bytes)
2518 PUSHI (val->byte_value);
2519 newinsn = AMPAMP (getfield_resolved_1);
2523 if (type == JvPrimClass (char))
2525 PUSHI (val->char_value);
2526 newinsn = AMPAMP (getfield_resolved_char);
2530 PUSHI (val->short_value);
2531 newinsn = AMPAMP (getfield_resolved_short);
2536 PUSHI (val->int_value);
2537 newinsn = AMPAMP (getfield_resolved_4);
2541 PUSHL (val->long_value);
2542 newinsn = AMPAMP (getfield_resolved_8);
2548 PUSHA (val->object_value);
2549 newinsn = AMPAMP (getfield_resolved_obj);
2552 #ifdef DIRECT_THREADED
2553 pc[-2].insn = newinsn;
2554 pc[-1].int_val = field_offset;
2555 #endif /* DIRECT_THREADED */
2559 #ifdef DIRECT_THREADED
2560 getfield_resolved_1:
2562 char *obj = (char *) POPA ();
2564 PUSHI (*(jbyte *) (obj + INTVAL ()));
2568 getfield_resolved_char:
2570 char *obj = (char *) POPA ();
2572 PUSHI (*(jchar *) (obj + INTVAL ()));
2576 getfield_resolved_short:
2578 char *obj = (char *) POPA ();
2580 PUSHI (*(jshort *) (obj + INTVAL ()));
2584 getfield_resolved_4:
2586 char *obj = (char *) POPA ();
2588 PUSHI (*(jint *) (obj + INTVAL ()));
2592 getfield_resolved_8:
2594 char *obj = (char *) POPA ();
2596 PUSHL (*(jlong *) (obj + INTVAL ()));
2600 getfield_resolved_obj:
2602 char *obj = (char *) POPA ();
2604 PUSHA (*(jobject *) (obj + INTVAL ()));
2607 #endif /* DIRECT_THREADED */
2611 jint fieldref_index = GET2U ();
2612 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2613 _Jv_Field *field = pool_data[fieldref_index].field;
2615 jclass type = field->type;
2617 // ResolvePoolEntry cannot check this
2618 if ((field->flags & Modifier::STATIC) == 0)
2619 throw_incompatible_class_change_error
2620 (JvNewStringLatin1 ("field no longer static"));
2622 void *newinsn = NULL;
2623 if (type->isPrimitive ())
2625 switch (type->size_in_bytes)
2629 jint value = POPI();
2630 *field->u.byte_addr = value;
2631 newinsn = AMPAMP (putstatic_resolved_1);
2637 jint value = POPI();
2638 *field->u.char_addr = value;
2639 newinsn = AMPAMP (putstatic_resolved_2);
2645 jint value = POPI();
2646 *field->u.int_addr = value;
2647 newinsn = AMPAMP (putstatic_resolved_4);
2653 jlong value = POPL();
2654 *field->u.long_addr = value;
2655 newinsn = AMPAMP (putstatic_resolved_8);
2662 jobject value = POPA();
2663 *field->u.object_addr = value;
2664 newinsn = AMPAMP (putstatic_resolved_obj);
2667 #ifdef DIRECT_THREADED
2668 pc[-2].insn = newinsn;
2669 pc[-1].datum = field->u.addr;
2670 #endif /* DIRECT_THREADED */
2674 #ifdef DIRECT_THREADED
2675 putstatic_resolved_1:
2676 *(jbyte *) AVAL () = POPI ();
2679 putstatic_resolved_2:
2680 *(jchar *) AVAL () = POPI ();
2683 putstatic_resolved_4:
2684 *(jint *) AVAL () = POPI ();
2687 putstatic_resolved_8:
2688 *(jlong *) AVAL () = POPL ();
2691 putstatic_resolved_obj:
2692 *(jobject *) AVAL () = POPA ();
2694 #endif /* DIRECT_THREADED */
2698 jint fieldref_index = GET2U ();
2699 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2700 _Jv_Field *field = pool_data[fieldref_index].field;
2702 jclass type = field->type;
2704 if ((field->flags & Modifier::STATIC) != 0)
2705 throw_incompatible_class_change_error
2706 (JvNewStringLatin1 ("field is static"));
2708 jint field_offset = field->u.boffset;
2709 if (field_offset > 0xffff)
2710 throw new java::lang::VirtualMachineError;
2712 void *newinsn = NULL;
2713 if (type->isPrimitive ())
2715 switch (type->size_in_bytes)
2719 jint value = POPI();
2720 jobject obj = POPA();
2722 *(jbyte*) ((char*)obj + field_offset) = value;
2723 newinsn = AMPAMP (putfield_resolved_1);
2729 jint value = POPI();
2730 jobject obj = POPA();
2732 *(jchar*) ((char*)obj + field_offset) = value;
2733 newinsn = AMPAMP (putfield_resolved_2);
2739 jint value = POPI();
2740 jobject obj = POPA();
2742 *(jint*) ((char*)obj + field_offset) = value;
2743 newinsn = AMPAMP (putfield_resolved_4);
2749 jlong value = POPL();
2750 jobject obj = POPA();
2752 *(jlong*) ((char*)obj + field_offset) = value;
2753 newinsn = AMPAMP (putfield_resolved_8);
2760 jobject value = POPA();
2761 jobject obj = POPA();
2763 *(jobject*) ((char*)obj + field_offset) = value;
2764 newinsn = AMPAMP (putfield_resolved_obj);
2767 #ifdef DIRECT_THREADED
2768 pc[-2].insn = newinsn;
2769 pc[-1].int_val = field_offset;
2770 #endif /* DIRECT_THREADED */
2774 #ifdef DIRECT_THREADED
2775 putfield_resolved_1:
2778 char *obj = (char *) POPA ();
2780 *(jbyte *) (obj + INTVAL ()) = val;
2784 putfield_resolved_2:
2787 char *obj = (char *) POPA ();
2789 *(jchar *) (obj + INTVAL ()) = val;
2793 putfield_resolved_4:
2796 char *obj = (char *) POPA ();
2798 *(jint *) (obj + INTVAL ()) = val;
2802 putfield_resolved_8:
2804 jlong val = POPL ();
2805 char *obj = (char *) POPA ();
2807 *(jlong *) (obj + INTVAL ()) = val;
2811 putfield_resolved_obj:
2813 jobject val = POPA ();
2814 char *obj = (char *) POPA ();
2816 *(jobject *) (obj + INTVAL ()) = val;
2819 #endif /* DIRECT_THREADED */
2823 int index = GET2U ();
2825 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2828 sp -= rmeth->stack_item_count;
2830 // We don't use NULLCHECK here because we can't rely on that
2831 // working for <init>. So instead we do an explicit test.
2835 throw new java::lang::NullPointerException;
2838 fun = (void (*)()) rmeth->method->ncode;
2840 #ifdef DIRECT_THREADED
2841 // Rewrite instruction so that we use a faster pre-resolved
2843 pc[-2].insn = &&invokespecial_resolved;
2844 pc[-1].datum = rmeth;
2845 #endif /* DIRECT_THREADED */
2847 goto perform_invoke;
2849 #ifdef DIRECT_THREADED
2850 invokespecial_resolved:
2852 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2853 sp -= rmeth->stack_item_count;
2854 // We don't use NULLCHECK here because we can't rely on that
2855 // working for <init>. So instead we do an explicit test.
2859 throw new java::lang::NullPointerException;
2861 fun = (void (*)()) rmeth->method->ncode;
2863 goto perform_invoke;
2864 #endif /* DIRECT_THREADED */
2868 int index = GET2U ();
2870 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2873 sp -= rmeth->stack_item_count;
2875 fun = (void (*)()) rmeth->method->ncode;
2877 #ifdef DIRECT_THREADED
2878 // Rewrite instruction so that we use a faster pre-resolved
2880 pc[-2].insn = &&invokestatic_resolved;
2881 pc[-1].datum = rmeth;
2882 #endif /* DIRECT_THREADED */
2884 goto perform_invoke;
2886 #ifdef DIRECT_THREADED
2887 invokestatic_resolved:
2889 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2890 sp -= rmeth->stack_item_count;
2891 fun = (void (*)()) rmeth->method->ncode;
2893 goto perform_invoke;
2894 #endif /* DIRECT_THREADED */
2896 insn_invokeinterface:
2898 int index = GET2U ();
2900 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2903 sp -= rmeth->stack_item_count;
2905 jobject rcv = sp[0].o;
2910 _Jv_LookupInterfaceMethod (rcv->getClass (),
2911 rmeth->method->name,
2912 rmeth->method->signature);
2914 #ifdef DIRECT_THREADED
2915 // Rewrite instruction so that we use a faster pre-resolved
2917 pc[-2].insn = &&invokeinterface_resolved;
2918 pc[-1].datum = rmeth;
2920 // Skip dummy bytes.
2922 #endif /* DIRECT_THREADED */
2924 goto perform_invoke;
2926 #ifdef DIRECT_THREADED
2927 invokeinterface_resolved:
2929 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2930 sp -= rmeth->stack_item_count;
2931 jobject rcv = sp[0].o;
2934 _Jv_LookupInterfaceMethod (rcv->getClass (),
2935 rmeth->method->name,
2936 rmeth->method->signature);
2938 goto perform_invoke;
2939 #endif /* DIRECT_THREADED */
2943 int index = GET2U ();
2944 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2946 /* VM spec, section 3.11.5 */
2947 if ((klass->getModifiers() & Modifier::ABSTRACT)
2948 || klass->isInterface())
2949 throw new java::lang::InstantiationException;
2950 jobject res = _Jv_AllocObject (klass);
2953 #ifdef DIRECT_THREADED
2954 pc[-2].insn = &&new_resolved;
2955 pc[-1].datum = klass;
2956 #endif /* DIRECT_THREADED */
2960 #ifdef DIRECT_THREADED
2963 jclass klass = (jclass) AVAL ();
2964 jobject res = _Jv_AllocObject (klass);
2968 #endif /* DIRECT_THREADED */
2972 int atype = GET1U ();
2974 jobject result = _Jv_NewArray (atype, size);
2981 int index = GET2U ();
2982 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2985 jobject result = _Jv_NewObjectArray (size, klass, 0);
2988 #ifdef DIRECT_THREADED
2989 pc[-2].insn = &&anewarray_resolved;
2990 pc[-1].datum = klass;
2991 #endif /* DIRECT_THREADED */
2995 #ifdef DIRECT_THREADED
2998 jclass klass = (jclass) AVAL ();
3000 jobject result = _Jv_NewObjectArray (size, klass, 0);
3004 #endif /* DIRECT_THREADED */
3008 __JArray *arr = (__JArray*)POPA();
3009 NULLARRAYCHECK (arr);
3010 PUSHI (arr->length);
3016 jobject value = POPA();
3017 throw static_cast<jthrowable>(value);
3024 jobject value = POPA();
3025 jint index = GET2U ();
3026 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3029 value = (jobject) _Jv_CheckCast (to, value);
3033 #ifdef DIRECT_THREADED
3034 pc[-2].insn = &&checkcast_resolved;
3036 #endif /* DIRECT_THREADED */
3040 #ifdef DIRECT_THREADED
3044 jobject value = POPA ();
3045 jclass to = (jclass) AVAL ();
3046 value = (jobject) _Jv_CheckCast (to, value);
3050 #endif /* DIRECT_THREADED */
3055 jobject value = POPA();
3056 jint index = GET2U ();
3057 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3059 PUSHI (to->isInstance (value));
3061 #ifdef DIRECT_THREADED
3062 pc[-2].insn = &&instanceof_resolved;
3064 #endif /* DIRECT_THREADED */
3068 #ifdef DIRECT_THREADED
3069 instanceof_resolved:
3071 jobject value = POPA ();
3072 jclass to = (jclass) AVAL ();
3073 PUSHI (to->isInstance (value));
3076 #endif /* DIRECT_THREADED */
3080 jobject value = POPA();
3082 _Jv_MonitorEnter (value);
3088 jobject value = POPA();
3090 _Jv_MonitorExit (value);
3096 jobject val = POPA();
3106 jobject val = POPA();
3114 insn_multianewarray:
3116 int kind_index = GET2U ();
3120 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3122 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3124 for (int i = dim - 1; i >= 0; i--)
3129 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3135 #ifndef DIRECT_THREADED
3138 jint the_mod_op = get1u (pc++);
3139 jint wide = get2u (pc); pc += 2;
3184 pc = (unsigned char*) PEEKA (wide);
3189 jint amount = get2s (pc); pc += 2;
3190 jint value = PEEKI (wide);
3191 POKEI (wide, value+amount);
3196 throw_internal_error ("illegal bytecode modified by wide");
3200 #endif /* DIRECT_THREADED */
3202 catch (java::lang::Throwable *ex)
3204 #ifdef DIRECT_THREADED
3205 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3207 int logical_pc = pc - 1 - bytecode ();
3209 _Jv_InterpException *exc = meth->exceptions ();
3210 jclass exc_class = ex->getClass ();
3212 for (int i = 0; i < meth->exc_count; i++)
3214 if (PCVAL (exc[i].start_pc) <= logical_pc
3215 && logical_pc < PCVAL (exc[i].end_pc))
3217 #ifdef DIRECT_THREADED
3218 jclass handler = (jclass) exc[i].handler_type.p;
3220 jclass handler = NULL;
3221 if (exc[i].handler_type.i != 0)
3222 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3223 exc[i].handler_type.i)).clazz;
3224 #endif /* DIRECT_THREADED */
3226 if (handler == NULL || handler->isAssignableFrom (exc_class))
3228 #ifdef DIRECT_THREADED
3229 pc = (insn_slot *) exc[i].handler_pc.p;
3231 pc = bytecode () + exc[i].handler_pc.i;
3232 #endif /* DIRECT_THREADED */
3234 sp++->o = ex; // Push exception.
3240 // No handler, so re-throw.
3246 throw_internal_error (char *msg)
3248 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3252 throw_incompatible_class_change_error (jstring msg)
3254 throw new java::lang::IncompatibleClassChangeError (msg);
3258 static java::lang::NullPointerException *null_pointer_exc;
3260 throw_null_pointer_exception ()
3262 if (null_pointer_exc == NULL)
3263 null_pointer_exc = new java::lang::NullPointerException;
3265 throw null_pointer_exc;
3269 /* Look up source code line number for given bytecode (or direct threaded
3272 _Jv_InterpMethod::get_source_line(pc_t mpc)
3274 int line = line_table_len > 0 ? line_table[0].line : -1;
3275 for (int i = 1; i < line_table_len; i++)
3276 if (line_table[i].pc > mpc)
3279 line = line_table[i].line;
3284 /** Do static initialization for fields with a constant initializer */
3286 _Jv_InitField (jobject obj, jclass klass, int index)
3288 using namespace java::lang::reflect;
3290 if (obj != 0 && klass == 0)
3291 klass = obj->getClass ();
3293 if (!_Jv_IsInterpretedClass (klass))
3296 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3298 _Jv_Field * field = (&klass->fields[0]) + index;
3300 if (index > klass->field_count)
3301 throw_internal_error ("field out of range");
3303 int init = iclass->field_initializers[index];
3307 _Jv_Constants *pool = &klass->constants;
3308 int tag = pool->tags[init];
3310 if (! field->isResolved ())
3311 throw_internal_error ("initializing unresolved field");
3313 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3314 throw_internal_error ("initializing non-static field with no object");
3318 if ((field->flags & Modifier::STATIC) != 0)
3319 addr = (void*) field->u.addr;
3321 addr = (void*) (((char*)obj) + field->u.boffset);
3325 case JV_CONSTANT_String:
3328 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3329 pool->data[init].string = str;
3330 pool->tags[init] = JV_CONSTANT_ResolvedString;
3334 case JV_CONSTANT_ResolvedString:
3335 if (! (field->type == &java::lang::String::class$
3336 || field->type == &java::lang::Class::class$))
3337 throw_class_format_error ("string initialiser to non-string field");
3339 *(jstring*)addr = pool->data[init].string;
3342 case JV_CONSTANT_Integer:
3344 int value = pool->data[init].i;
3346 if (field->type == JvPrimClass (boolean))
3347 *(jboolean*)addr = (jboolean)value;
3349 else if (field->type == JvPrimClass (byte))
3350 *(jbyte*)addr = (jbyte)value;
3352 else if (field->type == JvPrimClass (char))
3353 *(jchar*)addr = (jchar)value;
3355 else if (field->type == JvPrimClass (short))
3356 *(jshort*)addr = (jshort)value;
3358 else if (field->type == JvPrimClass (int))
3359 *(jint*)addr = (jint)value;
3362 throw_class_format_error ("erroneous field initializer");
3366 case JV_CONSTANT_Long:
3367 if (field->type != JvPrimClass (long))
3368 throw_class_format_error ("erroneous field initializer");
3370 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3373 case JV_CONSTANT_Float:
3374 if (field->type != JvPrimClass (float))
3375 throw_class_format_error ("erroneous field initializer");
3377 *(jfloat*)addr = pool->data[init].f;
3380 case JV_CONSTANT_Double:
3381 if (field->type != JvPrimClass (double))
3382 throw_class_format_error ("erroneous field initializer");
3384 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3388 throw_class_format_error ("erroneous field initializer");
3392 inline static unsigned char*
3393 skip_one_type (unsigned char* ptr)
3404 do { ch = *ptr++; } while (ch != ';');
3411 get_ffi_type_from_signature (unsigned char* ptr)
3417 return &ffi_type_pointer;
3421 // On some platforms a bool is a byte, on others an int.
3422 if (sizeof (jboolean) == sizeof (jbyte))
3423 return &ffi_type_sint8;
3426 JvAssert (sizeof (jbyte) == sizeof (jint));
3427 return &ffi_type_sint32;
3432 return &ffi_type_sint8;
3436 return &ffi_type_uint16;
3440 return &ffi_type_sint16;
3444 return &ffi_type_sint32;
3448 return &ffi_type_sint64;
3452 return &ffi_type_float;
3456 return &ffi_type_double;
3460 return &ffi_type_void;
3464 throw_internal_error ("unknown type in signature");
3467 /* this function yields the number of actual arguments, that is, if the
3468 * function is non-static, then one is added to the number of elements
3469 * found in the signature */
3472 _Jv_count_arguments (_Jv_Utf8Const *signature,
3475 unsigned char *ptr = (unsigned char*) signature->chars();
3476 int arg_count = staticp ? 0 : 1;
3478 /* first, count number of arguments */
3486 ptr = skip_one_type (ptr);
3493 /* This beast will build a cif, given the signature. Memory for
3494 * the cif itself and for the argument types must be allocated by the
3499 init_cif (_Jv_Utf8Const* signature,
3503 ffi_type **arg_types,
3506 unsigned char *ptr = (unsigned char*) signature->chars();
3508 int arg_index = 0; // arg number
3509 int item_count = 0; // stack-item count
3514 arg_types[arg_index++] = &ffi_type_pointer;
3524 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3526 if (*ptr == 'J' || *ptr == 'D')
3531 ptr = skip_one_type (ptr);
3536 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3538 ptr = skip_one_type (ptr);
3539 if (ptr != (unsigned char*)signature->chars() + signature->len())
3540 throw_internal_error ("did not find end of signature");
3542 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3543 arg_count, rtype, arg_types) != FFI_OK)
3544 throw_internal_error ("ffi_prep_cif failed");
3546 if (rtype_p != NULL)
3552 #if FFI_NATIVE_RAW_API
3553 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3554 # define FFI_RAW_SIZE ffi_raw_size
3556 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3557 # define FFI_RAW_SIZE ffi_java_raw_size
3560 /* we put this one here, and not in interpret.cc because it
3561 * calls the utility routines _Jv_count_arguments
3562 * which are static to this module. The following struct defines the
3563 * layout we use for the stubs, it's only used in the ncode method. */
3566 ffi_raw_closure closure;
3568 ffi_type *arg_types[0];
3571 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3574 _Jv_InterpMethod::ncode ()
3576 using namespace java::lang::reflect;
3578 if (self->ncode != 0)
3581 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3582 int arg_count = _Jv_count_arguments (self->signature, staticp);
3584 ncode_closure *closure =
3585 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3586 + arg_count * sizeof (ffi_type*));
3588 init_cif (self->signature,
3592 &closure->arg_types[0],
3595 ffi_closure_fun fun;
3597 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3599 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3601 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3604 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3606 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3611 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3613 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3616 FFI_PREP_RAW_CLOSURE (&closure->closure,
3621 self->ncode = (void*)closure;
3626 _Jv_JNIMethod::ncode ()
3628 using namespace java::lang::reflect;
3630 if (self->ncode != 0)
3633 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3634 int arg_count = _Jv_count_arguments (self->signature, staticp);
3636 ncode_closure *closure =
3637 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3638 + arg_count * sizeof (ffi_type*));
3641 init_cif (self->signature,
3645 &closure->arg_types[0],
3648 ffi_closure_fun fun;
3650 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3652 // Initialize the argument types and CIF that represent the actual
3653 // underlying JNI function.
3655 if ((self->accflags & Modifier::STATIC))
3657 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3658 * sizeof (ffi_type *));
3660 jni_arg_types[offset++] = &ffi_type_pointer;
3661 if ((self->accflags & Modifier::STATIC))
3662 jni_arg_types[offset++] = &ffi_type_pointer;
3663 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3664 arg_count * sizeof (ffi_type *));
3666 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3667 extra_args + arg_count, rtype,
3668 jni_arg_types) != FFI_OK)
3669 throw_internal_error ("ffi_prep_cif failed for JNI function");
3671 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3673 // FIXME: for now we assume that all native methods for
3674 // interpreted code use JNI.
3675 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3677 FFI_PREP_RAW_CLOSURE (&closure->closure,
3682 self->ncode = (void *) closure;
3687 throw_class_format_error (jstring msg)
3690 ? new java::lang::ClassFormatError (msg)
3691 : new java::lang::ClassFormatError);
3695 throw_class_format_error (char *msg)
3697 throw_class_format_error (JvNewStringLatin1 (msg));
3703 _Jv_InterpreterEngine::do_verify (jclass klass)
3705 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3706 for (int i = 0; i < klass->method_count; i++)
3708 using namespace java::lang::reflect;
3709 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3710 _Jv_ushort accflags = klass->methods[i].accflags;
3711 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3713 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3714 _Jv_VerifyMethod (im);
3720 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3722 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3723 for (int i = 0; i < klass->method_count; i++)
3725 // Just skip abstract methods. This is particularly important
3726 // because we don't resize the interpreted_methods array when
3727 // miranda methods are added to it.
3728 if ((klass->methods[i].accflags
3729 & java::lang::reflect::Modifier::ABSTRACT)
3733 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3735 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3738 // You might think we could use a virtual `ncode' method in
3739 // the _Jv_MethodBase and unify the native and non-native
3740 // cases. Well, we can't, because we don't allocate these
3741 // objects using `new', and thus they don't get a vtable.
3742 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3743 klass->methods[i].ncode = jnim->ncode ();
3745 else if (imeth != 0) // it could be abstract
3747 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3748 klass->methods[i].ncode = im->ncode ();
3754 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3757 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3759 char *static_data = (char *) _Jv_AllocBytes (static_size);
3761 for (int i = 0; i < klass->field_count; i++)
3763 _Jv_Field *field = &klass->fields[i];
3765 if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
3767 field->u.addr = static_data + field->u.boffset;
3769 if (iclass->field_initializers[i] != 0)
3771 _Jv_Linker::resolve_field (field, klass->loader);
3772 _Jv_InitField (0, klass, i);
3777 // Now we don't need the field_initializers anymore, so let the
3778 // collector get rid of it.
3779 iclass->field_initializers = 0;
3782 _Jv_ResolvedMethod *
3783 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
3784 jboolean staticp, jint vtable_index)
3786 int arg_count = _Jv_count_arguments (method->signature, staticp);
3788 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
3789 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
3790 + arg_count*sizeof (ffi_type*));
3792 result->stack_item_count
3793 = init_cif (method->signature,
3797 &result->arg_types[0],
3800 result->vtable_index = vtable_index;
3801 result->method = method;
3802 result->klass = klass;
3808 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
3810 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3811 for (int i = 0; i < klass->method_count; i++)
3813 // Just skip abstract methods. This is particularly important
3814 // because we don't resize the interpreted_methods array when
3815 // miranda methods are added to it.
3816 if ((klass->methods[i].accflags
3817 & java::lang::reflect::Modifier::ABSTRACT)
3820 // Miranda method additions mean that the `methods' array moves.
3821 // We cache a pointer into this array, so we have to update.
3822 iclass->interpreted_methods[i]->self = &klass->methods[i];
3826 #endif // INTERPRETER