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
*,
352 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
353 run (ret
, args
, _this
);
357 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
362 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
363 run_debug (ret
, args
, _this
);
367 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
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
*,
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
*,
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
*,
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
*,
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
*,
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
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
981 #undef DEBUG_LOCALS_INSN
982 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
984 #include "interpret-run.cc"
988 _Jv_InterpMethod::run_debug (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
991 #undef DEBUG_LOCALS_INSN
992 #define DEBUG_LOCALS_INSN(s, t) \
995 frame_desc.locals_type[s] = t; \
999 #include "interpret-run.cc"
1003 throw_internal_error (const char *msg
)
1005 jthrowable t
= new java::lang::InternalError (JvNewStringLatin1 (msg
));
1006 REPORT_EXCEPTION (t
);
1011 throw_incompatible_class_change_error (jstring msg
)
1013 jthrowable t
= new java::lang::IncompatibleClassChangeError (msg
);
1014 REPORT_EXCEPTION (t
);
1019 throw_null_pointer_exception ()
1021 jthrowable t
= new java::lang::NullPointerException
;
1022 REPORT_EXCEPTION (t
);
1026 /* Look up source code line number for given bytecode (or direct threaded
1029 _Jv_InterpMethod::get_source_line(pc_t mpc
)
1031 int line
= line_table_len
> 0 ? line_table
[0].line
: -1;
1032 for (int i
= 1; i
< line_table_len
; i
++)
1033 if (line_table
[i
].pc
> mpc
)
1036 line
= line_table
[i
].line
;
1041 /** Do static initialization for fields with a constant initializer */
1043 _Jv_InitField (jobject obj
, jclass klass
, int index
)
1045 using namespace java::lang::reflect
;
1047 if (obj
!= 0 && klass
== 0)
1048 klass
= obj
->getClass ();
1050 if (!_Jv_IsInterpretedClass (klass
))
1053 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*)klass
->aux_info
;
1055 _Jv_Field
* field
= (&klass
->fields
[0]) + index
;
1057 if (index
> klass
->field_count
)
1058 throw_internal_error ("field out of range");
1060 int init
= iclass
->field_initializers
[index
];
1064 _Jv_Constants
*pool
= &klass
->constants
;
1065 int tag
= pool
->tags
[init
];
1067 if (! field
->isResolved ())
1068 throw_internal_error ("initializing unresolved field");
1070 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
1071 throw_internal_error ("initializing non-static field with no object");
1075 if ((field
->flags
& Modifier::STATIC
) != 0)
1076 addr
= (void*) field
->u
.addr
;
1078 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
1082 case JV_CONSTANT_String
:
1085 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
1086 pool
->data
[init
].string
= str
;
1087 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
1091 case JV_CONSTANT_ResolvedString
:
1092 if (! (field
->type
== &java::lang::String::class$
1093 || field
->type
== &java::lang::Class::class$
))
1094 throw_class_format_error ("string initialiser to non-string field");
1096 *(jstring
*)addr
= pool
->data
[init
].string
;
1099 case JV_CONSTANT_Integer
:
1101 int value
= pool
->data
[init
].i
;
1103 if (field
->type
== JvPrimClass (boolean
))
1104 *(jboolean
*)addr
= (jboolean
)value
;
1106 else if (field
->type
== JvPrimClass (byte
))
1107 *(jbyte
*)addr
= (jbyte
)value
;
1109 else if (field
->type
== JvPrimClass (char))
1110 *(jchar
*)addr
= (jchar
)value
;
1112 else if (field
->type
== JvPrimClass (short))
1113 *(jshort
*)addr
= (jshort
)value
;
1115 else if (field
->type
== JvPrimClass (int))
1116 *(jint
*)addr
= (jint
)value
;
1119 throw_class_format_error ("erroneous field initializer");
1123 case JV_CONSTANT_Long
:
1124 if (field
->type
!= JvPrimClass (long))
1125 throw_class_format_error ("erroneous field initializer");
1127 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
1130 case JV_CONSTANT_Float
:
1131 if (field
->type
!= JvPrimClass (float))
1132 throw_class_format_error ("erroneous field initializer");
1134 *(jfloat
*)addr
= pool
->data
[init
].f
;
1137 case JV_CONSTANT_Double
:
1138 if (field
->type
!= JvPrimClass (double))
1139 throw_class_format_error ("erroneous field initializer");
1141 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
1145 throw_class_format_error ("erroneous field initializer");
1149 inline static unsigned char*
1150 skip_one_type (unsigned char* ptr
)
1161 do { ch
= *ptr
++; } while (ch
!= ';');
1168 get_ffi_type_from_signature (unsigned char* ptr
)
1174 return &ffi_type_pointer
;
1178 // On some platforms a bool is a byte, on others an int.
1179 if (sizeof (jboolean
) == sizeof (jbyte
))
1180 return &ffi_type_sint8
;
1183 JvAssert (sizeof (jbyte
) == sizeof (jint
));
1184 return &ffi_type_sint32
;
1189 return &ffi_type_sint8
;
1193 return &ffi_type_uint16
;
1197 return &ffi_type_sint16
;
1201 return &ffi_type_sint32
;
1205 return &ffi_type_sint64
;
1209 return &ffi_type_float
;
1213 return &ffi_type_double
;
1217 return &ffi_type_void
;
1221 throw_internal_error ("unknown type in signature");
1224 /* this function yields the number of actual arguments, that is, if the
1225 * function is non-static, then one is added to the number of elements
1226 * found in the signature */
1229 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
1232 unsigned char *ptr
= (unsigned char*) signature
->chars();
1233 int arg_count
= staticp
? 0 : 1;
1235 /* first, count number of arguments */
1243 ptr
= skip_one_type (ptr
);
1250 /* This beast will build a cif, given the signature. Memory for
1251 * the cif itself and for the argument types must be allocated by the
1256 _Jv_init_cif (_Jv_Utf8Const
* signature
,
1260 ffi_type
**arg_types
,
1263 unsigned char *ptr
= (unsigned char*) signature
->chars();
1265 int arg_index
= 0; // arg number
1266 int item_count
= 0; // stack-item count
1271 arg_types
[arg_index
++] = &ffi_type_pointer
;
1281 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1283 if (*ptr
== 'J' || *ptr
== 'D')
1288 ptr
= skip_one_type (ptr
);
1293 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1295 ptr
= skip_one_type (ptr
);
1296 if (ptr
!= (unsigned char*)signature
->chars() + signature
->len())
1297 throw_internal_error ("did not find end of signature");
1299 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1300 arg_count
, rtype
, arg_types
) != FFI_OK
)
1301 throw_internal_error ("ffi_prep_cif failed");
1303 if (rtype_p
!= NULL
)
1309 #if FFI_NATIVE_RAW_API
1310 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1311 # define FFI_RAW_SIZE ffi_raw_size
1313 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1314 # define FFI_RAW_SIZE ffi_java_raw_size
1317 /* we put this one here, and not in interpret.cc because it
1318 * calls the utility routines _Jv_count_arguments
1319 * which are static to this module. The following struct defines the
1320 * layout we use for the stubs, it's only used in the ncode method. */
1323 ffi_raw_closure closure
;
1324 _Jv_ClosureList list
;
1326 ffi_type
*arg_types
[0];
1329 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
1332 _Jv_InterpMethod::ncode (jclass klass
)
1334 using namespace java::lang::reflect
;
1336 if (self
->ncode
!= 0)
1339 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1340 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1343 ncode_closure
*closure
=
1344 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1345 + arg_count
* sizeof (ffi_type
*),
1347 closure
->list
.registerClosure (klass
, closure
);
1349 _Jv_init_cif (self
->signature
,
1353 &closure
->arg_types
[0],
1356 ffi_closure_fun fun
;
1358 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1360 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1362 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1367 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1369 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1374 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1376 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1384 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1386 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1391 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1393 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1397 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1408 /* Find the index of the given insn in the array of insn slots
1409 for this method. Returns -1 if not found. */
1411 _Jv_InterpMethod::insn_index (pc_t pc
)
1414 #ifdef DIRECT_THREADED
1415 jlong right
= number_insn_slots
;
1416 pc_t insns
= prepared
;
1418 jlong right
= code_length
;
1419 pc_t insns
= bytecode ();
1424 jlong mid
= (left
+ right
) / 2;
1425 if (&insns
[mid
] == pc
)
1428 if (pc
< &insns
[mid
])
1437 // Method to check if an exception is caught at some location in a method
1438 // (meth). Returns true if this method (meth) contains a catch block for the
1439 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1440 // to the location of the beginning of the catch block.
1442 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1443 java::lang::Throwable
*ex
)
1445 #ifdef DIRECT_THREADED
1446 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1448 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1450 _Jv_InterpException
*exc
= meth
->exceptions ();
1451 jclass exc_class
= ex
->getClass ();
1453 for (int i
= 0; i
< meth
->exc_count
; i
++)
1455 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1456 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1458 #ifdef DIRECT_THREADED
1459 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1461 jclass handler
= NULL
;
1462 if (exc
[i
].handler_type
.i
!= 0)
1464 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1466 #endif /* DIRECT_THREADED */
1467 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1469 #ifdef DIRECT_THREADED
1470 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1472 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1473 #endif /* DIRECT_THREADED */
1483 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1484 jintArray
& line_numbers
,
1485 jlongArray
& code_indices
)
1487 #ifdef DIRECT_THREADED
1488 /* For the DIRECT_THREADED case, if the method has not yet been
1489 * compiled, the linetable will change to insn slots instead of
1490 * bytecode PCs. It is probably easiest, in this case, to simply
1491 * compile the method and guarantee that we are using insn
1494 _Jv_CompileMethod (this);
1496 if (line_table_len
> 0)
1499 end
= number_insn_slots
;
1500 line_numbers
= JvNewIntArray (line_table_len
);
1501 code_indices
= JvNewLongArray (line_table_len
);
1503 jint
* lines
= elements (line_numbers
);
1504 jlong
* indices
= elements (code_indices
);
1505 for (int i
= 0; i
< line_table_len
; ++i
)
1507 lines
[i
] = line_table
[i
].line
;
1508 indices
[i
] = insn_index (line_table
[i
].pc
);
1511 #else // !DIRECT_THREADED
1512 if (line_table_len
> 0)
1516 line_numbers
= JvNewIntArray (line_table_len
);
1517 code_indices
= JvNewLongArray (line_table_len
);
1519 jint
* lines
= elements (line_numbers
);
1520 jlong
* indices
= elements (code_indices
);
1521 for (int i
= 0; i
< line_table_len
; ++i
)
1523 lines
[i
] = line_table
[i
].line
;
1524 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1527 #endif // !DIRECT_THREADED
1531 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1532 char **generic_sig
, jlong
*startloc
,
1533 jint
*length
, jint
*slot
,
1536 #ifdef DIRECT_THREADED
1537 _Jv_CompileMethod (this);
1540 if (local_var_table
== NULL
)
1542 if (table_slot
>= local_var_table_len
)
1546 *name
= local_var_table
[table_slot
].name
;
1547 *sig
= local_var_table
[table_slot
].descriptor
;
1548 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1550 #ifdef DIRECT_THREADED
1551 *startloc
= insn_index (local_var_table
[table_slot
].pc
);
1553 *startloc
= static_cast<jlong
> (local_var_table
[table_slot
].bytecode_pc
);
1555 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1556 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1558 return local_var_table_len
- table_slot
- 1;
1562 _Jv_InterpMethod::install_break (jlong index
)
1564 return set_insn (index
, breakpoint_insn
);
1568 _Jv_InterpMethod::get_insn (jlong index
)
1572 #ifdef DIRECT_THREADED
1573 if (index
>= number_insn_slots
|| index
< 0)
1577 #else // !DIRECT_THREADED
1578 if (index
>= code_length
|| index
< 0)
1581 code
= reinterpret_cast<pc_t
> (bytecode ());
1582 #endif // !DIRECT_THREADED
1584 return &code
[index
];
1588 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1590 #ifdef DIRECT_THREADED
1591 if (index
>= number_insn_slots
|| index
< 0)
1594 pc_t code
= prepared
;
1595 code
[index
].insn
= insn
->insn
;
1596 #else // !DIRECT_THREADED
1597 if (index
>= code_length
|| index
< 0)
1600 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1601 code
[index
] = *insn
;
1602 #endif // !DIRECT_THREADED
1604 return &code
[index
];
1608 _Jv_InterpMethod::breakpoint_at (jlong index
)
1610 pc_t insn
= get_insn (index
);
1613 #ifdef DIRECT_THREADED
1614 return (insn
->insn
== breakpoint_insn
->insn
);
1616 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1617 return (code
[index
] == breakpoint_insn
);
1625 _Jv_JNIMethod::ncode (jclass klass
)
1627 using namespace java::lang::reflect
;
1629 if (self
->ncode
!= 0)
1632 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1633 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1636 ncode_closure
*closure
=
1637 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1638 + arg_count
* sizeof (ffi_type
*),
1640 closure
->list
.registerClosure (klass
, closure
);
1643 _Jv_init_cif (self
->signature
,
1647 &closure
->arg_types
[0],
1650 ffi_closure_fun fun
;
1652 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1654 // Initialize the argument types and CIF that represent the actual
1655 // underlying JNI function.
1657 if ((self
->accflags
& Modifier::STATIC
))
1659 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1660 * sizeof (ffi_type
*));
1662 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1663 if ((self
->accflags
& Modifier::STATIC
))
1664 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1665 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1666 arg_count
* sizeof (ffi_type
*));
1668 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1669 extra_args
+ arg_count
, rtype
,
1670 jni_arg_types
) != FFI_OK
)
1671 throw_internal_error ("ffi_prep_cif failed for JNI function");
1673 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1675 // FIXME: for now we assume that all native methods for
1676 // interpreted code use JNI.
1677 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1679 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1690 throw_class_format_error (jstring msg
)
1693 ? new java::lang::ClassFormatError (msg
)
1694 : new java::lang::ClassFormatError
);
1695 REPORT_EXCEPTION (t
);
1700 throw_class_format_error (const char *msg
)
1702 throw_class_format_error (JvNewStringLatin1 (msg
));
1705 /* This function finds the method and location where the exception EXC
1706 is caught in the stack frame. On return, it sets CATCH_METHOD and
1707 CATCH_LOCATION with the method and location where the catch will
1708 occur. If the exception is not caught, these are set to 0.
1710 This function should only be used with the DEBUG interpreter. */
1712 find_catch_location (::java::lang::Throwable
*exc
, jthread thread
,
1713 jmethodID
*catch_method
, jlong
*catch_loc
)
1718 _Jv_InterpFrame
*frame
1719 = reinterpret_cast<_Jv_InterpFrame
*> (thread
->interp_frame
);
1720 while (frame
!= NULL
)
1722 pc_t pc
= frame
->get_pc ();
1723 _Jv_InterpMethod
*imeth
1724 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1725 if (imeth
->check_handler (&pc
, imeth
, exc
))
1727 // This method handles the exception.
1728 *catch_method
= imeth
->get_method ();
1729 *catch_loc
= imeth
->insn_index (pc
);
1733 frame
= frame
->next_interp
;
1737 /* This method handles JVMTI notifications of thrown exceptions. It
1738 calls find_catch_location to figure out where the exception is
1739 caught (if it is caught).
1741 Like find_catch_location, this should only be called with the
1742 DEBUG interpreter. Since a few exceptions occur outside the
1743 interpreter proper, it is important to not call this function
1744 without checking JVMTI_REQUESTED_EVENT(Exception) first. */
1746 _Jv_ReportJVMTIExceptionThrow (jthrowable ex
)
1748 jthread thread
= ::java::lang::Thread::currentThread ();
1749 _Jv_Frame
*frame
= reinterpret_cast<_Jv_Frame
*> (thread
->frame
);
1750 jmethodID throw_meth
= frame
->self
->get_method ();
1751 jlocation throw_loc
= -1;
1752 if (frame
->frame_type
== frame_interpreter
)
1754 _Jv_InterpFrame
* iframe
1755 = reinterpret_cast<_Jv_InterpFrame
*> (frame
);
1756 _Jv_InterpMethod
*imeth
1757 = reinterpret_cast<_Jv_InterpMethod
*> (frame
->self
);
1758 throw_loc
= imeth
->insn_index (iframe
->get_pc ());
1762 jmethodID catch_method
;
1763 find_catch_location (ex
, thread
, &catch_method
, &catch_loc
);
1764 _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION
, thread
,
1765 _Jv_GetCurrentJNIEnv (), throw_meth
, throw_loc
,
1766 ex
, catch_method
, catch_loc
);
1772 _Jv_InterpreterEngine::do_verify (jclass klass
)
1774 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1775 for (int i
= 0; i
< klass
->method_count
; i
++)
1777 using namespace java::lang::reflect
;
1778 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1779 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1780 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1782 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1783 _Jv_VerifyMethod (im
);
1789 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1791 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1792 for (int i
= 0; i
< klass
->method_count
; i
++)
1794 // Just skip abstract methods. This is particularly important
1795 // because we don't resize the interpreted_methods array when
1796 // miranda methods are added to it.
1797 if ((klass
->methods
[i
].accflags
1798 & java::lang::reflect::Modifier::ABSTRACT
)
1802 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1804 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1807 // You might think we could use a virtual `ncode' method in
1808 // the _Jv_MethodBase and unify the native and non-native
1809 // cases. Well, we can't, because we don't allocate these
1810 // objects using `new', and thus they don't get a vtable.
1811 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1812 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1814 else if (imeth
!= 0) // it could be abstract
1816 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1817 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1823 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1825 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1827 if (!iclass
->closures
)
1828 iclass
->closures
= _Jv_ClosureListFinalizer ();
1830 return iclass
->closures
;
1834 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1838 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1840 // Splitting the allocations here lets us scan reference fields and
1841 // avoid scanning non-reference fields. How reference fields are
1842 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1843 // means that this memory will be scanned conservatively (same
1844 // difference, since we know all the contents here are pointers).
1845 // Then we put pointers into this memory into the 'fields'
1846 // structure. Most of these are interior pointers, which is ok (but
1847 // even so the pointer to the first reference field will be used and
1848 // that is not an interior pointer). The 'fields' array is also
1849 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1850 // be scanned. A pointer to this array is held by Class and thus
1851 // seen by the collector.
1852 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1853 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1855 for (int i
= 0; i
< klass
->field_count
; i
++)
1857 _Jv_Field
*field
= &klass
->fields
[i
];
1859 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1862 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1863 field
->u
.addr
= base
+ field
->u
.boffset
;
1865 if (iclass
->field_initializers
[i
] != 0)
1867 _Jv_Linker::resolve_field (field
, klass
->loader
);
1868 _Jv_InitField (0, klass
, i
);
1872 // Now we don't need the field_initializers anymore, so let the
1873 // collector get rid of it.
1874 iclass
->field_initializers
= 0;
1877 _Jv_ResolvedMethod
*
1878 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1881 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1883 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1884 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1885 + arg_count
*sizeof (ffi_type
*));
1887 result
->stack_item_count
1888 = _Jv_init_cif (method
->signature
,
1892 &result
->arg_types
[0],
1895 result
->method
= method
;
1896 result
->klass
= klass
;
1902 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1904 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1905 for (int i
= 0; i
< klass
->method_count
; i
++)
1907 // Just skip abstract methods. This is particularly important
1908 // because we don't resize the interpreted_methods array when
1909 // miranda methods are added to it.
1910 if ((klass
->methods
[i
].accflags
1911 & java::lang::reflect::Modifier::ABSTRACT
)
1914 // Miranda method additions mean that the `methods' array moves.
1915 // We cache a pointer into this array, so we have to update.
1916 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1920 #ifdef DIRECT_THREADED
1922 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1924 if (method
->prepared
== NULL
)
1927 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1929 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1932 #endif // DIRECT_THREADED