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
;
84 _Jv_MutexInit (&compile_mutex
);
87 void _Jv_InitInterpreter() {}
90 // The breakpoint instruction. For the direct threaded case,
91 // _Jv_InterpMethod::compile will initialize breakpoint_insn
92 // the first time it is called.
93 #ifdef DIRECT_THREADED
94 insn_slot
_Jv_InterpMethod::bp_insn_slot
;
95 pc_t
_Jv_InterpMethod::breakpoint_insn
= NULL
;
97 unsigned char _Jv_InterpMethod::bp_insn_opcode
98 = static_cast<unsigned char> (op_breakpoint
);
99 pc_t
_Jv_InterpMethod::breakpoint_insn
= &_Jv_InterpMethod::bp_insn_opcode
;
102 extern "C" double __ieee754_fmod (double,double);
104 static inline void dupx (_Jv_word
*sp
, int n
, int x
)
106 // first "slide" n+x elements n to the right
108 for (int i
= 0; i
< n
+x
; i
++)
110 sp
[(top
-i
)] = sp
[(top
-i
)-n
];
113 // next, copy the n top elements, n+x down
114 for (int i
= 0; i
< n
; i
++)
116 sp
[top
-(n
+x
)-i
] = sp
[top
-i
];
120 // Used to convert from floating types to integral types.
121 template<typename TO
, typename FROM
>
123 convert (FROM val
, TO min
, TO max
)
126 if (val
>= (FROM
) max
)
128 else if (val
<= (FROM
) min
)
137 #define PUSHA(V) (sp++)->o = (V)
138 #define PUSHI(V) (sp++)->i = (V)
139 #define PUSHF(V) (sp++)->f = (V)
140 #if SIZEOF_VOID_P == 8
141 # define PUSHL(V) (sp->l = (V), sp += 2)
142 # define PUSHD(V) (sp->d = (V), sp += 2)
144 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
145 (sp++)->ia[0] = w2.ia[0]; \
146 (sp++)->ia[0] = w2.ia[1]; } while (0)
147 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
148 (sp++)->ia[0] = w2.ia[0]; \
149 (sp++)->ia[0] = w2.ia[1]; } while (0)
152 #define POPA() ((--sp)->o)
153 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
154 #define POPF() ((jfloat) (--sp)->f)
155 #if SIZEOF_VOID_P == 8
156 # define POPL() (sp -= 2, (jlong) sp->l)
157 # define POPD() (sp -= 2, (jdouble) sp->d)
159 # define POPL() ({ _Jv_word2 w2; \
160 w2.ia[1] = (--sp)->ia[0]; \
161 w2.ia[0] = (--sp)->ia[0]; w2.l; })
162 # define POPD() ({ _Jv_word2 w2; \
163 w2.ia[1] = (--sp)->ia[0]; \
164 w2.ia[0] = (--sp)->ia[0]; w2.d; })
167 #define LOADA(I) (sp++)->o = locals[I].o
168 #define LOADI(I) (sp++)->i = locals[I].i
169 #define LOADF(I) (sp++)->f = locals[I].f
170 #if SIZEOF_VOID_P == 8
171 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
172 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
174 # define LOADL(I) do { jint __idx = (I); \
175 (sp++)->ia[0] = locals[__idx].ia[0]; \
176 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
178 # define LOADD(I) LOADL(I)
185 DEBUG_LOCALS_INSN (__idx, 'o'); \
186 locals[__idx].o = (--sp)->o; \
193 DEBUG_LOCALS_INSN (__idx, 'i'); \
194 locals[__idx].i = (--sp)->i; \
200 DEBUG_LOCALS_INSN (__idx, 'f'); \
201 locals[__idx].f = (--sp)->f; \
204 #if SIZEOF_VOID_P == 8
209 DEBUG_LOCALS_INSN (__idx, 'l'); \
210 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
211 (sp -= 2, locals[__idx].l = sp->l); \
218 DEBUG_LOCALS_INSN (__idx, 'd'); \
219 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
220 (sp -= 2, locals[__idx].d = sp->d); \
229 DEBUG_LOCALS_INSN (__idx, 'l'); \
230 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
231 locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
232 locals[__idx].ia[0] = (--sp)->ia[0]; \
238 DEBUG_LOCALS_INSN (__idx, 'd'); \
239 DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
240 locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
241 locals[__idx].ia[0] = (--sp)->ia[0]; \
245 #define PEEKI(I) (locals+(I))->i
246 #define PEEKA(I) (locals+(I))->o
252 DEBUG_LOCALS_INSN (__idx, 'i'); \
253 ((locals + __idx)->i = (V)); \
258 #define BINOPI(OP) { \
259 jint value2 = POPI(); \
260 jint value1 = POPI(); \
261 PUSHI(value1 OP value2); \
264 #define BINOPF(OP) { \
265 jfloat value2 = POPF(); \
266 jfloat value1 = POPF(); \
267 PUSHF(value1 OP value2); \
270 #define BINOPL(OP) { \
271 jlong value2 = POPL(); \
272 jlong value1 = POPL(); \
273 PUSHL(value1 OP value2); \
276 #define BINOPD(OP) { \
277 jdouble value2 = POPD(); \
278 jdouble value1 = POPD(); \
279 PUSHD(value1 OP value2); \
283 get1s (unsigned char* loc
)
285 return *(signed char*)loc
;
289 get1u (unsigned char* loc
)
295 get2s(unsigned char* loc
)
297 return (((jint
)*(signed char*)loc
) << 8) | ((jint
)*(loc
+1));
301 get2u (unsigned char* loc
)
303 return (((jint
)(*loc
)) << 8) | ((jint
)*(loc
+1));
307 get4 (unsigned char* loc
)
309 return (((jint
)(loc
[0])) << 24)
310 | (((jint
)(loc
[1])) << 16)
311 | (((jint
)(loc
[2])) << 8)
312 | (((jint
)(loc
[3])) << 0);
315 #define SAVE_PC() frame_desc.pc = pc
317 // We used to define this conditionally, depending on HANDLE_SEGV.
318 // However, that runs into a problem if a chunk in low memory is
319 // mapped and we try to look at a field near the end of a large
320 // object. See PR 26858 for details. It is, most likely, relatively
321 // inexpensive to simply do this check always.
322 #define NULLCHECK(X) \
323 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
325 // Note that we can still conditionally define NULLARRAYCHECK, since
326 // we know that all uses of an array will first reference the length
327 // field, which is first -- and thus will trigger a SEGV.
329 #define NULLARRAYCHECK(X) SAVE_PC()
331 #define NULLARRAYCHECK(X) \
335 if ((X) == NULL) { throw_null_pointer_exception (); } \
339 #define ARRAYBOUNDSCHECK(array, index) \
342 if (((unsigned) index) >= (unsigned) (array->length)) \
343 _Jv_ThrowBadArrayIndex (index); \
347 _Jv_InterpMethod::run_normal (ffi_cif
*,
349 INTERP_FFI_RAW_TYPE
*args
,
352 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
353 run (ret
, args
, _this
);
357 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
359 INTERP_FFI_RAW_TYPE
*args
,
362 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
363 run_debug (ret
, args
, _this
);
367 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
369 INTERP_FFI_RAW_TYPE
*args
,
372 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
374 jobject rcv
= (jobject
) args
[0].ptr
;
375 JvSynchronize
mutex (rcv
);
377 run (ret
, args
, _this
);
381 _Jv_InterpMethod::run_synch_object_debug (ffi_cif
*,
383 INTERP_FFI_RAW_TYPE
*args
,
386 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
388 jobject rcv
= (jobject
) args
[0].ptr
;
389 JvSynchronize
mutex (rcv
);
391 run_debug (ret
, args
, _this
);
395 _Jv_InterpMethod::run_class (ffi_cif
*,
397 INTERP_FFI_RAW_TYPE
*args
,
400 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
401 _Jv_InitClass (_this
->defining_class
);
402 run (ret
, args
, _this
);
406 _Jv_InterpMethod::run_class_debug (ffi_cif
*,
408 INTERP_FFI_RAW_TYPE
*args
,
411 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
412 _Jv_InitClass (_this
->defining_class
);
413 run_debug (ret
, args
, _this
);
417 _Jv_InterpMethod::run_synch_class (ffi_cif
*,
419 INTERP_FFI_RAW_TYPE
*args
,
422 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
424 jclass sync
= _this
->defining_class
;
425 _Jv_InitClass (sync
);
426 JvSynchronize
mutex (sync
);
428 run (ret
, args
, _this
);
432 _Jv_InterpMethod::run_synch_class_debug (ffi_cif
*,
434 INTERP_FFI_RAW_TYPE
*args
,
437 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
439 jclass sync
= _this
->defining_class
;
440 _Jv_InitClass (sync
);
441 JvSynchronize
mutex (sync
);
443 run_debug (ret
, args
, _this
);
446 #ifdef DIRECT_THREADED
447 // "Compile" a method by turning it from bytecode to direct-threaded
450 _Jv_InterpMethod::compile (const void * const *insn_targets
)
452 insn_slot
*insns
= NULL
;
454 unsigned char *codestart
= bytecode ();
455 unsigned char *end
= codestart
+ code_length
;
456 _Jv_word
*pool_data
= defining_class
->constants
.data
;
458 #define SET_ONE(Field, Value) \
464 insns[next++].Field = Value; \
468 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
469 #define SET_INT(Value) SET_ONE (int_val, Value)
470 #define SET_DATUM(Value) SET_ONE (datum, Value)
472 // Map from bytecode PC to slot in INSNS.
473 int *pc_mapping
= (int *) __builtin_alloca (sizeof (int) * code_length
);
474 for (int i
= 0; i
< code_length
; ++i
)
477 for (int i
= 0; i
< 2; ++i
)
479 jboolean first_pass
= i
== 0;
483 insns
= (insn_slot
*) _Jv_AllocBytes (sizeof (insn_slot
) * next
);
484 number_insn_slots
= next
;
488 unsigned char *pc
= codestart
;
491 int base_pc_val
= pc
- codestart
;
493 pc_mapping
[base_pc_val
] = next
;
495 java_opcode opcode
= (java_opcode
) *pc
++;
497 if (opcode
== op_nop
)
499 SET_INSN (insn_targets
[opcode
]);
640 case op_monitorenter
:
650 // No argument, nothing else to do.
654 SET_INT (get1s (pc
));
660 int index
= get1u (pc
);
662 // For an unresolved class we want to delay resolution
664 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
667 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
671 SET_DATUM (pool_data
[index
].o
);
687 SET_INT (get1u (pc
));
692 SET_INT (get1u (pc
));
693 SET_INT (get1s (pc
+ 1));
699 int index
= get2u (pc
);
701 // For an unresolved class we want to delay resolution
703 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
706 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
710 SET_DATUM (pool_data
[index
].o
);
716 int index
= get2u (pc
);
718 SET_DATUM (&pool_data
[index
]);
723 SET_INT (get2s (pc
));
735 case op_invokespecial
:
736 case op_invokestatic
:
737 case op_invokevirtual
:
738 SET_INT (get2u (pc
));
742 case op_multianewarray
:
743 SET_INT (get2u (pc
));
744 SET_INT (get1u (pc
+ 2));
767 int offset
= get2s (pc
);
770 int new_pc
= base_pc_val
+ offset
;
772 bool orig_was_goto
= opcode
== op_goto
;
774 // Thread jumps. We limit the loop count; this lets
775 // us avoid infinite loops if the bytecode contains
776 // such. `10' is arbitrary.
778 while (codestart
[new_pc
] == op_goto
&& count
-- > 0)
779 new_pc
+= get2s (&codestart
[new_pc
+ 1]);
781 // If the jump takes us to a `return' instruction and
782 // the original branch was an unconditional goto, then
783 // we hoist the return.
784 opcode
= (java_opcode
) codestart
[new_pc
];
786 && (opcode
== op_ireturn
|| opcode
== op_lreturn
787 || opcode
== op_freturn
|| opcode
== op_dreturn
788 || opcode
== op_areturn
|| opcode
== op_return
))
791 SET_INSN (insn_targets
[opcode
]);
794 SET_DATUM (&insns
[pc_mapping
[new_pc
]]);
800 while ((pc
- codestart
) % 4 != 0)
803 jint def
= get4 (pc
);
804 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
810 int high
= get4 (pc
);
814 for (int i
= low
; i
<= high
; ++i
)
816 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ get4 (pc
)]]);
822 case op_lookupswitch
:
824 while ((pc
- codestart
) % 4 != 0)
827 jint def
= get4 (pc
);
828 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
831 jint npairs
= get4 (pc
);
837 jint match
= get4 (pc
);
838 jint offset
= get4 (pc
+ 4);
840 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
846 case op_invokeinterface
:
848 jint index
= get2u (pc
);
850 // We ignore the next two bytes.
858 opcode
= (java_opcode
) get1u (pc
);
860 jint val
= get2u (pc
);
863 // We implement narrow and wide instructions using the
864 // same code in the interpreter. So we rewrite the
865 // instruction slot here.
867 insns
[next
- 1].insn
= (void *) insn_targets
[opcode
];
870 if (opcode
== op_iinc
)
872 SET_INT (get2s (pc
));
881 jint offset
= get4 (pc
);
883 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
887 // Some "can't happen" cases that we include for
888 // error-checking purposes.
906 case op_getstatic_2s
:
907 case op_getstatic_2u
:
919 // Now update exceptions.
920 _Jv_InterpException
*exc
= exceptions ();
921 for (int i
= 0; i
< exc_count
; ++i
)
923 exc
[i
].start_pc
.p
= &insns
[pc_mapping
[exc
[i
].start_pc
.i
]];
924 exc
[i
].end_pc
.p
= &insns
[pc_mapping
[exc
[i
].end_pc
.i
]];
925 exc
[i
].handler_pc
.p
= &insns
[pc_mapping
[exc
[i
].handler_pc
.i
]];
926 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
927 // during compilation.
929 = (_Jv_Linker::resolve_pool_entry (defining_class
,
930 exc
[i
].handler_type
.i
)).clazz
;
931 exc
[i
].handler_type
.p
= handler
;
934 // Translate entries in the LineNumberTable from bytecode PC's to direct
935 // threaded interpreter instruction values.
936 for (int i
= 0; i
< line_table_len
; i
++)
938 int byte_pc
= line_table
[i
].bytecode_pc
;
939 // It isn't worth throwing an exception if this table is
940 // corrupted, but at the same time we don't want a crash.
941 if (byte_pc
< 0 || byte_pc
>= code_length
)
943 line_table
[i
].pc
= &insns
[pc_mapping
[byte_pc
]];
948 // Now remap the variable table for this method.
949 for (int i
= 0; i
< local_var_table_len
; ++i
)
951 int start_byte
= local_var_table
[i
].bytecode_pc
;
952 if (start_byte
< 0 || start_byte
>= code_length
)
954 jlocation start
= pc_mapping
[start_byte
];
956 int end_byte
= start_byte
+ local_var_table
[i
].length
;
959 jlocation end
= ((end_byte
>= code_length
)
961 : pc_mapping
[end_byte
]);
963 local_var_table
[i
].pc
= &insns
[start
];
964 local_var_table
[i
].length
= end
- start
+ 1;
967 if (breakpoint_insn
== NULL
)
969 bp_insn_slot
.insn
= const_cast<void *> (insn_targets
[op_breakpoint
]);
970 breakpoint_insn
= &bp_insn_slot
;
973 #endif /* DIRECT_THREADED */
975 /* Run the given method.
976 When args is NULL, don't run anything -- just compile it. */
978 _Jv_InterpMethod::run (void *retp
, INTERP_FFI_RAW_TYPE
*args
,
979 _Jv_InterpMethod
*meth
)
982 #undef DEBUG_LOCALS_INSN
983 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
985 #include "interpret-run.cc"
989 _Jv_InterpMethod::run_debug (void *retp
, INTERP_FFI_RAW_TYPE
*args
,
990 _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 /* we put this one here, and not in interpret.cc because it
1312 * calls the utility routines _Jv_count_arguments
1313 * which are static to this module. The following struct defines the
1314 * layout we use for the stubs, it's only used in the ncode method. */
1316 #if FFI_NATIVE_RAW_API
1317 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1318 # define FFI_RAW_SIZE ffi_raw_size
1320 ffi_raw_closure closure
;
1321 _Jv_ClosureList list
;
1323 ffi_type
*arg_types
[0];
1325 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,INTERP_FFI_RAW_TYPE
*,void*);
1327 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1328 # define FFI_RAW_SIZE ffi_java_raw_size
1330 ffi_java_raw_closure closure
;
1331 _Jv_ClosureList list
;
1333 ffi_type
*arg_types
[0];
1335 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_java_raw
*,void*);
1339 _Jv_InterpMethod::ncode (jclass klass
)
1341 using namespace java::lang::reflect
;
1343 if (self
->ncode
!= 0)
1346 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1347 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1350 ncode_closure
*closure
=
1351 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1352 + arg_count
* sizeof (ffi_type
*),
1354 closure
->list
.registerClosure (klass
, closure
);
1356 _Jv_init_cif (self
->signature
,
1360 &closure
->arg_types
[0],
1363 ffi_closure_fun fun
;
1365 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1367 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1369 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1374 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1376 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1381 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1383 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1391 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1393 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1398 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1400 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1404 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1415 /* Find the index of the given insn in the array of insn slots
1416 for this method. Returns -1 if not found. */
1418 _Jv_InterpMethod::insn_index (pc_t pc
)
1421 #ifdef DIRECT_THREADED
1422 jlong right
= number_insn_slots
;
1423 pc_t insns
= prepared
;
1425 jlong right
= code_length
;
1426 pc_t insns
= bytecode ();
1431 jlong mid
= (left
+ right
) / 2;
1432 if (&insns
[mid
] == pc
)
1435 if (pc
< &insns
[mid
])
1444 // Method to check if an exception is caught at some location in a method
1445 // (meth). Returns true if this method (meth) contains a catch block for the
1446 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1447 // to the location of the beginning of the catch block.
1449 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1450 java::lang::Throwable
*ex
)
1452 #ifdef DIRECT_THREADED
1453 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1455 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1457 _Jv_InterpException
*exc
= meth
->exceptions ();
1458 jclass exc_class
= ex
->getClass ();
1460 for (int i
= 0; i
< meth
->exc_count
; i
++)
1462 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1463 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1465 #ifdef DIRECT_THREADED
1466 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1468 jclass handler
= NULL
;
1469 if (exc
[i
].handler_type
.i
!= 0)
1471 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1473 #endif /* DIRECT_THREADED */
1474 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1476 #ifdef DIRECT_THREADED
1477 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1479 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1480 #endif /* DIRECT_THREADED */
1490 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1491 jintArray
& line_numbers
,
1492 jlongArray
& code_indices
)
1494 #ifdef DIRECT_THREADED
1495 /* For the DIRECT_THREADED case, if the method has not yet been
1496 * compiled, the linetable will change to insn slots instead of
1497 * bytecode PCs. It is probably easiest, in this case, to simply
1498 * compile the method and guarantee that we are using insn
1501 _Jv_CompileMethod (this);
1503 if (line_table_len
> 0)
1506 end
= number_insn_slots
;
1507 line_numbers
= JvNewIntArray (line_table_len
);
1508 code_indices
= JvNewLongArray (line_table_len
);
1510 jint
* lines
= elements (line_numbers
);
1511 jlong
* indices
= elements (code_indices
);
1512 for (int i
= 0; i
< line_table_len
; ++i
)
1514 lines
[i
] = line_table
[i
].line
;
1515 indices
[i
] = insn_index (line_table
[i
].pc
);
1518 #else // !DIRECT_THREADED
1519 if (line_table_len
> 0)
1523 line_numbers
= JvNewIntArray (line_table_len
);
1524 code_indices
= JvNewLongArray (line_table_len
);
1526 jint
* lines
= elements (line_numbers
);
1527 jlong
* indices
= elements (code_indices
);
1528 for (int i
= 0; i
< line_table_len
; ++i
)
1530 lines
[i
] = line_table
[i
].line
;
1531 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1534 #endif // !DIRECT_THREADED
1538 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1539 char **generic_sig
, jlong
*startloc
,
1540 jint
*length
, jint
*slot
,
1543 #ifdef DIRECT_THREADED
1544 _Jv_CompileMethod (this);
1547 if (local_var_table
== NULL
)
1549 if (table_slot
>= local_var_table_len
)
1553 *name
= local_var_table
[table_slot
].name
;
1554 *sig
= local_var_table
[table_slot
].descriptor
;
1555 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1557 #ifdef DIRECT_THREADED
1558 *startloc
= insn_index (local_var_table
[table_slot
].pc
);
1560 *startloc
= static_cast<jlong
> (local_var_table
[table_slot
].bytecode_pc
);
1562 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1563 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1565 return local_var_table_len
- table_slot
- 1;
1569 _Jv_InterpMethod::install_break (jlong index
)
1571 return set_insn (index
, breakpoint_insn
);
1575 _Jv_InterpMethod::get_insn (jlong index
)
1579 #ifdef DIRECT_THREADED
1580 if (index
>= number_insn_slots
|| index
< 0)
1584 #else // !DIRECT_THREADED
1585 if (index
>= code_length
|| index
< 0)
1588 code
= reinterpret_cast<pc_t
> (bytecode ());
1589 #endif // !DIRECT_THREADED
1591 return &code
[index
];
1595 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1597 #ifdef DIRECT_THREADED
1598 if (index
>= number_insn_slots
|| index
< 0)
1601 pc_t code
= prepared
;
1602 code
[index
].insn
= insn
->insn
;
1603 #else // !DIRECT_THREADED
1604 if (index
>= code_length
|| index
< 0)
1607 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1608 code
[index
] = *insn
;
1609 #endif // !DIRECT_THREADED
1611 return &code
[index
];
1615 _Jv_InterpMethod::breakpoint_at (jlong index
)
1617 pc_t insn
= get_insn (index
);
1620 #ifdef DIRECT_THREADED
1621 return (insn
->insn
== breakpoint_insn
->insn
);
1623 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1624 return (code
[index
] == breakpoint_insn
);
1632 _Jv_JNIMethod::ncode (jclass klass
)
1634 using namespace java::lang::reflect
;
1636 if (self
->ncode
!= 0)
1639 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1640 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1643 ncode_closure
*closure
=
1644 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1645 + arg_count
* sizeof (ffi_type
*),
1647 closure
->list
.registerClosure (klass
, closure
);
1650 _Jv_init_cif (self
->signature
,
1654 &closure
->arg_types
[0],
1657 ffi_closure_fun fun
;
1659 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1661 // Initialize the argument types and CIF that represent the actual
1662 // underlying JNI function.
1664 if ((self
->accflags
& Modifier::STATIC
))
1666 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1667 * sizeof (ffi_type
*));
1669 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1670 if ((self
->accflags
& Modifier::STATIC
))
1671 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1672 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1673 arg_count
* sizeof (ffi_type
*));
1675 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1676 extra_args
+ arg_count
, rtype
,
1677 jni_arg_types
) != FFI_OK
)
1678 throw_internal_error ("ffi_prep_cif failed for JNI function");
1680 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1682 // FIXME: for now we assume that all native methods for
1683 // interpreted code use JNI.
1684 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1686 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1697 throw_class_format_error (jstring msg
)
1700 ? new java::lang::ClassFormatError (msg
)
1701 : new java::lang::ClassFormatError
);
1702 REPORT_EXCEPTION (t
);
1707 throw_class_format_error (const char *msg
)
1709 throw_class_format_error (JvNewStringLatin1 (msg
));
1712 /* This function finds the method and location where the exception EXC
1713 is caught in the stack frame. On return, it sets CATCH_METHOD and
1714 CATCH_LOCATION with the method and location where the catch will
1715 occur. If the exception is not caught, these are set to 0.
1717 This function should only be used with the DEBUG interpreter. */
1719 find_catch_location (::java::lang::Throwable
*exc
, jthread thread
,
1720 jmethodID
*catch_method
, jlong
*catch_loc
)
1725 _Jv_InterpFrame
*frame
1726 = reinterpret_cast<_Jv_InterpFrame
*> (thread
->interp_frame
);
1727 while (frame
!= NULL
)
1729 pc_t pc
= frame
->get_pc ();
1730 _Jv_InterpMethod
*imeth
1731 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1732 if (imeth
->check_handler (&pc
, imeth
, exc
))
1734 // This method handles the exception.
1735 *catch_method
= imeth
->get_method ();
1736 *catch_loc
= imeth
->insn_index (pc
);
1740 frame
= frame
->next_interp
;
1744 /* This method handles JVMTI notifications of thrown exceptions. It
1745 calls find_catch_location to figure out where the exception is
1746 caught (if it is caught).
1748 Like find_catch_location, this should only be called with the
1749 DEBUG interpreter. Since a few exceptions occur outside the
1750 interpreter proper, it is important to not call this function
1751 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1753 _Jv_ReportJVMTIExceptionThrow (jthrowable ex
)
1755 jthread thread
= ::java::lang::Thread::currentThread ();
1756 _Jv_Frame
*frame
= reinterpret_cast<_Jv_Frame
*> (thread
->frame
);
1757 jmethodID throw_meth
= frame
->self
->get_method ();
1758 jlocation throw_loc
= -1;
1759 if (frame
->frame_type
== frame_interpreter
)
1761 _Jv_InterpFrame
* iframe
1762 = reinterpret_cast<_Jv_InterpFrame
*> (frame
);
1763 _Jv_InterpMethod
*imeth
1764 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1765 throw_loc
= imeth
->insn_index (iframe
->get_pc ());
1769 jmethodID catch_method
;
1770 find_catch_location (ex
, thread
, &catch_method
, &catch_loc
);
1771 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION
, thread
,
1772 _Jv_GetCurrentJNIEnv (), throw_meth
, throw_loc
,
1773 ex
, catch_method
, catch_loc
);
1779 _Jv_InterpreterEngine::do_verify (jclass klass
)
1781 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1782 for (int i
= 0; i
< klass
->method_count
; i
++)
1784 using namespace java::lang::reflect
;
1785 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1786 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1787 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1789 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1790 _Jv_VerifyMethod (im
);
1796 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1798 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1799 for (int i
= 0; i
< klass
->method_count
; i
++)
1801 // Just skip abstract methods. This is particularly important
1802 // because we don't resize the interpreted_methods array when
1803 // miranda methods are added to it.
1804 if ((klass
->methods
[i
].accflags
1805 & java::lang::reflect::Modifier::ABSTRACT
)
1809 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1811 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1814 // You might think we could use a virtual `ncode' method in
1815 // the _Jv_MethodBase and unify the native and non-native
1816 // cases. Well, we can't, because we don't allocate these
1817 // objects using `new', and thus they don't get a vtable.
1818 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1819 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1821 else if (imeth
!= 0) // it could be abstract
1823 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1824 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1830 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1832 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1834 if (!iclass
->closures
)
1835 iclass
->closures
= _Jv_ClosureListFinalizer ();
1837 return iclass
->closures
;
1841 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1845 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1847 // Splitting the allocations here lets us scan reference fields and
1848 // avoid scanning non-reference fields. How reference fields are
1849 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1850 // means that this memory will be scanned conservatively (same
1851 // difference, since we know all the contents here are pointers).
1852 // Then we put pointers into this memory into the 'fields'
1853 // structure. Most of these are interior pointers, which is ok (but
1854 // even so the pointer to the first reference field will be used and
1855 // that is not an interior pointer). The 'fields' array is also
1856 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1857 // be scanned. A pointer to this array is held by Class and thus
1858 // seen by the collector.
1859 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1860 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1862 for (int i
= 0; i
< klass
->field_count
; i
++)
1864 _Jv_Field
*field
= &klass
->fields
[i
];
1866 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1869 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1870 field
->u
.addr
= base
+ field
->u
.boffset
;
1872 if (iclass
->field_initializers
[i
] != 0)
1874 _Jv_Linker::resolve_field (field
, klass
->loader
);
1875 _Jv_InitField (0, klass
, i
);
1879 // Now we don't need the field_initializers anymore, so let the
1880 // collector get rid of it.
1881 iclass
->field_initializers
= 0;
1884 _Jv_ResolvedMethod
*
1885 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1888 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1890 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1891 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1892 + arg_count
*sizeof (ffi_type
*));
1894 result
->stack_item_count
1895 = _Jv_init_cif (method
->signature
,
1899 &result
->arg_types
[0],
1902 result
->method
= method
;
1903 result
->klass
= klass
;
1909 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1911 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1912 for (int i
= 0; i
< klass
->method_count
; i
++)
1914 // Just skip abstract methods. This is particularly important
1915 // because we don't resize the interpreted_methods array when
1916 // miranda methods are added to it.
1917 if ((klass
->methods
[i
].accflags
1918 & java::lang::reflect::Modifier::ABSTRACT
)
1921 // Miranda method additions mean that the `methods' array moves.
1922 // We cache a pointer into this array, so we have to update.
1923 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1927 #ifdef DIRECT_THREADED
1929 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1931 if (method
->prepared
== NULL
)
1934 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1936 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1939 #endif // DIRECT_THREADED