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/classpath/jdwp/Jdwp.h>
44 #include <gnu/gcj/jvmti/Breakpoint.h>
45 #include <gnu/gcj/jvmti/BreakpointManager.h>
46 #include <gnu/gcj/jvmti/ExceptionEvent.h>
50 // Execution engine for interpreted code.
51 _Jv_InterpreterEngine _Jv_soleInterpreterEngine
;
57 static void throw_internal_error (const char *msg
)
58 __attribute__ ((__noreturn__
));
59 static void throw_incompatible_class_change_error (jstring msg
)
60 __attribute__ ((__noreturn__
));
61 static void throw_null_pointer_exception ()
62 __attribute__ ((__noreturn__
));
64 static void throw_class_format_error (jstring msg
)
65 __attribute__ ((__noreturn__
));
66 static void throw_class_format_error (const char *msg
)
67 __attribute__ ((__noreturn__
));
69 #ifdef DIRECT_THREADED
70 // Lock to ensure that methods are not compiled concurrently.
71 // We could use a finer-grained lock here, however it is not safe to use
72 // the Class monitor as user code in another thread could hold it.
73 static _Jv_Mutex_t compile_mutex
;
78 _Jv_MutexInit (&compile_mutex
);
81 void _Jv_InitInterpreter() {}
84 // The breakpoint instruction. For the direct threaded case,
85 // _Jv_InterpMethod::compile will initialize breakpoint_insn
86 // the first time it is called.
87 #ifdef DIRECT_THREADED
88 insn_slot
_Jv_InterpMethod::bp_insn_slot
;
89 pc_t
_Jv_InterpMethod::breakpoint_insn
= NULL
;
91 unsigned char _Jv_InterpMethod::bp_insn_opcode
92 = static_cast<unsigned char> (op_breakpoint
);
93 pc_t
_Jv_InterpMethod::breakpoint_insn
= &_Jv_InterpMethod::bp_insn_opcode
;
96 extern "C" double __ieee754_fmod (double,double);
98 static inline void dupx (_Jv_word
*sp
, int n
, int x
)
100 // first "slide" n+x elements n to the right
102 for (int i
= 0; i
< n
+x
; i
++)
104 sp
[(top
-i
)] = sp
[(top
-i
)-n
];
107 // next, copy the n top elements, n+x down
108 for (int i
= 0; i
< n
; i
++)
110 sp
[top
-(n
+x
)-i
] = sp
[top
-i
];
114 // Used to convert from floating types to integral types.
115 template<typename TO
, typename FROM
>
117 convert (FROM val
, TO min
, TO max
)
120 if (val
>= (FROM
) max
)
122 else if (val
<= (FROM
) min
)
131 #define PUSHA(V) (sp++)->o = (V)
132 #define PUSHI(V) (sp++)->i = (V)
133 #define PUSHF(V) (sp++)->f = (V)
134 #if SIZEOF_VOID_P == 8
135 # define PUSHL(V) (sp->l = (V), sp += 2)
136 # define PUSHD(V) (sp->d = (V), sp += 2)
138 # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
139 (sp++)->ia[0] = w2.ia[0]; \
140 (sp++)->ia[0] = w2.ia[1]; } while (0)
141 # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
142 (sp++)->ia[0] = w2.ia[0]; \
143 (sp++)->ia[0] = w2.ia[1]; } while (0)
146 #define POPA() ((--sp)->o)
147 #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
148 #define POPF() ((jfloat) (--sp)->f)
149 #if SIZEOF_VOID_P == 8
150 # define POPL() (sp -= 2, (jlong) sp->l)
151 # define POPD() (sp -= 2, (jdouble) sp->d)
153 # define POPL() ({ _Jv_word2 w2; \
154 w2.ia[1] = (--sp)->ia[0]; \
155 w2.ia[0] = (--sp)->ia[0]; w2.l; })
156 # define POPD() ({ _Jv_word2 w2; \
157 w2.ia[1] = (--sp)->ia[0]; \
158 w2.ia[0] = (--sp)->ia[0]; w2.d; })
161 #define LOADA(I) (sp++)->o = locals[I].o
162 #define LOADI(I) (sp++)->i = locals[I].i
163 #define LOADF(I) (sp++)->f = locals[I].f
164 #if SIZEOF_VOID_P == 8
165 # define LOADL(I) (sp->l = locals[I].l, sp += 2)
166 # define LOADD(I) (sp->d = locals[I].d, sp += 2)
168 # define LOADL(I) do { jint __idx = (I); \
169 (sp++)->ia[0] = locals[__idx].ia[0]; \
170 (sp++)->ia[0] = locals[__idx+1].ia[0]; \
172 # define LOADD(I) LOADL(I)
177 DEBUG_LOCALS_INSN (I, 'o'); \
178 locals[I].o = (--sp)->o; \
182 DEBUG_LOCALS_INSN (I, 'i'); \
183 locals[I].i = (--sp)->i; \
187 DEBUG_LOCALS_INSN (I, 'f'); \
188 locals[I].f = (--sp)->f; \
190 #if SIZEOF_VOID_P == 8
193 DEBUG_LOCALS_INSN (I, 'l'); \
194 (sp -= 2, locals[I].l = sp->l); \
198 DEBUG_LOCALS_INSN (I, 'd'); \
199 (sp -= 2, locals[I].d = sp->d); \
205 DEBUG_LOCALS_INSN (I, 'l'); \
207 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
208 locals[__idx].ia[0] = (--sp)->ia[0]; \
212 DEBUG_LOCALS_INSN(I, 'd'); \
214 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
215 locals[__idx].ia[0] = (--sp)->ia[0]; \
219 #define PEEKI(I) (locals+(I))->i
220 #define PEEKA(I) (locals+(I))->o
223 DEBUG_LOCALS_INSN(I,'i'); \
224 ((locals+(I))->i = (V))
227 #define BINOPI(OP) { \
228 jint value2 = POPI(); \
229 jint value1 = POPI(); \
230 PUSHI(value1 OP value2); \
233 #define BINOPF(OP) { \
234 jfloat value2 = POPF(); \
235 jfloat value1 = POPF(); \
236 PUSHF(value1 OP value2); \
239 #define BINOPL(OP) { \
240 jlong value2 = POPL(); \
241 jlong value1 = POPL(); \
242 PUSHL(value1 OP value2); \
245 #define BINOPD(OP) { \
246 jdouble value2 = POPD(); \
247 jdouble value1 = POPD(); \
248 PUSHD(value1 OP value2); \
252 get1s (unsigned char* loc
)
254 return *(signed char*)loc
;
258 get1u (unsigned char* loc
)
264 get2s(unsigned char* loc
)
266 return (((jint
)*(signed char*)loc
) << 8) | ((jint
)*(loc
+1));
270 get2u (unsigned char* loc
)
272 return (((jint
)(*loc
)) << 8) | ((jint
)*(loc
+1));
276 get4 (unsigned char* loc
)
278 return (((jint
)(loc
[0])) << 24)
279 | (((jint
)(loc
[1])) << 16)
280 | (((jint
)(loc
[2])) << 8)
281 | (((jint
)(loc
[3])) << 0);
284 #define SAVE_PC() frame_desc.pc = pc
286 // We used to define this conditionally, depending on HANDLE_SEGV.
287 // However, that runs into a problem if a chunk in low memory is
288 // mapped and we try to look at a field near the end of a large
289 // object. See PR 26858 for details. It is, most likely, relatively
290 // inexpensive to simply do this check always.
291 #define NULLCHECK(X) \
292 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
294 // Note that we can still conditionally define NULLARRAYCHECK, since
295 // we know that all uses of an array will first reference the length
296 // field, which is first -- and thus will trigger a SEGV.
298 #define NULLARRAYCHECK(X) SAVE_PC()
300 #define NULLARRAYCHECK(X) \
304 if ((X) == NULL) { throw_null_pointer_exception (); } \
308 #define ARRAYBOUNDSCHECK(array, index) \
311 if (((unsigned) index) >= (unsigned) (array->length)) \
312 _Jv_ThrowBadArrayIndex (index); \
316 _Jv_InterpMethod::run_normal (ffi_cif
*,
321 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
322 run (ret
, args
, _this
);
326 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
331 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
332 run_debug (ret
, args
, _this
);
336 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
341 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
343 jobject rcv
= (jobject
) args
[0].ptr
;
344 JvSynchronize
mutex (rcv
);
346 run (ret
, args
, _this
);
350 _Jv_InterpMethod::run_synch_object_debug (ffi_cif
*,
355 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
357 jobject rcv
= (jobject
) args
[0].ptr
;
358 JvSynchronize
mutex (rcv
);
360 run_debug (ret
, args
, _this
);
364 _Jv_InterpMethod::run_class (ffi_cif
*,
369 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
370 _Jv_InitClass (_this
->defining_class
);
371 run (ret
, args
, _this
);
375 _Jv_InterpMethod::run_class_debug (ffi_cif
*,
380 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
381 _Jv_InitClass (_this
->defining_class
);
382 run_debug (ret
, args
, _this
);
386 _Jv_InterpMethod::run_synch_class (ffi_cif
*,
391 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
393 jclass sync
= _this
->defining_class
;
394 _Jv_InitClass (sync
);
395 JvSynchronize
mutex (sync
);
397 run (ret
, args
, _this
);
401 _Jv_InterpMethod::run_synch_class_debug (ffi_cif
*,
406 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
408 jclass sync
= _this
->defining_class
;
409 _Jv_InitClass (sync
);
410 JvSynchronize
mutex (sync
);
412 run_debug (ret
, args
, _this
);
415 #ifdef DIRECT_THREADED
416 // "Compile" a method by turning it from bytecode to direct-threaded
419 _Jv_InterpMethod::compile (const void * const *insn_targets
)
421 insn_slot
*insns
= NULL
;
423 unsigned char *codestart
= bytecode ();
424 unsigned char *end
= codestart
+ code_length
;
425 _Jv_word
*pool_data
= defining_class
->constants
.data
;
427 #define SET_ONE(Field, Value) \
433 insns[next++].Field = Value; \
437 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
438 #define SET_INT(Value) SET_ONE (int_val, Value)
439 #define SET_DATUM(Value) SET_ONE (datum, Value)
441 // Map from bytecode PC to slot in INSNS.
442 int *pc_mapping
= (int *) __builtin_alloca (sizeof (int) * code_length
);
443 for (int i
= 0; i
< code_length
; ++i
)
446 for (int i
= 0; i
< 2; ++i
)
448 jboolean first_pass
= i
== 0;
452 insns
= (insn_slot
*) _Jv_AllocBytes (sizeof (insn_slot
) * next
);
453 number_insn_slots
= next
;
457 unsigned char *pc
= codestart
;
460 int base_pc_val
= pc
- codestart
;
462 pc_mapping
[base_pc_val
] = next
;
464 java_opcode opcode
= (java_opcode
) *pc
++;
466 if (opcode
== op_nop
)
468 SET_INSN (insn_targets
[opcode
]);
609 case op_monitorenter
:
619 // No argument, nothing else to do.
623 SET_INT (get1s (pc
));
629 int index
= get1u (pc
);
631 // For an unresolved class we want to delay resolution
633 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
636 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
640 SET_DATUM (pool_data
[index
].o
);
656 SET_INT (get1u (pc
));
661 SET_INT (get1u (pc
));
662 SET_INT (get1s (pc
+ 1));
668 int index
= get2u (pc
);
670 // For an unresolved class we want to delay resolution
672 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
675 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
679 SET_DATUM (pool_data
[index
].o
);
685 int index
= get2u (pc
);
687 SET_DATUM (&pool_data
[index
]);
692 SET_INT (get2s (pc
));
704 case op_invokespecial
:
705 case op_invokestatic
:
706 case op_invokevirtual
:
707 SET_INT (get2u (pc
));
711 case op_multianewarray
:
712 SET_INT (get2u (pc
));
713 SET_INT (get1u (pc
+ 2));
736 int offset
= get2s (pc
);
739 int new_pc
= base_pc_val
+ offset
;
741 bool orig_was_goto
= opcode
== op_goto
;
743 // Thread jumps. We limit the loop count; this lets
744 // us avoid infinite loops if the bytecode contains
745 // such. `10' is arbitrary.
747 while (codestart
[new_pc
] == op_goto
&& count
-- > 0)
748 new_pc
+= get2s (&codestart
[new_pc
+ 1]);
750 // If the jump takes us to a `return' instruction and
751 // the original branch was an unconditional goto, then
752 // we hoist the return.
753 opcode
= (java_opcode
) codestart
[new_pc
];
755 && (opcode
== op_ireturn
|| opcode
== op_lreturn
756 || opcode
== op_freturn
|| opcode
== op_dreturn
757 || opcode
== op_areturn
|| opcode
== op_return
))
760 SET_INSN (insn_targets
[opcode
]);
763 SET_DATUM (&insns
[pc_mapping
[new_pc
]]);
769 while ((pc
- codestart
) % 4 != 0)
772 jint def
= get4 (pc
);
773 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
779 int high
= get4 (pc
);
783 for (int i
= low
; i
<= high
; ++i
)
785 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ get4 (pc
)]]);
791 case op_lookupswitch
:
793 while ((pc
- codestart
) % 4 != 0)
796 jint def
= get4 (pc
);
797 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
800 jint npairs
= get4 (pc
);
806 jint match
= get4 (pc
);
807 jint offset
= get4 (pc
+ 4);
809 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
815 case op_invokeinterface
:
817 jint index
= get2u (pc
);
819 // We ignore the next two bytes.
827 opcode
= (java_opcode
) get1u (pc
);
829 jint val
= get2u (pc
);
832 // We implement narrow and wide instructions using the
833 // same code in the interpreter. So we rewrite the
834 // instruction slot here.
836 insns
[next
- 1].insn
= (void *) insn_targets
[opcode
];
839 if (opcode
== op_iinc
)
841 SET_INT (get2s (pc
));
850 jint offset
= get4 (pc
);
852 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
856 // Some "can't happen" cases that we include for
857 // error-checking purposes.
875 case op_getstatic_2s
:
876 case op_getstatic_2u
:
888 // Now update exceptions.
889 _Jv_InterpException
*exc
= exceptions ();
890 for (int i
= 0; i
< exc_count
; ++i
)
892 exc
[i
].start_pc
.p
= &insns
[pc_mapping
[exc
[i
].start_pc
.i
]];
893 exc
[i
].end_pc
.p
= &insns
[pc_mapping
[exc
[i
].end_pc
.i
]];
894 exc
[i
].handler_pc
.p
= &insns
[pc_mapping
[exc
[i
].handler_pc
.i
]];
895 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
896 // during compilation.
898 = (_Jv_Linker::resolve_pool_entry (defining_class
,
899 exc
[i
].handler_type
.i
)).clazz
;
900 exc
[i
].handler_type
.p
= handler
;
903 // Translate entries in the LineNumberTable from bytecode PC's to direct
904 // threaded interpreter instruction values.
905 for (int i
= 0; i
< line_table_len
; i
++)
907 int byte_pc
= line_table
[i
].bytecode_pc
;
908 // It isn't worth throwing an exception if this table is
909 // corrupted, but at the same time we don't want a crash.
910 if (byte_pc
< 0 || byte_pc
>= code_length
)
912 line_table
[i
].pc
= &insns
[pc_mapping
[byte_pc
]];
917 if (breakpoint_insn
== NULL
)
919 bp_insn_slot
.insn
= const_cast<void *> (insn_targets
[op_breakpoint
]);
920 breakpoint_insn
= &bp_insn_slot
;
923 #endif /* DIRECT_THREADED */
925 /* Run the given method.
926 When args is NULL, don't run anything -- just compile it. */
928 _Jv_InterpMethod::run (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
931 #undef DEBUG_LOCALS_INSN
932 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
934 #include "interpret-run.cc"
938 _Jv_InterpMethod::run_debug (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
941 #undef DEBUG_LOCALS_INSN
942 #define DEBUG_LOCALS_INSN(s, t) do {} while(0)
944 #include "interpret-run.cc"
948 throw_internal_error (const char *msg
)
950 throw new java::lang::InternalError (JvNewStringLatin1 (msg
));
954 throw_incompatible_class_change_error (jstring msg
)
956 throw new java::lang::IncompatibleClassChangeError (msg
);
960 throw_null_pointer_exception ()
962 throw new java::lang::NullPointerException
;
965 /* Look up source code line number for given bytecode (or direct threaded
968 _Jv_InterpMethod::get_source_line(pc_t mpc
)
970 int line
= line_table_len
> 0 ? line_table
[0].line
: -1;
971 for (int i
= 1; i
< line_table_len
; i
++)
972 if (line_table
[i
].pc
> mpc
)
975 line
= line_table
[i
].line
;
980 /** Do static initialization for fields with a constant initializer */
982 _Jv_InitField (jobject obj
, jclass klass
, int index
)
984 using namespace java::lang::reflect
;
986 if (obj
!= 0 && klass
== 0)
987 klass
= obj
->getClass ();
989 if (!_Jv_IsInterpretedClass (klass
))
992 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*)klass
->aux_info
;
994 _Jv_Field
* field
= (&klass
->fields
[0]) + index
;
996 if (index
> klass
->field_count
)
997 throw_internal_error ("field out of range");
999 int init
= iclass
->field_initializers
[index
];
1003 _Jv_Constants
*pool
= &klass
->constants
;
1004 int tag
= pool
->tags
[init
];
1006 if (! field
->isResolved ())
1007 throw_internal_error ("initializing unresolved field");
1009 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
1010 throw_internal_error ("initializing non-static field with no object");
1014 if ((field
->flags
& Modifier::STATIC
) != 0)
1015 addr
= (void*) field
->u
.addr
;
1017 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
1021 case JV_CONSTANT_String
:
1024 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
1025 pool
->data
[init
].string
= str
;
1026 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
1030 case JV_CONSTANT_ResolvedString
:
1031 if (! (field
->type
== &java::lang::String::class$
1032 || field
->type
== &java::lang::Class::class$
))
1033 throw_class_format_error ("string initialiser to non-string field");
1035 *(jstring
*)addr
= pool
->data
[init
].string
;
1038 case JV_CONSTANT_Integer
:
1040 int value
= pool
->data
[init
].i
;
1042 if (field
->type
== JvPrimClass (boolean
))
1043 *(jboolean
*)addr
= (jboolean
)value
;
1045 else if (field
->type
== JvPrimClass (byte
))
1046 *(jbyte
*)addr
= (jbyte
)value
;
1048 else if (field
->type
== JvPrimClass (char))
1049 *(jchar
*)addr
= (jchar
)value
;
1051 else if (field
->type
== JvPrimClass (short))
1052 *(jshort
*)addr
= (jshort
)value
;
1054 else if (field
->type
== JvPrimClass (int))
1055 *(jint
*)addr
= (jint
)value
;
1058 throw_class_format_error ("erroneous field initializer");
1062 case JV_CONSTANT_Long
:
1063 if (field
->type
!= JvPrimClass (long))
1064 throw_class_format_error ("erroneous field initializer");
1066 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
1069 case JV_CONSTANT_Float
:
1070 if (field
->type
!= JvPrimClass (float))
1071 throw_class_format_error ("erroneous field initializer");
1073 *(jfloat
*)addr
= pool
->data
[init
].f
;
1076 case JV_CONSTANT_Double
:
1077 if (field
->type
!= JvPrimClass (double))
1078 throw_class_format_error ("erroneous field initializer");
1080 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
1084 throw_class_format_error ("erroneous field initializer");
1088 inline static unsigned char*
1089 skip_one_type (unsigned char* ptr
)
1100 do { ch
= *ptr
++; } while (ch
!= ';');
1107 get_ffi_type_from_signature (unsigned char* ptr
)
1113 return &ffi_type_pointer
;
1117 // On some platforms a bool is a byte, on others an int.
1118 if (sizeof (jboolean
) == sizeof (jbyte
))
1119 return &ffi_type_sint8
;
1122 JvAssert (sizeof (jbyte
) == sizeof (jint
));
1123 return &ffi_type_sint32
;
1128 return &ffi_type_sint8
;
1132 return &ffi_type_uint16
;
1136 return &ffi_type_sint16
;
1140 return &ffi_type_sint32
;
1144 return &ffi_type_sint64
;
1148 return &ffi_type_float
;
1152 return &ffi_type_double
;
1156 return &ffi_type_void
;
1160 throw_internal_error ("unknown type in signature");
1163 /* this function yields the number of actual arguments, that is, if the
1164 * function is non-static, then one is added to the number of elements
1165 * found in the signature */
1168 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
1171 unsigned char *ptr
= (unsigned char*) signature
->chars();
1172 int arg_count
= staticp
? 0 : 1;
1174 /* first, count number of arguments */
1182 ptr
= skip_one_type (ptr
);
1189 /* This beast will build a cif, given the signature. Memory for
1190 * the cif itself and for the argument types must be allocated by the
1195 _Jv_init_cif (_Jv_Utf8Const
* signature
,
1199 ffi_type
**arg_types
,
1202 unsigned char *ptr
= (unsigned char*) signature
->chars();
1204 int arg_index
= 0; // arg number
1205 int item_count
= 0; // stack-item count
1210 arg_types
[arg_index
++] = &ffi_type_pointer
;
1220 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1222 if (*ptr
== 'J' || *ptr
== 'D')
1227 ptr
= skip_one_type (ptr
);
1232 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1234 ptr
= skip_one_type (ptr
);
1235 if (ptr
!= (unsigned char*)signature
->chars() + signature
->len())
1236 throw_internal_error ("did not find end of signature");
1238 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1239 arg_count
, rtype
, arg_types
) != FFI_OK
)
1240 throw_internal_error ("ffi_prep_cif failed");
1242 if (rtype_p
!= NULL
)
1248 #if FFI_NATIVE_RAW_API
1249 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1250 # define FFI_RAW_SIZE ffi_raw_size
1252 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1253 # define FFI_RAW_SIZE ffi_java_raw_size
1256 /* we put this one here, and not in interpret.cc because it
1257 * calls the utility routines _Jv_count_arguments
1258 * which are static to this module. The following struct defines the
1259 * layout we use for the stubs, it's only used in the ncode method. */
1262 ffi_raw_closure closure
;
1264 ffi_type
*arg_types
[0];
1267 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
1270 _Jv_InterpMethod::ncode ()
1272 using namespace java::lang::reflect
;
1274 if (self
->ncode
!= 0)
1277 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1278 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1280 ncode_closure
*closure
=
1281 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
1282 + arg_count
* sizeof (ffi_type
*));
1284 _Jv_init_cif (self
->signature
,
1288 &closure
->arg_types
[0],
1291 ffi_closure_fun fun
;
1293 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1295 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1297 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1302 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1304 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1309 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1311 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1319 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1321 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1326 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1328 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1332 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1337 self
->ncode
= (void*)closure
;
1341 /* Find the index of the given insn in the array of insn slots
1342 for this method. Returns -1 if not found. */
1344 _Jv_InterpMethod::insn_index (pc_t pc
)
1347 #ifdef DIRECT_THREADED
1348 jlong right
= number_insn_slots
;
1349 pc_t insns
= prepared
;
1351 jlong right
= code_length
;
1352 pc_t insns
= bytecode ();
1357 jlong mid
= (left
+ right
) / 2;
1358 if (&insns
[mid
] == pc
)
1361 if (pc
< &insns
[mid
])
1370 // Method to check if an exception is caught at some location in a method
1371 // (meth). Returns true if this method (meth) contains a catch block for the
1372 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1373 // to the location of the beginning of the catch block.
1375 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1376 java::lang::Throwable
*ex
)
1378 #ifdef DIRECT_THREADED
1379 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1381 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1383 _Jv_InterpException
*exc
= meth
->exceptions ();
1384 jclass exc_class
= ex
->getClass ();
1386 for (int i
= 0; i
< meth
->exc_count
; i
++)
1388 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1389 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1391 #ifdef DIRECT_THREADED
1392 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1394 jclass handler
= NULL
;
1395 if (exc
[i
].handler_type
.i
!= 0)
1397 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1399 #endif /* DIRECT_THREADED */
1400 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1402 #ifdef DIRECT_THREADED
1403 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1405 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1406 #endif /* DIRECT_THREADED */
1416 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1417 jintArray
& line_numbers
,
1418 jlongArray
& code_indices
)
1420 #ifdef DIRECT_THREADED
1421 /* For the DIRECT_THREADED case, if the method has not yet been
1422 * compiled, the linetable will change to insn slots instead of
1423 * bytecode PCs. It is probably easiest, in this case, to simply
1424 * compile the method and guarantee that we are using insn
1427 _Jv_CompileMethod (this);
1429 if (line_table_len
> 0)
1432 end
= number_insn_slots
;
1433 line_numbers
= JvNewIntArray (line_table_len
);
1434 code_indices
= JvNewLongArray (line_table_len
);
1436 jint
* lines
= elements (line_numbers
);
1437 jlong
* indices
= elements (code_indices
);
1438 for (int i
= 0; i
< line_table_len
; ++i
)
1440 lines
[i
] = line_table
[i
].line
;
1441 indices
[i
] = insn_index (line_table
[i
].pc
);
1444 #else // !DIRECT_THREADED
1445 if (line_table_len
> 0)
1449 line_numbers
= JvNewIntArray (line_table_len
);
1450 code_indices
= JvNewLongArray (line_table_len
);
1452 jint
* lines
= elements (line_numbers
);
1453 jlong
* indices
= elements (code_indices
);
1454 for (int i
= 0; i
< line_table_len
; ++i
)
1456 lines
[i
] = line_table
[i
].line
;
1457 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1460 #endif // !DIRECT_THREADED
1464 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1465 char **generic_sig
, jlong
*startloc
,
1466 jint
*length
, jint
*slot
,
1469 if (local_var_table
== NULL
)
1471 if (table_slot
>= local_var_table_len
)
1475 *name
= local_var_table
[table_slot
].name
;
1476 *sig
= local_var_table
[table_slot
].descriptor
;
1477 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1479 *startloc
= static_cast<jlong
>
1480 (local_var_table
[table_slot
].bytecode_start_pc
);
1481 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1482 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1484 return local_var_table_len
- table_slot
-1;
1488 _Jv_InterpMethod::install_break (jlong index
)
1490 return set_insn (index
, breakpoint_insn
);
1494 _Jv_InterpMethod::get_insn (jlong index
)
1498 #ifdef DIRECT_THREADED
1499 if (index
>= number_insn_slots
|| index
< 0)
1503 #else // !DIRECT_THREADED
1504 if (index
>= code_length
|| index
< 0)
1507 code
= reinterpret_cast<pc_t
> (bytecode ());
1508 #endif // !DIRECT_THREADED
1510 return &code
[index
];
1514 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1516 #ifdef DIRECT_THREADED
1517 if (index
>= number_insn_slots
|| index
< 0)
1520 pc_t code
= prepared
;
1521 code
[index
].insn
= insn
->insn
;
1522 #else // !DIRECT_THREADED
1523 if (index
>= code_length
|| index
< 0)
1526 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1527 code
[index
] = *insn
;
1528 #endif // !DIRECT_THREADED
1530 return &code
[index
];
1534 _Jv_JNIMethod::ncode ()
1536 using namespace java::lang::reflect
;
1538 if (self
->ncode
!= 0)
1541 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1542 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1544 ncode_closure
*closure
=
1545 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
1546 + arg_count
* sizeof (ffi_type
*));
1549 _Jv_init_cif (self
->signature
,
1553 &closure
->arg_types
[0],
1556 ffi_closure_fun fun
;
1558 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1560 // Initialize the argument types and CIF that represent the actual
1561 // underlying JNI function.
1563 if ((self
->accflags
& Modifier::STATIC
))
1565 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1566 * sizeof (ffi_type
*));
1568 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1569 if ((self
->accflags
& Modifier::STATIC
))
1570 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1571 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1572 arg_count
* sizeof (ffi_type
*));
1574 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1575 extra_args
+ arg_count
, rtype
,
1576 jni_arg_types
) != FFI_OK
)
1577 throw_internal_error ("ffi_prep_cif failed for JNI function");
1579 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1581 // FIXME: for now we assume that all native methods for
1582 // interpreted code use JNI.
1583 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1585 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1590 self
->ncode
= (void *) closure
;
1595 throw_class_format_error (jstring msg
)
1598 ? new java::lang::ClassFormatError (msg
)
1599 : new java::lang::ClassFormatError
);
1603 throw_class_format_error (const char *msg
)
1605 throw_class_format_error (JvNewStringLatin1 (msg
));
1611 _Jv_InterpreterEngine::do_verify (jclass klass
)
1613 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1614 for (int i
= 0; i
< klass
->method_count
; i
++)
1616 using namespace java::lang::reflect
;
1617 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1618 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1619 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1621 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1622 _Jv_VerifyMethod (im
);
1628 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1630 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1631 for (int i
= 0; i
< klass
->method_count
; i
++)
1633 // Just skip abstract methods. This is particularly important
1634 // because we don't resize the interpreted_methods array when
1635 // miranda methods are added to it.
1636 if ((klass
->methods
[i
].accflags
1637 & java::lang::reflect::Modifier::ABSTRACT
)
1641 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1643 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1646 // You might think we could use a virtual `ncode' method in
1647 // the _Jv_MethodBase and unify the native and non-native
1648 // cases. Well, we can't, because we don't allocate these
1649 // objects using `new', and thus they don't get a vtable.
1650 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1651 klass
->methods
[i
].ncode
= jnim
->ncode ();
1653 else if (imeth
!= 0) // it could be abstract
1655 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1656 klass
->methods
[i
].ncode
= im
->ncode ();
1662 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1666 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1668 // Splitting the allocations here lets us scan reference fields and
1669 // avoid scanning non-reference fields. How reference fields are
1670 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1671 // means that this memory will be scanned conservatively (same
1672 // difference, since we know all the contents here are pointers).
1673 // Then we put pointers into this memory into the 'fields'
1674 // structure. Most of these are interior pointers, which is ok (but
1675 // even so the pointer to the first reference field will be used and
1676 // that is not an interior pointer). The 'fields' array is also
1677 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1678 // be scanned. A pointer to this array is held by Class and thus
1679 // seen by the collector.
1680 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1681 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1683 for (int i
= 0; i
< klass
->field_count
; i
++)
1685 _Jv_Field
*field
= &klass
->fields
[i
];
1687 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1690 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1691 field
->u
.addr
= base
+ field
->u
.boffset
;
1693 if (iclass
->field_initializers
[i
] != 0)
1695 _Jv_Linker::resolve_field (field
, klass
->loader
);
1696 _Jv_InitField (0, klass
, i
);
1700 // Now we don't need the field_initializers anymore, so let the
1701 // collector get rid of it.
1702 iclass
->field_initializers
= 0;
1705 _Jv_ResolvedMethod
*
1706 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1709 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1711 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1712 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1713 + arg_count
*sizeof (ffi_type
*));
1715 result
->stack_item_count
1716 = _Jv_init_cif (method
->signature
,
1720 &result
->arg_types
[0],
1723 result
->method
= method
;
1724 result
->klass
= klass
;
1730 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1732 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1733 for (int i
= 0; i
< klass
->method_count
; i
++)
1735 // Just skip abstract methods. This is particularly important
1736 // because we don't resize the interpreted_methods array when
1737 // miranda methods are added to it.
1738 if ((klass
->methods
[i
].accflags
1739 & java::lang::reflect::Modifier::ABSTRACT
)
1742 // Miranda method additions mean that the `methods' array moves.
1743 // We cache a pointer into this array, so we have to update.
1744 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1748 #ifdef DIRECT_THREADED
1750 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1752 if (method
->prepared
== NULL
)
1755 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1757 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1760 #endif // DIRECT_THREADED
1762 #endif // INTERPRETER