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/gcj/jvmti/Breakpoint.h>
44 #include <gnu/gcj/jvmti/BreakpointManager.h>
45 #include <gnu/gcj/jvmti/ExceptionEvent.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 static void find_catch_location (jthrowable
, jthread
, jmethodID
*, jlong
*);
70 // A macro to facilitate JVMTI exception reporting
71 #define REPORT_EXCEPTION(Jthrowable) \
73 if (JVMTI_REQUESTED_EVENT (Exception)) \
74 _Jv_ReportJVMTIExceptionThrow (Jthrowable); \
78 #ifdef DIRECT_THREADED
79 // Lock to ensure that methods are not compiled concurrently.
80 // We could use a finer-grained lock here, however it is not safe to use
81 // the Class monitor as user code in another thread could hold it.
82 static _Jv_Mutex_t compile_mutex
;
87 _Jv_MutexInit (&compile_mutex
);
90 void _Jv_InitInterpreter() {}
93 // The breakpoint instruction. For the direct threaded case,
94 // _Jv_InterpMethod::compile will initialize breakpoint_insn
95 // the first time it is called.
96 #ifdef DIRECT_THREADED
97 insn_slot
_Jv_InterpMethod::bp_insn_slot
;
98 pc_t
_Jv_InterpMethod::breakpoint_insn
= NULL
;
100 unsigned char _Jv_InterpMethod::bp_insn_opcode
101 = static_cast<unsigned char> (op_breakpoint
);
102 pc_t
_Jv_InterpMethod::breakpoint_insn
= &_Jv_InterpMethod::bp_insn_opcode
;
105 extern "C" double __ieee754_fmod (double,double);
107 static inline void dupx (_Jv_word
*sp
, int n
, int x
)
109 // first "slide" n+x elements n to the right
111 for (int i
= 0; i
< n
+x
; i
++)
113 sp
[(top
-i
)] = sp
[(top
-i
)-n
];
116 // next, copy the n top elements, n+x down
117 for (int i
= 0; i
< n
; i
++)
119 sp
[top
-(n
+x
)-i
] = sp
[top
-i
];
123 // Used to convert from floating types to integral types.
124 template<typename TO
, typename FROM
>
126 convert (FROM val
, TO min
, TO max
)
129 if (val
>= (FROM
) max
)
131 else if (val
<= (FROM
) min
)
140 #define PUSHA(V) (sp++)->o = (V)
141 #define PUSHI(V) (sp++)->i = (V)
142 #define PUSHF(V) (sp++)->f = (V)
143 #if SIZEOF_VOID_P == 8
144 # define PUSHL(V) (sp->l = (V), sp += 2)
145 # define PUSHD(V) (sp->d = (V), sp += 2)
147 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
148 (sp++)->ia[0] = w2.ia[0]; \
149 (sp++)->ia[0] = w2.ia[1]; } while (0)
150 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
151 (sp++)->ia[0] = w2.ia[0]; \
152 (sp++)->ia[0] = w2.ia[1]; } while (0)
155 #define POPA() ((--sp)->o)
156 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
157 #define POPF() ((jfloat) (--sp)->f)
158 #if SIZEOF_VOID_P == 8
159 # define POPL() (sp -= 2, (jlong) sp->l)
160 # define POPD() (sp -= 2, (jdouble) sp->d)
162 # define POPL() ({ _Jv_word2 w2; \
163 w2.ia[1] = (--sp)->ia[0]; \
164 w2.ia[0] = (--sp)->ia[0]; w2.l; })
165 # define POPD() ({ _Jv_word2 w2; \
166 w2.ia[1] = (--sp)->ia[0]; \
167 w2.ia[0] = (--sp)->ia[0]; w2.d; })
170 #define LOADA(I) (sp++)->o = locals[I].o
171 #define LOADI(I) (sp++)->i = locals[I].i
172 #define LOADF(I) (sp++)->f = locals[I].f
173 #if SIZEOF_VOID_P == 8
174 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
175 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
177 # define LOADL(I) do { jint __idx = (I); \
178 (sp++)->ia[0] = locals[__idx].ia[0]; \
179 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
181 # define LOADD(I) LOADL(I)
186 DEBUG_LOCALS_INSN (I, 'o'); \
187 locals[I].o = (--sp)->o; \
191 DEBUG_LOCALS_INSN (I, 'i'); \
192 locals[I].i = (--sp)->i; \
196 DEBUG_LOCALS_INSN (I, 'f'); \
197 locals[I].f = (--sp)->f; \
199 #if SIZEOF_VOID_P == 8
202 DEBUG_LOCALS_INSN (I, 'l'); \
203 DEBUG_LOCALS_INSN (I + 1, 'x'); \
204 (sp -= 2, locals[I].l = sp->l); \
208 DEBUG_LOCALS_INSN (I, 'd'); \
209 DEBUG_LOCALS_INSN (I + 1, 'x'); \
210 (sp -= 2, locals[I].d = sp->d); \
216 DEBUG_LOCALS_INSN (I, 'l'); \
217 DEBUG_LOCALS_INSN (I + 1, 'x'); \
219 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
220 locals[__idx].ia[0] = (--sp)->ia[0]; \
224 DEBUG_LOCALS_INSN (I, 'd'); \
225 DEBUG_LOCALS_INSN (I + 1, 'x'); \
227 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
228 locals[__idx].ia[0] = (--sp)->ia[0]; \
232 #define PEEKI(I) (locals+(I))->i
233 #define PEEKA(I) (locals+(I))->o
236 DEBUG_LOCALS_INSN(I,'i'); \
237 ((locals+(I))->i = (V))
240 #define BINOPI(OP) { \
241 jint value2 = POPI(); \
242 jint value1 = POPI(); \
243 PUSHI(value1 OP value2); \
246 #define BINOPF(OP) { \
247 jfloat value2 = POPF(); \
248 jfloat value1 = POPF(); \
249 PUSHF(value1 OP value2); \
252 #define BINOPL(OP) { \
253 jlong value2 = POPL(); \
254 jlong value1 = POPL(); \
255 PUSHL(value1 OP value2); \
258 #define BINOPD(OP) { \
259 jdouble value2 = POPD(); \
260 jdouble value1 = POPD(); \
261 PUSHD(value1 OP value2); \
265 get1s (unsigned char* loc
)
267 return *(signed char*)loc
;
271 get1u (unsigned char* loc
)
277 get2s(unsigned char* loc
)
279 return (((jint
)*(signed char*)loc
) << 8) | ((jint
)*(loc
+1));
283 get2u (unsigned char* loc
)
285 return (((jint
)(*loc
)) << 8) | ((jint
)*(loc
+1));
289 get4 (unsigned char* loc
)
291 return (((jint
)(loc
[0])) << 24)
292 | (((jint
)(loc
[1])) << 16)
293 | (((jint
)(loc
[2])) << 8)
294 | (((jint
)(loc
[3])) << 0);
297 #define SAVE_PC() frame_desc.pc = pc
299 // We used to define this conditionally, depending on HANDLE_SEGV.
300 // However, that runs into a problem if a chunk in low memory is
301 // mapped and we try to look at a field near the end of a large
302 // object. See PR 26858 for details. It is, most likely, relatively
303 // inexpensive to simply do this check always.
304 #define NULLCHECK(X) \
305 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
307 // Note that we can still conditionally define NULLARRAYCHECK, since
308 // we know that all uses of an array will first reference the length
309 // field, which is first -- and thus will trigger a SEGV.
311 #define NULLARRAYCHECK(X) SAVE_PC()
313 #define NULLARRAYCHECK(X) \
317 if ((X) == NULL) { throw_null_pointer_exception (); } \
321 #define ARRAYBOUNDSCHECK(array, index) \
324 if (((unsigned) index) >= (unsigned) (array->length)) \
325 _Jv_ThrowBadArrayIndex (index); \
329 _Jv_InterpMethod::run_normal (ffi_cif
*,
334 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
335 run (ret
, args
, _this
);
339 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
344 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
345 run_debug (ret
, args
, _this
);
349 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
354 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
356 jobject rcv
= (jobject
) args
[0].ptr
;
357 JvSynchronize
mutex (rcv
);
359 run (ret
, args
, _this
);
363 _Jv_InterpMethod::run_synch_object_debug (ffi_cif
*,
368 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
370 jobject rcv
= (jobject
) args
[0].ptr
;
371 JvSynchronize
mutex (rcv
);
373 run_debug (ret
, args
, _this
);
377 _Jv_InterpMethod::run_class (ffi_cif
*,
382 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
383 _Jv_InitClass (_this
->defining_class
);
384 run (ret
, args
, _this
);
388 _Jv_InterpMethod::run_class_debug (ffi_cif
*,
393 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
394 _Jv_InitClass (_this
->defining_class
);
395 run_debug (ret
, args
, _this
);
399 _Jv_InterpMethod::run_synch_class (ffi_cif
*,
404 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
406 jclass sync
= _this
->defining_class
;
407 _Jv_InitClass (sync
);
408 JvSynchronize
mutex (sync
);
410 run (ret
, args
, _this
);
414 _Jv_InterpMethod::run_synch_class_debug (ffi_cif
*,
419 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
421 jclass sync
= _this
->defining_class
;
422 _Jv_InitClass (sync
);
423 JvSynchronize
mutex (sync
);
425 run_debug (ret
, args
, _this
);
428 #ifdef DIRECT_THREADED
429 // "Compile" a method by turning it from bytecode to direct-threaded
432 _Jv_InterpMethod::compile (const void * const *insn_targets
)
434 insn_slot
*insns
= NULL
;
436 unsigned char *codestart
= bytecode ();
437 unsigned char *end
= codestart
+ code_length
;
438 _Jv_word
*pool_data
= defining_class
->constants
.data
;
440 #define SET_ONE(Field, Value) \
446 insns[next++].Field = Value; \
450 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
451 #define SET_INT(Value) SET_ONE (int_val, Value)
452 #define SET_DATUM(Value) SET_ONE (datum, Value)
454 // Map from bytecode PC to slot in INSNS.
455 int *pc_mapping
= (int *) __builtin_alloca (sizeof (int) * code_length
);
456 for (int i
= 0; i
< code_length
; ++i
)
459 for (int i
= 0; i
< 2; ++i
)
461 jboolean first_pass
= i
== 0;
465 insns
= (insn_slot
*) _Jv_AllocBytes (sizeof (insn_slot
) * next
);
466 number_insn_slots
= next
;
470 unsigned char *pc
= codestart
;
473 int base_pc_val
= pc
- codestart
;
475 pc_mapping
[base_pc_val
] = next
;
477 java_opcode opcode
= (java_opcode
) *pc
++;
479 if (opcode
== op_nop
)
481 SET_INSN (insn_targets
[opcode
]);
622 case op_monitorenter
:
632 // No argument, nothing else to do.
636 SET_INT (get1s (pc
));
642 int index
= get1u (pc
);
644 // For an unresolved class we want to delay resolution
646 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
649 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
653 SET_DATUM (pool_data
[index
].o
);
669 SET_INT (get1u (pc
));
674 SET_INT (get1u (pc
));
675 SET_INT (get1s (pc
+ 1));
681 int index
= get2u (pc
);
683 // For an unresolved class we want to delay resolution
685 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
688 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
692 SET_DATUM (pool_data
[index
].o
);
698 int index
= get2u (pc
);
700 SET_DATUM (&pool_data
[index
]);
705 SET_INT (get2s (pc
));
717 case op_invokespecial
:
718 case op_invokestatic
:
719 case op_invokevirtual
:
720 SET_INT (get2u (pc
));
724 case op_multianewarray
:
725 SET_INT (get2u (pc
));
726 SET_INT (get1u (pc
+ 2));
749 int offset
= get2s (pc
);
752 int new_pc
= base_pc_val
+ offset
;
754 bool orig_was_goto
= opcode
== op_goto
;
756 // Thread jumps. We limit the loop count; this lets
757 // us avoid infinite loops if the bytecode contains
758 // such. `10' is arbitrary.
760 while (codestart
[new_pc
] == op_goto
&& count
-- > 0)
761 new_pc
+= get2s (&codestart
[new_pc
+ 1]);
763 // If the jump takes us to a `return' instruction and
764 // the original branch was an unconditional goto, then
765 // we hoist the return.
766 opcode
= (java_opcode
) codestart
[new_pc
];
768 && (opcode
== op_ireturn
|| opcode
== op_lreturn
769 || opcode
== op_freturn
|| opcode
== op_dreturn
770 || opcode
== op_areturn
|| opcode
== op_return
))
773 SET_INSN (insn_targets
[opcode
]);
776 SET_DATUM (&insns
[pc_mapping
[new_pc
]]);
782 while ((pc
- codestart
) % 4 != 0)
785 jint def
= get4 (pc
);
786 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
792 int high
= get4 (pc
);
796 for (int i
= low
; i
<= high
; ++i
)
798 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ get4 (pc
)]]);
804 case op_lookupswitch
:
806 while ((pc
- codestart
) % 4 != 0)
809 jint def
= get4 (pc
);
810 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
813 jint npairs
= get4 (pc
);
819 jint match
= get4 (pc
);
820 jint offset
= get4 (pc
+ 4);
822 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
828 case op_invokeinterface
:
830 jint index
= get2u (pc
);
832 // We ignore the next two bytes.
840 opcode
= (java_opcode
) get1u (pc
);
842 jint val
= get2u (pc
);
845 // We implement narrow and wide instructions using the
846 // same code in the interpreter. So we rewrite the
847 // instruction slot here.
849 insns
[next
- 1].insn
= (void *) insn_targets
[opcode
];
852 if (opcode
== op_iinc
)
854 SET_INT (get2s (pc
));
863 jint offset
= get4 (pc
);
865 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
869 // Some "can't happen" cases that we include for
870 // error-checking purposes.
888 case op_getstatic_2s
:
889 case op_getstatic_2u
:
901 // Now update exceptions.
902 _Jv_InterpException
*exc
= exceptions ();
903 for (int i
= 0; i
< exc_count
; ++i
)
905 exc
[i
].start_pc
.p
= &insns
[pc_mapping
[exc
[i
].start_pc
.i
]];
906 exc
[i
].end_pc
.p
= &insns
[pc_mapping
[exc
[i
].end_pc
.i
]];
907 exc
[i
].handler_pc
.p
= &insns
[pc_mapping
[exc
[i
].handler_pc
.i
]];
908 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
909 // during compilation.
911 = (_Jv_Linker::resolve_pool_entry (defining_class
,
912 exc
[i
].handler_type
.i
)).clazz
;
913 exc
[i
].handler_type
.p
= handler
;
916 // Translate entries in the LineNumberTable from bytecode PC's to direct
917 // threaded interpreter instruction values.
918 for (int i
= 0; i
< line_table_len
; i
++)
920 int byte_pc
= line_table
[i
].bytecode_pc
;
921 // It isn't worth throwing an exception if this table is
922 // corrupted, but at the same time we don't want a crash.
923 if (byte_pc
< 0 || byte_pc
>= code_length
)
925 line_table
[i
].pc
= &insns
[pc_mapping
[byte_pc
]];
930 if (breakpoint_insn
== NULL
)
932 bp_insn_slot
.insn
= const_cast<void *> (insn_targets
[op_breakpoint
]);
933 breakpoint_insn
= &bp_insn_slot
;
936 #endif /* DIRECT_THREADED */
938 /* Run the given method.
939 When args is NULL, don't run anything -- just compile it. */
941 _Jv_InterpMethod::run (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
944 #undef DEBUG_LOCALS_INSN
945 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
947 #include "interpret-run.cc"
951 _Jv_InterpMethod::run_debug (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
954 #undef DEBUG_LOCALS_INSN
955 #define DEBUG_LOCALS_INSN(s, t) \
958 frame_desc.locals_type[s] = t; \
962 #include "interpret-run.cc"
966 throw_internal_error (const char *msg
)
968 jthrowable t
= new java::lang::InternalError (JvNewStringLatin1 (msg
));
969 REPORT_EXCEPTION (t
);
974 throw_incompatible_class_change_error (jstring msg
)
976 jthrowable t
= new java::lang::IncompatibleClassChangeError (msg
);
977 REPORT_EXCEPTION (t
);
982 throw_null_pointer_exception ()
984 jthrowable t
= new java::lang::NullPointerException
;
985 REPORT_EXCEPTION (t
);
989 /* Look up source code line number for given bytecode (or direct threaded
992 _Jv_InterpMethod::get_source_line(pc_t mpc
)
994 int line
= line_table_len
> 0 ? line_table
[0].line
: -1;
995 for (int i
= 1; i
< line_table_len
; i
++)
996 if (line_table
[i
].pc
> mpc
)
999 line
= line_table
[i
].line
;
1004 /** Do static initialization for fields with a constant initializer */
1006 _Jv_InitField (jobject obj
, jclass klass
, int index
)
1008 using namespace java::lang::reflect
;
1010 if (obj
!= 0 && klass
== 0)
1011 klass
= obj
->getClass ();
1013 if (!_Jv_IsInterpretedClass (klass
))
1016 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*)klass
->aux_info
;
1018 _Jv_Field
* field
= (&klass
->fields
[0]) + index
;
1020 if (index
> klass
->field_count
)
1021 throw_internal_error ("field out of range");
1023 int init
= iclass
->field_initializers
[index
];
1027 _Jv_Constants
*pool
= &klass
->constants
;
1028 int tag
= pool
->tags
[init
];
1030 if (! field
->isResolved ())
1031 throw_internal_error ("initializing unresolved field");
1033 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
1034 throw_internal_error ("initializing non-static field with no object");
1038 if ((field
->flags
& Modifier::STATIC
) != 0)
1039 addr
= (void*) field
->u
.addr
;
1041 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
1045 case JV_CONSTANT_String
:
1048 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
1049 pool
->data
[init
].string
= str
;
1050 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
1054 case JV_CONSTANT_ResolvedString
:
1055 if (! (field
->type
== &java::lang::String::class$
1056 || field
->type
== &java::lang::Class::class$
))
1057 throw_class_format_error ("string initialiser to non-string field");
1059 *(jstring
*)addr
= pool
->data
[init
].string
;
1062 case JV_CONSTANT_Integer
:
1064 int value
= pool
->data
[init
].i
;
1066 if (field
->type
== JvPrimClass (boolean
))
1067 *(jboolean
*)addr
= (jboolean
)value
;
1069 else if (field
->type
== JvPrimClass (byte
))
1070 *(jbyte
*)addr
= (jbyte
)value
;
1072 else if (field
->type
== JvPrimClass (char))
1073 *(jchar
*)addr
= (jchar
)value
;
1075 else if (field
->type
== JvPrimClass (short))
1076 *(jshort
*)addr
= (jshort
)value
;
1078 else if (field
->type
== JvPrimClass (int))
1079 *(jint
*)addr
= (jint
)value
;
1082 throw_class_format_error ("erroneous field initializer");
1086 case JV_CONSTANT_Long
:
1087 if (field
->type
!= JvPrimClass (long))
1088 throw_class_format_error ("erroneous field initializer");
1090 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
1093 case JV_CONSTANT_Float
:
1094 if (field
->type
!= JvPrimClass (float))
1095 throw_class_format_error ("erroneous field initializer");
1097 *(jfloat
*)addr
= pool
->data
[init
].f
;
1100 case JV_CONSTANT_Double
:
1101 if (field
->type
!= JvPrimClass (double))
1102 throw_class_format_error ("erroneous field initializer");
1104 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
1108 throw_class_format_error ("erroneous field initializer");
1112 inline static unsigned char*
1113 skip_one_type (unsigned char* ptr
)
1124 do { ch
= *ptr
++; } while (ch
!= ';');
1131 get_ffi_type_from_signature (unsigned char* ptr
)
1137 return &ffi_type_pointer
;
1141 // On some platforms a bool is a byte, on others an int.
1142 if (sizeof (jboolean
) == sizeof (jbyte
))
1143 return &ffi_type_sint8
;
1146 JvAssert (sizeof (jbyte
) == sizeof (jint
));
1147 return &ffi_type_sint32
;
1152 return &ffi_type_sint8
;
1156 return &ffi_type_uint16
;
1160 return &ffi_type_sint16
;
1164 return &ffi_type_sint32
;
1168 return &ffi_type_sint64
;
1172 return &ffi_type_float
;
1176 return &ffi_type_double
;
1180 return &ffi_type_void
;
1184 throw_internal_error ("unknown type in signature");
1187 /* this function yields the number of actual arguments, that is, if the
1188 * function is non-static, then one is added to the number of elements
1189 * found in the signature */
1192 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
1195 unsigned char *ptr
= (unsigned char*) signature
->chars();
1196 int arg_count
= staticp
? 0 : 1;
1198 /* first, count number of arguments */
1206 ptr
= skip_one_type (ptr
);
1213 /* This beast will build a cif, given the signature. Memory for
1214 * the cif itself and for the argument types must be allocated by the
1219 _Jv_init_cif (_Jv_Utf8Const
* signature
,
1223 ffi_type
**arg_types
,
1226 unsigned char *ptr
= (unsigned char*) signature
->chars();
1228 int arg_index
= 0; // arg number
1229 int item_count
= 0; // stack-item count
1234 arg_types
[arg_index
++] = &ffi_type_pointer
;
1244 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1246 if (*ptr
== 'J' || *ptr
== 'D')
1251 ptr
= skip_one_type (ptr
);
1256 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1258 ptr
= skip_one_type (ptr
);
1259 if (ptr
!= (unsigned char*)signature
->chars() + signature
->len())
1260 throw_internal_error ("did not find end of signature");
1262 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1263 arg_count
, rtype
, arg_types
) != FFI_OK
)
1264 throw_internal_error ("ffi_prep_cif failed");
1266 if (rtype_p
!= NULL
)
1272 #if FFI_NATIVE_RAW_API
1273 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1274 # define FFI_RAW_SIZE ffi_raw_size
1276 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1277 # define FFI_RAW_SIZE ffi_java_raw_size
1280 /* we put this one here, and not in interpret.cc because it
1281 * calls the utility routines _Jv_count_arguments
1282 * which are static to this module. The following struct defines the
1283 * layout we use for the stubs, it's only used in the ncode method. */
1286 ffi_raw_closure closure
;
1287 _Jv_ClosureList list
;
1289 ffi_type
*arg_types
[0];
1292 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
1295 _Jv_InterpMethod::ncode (jclass klass
)
1297 using namespace java::lang::reflect
;
1299 if (self
->ncode
!= 0)
1302 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1303 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1306 ncode_closure
*closure
=
1307 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1308 + arg_count
* sizeof (ffi_type
*),
1310 closure
->list
.registerClosure (klass
, closure
);
1312 _Jv_init_cif (self
->signature
,
1316 &closure
->arg_types
[0],
1319 ffi_closure_fun fun
;
1321 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1323 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1325 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1330 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1332 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1337 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1339 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1347 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1349 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1354 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1356 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1360 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1371 /* Find the index of the given insn in the array of insn slots
1372 for this method. Returns -1 if not found. */
1374 _Jv_InterpMethod::insn_index (pc_t pc
)
1377 #ifdef DIRECT_THREADED
1378 jlong right
= number_insn_slots
;
1379 pc_t insns
= prepared
;
1381 jlong right
= code_length
;
1382 pc_t insns
= bytecode ();
1387 jlong mid
= (left
+ right
) / 2;
1388 if (&insns
[mid
] == pc
)
1391 if (pc
< &insns
[mid
])
1400 // Method to check if an exception is caught at some location in a method
1401 // (meth). Returns true if this method (meth) contains a catch block for the
1402 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1403 // to the location of the beginning of the catch block.
1405 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1406 java::lang::Throwable
*ex
)
1408 #ifdef DIRECT_THREADED
1409 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1411 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1413 _Jv_InterpException
*exc
= meth
->exceptions ();
1414 jclass exc_class
= ex
->getClass ();
1416 for (int i
= 0; i
< meth
->exc_count
; i
++)
1418 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1419 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1421 #ifdef DIRECT_THREADED
1422 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1424 jclass handler
= NULL
;
1425 if (exc
[i
].handler_type
.i
!= 0)
1427 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1429 #endif /* DIRECT_THREADED */
1430 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1432 #ifdef DIRECT_THREADED
1433 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1435 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1436 #endif /* DIRECT_THREADED */
1446 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1447 jintArray
& line_numbers
,
1448 jlongArray
& code_indices
)
1450 #ifdef DIRECT_THREADED
1451 /* For the DIRECT_THREADED case, if the method has not yet been
1452 * compiled, the linetable will change to insn slots instead of
1453 * bytecode PCs. It is probably easiest, in this case, to simply
1454 * compile the method and guarantee that we are using insn
1457 _Jv_CompileMethod (this);
1459 if (line_table_len
> 0)
1462 end
= number_insn_slots
;
1463 line_numbers
= JvNewIntArray (line_table_len
);
1464 code_indices
= JvNewLongArray (line_table_len
);
1466 jint
* lines
= elements (line_numbers
);
1467 jlong
* indices
= elements (code_indices
);
1468 for (int i
= 0; i
< line_table_len
; ++i
)
1470 lines
[i
] = line_table
[i
].line
;
1471 indices
[i
] = insn_index (line_table
[i
].pc
);
1474 #else // !DIRECT_THREADED
1475 if (line_table_len
> 0)
1479 line_numbers
= JvNewIntArray (line_table_len
);
1480 code_indices
= JvNewLongArray (line_table_len
);
1482 jint
* lines
= elements (line_numbers
);
1483 jlong
* indices
= elements (code_indices
);
1484 for (int i
= 0; i
< line_table_len
; ++i
)
1486 lines
[i
] = line_table
[i
].line
;
1487 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1490 #endif // !DIRECT_THREADED
1494 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1495 char **generic_sig
, jlong
*startloc
,
1496 jint
*length
, jint
*slot
,
1499 if (local_var_table
== NULL
)
1501 if (table_slot
>= local_var_table_len
)
1505 *name
= local_var_table
[table_slot
].name
;
1506 *sig
= local_var_table
[table_slot
].descriptor
;
1507 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1509 *startloc
= static_cast<jlong
>
1510 (local_var_table
[table_slot
].bytecode_start_pc
);
1511 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1512 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1514 return local_var_table_len
- table_slot
-1;
1518 _Jv_InterpMethod::install_break (jlong index
)
1520 return set_insn (index
, breakpoint_insn
);
1524 _Jv_InterpMethod::get_insn (jlong index
)
1528 #ifdef DIRECT_THREADED
1529 if (index
>= number_insn_slots
|| index
< 0)
1533 #else // !DIRECT_THREADED
1534 if (index
>= code_length
|| index
< 0)
1537 code
= reinterpret_cast<pc_t
> (bytecode ());
1538 #endif // !DIRECT_THREADED
1540 return &code
[index
];
1544 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1546 #ifdef DIRECT_THREADED
1547 if (index
>= number_insn_slots
|| index
< 0)
1550 pc_t code
= prepared
;
1551 code
[index
].insn
= insn
->insn
;
1552 #else // !DIRECT_THREADED
1553 if (index
>= code_length
|| index
< 0)
1556 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1557 code
[index
] = *insn
;
1558 #endif // !DIRECT_THREADED
1560 return &code
[index
];
1564 _Jv_JNIMethod::ncode (jclass klass
)
1566 using namespace java::lang::reflect
;
1568 if (self
->ncode
!= 0)
1571 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1572 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1575 ncode_closure
*closure
=
1576 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1577 + arg_count
* sizeof (ffi_type
*),
1579 closure
->list
.registerClosure (klass
, closure
);
1582 _Jv_init_cif (self
->signature
,
1586 &closure
->arg_types
[0],
1589 ffi_closure_fun fun
;
1591 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1593 // Initialize the argument types and CIF that represent the actual
1594 // underlying JNI function.
1596 if ((self
->accflags
& Modifier::STATIC
))
1598 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1599 * sizeof (ffi_type
*));
1601 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1602 if ((self
->accflags
& Modifier::STATIC
))
1603 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1604 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1605 arg_count
* sizeof (ffi_type
*));
1607 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1608 extra_args
+ arg_count
, rtype
,
1609 jni_arg_types
) != FFI_OK
)
1610 throw_internal_error ("ffi_prep_cif failed for JNI function");
1612 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1614 // FIXME: for now we assume that all native methods for
1615 // interpreted code use JNI.
1616 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1618 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1629 throw_class_format_error (jstring msg
)
1632 ? new java::lang::ClassFormatError (msg
)
1633 : new java::lang::ClassFormatError
);
1634 REPORT_EXCEPTION (t
);
1639 throw_class_format_error (const char *msg
)
1641 throw_class_format_error (JvNewStringLatin1 (msg
));
1644 /* This function finds the method and location where the exception EXC
1645 is caught in the stack frame. On return, it sets CATCH_METHOD and
1646 CATCH_LOCATION with the method and location where the catch will
1647 occur. If the exception is not caught, these are set to 0.
1649 This function should only be used with the DEBUG interpreter. */
1651 find_catch_location (::java::lang::Throwable
*exc
, jthread thread
,
1652 jmethodID
*catch_method
, jlong
*catch_loc
)
1657 _Jv_InterpFrame
*frame
1658 = reinterpret_cast<_Jv_InterpFrame
*> (thread
->interp_frame
);
1659 while (frame
!= NULL
)
1661 pc_t pc
= frame
->get_pc ();
1662 _Jv_InterpMethod
*imeth
1663 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1664 if (imeth
->check_handler (&pc
, imeth
, exc
))
1666 // This method handles the exception.
1667 *catch_method
= imeth
->get_method ();
1668 *catch_loc
= imeth
->insn_index (pc
);
1672 frame
= frame
->next_interp
;
1676 /* This method handles JVMTI notifications of thrown exceptions. It
1677 calls find_catch_location to figure out where the exception is
1678 caught (if it is caught).
1680 Like find_catch_location, this should only be called with the
1681 DEBUG interpreter. Since a few exceptions occur outside the
1682 interpreter proper, it is important to not call this function
1683 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1685 _Jv_ReportJVMTIExceptionThrow (jthrowable ex
)
1687 jthread thread
= ::java::lang::Thread::currentThread ();
1688 _Jv_Frame
*frame
= reinterpret_cast<_Jv_Frame
*> (thread
->frame
);
1689 jmethodID throw_meth
= frame
->self
->get_method ();
1690 jlocation throw_loc
= -1;
1691 if (frame
->frame_type
== frame_interpreter
)
1693 _Jv_InterpFrame
* iframe
1694 = reinterpret_cast<_Jv_InterpFrame
*> (frame
);
1695 _Jv_InterpMethod
*imeth
1696 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1697 throw_loc
= imeth
->insn_index (iframe
->get_pc ());
1701 jmethodID catch_method
;
1702 find_catch_location (ex
, thread
, &catch_method
, &catch_loc
);
1703 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION
, thread
,
1704 _Jv_GetCurrentJNIEnv (), throw_meth
, throw_loc
,
1705 ex
, catch_method
, catch_loc
);
1711 _Jv_InterpreterEngine::do_verify (jclass klass
)
1713 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1714 for (int i
= 0; i
< klass
->method_count
; i
++)
1716 using namespace java::lang::reflect
;
1717 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1718 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1719 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1721 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1722 _Jv_VerifyMethod (im
);
1728 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1730 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1731 for (int i
= 0; i
< klass
->method_count
; i
++)
1733 // Just skip abstract methods. This is particularly important
1734 // because we don't resize the interpreted_methods array when
1735 // miranda methods are added to it.
1736 if ((klass
->methods
[i
].accflags
1737 & java::lang::reflect::Modifier::ABSTRACT
)
1741 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1743 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1746 // You might think we could use a virtual `ncode' method in
1747 // the _Jv_MethodBase and unify the native and non-native
1748 // cases. Well, we can't, because we don't allocate these
1749 // objects using `new', and thus they don't get a vtable.
1750 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1751 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1753 else if (imeth
!= 0) // it could be abstract
1755 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1756 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1762 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1764 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1766 if (!iclass
->closures
)
1767 iclass
->closures
= _Jv_ClosureListFinalizer ();
1769 return iclass
->closures
;
1773 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1777 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1779 // Splitting the allocations here lets us scan reference fields and
1780 // avoid scanning non-reference fields. How reference fields are
1781 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1782 // means that this memory will be scanned conservatively (same
1783 // difference, since we know all the contents here are pointers).
1784 // Then we put pointers into this memory into the 'fields'
1785 // structure. Most of these are interior pointers, which is ok (but
1786 // even so the pointer to the first reference field will be used and
1787 // that is not an interior pointer). The 'fields' array is also
1788 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1789 // be scanned. A pointer to this array is held by Class and thus
1790 // seen by the collector.
1791 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1792 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1794 for (int i
= 0; i
< klass
->field_count
; i
++)
1796 _Jv_Field
*field
= &klass
->fields
[i
];
1798 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1801 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1802 field
->u
.addr
= base
+ field
->u
.boffset
;
1804 if (iclass
->field_initializers
[i
] != 0)
1806 _Jv_Linker::resolve_field (field
, klass
->loader
);
1807 _Jv_InitField (0, klass
, i
);
1811 // Now we don't need the field_initializers anymore, so let the
1812 // collector get rid of it.
1813 iclass
->field_initializers
= 0;
1816 _Jv_ResolvedMethod
*
1817 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1820 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1822 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1823 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1824 + arg_count
*sizeof (ffi_type
*));
1826 result
->stack_item_count
1827 = _Jv_init_cif (method
->signature
,
1831 &result
->arg_types
[0],
1834 result
->method
= method
;
1835 result
->klass
= klass
;
1841 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1843 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1844 for (int i
= 0; i
< klass
->method_count
; i
++)
1846 // Just skip abstract methods. This is particularly important
1847 // because we don't resize the interpreted_methods array when
1848 // miranda methods are added to it.
1849 if ((klass
->methods
[i
].accflags
1850 & java::lang::reflect::Modifier::ABSTRACT
)
1853 // Miranda method additions mean that the `methods' array moves.
1854 // We cache a pointer into this array, so we have to update.
1855 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1859 #ifdef DIRECT_THREADED
1861 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1863 if (method
->prepared
== NULL
)
1866 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1868 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1871 #endif // DIRECT_THREADED
1873 #endif // INTERPRETER