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>
46 // Execution engine for interpreted code.
47 _Jv_InterpreterEngine _Jv_soleInterpreterEngine
;
53 static void throw_internal_error (const char *msg
)
54 __attribute__ ((__noreturn__
));
55 static void throw_incompatible_class_change_error (jstring msg
)
56 __attribute__ ((__noreturn__
));
57 static void throw_null_pointer_exception ()
58 __attribute__ ((__noreturn__
));
60 static void throw_class_format_error (jstring msg
)
61 __attribute__ ((__noreturn__
));
62 static void throw_class_format_error (const char *msg
)
63 __attribute__ ((__noreturn__
));
65 static void find_catch_location (jthrowable
, jthread
, jmethodID
*, jlong
*);
67 // A macro to facilitate JVMTI exception reporting
68 #define REPORT_EXCEPTION(Jthrowable) \
70 if (JVMTI_REQUESTED_EVENT (Exception)) \
71 _Jv_ReportJVMTIExceptionThrow (Jthrowable); \
75 #ifdef DIRECT_THREADED
76 // Lock to ensure that methods are not compiled concurrently.
77 // We could use a finer-grained lock here, however it is not safe to use
78 // the Class monitor as user code in another thread could hold it.
79 static _Jv_Mutex_t compile_mutex
;
81 // See class ThreadCountAdjuster and REWRITE_INSN for how this is
83 _Jv_Mutex_t
_Jv_InterpMethod::rewrite_insn_mutex
;
88 _Jv_MutexInit (&compile_mutex
);
89 _Jv_MutexInit (&_Jv_InterpMethod::rewrite_insn_mutex
);
92 void _Jv_InitInterpreter() {}
95 // The breakpoint instruction. For the direct threaded case,
96 // _Jv_InterpMethod::compile will initialize breakpoint_insn
97 // the first time it is called.
98 #ifdef DIRECT_THREADED
99 insn_slot
_Jv_InterpMethod::bp_insn_slot
;
100 pc_t
_Jv_InterpMethod::breakpoint_insn
= NULL
;
102 unsigned char _Jv_InterpMethod::bp_insn_opcode
103 = static_cast<unsigned char> (op_breakpoint
);
104 pc_t
_Jv_InterpMethod::breakpoint_insn
= &_Jv_InterpMethod::bp_insn_opcode
;
107 extern "C" double __ieee754_fmod (double,double);
109 static inline void dupx (_Jv_word
*sp
, int n
, int x
)
111 // first "slide" n+x elements n to the right
113 for (int i
= 0; i
< n
+x
; i
++)
115 sp
[(top
-i
)] = sp
[(top
-i
)-n
];
118 // next, copy the n top elements, n+x down
119 for (int i
= 0; i
< n
; i
++)
121 sp
[top
-(n
+x
)-i
] = sp
[top
-i
];
125 // Used to convert from floating types to integral types.
126 template<typename TO
, typename FROM
>
128 convert (FROM val
, TO min
, TO max
)
131 if (val
>= (FROM
) max
)
133 else if (val
<= (FROM
) min
)
142 #define PUSHA(V) (sp++)->o = (V)
143 #define PUSHI(V) (sp++)->i = (V)
144 #define PUSHF(V) (sp++)->f = (V)
145 #if SIZEOF_VOID_P == 8
146 # define PUSHL(V) (sp->l = (V), sp += 2)
147 # define PUSHD(V) (sp->d = (V), sp += 2)
149 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
150 (sp++)->ia[0] = w2.ia[0]; \
151 (sp++)->ia[0] = w2.ia[1]; } while (0)
152 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
153 (sp++)->ia[0] = w2.ia[0]; \
154 (sp++)->ia[0] = w2.ia[1]; } while (0)
157 #define POPA() ((--sp)->o)
158 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
159 #define POPF() ((jfloat) (--sp)->f)
160 #if SIZEOF_VOID_P == 8
161 # define POPL() (sp -= 2, (jlong) sp->l)
162 # define POPD() (sp -= 2, (jdouble) sp->d)
164 # define POPL() ({ _Jv_word2 w2; \
165 w2.ia[1] = (--sp)->ia[0]; \
166 w2.ia[0] = (--sp)->ia[0]; w2.l; })
167 # define POPD() ({ _Jv_word2 w2; \
168 w2.ia[1] = (--sp)->ia[0]; \
169 w2.ia[0] = (--sp)->ia[0]; w2.d; })
172 #define LOADA(I) (sp++)->o = locals[I].o
173 #define LOADI(I) (sp++)->i = locals[I].i
174 #define LOADF(I) (sp++)->f = locals[I].f
175 #if SIZEOF_VOID_P == 8
176 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
177 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
179 # define LOADL(I) do { jint __idx = (I); \
180 (sp++)->ia[0] = locals[__idx].ia[0]; \
181 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
183 # define LOADD(I) LOADL(I)
190 DEBUG_LOCALS_INSN (__idx, 'o'); \
191 locals[__idx].o = (--sp)->o; \
198 DEBUG_LOCALS_INSN (__idx, 'i'); \
199 locals[__idx].i = (--sp)->i; \
205 DEBUG_LOCALS_INSN (__idx, 'f'); \
206 locals[__idx].f = (--sp)->f; \
209 #if SIZEOF_VOID_P == 8
214 DEBUG_LOCALS_INSN (__idx, 'l'); \
215 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
216 (sp -= 2, locals[__idx].l = sp->l); \
223 DEBUG_LOCALS_INSN (__idx, 'd'); \
224 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
225 (sp -= 2, locals[__idx].d = sp->d); \
234 DEBUG_LOCALS_INSN (__idx, 'l'); \
235 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
236 locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
237 locals[__idx].ia[0] = (--sp)->ia[0]; \
243 DEBUG_LOCALS_INSN (__idx, 'd'); \
244 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
245 locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
246 locals[__idx].ia[0] = (--sp)->ia[0]; \
250 #define PEEKI(I) (locals+(I))->i
251 #define PEEKA(I) (locals+(I))->o
257 DEBUG_LOCALS_INSN (__idx, 'i'); \
258 ((locals + __idx)->i = (V)); \
263 #define BINOPI(OP) { \
264 jint value2 = POPI(); \
265 jint value1 = POPI(); \
266 PUSHI(value1 OP value2); \
269 #define BINOPF(OP) { \
270 jfloat value2 = POPF(); \
271 jfloat value1 = POPF(); \
272 PUSHF(value1 OP value2); \
275 #define BINOPL(OP) { \
276 jlong value2 = POPL(); \
277 jlong value1 = POPL(); \
278 PUSHL(value1 OP value2); \
281 #define BINOPD(OP) { \
282 jdouble value2 = POPD(); \
283 jdouble value1 = POPD(); \
284 PUSHD(value1 OP value2); \
288 get1s (unsigned char* loc
)
290 return *(signed char*)loc
;
294 get1u (unsigned char* loc
)
300 get2s(unsigned char* loc
)
302 return (((jint
)*(signed char*)loc
) << 8) | ((jint
)*(loc
+1));
306 get2u (unsigned char* loc
)
308 return (((jint
)(*loc
)) << 8) | ((jint
)*(loc
+1));
312 get4 (unsigned char* loc
)
314 return (((jint
)(loc
[0])) << 24)
315 | (((jint
)(loc
[1])) << 16)
316 | (((jint
)(loc
[2])) << 8)
317 | (((jint
)(loc
[3])) << 0);
320 #define SAVE_PC() frame_desc.pc = pc
322 // We used to define this conditionally, depending on HANDLE_SEGV.
323 // However, that runs into a problem if a chunk in low memory is
324 // mapped and we try to look at a field near the end of a large
325 // object. See PR 26858 for details. It is, most likely, relatively
326 // inexpensive to simply do this check always.
327 #define NULLCHECK(X) \
328 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
330 // Note that we can still conditionally define NULLARRAYCHECK, since
331 // we know that all uses of an array will first reference the length
332 // field, which is first -- and thus will trigger a SEGV.
334 #define NULLARRAYCHECK(X) SAVE_PC()
336 #define NULLARRAYCHECK(X) \
340 if ((X) == NULL) { throw_null_pointer_exception (); } \
344 #define ARRAYBOUNDSCHECK(array, index) \
347 if (((unsigned) index) >= (unsigned) (array->length)) \
348 _Jv_ThrowBadArrayIndex (index); \
352 _Jv_InterpMethod::run_normal (ffi_cif
*,
354 INTERP_FFI_RAW_TYPE
*args
,
357 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
358 run (ret
, args
, _this
);
362 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
364 INTERP_FFI_RAW_TYPE
*args
,
367 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
368 run_debug (ret
, args
, _this
);
372 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
374 INTERP_FFI_RAW_TYPE
*args
,
377 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
379 jobject rcv
= (jobject
) args
[0].ptr
;
380 JvSynchronize
mutex (rcv
);
382 run (ret
, args
, _this
);
386 _Jv_InterpMethod::run_synch_object_debug (ffi_cif
*,
388 INTERP_FFI_RAW_TYPE
*args
,
391 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
393 jobject rcv
= (jobject
) args
[0].ptr
;
394 JvSynchronize
mutex (rcv
);
396 run_debug (ret
, args
, _this
);
400 _Jv_InterpMethod::run_class (ffi_cif
*,
402 INTERP_FFI_RAW_TYPE
*args
,
405 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
406 _Jv_InitClass (_this
->defining_class
);
407 run (ret
, args
, _this
);
411 _Jv_InterpMethod::run_class_debug (ffi_cif
*,
413 INTERP_FFI_RAW_TYPE
*args
,
416 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
417 _Jv_InitClass (_this
->defining_class
);
418 run_debug (ret
, args
, _this
);
422 _Jv_InterpMethod::run_synch_class (ffi_cif
*,
424 INTERP_FFI_RAW_TYPE
*args
,
427 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
429 jclass sync
= _this
->defining_class
;
430 _Jv_InitClass (sync
);
431 JvSynchronize
mutex (sync
);
433 run (ret
, args
, _this
);
437 _Jv_InterpMethod::run_synch_class_debug (ffi_cif
*,
439 INTERP_FFI_RAW_TYPE
*args
,
442 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
444 jclass sync
= _this
->defining_class
;
445 _Jv_InitClass (sync
);
446 JvSynchronize
mutex (sync
);
448 run_debug (ret
, args
, _this
);
451 #ifdef DIRECT_THREADED
452 // "Compile" a method by turning it from bytecode to direct-threaded
455 _Jv_InterpMethod::compile (const void * const *insn_targets
)
457 insn_slot
*insns
= NULL
;
459 unsigned char *codestart
= bytecode ();
460 unsigned char *end
= codestart
+ code_length
;
461 _Jv_word
*pool_data
= defining_class
->constants
.data
;
463 #define SET_ONE(Field, Value) \
469 insns[next++].Field = Value; \
473 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
474 #define SET_INT(Value) SET_ONE (int_val, Value)
475 #define SET_DATUM(Value) SET_ONE (datum, Value)
477 // Map from bytecode PC to slot in INSNS.
478 int *pc_mapping
= (int *) __builtin_alloca (sizeof (int) * code_length
);
479 for (int i
= 0; i
< code_length
; ++i
)
482 for (int i
= 0; i
< 2; ++i
)
484 jboolean first_pass
= i
== 0;
488 insns
= (insn_slot
*) _Jv_AllocBytes (sizeof (insn_slot
) * next
);
489 number_insn_slots
= next
;
493 unsigned char *pc
= codestart
;
496 int base_pc_val
= pc
- codestart
;
498 pc_mapping
[base_pc_val
] = next
;
500 java_opcode opcode
= (java_opcode
) *pc
++;
502 if (opcode
== op_nop
)
504 SET_INSN (insn_targets
[opcode
]);
645 case op_monitorenter
:
655 // No argument, nothing else to do.
659 SET_INT (get1s (pc
));
665 int index
= get1u (pc
);
667 // For an unresolved class we want to delay resolution
669 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
672 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
676 SET_DATUM (pool_data
[index
].o
);
692 SET_INT (get1u (pc
));
697 SET_INT (get1u (pc
));
698 SET_INT (get1s (pc
+ 1));
704 int index
= get2u (pc
);
706 // For an unresolved class we want to delay resolution
708 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
711 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
715 SET_DATUM (pool_data
[index
].o
);
721 int index
= get2u (pc
);
723 SET_DATUM (&pool_data
[index
]);
728 SET_INT (get2s (pc
));
740 case op_invokespecial
:
741 case op_invokestatic
:
742 case op_invokevirtual
:
743 SET_INT (get2u (pc
));
747 case op_multianewarray
:
748 SET_INT (get2u (pc
));
749 SET_INT (get1u (pc
+ 2));
772 int offset
= get2s (pc
);
775 int new_pc
= base_pc_val
+ offset
;
777 bool orig_was_goto
= opcode
== op_goto
;
779 // Thread jumps. We limit the loop count; this lets
780 // us avoid infinite loops if the bytecode contains
781 // such. `10' is arbitrary.
783 while (codestart
[new_pc
] == op_goto
&& count
-- > 0)
784 new_pc
+= get2s (&codestart
[new_pc
+ 1]);
786 // If the jump takes us to a `return' instruction and
787 // the original branch was an unconditional goto, then
788 // we hoist the return.
789 opcode
= (java_opcode
) codestart
[new_pc
];
791 && (opcode
== op_ireturn
|| opcode
== op_lreturn
792 || opcode
== op_freturn
|| opcode
== op_dreturn
793 || opcode
== op_areturn
|| opcode
== op_return
))
796 SET_INSN (insn_targets
[opcode
]);
799 SET_DATUM (&insns
[pc_mapping
[new_pc
]]);
805 while ((pc
- codestart
) % 4 != 0)
808 jint def
= get4 (pc
);
809 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
815 int high
= get4 (pc
);
819 for (int i
= low
; i
<= high
; ++i
)
821 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ get4 (pc
)]]);
827 case op_lookupswitch
:
829 while ((pc
- codestart
) % 4 != 0)
832 jint def
= get4 (pc
);
833 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
836 jint npairs
= get4 (pc
);
842 jint match
= get4 (pc
);
843 jint offset
= get4 (pc
+ 4);
845 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
851 case op_invokeinterface
:
853 jint index
= get2u (pc
);
855 // We ignore the next two bytes.
863 opcode
= (java_opcode
) get1u (pc
);
865 jint val
= get2u (pc
);
868 // We implement narrow and wide instructions using the
869 // same code in the interpreter. So we rewrite the
870 // instruction slot here.
872 insns
[next
- 1].insn
= (void *) insn_targets
[opcode
];
875 if (opcode
== op_iinc
)
877 SET_INT (get2s (pc
));
886 jint offset
= get4 (pc
);
888 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
892 // Some "can't happen" cases that we include for
893 // error-checking purposes.
911 case op_getstatic_2s
:
912 case op_getstatic_2u
:
924 // Now update exceptions.
925 _Jv_InterpException
*exc
= exceptions ();
926 for (int i
= 0; i
< exc_count
; ++i
)
928 exc
[i
].start_pc
.p
= &insns
[pc_mapping
[exc
[i
].start_pc
.i
]];
929 exc
[i
].end_pc
.p
= &insns
[pc_mapping
[exc
[i
].end_pc
.i
]];
930 exc
[i
].handler_pc
.p
= &insns
[pc_mapping
[exc
[i
].handler_pc
.i
]];
931 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
932 // during compilation.
934 = (_Jv_Linker::resolve_pool_entry (defining_class
,
935 exc
[i
].handler_type
.i
)).clazz
;
936 exc
[i
].handler_type
.p
= handler
;
939 // Translate entries in the LineNumberTable from bytecode PC's to direct
940 // threaded interpreter instruction values.
941 for (int i
= 0; i
< line_table_len
; i
++)
943 int byte_pc
= line_table
[i
].bytecode_pc
;
944 // It isn't worth throwing an exception if this table is
945 // corrupted, but at the same time we don't want a crash.
946 if (byte_pc
< 0 || byte_pc
>= code_length
)
948 line_table
[i
].pc
= &insns
[pc_mapping
[byte_pc
]];
953 // Now remap the variable table for this method.
954 for (int i
= 0; i
< local_var_table_len
; ++i
)
956 int start_byte
= local_var_table
[i
].bytecode_pc
;
957 if (start_byte
< 0 || start_byte
>= code_length
)
959 jlocation start
= pc_mapping
[start_byte
];
961 int end_byte
= start_byte
+ local_var_table
[i
].length
;
964 jlocation end
= ((end_byte
>= code_length
)
966 : pc_mapping
[end_byte
]);
968 local_var_table
[i
].pc
= &insns
[start
];
969 local_var_table
[i
].length
= end
- start
+ 1;
972 if (breakpoint_insn
== NULL
)
974 bp_insn_slot
.insn
= const_cast<void *> (insn_targets
[op_breakpoint
]);
975 breakpoint_insn
= &bp_insn_slot
;
978 #endif /* DIRECT_THREADED */
980 /* Run the given method.
981 When args is NULL, don't run anything -- just compile it. */
983 _Jv_InterpMethod::run (void *retp
, INTERP_FFI_RAW_TYPE
*args
,
984 _Jv_InterpMethod
*meth
)
987 #undef DEBUG_LOCALS_INSN
988 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
990 #include "interpret-run.cc"
994 _Jv_InterpMethod::run_debug (void *retp
, INTERP_FFI_RAW_TYPE
*args
,
995 _Jv_InterpMethod
*meth
)
998 #undef DEBUG_LOCALS_INSN
999 #define DEBUG_LOCALS_INSN(s, t) \
1002 frame_desc.locals_type[s] = t; \
1006 #include "interpret-run.cc"
1010 throw_internal_error (const char *msg
)
1012 jthrowable t
= new java::lang::InternalError (JvNewStringLatin1 (msg
));
1013 REPORT_EXCEPTION (t
);
1018 throw_incompatible_class_change_error (jstring msg
)
1020 jthrowable t
= new java::lang::IncompatibleClassChangeError (msg
);
1021 REPORT_EXCEPTION (t
);
1026 throw_null_pointer_exception ()
1028 jthrowable t
= new java::lang::NullPointerException
;
1029 REPORT_EXCEPTION (t
);
1033 /* Look up source code line number for given bytecode (or direct threaded
1036 _Jv_InterpMethod::get_source_line(pc_t mpc
)
1038 int line
= line_table_len
> 0 ? line_table
[0].line
: -1;
1039 for (int i
= 1; i
< line_table_len
; i
++)
1040 if (line_table
[i
].pc
> mpc
)
1043 line
= line_table
[i
].line
;
1048 /** Do static initialization for fields with a constant initializer */
1050 _Jv_InitField (jobject obj
, jclass klass
, int index
)
1052 using namespace java::lang::reflect
;
1054 if (obj
!= 0 && klass
== 0)
1055 klass
= obj
->getClass ();
1057 if (!_Jv_IsInterpretedClass (klass
))
1060 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*)klass
->aux_info
;
1062 _Jv_Field
* field
= (&klass
->fields
[0]) + index
;
1064 if (index
> klass
->field_count
)
1065 throw_internal_error ("field out of range");
1067 int init
= iclass
->field_initializers
[index
];
1071 _Jv_Constants
*pool
= &klass
->constants
;
1072 int tag
= pool
->tags
[init
];
1074 if (! field
->isResolved ())
1075 throw_internal_error ("initializing unresolved field");
1077 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
1078 throw_internal_error ("initializing non-static field with no object");
1082 if ((field
->flags
& Modifier::STATIC
) != 0)
1083 addr
= (void*) field
->u
.addr
;
1085 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
1089 case JV_CONSTANT_String
:
1092 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
1093 pool
->data
[init
].string
= str
;
1094 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
1098 case JV_CONSTANT_ResolvedString
:
1099 if (! (field
->type
== &java::lang::String::class$
1100 || field
->type
== &java::lang::Class::class$
))
1101 throw_class_format_error ("string initialiser to non-string field");
1103 *(jstring
*)addr
= pool
->data
[init
].string
;
1106 case JV_CONSTANT_Integer
:
1108 int value
= pool
->data
[init
].i
;
1110 if (field
->type
== JvPrimClass (boolean
))
1111 *(jboolean
*)addr
= (jboolean
)value
;
1113 else if (field
->type
== JvPrimClass (byte
))
1114 *(jbyte
*)addr
= (jbyte
)value
;
1116 else if (field
->type
== JvPrimClass (char))
1117 *(jchar
*)addr
= (jchar
)value
;
1119 else if (field
->type
== JvPrimClass (short))
1120 *(jshort
*)addr
= (jshort
)value
;
1122 else if (field
->type
== JvPrimClass (int))
1123 *(jint
*)addr
= (jint
)value
;
1126 throw_class_format_error ("erroneous field initializer");
1130 case JV_CONSTANT_Long
:
1131 if (field
->type
!= JvPrimClass (long))
1132 throw_class_format_error ("erroneous field initializer");
1134 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
1137 case JV_CONSTANT_Float
:
1138 if (field
->type
!= JvPrimClass (float))
1139 throw_class_format_error ("erroneous field initializer");
1141 *(jfloat
*)addr
= pool
->data
[init
].f
;
1144 case JV_CONSTANT_Double
:
1145 if (field
->type
!= JvPrimClass (double))
1146 throw_class_format_error ("erroneous field initializer");
1148 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
1152 throw_class_format_error ("erroneous field initializer");
1156 inline static unsigned char*
1157 skip_one_type (unsigned char* ptr
)
1168 do { ch
= *ptr
++; } while (ch
!= ';');
1175 get_ffi_type_from_signature (unsigned char* ptr
)
1181 return &ffi_type_pointer
;
1185 // On some platforms a bool is a byte, on others an int.
1186 if (sizeof (jboolean
) == sizeof (jbyte
))
1187 return &ffi_type_sint8
;
1190 JvAssert (sizeof (jbyte
) == sizeof (jint
));
1191 return &ffi_type_sint32
;
1196 return &ffi_type_sint8
;
1200 return &ffi_type_uint16
;
1204 return &ffi_type_sint16
;
1208 return &ffi_type_sint32
;
1212 return &ffi_type_sint64
;
1216 return &ffi_type_float
;
1220 return &ffi_type_double
;
1224 return &ffi_type_void
;
1228 throw_internal_error ("unknown type in signature");
1231 /* this function yields the number of actual arguments, that is, if the
1232 * function is non-static, then one is added to the number of elements
1233 * found in the signature */
1236 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
1239 unsigned char *ptr
= (unsigned char*) signature
->chars();
1240 int arg_count
= staticp
? 0 : 1;
1242 /* first, count number of arguments */
1250 ptr
= skip_one_type (ptr
);
1257 /* This beast will build a cif, given the signature. Memory for
1258 * the cif itself and for the argument types must be allocated by the
1263 _Jv_init_cif (_Jv_Utf8Const
* signature
,
1267 ffi_type
**arg_types
,
1270 unsigned char *ptr
= (unsigned char*) signature
->chars();
1272 int arg_index
= 0; // arg number
1273 int item_count
= 0; // stack-item count
1278 arg_types
[arg_index
++] = &ffi_type_pointer
;
1288 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1290 if (*ptr
== 'J' || *ptr
== 'D')
1295 ptr
= skip_one_type (ptr
);
1300 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1302 ptr
= skip_one_type (ptr
);
1303 if (ptr
!= (unsigned char*)signature
->chars() + signature
->len())
1304 throw_internal_error ("did not find end of signature");
1306 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1307 arg_count
, rtype
, arg_types
) != FFI_OK
)
1308 throw_internal_error ("ffi_prep_cif failed");
1310 if (rtype_p
!= NULL
)
1316 /* we put this one here, and not in interpret.cc because it
1317 * calls the utility routines _Jv_count_arguments
1318 * which are static to this module. The following struct defines the
1319 * layout we use for the stubs, it's only used in the ncode method. */
1321 #if FFI_NATIVE_RAW_API
1322 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1323 # define FFI_RAW_SIZE ffi_raw_size
1325 ffi_raw_closure closure
;
1326 _Jv_ClosureList list
;
1328 ffi_type
*arg_types
[0];
1330 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,INTERP_FFI_RAW_TYPE
*,void*);
1332 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1333 # define FFI_RAW_SIZE ffi_java_raw_size
1335 ffi_java_raw_closure closure
;
1336 _Jv_ClosureList list
;
1338 ffi_type
*arg_types
[0];
1340 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_java_raw
*,void*);
1344 _Jv_InterpMethod::ncode (jclass klass
)
1346 using namespace java::lang::reflect
;
1348 if (self
->ncode
!= 0)
1351 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1352 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1355 ncode_closure
*closure
=
1356 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1357 + arg_count
* sizeof (ffi_type
*),
1359 closure
->list
.registerClosure (klass
, closure
);
1361 _Jv_init_cif (self
->signature
,
1365 &closure
->arg_types
[0],
1368 ffi_closure_fun fun
;
1370 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1372 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1374 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1379 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1381 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1386 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1388 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1396 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1398 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1403 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1405 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1409 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1420 /* Find the index of the given insn in the array of insn slots
1421 for this method. Returns -1 if not found. */
1423 _Jv_InterpMethod::insn_index (pc_t pc
)
1426 #ifdef DIRECT_THREADED
1427 jlong right
= number_insn_slots
;
1428 pc_t insns
= prepared
;
1430 jlong right
= code_length
;
1431 pc_t insns
= bytecode ();
1436 jlong mid
= (left
+ right
) / 2;
1437 if (&insns
[mid
] == pc
)
1440 if (pc
< &insns
[mid
])
1449 // Method to check if an exception is caught at some location in a method
1450 // (meth). Returns true if this method (meth) contains a catch block for the
1451 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1452 // to the location of the beginning of the catch block.
1454 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1455 java::lang::Throwable
*ex
)
1457 #ifdef DIRECT_THREADED
1458 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1460 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1462 _Jv_InterpException
*exc
= meth
->exceptions ();
1463 jclass exc_class
= ex
->getClass ();
1465 for (int i
= 0; i
< meth
->exc_count
; i
++)
1467 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1468 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1470 #ifdef DIRECT_THREADED
1471 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1473 jclass handler
= NULL
;
1474 if (exc
[i
].handler_type
.i
!= 0)
1476 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1478 #endif /* DIRECT_THREADED */
1479 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1481 #ifdef DIRECT_THREADED
1482 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1484 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1485 #endif /* DIRECT_THREADED */
1495 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1496 jintArray
& line_numbers
,
1497 jlongArray
& code_indices
)
1499 #ifdef DIRECT_THREADED
1500 /* For the DIRECT_THREADED case, if the method has not yet been
1501 * compiled, the linetable will change to insn slots instead of
1502 * bytecode PCs. It is probably easiest, in this case, to simply
1503 * compile the method and guarantee that we are using insn
1506 _Jv_CompileMethod (this);
1508 if (line_table_len
> 0)
1511 end
= number_insn_slots
;
1512 line_numbers
= JvNewIntArray (line_table_len
);
1513 code_indices
= JvNewLongArray (line_table_len
);
1515 jint
* lines
= elements (line_numbers
);
1516 jlong
* indices
= elements (code_indices
);
1517 for (int i
= 0; i
< line_table_len
; ++i
)
1519 lines
[i
] = line_table
[i
].line
;
1520 indices
[i
] = insn_index (line_table
[i
].pc
);
1523 #else // !DIRECT_THREADED
1524 if (line_table_len
> 0)
1528 line_numbers
= JvNewIntArray (line_table_len
);
1529 code_indices
= JvNewLongArray (line_table_len
);
1531 jint
* lines
= elements (line_numbers
);
1532 jlong
* indices
= elements (code_indices
);
1533 for (int i
= 0; i
< line_table_len
; ++i
)
1535 lines
[i
] = line_table
[i
].line
;
1536 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1539 #endif // !DIRECT_THREADED
1543 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1544 char **generic_sig
, jlong
*startloc
,
1545 jint
*length
, jint
*slot
,
1548 #ifdef DIRECT_THREADED
1549 _Jv_CompileMethod (this);
1552 if (local_var_table
== NULL
)
1554 if (table_slot
>= local_var_table_len
)
1558 *name
= local_var_table
[table_slot
].name
;
1559 *sig
= local_var_table
[table_slot
].descriptor
;
1560 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1562 #ifdef DIRECT_THREADED
1563 *startloc
= insn_index (local_var_table
[table_slot
].pc
);
1565 *startloc
= static_cast<jlong
> (local_var_table
[table_slot
].bytecode_pc
);
1567 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1568 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1570 return local_var_table_len
- table_slot
- 1;
1574 _Jv_InterpMethod::install_break (jlong index
)
1576 return set_insn (index
, breakpoint_insn
);
1580 _Jv_InterpMethod::get_insn (jlong index
)
1584 #ifdef DIRECT_THREADED
1585 if (index
>= number_insn_slots
|| index
< 0)
1589 #else // !DIRECT_THREADED
1590 if (index
>= code_length
|| index
< 0)
1593 code
= reinterpret_cast<pc_t
> (bytecode ());
1594 #endif // !DIRECT_THREADED
1596 return &code
[index
];
1600 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1602 #ifdef DIRECT_THREADED
1603 if (index
>= number_insn_slots
|| index
< 0)
1606 pc_t code
= prepared
;
1607 code
[index
].insn
= insn
->insn
;
1608 #else // !DIRECT_THREADED
1609 if (index
>= code_length
|| index
< 0)
1612 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1613 code
[index
] = *insn
;
1614 #endif // !DIRECT_THREADED
1616 return &code
[index
];
1620 _Jv_InterpMethod::breakpoint_at (jlong index
)
1622 pc_t insn
= get_insn (index
);
1625 #ifdef DIRECT_THREADED
1626 return (insn
->insn
== breakpoint_insn
->insn
);
1628 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1629 return (code
[index
] == breakpoint_insn
);
1637 _Jv_JNIMethod::ncode (jclass klass
)
1639 using namespace java::lang::reflect
;
1641 if (self
->ncode
!= 0)
1644 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1645 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1648 ncode_closure
*closure
=
1649 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1650 + arg_count
* sizeof (ffi_type
*),
1652 closure
->list
.registerClosure (klass
, closure
);
1655 _Jv_init_cif (self
->signature
,
1659 &closure
->arg_types
[0],
1662 ffi_closure_fun fun
;
1664 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1666 // Initialize the argument types and CIF that represent the actual
1667 // underlying JNI function.
1669 if ((self
->accflags
& Modifier::STATIC
))
1671 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1672 * sizeof (ffi_type
*));
1674 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1675 if ((self
->accflags
& Modifier::STATIC
))
1676 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1677 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1678 arg_count
* sizeof (ffi_type
*));
1680 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1681 extra_args
+ arg_count
, rtype
,
1682 jni_arg_types
) != FFI_OK
)
1683 throw_internal_error ("ffi_prep_cif failed for JNI function");
1685 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1687 // FIXME: for now we assume that all native methods for
1688 // interpreted code use JNI.
1689 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1691 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1702 throw_class_format_error (jstring msg
)
1705 ? new java::lang::ClassFormatError (msg
)
1706 : new java::lang::ClassFormatError
);
1707 REPORT_EXCEPTION (t
);
1712 throw_class_format_error (const char *msg
)
1714 throw_class_format_error (JvNewStringLatin1 (msg
));
1717 /* This function finds the method and location where the exception EXC
1718 is caught in the stack frame. On return, it sets CATCH_METHOD and
1719 CATCH_LOCATION with the method and location where the catch will
1720 occur. If the exception is not caught, these are set to 0.
1722 This function should only be used with the DEBUG interpreter. */
1724 find_catch_location (::java::lang::Throwable
*exc
, jthread thread
,
1725 jmethodID
*catch_method
, jlong
*catch_loc
)
1730 _Jv_InterpFrame
*frame
1731 = reinterpret_cast<_Jv_InterpFrame
*> (thread
->interp_frame
);
1732 while (frame
!= NULL
)
1734 pc_t pc
= frame
->get_pc ();
1735 _Jv_InterpMethod
*imeth
1736 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1737 if (imeth
->check_handler (&pc
, imeth
, exc
))
1739 // This method handles the exception.
1740 *catch_method
= imeth
->get_method ();
1741 *catch_loc
= imeth
->insn_index (pc
);
1745 frame
= frame
->next_interp
;
1749 /* This method handles JVMTI notifications of thrown exceptions. It
1750 calls find_catch_location to figure out where the exception is
1751 caught (if it is caught).
1753 Like find_catch_location, this should only be called with the
1754 DEBUG interpreter. Since a few exceptions occur outside the
1755 interpreter proper, it is important to not call this function
1756 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1758 _Jv_ReportJVMTIExceptionThrow (jthrowable ex
)
1760 jthread thread
= ::java::lang::Thread::currentThread ();
1761 _Jv_Frame
*frame
= reinterpret_cast<_Jv_Frame
*> (thread
->frame
);
1762 jmethodID throw_meth
= frame
->self
->get_method ();
1763 jlocation throw_loc
= -1;
1764 if (frame
->frame_type
== frame_interpreter
)
1766 _Jv_InterpFrame
* iframe
1767 = reinterpret_cast<_Jv_InterpFrame
*> (frame
);
1768 _Jv_InterpMethod
*imeth
1769 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1770 throw_loc
= imeth
->insn_index (iframe
->get_pc ());
1774 jmethodID catch_method
;
1775 find_catch_location (ex
, thread
, &catch_method
, &catch_loc
);
1776 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION
, thread
,
1777 _Jv_GetCurrentJNIEnv (), throw_meth
, throw_loc
,
1778 ex
, catch_method
, catch_loc
);
1784 _Jv_InterpreterEngine::do_verify (jclass klass
)
1786 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1787 for (int i
= 0; i
< klass
->method_count
; i
++)
1789 using namespace java::lang::reflect
;
1790 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1791 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1792 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1794 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1795 _Jv_VerifyMethod (im
);
1801 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1803 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1804 for (int i
= 0; i
< klass
->method_count
; i
++)
1806 // Just skip abstract methods. This is particularly important
1807 // because we don't resize the interpreted_methods array when
1808 // miranda methods are added to it.
1809 if ((klass
->methods
[i
].accflags
1810 & java::lang::reflect::Modifier::ABSTRACT
)
1814 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1816 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1819 // You might think we could use a virtual `ncode' method in
1820 // the _Jv_MethodBase and unify the native and non-native
1821 // cases. Well, we can't, because we don't allocate these
1822 // objects using `new', and thus they don't get a vtable.
1823 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1824 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1826 else if (imeth
!= 0) // it could be abstract
1828 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1829 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1835 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1837 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1839 if (!iclass
->closures
)
1840 iclass
->closures
= _Jv_ClosureListFinalizer ();
1842 return iclass
->closures
;
1846 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1850 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1852 // Splitting the allocations here lets us scan reference fields and
1853 // avoid scanning non-reference fields. How reference fields are
1854 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1855 // means that this memory will be scanned conservatively (same
1856 // difference, since we know all the contents here are pointers).
1857 // Then we put pointers into this memory into the 'fields'
1858 // structure. Most of these are interior pointers, which is ok (but
1859 // even so the pointer to the first reference field will be used and
1860 // that is not an interior pointer). The 'fields' array is also
1861 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1862 // be scanned. A pointer to this array is held by Class and thus
1863 // seen by the collector.
1864 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1865 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1867 for (int i
= 0; i
< klass
->field_count
; i
++)
1869 _Jv_Field
*field
= &klass
->fields
[i
];
1871 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1874 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1875 field
->u
.addr
= base
+ field
->u
.boffset
;
1877 if (iclass
->field_initializers
[i
] != 0)
1879 _Jv_Linker::resolve_field (field
, klass
->loader
);
1880 _Jv_InitField (0, klass
, i
);
1884 // Now we don't need the field_initializers anymore, so let the
1885 // collector get rid of it.
1886 iclass
->field_initializers
= 0;
1889 _Jv_ResolvedMethod
*
1890 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1893 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1895 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1896 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1897 + arg_count
*sizeof (ffi_type
*));
1899 result
->stack_item_count
1900 = _Jv_init_cif (method
->signature
,
1904 &result
->arg_types
[0],
1907 result
->method
= method
;
1908 result
->klass
= klass
;
1914 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1916 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1917 for (int i
= 0; i
< klass
->method_count
; i
++)
1919 // Just skip abstract methods. This is particularly important
1920 // because we don't resize the interpreted_methods array when
1921 // miranda methods are added to it.
1922 if ((klass
->methods
[i
].accflags
1923 & java::lang::reflect::Modifier::ABSTRACT
)
1926 // Miranda method additions mean that the `methods' array moves.
1927 // We cache a pointer into this array, so we have to update.
1928 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1932 #ifdef DIRECT_THREADED
1934 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1936 if (method
->prepared
== NULL
)
1939 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1941 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1944 #endif // DIRECT_THREADED