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/Thread.h>
34 #include <java-insns.h>
35 #include <java-signal.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <execution.h>
38 #include <java/lang/reflect/Modifier.h>
42 // Execution engine for interpreted code.
43 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
49 static void throw_internal_error (char *msg)
50 __attribute__ ((__noreturn__));
51 static void throw_incompatible_class_change_error (jstring msg)
52 __attribute__ ((__noreturn__));
54 static void throw_null_pointer_exception ()
55 __attribute__ ((__noreturn__));
58 static void throw_class_format_error (jstring msg)
59 __attribute__ ((__noreturn__));
60 static void throw_class_format_error (char *msg)
61 __attribute__ ((__noreturn__));
63 #ifdef DIRECT_THREADED
64 // Lock to ensure that methods are not compiled concurrently.
65 // We could use a finer-grained lock here, however it is not safe to use
66 // the Class monitor as user code in another thread could hold it.
67 static _Jv_Mutex_t compile_mutex;
72 _Jv_MutexInit (&compile_mutex);
75 void _Jv_InitInterpreter() {}
78 extern "C" double __ieee754_fmod (double,double);
80 static inline void dupx (_Jv_word *sp, int n, int x)
82 // first "slide" n+x elements n to the right
84 for (int i = 0; i < n+x; i++)
86 sp[(top-i)] = sp[(top-i)-n];
89 // next, copy the n top elements, n+x down
90 for (int i = 0; i < n; i++)
92 sp[top-(n+x)-i] = sp[top-i];
96 // Used to convert from floating types to integral types.
97 template<typename TO, typename FROM>
99 convert (FROM val, TO min, TO max)
102 if (val >= (FROM) max)
104 else if (val <= (FROM) min)
113 #define PUSHA(V) (sp++)->o = (V)
114 #define PUSHI(V) (sp++)->i = (V)
115 #define PUSHF(V) (sp++)->f = (V)
116 #if SIZEOF_VOID_P == 8
117 # define PUSHL(V) (sp->l = (V), sp += 2)
118 # define PUSHD(V) (sp->d = (V), sp += 2)
120 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
121 (sp++)->ia[0] = w2.ia[0]; \
122 (sp++)->ia[0] = w2.ia[1]; } while (0)
123 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
124 (sp++)->ia[0] = w2.ia[0]; \
125 (sp++)->ia[0] = w2.ia[1]; } while (0)
128 #define POPA() ((--sp)->o)
129 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
130 #define POPF() ((jfloat) (--sp)->f)
131 #if SIZEOF_VOID_P == 8
132 # define POPL() (sp -= 2, (jlong) sp->l)
133 # define POPD() (sp -= 2, (jdouble) sp->d)
135 # define POPL() ({ _Jv_word2 w2; \
136 w2.ia[1] = (--sp)->ia[0]; \
137 w2.ia[0] = (--sp)->ia[0]; w2.l; })
138 # define POPD() ({ _Jv_word2 w2; \
139 w2.ia[1] = (--sp)->ia[0]; \
140 w2.ia[0] = (--sp)->ia[0]; w2.d; })
143 #define LOADA(I) (sp++)->o = locals[I].o
144 #define LOADI(I) (sp++)->i = locals[I].i
145 #define LOADF(I) (sp++)->f = locals[I].f
146 #if SIZEOF_VOID_P == 8
147 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
148 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
150 # define LOADL(I) do { jint __idx = (I); \
151 (sp++)->ia[0] = locals[__idx].ia[0]; \
152 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
154 # define LOADD(I) LOADL(I)
157 #define STOREA(I) locals[I].o = (--sp)->o
158 #define STOREI(I) locals[I].i = (--sp)->i
159 #define STOREF(I) locals[I].f = (--sp)->f
160 #if SIZEOF_VOID_P == 8
161 # define STOREL(I) (sp -= 2, locals[I].l = sp->l)
162 # define STORED(I) (sp -= 2, locals[I].d = sp->d)
164 # define STOREL(I) do { jint __idx = (I); \
165 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
166 locals[__idx].ia[0] = (--sp)->ia[0]; \
168 # define STORED(I) STOREL(I)
171 #define PEEKI(I) (locals+(I))->i
172 #define PEEKA(I) (locals+(I))->o
174 #define POKEI(I,V) ((locals+(I))->i = (V))
177 #define BINOPI(OP) { \
178 jint value2 = POPI(); \
179 jint value1 = POPI(); \
180 PUSHI(value1 OP value2); \
183 #define BINOPF(OP) { \
184 jfloat value2 = POPF(); \
185 jfloat value1 = POPF(); \
186 PUSHF(value1 OP value2); \
189 #define BINOPL(OP) { \
190 jlong value2 = POPL(); \
191 jlong value1 = POPL(); \
192 PUSHL(value1 OP value2); \
195 #define BINOPD(OP) { \
196 jdouble value2 = POPD(); \
197 jdouble value1 = POPD(); \
198 PUSHD(value1 OP value2); \
201 static inline jint get1s(unsigned char* loc) {
202 return *(signed char*)loc;
205 static inline jint get1u(unsigned char* loc) {
209 static inline jint get2s(unsigned char* loc) {
210 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
213 static inline jint get2u(unsigned char* loc) {
214 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
217 static jint get4(unsigned char* loc) {
218 return (((jint)(loc[0])) << 24)
219 | (((jint)(loc[1])) << 16)
220 | (((jint)(loc[2])) << 8)
221 | (((jint)(loc[3])) << 0);
224 #define SAVE_PC() frame_desc.pc = pc
227 #define NULLCHECK(X) SAVE_PC()
228 #define NULLARRAYCHECK(X) SAVE_PC()
230 #define NULLCHECK(X) \
231 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
232 #define NULLARRAYCHECK(X) \
233 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
236 #define ARRAYBOUNDSCHECK(array, index) \
239 if (((unsigned) index) >= (unsigned) (array->length)) \
240 _Jv_ThrowBadArrayIndex (index); \
245 _Jv_InterpMethod::run_normal (ffi_cif *,
250 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
251 run (ret, args, _this);
255 _Jv_InterpMethod::run_synch_object (ffi_cif *,
260 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
262 jobject rcv = (jobject) args[0].ptr;
263 JvSynchronize mutex (rcv);
265 run (ret, args, _this);
269 _Jv_InterpMethod::run_class (ffi_cif *,
274 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
275 _Jv_InitClass (_this->defining_class);
276 run (ret, args, _this);
280 _Jv_InterpMethod::run_synch_class (ffi_cif *,
285 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
287 jclass sync = _this->defining_class;
288 _Jv_InitClass (sync);
289 JvSynchronize mutex (sync);
291 run (ret, args, _this);
294 #ifdef DIRECT_THREADED
295 // "Compile" a method by turning it from bytecode to direct-threaded
298 _Jv_InterpMethod::compile (const void * const *insn_targets)
300 insn_slot *insns = NULL;
302 unsigned char *codestart = bytecode ();
303 unsigned char *end = codestart + code_length;
304 _Jv_word *pool_data = defining_class->constants.data;
306 #define SET_ONE(Field, Value) \
312 insns[next++].Field = Value; \
316 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
317 #define SET_INT(Value) SET_ONE (int_val, Value)
318 #define SET_DATUM(Value) SET_ONE (datum, Value)
320 // Map from bytecode PC to slot in INSNS.
321 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
322 for (int i = 0; i < code_length; ++i)
325 for (int i = 0; i < 2; ++i)
327 jboolean first_pass = i == 0;
331 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
335 unsigned char *pc = codestart;
338 int base_pc_val = pc - codestart;
340 pc_mapping[base_pc_val] = next;
342 java_opcode opcode = (java_opcode) *pc++;
344 if (opcode == op_nop)
346 SET_INSN (insn_targets[opcode]);
487 case op_monitorenter:
497 // No argument, nothing else to do.
501 SET_INT (get1s (pc));
507 int index = get1u (pc);
509 SET_DATUM (pool_data[index].o);
525 SET_INT (get1u (pc));
530 SET_INT (get1u (pc));
531 SET_INT (get1s (pc + 1));
537 int index = get2u (pc);
539 SET_DATUM (pool_data[index].o);
545 int index = get2u (pc);
547 SET_DATUM (&pool_data[index]);
552 SET_INT (get2s (pc));
564 case op_invokespecial:
565 case op_invokestatic:
566 case op_invokevirtual:
567 SET_INT (get2u (pc));
571 case op_multianewarray:
572 SET_INT (get2u (pc));
573 SET_INT (get1u (pc + 2));
596 int offset = get2s (pc);
599 int new_pc = base_pc_val + offset;
601 bool orig_was_goto = opcode == op_goto;
603 // Thread jumps. We limit the loop count; this lets
604 // us avoid infinite loops if the bytecode contains
605 // such. `10' is arbitrary.
607 while (codestart[new_pc] == op_goto && count-- > 0)
608 new_pc += get2s (&codestart[new_pc + 1]);
610 // If the jump takes us to a `return' instruction and
611 // the original branch was an unconditional goto, then
612 // we hoist the return.
613 opcode = (java_opcode) codestart[new_pc];
615 && (opcode == op_ireturn || opcode == op_lreturn
616 || opcode == op_freturn || opcode == op_dreturn
617 || opcode == op_areturn || opcode == op_return))
620 SET_INSN (insn_targets[opcode]);
623 SET_DATUM (&insns[pc_mapping[new_pc]]);
629 while ((pc - codestart) % 4 != 0)
632 jint def = get4 (pc);
633 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
639 int high = get4 (pc);
643 for (int i = low; i <= high; ++i)
645 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
651 case op_lookupswitch:
653 while ((pc - codestart) % 4 != 0)
656 jint def = get4 (pc);
657 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
660 jint npairs = get4 (pc);
666 jint match = get4 (pc);
667 jint offset = get4 (pc + 4);
669 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
675 case op_invokeinterface:
677 jint index = get2u (pc);
679 // We ignore the next two bytes.
687 opcode = (java_opcode) get1u (pc);
689 jint val = get2u (pc);
692 // We implement narrow and wide instructions using the
693 // same code in the interpreter. So we rewrite the
694 // instruction slot here.
696 insns[next - 1].insn = (void *) insn_targets[opcode];
699 if (opcode == op_iinc)
701 SET_INT (get2s (pc));
710 jint offset = get4 (pc);
712 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
716 // Some "can't happen" cases that we include for
717 // error-checking purposes.
735 case op_getstatic_2s:
736 case op_getstatic_2u:
747 // Now update exceptions.
748 _Jv_InterpException *exc = exceptions ();
749 for (int i = 0; i < exc_count; ++i)
751 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
752 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
753 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
755 = (_Jv_Linker::resolve_pool_entry (defining_class,
756 exc[i].handler_type.i)).clazz;
757 exc[i].handler_type.p = handler;
760 // Translate entries in the LineNumberTable from bytecode PC's to direct
761 // threaded interpreter instruction values.
762 for (int i = 0; i < line_table_len; i++)
764 int byte_pc = line_table[i].bytecode_pc;
765 // It isn't worth throwing an exception if this table is
766 // corrupted, but at the same time we don't want a crash.
767 if (byte_pc < 0 || byte_pc >= code_length)
769 line_table[i].pc = &insns[pc_mapping[byte_pc]];
774 #endif /* DIRECT_THREADED */
777 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
779 using namespace java::lang::reflect;
781 // FRAME_DESC registers this particular invocation as the top-most
782 // interpreter frame. This lets the stack tracing code (for
783 // Throwable) print information about the method being interpreted
784 // rather than about the interpreter itself. FRAME_DESC has a
785 // destructor so it cleans up automatically when the interpreter
787 java::lang::Thread *thread = java::lang::Thread::currentThread();
788 _Jv_InterpFrame frame_desc (meth,
789 (_Jv_InterpFrame **) &thread->interp_frame);
791 _Jv_word stack[meth->max_stack];
792 _Jv_word *sp = stack;
794 _Jv_word locals[meth->max_locals];
796 /* Go straight at it! the ffi raw format matches the internal
797 stack representation exactly. At least, that's the idea.
799 memcpy ((void*) locals, (void*) args, meth->args_raw_size);
801 _Jv_word *pool_data = meth->defining_class->constants.data;
803 /* These three are temporaries for common code used by several
806 _Jv_ResolvedMethod* rmeth;
809 #define INSN_LABEL(op) &&insn_##op
811 static const void *const insn_target[] =
814 INSN_LABEL(aconst_null),
815 INSN_LABEL(iconst_m1),
816 INSN_LABEL(iconst_0),
817 INSN_LABEL(iconst_1),
818 INSN_LABEL(iconst_2),
819 INSN_LABEL(iconst_3),
820 INSN_LABEL(iconst_4),
821 INSN_LABEL(iconst_5),
822 INSN_LABEL(lconst_0),
823 INSN_LABEL(lconst_1),
824 INSN_LABEL(fconst_0),
825 INSN_LABEL(fconst_1),
826 INSN_LABEL(fconst_2),
827 INSN_LABEL(dconst_0),
828 INSN_LABEL(dconst_1),
872 INSN_LABEL(istore_0),
873 INSN_LABEL(istore_1),
874 INSN_LABEL(istore_2),
875 INSN_LABEL(istore_3),
876 INSN_LABEL(lstore_0),
877 INSN_LABEL(lstore_1),
878 INSN_LABEL(lstore_2),
879 INSN_LABEL(lstore_3),
880 INSN_LABEL(fstore_0),
881 INSN_LABEL(fstore_1),
882 INSN_LABEL(fstore_2),
883 INSN_LABEL(fstore_3),
884 INSN_LABEL(dstore_0),
885 INSN_LABEL(dstore_1),
886 INSN_LABEL(dstore_2),
887 INSN_LABEL(dstore_3),
888 INSN_LABEL(astore_0),
889 INSN_LABEL(astore_1),
890 INSN_LABEL(astore_2),
891 INSN_LABEL(astore_3),
972 INSN_LABEL(if_icmpeq),
973 INSN_LABEL(if_icmpne),
974 INSN_LABEL(if_icmplt),
975 INSN_LABEL(if_icmpge),
976 INSN_LABEL(if_icmpgt),
977 INSN_LABEL(if_icmple),
978 INSN_LABEL(if_acmpeq),
979 INSN_LABEL(if_acmpne),
983 INSN_LABEL(tableswitch),
984 INSN_LABEL(lookupswitch),
991 INSN_LABEL(getstatic),
992 INSN_LABEL(putstatic),
993 INSN_LABEL(getfield),
994 INSN_LABEL(putfield),
995 INSN_LABEL(invokevirtual),
996 INSN_LABEL(invokespecial),
997 INSN_LABEL(invokestatic),
998 INSN_LABEL(invokeinterface),
1001 INSN_LABEL(newarray),
1002 INSN_LABEL(anewarray),
1003 INSN_LABEL(arraylength),
1005 INSN_LABEL(checkcast),
1006 INSN_LABEL(instanceof),
1007 INSN_LABEL(monitorenter),
1008 INSN_LABEL(monitorexit),
1009 #ifdef DIRECT_THREADED
1014 INSN_LABEL(multianewarray),
1016 INSN_LABEL(ifnonnull),
1024 #ifdef DIRECT_THREADED
1026 #define NEXT_INSN goto *((pc++)->insn)
1027 #define INTVAL() ((pc++)->int_val)
1028 #define AVAL() ((pc++)->datum)
1030 #define GET1S() INTVAL ()
1031 #define GET2S() INTVAL ()
1032 #define GET1U() INTVAL ()
1033 #define GET2U() INTVAL ()
1034 #define AVAL1U() AVAL ()
1035 #define AVAL2U() AVAL ()
1036 #define AVAL2UP() AVAL ()
1037 #define SKIP_GOTO ++pc
1038 #define GOTO_VAL() (insn_slot *) pc->datum
1039 #define PCVAL(unionval) unionval.p
1040 #define AMPAMP(label) &&label
1042 // Compile if we must. NOTE: Double-check locking.
1043 if (meth->prepared == NULL)
1045 _Jv_MutexLock (&compile_mutex);
1046 if (meth->prepared == NULL)
1047 meth->compile (insn_target);
1048 _Jv_MutexUnlock (&compile_mutex);
1050 pc = (insn_slot *) meth->prepared;
1054 #define NEXT_INSN goto *(insn_target[*pc++])
1056 #define GET1S() get1s (pc++)
1057 #define GET2S() (pc += 2, get2s (pc- 2))
1058 #define GET1U() get1u (pc++)
1059 #define GET2U() (pc += 2, get2u (pc - 2))
1060 #define AVAL1U() ({ int index = get1u (pc++); pool_data[index].o; })
1061 #define AVAL2U() ({ int index = get2u (pc); pc += 2; pool_data[index].o; })
1062 #define AVAL2UP() ({ int index = get2u (pc); pc += 2; &pool_data[index]; })
1063 #define SKIP_GOTO pc += 2
1064 #define GOTO_VAL() pc - 1 + get2s (pc)
1065 #define PCVAL(unionval) unionval.i
1066 #define AMPAMP(label) NULL
1070 #endif /* DIRECT_THREADED */
1072 #define TAKE_GOTO pc = GOTO_VAL ()
1076 // We keep nop around. It is used if we're interpreting the
1077 // bytecodes and not doing direct threading.
1081 /* The first few instructions here are ordered according to their
1082 frequency, in the hope that this will improve code locality a
1085 insn_aload_0: // 0x2a
1093 insn_iload_1: // 0x1b
1097 insn_invokevirtual: // 0xb6
1099 int index = GET2U ();
1101 /* _Jv_Linker::resolve_pool_entry returns immediately if the
1102 * value already is resolved. If we want to clutter up the
1103 * code here to gain a little performance, then we can check
1104 * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
1105 * directly. For now, I don't think it is worth it. */
1108 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
1111 sp -= rmeth->stack_item_count;
1112 // We don't use NULLCHECK here because we can't rely on that
1113 // working if the method is final. So instead we do an
1117 //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
1118 throw new java::lang::NullPointerException;
1121 if (rmeth->vtable_index == -1)
1123 // final methods do not appear in the vtable,
1124 // if it does not appear in the superclass.
1125 fun = (void (*)()) rmeth->method->ncode;
1129 jobject rcv = sp[0].o;
1130 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1131 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1134 #ifdef DIRECT_THREADED
1135 // Rewrite instruction so that we use a faster pre-resolved
1137 pc[-2].insn = &&invokevirtual_resolved;
1138 pc[-1].datum = rmeth;
1139 #endif /* DIRECT_THREADED */
1141 goto perform_invoke;
1143 #ifdef DIRECT_THREADED
1144 invokevirtual_resolved:
1146 rmeth = (_Jv_ResolvedMethod *) AVAL ();
1147 sp -= rmeth->stack_item_count;
1148 // We don't use NULLCHECK here because we can't rely on that
1149 // working if the method is final. So instead we do an
1154 throw new java::lang::NullPointerException;
1157 if (rmeth->vtable_index == -1)
1159 // final methods do not appear in the vtable,
1160 // if it does not appear in the superclass.
1161 fun = (void (*)()) rmeth->method->ncode;
1165 jobject rcv = sp[0].o;
1166 _Jv_VTable *table = *(_Jv_VTable**) rcv;
1167 fun = (void (*)()) table->get_method (rmeth->vtable_index);
1170 goto perform_invoke;
1171 #endif /* DIRECT_THREADED */
1177 /* here goes the magic again... */
1178 ffi_cif *cif = &rmeth->cif;
1179 ffi_raw *raw = (ffi_raw*) sp;
1183 #if FFI_NATIVE_RAW_API
1184 /* We assume that this is only implemented if it's correct */
1185 /* to use it here. On a 64 bit machine, it never is. */
1186 ffi_raw_call (cif, fun, (void*)&rvalue, raw);
1188 ffi_java_raw_call (cif, fun, (void*)&rvalue, raw);
1191 int rtype = cif->rtype->type;
1193 /* the likelyhood of object, int, or void return is very high,
1194 * so those are checked before the switch */
1195 if (rtype == FFI_TYPE_POINTER)
1197 PUSHA (rvalue.object_value);
1199 else if (rtype == FFI_TYPE_SINT32)
1201 PUSHI (rvalue.int_value);
1203 else if (rtype == FFI_TYPE_VOID)
1211 case FFI_TYPE_SINT8:
1212 PUSHI ((jbyte)(rvalue.int_value & 0xff));
1215 case FFI_TYPE_SINT16:
1216 PUSHI ((jshort)(rvalue.int_value & 0xffff));
1219 case FFI_TYPE_UINT16:
1220 PUSHI (rvalue.int_value & 0xffff);
1223 case FFI_TYPE_FLOAT:
1224 PUSHF (rvalue.float_value);
1227 case FFI_TYPE_DOUBLE:
1228 PUSHD (rvalue.double_value);
1231 case FFI_TYPE_SINT64:
1232 PUSHL (rvalue.long_value);
1236 throw_internal_error ("unknown return type in invokeXXX");
1303 // For direct threaded, bipush and sipush are the same.
1304 #ifndef DIRECT_THREADED
1307 #endif /* DIRECT_THREADED */
1313 // For direct threaded, ldc and ldc_w are the same.
1314 #ifndef DIRECT_THREADED
1315 PUSHA ((jobject) AVAL1U ());
1317 #endif /* DIRECT_THREADED */
1319 PUSHA ((jobject) AVAL2U ());
1324 void *where = AVAL2UP ();
1325 memcpy (sp, where, 2*sizeof (_Jv_word));
1420 jint index = POPI();
1421 jintArray arr = (jintArray) POPA();
1422 NULLARRAYCHECK (arr);
1423 ARRAYBOUNDSCHECK (arr, index);
1424 PUSHI( elements(arr)[index] );
1430 jint index = POPI();
1431 jlongArray arr = (jlongArray) POPA();
1432 NULLARRAYCHECK (arr);
1433 ARRAYBOUNDSCHECK (arr, index);
1434 PUSHL( elements(arr)[index] );
1440 jint index = POPI();
1441 jfloatArray arr = (jfloatArray) POPA();
1442 NULLARRAYCHECK (arr);
1443 ARRAYBOUNDSCHECK (arr, index);
1444 PUSHF( elements(arr)[index] );
1450 jint index = POPI();
1451 jdoubleArray arr = (jdoubleArray) POPA();
1452 NULLARRAYCHECK (arr);
1453 ARRAYBOUNDSCHECK (arr, index);
1454 PUSHD( elements(arr)[index] );
1460 jint index = POPI();
1461 jobjectArray arr = (jobjectArray) POPA();
1462 NULLARRAYCHECK (arr);
1463 ARRAYBOUNDSCHECK (arr, index);
1464 PUSHA( elements(arr)[index] );
1470 jint index = POPI();
1471 jbyteArray arr = (jbyteArray) POPA();
1472 NULLARRAYCHECK (arr);
1473 ARRAYBOUNDSCHECK (arr, index);
1474 PUSHI( elements(arr)[index] );
1480 jint index = POPI();
1481 jcharArray arr = (jcharArray) POPA();
1482 NULLARRAYCHECK (arr);
1483 ARRAYBOUNDSCHECK (arr, index);
1484 PUSHI( elements(arr)[index] );
1490 jint index = POPI();
1491 jshortArray arr = (jshortArray) POPA();
1492 NULLARRAYCHECK (arr);
1493 ARRAYBOUNDSCHECK (arr, index);
1494 PUSHI( elements(arr)[index] );
1600 jint value = POPI();
1601 jint index = POPI();
1602 jintArray arr = (jintArray) POPA();
1603 NULLARRAYCHECK (arr);
1604 ARRAYBOUNDSCHECK (arr, index);
1605 elements(arr)[index] = value;
1611 jlong value = POPL();
1612 jint index = POPI();
1613 jlongArray arr = (jlongArray) POPA();
1614 NULLARRAYCHECK (arr);
1615 ARRAYBOUNDSCHECK (arr, index);
1616 elements(arr)[index] = value;
1622 jfloat value = POPF();
1623 jint index = POPI();
1624 jfloatArray arr = (jfloatArray) POPA();
1625 NULLARRAYCHECK (arr);
1626 ARRAYBOUNDSCHECK (arr, index);
1627 elements(arr)[index] = value;
1633 jdouble value = POPD();
1634 jint index = POPI();
1635 jdoubleArray arr = (jdoubleArray) POPA();
1636 NULLARRAYCHECK (arr);
1637 ARRAYBOUNDSCHECK (arr, index);
1638 elements(arr)[index] = value;
1644 jobject value = POPA();
1645 jint index = POPI();
1646 jobjectArray arr = (jobjectArray) POPA();
1647 NULLARRAYCHECK (arr);
1648 ARRAYBOUNDSCHECK (arr, index);
1649 _Jv_CheckArrayStore (arr, value);
1650 elements(arr)[index] = value;
1656 jbyte value = (jbyte) POPI();
1657 jint index = POPI();
1658 jbyteArray arr = (jbyteArray) POPA();
1659 NULLARRAYCHECK (arr);
1660 ARRAYBOUNDSCHECK (arr, index);
1661 elements(arr)[index] = value;
1667 jchar value = (jchar) POPI();
1668 jint index = POPI();
1669 jcharArray arr = (jcharArray) POPA();
1670 NULLARRAYCHECK (arr);
1671 ARRAYBOUNDSCHECK (arr, index);
1672 elements(arr)[index] = value;
1678 jshort value = (jshort) POPI();
1679 jint index = POPI();
1680 jshortArray arr = (jshortArray) POPA();
1681 NULLARRAYCHECK (arr);
1682 ARRAYBOUNDSCHECK (arr, index);
1683 elements(arr)[index] = value;
1701 dupx (sp, 1, 1); sp+=1;
1705 dupx (sp, 1, 2); sp+=1;
1715 dupx (sp, 2, 1); sp+=2;
1719 dupx (sp, 2, 2); sp+=2;
1724 jobject tmp1 = POPA();
1725 jobject tmp2 = POPA();
1781 jint value2 = POPI();
1782 jint value1 = POPI();
1783 jint res = _Jv_divI (value1, value2);
1790 jlong value2 = POPL();
1791 jlong value1 = POPL();
1792 jlong res = _Jv_divJ (value1, value2);
1799 jfloat value2 = POPF();
1800 jfloat value1 = POPF();
1801 jfloat res = value1 / value2;
1808 jdouble value2 = POPD();
1809 jdouble value1 = POPD();
1810 jdouble res = value1 / value2;
1817 jint value2 = POPI();
1818 jint value1 = POPI();
1819 jint res = _Jv_remI (value1, value2);
1826 jlong value2 = POPL();
1827 jlong value1 = POPL();
1828 jlong res = _Jv_remJ (value1, value2);
1835 jfloat value2 = POPF();
1836 jfloat value1 = POPF();
1837 jfloat res = __ieee754_fmod (value1, value2);
1844 jdouble value2 = POPD();
1845 jdouble value1 = POPD();
1846 jdouble res = __ieee754_fmod (value1, value2);
1853 jint value = POPI();
1860 jlong value = POPL();
1867 jfloat value = POPF();
1874 jdouble value = POPD();
1881 jint shift = (POPI() & 0x1f);
1882 jint value = POPI();
1883 PUSHI (value << shift);
1889 jint shift = (POPI() & 0x3f);
1890 jlong value = POPL();
1891 PUSHL (value << shift);
1897 jint shift = (POPI() & 0x1f);
1898 jint value = POPI();
1899 PUSHI (value >> shift);
1905 jint shift = (POPI() & 0x3f);
1906 jlong value = POPL();
1907 PUSHL (value >> shift);
1913 jint shift = (POPI() & 0x1f);
1914 _Jv_uint value = (_Jv_uint) POPI();
1915 PUSHI ((jint) (value >> shift));
1921 jint shift = (POPI() & 0x3f);
1922 _Jv_ulong value = (_Jv_ulong) POPL();
1923 PUSHL ((jlong) (value >> shift));
1953 jint index = GET1U ();
1954 jint amount = GET1S ();
1955 locals[index].i += amount;
1960 {jlong value = POPI(); PUSHL (value);}
1964 {jfloat value = POPI(); PUSHF (value);}
1968 {jdouble value = POPI(); PUSHD (value);}
1972 {jint value = POPL(); PUSHI (value);}
1976 {jfloat value = POPL(); PUSHF (value);}
1980 {jdouble value = POPL(); PUSHD (value);}
1985 using namespace java::lang;
1986 jint value = convert (POPF (), Integer::MIN_VALUE, Integer::MAX_VALUE);
1993 using namespace java::lang;
1994 jlong value = convert (POPF (), Long::MIN_VALUE, Long::MAX_VALUE);
2000 { jdouble value = POPF (); PUSHD(value); }
2005 using namespace java::lang;
2006 jint value = convert (POPD (), Integer::MIN_VALUE, Integer::MAX_VALUE);
2013 using namespace java::lang;
2014 jlong value = convert (POPD (), Long::MIN_VALUE, Long::MAX_VALUE);
2020 { jfloat value = POPD (); PUSHF(value); }
2024 { jbyte value = POPI (); PUSHI(value); }
2028 { jchar value = POPI (); PUSHI(value); }
2032 { jshort value = POPI (); PUSHI(value); }
2037 jlong value2 = POPL ();
2038 jlong value1 = POPL ();
2039 if (value1 > value2)
2041 else if (value1 == value2)
2057 jfloat value2 = POPF ();
2058 jfloat value1 = POPF ();
2059 if (value1 > value2)
2061 else if (value1 == value2)
2063 else if (value1 < value2)
2079 jdouble value2 = POPD ();
2080 jdouble value1 = POPD ();
2081 if (value1 > value2)
2083 else if (value1 == value2)
2085 else if (value1 < value2)
2148 jint value2 = POPI();
2149 jint value1 = POPI();
2150 if (value1 == value2)
2159 jint value2 = POPI();
2160 jint value1 = POPI();
2161 if (value1 != value2)
2170 jint value2 = POPI();
2171 jint value1 = POPI();
2172 if (value1 < value2)
2181 jint value2 = POPI();
2182 jint value1 = POPI();
2183 if (value1 >= value2)
2192 jint value2 = POPI();
2193 jint value1 = POPI();
2194 if (value1 > value2)
2203 jint value2 = POPI();
2204 jint value1 = POPI();
2205 if (value1 <= value2)
2214 jobject value2 = POPA();
2215 jobject value1 = POPA();
2216 if (value1 == value2)
2225 jobject value2 = POPA();
2226 jobject value1 = POPA();
2227 if (value1 != value2)
2235 #ifndef DIRECT_THREADED
2236 // For direct threaded, goto and goto_w are the same.
2237 pc = pc - 1 + get4 (pc);
2239 #endif /* DIRECT_THREADED */
2245 #ifndef DIRECT_THREADED
2246 // For direct threaded, jsr and jsr_w are the same.
2248 pc_t next = pc - 1 + get4 (pc);
2250 PUSHA ((jobject) pc);
2254 #endif /* DIRECT_THREADED */
2257 pc_t next = GOTO_VAL();
2259 PUSHA ((jobject) pc);
2266 jint index = GET1U ();
2267 pc = (pc_t) PEEKA (index);
2273 #ifdef DIRECT_THREADED
2274 void *def = (pc++)->datum;
2278 jint low = INTVAL ();
2279 jint high = INTVAL ();
2281 if (index < low || index > high)
2282 pc = (insn_slot *) def;
2284 pc = (insn_slot *) ((pc + index - low)->datum);
2286 pc_t base_pc = pc - 1;
2287 int index = POPI ();
2289 pc_t base = (pc_t) bytecode ();
2290 while ((pc - base) % 4 != 0)
2293 jint def = get4 (pc);
2294 jint low = get4 (pc + 4);
2295 jint high = get4 (pc + 8);
2296 if (index < low || index > high)
2299 pc = base_pc + get4 (pc + 4 * (index - low + 3));
2300 #endif /* DIRECT_THREADED */
2306 #ifdef DIRECT_THREADED
2307 void *def = (pc++)->insn;
2311 jint npairs = INTVAL ();
2313 int max = npairs - 1;
2316 // Simple binary search...
2319 int half = (min + max) / 2;
2320 int match = pc[2 * half].int_val;
2325 pc = (insn_slot *) pc[2 * half + 1].datum;
2328 else if (index < match)
2329 // We can use HALF - 1 here because we check again on
2333 // We can use HALF + 1 here because we check again on
2337 if (index == pc[2 * min].int_val)
2338 pc = (insn_slot *) pc[2 * min + 1].datum;
2340 pc = (insn_slot *) def;
2342 unsigned char *base_pc = pc-1;
2345 unsigned char* base = bytecode ();
2346 while ((pc-base) % 4 != 0)
2349 jint def = get4 (pc);
2350 jint npairs = get4 (pc+4);
2355 // Simple binary search...
2358 int half = (min+max)/2;
2359 int match = get4 (pc+ 4*(2 + 2*half));
2363 else if (index < match)
2364 // We can use HALF - 1 here because we check again on
2368 // We can use HALF + 1 here because we check again on
2373 if (index == get4 (pc+ 4*(2 + 2*min)))
2374 pc = base_pc + get4 (pc+ 4*(2 + 2*min + 1));
2377 #endif /* DIRECT_THREADED */
2382 *(jobject *) retp = POPA ();
2386 *(jlong *) retp = POPL ();
2390 *(jfloat *) retp = POPF ();
2394 *(jdouble *) retp = POPD ();
2398 *(jint *) retp = POPI ();
2406 jint fieldref_index = GET2U ();
2407 SAVE_PC(); // Constant pool resolution could throw.
2408 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2409 _Jv_Field *field = pool_data[fieldref_index].field;
2411 if ((field->flags & Modifier::STATIC) == 0)
2412 throw_incompatible_class_change_error
2413 (JvNewStringLatin1 ("field no longer static"));
2415 jclass type = field->type;
2417 // We rewrite the instruction once we discover what it refers
2419 void *newinsn = NULL;
2420 if (type->isPrimitive ())
2422 switch (type->size_in_bytes)
2425 PUSHI (*field->u.byte_addr);
2426 newinsn = AMPAMP (getstatic_resolved_1);
2430 if (type == JvPrimClass (char))
2432 PUSHI (*field->u.char_addr);
2433 newinsn = AMPAMP (getstatic_resolved_char);
2437 PUSHI (*field->u.short_addr);
2438 newinsn = AMPAMP (getstatic_resolved_short);
2443 PUSHI(*field->u.int_addr);
2444 newinsn = AMPAMP (getstatic_resolved_4);
2448 PUSHL(*field->u.long_addr);
2449 newinsn = AMPAMP (getstatic_resolved_8);
2455 PUSHA(*field->u.object_addr);
2456 newinsn = AMPAMP (getstatic_resolved_obj);
2459 #ifdef DIRECT_THREADED
2460 pc[-2].insn = newinsn;
2461 pc[-1].datum = field->u.addr;
2462 #endif /* DIRECT_THREADED */
2466 #ifdef DIRECT_THREADED
2467 getstatic_resolved_1:
2468 PUSHI (*(jbyte *) AVAL ());
2471 getstatic_resolved_char:
2472 PUSHI (*(jchar *) AVAL ());
2475 getstatic_resolved_short:
2476 PUSHI (*(jshort *) AVAL ());
2479 getstatic_resolved_4:
2480 PUSHI (*(jint *) AVAL ());
2483 getstatic_resolved_8:
2484 PUSHL (*(jlong *) AVAL ());
2487 getstatic_resolved_obj:
2488 PUSHA (*(jobject *) AVAL ());
2490 #endif /* DIRECT_THREADED */
2494 jint fieldref_index = GET2U ();
2495 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2496 _Jv_Field *field = pool_data[fieldref_index].field;
2498 if ((field->flags & Modifier::STATIC) != 0)
2499 throw_incompatible_class_change_error
2500 (JvNewStringLatin1 ("field is static"));
2502 jclass type = field->type;
2503 jint field_offset = field->u.boffset;
2504 if (field_offset > 0xffff)
2505 throw new java::lang::VirtualMachineError;
2507 jobject obj = POPA();
2510 void *newinsn = NULL;
2511 _Jv_value *val = (_Jv_value *) ((char *)obj + field_offset);
2512 if (type->isPrimitive ())
2514 switch (type->size_in_bytes)
2517 PUSHI (val->byte_value);
2518 newinsn = AMPAMP (getfield_resolved_1);
2522 if (type == JvPrimClass (char))
2524 PUSHI (val->char_value);
2525 newinsn = AMPAMP (getfield_resolved_char);
2529 PUSHI (val->short_value);
2530 newinsn = AMPAMP (getfield_resolved_short);
2535 PUSHI (val->int_value);
2536 newinsn = AMPAMP (getfield_resolved_4);
2540 PUSHL (val->long_value);
2541 newinsn = AMPAMP (getfield_resolved_8);
2547 PUSHA (val->object_value);
2548 newinsn = AMPAMP (getfield_resolved_obj);
2551 #ifdef DIRECT_THREADED
2552 pc[-2].insn = newinsn;
2553 pc[-1].int_val = field_offset;
2554 #endif /* DIRECT_THREADED */
2558 #ifdef DIRECT_THREADED
2559 getfield_resolved_1:
2561 char *obj = (char *) POPA ();
2563 PUSHI (*(jbyte *) (obj + INTVAL ()));
2567 getfield_resolved_char:
2569 char *obj = (char *) POPA ();
2571 PUSHI (*(jchar *) (obj + INTVAL ()));
2575 getfield_resolved_short:
2577 char *obj = (char *) POPA ();
2579 PUSHI (*(jshort *) (obj + INTVAL ()));
2583 getfield_resolved_4:
2585 char *obj = (char *) POPA ();
2587 PUSHI (*(jint *) (obj + INTVAL ()));
2591 getfield_resolved_8:
2593 char *obj = (char *) POPA ();
2595 PUSHL (*(jlong *) (obj + INTVAL ()));
2599 getfield_resolved_obj:
2601 char *obj = (char *) POPA ();
2603 PUSHA (*(jobject *) (obj + INTVAL ()));
2606 #endif /* DIRECT_THREADED */
2610 jint fieldref_index = GET2U ();
2611 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2612 _Jv_Field *field = pool_data[fieldref_index].field;
2614 jclass type = field->type;
2616 // ResolvePoolEntry cannot check this
2617 if ((field->flags & Modifier::STATIC) == 0)
2618 throw_incompatible_class_change_error
2619 (JvNewStringLatin1 ("field no longer static"));
2621 void *newinsn = NULL;
2622 if (type->isPrimitive ())
2624 switch (type->size_in_bytes)
2628 jint value = POPI();
2629 *field->u.byte_addr = value;
2630 newinsn = AMPAMP (putstatic_resolved_1);
2636 jint value = POPI();
2637 *field->u.char_addr = value;
2638 newinsn = AMPAMP (putstatic_resolved_2);
2644 jint value = POPI();
2645 *field->u.int_addr = value;
2646 newinsn = AMPAMP (putstatic_resolved_4);
2652 jlong value = POPL();
2653 *field->u.long_addr = value;
2654 newinsn = AMPAMP (putstatic_resolved_8);
2661 jobject value = POPA();
2662 *field->u.object_addr = value;
2663 newinsn = AMPAMP (putstatic_resolved_obj);
2666 #ifdef DIRECT_THREADED
2667 pc[-2].insn = newinsn;
2668 pc[-1].datum = field->u.addr;
2669 #endif /* DIRECT_THREADED */
2673 #ifdef DIRECT_THREADED
2674 putstatic_resolved_1:
2675 *(jbyte *) AVAL () = POPI ();
2678 putstatic_resolved_2:
2679 *(jchar *) AVAL () = POPI ();
2682 putstatic_resolved_4:
2683 *(jint *) AVAL () = POPI ();
2686 putstatic_resolved_8:
2687 *(jlong *) AVAL () = POPL ();
2690 putstatic_resolved_obj:
2691 *(jobject *) AVAL () = POPA ();
2693 #endif /* DIRECT_THREADED */
2697 jint fieldref_index = GET2U ();
2698 _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
2699 _Jv_Field *field = pool_data[fieldref_index].field;
2701 jclass type = field->type;
2703 if ((field->flags & Modifier::STATIC) != 0)
2704 throw_incompatible_class_change_error
2705 (JvNewStringLatin1 ("field is static"));
2707 jint field_offset = field->u.boffset;
2708 if (field_offset > 0xffff)
2709 throw new java::lang::VirtualMachineError;
2711 void *newinsn = NULL;
2712 if (type->isPrimitive ())
2714 switch (type->size_in_bytes)
2718 jint value = POPI();
2719 jobject obj = POPA();
2721 *(jbyte*) ((char*)obj + field_offset) = value;
2722 newinsn = AMPAMP (putfield_resolved_1);
2728 jint value = POPI();
2729 jobject obj = POPA();
2731 *(jchar*) ((char*)obj + field_offset) = value;
2732 newinsn = AMPAMP (putfield_resolved_2);
2738 jint value = POPI();
2739 jobject obj = POPA();
2741 *(jint*) ((char*)obj + field_offset) = value;
2742 newinsn = AMPAMP (putfield_resolved_4);
2748 jlong value = POPL();
2749 jobject obj = POPA();
2751 *(jlong*) ((char*)obj + field_offset) = value;
2752 newinsn = AMPAMP (putfield_resolved_8);
2759 jobject value = POPA();
2760 jobject obj = POPA();
2762 *(jobject*) ((char*)obj + field_offset) = value;
2763 newinsn = AMPAMP (putfield_resolved_obj);
2766 #ifdef DIRECT_THREADED
2767 pc[-2].insn = newinsn;
2768 pc[-1].int_val = field_offset;
2769 #endif /* DIRECT_THREADED */
2773 #ifdef DIRECT_THREADED
2774 putfield_resolved_1:
2777 char *obj = (char *) POPA ();
2779 *(jbyte *) (obj + INTVAL ()) = val;
2783 putfield_resolved_2:
2786 char *obj = (char *) POPA ();
2788 *(jchar *) (obj + INTVAL ()) = val;
2792 putfield_resolved_4:
2795 char *obj = (char *) POPA ();
2797 *(jint *) (obj + INTVAL ()) = val;
2801 putfield_resolved_8:
2803 jlong val = POPL ();
2804 char *obj = (char *) POPA ();
2806 *(jlong *) (obj + INTVAL ()) = val;
2810 putfield_resolved_obj:
2812 jobject val = POPA ();
2813 char *obj = (char *) POPA ();
2815 *(jobject *) (obj + INTVAL ()) = val;
2818 #endif /* DIRECT_THREADED */
2822 int index = GET2U ();
2824 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2827 sp -= rmeth->stack_item_count;
2829 // We don't use NULLCHECK here because we can't rely on that
2830 // working for <init>. So instead we do an explicit test.
2834 throw new java::lang::NullPointerException;
2837 fun = (void (*)()) rmeth->method->ncode;
2839 #ifdef DIRECT_THREADED
2840 // Rewrite instruction so that we use a faster pre-resolved
2842 pc[-2].insn = &&invokespecial_resolved;
2843 pc[-1].datum = rmeth;
2844 #endif /* DIRECT_THREADED */
2846 goto perform_invoke;
2848 #ifdef DIRECT_THREADED
2849 invokespecial_resolved:
2851 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2852 sp -= rmeth->stack_item_count;
2853 // We don't use NULLCHECK here because we can't rely on that
2854 // working for <init>. So instead we do an explicit test.
2858 throw new java::lang::NullPointerException;
2860 fun = (void (*)()) rmeth->method->ncode;
2862 goto perform_invoke;
2863 #endif /* DIRECT_THREADED */
2867 int index = GET2U ();
2869 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2872 sp -= rmeth->stack_item_count;
2874 fun = (void (*)()) rmeth->method->ncode;
2876 #ifdef DIRECT_THREADED
2877 // Rewrite instruction so that we use a faster pre-resolved
2879 pc[-2].insn = &&invokestatic_resolved;
2880 pc[-1].datum = rmeth;
2881 #endif /* DIRECT_THREADED */
2883 goto perform_invoke;
2885 #ifdef DIRECT_THREADED
2886 invokestatic_resolved:
2888 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2889 sp -= rmeth->stack_item_count;
2890 fun = (void (*)()) rmeth->method->ncode;
2892 goto perform_invoke;
2893 #endif /* DIRECT_THREADED */
2895 insn_invokeinterface:
2897 int index = GET2U ();
2899 rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2902 sp -= rmeth->stack_item_count;
2904 jobject rcv = sp[0].o;
2909 _Jv_LookupInterfaceMethod (rcv->getClass (),
2910 rmeth->method->name,
2911 rmeth->method->signature);
2913 #ifdef DIRECT_THREADED
2914 // Rewrite instruction so that we use a faster pre-resolved
2916 pc[-2].insn = &&invokeinterface_resolved;
2917 pc[-1].datum = rmeth;
2919 // Skip dummy bytes.
2921 #endif /* DIRECT_THREADED */
2923 goto perform_invoke;
2925 #ifdef DIRECT_THREADED
2926 invokeinterface_resolved:
2928 rmeth = (_Jv_ResolvedMethod *) AVAL ();
2929 sp -= rmeth->stack_item_count;
2930 jobject rcv = sp[0].o;
2933 _Jv_LookupInterfaceMethod (rcv->getClass (),
2934 rmeth->method->name,
2935 rmeth->method->signature);
2937 goto perform_invoke;
2938 #endif /* DIRECT_THREADED */
2942 int index = GET2U ();
2943 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2945 jobject res = _Jv_AllocObject (klass);
2948 #ifdef DIRECT_THREADED
2949 pc[-2].insn = &&new_resolved;
2950 pc[-1].datum = klass;
2951 #endif /* DIRECT_THREADED */
2955 #ifdef DIRECT_THREADED
2958 jclass klass = (jclass) AVAL ();
2959 jobject res = _Jv_AllocObject (klass);
2963 #endif /* DIRECT_THREADED */
2967 int atype = GET1U ();
2969 jobject result = _Jv_NewArray (atype, size);
2976 int index = GET2U ();
2977 jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
2980 jobject result = _Jv_NewObjectArray (size, klass, 0);
2983 #ifdef DIRECT_THREADED
2984 pc[-2].insn = &&anewarray_resolved;
2985 pc[-1].datum = klass;
2986 #endif /* DIRECT_THREADED */
2990 #ifdef DIRECT_THREADED
2993 jclass klass = (jclass) AVAL ();
2995 jobject result = _Jv_NewObjectArray (size, klass, 0);
2999 #endif /* DIRECT_THREADED */
3003 __JArray *arr = (__JArray*)POPA();
3004 NULLARRAYCHECK (arr);
3005 PUSHI (arr->length);
3011 jobject value = POPA();
3012 throw static_cast<jthrowable>(value);
3019 jobject value = POPA();
3020 jint index = GET2U ();
3021 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3024 value = (jobject) _Jv_CheckCast (to, value);
3028 #ifdef DIRECT_THREADED
3029 pc[-2].insn = &&checkcast_resolved;
3031 #endif /* DIRECT_THREADED */
3035 #ifdef DIRECT_THREADED
3039 jobject value = POPA ();
3040 jclass to = (jclass) AVAL ();
3041 value = (jobject) _Jv_CheckCast (to, value);
3045 #endif /* DIRECT_THREADED */
3050 jobject value = POPA();
3051 jint index = GET2U ();
3052 jclass to = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3054 PUSHI (to->isInstance (value));
3056 #ifdef DIRECT_THREADED
3057 pc[-2].insn = &&instanceof_resolved;
3059 #endif /* DIRECT_THREADED */
3063 #ifdef DIRECT_THREADED
3064 instanceof_resolved:
3066 jobject value = POPA ();
3067 jclass to = (jclass) AVAL ();
3068 PUSHI (to->isInstance (value));
3071 #endif /* DIRECT_THREADED */
3075 jobject value = POPA();
3077 _Jv_MonitorEnter (value);
3083 jobject value = POPA();
3085 _Jv_MonitorExit (value);
3091 jobject val = POPA();
3101 jobject val = POPA();
3109 insn_multianewarray:
3111 int kind_index = GET2U ();
3115 = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
3117 jint *sizes = (jint*) __builtin_alloca (sizeof (jint)*dim);
3119 for (int i = dim - 1; i >= 0; i--)
3124 jobject res = _Jv_NewMultiArray (type,dim, sizes);
3130 #ifndef DIRECT_THREADED
3133 jint the_mod_op = get1u (pc++);
3134 jint wide = get2u (pc); pc += 2;
3179 pc = (unsigned char*) PEEKA (wide);
3184 jint amount = get2s (pc); pc += 2;
3185 jint value = PEEKI (wide);
3186 POKEI (wide, value+amount);
3191 throw_internal_error ("illegal bytecode modified by wide");
3195 #endif /* DIRECT_THREADED */
3197 catch (java::lang::Throwable *ex)
3199 #ifdef DIRECT_THREADED
3200 void *logical_pc = (void *) ((insn_slot *) pc - 1);
3202 int logical_pc = pc - 1 - bytecode ();
3204 _Jv_InterpException *exc = meth->exceptions ();
3205 jclass exc_class = ex->getClass ();
3207 for (int i = 0; i < meth->exc_count; i++)
3209 if (PCVAL (exc[i].start_pc) <= logical_pc
3210 && logical_pc < PCVAL (exc[i].end_pc))
3212 #ifdef DIRECT_THREADED
3213 jclass handler = (jclass) exc[i].handler_type.p;
3215 jclass handler = NULL;
3216 if (exc[i].handler_type.i != 0)
3217 handler = (_Jv_Linker::resolve_pool_entry (defining_class,
3218 exc[i].handler_type.i)).clazz;
3219 #endif /* DIRECT_THREADED */
3221 if (handler == NULL || handler->isAssignableFrom (exc_class))
3223 #ifdef DIRECT_THREADED
3224 pc = (insn_slot *) exc[i].handler_pc.p;
3226 pc = bytecode () + exc[i].handler_pc.i;
3227 #endif /* DIRECT_THREADED */
3229 sp++->o = ex; // Push exception.
3235 // No handler, so re-throw.
3241 throw_internal_error (char *msg)
3243 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
3247 throw_incompatible_class_change_error (jstring msg)
3249 throw new java::lang::IncompatibleClassChangeError (msg);
3253 static java::lang::NullPointerException *null_pointer_exc;
3255 throw_null_pointer_exception ()
3257 if (null_pointer_exc == NULL)
3258 null_pointer_exc = new java::lang::NullPointerException;
3260 throw null_pointer_exc;
3264 /* Look up source code line number for given bytecode (or direct threaded
3267 _Jv_InterpMethod::get_source_line(pc_t mpc)
3269 int line = line_table_len > 0 ? line_table[0].line : -1;
3270 for (int i = 1; i < line_table_len; i++)
3271 if (line_table[i].pc > mpc)
3274 line = line_table[i].line;
3279 /** Do static initialization for fields with a constant initializer */
3281 _Jv_InitField (jobject obj, jclass klass, int index)
3283 using namespace java::lang::reflect;
3285 if (obj != 0 && klass == 0)
3286 klass = obj->getClass ();
3288 if (!_Jv_IsInterpretedClass (klass))
3291 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
3293 _Jv_Field * field = (&klass->fields[0]) + index;
3295 if (index > klass->field_count)
3296 throw_internal_error ("field out of range");
3298 int init = iclass->field_initializers[index];
3302 _Jv_Constants *pool = &klass->constants;
3303 int tag = pool->tags[init];
3305 if (! field->isResolved ())
3306 throw_internal_error ("initializing unresolved field");
3308 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
3309 throw_internal_error ("initializing non-static field with no object");
3313 if ((field->flags & Modifier::STATIC) != 0)
3314 addr = (void*) field->u.addr;
3316 addr = (void*) (((char*)obj) + field->u.boffset);
3320 case JV_CONSTANT_String:
3323 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
3324 pool->data[init].string = str;
3325 pool->tags[init] = JV_CONSTANT_ResolvedString;
3329 case JV_CONSTANT_ResolvedString:
3330 if (! (field->type == &java::lang::String::class$
3331 || field->type == &java::lang::Class::class$))
3332 throw_class_format_error ("string initialiser to non-string field");
3334 *(jstring*)addr = pool->data[init].string;
3337 case JV_CONSTANT_Integer:
3339 int value = pool->data[init].i;
3341 if (field->type == JvPrimClass (boolean))
3342 *(jboolean*)addr = (jboolean)value;
3344 else if (field->type == JvPrimClass (byte))
3345 *(jbyte*)addr = (jbyte)value;
3347 else if (field->type == JvPrimClass (char))
3348 *(jchar*)addr = (jchar)value;
3350 else if (field->type == JvPrimClass (short))
3351 *(jshort*)addr = (jshort)value;
3353 else if (field->type == JvPrimClass (int))
3354 *(jint*)addr = (jint)value;
3357 throw_class_format_error ("erroneous field initializer");
3361 case JV_CONSTANT_Long:
3362 if (field->type != JvPrimClass (long))
3363 throw_class_format_error ("erroneous field initializer");
3365 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
3368 case JV_CONSTANT_Float:
3369 if (field->type != JvPrimClass (float))
3370 throw_class_format_error ("erroneous field initializer");
3372 *(jfloat*)addr = pool->data[init].f;
3375 case JV_CONSTANT_Double:
3376 if (field->type != JvPrimClass (double))
3377 throw_class_format_error ("erroneous field initializer");
3379 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
3383 throw_class_format_error ("erroneous field initializer");
3387 inline static unsigned char*
3388 skip_one_type (unsigned char* ptr)
3399 do { ch = *ptr++; } while (ch != ';');
3406 get_ffi_type_from_signature (unsigned char* ptr)
3412 return &ffi_type_pointer;
3416 // On some platforms a bool is a byte, on others an int.
3417 if (sizeof (jboolean) == sizeof (jbyte))
3418 return &ffi_type_sint8;
3421 JvAssert (sizeof (jbyte) == sizeof (jint));
3422 return &ffi_type_sint32;
3427 return &ffi_type_sint8;
3431 return &ffi_type_uint16;
3435 return &ffi_type_sint16;
3439 return &ffi_type_sint32;
3443 return &ffi_type_sint64;
3447 return &ffi_type_float;
3451 return &ffi_type_double;
3455 return &ffi_type_void;
3459 throw_internal_error ("unknown type in signature");
3462 /* this function yields the number of actual arguments, that is, if the
3463 * function is non-static, then one is added to the number of elements
3464 * found in the signature */
3467 _Jv_count_arguments (_Jv_Utf8Const *signature,
3470 unsigned char *ptr = (unsigned char*) signature->chars();
3471 int arg_count = staticp ? 0 : 1;
3473 /* first, count number of arguments */
3481 ptr = skip_one_type (ptr);
3488 /* This beast will build a cif, given the signature. Memory for
3489 * the cif itself and for the argument types must be allocated by the
3494 init_cif (_Jv_Utf8Const* signature,
3498 ffi_type **arg_types,
3501 unsigned char *ptr = (unsigned char*) signature->chars();
3503 int arg_index = 0; // arg number
3504 int item_count = 0; // stack-item count
3509 arg_types[arg_index++] = &ffi_type_pointer;
3519 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
3521 if (*ptr == 'J' || *ptr == 'D')
3526 ptr = skip_one_type (ptr);
3531 ffi_type *rtype = get_ffi_type_from_signature (ptr);
3533 ptr = skip_one_type (ptr);
3534 if (ptr != (unsigned char*)signature->chars() + signature->len())
3535 throw_internal_error ("did not find end of signature");
3537 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
3538 arg_count, rtype, arg_types) != FFI_OK)
3539 throw_internal_error ("ffi_prep_cif failed");
3541 if (rtype_p != NULL)
3547 #if FFI_NATIVE_RAW_API
3548 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
3549 # define FFI_RAW_SIZE ffi_raw_size
3551 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
3552 # define FFI_RAW_SIZE ffi_java_raw_size
3555 /* we put this one here, and not in interpret.cc because it
3556 * calls the utility routines _Jv_count_arguments
3557 * which are static to this module. The following struct defines the
3558 * layout we use for the stubs, it's only used in the ncode method. */
3561 ffi_raw_closure closure;
3563 ffi_type *arg_types[0];
3566 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
3569 _Jv_InterpMethod::ncode ()
3571 using namespace java::lang::reflect;
3573 if (self->ncode != 0)
3576 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3577 int arg_count = _Jv_count_arguments (self->signature, staticp);
3579 ncode_closure *closure =
3580 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3581 + arg_count * sizeof (ffi_type*));
3583 init_cif (self->signature,
3587 &closure->arg_types[0],
3590 ffi_closure_fun fun;
3592 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3594 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
3596 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
3599 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
3601 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
3606 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
3608 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
3611 FFI_PREP_RAW_CLOSURE (&closure->closure,
3616 self->ncode = (void*)closure;
3621 _Jv_JNIMethod::ncode ()
3623 using namespace java::lang::reflect;
3625 if (self->ncode != 0)
3628 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
3629 int arg_count = _Jv_count_arguments (self->signature, staticp);
3631 ncode_closure *closure =
3632 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
3633 + arg_count * sizeof (ffi_type*));
3636 init_cif (self->signature,
3640 &closure->arg_types[0],
3643 ffi_closure_fun fun;
3645 args_raw_size = FFI_RAW_SIZE (&closure->cif);
3647 // Initialize the argument types and CIF that represent the actual
3648 // underlying JNI function.
3650 if ((self->accflags & Modifier::STATIC))
3652 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
3653 * sizeof (ffi_type *));
3655 jni_arg_types[offset++] = &ffi_type_pointer;
3656 if ((self->accflags & Modifier::STATIC))
3657 jni_arg_types[offset++] = &ffi_type_pointer;
3658 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
3659 arg_count * sizeof (ffi_type *));
3661 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
3662 extra_args + arg_count, rtype,
3663 jni_arg_types) != FFI_OK)
3664 throw_internal_error ("ffi_prep_cif failed for JNI function");
3666 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
3668 // FIXME: for now we assume that all native methods for
3669 // interpreted code use JNI.
3670 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
3672 FFI_PREP_RAW_CLOSURE (&closure->closure,
3677 self->ncode = (void *) closure;
3682 throw_class_format_error (jstring msg)
3685 ? new java::lang::ClassFormatError (msg)
3686 : new java::lang::ClassFormatError);
3690 throw_class_format_error (char *msg)
3692 throw_class_format_error (JvNewStringLatin1 (msg));
3698 _Jv_InterpreterEngine::do_verify (jclass klass)
3700 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3701 for (int i = 0; i < klass->method_count; i++)
3703 using namespace java::lang::reflect;
3704 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3705 _Jv_ushort accflags = klass->methods[i].accflags;
3706 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
3708 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3709 _Jv_VerifyMethod (im);
3715 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
3717 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3718 for (int i = 0; i < klass->method_count; i++)
3720 // Just skip abstract methods. This is particularly important
3721 // because we don't resize the interpreted_methods array when
3722 // miranda methods are added to it.
3723 if ((klass->methods[i].accflags
3724 & java::lang::reflect::Modifier::ABSTRACT)
3728 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
3730 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
3733 // You might think we could use a virtual `ncode' method in
3734 // the _Jv_MethodBase and unify the native and non-native
3735 // cases. Well, we can't, because we don't allocate these
3736 // objects using `new', and thus they don't get a vtable.
3737 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
3738 klass->methods[i].ncode = jnim->ncode ();
3740 else if (imeth != 0) // it could be abstract
3742 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
3743 klass->methods[i].ncode = im->ncode ();
3749 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
3752 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
3754 char *static_data = (char *) _Jv_AllocBytes (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