1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
16 #pragma implementation "java-interp.h"
19 #include <java-cpool.h>
20 #include <java-interp.h>
21 #include <java/lang/System.h>
22 #include <java/lang/String.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/StringBuffer.h>
26 #include <java/lang/Class.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/InternalError.h>
29 #include <java/lang/NullPointerException.h>
30 #include <java/lang/ArithmeticException.h>
31 #include <java/lang/IncompatibleClassChangeError.h>
32 #include <java/lang/InstantiationException.h>
33 #include <java/lang/Thread.h>
34 #include <java-insns.h>
35 #include <java-signal.h>
36 #include <java/lang/ClassFormatError.h>
37 #include <execution.h>
38 #include <java/lang/reflect/Modifier.h>
41 #include "jvmti-int.h"
43 #include <gnu/classpath/jdwp/Jdwp.h>
44 #include <gnu/gcj/jvmti/Breakpoint.h>
45 #include <gnu/gcj/jvmti/BreakpointManager.h>
49 // Execution engine for interpreted code.
50 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
56 static void throw_internal_error (const char *msg)
57 __attribute__ ((__noreturn__));
58 static void throw_incompatible_class_change_error (jstring msg)
59 __attribute__ ((__noreturn__));
60 static void throw_null_pointer_exception ()
61 __attribute__ ((__noreturn__));
63 static void throw_class_format_error (jstring msg)
64 __attribute__ ((__noreturn__));
65 static void throw_class_format_error (const char *msg)
66 __attribute__ ((__noreturn__));
68 #ifdef DIRECT_THREADED
69 // Lock to ensure that methods are not compiled concurrently.
70 // We could use a finer-grained lock here, however it is not safe to use
71 // the Class monitor as user code in another thread could hold it.
72 static _Jv_Mutex_t compile_mutex;
77 _Jv_MutexInit (&compile_mutex);
80 void _Jv_InitInterpreter() {}
83 // The breakpoint instruction. For the direct threaded case,
84 // _Jv_InterpMethod::compile will initialize breakpoint_insn
85 // the first time it is called.
86 #ifdef DIRECT_THREADED
87 insn_slot _Jv_InterpMethod::bp_insn_slot;
88 pc_t _Jv_InterpMethod::breakpoint_insn = NULL;
90 unsigned char _Jv_InterpMethod::bp_insn_opcode
91 = static_cast<unsigned char> (op_breakpoint);
92 pc_t _Jv_InterpMethod::breakpoint_insn = &_Jv_InterpMethod::bp_insn_opcode;
95 extern "C" double __ieee754_fmod (double,double);
97 static inline void dupx (_Jv_word *sp, int n, int x)
99 // first "slide" n+x elements n to the right
101 for (int i = 0; i < n+x; i++)
103 sp[(top-i)] = sp[(top-i)-n];
106 // next, copy the n top elements, n+x down
107 for (int i = 0; i < n; i++)
109 sp[top-(n+x)-i] = sp[top-i];
113 // Used to convert from floating types to integral types.
114 template<typename TO, typename FROM>
116 convert (FROM val, TO min, TO max)
119 if (val >= (FROM) max)
121 else if (val <= (FROM) min)
130 #define PUSHA(V) (sp++)->o = (V)
131 #define PUSHI(V) (sp++)->i = (V)
132 #define PUSHF(V) (sp++)->f = (V)
133 #if SIZEOF_VOID_P == 8
134 # define PUSHL(V) (sp->l = (V), sp += 2)
135 # define PUSHD(V) (sp->d = (V), sp += 2)
137 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
138 (sp++)->ia[0] = w2.ia[0]; \
139 (sp++)->ia[0] = w2.ia[1]; } while (0)
140 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
141 (sp++)->ia[0] = w2.ia[0]; \
142 (sp++)->ia[0] = w2.ia[1]; } while (0)
145 #define POPA() ((--sp)->o)
146 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
147 #define POPF() ((jfloat) (--sp)->f)
148 #if SIZEOF_VOID_P == 8
149 # define POPL() (sp -= 2, (jlong) sp->l)
150 # define POPD() (sp -= 2, (jdouble) sp->d)
152 # define POPL() ({ _Jv_word2 w2; \
153 w2.ia[1] = (--sp)->ia[0]; \
154 w2.ia[0] = (--sp)->ia[0]; w2.l; })
155 # define POPD() ({ _Jv_word2 w2; \
156 w2.ia[1] = (--sp)->ia[0]; \
157 w2.ia[0] = (--sp)->ia[0]; w2.d; })
160 #define LOADA(I) (sp++)->o = locals[I].o
161 #define LOADI(I) (sp++)->i = locals[I].i
162 #define LOADF(I) (sp++)->f = locals[I].f
163 #if SIZEOF_VOID_P == 8
164 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
165 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
167 # define LOADL(I) do { jint __idx = (I); \
168 (sp++)->ia[0] = locals[__idx].ia[0]; \
169 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
171 # define LOADD(I) LOADL(I)
176 DEBUG_LOCALS_INSN (I, 'o'); \
177 locals[I].o = (--sp)->o; \
181 DEBUG_LOCALS_INSN (I, 'i'); \
182 locals[I].i = (--sp)->i; \
186 DEBUG_LOCALS_INSN (I, 'f'); \
187 locals[I].f = (--sp)->f; \
189 #if SIZEOF_VOID_P == 8
192 DEBUG_LOCALS_INSN (I, 'l'); \
193 (sp -= 2, locals[I].l = sp->l); \
197 DEBUG_LOCALS_INSN (I, 'd'); \
198 (sp -= 2, locals[I].d = sp->d); \
204 DEBUG_LOCALS_INSN (I, 'l'); \
206 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
207 locals[__idx].ia[0] = (--sp)->ia[0]; \
211 DEBUG_LOCALS_INSN(I, 'd'); \
213 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
214 locals[__idx].ia[0] = (--sp)->ia[0]; \
218 #define PEEKI(I) (locals+(I))->i
219 #define PEEKA(I) (locals+(I))->o
222 DEBUG_LOCALS_INSN(I,'i'); \
223 ((locals+(I))->i = (V))
226 #define BINOPI(OP) { \
227 jint value2 = POPI(); \
228 jint value1 = POPI(); \
229 PUSHI(value1 OP value2); \
232 #define BINOPF(OP) { \
233 jfloat value2 = POPF(); \
234 jfloat value1 = POPF(); \
235 PUSHF(value1 OP value2); \
238 #define BINOPL(OP) { \
239 jlong value2 = POPL(); \
240 jlong value1 = POPL(); \
241 PUSHL(value1 OP value2); \
244 #define BINOPD(OP) { \
245 jdouble value2 = POPD(); \
246 jdouble value1 = POPD(); \
247 PUSHD(value1 OP value2); \
251 get1s (unsigned char* loc)
253 return *(signed char*)loc;
257 get1u (unsigned char* loc)
263 get2s(unsigned char* loc)
265 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
269 get2u (unsigned char* loc)
271 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
275 get4 (unsigned char* loc)
277 return (((jint)(loc[0])) << 24)
278 | (((jint)(loc[1])) << 16)
279 | (((jint)(loc[2])) << 8)
280 | (((jint)(loc[3])) << 0);
283 #define SAVE_PC() frame_desc.pc = pc
285 // We used to define this conditionally, depending on HANDLE_SEGV.
286 // However, that runs into a problem if a chunk in low memory is
287 // mapped and we try to look at a field near the end of a large
288 // object. See PR 26858 for details. It is, most likely, relatively
289 // inexpensive to simply do this check always.
290 #define NULLCHECK(X) \
291 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
293 // Note that we can still conditionally define NULLARRAYCHECK, since
294 // we know that all uses of an array will first reference the length
295 // field, which is first -- and thus will trigger a SEGV.
297 #define NULLARRAYCHECK(X) SAVE_PC()
299 #define NULLARRAYCHECK(X) \
303 if ((X) == NULL) { throw_null_pointer_exception (); } \
307 #define ARRAYBOUNDSCHECK(array, index) \
310 if (((unsigned) index) >= (unsigned) (array->length)) \
311 _Jv_ThrowBadArrayIndex (index); \
315 _Jv_InterpMethod::run_normal (ffi_cif *,
320 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
321 run (ret, args, _this);
325 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
330 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
331 run_debug (ret, args, _this);
335 _Jv_InterpMethod::run_synch_object (ffi_cif *,
340 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
342 jobject rcv = (jobject) args[0].ptr;
343 JvSynchronize mutex (rcv);
345 run (ret, args, _this);
349 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
354 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
356 jobject rcv = (jobject) args[0].ptr;
357 JvSynchronize mutex (rcv);
359 run_debug (ret, args, _this);
363 _Jv_InterpMethod::run_class (ffi_cif *,
368 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
369 _Jv_InitClass (_this->defining_class);
370 run (ret, args, _this);
374 _Jv_InterpMethod::run_class_debug (ffi_cif *,
379 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
380 _Jv_InitClass (_this->defining_class);
381 run_debug (ret, args, _this);
385 _Jv_InterpMethod::run_synch_class (ffi_cif *,
390 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
392 jclass sync = _this->defining_class;
393 _Jv_InitClass (sync);
394 JvSynchronize mutex (sync);
396 run (ret, args, _this);
400 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
405 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
407 jclass sync = _this->defining_class;
408 _Jv_InitClass (sync);
409 JvSynchronize mutex (sync);
411 run_debug (ret, args, _this);
414 #ifdef DIRECT_THREADED
415 // "Compile" a method by turning it from bytecode to direct-threaded
418 _Jv_InterpMethod::compile (const void * const *insn_targets)
420 insn_slot *insns = NULL;
422 unsigned char *codestart = bytecode ();
423 unsigned char *end = codestart + code_length;
424 _Jv_word *pool_data = defining_class->constants.data;
426 #define SET_ONE(Field, Value) \
432 insns[next++].Field = Value; \
436 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
437 #define SET_INT(Value) SET_ONE (int_val, Value)
438 #define SET_DATUM(Value) SET_ONE (datum, Value)
440 // Map from bytecode PC to slot in INSNS.
441 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
442 for (int i = 0; i < code_length; ++i)
445 for (int i = 0; i < 2; ++i)
447 jboolean first_pass = i == 0;
451 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
452 number_insn_slots = next;
456 unsigned char *pc = codestart;
459 int base_pc_val = pc - codestart;
461 pc_mapping[base_pc_val] = next;
463 java_opcode opcode = (java_opcode) *pc++;
465 if (opcode == op_nop)
467 SET_INSN (insn_targets[opcode]);
608 case op_monitorenter:
618 // No argument, nothing else to do.
622 SET_INT (get1s (pc));
628 int index = get1u (pc);
630 // For an unresolved class we want to delay resolution
632 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
635 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
639 SET_DATUM (pool_data[index].o);
655 SET_INT (get1u (pc));
660 SET_INT (get1u (pc));
661 SET_INT (get1s (pc + 1));
667 int index = get2u (pc);
669 // For an unresolved class we want to delay resolution
671 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
674 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
678 SET_DATUM (pool_data[index].o);
684 int index = get2u (pc);
686 SET_DATUM (&pool_data[index]);
691 SET_INT (get2s (pc));
703 case op_invokespecial:
704 case op_invokestatic:
705 case op_invokevirtual:
706 SET_INT (get2u (pc));
710 case op_multianewarray:
711 SET_INT (get2u (pc));
712 SET_INT (get1u (pc + 2));
735 int offset = get2s (pc);
738 int new_pc = base_pc_val + offset;
740 bool orig_was_goto = opcode == op_goto;
742 // Thread jumps. We limit the loop count; this lets
743 // us avoid infinite loops if the bytecode contains
744 // such. `10' is arbitrary.
746 while (codestart[new_pc] == op_goto && count-- > 0)
747 new_pc += get2s (&codestart[new_pc + 1]);
749 // If the jump takes us to a `return' instruction and
750 // the original branch was an unconditional goto, then
751 // we hoist the return.
752 opcode = (java_opcode) codestart[new_pc];
754 && (opcode == op_ireturn || opcode == op_lreturn
755 || opcode == op_freturn || opcode == op_dreturn
756 || opcode == op_areturn || opcode == op_return))
759 SET_INSN (insn_targets[opcode]);
762 SET_DATUM (&insns[pc_mapping[new_pc]]);
768 while ((pc - codestart) % 4 != 0)
771 jint def = get4 (pc);
772 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
778 int high = get4 (pc);
782 for (int i = low; i <= high; ++i)
784 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
790 case op_lookupswitch:
792 while ((pc - codestart) % 4 != 0)
795 jint def = get4 (pc);
796 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
799 jint npairs = get4 (pc);
805 jint match = get4 (pc);
806 jint offset = get4 (pc + 4);
808 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
814 case op_invokeinterface:
816 jint index = get2u (pc);
818 // We ignore the next two bytes.
826 opcode = (java_opcode) get1u (pc);
828 jint val = get2u (pc);
831 // We implement narrow and wide instructions using the
832 // same code in the interpreter. So we rewrite the
833 // instruction slot here.
835 insns[next - 1].insn = (void *) insn_targets[opcode];
838 if (opcode == op_iinc)
840 SET_INT (get2s (pc));
849 jint offset = get4 (pc);
851 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
855 // Some "can't happen" cases that we include for
856 // error-checking purposes.
874 case op_getstatic_2s:
875 case op_getstatic_2u:
887 // Now update exceptions.
888 _Jv_InterpException *exc = exceptions ();
889 for (int i = 0; i < exc_count; ++i)
891 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
892 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
893 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
894 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
895 // during compilation.
897 = (_Jv_Linker::resolve_pool_entry (defining_class,
898 exc[i].handler_type.i)).clazz;
899 exc[i].handler_type.p = handler;
902 // Translate entries in the LineNumberTable from bytecode PC's to direct
903 // threaded interpreter instruction values.
904 for (int i = 0; i < line_table_len; i++)
906 int byte_pc = line_table[i].bytecode_pc;
907 // It isn't worth throwing an exception if this table is
908 // corrupted, but at the same time we don't want a crash.
909 if (byte_pc < 0 || byte_pc >= code_length)
911 line_table[i].pc = &insns[pc_mapping[byte_pc]];
916 if (breakpoint_insn == NULL)
918 bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
919 breakpoint_insn = &bp_insn_slot;
922 #endif /* DIRECT_THREADED */
924 /* Run the given method.
925 When args is NULL, don't run anything -- just compile it. */
927 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
930 #undef DEBUG_LOCALS_INSN
931 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
933 #include "interpret-run.cc"
937 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
940 #undef DEBUG_LOCALS_INSN
941 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
943 #include "interpret-run.cc"
947 throw_internal_error (const char *msg)
949 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
953 throw_incompatible_class_change_error (jstring msg)
955 throw new java::lang::IncompatibleClassChangeError (msg);
959 throw_null_pointer_exception ()
961 throw new java::lang::NullPointerException;
964 /* Look up source code line number for given bytecode (or direct threaded
967 _Jv_InterpMethod::get_source_line(pc_t mpc)
969 int line = line_table_len > 0 ? line_table[0].line : -1;
970 for (int i = 1; i < line_table_len; i++)
971 if (line_table[i].pc > mpc)
974 line = line_table[i].line;
979 /** Do static initialization for fields with a constant initializer */
981 _Jv_InitField (jobject obj, jclass klass, int index)
983 using namespace java::lang::reflect;
985 if (obj != 0 && klass == 0)
986 klass = obj->getClass ();
988 if (!_Jv_IsInterpretedClass (klass))
991 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
993 _Jv_Field * field = (&klass->fields[0]) + index;
995 if (index > klass->field_count)
996 throw_internal_error ("field out of range");
998 int init = iclass->field_initializers[index];
1002 _Jv_Constants *pool = &klass->constants;
1003 int tag = pool->tags[init];
1005 if (! field->isResolved ())
1006 throw_internal_error ("initializing unresolved field");
1008 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
1009 throw_internal_error ("initializing non-static field with no object");
1013 if ((field->flags & Modifier::STATIC) != 0)
1014 addr = (void*) field->u.addr;
1016 addr = (void*) (((char*)obj) + field->u.boffset);
1020 case JV_CONSTANT_String:
1023 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1024 pool->data[init].string = str;
1025 pool->tags[init] = JV_CONSTANT_ResolvedString;
1029 case JV_CONSTANT_ResolvedString:
1030 if (! (field->type == &java::lang::String::class$
1031 || field->type == &java::lang::Class::class$))
1032 throw_class_format_error ("string initialiser to non-string field");
1034 *(jstring*)addr = pool->data[init].string;
1037 case JV_CONSTANT_Integer:
1039 int value = pool->data[init].i;
1041 if (field->type == JvPrimClass (boolean))
1042 *(jboolean*)addr = (jboolean)value;
1044 else if (field->type == JvPrimClass (byte))
1045 *(jbyte*)addr = (jbyte)value;
1047 else if (field->type == JvPrimClass (char))
1048 *(jchar*)addr = (jchar)value;
1050 else if (field->type == JvPrimClass (short))
1051 *(jshort*)addr = (jshort)value;
1053 else if (field->type == JvPrimClass (int))
1054 *(jint*)addr = (jint)value;
1057 throw_class_format_error ("erroneous field initializer");
1061 case JV_CONSTANT_Long:
1062 if (field->type != JvPrimClass (long))
1063 throw_class_format_error ("erroneous field initializer");
1065 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1068 case JV_CONSTANT_Float:
1069 if (field->type != JvPrimClass (float))
1070 throw_class_format_error ("erroneous field initializer");
1072 *(jfloat*)addr = pool->data[init].f;
1075 case JV_CONSTANT_Double:
1076 if (field->type != JvPrimClass (double))
1077 throw_class_format_error ("erroneous field initializer");
1079 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1083 throw_class_format_error ("erroneous field initializer");
1087 inline static unsigned char*
1088 skip_one_type (unsigned char* ptr)
1099 do { ch = *ptr++; } while (ch != ';');
1106 get_ffi_type_from_signature (unsigned char* ptr)
1112 return &ffi_type_pointer;
1116 // On some platforms a bool is a byte, on others an int.
1117 if (sizeof (jboolean) == sizeof (jbyte))
1118 return &ffi_type_sint8;
1121 JvAssert (sizeof (jbyte) == sizeof (jint));
1122 return &ffi_type_sint32;
1127 return &ffi_type_sint8;
1131 return &ffi_type_uint16;
1135 return &ffi_type_sint16;
1139 return &ffi_type_sint32;
1143 return &ffi_type_sint64;
1147 return &ffi_type_float;
1151 return &ffi_type_double;
1155 return &ffi_type_void;
1159 throw_internal_error ("unknown type in signature");
1162 /* this function yields the number of actual arguments, that is, if the
1163 * function is non-static, then one is added to the number of elements
1164 * found in the signature */
1167 _Jv_count_arguments (_Jv_Utf8Const *signature,
1170 unsigned char *ptr = (unsigned char*) signature->chars();
1171 int arg_count = staticp ? 0 : 1;
1173 /* first, count number of arguments */
1181 ptr = skip_one_type (ptr);
1188 /* This beast will build a cif, given the signature. Memory for
1189 * the cif itself and for the argument types must be allocated by the
1194 _Jv_init_cif (_Jv_Utf8Const* signature,
1198 ffi_type **arg_types,
1201 unsigned char *ptr = (unsigned char*) signature->chars();
1203 int arg_index = 0; // arg number
1204 int item_count = 0; // stack-item count
1209 arg_types[arg_index++] = &ffi_type_pointer;
1219 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1221 if (*ptr == 'J' || *ptr == 'D')
1226 ptr = skip_one_type (ptr);
1231 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1233 ptr = skip_one_type (ptr);
1234 if (ptr != (unsigned char*)signature->chars() + signature->len())
1235 throw_internal_error ("did not find end of signature");
1237 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1238 arg_count, rtype, arg_types) != FFI_OK)
1239 throw_internal_error ("ffi_prep_cif failed");
1241 if (rtype_p != NULL)
1247 #if FFI_NATIVE_RAW_API
1248 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1249 # define FFI_RAW_SIZE ffi_raw_size
1251 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1252 # define FFI_RAW_SIZE ffi_java_raw_size
1255 /* we put this one here, and not in interpret.cc because it
1256 * calls the utility routines _Jv_count_arguments
1257 * which are static to this module. The following struct defines the
1258 * layout we use for the stubs, it's only used in the ncode method. */
1261 ffi_raw_closure closure;
1263 ffi_type *arg_types[0];
1266 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1269 _Jv_InterpMethod::ncode ()
1271 using namespace java::lang::reflect;
1273 if (self->ncode != 0)
1276 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1277 int arg_count = _Jv_count_arguments (self->signature, staticp);
1279 ncode_closure *closure =
1280 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1281 + arg_count * sizeof (ffi_type*));
1283 _Jv_init_cif (self->signature,
1287 &closure->arg_types[0],
1290 ffi_closure_fun fun;
1292 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1294 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1296 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1301 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1303 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1308 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1310 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1318 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1320 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1325 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1327 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1331 FFI_PREP_RAW_CLOSURE (&closure->closure,
1336 self->ncode = (void*)closure;
1340 /* Find the index of the given insn in the array of insn slots
1341 for this method. Returns -1 if not found. */
1343 _Jv_InterpMethod::insn_index (pc_t pc)
1346 #ifdef DIRECT_THREADED
1347 jlong right = number_insn_slots;
1348 pc_t insns = prepared;
1350 jlong right = code_length;
1351 pc_t insns = bytecode ();
1356 jlong mid = (left + right) / 2;
1357 if (&insns[mid] == pc)
1360 if (pc < &insns[mid])
1370 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1371 jintArray& line_numbers,
1372 jlongArray& code_indices)
1374 #ifdef DIRECT_THREADED
1375 /* For the DIRECT_THREADED case, if the method has not yet been
1376 * compiled, the linetable will change to insn slots instead of
1377 * bytecode PCs. It is probably easiest, in this case, to simply
1378 * compile the method and guarantee that we are using insn
1381 _Jv_CompileMethod (this);
1383 if (line_table_len > 0)
1386 end = number_insn_slots;
1387 line_numbers = JvNewIntArray (line_table_len);
1388 code_indices = JvNewLongArray (line_table_len);
1390 jint* lines = elements (line_numbers);
1391 jlong* indices = elements (code_indices);
1392 for (int i = 0; i < line_table_len; ++i)
1394 lines[i] = line_table[i].line;
1395 indices[i] = insn_index (line_table[i].pc);
1398 #else // !DIRECT_THREADED
1399 if (line_table_len > 0)
1403 line_numbers = JvNewIntArray (line_table_len);
1404 code_indices = JvNewLongArray (line_table_len);
1406 jint* lines = elements (line_numbers);
1407 jlong* indices = elements (code_indices);
1408 for (int i = 0; i < line_table_len; ++i)
1410 lines[i] = line_table[i].line;
1411 indices[i] = (jlong) line_table[i].bytecode_pc;
1414 #endif // !DIRECT_THREADED
1418 _Jv_InterpMethod::install_break (jlong index)
1420 return set_insn (index, breakpoint_insn);
1424 _Jv_InterpMethod::get_insn (jlong index)
1428 #ifdef DIRECT_THREADED
1429 if (index >= number_insn_slots || index < 0)
1433 #else // !DIRECT_THREADED
1434 if (index >= code_length || index < 0)
1437 code = reinterpret_cast<pc_t> (bytecode ());
1438 #endif // !DIRECT_THREADED
1440 return &code[index];
1444 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1446 #ifdef DIRECT_THREADED
1447 if (index >= number_insn_slots || index < 0)
1450 pc_t code = prepared;
1451 code[index].insn = insn->insn;
1452 #else // !DIRECT_THREADED
1453 if (index >= code_length || index < 0)
1456 pc_t code = reinterpret_cast<pc_t> (bytecode ());
1457 code[index] = *insn;
1458 #endif // !DIRECT_THREADED
1460 return &code[index];
1464 _Jv_JNIMethod::ncode ()
1466 using namespace java::lang::reflect;
1468 if (self->ncode != 0)
1471 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1472 int arg_count = _Jv_count_arguments (self->signature, staticp);
1474 ncode_closure *closure =
1475 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1476 + arg_count * sizeof (ffi_type*));
1479 _Jv_init_cif (self->signature,
1483 &closure->arg_types[0],
1486 ffi_closure_fun fun;
1488 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1490 // Initialize the argument types and CIF that represent the actual
1491 // underlying JNI function.
1493 if ((self->accflags & Modifier::STATIC))
1495 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1496 * sizeof (ffi_type *));
1498 jni_arg_types[offset++] = &ffi_type_pointer;
1499 if ((self->accflags & Modifier::STATIC))
1500 jni_arg_types[offset++] = &ffi_type_pointer;
1501 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1502 arg_count * sizeof (ffi_type *));
1504 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1505 extra_args + arg_count, rtype,
1506 jni_arg_types) != FFI_OK)
1507 throw_internal_error ("ffi_prep_cif failed for JNI function");
1509 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1511 // FIXME: for now we assume that all native methods for
1512 // interpreted code use JNI.
1513 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1515 FFI_PREP_RAW_CLOSURE (&closure->closure,
1520 self->ncode = (void *) closure;
1525 throw_class_format_error (jstring msg)
1528 ? new java::lang::ClassFormatError (msg)
1529 : new java::lang::ClassFormatError);
1533 throw_class_format_error (const char *msg)
1535 throw_class_format_error (JvNewStringLatin1 (msg));
1541 _Jv_InterpreterEngine::do_verify (jclass klass)
1543 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1544 for (int i = 0; i < klass->method_count; i++)
1546 using namespace java::lang::reflect;
1547 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1548 _Jv_ushort accflags = klass->methods[i].accflags;
1549 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1551 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1552 _Jv_VerifyMethod (im);
1558 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1560 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1561 for (int i = 0; i < klass->method_count; i++)
1563 // Just skip abstract methods. This is particularly important
1564 // because we don't resize the interpreted_methods array when
1565 // miranda methods are added to it.
1566 if ((klass->methods[i].accflags
1567 & java::lang::reflect::Modifier::ABSTRACT)
1571 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1573 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1576 // You might think we could use a virtual `ncode' method in
1577 // the _Jv_MethodBase and unify the native and non-native
1578 // cases. Well, we can't, because we don't allocate these
1579 // objects using `new', and thus they don't get a vtable.
1580 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1581 klass->methods[i].ncode = jnim->ncode ();
1583 else if (imeth != 0) // it could be abstract
1585 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1586 klass->methods[i].ncode = im->ncode ();
1592 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1596 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1598 // Splitting the allocations here lets us scan reference fields and
1599 // avoid scanning non-reference fields. How reference fields are
1600 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1601 // means that this memory will be scanned conservatively (same
1602 // difference, since we know all the contents here are pointers).
1603 // Then we put pointers into this memory into the 'fields'
1604 // structure. Most of these are interior pointers, which is ok (but
1605 // even so the pointer to the first reference field will be used and
1606 // that is not an interior pointer). The 'fields' array is also
1607 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1608 // be scanned. A pointer to this array is held by Class and thus
1609 // seen by the collector.
1610 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1611 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1613 for (int i = 0; i < klass->field_count; i++)
1615 _Jv_Field *field = &klass->fields[i];
1617 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1620 char *base = field->isRef() ? reference_fields : non_reference_fields;
1621 field->u.addr = base + field->u.boffset;
1623 if (iclass->field_initializers[i] != 0)
1625 _Jv_Linker::resolve_field (field, klass->loader);
1626 _Jv_InitField (0, klass, i);
1630 // Now we don't need the field_initializers anymore, so let the
1631 // collector get rid of it.
1632 iclass->field_initializers = 0;
1635 _Jv_ResolvedMethod *
1636 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1639 int arg_count = _Jv_count_arguments (method->signature, staticp);
1641 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1642 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1643 + arg_count*sizeof (ffi_type*));
1645 result->stack_item_count
1646 = _Jv_init_cif (method->signature,
1650 &result->arg_types[0],
1653 result->method = method;
1654 result->klass = klass;
1660 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1662 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1663 for (int i = 0; i < klass->method_count; i++)
1665 // Just skip abstract methods. This is particularly important
1666 // because we don't resize the interpreted_methods array when
1667 // miranda methods are added to it.
1668 if ((klass->methods[i].accflags
1669 & java::lang::reflect::Modifier::ABSTRACT)
1672 // Miranda method additions mean that the `methods' array moves.
1673 // We cache a pointer into this array, so we have to update.
1674 iclass->interpreted_methods[i]->self = &klass->methods[i];
1678 #ifdef DIRECT_THREADED
1680 _Jv_CompileMethod (_Jv_InterpMethod* method)
1682 if (method->prepared == NULL)
1685 _Jv_InterpMethod::run_debug (NULL, NULL, method);
1687 _Jv_InterpMethod::run (NULL, NULL, method);
1690 #endif // DIRECT_THREADED
1692 #endif // INTERPRETER