1 // interpret.cc - Code for the interpreter
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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>
40 #include <gnu/classpath/jdwp/Jdwp.h>
44 // Execution engine for interpreted code.
45 _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
51 static void throw_internal_error (const char *msg)
52 __attribute__ ((__noreturn__));
53 static void throw_incompatible_class_change_error (jstring msg)
54 __attribute__ ((__noreturn__));
55 static void throw_null_pointer_exception ()
56 __attribute__ ((__noreturn__));
58 static void throw_class_format_error (jstring msg)
59 __attribute__ ((__noreturn__));
60 static void throw_class_format_error (const 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 // The breakpoint instruction. For the direct threaded case,
79 // _Jv_InterpMethod::compile will initialize breakpoint_insn
80 // the first time it is called.
81 #ifdef DIRECT_THREADED
82 insn_slot _Jv_InterpMethod::bp_insn_slot;
83 pc_t _Jv_InterpMethod::breakpoint_insn = NULL;
85 unsigned char _Jv_InterpMethod::bp_insn_opcode
86 = static_cast<unsigned char> (op_breakpoint);
87 pc_t _Jv_InterpMethod::breakpoint_insn = &_Jv_InterpMethod::bp_insn_opcode;
90 extern "C" double __ieee754_fmod (double,double);
92 static inline void dupx (_Jv_word *sp, int n, int x)
94 // first "slide" n+x elements n to the right
96 for (int i = 0; i < n+x; i++)
98 sp[(top-i)] = sp[(top-i)-n];
101 // next, copy the n top elements, n+x down
102 for (int i = 0; i < n; i++)
104 sp[top-(n+x)-i] = sp[top-i];
108 // Used to convert from floating types to integral types.
109 template<typename TO, typename FROM>
111 convert (FROM val, TO min, TO max)
114 if (val >= (FROM) max)
116 else if (val <= (FROM) min)
125 #define PUSHA(V) (sp++)->o = (V)
126 #define PUSHI(V) (sp++)->i = (V)
127 #define PUSHF(V) (sp++)->f = (V)
128 #if SIZEOF_VOID_P == 8
129 # define PUSHL(V) (sp->l = (V), sp += 2)
130 # define PUSHD(V) (sp->d = (V), sp += 2)
132 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
133 (sp++)->ia[0] = w2.ia[0]; \
134 (sp++)->ia[0] = w2.ia[1]; } while (0)
135 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
136 (sp++)->ia[0] = w2.ia[0]; \
137 (sp++)->ia[0] = w2.ia[1]; } while (0)
140 #define POPA() ((--sp)->o)
141 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
142 #define POPF() ((jfloat) (--sp)->f)
143 #if SIZEOF_VOID_P == 8
144 # define POPL() (sp -= 2, (jlong) sp->l)
145 # define POPD() (sp -= 2, (jdouble) sp->d)
147 # define POPL() ({ _Jv_word2 w2; \
148 w2.ia[1] = (--sp)->ia[0]; \
149 w2.ia[0] = (--sp)->ia[0]; w2.l; })
150 # define POPD() ({ _Jv_word2 w2; \
151 w2.ia[1] = (--sp)->ia[0]; \
152 w2.ia[0] = (--sp)->ia[0]; w2.d; })
155 #define LOADA(I) (sp++)->o = locals[I].o
156 #define LOADI(I) (sp++)->i = locals[I].i
157 #define LOADF(I) (sp++)->f = locals[I].f
158 #if SIZEOF_VOID_P == 8
159 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
160 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
162 # define LOADL(I) do { jint __idx = (I); \
163 (sp++)->ia[0] = locals[__idx].ia[0]; \
164 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
166 # define LOADD(I) LOADL(I)
171 DEBUG_LOCALS_INSN(I, 'o'); \
172 locals[I].o = (--sp)->o; \
176 DEBUG_LOCALS_INSN (I, 'i'); \
177 locals[I].i = (--sp)->i; \
181 DEBUG_LOCALS_INSN (I, 'f'); \
182 locals[I].f = (--sp)->f; \
184 #if SIZEOF_VOID_P == 8
187 DEBUG_LOCALS_INSN (I, 'l'); \
188 (sp -= 2, locals[I].l = sp->l); \
192 DEBUG_LOCALS_INSN (I, 'd'); \
193 (sp -= 2, locals[I].d = sp->d); \
198 do { DEBUG_LOCALS_INSN(I, 'l'); \
200 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
201 locals[__idx].ia[0] = (--sp)->ia[0]; \
204 do { DEBUG_LOCALS_INSN(I, 'd'); \
206 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
207 locals[__idx].ia[0] = (--sp)->ia[0]; \
211 #define PEEKI(I) (locals+(I))->i
212 #define PEEKA(I) (locals+(I))->o
215 DEBUG_LOCALS_INSN(I,'i'); \
216 ((locals+(I))->i = (V))
219 #define BINOPI(OP) { \
220 jint value2 = POPI(); \
221 jint value1 = POPI(); \
222 PUSHI(value1 OP value2); \
225 #define BINOPF(OP) { \
226 jfloat value2 = POPF(); \
227 jfloat value1 = POPF(); \
228 PUSHF(value1 OP value2); \
231 #define BINOPL(OP) { \
232 jlong value2 = POPL(); \
233 jlong value1 = POPL(); \
234 PUSHL(value1 OP value2); \
237 #define BINOPD(OP) { \
238 jdouble value2 = POPD(); \
239 jdouble value1 = POPD(); \
240 PUSHD(value1 OP value2); \
243 static inline jint get1s(unsigned char* loc) {
244 return *(signed char*)loc;
247 static inline jint get1u(unsigned char* loc) {
251 static inline jint get2s(unsigned char* loc) {
252 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
255 static inline jint get2u(unsigned char* loc) {
256 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
259 static jint get4(unsigned char* loc) {
260 return (((jint)(loc[0])) << 24)
261 | (((jint)(loc[1])) << 16)
262 | (((jint)(loc[2])) << 8)
263 | (((jint)(loc[3])) << 0);
266 #define SAVE_PC() frame_desc.pc = pc
268 // We used to define this conditionally, depending on HANDLE_SEGV.
269 // However, that runs into a problem if a chunk in low memory is
270 // mapped and we try to look at a field near the end of a large
271 // object. See PR 26858 for details. It is, most likely, relatively
272 // inexpensive to simply do this check always.
273 #define NULLCHECK(X) \
274 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
276 // Note that we can still conditionally define NULLARRAYCHECK, since
277 // we know that all uses of an array will first reference the length
278 // field, which is first -- and thus will trigger a SEGV.
280 #define NULLARRAYCHECK(X) SAVE_PC()
282 #define NULLARRAYCHECK(X) \
283 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
286 #define ARRAYBOUNDSCHECK(array, index) \
289 if (((unsigned) index) >= (unsigned) (array->length)) \
290 _Jv_ThrowBadArrayIndex (index); \
295 _Jv_InterpMethod::run_normal (ffi_cif *,
300 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
301 run (ret, args, _this);
305 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
310 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
311 run_debug (ret, args, _this);
315 _Jv_InterpMethod::run_synch_object (ffi_cif *,
320 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
322 jobject rcv = (jobject) args[0].ptr;
323 JvSynchronize mutex (rcv);
325 run (ret, args, _this);
329 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
334 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
336 jobject rcv = (jobject) args[0].ptr;
337 JvSynchronize mutex (rcv);
339 run_debug (ret, args, _this);
343 _Jv_InterpMethod::run_class (ffi_cif *,
348 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
349 _Jv_InitClass (_this->defining_class);
350 run (ret, args, _this);
354 _Jv_InterpMethod::run_class_debug (ffi_cif *,
359 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
360 _Jv_InitClass (_this->defining_class);
361 run_debug (ret, args, _this);
365 _Jv_InterpMethod::run_synch_class (ffi_cif *,
370 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
372 jclass sync = _this->defining_class;
373 _Jv_InitClass (sync);
374 JvSynchronize mutex (sync);
376 run (ret, args, _this);
380 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
385 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
387 jclass sync = _this->defining_class;
388 _Jv_InitClass (sync);
389 JvSynchronize mutex (sync);
391 run_debug (ret, args, _this);
394 #ifdef DIRECT_THREADED
395 // "Compile" a method by turning it from bytecode to direct-threaded
398 _Jv_InterpMethod::compile (const void * const *insn_targets)
400 insn_slot *insns = NULL;
402 unsigned char *codestart = bytecode ();
403 unsigned char *end = codestart + code_length;
404 _Jv_word *pool_data = defining_class->constants.data;
406 #define SET_ONE(Field, Value) \
412 insns[next++].Field = Value; \
416 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
417 #define SET_INT(Value) SET_ONE (int_val, Value)
418 #define SET_DATUM(Value) SET_ONE (datum, Value)
420 // Map from bytecode PC to slot in INSNS.
421 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
422 for (int i = 0; i < code_length; ++i)
425 for (int i = 0; i < 2; ++i)
427 jboolean first_pass = i == 0;
431 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
432 number_insn_slots = next;
436 unsigned char *pc = codestart;
439 int base_pc_val = pc - codestart;
441 pc_mapping[base_pc_val] = next;
443 java_opcode opcode = (java_opcode) *pc++;
445 if (opcode == op_nop)
447 SET_INSN (insn_targets[opcode]);
588 case op_monitorenter:
598 // No argument, nothing else to do.
602 SET_INT (get1s (pc));
608 int index = get1u (pc);
610 // For an unresolved class we want to delay resolution
612 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
615 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
619 SET_DATUM (pool_data[index].o);
635 SET_INT (get1u (pc));
640 SET_INT (get1u (pc));
641 SET_INT (get1s (pc + 1));
647 int index = get2u (pc);
649 // For an unresolved class we want to delay resolution
651 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
654 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
658 SET_DATUM (pool_data[index].o);
664 int index = get2u (pc);
666 SET_DATUM (&pool_data[index]);
671 SET_INT (get2s (pc));
683 case op_invokespecial:
684 case op_invokestatic:
685 case op_invokevirtual:
686 SET_INT (get2u (pc));
690 case op_multianewarray:
691 SET_INT (get2u (pc));
692 SET_INT (get1u (pc + 2));
715 int offset = get2s (pc);
718 int new_pc = base_pc_val + offset;
720 bool orig_was_goto = opcode == op_goto;
722 // Thread jumps. We limit the loop count; this lets
723 // us avoid infinite loops if the bytecode contains
724 // such. `10' is arbitrary.
726 while (codestart[new_pc] == op_goto && count-- > 0)
727 new_pc += get2s (&codestart[new_pc + 1]);
729 // If the jump takes us to a `return' instruction and
730 // the original branch was an unconditional goto, then
731 // we hoist the return.
732 opcode = (java_opcode) codestart[new_pc];
734 && (opcode == op_ireturn || opcode == op_lreturn
735 || opcode == op_freturn || opcode == op_dreturn
736 || opcode == op_areturn || opcode == op_return))
739 SET_INSN (insn_targets[opcode]);
742 SET_DATUM (&insns[pc_mapping[new_pc]]);
748 while ((pc - codestart) % 4 != 0)
751 jint def = get4 (pc);
752 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
758 int high = get4 (pc);
762 for (int i = low; i <= high; ++i)
764 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
770 case op_lookupswitch:
772 while ((pc - codestart) % 4 != 0)
775 jint def = get4 (pc);
776 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
779 jint npairs = get4 (pc);
785 jint match = get4 (pc);
786 jint offset = get4 (pc + 4);
788 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
794 case op_invokeinterface:
796 jint index = get2u (pc);
798 // We ignore the next two bytes.
806 opcode = (java_opcode) get1u (pc);
808 jint val = get2u (pc);
811 // We implement narrow and wide instructions using the
812 // same code in the interpreter. So we rewrite the
813 // instruction slot here.
815 insns[next - 1].insn = (void *) insn_targets[opcode];
818 if (opcode == op_iinc)
820 SET_INT (get2s (pc));
829 jint offset = get4 (pc);
831 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
835 // Some "can't happen" cases that we include for
836 // error-checking purposes.
854 case op_getstatic_2s:
855 case op_getstatic_2u:
867 // Now update exceptions.
868 _Jv_InterpException *exc = exceptions ();
869 for (int i = 0; i < exc_count; ++i)
871 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
872 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
873 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
874 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
875 // during compilation.
877 = (_Jv_Linker::resolve_pool_entry (defining_class,
878 exc[i].handler_type.i)).clazz;
879 exc[i].handler_type.p = handler;
882 // Translate entries in the LineNumberTable from bytecode PC's to direct
883 // threaded interpreter instruction values.
884 for (int i = 0; i < line_table_len; i++)
886 int byte_pc = line_table[i].bytecode_pc;
887 // It isn't worth throwing an exception if this table is
888 // corrupted, but at the same time we don't want a crash.
889 if (byte_pc < 0 || byte_pc >= code_length)
891 line_table[i].pc = &insns[pc_mapping[byte_pc]];
896 if (breakpoint_insn == NULL)
898 bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
899 breakpoint_insn = &bp_insn_slot;
902 #endif /* DIRECT_THREADED */
904 /* Run the given method.
905 When args is NULL, don't run anything -- just compile it. */
907 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
910 #undef DEBUG_LOCALS_INSN
911 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
913 #include "interpret-run.cc"
917 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
919 /* Used to keep track of local variable type
929 #undef DEBUG_LOCALS_INSN
930 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
932 #include "interpret-run.cc"
936 throw_internal_error (const char *msg)
938 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
942 throw_incompatible_class_change_error (jstring msg)
944 throw new java::lang::IncompatibleClassChangeError (msg);
948 throw_null_pointer_exception ()
950 throw new java::lang::NullPointerException;
953 /* Look up source code line number for given bytecode (or direct threaded
956 _Jv_InterpMethod::get_source_line(pc_t mpc)
958 int line = line_table_len > 0 ? line_table[0].line : -1;
959 for (int i = 1; i < line_table_len; i++)
960 if (line_table[i].pc > mpc)
963 line = line_table[i].line;
968 /** Do static initialization for fields with a constant initializer */
970 _Jv_InitField (jobject obj, jclass klass, int index)
972 using namespace java::lang::reflect;
974 if (obj != 0 && klass == 0)
975 klass = obj->getClass ();
977 if (!_Jv_IsInterpretedClass (klass))
980 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
982 _Jv_Field * field = (&klass->fields[0]) + index;
984 if (index > klass->field_count)
985 throw_internal_error ("field out of range");
987 int init = iclass->field_initializers[index];
991 _Jv_Constants *pool = &klass->constants;
992 int tag = pool->tags[init];
994 if (! field->isResolved ())
995 throw_internal_error ("initializing unresolved field");
997 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
998 throw_internal_error ("initializing non-static field with no object");
1002 if ((field->flags & Modifier::STATIC) != 0)
1003 addr = (void*) field->u.addr;
1005 addr = (void*) (((char*)obj) + field->u.boffset);
1009 case JV_CONSTANT_String:
1012 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1013 pool->data[init].string = str;
1014 pool->tags[init] = JV_CONSTANT_ResolvedString;
1018 case JV_CONSTANT_ResolvedString:
1019 if (! (field->type == &java::lang::String::class$
1020 || field->type == &java::lang::Class::class$))
1021 throw_class_format_error ("string initialiser to non-string field");
1023 *(jstring*)addr = pool->data[init].string;
1026 case JV_CONSTANT_Integer:
1028 int value = pool->data[init].i;
1030 if (field->type == JvPrimClass (boolean))
1031 *(jboolean*)addr = (jboolean)value;
1033 else if (field->type == JvPrimClass (byte))
1034 *(jbyte*)addr = (jbyte)value;
1036 else if (field->type == JvPrimClass (char))
1037 *(jchar*)addr = (jchar)value;
1039 else if (field->type == JvPrimClass (short))
1040 *(jshort*)addr = (jshort)value;
1042 else if (field->type == JvPrimClass (int))
1043 *(jint*)addr = (jint)value;
1046 throw_class_format_error ("erroneous field initializer");
1050 case JV_CONSTANT_Long:
1051 if (field->type != JvPrimClass (long))
1052 throw_class_format_error ("erroneous field initializer");
1054 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1057 case JV_CONSTANT_Float:
1058 if (field->type != JvPrimClass (float))
1059 throw_class_format_error ("erroneous field initializer");
1061 *(jfloat*)addr = pool->data[init].f;
1064 case JV_CONSTANT_Double:
1065 if (field->type != JvPrimClass (double))
1066 throw_class_format_error ("erroneous field initializer");
1068 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1072 throw_class_format_error ("erroneous field initializer");
1076 inline static unsigned char*
1077 skip_one_type (unsigned char* ptr)
1088 do { ch = *ptr++; } while (ch != ';');
1095 get_ffi_type_from_signature (unsigned char* ptr)
1101 return &ffi_type_pointer;
1105 // On some platforms a bool is a byte, on others an int.
1106 if (sizeof (jboolean) == sizeof (jbyte))
1107 return &ffi_type_sint8;
1110 JvAssert (sizeof (jbyte) == sizeof (jint));
1111 return &ffi_type_sint32;
1116 return &ffi_type_sint8;
1120 return &ffi_type_uint16;
1124 return &ffi_type_sint16;
1128 return &ffi_type_sint32;
1132 return &ffi_type_sint64;
1136 return &ffi_type_float;
1140 return &ffi_type_double;
1144 return &ffi_type_void;
1148 throw_internal_error ("unknown type in signature");
1151 /* this function yields the number of actual arguments, that is, if the
1152 * function is non-static, then one is added to the number of elements
1153 * found in the signature */
1156 _Jv_count_arguments (_Jv_Utf8Const *signature,
1159 unsigned char *ptr = (unsigned char*) signature->chars();
1160 int arg_count = staticp ? 0 : 1;
1162 /* first, count number of arguments */
1170 ptr = skip_one_type (ptr);
1177 /* This beast will build a cif, given the signature. Memory for
1178 * the cif itself and for the argument types must be allocated by the
1183 init_cif (_Jv_Utf8Const* signature,
1187 ffi_type **arg_types,
1190 unsigned char *ptr = (unsigned char*) signature->chars();
1192 int arg_index = 0; // arg number
1193 int item_count = 0; // stack-item count
1198 arg_types[arg_index++] = &ffi_type_pointer;
1208 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1210 if (*ptr == 'J' || *ptr == 'D')
1215 ptr = skip_one_type (ptr);
1220 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1222 ptr = skip_one_type (ptr);
1223 if (ptr != (unsigned char*)signature->chars() + signature->len())
1224 throw_internal_error ("did not find end of signature");
1226 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1227 arg_count, rtype, arg_types) != FFI_OK)
1228 throw_internal_error ("ffi_prep_cif failed");
1230 if (rtype_p != NULL)
1236 #if FFI_NATIVE_RAW_API
1237 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1238 # define FFI_RAW_SIZE ffi_raw_size
1240 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1241 # define FFI_RAW_SIZE ffi_java_raw_size
1244 /* we put this one here, and not in interpret.cc because it
1245 * calls the utility routines _Jv_count_arguments
1246 * which are static to this module. The following struct defines the
1247 * layout we use for the stubs, it's only used in the ncode method. */
1250 ffi_raw_closure closure;
1252 ffi_type *arg_types[0];
1255 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1258 _Jv_InterpMethod::ncode ()
1260 using namespace java::lang::reflect;
1262 if (self->ncode != 0)
1265 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1266 int arg_count = _Jv_count_arguments (self->signature, staticp);
1268 ncode_closure *closure =
1269 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1270 + arg_count * sizeof (ffi_type*));
1272 init_cif (self->signature,
1276 &closure->arg_types[0],
1279 ffi_closure_fun fun;
1281 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1283 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1285 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1289 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1290 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1292 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1296 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1297 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1299 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1306 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1307 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1309 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1313 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1314 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1316 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1320 FFI_PREP_RAW_CLOSURE (&closure->closure,
1325 self->ncode = (void*)closure;
1329 /* Find the index of the given insn in the array of insn slots
1330 for this method. Returns -1 if not found. */
1332 _Jv_InterpMethod::insn_index (pc_t pc)
1335 #ifdef DIRECT_THREADED
1336 jlong right = number_insn_slots;
1337 pc_t insns = prepared;
1339 jlong right = code_length;
1340 pc_t insns = bytecode ();
1345 jlong mid = (left + right) / 2;
1346 if (&insns[mid] == pc)
1349 if (pc < &insns[mid])
1359 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1360 jintArray& line_numbers,
1361 jlongArray& code_indices)
1363 #ifdef DIRECT_THREADED
1364 /* For the DIRECT_THREADED case, if the method has not yet been
1365 * compiled, the linetable will change to insn slots instead of
1366 * bytecode PCs. It is probably easiest, in this case, to simply
1367 * compile the method and guarantee that we are using insn
1370 _Jv_CompileMethod (this);
1372 if (line_table_len > 0)
1375 end = number_insn_slots;
1376 line_numbers = JvNewIntArray (line_table_len);
1377 code_indices = JvNewLongArray (line_table_len);
1379 jint* lines = elements (line_numbers);
1380 jlong* indices = elements (code_indices);
1381 for (int i = 0; i < line_table_len; ++i)
1383 lines[i] = line_table[i].line;
1384 indices[i] = insn_index (line_table[i].pc);
1387 #else // !DIRECT_THREADED
1388 if (line_table_len > 0)
1392 line_numbers = JvNewIntArray (line_table_len);
1393 code_indices = JvNewLongArray (line_table_len);
1395 jint* lines = elements (line_numbers);
1396 jlong* indices = elements (code_indices);
1397 for (int i = 0; i < line_table_len; ++i)
1399 lines[i] = line_table[i].line;
1400 indices[i] = (jlong) line_table[i].bytecode_pc;
1403 #endif // !DIRECT_THREADED
1407 _Jv_InterpMethod::install_break (jlong index)
1409 return set_insn (index, breakpoint_insn);
1413 _Jv_InterpMethod::get_insn (jlong index)
1417 #ifdef DIRECT_THREADED
1418 if (index >= number_insn_slots || index < 0)
1422 #else // !DIRECT_THREADED
1423 if (index >= code_length || index < 0)
1426 code = reinterpret_cast<pc_t> (bytecode ());
1427 #endif // !DIRECT_THREADED
1429 return &code[index];
1433 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1435 #ifdef DIRECT_THREADED
1436 if (index >= number_insn_slots || index < 0)
1439 pc_t code = prepared;
1440 code[index].insn = insn->insn;
1441 #else // !DIRECT_THREADED
1442 if (index >= code_length || index < 0)
1445 pc_t code = reinterpret_cast<pc_t> (bytecode ());
1446 code[index] = *insn;
1447 #endif // !DIRECT_THREADED
1449 return &code[index];
1453 _Jv_JNIMethod::ncode ()
1455 using namespace java::lang::reflect;
1457 if (self->ncode != 0)
1460 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1461 int arg_count = _Jv_count_arguments (self->signature, staticp);
1463 ncode_closure *closure =
1464 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1465 + arg_count * sizeof (ffi_type*));
1468 init_cif (self->signature,
1472 &closure->arg_types[0],
1475 ffi_closure_fun fun;
1477 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1479 // Initialize the argument types and CIF that represent the actual
1480 // underlying JNI function.
1482 if ((self->accflags & Modifier::STATIC))
1484 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1485 * sizeof (ffi_type *));
1487 jni_arg_types[offset++] = &ffi_type_pointer;
1488 if ((self->accflags & Modifier::STATIC))
1489 jni_arg_types[offset++] = &ffi_type_pointer;
1490 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1491 arg_count * sizeof (ffi_type *));
1493 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1494 extra_args + arg_count, rtype,
1495 jni_arg_types) != FFI_OK)
1496 throw_internal_error ("ffi_prep_cif failed for JNI function");
1498 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1500 // FIXME: for now we assume that all native methods for
1501 // interpreted code use JNI.
1502 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1504 FFI_PREP_RAW_CLOSURE (&closure->closure,
1509 self->ncode = (void *) closure;
1514 throw_class_format_error (jstring msg)
1517 ? new java::lang::ClassFormatError (msg)
1518 : new java::lang::ClassFormatError);
1522 throw_class_format_error (const char *msg)
1524 throw_class_format_error (JvNewStringLatin1 (msg));
1530 _Jv_InterpreterEngine::do_verify (jclass klass)
1532 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1533 for (int i = 0; i < klass->method_count; i++)
1535 using namespace java::lang::reflect;
1536 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1537 _Jv_ushort accflags = klass->methods[i].accflags;
1538 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1540 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1541 _Jv_VerifyMethod (im);
1547 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1549 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1550 for (int i = 0; i < klass->method_count; i++)
1552 // Just skip abstract methods. This is particularly important
1553 // because we don't resize the interpreted_methods array when
1554 // miranda methods are added to it.
1555 if ((klass->methods[i].accflags
1556 & java::lang::reflect::Modifier::ABSTRACT)
1560 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1562 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1565 // You might think we could use a virtual `ncode' method in
1566 // the _Jv_MethodBase and unify the native and non-native
1567 // cases. Well, we can't, because we don't allocate these
1568 // objects using `new', and thus they don't get a vtable.
1569 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1570 klass->methods[i].ncode = jnim->ncode ();
1572 else if (imeth != 0) // it could be abstract
1574 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1575 klass->methods[i].ncode = im->ncode ();
1581 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1585 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1587 // Splitting the allocations here lets us scan reference fields and
1588 // avoid scanning non-reference fields. How reference fields are
1589 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1590 // means that this memory will be scanned conservatively (same
1591 // difference, since we know all the contents here are pointers).
1592 // Then we put pointers into this memory into the 'fields'
1593 // structure. Most of these are interior pointers, which is ok (but
1594 // even so the pointer to the first reference field will be used and
1595 // that is not an interior pointer). The 'fields' array is also
1596 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1597 // be scanned. A pointer to this array is held by Class and thus
1598 // seen by the collector.
1599 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1600 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1602 for (int i = 0; i < klass->field_count; i++)
1604 _Jv_Field *field = &klass->fields[i];
1606 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1609 char *base = field->isRef() ? reference_fields : non_reference_fields;
1610 field->u.addr = base + field->u.boffset;
1612 if (iclass->field_initializers[i] != 0)
1614 _Jv_Linker::resolve_field (field, klass->loader);
1615 _Jv_InitField (0, klass, i);
1619 // Now we don't need the field_initializers anymore, so let the
1620 // collector get rid of it.
1621 iclass->field_initializers = 0;
1624 _Jv_ResolvedMethod *
1625 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1628 int arg_count = _Jv_count_arguments (method->signature, staticp);
1630 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1631 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1632 + arg_count*sizeof (ffi_type*));
1634 result->stack_item_count
1635 = init_cif (method->signature,
1639 &result->arg_types[0],
1642 result->method = method;
1643 result->klass = klass;
1649 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1651 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1652 for (int i = 0; i < klass->method_count; i++)
1654 // Just skip abstract methods. This is particularly important
1655 // because we don't resize the interpreted_methods array when
1656 // miranda methods are added to it.
1657 if ((klass->methods[i].accflags
1658 & java::lang::reflect::Modifier::ABSTRACT)
1661 // Miranda method additions mean that the `methods' array moves.
1662 // We cache a pointer into this array, so we have to update.
1663 iclass->interpreted_methods[i]->self = &klass->methods[i];
1667 #ifdef DIRECT_THREADED
1669 _Jv_CompileMethod (_Jv_InterpMethod* method)
1671 if (method->prepared == NULL)
1672 _Jv_InterpMethod::run (NULL, NULL, method);
1674 #endif // DIRECT_THREADED
1676 #endif // INTERPRETER