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 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)
159 DEBUG_LOCALS_INSN(I, 'o'); \
160 locals[I].o = (--sp)->o; \
164 DEBUG_LOCALS_INSN (I, 'i'); \
165 locals[I].i = (--sp)->i; \
169 DEBUG_LOCALS_INSN (I, 'f'); \
170 locals[I].f = (--sp)->f; \
172 #if SIZEOF_VOID_P == 8
175 DEBUG_LOCALS_INSN (I, 'l'); \
176 (sp -= 2, locals[I].l = sp->l); \
180 DEBUG_LOCALS_INSN (I, 'd'); \
181 (sp -= 2, locals[I].d = sp->d); \
186 do { DEBUG_LOCALS_INSN(I, 'l'); \
188 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
189 locals[__idx].ia[0] = (--sp)->ia[0]; \
192 do { DEBUG_LOCALS_INSN(I, 'd'); \
194 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
195 locals[__idx].ia[0] = (--sp)->ia[0]; \
199 #define PEEKI(I) (locals+(I))->i
200 #define PEEKA(I) (locals+(I))->o
203 DEBUG_LOCALS_INSN(I,i) \
204 ((locals+(I))->i = (V))
207 #define BINOPI(OP) { \
208 jint value2 = POPI(); \
209 jint value1 = POPI(); \
210 PUSHI(value1 OP value2); \
213 #define BINOPF(OP) { \
214 jfloat value2 = POPF(); \
215 jfloat value1 = POPF(); \
216 PUSHF(value1 OP value2); \
219 #define BINOPL(OP) { \
220 jlong value2 = POPL(); \
221 jlong value1 = POPL(); \
222 PUSHL(value1 OP value2); \
225 #define BINOPD(OP) { \
226 jdouble value2 = POPD(); \
227 jdouble value1 = POPD(); \
228 PUSHD(value1 OP value2); \
231 static inline jint get1s(unsigned char* loc) {
232 return *(signed char*)loc;
235 static inline jint get1u(unsigned char* loc) {
239 static inline jint get2s(unsigned char* loc) {
240 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
243 static inline jint get2u(unsigned char* loc) {
244 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
247 static jint get4(unsigned char* loc) {
248 return (((jint)(loc[0])) << 24)
249 | (((jint)(loc[1])) << 16)
250 | (((jint)(loc[2])) << 8)
251 | (((jint)(loc[3])) << 0);
254 #define SAVE_PC() frame_desc.pc = pc
256 // We used to define this conditionally, depending on HANDLE_SEGV.
257 // However, that runs into a problem if a chunk in low memory is
258 // mapped and we try to look at a field near the end of a large
259 // object. See PR 26858 for details. It is, most likely, relatively
260 // inexpensive to simply do this check always.
261 #define NULLCHECK(X) \
262 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
264 // Note that we can still conditionally define NULLARRAYCHECK, since
265 // we know that all uses of an array will first reference the length
266 // field, which is first -- and thus will trigger a SEGV.
268 #define NULLARRAYCHECK(X) SAVE_PC()
270 #define NULLARRAYCHECK(X) \
271 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
274 #define ARRAYBOUNDSCHECK(array, index) \
277 if (((unsigned) index) >= (unsigned) (array->length)) \
278 _Jv_ThrowBadArrayIndex (index); \
283 _Jv_InterpMethod::run_normal (ffi_cif *,
288 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
289 run (ret, args, _this);
293 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
298 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
299 run_debug (ret, args, _this);
303 _Jv_InterpMethod::run_synch_object (ffi_cif *,
308 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
310 jobject rcv = (jobject) args[0].ptr;
311 JvSynchronize mutex (rcv);
313 run (ret, args, _this);
317 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
322 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
324 jobject rcv = (jobject) args[0].ptr;
325 JvSynchronize mutex (rcv);
327 run_debug (ret, args, _this);
331 _Jv_InterpMethod::run_class (ffi_cif *,
336 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
337 _Jv_InitClass (_this->defining_class);
338 run (ret, args, _this);
342 _Jv_InterpMethod::run_class_debug (ffi_cif *,
347 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
348 _Jv_InitClass (_this->defining_class);
349 run_debug (ret, args, _this);
353 _Jv_InterpMethod::run_synch_class (ffi_cif *,
358 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
360 jclass sync = _this->defining_class;
361 _Jv_InitClass (sync);
362 JvSynchronize mutex (sync);
364 run (ret, args, _this);
368 _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
373 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
375 jclass sync = _this->defining_class;
376 _Jv_InitClass (sync);
377 JvSynchronize mutex (sync);
379 run_debug (ret, args, _this);
382 #ifdef DIRECT_THREADED
383 // "Compile" a method by turning it from bytecode to direct-threaded
386 _Jv_InterpMethod::compile (const void * const *insn_targets)
388 insn_slot *insns = NULL;
390 unsigned char *codestart = bytecode ();
391 unsigned char *end = codestart + code_length;
392 _Jv_word *pool_data = defining_class->constants.data;
394 #define SET_ONE(Field, Value) \
400 insns[next++].Field = Value; \
404 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
405 #define SET_INT(Value) SET_ONE (int_val, Value)
406 #define SET_DATUM(Value) SET_ONE (datum, Value)
408 // Map from bytecode PC to slot in INSNS.
409 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
410 for (int i = 0; i < code_length; ++i)
413 for (int i = 0; i < 2; ++i)
415 jboolean first_pass = i == 0;
419 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
420 number_insn_slots = next;
424 unsigned char *pc = codestart;
427 int base_pc_val = pc - codestart;
429 pc_mapping[base_pc_val] = next;
431 java_opcode opcode = (java_opcode) *pc++;
433 if (opcode == op_nop)
435 SET_INSN (insn_targets[opcode]);
576 case op_monitorenter:
586 // No argument, nothing else to do.
590 SET_INT (get1s (pc));
596 int index = get1u (pc);
598 // For an unresolved class we want to delay resolution
600 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
603 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
607 SET_DATUM (pool_data[index].o);
623 SET_INT (get1u (pc));
628 SET_INT (get1u (pc));
629 SET_INT (get1s (pc + 1));
635 int index = get2u (pc);
637 // For an unresolved class we want to delay resolution
639 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
642 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
646 SET_DATUM (pool_data[index].o);
652 int index = get2u (pc);
654 SET_DATUM (&pool_data[index]);
659 SET_INT (get2s (pc));
671 case op_invokespecial:
672 case op_invokestatic:
673 case op_invokevirtual:
674 SET_INT (get2u (pc));
678 case op_multianewarray:
679 SET_INT (get2u (pc));
680 SET_INT (get1u (pc + 2));
703 int offset = get2s (pc);
706 int new_pc = base_pc_val + offset;
708 bool orig_was_goto = opcode == op_goto;
710 // Thread jumps. We limit the loop count; this lets
711 // us avoid infinite loops if the bytecode contains
712 // such. `10' is arbitrary.
714 while (codestart[new_pc] == op_goto && count-- > 0)
715 new_pc += get2s (&codestart[new_pc + 1]);
717 // If the jump takes us to a `return' instruction and
718 // the original branch was an unconditional goto, then
719 // we hoist the return.
720 opcode = (java_opcode) codestart[new_pc];
722 && (opcode == op_ireturn || opcode == op_lreturn
723 || opcode == op_freturn || opcode == op_dreturn
724 || opcode == op_areturn || opcode == op_return))
727 SET_INSN (insn_targets[opcode]);
730 SET_DATUM (&insns[pc_mapping[new_pc]]);
736 while ((pc - codestart) % 4 != 0)
739 jint def = get4 (pc);
740 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
746 int high = get4 (pc);
750 for (int i = low; i <= high; ++i)
752 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
758 case op_lookupswitch:
760 while ((pc - codestart) % 4 != 0)
763 jint def = get4 (pc);
764 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
767 jint npairs = get4 (pc);
773 jint match = get4 (pc);
774 jint offset = get4 (pc + 4);
776 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
782 case op_invokeinterface:
784 jint index = get2u (pc);
786 // We ignore the next two bytes.
794 opcode = (java_opcode) get1u (pc);
796 jint val = get2u (pc);
799 // We implement narrow and wide instructions using the
800 // same code in the interpreter. So we rewrite the
801 // instruction slot here.
803 insns[next - 1].insn = (void *) insn_targets[opcode];
806 if (opcode == op_iinc)
808 SET_INT (get2s (pc));
817 jint offset = get4 (pc);
819 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
823 // Some "can't happen" cases that we include for
824 // error-checking purposes.
842 case op_getstatic_2s:
843 case op_getstatic_2u:
854 // Now update exceptions.
855 _Jv_InterpException *exc = exceptions ();
856 for (int i = 0; i < exc_count; ++i)
858 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
859 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
860 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
861 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
862 // during compilation.
864 = (_Jv_Linker::resolve_pool_entry (defining_class,
865 exc[i].handler_type.i)).clazz;
866 exc[i].handler_type.p = handler;
869 // Translate entries in the LineNumberTable from bytecode PC's to direct
870 // threaded interpreter instruction values.
871 for (int i = 0; i < line_table_len; i++)
873 int byte_pc = line_table[i].bytecode_pc;
874 // It isn't worth throwing an exception if this table is
875 // corrupted, but at the same time we don't want a crash.
876 if (byte_pc < 0 || byte_pc >= code_length)
878 line_table[i].pc = &insns[pc_mapping[byte_pc]];
883 #endif /* DIRECT_THREADED */
885 /* Run the given method.
886 When args is NULL, don't run anything -- just compile it. */
888 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
891 #undef DEBUG_LOCALS_INSN
892 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
894 #include "interpret-run.cc"
898 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
900 /* Used to keep track of local variable type
910 #undef DEBUG_LOCALS_INSN
911 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
913 #include "interpret-run.cc"
917 throw_internal_error (const char *msg)
919 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
923 throw_incompatible_class_change_error (jstring msg)
925 throw new java::lang::IncompatibleClassChangeError (msg);
929 throw_null_pointer_exception ()
931 throw new java::lang::NullPointerException;
934 /* Look up source code line number for given bytecode (or direct threaded
937 _Jv_InterpMethod::get_source_line(pc_t mpc)
939 int line = line_table_len > 0 ? line_table[0].line : -1;
940 for (int i = 1; i < line_table_len; i++)
941 if (line_table[i].pc > mpc)
944 line = line_table[i].line;
949 /** Do static initialization for fields with a constant initializer */
951 _Jv_InitField (jobject obj, jclass klass, int index)
953 using namespace java::lang::reflect;
955 if (obj != 0 && klass == 0)
956 klass = obj->getClass ();
958 if (!_Jv_IsInterpretedClass (klass))
961 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
963 _Jv_Field * field = (&klass->fields[0]) + index;
965 if (index > klass->field_count)
966 throw_internal_error ("field out of range");
968 int init = iclass->field_initializers[index];
972 _Jv_Constants *pool = &klass->constants;
973 int tag = pool->tags[init];
975 if (! field->isResolved ())
976 throw_internal_error ("initializing unresolved field");
978 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
979 throw_internal_error ("initializing non-static field with no object");
983 if ((field->flags & Modifier::STATIC) != 0)
984 addr = (void*) field->u.addr;
986 addr = (void*) (((char*)obj) + field->u.boffset);
990 case JV_CONSTANT_String:
993 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
994 pool->data[init].string = str;
995 pool->tags[init] = JV_CONSTANT_ResolvedString;
999 case JV_CONSTANT_ResolvedString:
1000 if (! (field->type == &java::lang::String::class$
1001 || field->type == &java::lang::Class::class$))
1002 throw_class_format_error ("string initialiser to non-string field");
1004 *(jstring*)addr = pool->data[init].string;
1007 case JV_CONSTANT_Integer:
1009 int value = pool->data[init].i;
1011 if (field->type == JvPrimClass (boolean))
1012 *(jboolean*)addr = (jboolean)value;
1014 else if (field->type == JvPrimClass (byte))
1015 *(jbyte*)addr = (jbyte)value;
1017 else if (field->type == JvPrimClass (char))
1018 *(jchar*)addr = (jchar)value;
1020 else if (field->type == JvPrimClass (short))
1021 *(jshort*)addr = (jshort)value;
1023 else if (field->type == JvPrimClass (int))
1024 *(jint*)addr = (jint)value;
1027 throw_class_format_error ("erroneous field initializer");
1031 case JV_CONSTANT_Long:
1032 if (field->type != JvPrimClass (long))
1033 throw_class_format_error ("erroneous field initializer");
1035 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1038 case JV_CONSTANT_Float:
1039 if (field->type != JvPrimClass (float))
1040 throw_class_format_error ("erroneous field initializer");
1042 *(jfloat*)addr = pool->data[init].f;
1045 case JV_CONSTANT_Double:
1046 if (field->type != JvPrimClass (double))
1047 throw_class_format_error ("erroneous field initializer");
1049 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1053 throw_class_format_error ("erroneous field initializer");
1057 inline static unsigned char*
1058 skip_one_type (unsigned char* ptr)
1069 do { ch = *ptr++; } while (ch != ';');
1076 get_ffi_type_from_signature (unsigned char* ptr)
1082 return &ffi_type_pointer;
1086 // On some platforms a bool is a byte, on others an int.
1087 if (sizeof (jboolean) == sizeof (jbyte))
1088 return &ffi_type_sint8;
1091 JvAssert (sizeof (jbyte) == sizeof (jint));
1092 return &ffi_type_sint32;
1097 return &ffi_type_sint8;
1101 return &ffi_type_uint16;
1105 return &ffi_type_sint16;
1109 return &ffi_type_sint32;
1113 return &ffi_type_sint64;
1117 return &ffi_type_float;
1121 return &ffi_type_double;
1125 return &ffi_type_void;
1129 throw_internal_error ("unknown type in signature");
1132 /* this function yields the number of actual arguments, that is, if the
1133 * function is non-static, then one is added to the number of elements
1134 * found in the signature */
1137 _Jv_count_arguments (_Jv_Utf8Const *signature,
1140 unsigned char *ptr = (unsigned char*) signature->chars();
1141 int arg_count = staticp ? 0 : 1;
1143 /* first, count number of arguments */
1151 ptr = skip_one_type (ptr);
1158 /* This beast will build a cif, given the signature. Memory for
1159 * the cif itself and for the argument types must be allocated by the
1164 init_cif (_Jv_Utf8Const* signature,
1168 ffi_type **arg_types,
1171 unsigned char *ptr = (unsigned char*) signature->chars();
1173 int arg_index = 0; // arg number
1174 int item_count = 0; // stack-item count
1179 arg_types[arg_index++] = &ffi_type_pointer;
1189 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1191 if (*ptr == 'J' || *ptr == 'D')
1196 ptr = skip_one_type (ptr);
1201 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1203 ptr = skip_one_type (ptr);
1204 if (ptr != (unsigned char*)signature->chars() + signature->len())
1205 throw_internal_error ("did not find end of signature");
1207 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1208 arg_count, rtype, arg_types) != FFI_OK)
1209 throw_internal_error ("ffi_prep_cif failed");
1211 if (rtype_p != NULL)
1217 #if FFI_NATIVE_RAW_API
1218 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1219 # define FFI_RAW_SIZE ffi_raw_size
1221 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1222 # define FFI_RAW_SIZE ffi_java_raw_size
1225 /* we put this one here, and not in interpret.cc because it
1226 * calls the utility routines _Jv_count_arguments
1227 * which are static to this module. The following struct defines the
1228 * layout we use for the stubs, it's only used in the ncode method. */
1231 ffi_raw_closure closure;
1233 ffi_type *arg_types[0];
1236 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1239 _Jv_InterpMethod::ncode ()
1241 using namespace java::lang::reflect;
1243 if (self->ncode != 0)
1246 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1247 int arg_count = _Jv_count_arguments (self->signature, staticp);
1249 ncode_closure *closure =
1250 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1251 + arg_count * sizeof (ffi_type*));
1253 init_cif (self->signature,
1257 &closure->arg_types[0],
1260 ffi_closure_fun fun;
1262 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1264 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1266 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1270 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1271 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1273 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1277 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1278 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1280 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1287 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1288 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1290 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1294 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1295 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1297 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1301 FFI_PREP_RAW_CLOSURE (&closure->closure,
1306 self->ncode = (void*)closure;
1310 #ifdef DIRECT_THREADED
1311 /* Find the index of the given insn in the array of insn slots
1312 for this method. Returns -1 if not found. */
1314 _Jv_InterpMethod::insn_index (pc_t pc)
1317 jlong right = number_insn_slots;
1318 insn_slot* slots = reinterpret_cast<insn_slot*> (prepared);
1322 jlong mid = (left + right) / 2;
1323 if (&slots[mid] == pc)
1326 if (pc < &slots[mid])
1334 #endif // DIRECT_THREADED
1337 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1338 jintArray& line_numbers,
1339 jlongArray& code_indices)
1341 #ifdef DIRECT_THREADED
1342 /* For the DIRECT_THREADED case, if the method has not yet been
1343 * compiled, the linetable will change to insn slots instead of
1344 * bytecode PCs. It is probably easiest, in this case, to simply
1345 * compile the method and guarantee that we are using insn
1348 _Jv_CompileMethod (this);
1350 if (line_table_len > 0)
1353 end = number_insn_slots;
1354 line_numbers = JvNewIntArray (line_table_len);
1355 code_indices = JvNewLongArray (line_table_len);
1357 jint* lines = elements (line_numbers);
1358 jlong* indices = elements (code_indices);
1359 for (int i = 0; i < line_table_len; ++i)
1361 lines[i] = line_table[i].line;
1362 indices[i] = insn_index (line_table[i].pc);
1365 #else // !DIRECT_THREADED
1366 if (line_table_len > 0)
1370 line_numbers = JvNewIntArray (line_table_len);
1371 code_indices = JvNewLongArray (line_table_len);
1373 jint* lines = elements (line_numbers);
1374 jlong* indices = elements (code_indices);
1375 for (int i = 0; i < line_table_len; ++i)
1377 lines[i] = line_table[i].line;
1378 indices[i] = (jlong) line_table[i].bytecode_pc;
1381 #endif // !DIRECT_THREADED
1385 _Jv_JNIMethod::ncode ()
1387 using namespace java::lang::reflect;
1389 if (self->ncode != 0)
1392 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1393 int arg_count = _Jv_count_arguments (self->signature, staticp);
1395 ncode_closure *closure =
1396 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1397 + arg_count * sizeof (ffi_type*));
1400 init_cif (self->signature,
1404 &closure->arg_types[0],
1407 ffi_closure_fun fun;
1409 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1411 // Initialize the argument types and CIF that represent the actual
1412 // underlying JNI function.
1414 if ((self->accflags & Modifier::STATIC))
1416 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1417 * sizeof (ffi_type *));
1419 jni_arg_types[offset++] = &ffi_type_pointer;
1420 if ((self->accflags & Modifier::STATIC))
1421 jni_arg_types[offset++] = &ffi_type_pointer;
1422 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1423 arg_count * sizeof (ffi_type *));
1425 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1426 extra_args + arg_count, rtype,
1427 jni_arg_types) != FFI_OK)
1428 throw_internal_error ("ffi_prep_cif failed for JNI function");
1430 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1432 // FIXME: for now we assume that all native methods for
1433 // interpreted code use JNI.
1434 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1436 FFI_PREP_RAW_CLOSURE (&closure->closure,
1441 self->ncode = (void *) closure;
1446 throw_class_format_error (jstring msg)
1449 ? new java::lang::ClassFormatError (msg)
1450 : new java::lang::ClassFormatError);
1454 throw_class_format_error (const char *msg)
1456 throw_class_format_error (JvNewStringLatin1 (msg));
1462 _Jv_InterpreterEngine::do_verify (jclass klass)
1464 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1465 for (int i = 0; i < klass->method_count; i++)
1467 using namespace java::lang::reflect;
1468 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1469 _Jv_ushort accflags = klass->methods[i].accflags;
1470 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1472 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1473 _Jv_VerifyMethod (im);
1479 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1481 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1482 for (int i = 0; i < klass->method_count; i++)
1484 // Just skip abstract methods. This is particularly important
1485 // because we don't resize the interpreted_methods array when
1486 // miranda methods are added to it.
1487 if ((klass->methods[i].accflags
1488 & java::lang::reflect::Modifier::ABSTRACT)
1492 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1494 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1497 // You might think we could use a virtual `ncode' method in
1498 // the _Jv_MethodBase and unify the native and non-native
1499 // cases. Well, we can't, because we don't allocate these
1500 // objects using `new', and thus they don't get a vtable.
1501 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1502 klass->methods[i].ncode = jnim->ncode ();
1504 else if (imeth != 0) // it could be abstract
1506 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1507 klass->methods[i].ncode = im->ncode ();
1513 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1517 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1519 // Splitting the allocations here lets us scan reference fields and
1520 // avoid scanning non-reference fields. How reference fields are
1521 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1522 // means that this memory will be scanned conservatively (same
1523 // difference, since we know all the contents here are pointers).
1524 // Then we put pointers into this memory into the 'fields'
1525 // structure. Most of these are interior pointers, which is ok (but
1526 // even so the pointer to the first reference field will be used and
1527 // that is not an interior pointer). The 'fields' array is also
1528 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1529 // be scanned. A pointer to this array is held by Class and thus
1530 // seen by the collector.
1531 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1532 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1534 for (int i = 0; i < klass->field_count; i++)
1536 _Jv_Field *field = &klass->fields[i];
1538 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1541 char *base = field->isRef() ? reference_fields : non_reference_fields;
1542 field->u.addr = base + field->u.boffset;
1544 if (iclass->field_initializers[i] != 0)
1546 _Jv_Linker::resolve_field (field, klass->loader);
1547 _Jv_InitField (0, klass, i);
1551 // Now we don't need the field_initializers anymore, so let the
1552 // collector get rid of it.
1553 iclass->field_initializers = 0;
1556 _Jv_ResolvedMethod *
1557 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1560 int arg_count = _Jv_count_arguments (method->signature, staticp);
1562 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1563 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1564 + arg_count*sizeof (ffi_type*));
1566 result->stack_item_count
1567 = init_cif (method->signature,
1571 &result->arg_types[0],
1574 result->method = method;
1575 result->klass = klass;
1581 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1583 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1584 for (int i = 0; i < klass->method_count; i++)
1586 // Just skip abstract methods. This is particularly important
1587 // because we don't resize the interpreted_methods array when
1588 // miranda methods are added to it.
1589 if ((klass->methods[i].accflags
1590 & java::lang::reflect::Modifier::ABSTRACT)
1593 // Miranda method additions mean that the `methods' array moves.
1594 // We cache a pointer into this array, so we have to update.
1595 iclass->interpreted_methods[i]->self = &klass->methods[i];
1599 #ifdef DIRECT_THREADED
1601 _Jv_CompileMethod (_Jv_InterpMethod* method)
1603 if (method->prepared == NULL)
1604 _Jv_InterpMethod::run (NULL, NULL, method);
1606 #endif // DIRECT_THREADED
1608 #endif // INTERPRETER