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); \
203 do { DEBUG_LOCALS_INSN(I, 'l'); \
205 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
206 locals[__idx].ia[0] = (--sp)->ia[0]; \
209 do { DEBUG_LOCALS_INSN(I, 'd'); \
211 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
212 locals[__idx].ia[0] = (--sp)->ia[0]; \
216 #define PEEKI(I) (locals+(I))->i
217 #define PEEKA(I) (locals+(I))->o
220 DEBUG_LOCALS_INSN(I,'i'); \
221 ((locals+(I))->i = (V))
224 #define BINOPI(OP) { \
225 jint value2 = POPI(); \
226 jint value1 = POPI(); \
227 PUSHI(value1 OP value2); \
230 #define BINOPF(OP) { \
231 jfloat value2 = POPF(); \
232 jfloat value1 = POPF(); \
233 PUSHF(value1 OP value2); \
236 #define BINOPL(OP) { \
237 jlong value2 = POPL(); \
238 jlong value1 = POPL(); \
239 PUSHL(value1 OP value2); \
242 #define BINOPD(OP) { \
243 jdouble value2 = POPD(); \
244 jdouble value1 = POPD(); \
245 PUSHD(value1 OP value2); \
248 static inline jint get1s(unsigned char* loc) {
249 return *(signed char*)loc;
252 static inline jint get1u(unsigned char* loc) {
256 static inline jint get2s(unsigned char* loc) {
257 return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
260 static inline jint get2u(unsigned char* loc) {
261 return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
264 static jint get4(unsigned char* loc) {
265 return (((jint)(loc[0])) << 24)
266 | (((jint)(loc[1])) << 16)
267 | (((jint)(loc[2])) << 8)
268 | (((jint)(loc[3])) << 0);
271 #define SAVE_PC() frame_desc.pc = pc
273 // We used to define this conditionally, depending on HANDLE_SEGV.
274 // However, that runs into a problem if a chunk in low memory is
275 // mapped and we try to look at a field near the end of a large
276 // object. See PR 26858 for details. It is, most likely, relatively
277 // inexpensive to simply do this check always.
278 #define NULLCHECK(X) \
279 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
281 // Note that we can still conditionally define NULLARRAYCHECK, since
282 // we know that all uses of an array will first reference the length
283 // field, which is first -- and thus will trigger a SEGV.
285 #define NULLARRAYCHECK(X) SAVE_PC()
287 #define NULLARRAYCHECK(X) \
288 do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
291 #define ARRAYBOUNDSCHECK(array, index) \
294 if (((unsigned) index) >= (unsigned) (array->length)) \
295 _Jv_ThrowBadArrayIndex (index); \
300 _Jv_InterpMethod::run_normal (ffi_cif *,
305 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
306 run (ret, args, _this);
310 _Jv_InterpMethod::run_normal_debug (ffi_cif *,
315 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
316 run_debug (ret, args, _this);
320 _Jv_InterpMethod::run_synch_object (ffi_cif *,
325 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
327 jobject rcv = (jobject) args[0].ptr;
328 JvSynchronize mutex (rcv);
330 run (ret, args, _this);
334 _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
339 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
341 jobject rcv = (jobject) args[0].ptr;
342 JvSynchronize mutex (rcv);
344 run_debug (ret, args, _this);
348 _Jv_InterpMethod::run_class (ffi_cif *,
353 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
354 _Jv_InitClass (_this->defining_class);
355 run (ret, args, _this);
359 _Jv_InterpMethod::run_class_debug (ffi_cif *,
364 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
365 _Jv_InitClass (_this->defining_class);
366 run_debug (ret, args, _this);
370 _Jv_InterpMethod::run_synch_class (ffi_cif *,
375 _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
377 jclass sync = _this->defining_class;
378 _Jv_InitClass (sync);
379 JvSynchronize mutex (sync);
381 run (ret, args, _this);
385 _Jv_InterpMethod::run_synch_class_debug (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_debug (ret, args, _this);
399 #ifdef DIRECT_THREADED
400 // "Compile" a method by turning it from bytecode to direct-threaded
403 _Jv_InterpMethod::compile (const void * const *insn_targets)
405 insn_slot *insns = NULL;
407 unsigned char *codestart = bytecode ();
408 unsigned char *end = codestart + code_length;
409 _Jv_word *pool_data = defining_class->constants.data;
411 #define SET_ONE(Field, Value) \
417 insns[next++].Field = Value; \
421 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
422 #define SET_INT(Value) SET_ONE (int_val, Value)
423 #define SET_DATUM(Value) SET_ONE (datum, Value)
425 // Map from bytecode PC to slot in INSNS.
426 int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
427 for (int i = 0; i < code_length; ++i)
430 for (int i = 0; i < 2; ++i)
432 jboolean first_pass = i == 0;
436 insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
437 number_insn_slots = next;
441 unsigned char *pc = codestart;
444 int base_pc_val = pc - codestart;
446 pc_mapping[base_pc_val] = next;
448 java_opcode opcode = (java_opcode) *pc++;
450 if (opcode == op_nop)
452 SET_INSN (insn_targets[opcode]);
593 case op_monitorenter:
603 // No argument, nothing else to do.
607 SET_INT (get1s (pc));
613 int index = get1u (pc);
615 // For an unresolved class we want to delay resolution
617 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
620 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
624 SET_DATUM (pool_data[index].o);
640 SET_INT (get1u (pc));
645 SET_INT (get1u (pc));
646 SET_INT (get1s (pc + 1));
652 int index = get2u (pc);
654 // For an unresolved class we want to delay resolution
656 if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
659 SET_INSN (insn_targets[int (op_jsr_w) + 1]);
663 SET_DATUM (pool_data[index].o);
669 int index = get2u (pc);
671 SET_DATUM (&pool_data[index]);
676 SET_INT (get2s (pc));
688 case op_invokespecial:
689 case op_invokestatic:
690 case op_invokevirtual:
691 SET_INT (get2u (pc));
695 case op_multianewarray:
696 SET_INT (get2u (pc));
697 SET_INT (get1u (pc + 2));
720 int offset = get2s (pc);
723 int new_pc = base_pc_val + offset;
725 bool orig_was_goto = opcode == op_goto;
727 // Thread jumps. We limit the loop count; this lets
728 // us avoid infinite loops if the bytecode contains
729 // such. `10' is arbitrary.
731 while (codestart[new_pc] == op_goto && count-- > 0)
732 new_pc += get2s (&codestart[new_pc + 1]);
734 // If the jump takes us to a `return' instruction and
735 // the original branch was an unconditional goto, then
736 // we hoist the return.
737 opcode = (java_opcode) codestart[new_pc];
739 && (opcode == op_ireturn || opcode == op_lreturn
740 || opcode == op_freturn || opcode == op_dreturn
741 || opcode == op_areturn || opcode == op_return))
744 SET_INSN (insn_targets[opcode]);
747 SET_DATUM (&insns[pc_mapping[new_pc]]);
753 while ((pc - codestart) % 4 != 0)
756 jint def = get4 (pc);
757 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
763 int high = get4 (pc);
767 for (int i = low; i <= high; ++i)
769 SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
775 case op_lookupswitch:
777 while ((pc - codestart) % 4 != 0)
780 jint def = get4 (pc);
781 SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
784 jint npairs = get4 (pc);
790 jint match = get4 (pc);
791 jint offset = get4 (pc + 4);
793 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
799 case op_invokeinterface:
801 jint index = get2u (pc);
803 // We ignore the next two bytes.
811 opcode = (java_opcode) get1u (pc);
813 jint val = get2u (pc);
816 // We implement narrow and wide instructions using the
817 // same code in the interpreter. So we rewrite the
818 // instruction slot here.
820 insns[next - 1].insn = (void *) insn_targets[opcode];
823 if (opcode == op_iinc)
825 SET_INT (get2s (pc));
834 jint offset = get4 (pc);
836 SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
840 // Some "can't happen" cases that we include for
841 // error-checking purposes.
859 case op_getstatic_2s:
860 case op_getstatic_2u:
872 // Now update exceptions.
873 _Jv_InterpException *exc = exceptions ();
874 for (int i = 0; i < exc_count; ++i)
876 exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
877 exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
878 exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
879 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
880 // during compilation.
882 = (_Jv_Linker::resolve_pool_entry (defining_class,
883 exc[i].handler_type.i)).clazz;
884 exc[i].handler_type.p = handler;
887 // Translate entries in the LineNumberTable from bytecode PC's to direct
888 // threaded interpreter instruction values.
889 for (int i = 0; i < line_table_len; i++)
891 int byte_pc = line_table[i].bytecode_pc;
892 // It isn't worth throwing an exception if this table is
893 // corrupted, but at the same time we don't want a crash.
894 if (byte_pc < 0 || byte_pc >= code_length)
896 line_table[i].pc = &insns[pc_mapping[byte_pc]];
901 if (breakpoint_insn == NULL)
903 bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
904 breakpoint_insn = &bp_insn_slot;
907 #endif /* DIRECT_THREADED */
909 /* Run the given method.
910 When args is NULL, don't run anything -- just compile it. */
912 _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
915 #undef DEBUG_LOCALS_INSN
916 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
918 #include "interpret-run.cc"
922 _Jv_InterpMethod::run_debug (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
924 /* Used to keep track of local variable type
934 #undef DEBUG_LOCALS_INSN
935 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
937 #include "interpret-run.cc"
941 throw_internal_error (const char *msg)
943 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
947 throw_incompatible_class_change_error (jstring msg)
949 throw new java::lang::IncompatibleClassChangeError (msg);
953 throw_null_pointer_exception ()
955 throw new java::lang::NullPointerException;
958 /* Look up source code line number for given bytecode (or direct threaded
961 _Jv_InterpMethod::get_source_line(pc_t mpc)
963 int line = line_table_len > 0 ? line_table[0].line : -1;
964 for (int i = 1; i < line_table_len; i++)
965 if (line_table[i].pc > mpc)
968 line = line_table[i].line;
973 /** Do static initialization for fields with a constant initializer */
975 _Jv_InitField (jobject obj, jclass klass, int index)
977 using namespace java::lang::reflect;
979 if (obj != 0 && klass == 0)
980 klass = obj->getClass ();
982 if (!_Jv_IsInterpretedClass (klass))
985 _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
987 _Jv_Field * field = (&klass->fields[0]) + index;
989 if (index > klass->field_count)
990 throw_internal_error ("field out of range");
992 int init = iclass->field_initializers[index];
996 _Jv_Constants *pool = &klass->constants;
997 int tag = pool->tags[init];
999 if (! field->isResolved ())
1000 throw_internal_error ("initializing unresolved field");
1002 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
1003 throw_internal_error ("initializing non-static field with no object");
1007 if ((field->flags & Modifier::STATIC) != 0)
1008 addr = (void*) field->u.addr;
1010 addr = (void*) (((char*)obj) + field->u.boffset);
1014 case JV_CONSTANT_String:
1017 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
1018 pool->data[init].string = str;
1019 pool->tags[init] = JV_CONSTANT_ResolvedString;
1023 case JV_CONSTANT_ResolvedString:
1024 if (! (field->type == &java::lang::String::class$
1025 || field->type == &java::lang::Class::class$))
1026 throw_class_format_error ("string initialiser to non-string field");
1028 *(jstring*)addr = pool->data[init].string;
1031 case JV_CONSTANT_Integer:
1033 int value = pool->data[init].i;
1035 if (field->type == JvPrimClass (boolean))
1036 *(jboolean*)addr = (jboolean)value;
1038 else if (field->type == JvPrimClass (byte))
1039 *(jbyte*)addr = (jbyte)value;
1041 else if (field->type == JvPrimClass (char))
1042 *(jchar*)addr = (jchar)value;
1044 else if (field->type == JvPrimClass (short))
1045 *(jshort*)addr = (jshort)value;
1047 else if (field->type == JvPrimClass (int))
1048 *(jint*)addr = (jint)value;
1051 throw_class_format_error ("erroneous field initializer");
1055 case JV_CONSTANT_Long:
1056 if (field->type != JvPrimClass (long))
1057 throw_class_format_error ("erroneous field initializer");
1059 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
1062 case JV_CONSTANT_Float:
1063 if (field->type != JvPrimClass (float))
1064 throw_class_format_error ("erroneous field initializer");
1066 *(jfloat*)addr = pool->data[init].f;
1069 case JV_CONSTANT_Double:
1070 if (field->type != JvPrimClass (double))
1071 throw_class_format_error ("erroneous field initializer");
1073 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
1077 throw_class_format_error ("erroneous field initializer");
1081 inline static unsigned char*
1082 skip_one_type (unsigned char* ptr)
1093 do { ch = *ptr++; } while (ch != ';');
1100 get_ffi_type_from_signature (unsigned char* ptr)
1106 return &ffi_type_pointer;
1110 // On some platforms a bool is a byte, on others an int.
1111 if (sizeof (jboolean) == sizeof (jbyte))
1112 return &ffi_type_sint8;
1115 JvAssert (sizeof (jbyte) == sizeof (jint));
1116 return &ffi_type_sint32;
1121 return &ffi_type_sint8;
1125 return &ffi_type_uint16;
1129 return &ffi_type_sint16;
1133 return &ffi_type_sint32;
1137 return &ffi_type_sint64;
1141 return &ffi_type_float;
1145 return &ffi_type_double;
1149 return &ffi_type_void;
1153 throw_internal_error ("unknown type in signature");
1156 /* this function yields the number of actual arguments, that is, if the
1157 * function is non-static, then one is added to the number of elements
1158 * found in the signature */
1161 _Jv_count_arguments (_Jv_Utf8Const *signature,
1164 unsigned char *ptr = (unsigned char*) signature->chars();
1165 int arg_count = staticp ? 0 : 1;
1167 /* first, count number of arguments */
1175 ptr = skip_one_type (ptr);
1182 /* This beast will build a cif, given the signature. Memory for
1183 * the cif itself and for the argument types must be allocated by the
1188 _Jv_init_cif (_Jv_Utf8Const* signature,
1192 ffi_type **arg_types,
1195 unsigned char *ptr = (unsigned char*) signature->chars();
1197 int arg_index = 0; // arg number
1198 int item_count = 0; // stack-item count
1203 arg_types[arg_index++] = &ffi_type_pointer;
1213 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1215 if (*ptr == 'J' || *ptr == 'D')
1220 ptr = skip_one_type (ptr);
1225 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1227 ptr = skip_one_type (ptr);
1228 if (ptr != (unsigned char*)signature->chars() + signature->len())
1229 throw_internal_error ("did not find end of signature");
1231 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1232 arg_count, rtype, arg_types) != FFI_OK)
1233 throw_internal_error ("ffi_prep_cif failed");
1235 if (rtype_p != NULL)
1241 #if FFI_NATIVE_RAW_API
1242 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1243 # define FFI_RAW_SIZE ffi_raw_size
1245 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1246 # define FFI_RAW_SIZE ffi_java_raw_size
1249 /* we put this one here, and not in interpret.cc because it
1250 * calls the utility routines _Jv_count_arguments
1251 * which are static to this module. The following struct defines the
1252 * layout we use for the stubs, it's only used in the ncode method. */
1255 ffi_raw_closure closure;
1257 ffi_type *arg_types[0];
1260 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1263 _Jv_InterpMethod::ncode ()
1265 using namespace java::lang::reflect;
1267 if (self->ncode != 0)
1270 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1271 int arg_count = _Jv_count_arguments (self->signature, staticp);
1273 ncode_closure *closure =
1274 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1275 + arg_count * sizeof (ffi_type*));
1277 _Jv_init_cif (self->signature,
1281 &closure->arg_types[0],
1284 ffi_closure_fun fun;
1286 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1288 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1290 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1294 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1295 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
1297 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1301 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1302 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
1304 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1311 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1312 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
1314 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
1318 if (::gnu::classpath::jdwp::Jdwp::isDebugging)
1319 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
1321 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1325 FFI_PREP_RAW_CLOSURE (&closure->closure,
1330 self->ncode = (void*)closure;
1334 /* Find the index of the given insn in the array of insn slots
1335 for this method. Returns -1 if not found. */
1337 _Jv_InterpMethod::insn_index (pc_t pc)
1340 #ifdef DIRECT_THREADED
1341 jlong right = number_insn_slots;
1342 pc_t insns = prepared;
1344 jlong right = code_length;
1345 pc_t insns = bytecode ();
1350 jlong mid = (left + right) / 2;
1351 if (&insns[mid] == pc)
1354 if (pc < &insns[mid])
1364 _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
1365 jintArray& line_numbers,
1366 jlongArray& code_indices)
1368 #ifdef DIRECT_THREADED
1369 /* For the DIRECT_THREADED case, if the method has not yet been
1370 * compiled, the linetable will change to insn slots instead of
1371 * bytecode PCs. It is probably easiest, in this case, to simply
1372 * compile the method and guarantee that we are using insn
1375 _Jv_CompileMethod (this);
1377 if (line_table_len > 0)
1380 end = number_insn_slots;
1381 line_numbers = JvNewIntArray (line_table_len);
1382 code_indices = JvNewLongArray (line_table_len);
1384 jint* lines = elements (line_numbers);
1385 jlong* indices = elements (code_indices);
1386 for (int i = 0; i < line_table_len; ++i)
1388 lines[i] = line_table[i].line;
1389 indices[i] = insn_index (line_table[i].pc);
1392 #else // !DIRECT_THREADED
1393 if (line_table_len > 0)
1397 line_numbers = JvNewIntArray (line_table_len);
1398 code_indices = JvNewLongArray (line_table_len);
1400 jint* lines = elements (line_numbers);
1401 jlong* indices = elements (code_indices);
1402 for (int i = 0; i < line_table_len; ++i)
1404 lines[i] = line_table[i].line;
1405 indices[i] = (jlong) line_table[i].bytecode_pc;
1408 #endif // !DIRECT_THREADED
1412 _Jv_InterpMethod::install_break (jlong index)
1414 return set_insn (index, breakpoint_insn);
1418 _Jv_InterpMethod::get_insn (jlong index)
1422 #ifdef DIRECT_THREADED
1423 if (index >= number_insn_slots || index < 0)
1427 #else // !DIRECT_THREADED
1428 if (index >= code_length || index < 0)
1431 code = reinterpret_cast<pc_t> (bytecode ());
1432 #endif // !DIRECT_THREADED
1434 return &code[index];
1438 _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
1440 #ifdef DIRECT_THREADED
1441 if (index >= number_insn_slots || index < 0)
1444 pc_t code = prepared;
1445 code[index].insn = insn->insn;
1446 #else // !DIRECT_THREADED
1447 if (index >= code_length || index < 0)
1450 pc_t code = reinterpret_cast<pc_t> (bytecode ());
1451 code[index] = *insn;
1452 #endif // !DIRECT_THREADED
1454 return &code[index];
1458 _Jv_JNIMethod::ncode ()
1460 using namespace java::lang::reflect;
1462 if (self->ncode != 0)
1465 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1466 int arg_count = _Jv_count_arguments (self->signature, staticp);
1468 ncode_closure *closure =
1469 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1470 + arg_count * sizeof (ffi_type*));
1473 _Jv_init_cif (self->signature,
1477 &closure->arg_types[0],
1480 ffi_closure_fun fun;
1482 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1484 // Initialize the argument types and CIF that represent the actual
1485 // underlying JNI function.
1487 if ((self->accflags & Modifier::STATIC))
1489 jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
1490 * sizeof (ffi_type *));
1492 jni_arg_types[offset++] = &ffi_type_pointer;
1493 if ((self->accflags & Modifier::STATIC))
1494 jni_arg_types[offset++] = &ffi_type_pointer;
1495 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1496 arg_count * sizeof (ffi_type *));
1498 if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
1499 extra_args + arg_count, rtype,
1500 jni_arg_types) != FFI_OK)
1501 throw_internal_error ("ffi_prep_cif failed for JNI function");
1503 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1505 // FIXME: for now we assume that all native methods for
1506 // interpreted code use JNI.
1507 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1509 FFI_PREP_RAW_CLOSURE (&closure->closure,
1514 self->ncode = (void *) closure;
1519 throw_class_format_error (jstring msg)
1522 ? new java::lang::ClassFormatError (msg)
1523 : new java::lang::ClassFormatError);
1527 throw_class_format_error (const char *msg)
1529 throw_class_format_error (JvNewStringLatin1 (msg));
1535 _Jv_InterpreterEngine::do_verify (jclass klass)
1537 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1538 for (int i = 0; i < klass->method_count; i++)
1540 using namespace java::lang::reflect;
1541 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1542 _Jv_ushort accflags = klass->methods[i].accflags;
1543 if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
1545 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1546 _Jv_VerifyMethod (im);
1552 _Jv_InterpreterEngine::do_create_ncode (jclass klass)
1554 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1555 for (int i = 0; i < klass->method_count; i++)
1557 // Just skip abstract methods. This is particularly important
1558 // because we don't resize the interpreted_methods array when
1559 // miranda methods are added to it.
1560 if ((klass->methods[i].accflags
1561 & java::lang::reflect::Modifier::ABSTRACT)
1565 _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
1567 if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
1570 // You might think we could use a virtual `ncode' method in
1571 // the _Jv_MethodBase and unify the native and non-native
1572 // cases. Well, we can't, because we don't allocate these
1573 // objects using `new', and thus they don't get a vtable.
1574 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
1575 klass->methods[i].ncode = jnim->ncode ();
1577 else if (imeth != 0) // it could be abstract
1579 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
1580 klass->methods[i].ncode = im->ncode ();
1586 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
1590 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1592 // Splitting the allocations here lets us scan reference fields and
1593 // avoid scanning non-reference fields. How reference fields are
1594 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1595 // means that this memory will be scanned conservatively (same
1596 // difference, since we know all the contents here are pointers).
1597 // Then we put pointers into this memory into the 'fields'
1598 // structure. Most of these are interior pointers, which is ok (but
1599 // even so the pointer to the first reference field will be used and
1600 // that is not an interior pointer). The 'fields' array is also
1601 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1602 // be scanned. A pointer to this array is held by Class and thus
1603 // seen by the collector.
1604 char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
1605 char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
1607 for (int i = 0; i < klass->field_count; i++)
1609 _Jv_Field *field = &klass->fields[i];
1611 if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
1614 char *base = field->isRef() ? reference_fields : non_reference_fields;
1615 field->u.addr = base + field->u.boffset;
1617 if (iclass->field_initializers[i] != 0)
1619 _Jv_Linker::resolve_field (field, klass->loader);
1620 _Jv_InitField (0, klass, i);
1624 // Now we don't need the field_initializers anymore, so let the
1625 // collector get rid of it.
1626 iclass->field_initializers = 0;
1629 _Jv_ResolvedMethod *
1630 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
1633 int arg_count = _Jv_count_arguments (method->signature, staticp);
1635 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1636 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1637 + arg_count*sizeof (ffi_type*));
1639 result->stack_item_count
1640 = _Jv_init_cif (method->signature,
1644 &result->arg_types[0],
1647 result->method = method;
1648 result->klass = klass;
1654 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
1656 _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
1657 for (int i = 0; i < klass->method_count; i++)
1659 // Just skip abstract methods. This is particularly important
1660 // because we don't resize the interpreted_methods array when
1661 // miranda methods are added to it.
1662 if ((klass->methods[i].accflags
1663 & java::lang::reflect::Modifier::ABSTRACT)
1666 // Miranda method additions mean that the `methods' array moves.
1667 // We cache a pointer into this array, so we have to update.
1668 iclass->interpreted_methods[i]->self = &klass->methods[i];
1672 #ifdef DIRECT_THREADED
1674 _Jv_CompileMethod (_Jv_InterpMethod* method)
1676 if (method->prepared == NULL)
1677 _Jv_InterpMethod::run (NULL, NULL, method);
1679 #endif // DIRECT_THREADED
1681 #endif // INTERPRETER