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 ffi_abi cabi
= FFI_DEFAULT_ABI
;
1307 #if defined (X86_WIN32) && !defined (__CYGWIN__)
1309 cabi
= FFI_THISCALL
;
1311 if (ffi_prep_cif (cif
, cabi
,
1312 arg_count
, rtype
, arg_types
) != FFI_OK
)
1313 throw_internal_error ("ffi_prep_cif failed");
1315 if (rtype_p
!= NULL
)
1321 /* we put this one here, and not in interpret.cc because it
1322 * calls the utility routines _Jv_count_arguments
1323 * which are static to this module. The following struct defines the
1324 * layout we use for the stubs, it's only used in the ncode method. */
1326 #if FFI_NATIVE_RAW_API
1327 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1328 # define FFI_RAW_SIZE ffi_raw_size
1330 ffi_raw_closure closure
;
1331 _Jv_ClosureList list
;
1333 ffi_type
*arg_types
[0];
1335 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,INTERP_FFI_RAW_TYPE
*,void*);
1337 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1338 # define FFI_RAW_SIZE ffi_java_raw_size
1340 ffi_java_raw_closure closure
;
1341 _Jv_ClosureList list
;
1343 ffi_type
*arg_types
[0];
1345 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_java_raw
*,void*);
1349 _Jv_InterpMethod::ncode (jclass klass
)
1351 using namespace java::lang::reflect
;
1353 if (self
->ncode
!= 0)
1356 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1357 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1360 ncode_closure
*closure
=
1361 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1362 + arg_count
* sizeof (ffi_type
*),
1364 closure
->list
.registerClosure (klass
, closure
);
1366 _Jv_init_cif (self
->signature
,
1370 &closure
->arg_types
[0],
1373 ffi_closure_fun fun
;
1375 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1377 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1379 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1384 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1386 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1391 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1393 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1401 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1403 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1408 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1410 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1414 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1425 /* Find the index of the given insn in the array of insn slots
1426 for this method. Returns -1 if not found. */
1428 _Jv_InterpMethod::insn_index (pc_t pc
)
1431 #ifdef DIRECT_THREADED
1432 jlong right
= number_insn_slots
;
1433 pc_t insns
= prepared
;
1435 jlong right
= code_length
;
1436 pc_t insns
= bytecode ();
1441 jlong mid
= (left
+ right
) / 2;
1442 if (&insns
[mid
] == pc
)
1445 if (pc
< &insns
[mid
])
1454 // Method to check if an exception is caught at some location in a method
1455 // (meth). Returns true if this method (meth) contains a catch block for the
1456 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1457 // to the location of the beginning of the catch block.
1459 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1460 java::lang::Throwable
*ex
)
1462 #ifdef DIRECT_THREADED
1463 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1465 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1467 _Jv_InterpException
*exc
= meth
->exceptions ();
1468 jclass exc_class
= ex
->getClass ();
1470 for (int i
= 0; i
< meth
->exc_count
; i
++)
1472 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1473 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1475 #ifdef DIRECT_THREADED
1476 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1478 jclass handler
= NULL
;
1479 if (exc
[i
].handler_type
.i
!= 0)
1481 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1482 exc
[i
].handler_type
.i
)).clazz
;
1483 #endif /* DIRECT_THREADED */
1484 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1486 #ifdef DIRECT_THREADED
1487 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1489 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1490 #endif /* DIRECT_THREADED */
1500 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1501 jintArray
& line_numbers
,
1502 jlongArray
& code_indices
)
1504 #ifdef DIRECT_THREADED
1505 /* For the DIRECT_THREADED case, if the method has not yet been
1506 * compiled, the linetable will change to insn slots instead of
1507 * bytecode PCs. It is probably easiest, in this case, to simply
1508 * compile the method and guarantee that we are using insn
1511 _Jv_CompileMethod (this);
1513 if (line_table_len
> 0)
1516 end
= number_insn_slots
;
1517 line_numbers
= JvNewIntArray (line_table_len
);
1518 code_indices
= JvNewLongArray (line_table_len
);
1520 jint
* lines
= elements (line_numbers
);
1521 jlong
* indices
= elements (code_indices
);
1522 for (int i
= 0; i
< line_table_len
; ++i
)
1524 lines
[i
] = line_table
[i
].line
;
1525 indices
[i
] = insn_index (line_table
[i
].pc
);
1528 #else // !DIRECT_THREADED
1529 if (line_table_len
> 0)
1533 line_numbers
= JvNewIntArray (line_table_len
);
1534 code_indices
= JvNewLongArray (line_table_len
);
1536 jint
* lines
= elements (line_numbers
);
1537 jlong
* indices
= elements (code_indices
);
1538 for (int i
= 0; i
< line_table_len
; ++i
)
1540 lines
[i
] = line_table
[i
].line
;
1541 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1544 #endif // !DIRECT_THREADED
1548 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1549 char **generic_sig
, jlong
*startloc
,
1550 jint
*length
, jint
*slot
,
1553 #ifdef DIRECT_THREADED
1554 _Jv_CompileMethod (this);
1557 if (local_var_table
== NULL
)
1559 if (table_slot
>= local_var_table_len
)
1563 *name
= local_var_table
[table_slot
].name
;
1564 *sig
= local_var_table
[table_slot
].descriptor
;
1565 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1567 #ifdef DIRECT_THREADED
1568 *startloc
= insn_index (local_var_table
[table_slot
].pc
);
1570 *startloc
= static_cast<jlong
> (local_var_table
[table_slot
].bytecode_pc
);
1572 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1573 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1575 return local_var_table_len
- table_slot
- 1;
1579 _Jv_InterpMethod::install_break (jlong index
)
1581 return set_insn (index
, breakpoint_insn
);
1585 _Jv_InterpMethod::get_insn (jlong index
)
1589 #ifdef DIRECT_THREADED
1590 if (index
>= number_insn_slots
|| index
< 0)
1594 #else // !DIRECT_THREADED
1595 if (index
>= code_length
|| index
< 0)
1598 code
= reinterpret_cast<pc_t
> (bytecode ());
1599 #endif // !DIRECT_THREADED
1601 return &code
[index
];
1605 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1607 #ifdef DIRECT_THREADED
1608 if (index
>= number_insn_slots
|| index
< 0)
1611 pc_t code
= prepared
;
1612 code
[index
].insn
= insn
->insn
;
1613 #else // !DIRECT_THREADED
1614 if (index
>= code_length
|| index
< 0)
1617 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1618 code
[index
] = *insn
;
1619 #endif // !DIRECT_THREADED
1621 return &code
[index
];
1625 _Jv_InterpMethod::breakpoint_at (jlong index
)
1627 pc_t insn
= get_insn (index
);
1630 #ifdef DIRECT_THREADED
1631 return (insn
->insn
== breakpoint_insn
->insn
);
1633 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1634 return (code
[index
] == bp_insn_opcode
);
1642 _Jv_JNIMethod::ncode (jclass klass
)
1644 using namespace java::lang::reflect
;
1646 if (self
->ncode
!= 0)
1649 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1650 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1653 ncode_closure
*closure
=
1654 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1655 + arg_count
* sizeof (ffi_type
*),
1657 closure
->list
.registerClosure (klass
, closure
);
1660 _Jv_init_cif (self
->signature
,
1664 &closure
->arg_types
[0],
1667 ffi_closure_fun fun
;
1669 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1671 // Initialize the argument types and CIF that represent the actual
1672 // underlying JNI function.
1674 if ((self
->accflags
& Modifier::STATIC
))
1676 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1677 * sizeof (ffi_type
*));
1679 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1680 if ((self
->accflags
& Modifier::STATIC
))
1681 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1682 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1683 arg_count
* sizeof (ffi_type
*));
1685 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1686 extra_args
+ arg_count
, rtype
,
1687 jni_arg_types
) != FFI_OK
)
1688 throw_internal_error ("ffi_prep_cif failed for JNI function");
1690 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1692 // FIXME: for now we assume that all native methods for
1693 // interpreted code use JNI.
1694 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1696 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1707 throw_class_format_error (jstring msg
)
1710 ? new java::lang::ClassFormatError (msg
)
1711 : new java::lang::ClassFormatError
);
1712 REPORT_EXCEPTION (t
);
1717 throw_class_format_error (const char *msg
)
1719 throw_class_format_error (JvNewStringLatin1 (msg
));
1722 /* This function finds the method and location where the exception EXC
1723 is caught in the stack frame. On return, it sets CATCH_METHOD and
1724 CATCH_LOCATION with the method and location where the catch will
1725 occur. If the exception is not caught, these are set to 0.
1727 This function should only be used with the __GCJ_DEBUG interpreter. */
1729 find_catch_location (::java::lang::Throwable
*exc
, jthread thread
,
1730 jmethodID
*catch_method
, jlong
*catch_loc
)
1735 _Jv_InterpFrame
*frame
1736 = reinterpret_cast<_Jv_InterpFrame
*> (thread
->interp_frame
);
1737 while (frame
!= NULL
)
1739 pc_t pc
= frame
->get_pc ();
1740 _Jv_InterpMethod
*imeth
1741 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1742 if (imeth
->check_handler (&pc
, imeth
, exc
))
1744 // This method handles the exception.
1745 *catch_method
= imeth
->get_method ();
1746 *catch_loc
= imeth
->insn_index (pc
);
1750 frame
= frame
->next_interp
;
1754 /* This method handles JVMTI notifications of thrown exceptions. It
1755 calls find_catch_location to figure out where the exception is
1756 caught (if it is caught).
1758 Like find_catch_location, this should only be called with the
1759 __GCJ_DEBUG interpreter. Since a few exceptions occur outside the
1760 interpreter proper, it is important to not call this function
1761 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1763 _Jv_ReportJVMTIExceptionThrow (jthrowable ex
)
1765 jthread thread
= ::java::lang::Thread::currentThread ();
1766 _Jv_Frame
*frame
= reinterpret_cast<_Jv_Frame
*> (thread
->frame
);
1767 jmethodID throw_meth
= frame
->self
->get_method ();
1768 jlocation throw_loc
= -1;
1769 if (frame
->frame_type
== frame_interpreter
)
1771 _Jv_InterpFrame
* iframe
1772 = reinterpret_cast<_Jv_InterpFrame
*> (frame
);
1773 _Jv_InterpMethod
*imeth
1774 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1775 throw_loc
= imeth
->insn_index (iframe
->get_pc ());
1779 jmethodID catch_method
;
1780 find_catch_location (ex
, thread
, &catch_method
, &catch_loc
);
1781 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION
, thread
,
1782 _Jv_GetCurrentJNIEnv (), throw_meth
, throw_loc
,
1783 ex
, catch_method
, catch_loc
);
1789 _Jv_InterpreterEngine::do_verify (jclass klass
)
1791 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1792 for (int i
= 0; i
< klass
->method_count
; i
++)
1794 using namespace java::lang::reflect
;
1795 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1796 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1797 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1799 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1800 _Jv_VerifyMethod (im
);
1806 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1808 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1809 for (int i
= 0; i
< klass
->method_count
; i
++)
1811 // Just skip abstract methods. This is particularly important
1812 // because we don't resize the interpreted_methods array when
1813 // miranda methods are added to it.
1814 if ((klass
->methods
[i
].accflags
1815 & java::lang::reflect::Modifier::ABSTRACT
)
1819 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1821 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1824 // You might think we could use a virtual `ncode' method in
1825 // the _Jv_MethodBase and unify the native and non-native
1826 // cases. Well, we can't, because we don't allocate these
1827 // objects using `new', and thus they don't get a vtable.
1828 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1829 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1831 else if (imeth
!= 0) // it could be abstract
1833 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1834 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1840 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1842 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1844 if (!iclass
->closures
)
1845 iclass
->closures
= _Jv_ClosureListFinalizer ();
1847 return iclass
->closures
;
1851 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1855 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1857 // Splitting the allocations here lets us scan reference fields and
1858 // avoid scanning non-reference fields. How reference fields are
1859 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1860 // means that this memory will be scanned conservatively (same
1861 // difference, since we know all the contents here are pointers).
1862 // Then we put pointers into this memory into the 'fields'
1863 // structure. Most of these are interior pointers, which is ok (but
1864 // even so the pointer to the first reference field will be used and
1865 // that is not an interior pointer). The 'fields' array is also
1866 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1867 // be scanned. A pointer to this array is held by Class and thus
1868 // seen by the collector.
1869 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1870 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1872 for (int i
= 0; i
< klass
->field_count
; i
++)
1874 _Jv_Field
*field
= &klass
->fields
[i
];
1876 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1879 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1880 field
->u
.addr
= base
+ field
->u
.boffset
;
1882 if (iclass
->field_initializers
[i
] != 0)
1884 _Jv_Linker::resolve_field (field
, klass
->loader
);
1885 _Jv_InitField (0, klass
, i
);
1889 // Now we don't need the field_initializers anymore, so let the
1890 // collector get rid of it.
1891 iclass
->field_initializers
= 0;
1894 _Jv_ResolvedMethod
*
1895 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1898 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1900 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1901 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1902 + arg_count
*sizeof (ffi_type
*));
1904 result
->stack_item_count
1905 = _Jv_init_cif (method
->signature
,
1909 &result
->arg_types
[0],
1912 result
->method
= method
;
1913 result
->klass
= klass
;
1919 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1921 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1922 for (int i
= 0; i
< klass
->method_count
; i
++)
1924 // Just skip abstract methods. This is particularly important
1925 // because we don't resize the interpreted_methods array when
1926 // miranda methods are added to it.
1927 if ((klass
->methods
[i
].accflags
1928 & java::lang::reflect::Modifier::ABSTRACT
)
1931 // Miranda method additions mean that the `methods' array moves.
1932 // We cache a pointer into this array, so we have to update.
1933 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1937 #ifdef DIRECT_THREADED
1939 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1941 if (method
->prepared
== NULL
)
1944 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1946 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1949 #endif // DIRECT_THREADED