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>
48 // Execution engine for interpreted code.
49 _Jv_InterpreterEngine _Jv_soleInterpreterEngine
;
55 static void throw_internal_error (const char *msg
)
56 __attribute__ ((__noreturn__
));
57 static void throw_incompatible_class_change_error (jstring msg
)
58 __attribute__ ((__noreturn__
));
59 static void throw_null_pointer_exception ()
60 __attribute__ ((__noreturn__
));
62 static void throw_class_format_error (jstring msg
)
63 __attribute__ ((__noreturn__
));
64 static void throw_class_format_error (const char *msg
)
65 __attribute__ ((__noreturn__
));
67 static void find_catch_location (jthrowable
, jthread
, jmethodID
*, jlong
*);
69 // A macro to facilitate JVMTI exception reporting
70 #define REPORT_EXCEPTION(Jthrowable) \
72 if (JVMTI_REQUESTED_EVENT (Exception)) \
73 _Jv_ReportJVMTIExceptionThrow (Jthrowable); \
77 #ifdef DIRECT_THREADED
78 // Lock to ensure that methods are not compiled concurrently.
79 // We could use a finer-grained lock here, however it is not safe to use
80 // the Class monitor as user code in another thread could hold it.
81 static _Jv_Mutex_t compile_mutex
;
86 _Jv_MutexInit (&compile_mutex
);
89 void _Jv_InitInterpreter() {}
92 // The breakpoint instruction. For the direct threaded case,
93 // _Jv_InterpMethod::compile will initialize breakpoint_insn
94 // the first time it is called.
95 #ifdef DIRECT_THREADED
96 insn_slot
_Jv_InterpMethod::bp_insn_slot
;
97 pc_t
_Jv_InterpMethod::breakpoint_insn
= NULL
;
99 unsigned char _Jv_InterpMethod::bp_insn_opcode
100 = static_cast<unsigned char> (op_breakpoint
);
101 pc_t
_Jv_InterpMethod::breakpoint_insn
= &_Jv_InterpMethod::bp_insn_opcode
;
104 extern "C" double __ieee754_fmod (double,double);
106 static inline void dupx (_Jv_word
*sp
, int n
, int x
)
108 // first "slide" n+x elements n to the right
110 for (int i
= 0; i
< n
+x
; i
++)
112 sp
[(top
-i
)] = sp
[(top
-i
)-n
];
115 // next, copy the n top elements, n+x down
116 for (int i
= 0; i
< n
; i
++)
118 sp
[top
-(n
+x
)-i
] = sp
[top
-i
];
122 // Used to convert from floating types to integral types.
123 template<typename TO
, typename FROM
>
125 convert (FROM val
, TO min
, TO max
)
128 if (val
>= (FROM
) max
)
130 else if (val
<= (FROM
) min
)
139 #define PUSHA(V) (sp++)->o = (V)
140 #define PUSHI(V) (sp++)->i = (V)
141 #define PUSHF(V) (sp++)->f = (V)
142 #if SIZEOF_VOID_P == 8
143 # define PUSHL(V) (sp->l = (V), sp += 2)
144 # define PUSHD(V) (sp->d = (V), sp += 2)
146 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
147 (sp++)->ia[0] = w2.ia[0]; \
148 (sp++)->ia[0] = w2.ia[1]; } while (0)
149 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
150 (sp++)->ia[0] = w2.ia[0]; \
151 (sp++)->ia[0] = w2.ia[1]; } while (0)
154 #define POPA() ((--sp)->o)
155 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
156 #define POPF() ((jfloat) (--sp)->f)
157 #if SIZEOF_VOID_P == 8
158 # define POPL() (sp -= 2, (jlong) sp->l)
159 # define POPD() (sp -= 2, (jdouble) sp->d)
161 # define POPL() ({ _Jv_word2 w2; \
162 w2.ia[1] = (--sp)->ia[0]; \
163 w2.ia[0] = (--sp)->ia[0]; w2.l; })
164 # define POPD() ({ _Jv_word2 w2; \
165 w2.ia[1] = (--sp)->ia[0]; \
166 w2.ia[0] = (--sp)->ia[0]; w2.d; })
169 #define LOADA(I) (sp++)->o = locals[I].o
170 #define LOADI(I) (sp++)->i = locals[I].i
171 #define LOADF(I) (sp++)->f = locals[I].f
172 #if SIZEOF_VOID_P == 8
173 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
174 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
176 # define LOADL(I) do { jint __idx = (I); \
177 (sp++)->ia[0] = locals[__idx].ia[0]; \
178 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
180 # define LOADD(I) LOADL(I)
187 DEBUG_LOCALS_INSN (__idx, 'o'); \
188 locals[__idx].o = (--sp)->o; \
195 DEBUG_LOCALS_INSN (__idx, 'i'); \
196 locals[__idx].i = (--sp)->i; \
202 DEBUG_LOCALS_INSN (__idx, 'f'); \
203 locals[__idx].f = (--sp)->f; \
206 #if SIZEOF_VOID_P == 8
211 DEBUG_LOCALS_INSN (__idx, 'l'); \
212 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
213 (sp -= 2, locals[__idx].l = sp->l); \
220 DEBUG_LOCALS_INSN (__idx, 'd'); \
221 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
222 (sp -= 2, locals[__idx].d = sp->d); \
231 DEBUG_LOCALS_INSN (__idx, 'l'); \
232 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
233 locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
234 locals[__idx].ia[0] = (--sp)->ia[0]; \
240 DEBUG_LOCALS_INSN (__idx, 'd'); \
241 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
242 locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
243 locals[__idx].ia[0] = (--sp)->ia[0]; \
247 #define PEEKI(I) (locals+(I))->i
248 #define PEEKA(I) (locals+(I))->o
254 DEBUG_LOCALS_INSN (__idx, 'i'); \
255 ((locals + __idx)->i = (V)); \
260 #define BINOPI(OP) { \
261 jint value2 = POPI(); \
262 jint value1 = POPI(); \
263 PUSHI(value1 OP value2); \
266 #define BINOPF(OP) { \
267 jfloat value2 = POPF(); \
268 jfloat value1 = POPF(); \
269 PUSHF(value1 OP value2); \
272 #define BINOPL(OP) { \
273 jlong value2 = POPL(); \
274 jlong value1 = POPL(); \
275 PUSHL(value1 OP value2); \
278 #define BINOPD(OP) { \
279 jdouble value2 = POPD(); \
280 jdouble value1 = POPD(); \
281 PUSHD(value1 OP value2); \
285 get1s (unsigned char* loc
)
287 return *(signed char*)loc
;
291 get1u (unsigned char* loc
)
297 get2s(unsigned char* loc
)
299 return (((jint
)*(signed char*)loc
) << 8) | ((jint
)*(loc
+1));
303 get2u (unsigned char* loc
)
305 return (((jint
)(*loc
)) << 8) | ((jint
)*(loc
+1));
309 get4 (unsigned char* loc
)
311 return (((jint
)(loc
[0])) << 24)
312 | (((jint
)(loc
[1])) << 16)
313 | (((jint
)(loc
[2])) << 8)
314 | (((jint
)(loc
[3])) << 0);
317 #define SAVE_PC() frame_desc.pc = pc
319 // We used to define this conditionally, depending on HANDLE_SEGV.
320 // However, that runs into a problem if a chunk in low memory is
321 // mapped and we try to look at a field near the end of a large
322 // object. See PR 26858 for details. It is, most likely, relatively
323 // inexpensive to simply do this check always.
324 #define NULLCHECK(X) \
325 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
327 // Note that we can still conditionally define NULLARRAYCHECK, since
328 // we know that all uses of an array will first reference the length
329 // field, which is first -- and thus will trigger a SEGV.
331 #define NULLARRAYCHECK(X) SAVE_PC()
333 #define NULLARRAYCHECK(X) \
337 if ((X) == NULL) { throw_null_pointer_exception (); } \
341 #define ARRAYBOUNDSCHECK(array, index) \
344 if (((unsigned) index) >= (unsigned) (array->length)) \
345 _Jv_ThrowBadArrayIndex (index); \
349 _Jv_InterpMethod::run_normal (ffi_cif
*,
354 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
355 run (ret
, args
, _this
);
359 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
364 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
365 run_debug (ret
, args
, _this
);
369 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
374 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
376 jobject rcv
= (jobject
) args
[0].ptr
;
377 JvSynchronize
mutex (rcv
);
379 run (ret
, args
, _this
);
383 _Jv_InterpMethod::run_synch_object_debug (ffi_cif
*,
388 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
390 jobject rcv
= (jobject
) args
[0].ptr
;
391 JvSynchronize
mutex (rcv
);
393 run_debug (ret
, args
, _this
);
397 _Jv_InterpMethod::run_class (ffi_cif
*,
402 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
403 _Jv_InitClass (_this
->defining_class
);
404 run (ret
, args
, _this
);
408 _Jv_InterpMethod::run_class_debug (ffi_cif
*,
413 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
414 _Jv_InitClass (_this
->defining_class
);
415 run_debug (ret
, args
, _this
);
419 _Jv_InterpMethod::run_synch_class (ffi_cif
*,
424 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
426 jclass sync
= _this
->defining_class
;
427 _Jv_InitClass (sync
);
428 JvSynchronize
mutex (sync
);
430 run (ret
, args
, _this
);
434 _Jv_InterpMethod::run_synch_class_debug (ffi_cif
*,
439 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
441 jclass sync
= _this
->defining_class
;
442 _Jv_InitClass (sync
);
443 JvSynchronize
mutex (sync
);
445 run_debug (ret
, args
, _this
);
448 #ifdef DIRECT_THREADED
449 // "Compile" a method by turning it from bytecode to direct-threaded
452 _Jv_InterpMethod::compile (const void * const *insn_targets
)
454 insn_slot
*insns
= NULL
;
456 unsigned char *codestart
= bytecode ();
457 unsigned char *end
= codestart
+ code_length
;
458 _Jv_word
*pool_data
= defining_class
->constants
.data
;
460 #define SET_ONE(Field, Value) \
466 insns[next++].Field = Value; \
470 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
471 #define SET_INT(Value) SET_ONE (int_val, Value)
472 #define SET_DATUM(Value) SET_ONE (datum, Value)
474 // Map from bytecode PC to slot in INSNS.
475 int *pc_mapping
= (int *) __builtin_alloca (sizeof (int) * code_length
);
476 for (int i
= 0; i
< code_length
; ++i
)
479 for (int i
= 0; i
< 2; ++i
)
481 jboolean first_pass
= i
== 0;
485 insns
= (insn_slot
*) _Jv_AllocBytes (sizeof (insn_slot
) * next
);
486 number_insn_slots
= next
;
490 unsigned char *pc
= codestart
;
493 int base_pc_val
= pc
- codestart
;
495 pc_mapping
[base_pc_val
] = next
;
497 java_opcode opcode
= (java_opcode
) *pc
++;
499 if (opcode
== op_nop
)
501 SET_INSN (insn_targets
[opcode
]);
642 case op_monitorenter
:
652 // No argument, nothing else to do.
656 SET_INT (get1s (pc
));
662 int index
= get1u (pc
);
664 // For an unresolved class we want to delay resolution
666 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
669 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
673 SET_DATUM (pool_data
[index
].o
);
689 SET_INT (get1u (pc
));
694 SET_INT (get1u (pc
));
695 SET_INT (get1s (pc
+ 1));
701 int index
= get2u (pc
);
703 // For an unresolved class we want to delay resolution
705 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
708 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
712 SET_DATUM (pool_data
[index
].o
);
718 int index
= get2u (pc
);
720 SET_DATUM (&pool_data
[index
]);
725 SET_INT (get2s (pc
));
737 case op_invokespecial
:
738 case op_invokestatic
:
739 case op_invokevirtual
:
740 SET_INT (get2u (pc
));
744 case op_multianewarray
:
745 SET_INT (get2u (pc
));
746 SET_INT (get1u (pc
+ 2));
769 int offset
= get2s (pc
);
772 int new_pc
= base_pc_val
+ offset
;
774 bool orig_was_goto
= opcode
== op_goto
;
776 // Thread jumps. We limit the loop count; this lets
777 // us avoid infinite loops if the bytecode contains
778 // such. `10' is arbitrary.
780 while (codestart
[new_pc
] == op_goto
&& count
-- > 0)
781 new_pc
+= get2s (&codestart
[new_pc
+ 1]);
783 // If the jump takes us to a `return' instruction and
784 // the original branch was an unconditional goto, then
785 // we hoist the return.
786 opcode
= (java_opcode
) codestart
[new_pc
];
788 && (opcode
== op_ireturn
|| opcode
== op_lreturn
789 || opcode
== op_freturn
|| opcode
== op_dreturn
790 || opcode
== op_areturn
|| opcode
== op_return
))
793 SET_INSN (insn_targets
[opcode
]);
796 SET_DATUM (&insns
[pc_mapping
[new_pc
]]);
802 while ((pc
- codestart
) % 4 != 0)
805 jint def
= get4 (pc
);
806 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
812 int high
= get4 (pc
);
816 for (int i
= low
; i
<= high
; ++i
)
818 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ get4 (pc
)]]);
824 case op_lookupswitch
:
826 while ((pc
- codestart
) % 4 != 0)
829 jint def
= get4 (pc
);
830 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
833 jint npairs
= get4 (pc
);
839 jint match
= get4 (pc
);
840 jint offset
= get4 (pc
+ 4);
842 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
848 case op_invokeinterface
:
850 jint index
= get2u (pc
);
852 // We ignore the next two bytes.
860 opcode
= (java_opcode
) get1u (pc
);
862 jint val
= get2u (pc
);
865 // We implement narrow and wide instructions using the
866 // same code in the interpreter. So we rewrite the
867 // instruction slot here.
869 insns
[next
- 1].insn
= (void *) insn_targets
[opcode
];
872 if (opcode
== op_iinc
)
874 SET_INT (get2s (pc
));
883 jint offset
= get4 (pc
);
885 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
889 // Some "can't happen" cases that we include for
890 // error-checking purposes.
908 case op_getstatic_2s
:
909 case op_getstatic_2u
:
921 // Now update exceptions.
922 _Jv_InterpException
*exc
= exceptions ();
923 for (int i
= 0; i
< exc_count
; ++i
)
925 exc
[i
].start_pc
.p
= &insns
[pc_mapping
[exc
[i
].start_pc
.i
]];
926 exc
[i
].end_pc
.p
= &insns
[pc_mapping
[exc
[i
].end_pc
.i
]];
927 exc
[i
].handler_pc
.p
= &insns
[pc_mapping
[exc
[i
].handler_pc
.i
]];
928 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
929 // during compilation.
931 = (_Jv_Linker::resolve_pool_entry (defining_class
,
932 exc
[i
].handler_type
.i
)).clazz
;
933 exc
[i
].handler_type
.p
= handler
;
936 // Translate entries in the LineNumberTable from bytecode PC's to direct
937 // threaded interpreter instruction values.
938 for (int i
= 0; i
< line_table_len
; i
++)
940 int byte_pc
= line_table
[i
].bytecode_pc
;
941 // It isn't worth throwing an exception if this table is
942 // corrupted, but at the same time we don't want a crash.
943 if (byte_pc
< 0 || byte_pc
>= code_length
)
945 line_table
[i
].pc
= &insns
[pc_mapping
[byte_pc
]];
950 // Now remap the variable table for this method.
951 for (int i
= 0; i
< local_var_table_len
; ++i
)
953 int start_byte
= local_var_table
[i
].bytecode_pc
;
954 if (start_byte
< 0 || start_byte
>= code_length
)
956 jlocation start
= pc_mapping
[start_byte
];
958 int end_byte
= start_byte
+ local_var_table
[i
].length
;
961 jlocation end
= ((end_byte
>= code_length
)
963 : pc_mapping
[end_byte
]);
965 local_var_table
[i
].pc
= &insns
[start
];
966 local_var_table
[i
].length
= end
- start
+ 1;
969 if (breakpoint_insn
== NULL
)
971 bp_insn_slot
.insn
= const_cast<void *> (insn_targets
[op_breakpoint
]);
972 breakpoint_insn
= &bp_insn_slot
;
975 #endif /* DIRECT_THREADED */
977 /* Run the given method.
978 When args is NULL, don't run anything -- just compile it. */
980 _Jv_InterpMethod::run (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
983 #undef DEBUG_LOCALS_INSN
984 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
986 #include "interpret-run.cc"
990 _Jv_InterpMethod::run_debug (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
993 #undef DEBUG_LOCALS_INSN
994 #define DEBUG_LOCALS_INSN(s, t) \
997 frame_desc.locals_type[s] = t; \
1001 #include "interpret-run.cc"
1005 throw_internal_error (const char *msg
)
1007 jthrowable t
= new java::lang::InternalError (JvNewStringLatin1 (msg
));
1008 REPORT_EXCEPTION (t
);
1013 throw_incompatible_class_change_error (jstring msg
)
1015 jthrowable t
= new java::lang::IncompatibleClassChangeError (msg
);
1016 REPORT_EXCEPTION (t
);
1021 throw_null_pointer_exception ()
1023 jthrowable t
= new java::lang::NullPointerException
;
1024 REPORT_EXCEPTION (t
);
1028 /* Look up source code line number for given bytecode (or direct threaded
1031 _Jv_InterpMethod::get_source_line(pc_t mpc
)
1033 int line
= line_table_len
> 0 ? line_table
[0].line
: -1;
1034 for (int i
= 1; i
< line_table_len
; i
++)
1035 if (line_table
[i
].pc
> mpc
)
1038 line
= line_table
[i
].line
;
1043 /** Do static initialization for fields with a constant initializer */
1045 _Jv_InitField (jobject obj
, jclass klass
, int index
)
1047 using namespace java::lang::reflect
;
1049 if (obj
!= 0 && klass
== 0)
1050 klass
= obj
->getClass ();
1052 if (!_Jv_IsInterpretedClass (klass
))
1055 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*)klass
->aux_info
;
1057 _Jv_Field
* field
= (&klass
->fields
[0]) + index
;
1059 if (index
> klass
->field_count
)
1060 throw_internal_error ("field out of range");
1062 int init
= iclass
->field_initializers
[index
];
1066 _Jv_Constants
*pool
= &klass
->constants
;
1067 int tag
= pool
->tags
[init
];
1069 if (! field
->isResolved ())
1070 throw_internal_error ("initializing unresolved field");
1072 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
1073 throw_internal_error ("initializing non-static field with no object");
1077 if ((field
->flags
& Modifier::STATIC
) != 0)
1078 addr
= (void*) field
->u
.addr
;
1080 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
1084 case JV_CONSTANT_String
:
1087 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
1088 pool
->data
[init
].string
= str
;
1089 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
1093 case JV_CONSTANT_ResolvedString
:
1094 if (! (field
->type
== &java::lang::String::class$
1095 || field
->type
== &java::lang::Class::class$
))
1096 throw_class_format_error ("string initialiser to non-string field");
1098 *(jstring
*)addr
= pool
->data
[init
].string
;
1101 case JV_CONSTANT_Integer
:
1103 int value
= pool
->data
[init
].i
;
1105 if (field
->type
== JvPrimClass (boolean
))
1106 *(jboolean
*)addr
= (jboolean
)value
;
1108 else if (field
->type
== JvPrimClass (byte
))
1109 *(jbyte
*)addr
= (jbyte
)value
;
1111 else if (field
->type
== JvPrimClass (char))
1112 *(jchar
*)addr
= (jchar
)value
;
1114 else if (field
->type
== JvPrimClass (short))
1115 *(jshort
*)addr
= (jshort
)value
;
1117 else if (field
->type
== JvPrimClass (int))
1118 *(jint
*)addr
= (jint
)value
;
1121 throw_class_format_error ("erroneous field initializer");
1125 case JV_CONSTANT_Long
:
1126 if (field
->type
!= JvPrimClass (long))
1127 throw_class_format_error ("erroneous field initializer");
1129 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
1132 case JV_CONSTANT_Float
:
1133 if (field
->type
!= JvPrimClass (float))
1134 throw_class_format_error ("erroneous field initializer");
1136 *(jfloat
*)addr
= pool
->data
[init
].f
;
1139 case JV_CONSTANT_Double
:
1140 if (field
->type
!= JvPrimClass (double))
1141 throw_class_format_error ("erroneous field initializer");
1143 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
1147 throw_class_format_error ("erroneous field initializer");
1151 inline static unsigned char*
1152 skip_one_type (unsigned char* ptr
)
1163 do { ch
= *ptr
++; } while (ch
!= ';');
1170 get_ffi_type_from_signature (unsigned char* ptr
)
1176 return &ffi_type_pointer
;
1180 // On some platforms a bool is a byte, on others an int.
1181 if (sizeof (jboolean
) == sizeof (jbyte
))
1182 return &ffi_type_sint8
;
1185 JvAssert (sizeof (jbyte
) == sizeof (jint
));
1186 return &ffi_type_sint32
;
1191 return &ffi_type_sint8
;
1195 return &ffi_type_uint16
;
1199 return &ffi_type_sint16
;
1203 return &ffi_type_sint32
;
1207 return &ffi_type_sint64
;
1211 return &ffi_type_float
;
1215 return &ffi_type_double
;
1219 return &ffi_type_void
;
1223 throw_internal_error ("unknown type in signature");
1226 /* this function yields the number of actual arguments, that is, if the
1227 * function is non-static, then one is added to the number of elements
1228 * found in the signature */
1231 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
1234 unsigned char *ptr
= (unsigned char*) signature
->chars();
1235 int arg_count
= staticp
? 0 : 1;
1237 /* first, count number of arguments */
1245 ptr
= skip_one_type (ptr
);
1252 /* This beast will build a cif, given the signature. Memory for
1253 * the cif itself and for the argument types must be allocated by the
1258 _Jv_init_cif (_Jv_Utf8Const
* signature
,
1262 ffi_type
**arg_types
,
1265 unsigned char *ptr
= (unsigned char*) signature
->chars();
1267 int arg_index
= 0; // arg number
1268 int item_count
= 0; // stack-item count
1273 arg_types
[arg_index
++] = &ffi_type_pointer
;
1283 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1285 if (*ptr
== 'J' || *ptr
== 'D')
1290 ptr
= skip_one_type (ptr
);
1295 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1297 ptr
= skip_one_type (ptr
);
1298 if (ptr
!= (unsigned char*)signature
->chars() + signature
->len())
1299 throw_internal_error ("did not find end of signature");
1301 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1302 arg_count
, rtype
, arg_types
) != FFI_OK
)
1303 throw_internal_error ("ffi_prep_cif failed");
1305 if (rtype_p
!= NULL
)
1311 #if FFI_NATIVE_RAW_API
1312 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1313 # define FFI_RAW_SIZE ffi_raw_size
1315 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1316 # define FFI_RAW_SIZE ffi_java_raw_size
1319 /* we put this one here, and not in interpret.cc because it
1320 * calls the utility routines _Jv_count_arguments
1321 * which are static to this module. The following struct defines the
1322 * layout we use for the stubs, it's only used in the ncode method. */
1325 ffi_raw_closure closure
;
1326 _Jv_ClosureList list
;
1328 ffi_type
*arg_types
[0];
1331 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
1334 _Jv_InterpMethod::ncode (jclass klass
)
1336 using namespace java::lang::reflect
;
1338 if (self
->ncode
!= 0)
1341 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1342 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1345 ncode_closure
*closure
=
1346 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1347 + arg_count
* sizeof (ffi_type
*),
1349 closure
->list
.registerClosure (klass
, closure
);
1351 _Jv_init_cif (self
->signature
,
1355 &closure
->arg_types
[0],
1358 ffi_closure_fun fun
;
1360 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1362 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1364 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1369 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1371 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1376 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1378 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1386 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1388 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1393 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1395 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1399 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1410 /* Find the index of the given insn in the array of insn slots
1411 for this method. Returns -1 if not found. */
1413 _Jv_InterpMethod::insn_index (pc_t pc
)
1416 #ifdef DIRECT_THREADED
1417 jlong right
= number_insn_slots
;
1418 pc_t insns
= prepared
;
1420 jlong right
= code_length
;
1421 pc_t insns
= bytecode ();
1426 jlong mid
= (left
+ right
) / 2;
1427 if (&insns
[mid
] == pc
)
1430 if (pc
< &insns
[mid
])
1439 // Method to check if an exception is caught at some location in a method
1440 // (meth). Returns true if this method (meth) contains a catch block for the
1441 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1442 // to the location of the beginning of the catch block.
1444 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1445 java::lang::Throwable
*ex
)
1447 #ifdef DIRECT_THREADED
1448 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1450 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1452 _Jv_InterpException
*exc
= meth
->exceptions ();
1453 jclass exc_class
= ex
->getClass ();
1455 for (int i
= 0; i
< meth
->exc_count
; i
++)
1457 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1458 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1460 #ifdef DIRECT_THREADED
1461 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1463 jclass handler
= NULL
;
1464 if (exc
[i
].handler_type
.i
!= 0)
1466 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1468 #endif /* DIRECT_THREADED */
1469 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1471 #ifdef DIRECT_THREADED
1472 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1474 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1475 #endif /* DIRECT_THREADED */
1485 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1486 jintArray
& line_numbers
,
1487 jlongArray
& code_indices
)
1489 #ifdef DIRECT_THREADED
1490 /* For the DIRECT_THREADED case, if the method has not yet been
1491 * compiled, the linetable will change to insn slots instead of
1492 * bytecode PCs. It is probably easiest, in this case, to simply
1493 * compile the method and guarantee that we are using insn
1496 _Jv_CompileMethod (this);
1498 if (line_table_len
> 0)
1501 end
= number_insn_slots
;
1502 line_numbers
= JvNewIntArray (line_table_len
);
1503 code_indices
= JvNewLongArray (line_table_len
);
1505 jint
* lines
= elements (line_numbers
);
1506 jlong
* indices
= elements (code_indices
);
1507 for (int i
= 0; i
< line_table_len
; ++i
)
1509 lines
[i
] = line_table
[i
].line
;
1510 indices
[i
] = insn_index (line_table
[i
].pc
);
1513 #else // !DIRECT_THREADED
1514 if (line_table_len
> 0)
1518 line_numbers
= JvNewIntArray (line_table_len
);
1519 code_indices
= JvNewLongArray (line_table_len
);
1521 jint
* lines
= elements (line_numbers
);
1522 jlong
* indices
= elements (code_indices
);
1523 for (int i
= 0; i
< line_table_len
; ++i
)
1525 lines
[i
] = line_table
[i
].line
;
1526 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1529 #endif // !DIRECT_THREADED
1533 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1534 char **generic_sig
, jlong
*startloc
,
1535 jint
*length
, jint
*slot
,
1538 if (local_var_table
== NULL
)
1540 if (table_slot
>= local_var_table_len
)
1544 *name
= local_var_table
[table_slot
].name
;
1545 *sig
= local_var_table
[table_slot
].descriptor
;
1546 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1548 #ifdef DIRECT_THREADED
1549 *startloc
= insn_index (local_var_table
[table_slot
].pc
);
1551 *startloc
= static_cast<jlong
> (local_var_table
[table_slot
].bytecode_pc
);
1553 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1554 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1556 return local_var_table_len
- table_slot
- 1;
1560 _Jv_InterpMethod::install_break (jlong index
)
1562 return set_insn (index
, breakpoint_insn
);
1566 _Jv_InterpMethod::get_insn (jlong index
)
1570 #ifdef DIRECT_THREADED
1571 if (index
>= number_insn_slots
|| index
< 0)
1575 #else // !DIRECT_THREADED
1576 if (index
>= code_length
|| index
< 0)
1579 code
= reinterpret_cast<pc_t
> (bytecode ());
1580 #endif // !DIRECT_THREADED
1582 return &code
[index
];
1586 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1588 #ifdef DIRECT_THREADED
1589 if (index
>= number_insn_slots
|| index
< 0)
1592 pc_t code
= prepared
;
1593 code
[index
].insn
= insn
->insn
;
1594 #else // !DIRECT_THREADED
1595 if (index
>= code_length
|| index
< 0)
1598 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1599 code
[index
] = *insn
;
1600 #endif // !DIRECT_THREADED
1602 return &code
[index
];
1606 _Jv_InterpMethod::breakpoint_at (jlong index
)
1608 pc_t insn
= get_insn (index
);
1611 #ifdef DIRECT_THREADED
1612 return (insn
->insn
== breakpoint_insn
->insn
);
1614 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1615 return (code
[index
] == breakpoint_insn
);
1623 _Jv_JNIMethod::ncode (jclass klass
)
1625 using namespace java::lang::reflect
;
1627 if (self
->ncode
!= 0)
1630 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1631 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1634 ncode_closure
*closure
=
1635 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1636 + arg_count
* sizeof (ffi_type
*),
1638 closure
->list
.registerClosure (klass
, closure
);
1641 _Jv_init_cif (self
->signature
,
1645 &closure
->arg_types
[0],
1648 ffi_closure_fun fun
;
1650 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1652 // Initialize the argument types and CIF that represent the actual
1653 // underlying JNI function.
1655 if ((self
->accflags
& Modifier::STATIC
))
1657 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1658 * sizeof (ffi_type
*));
1660 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1661 if ((self
->accflags
& Modifier::STATIC
))
1662 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1663 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1664 arg_count
* sizeof (ffi_type
*));
1666 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1667 extra_args
+ arg_count
, rtype
,
1668 jni_arg_types
) != FFI_OK
)
1669 throw_internal_error ("ffi_prep_cif failed for JNI function");
1671 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1673 // FIXME: for now we assume that all native methods for
1674 // interpreted code use JNI.
1675 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1677 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1688 throw_class_format_error (jstring msg
)
1691 ? new java::lang::ClassFormatError (msg
)
1692 : new java::lang::ClassFormatError
);
1693 REPORT_EXCEPTION (t
);
1698 throw_class_format_error (const char *msg
)
1700 throw_class_format_error (JvNewStringLatin1 (msg
));
1703 /* This function finds the method and location where the exception EXC
1704 is caught in the stack frame. On return, it sets CATCH_METHOD and
1705 CATCH_LOCATION with the method and location where the catch will
1706 occur. If the exception is not caught, these are set to 0.
1708 This function should only be used with the DEBUG interpreter. */
1710 find_catch_location (::java::lang::Throwable
*exc
, jthread thread
,
1711 jmethodID
*catch_method
, jlong
*catch_loc
)
1716 _Jv_InterpFrame
*frame
1717 = reinterpret_cast<_Jv_InterpFrame
*> (thread
->interp_frame
);
1718 while (frame
!= NULL
)
1720 pc_t pc
= frame
->get_pc ();
1721 _Jv_InterpMethod
*imeth
1722 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1723 if (imeth
->check_handler (&pc
, imeth
, exc
))
1725 // This method handles the exception.
1726 *catch_method
= imeth
->get_method ();
1727 *catch_loc
= imeth
->insn_index (pc
);
1731 frame
= frame
->next_interp
;
1735 /* This method handles JVMTI notifications of thrown exceptions. It
1736 calls find_catch_location to figure out where the exception is
1737 caught (if it is caught).
1739 Like find_catch_location, this should only be called with the
1740 DEBUG interpreter. Since a few exceptions occur outside the
1741 interpreter proper, it is important to not call this function
1742 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1744 _Jv_ReportJVMTIExceptionThrow (jthrowable ex
)
1746 jthread thread
= ::java::lang::Thread::currentThread ();
1747 _Jv_Frame
*frame
= reinterpret_cast<_Jv_Frame
*> (thread
->frame
);
1748 jmethodID throw_meth
= frame
->self
->get_method ();
1749 jlocation throw_loc
= -1;
1750 if (frame
->frame_type
== frame_interpreter
)
1752 _Jv_InterpFrame
* iframe
1753 = reinterpret_cast<_Jv_InterpFrame
*> (frame
);
1754 _Jv_InterpMethod
*imeth
1755 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1756 throw_loc
= imeth
->insn_index (iframe
->get_pc ());
1760 jmethodID catch_method
;
1761 find_catch_location (ex
, thread
, &catch_method
, &catch_loc
);
1762 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION
, thread
,
1763 _Jv_GetCurrentJNIEnv (), throw_meth
, throw_loc
,
1764 ex
, catch_method
, catch_loc
);
1770 _Jv_InterpreterEngine::do_verify (jclass klass
)
1772 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1773 for (int i
= 0; i
< klass
->method_count
; i
++)
1775 using namespace java::lang::reflect
;
1776 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1777 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1778 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1780 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1781 _Jv_VerifyMethod (im
);
1787 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1789 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1790 for (int i
= 0; i
< klass
->method_count
; i
++)
1792 // Just skip abstract methods. This is particularly important
1793 // because we don't resize the interpreted_methods array when
1794 // miranda methods are added to it.
1795 if ((klass
->methods
[i
].accflags
1796 & java::lang::reflect::Modifier::ABSTRACT
)
1800 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1802 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1805 // You might think we could use a virtual `ncode' method in
1806 // the _Jv_MethodBase and unify the native and non-native
1807 // cases. Well, we can't, because we don't allocate these
1808 // objects using `new', and thus they don't get a vtable.
1809 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1810 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1812 else if (imeth
!= 0) // it could be abstract
1814 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1815 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1821 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1823 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1825 if (!iclass
->closures
)
1826 iclass
->closures
= _Jv_ClosureListFinalizer ();
1828 return iclass
->closures
;
1832 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1836 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1838 // Splitting the allocations here lets us scan reference fields and
1839 // avoid scanning non-reference fields. How reference fields are
1840 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1841 // means that this memory will be scanned conservatively (same
1842 // difference, since we know all the contents here are pointers).
1843 // Then we put pointers into this memory into the 'fields'
1844 // structure. Most of these are interior pointers, which is ok (but
1845 // even so the pointer to the first reference field will be used and
1846 // that is not an interior pointer). The 'fields' array is also
1847 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1848 // be scanned. A pointer to this array is held by Class and thus
1849 // seen by the collector.
1850 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1851 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1853 for (int i
= 0; i
< klass
->field_count
; i
++)
1855 _Jv_Field
*field
= &klass
->fields
[i
];
1857 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1860 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1861 field
->u
.addr
= base
+ field
->u
.boffset
;
1863 if (iclass
->field_initializers
[i
] != 0)
1865 _Jv_Linker::resolve_field (field
, klass
->loader
);
1866 _Jv_InitField (0, klass
, i
);
1870 // Now we don't need the field_initializers anymore, so let the
1871 // collector get rid of it.
1872 iclass
->field_initializers
= 0;
1875 _Jv_ResolvedMethod
*
1876 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1879 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1881 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1882 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1883 + arg_count
*sizeof (ffi_type
*));
1885 result
->stack_item_count
1886 = _Jv_init_cif (method
->signature
,
1890 &result
->arg_types
[0],
1893 result
->method
= method
;
1894 result
->klass
= klass
;
1900 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1902 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1903 for (int i
= 0; i
< klass
->method_count
; i
++)
1905 // Just skip abstract methods. This is particularly important
1906 // because we don't resize the interpreted_methods array when
1907 // miranda methods are added to it.
1908 if ((klass
->methods
[i
].accflags
1909 & java::lang::reflect::Modifier::ABSTRACT
)
1912 // Miranda method additions mean that the `methods' array moves.
1913 // We cache a pointer into this array, so we have to update.
1914 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1918 #ifdef DIRECT_THREADED
1920 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1922 if (method
->prepared
== NULL
)
1925 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1927 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1930 #endif // DIRECT_THREADED
1932 #endif // INTERPRETER