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 DEBUG_LOCALS_INSN (I + 1, 'x'); \
195 (sp -= 2, locals[I].l = sp->l); \
199 DEBUG_LOCALS_INSN (I, 'd'); \
200 DEBUG_LOCALS_INSN (I + 1, 'x'); \
201 (sp -= 2, locals[I].d = sp->d); \
207 DEBUG_LOCALS_INSN (I, 'l'); \
208 DEBUG_LOCALS_INSN (I + 1, 'x'); \
210 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
211 locals[__idx].ia[0] = (--sp)->ia[0]; \
215 DEBUG_LOCALS_INSN (I, 'd'); \
216 DEBUG_LOCALS_INSN (I + 1, 'x'); \
218 locals[__idx+1].ia[0] = (--sp)->ia[0]; \
219 locals[__idx].ia[0] = (--sp)->ia[0]; \
223 #define PEEKI(I) (locals+(I))->i
224 #define PEEKA(I) (locals+(I))->o
227 DEBUG_LOCALS_INSN(I,'i'); \
228 ((locals+(I))->i = (V))
231 #define BINOPI(OP) { \
232 jint value2 = POPI(); \
233 jint value1 = POPI(); \
234 PUSHI(value1 OP value2); \
237 #define BINOPF(OP) { \
238 jfloat value2 = POPF(); \
239 jfloat value1 = POPF(); \
240 PUSHF(value1 OP value2); \
243 #define BINOPL(OP) { \
244 jlong value2 = POPL(); \
245 jlong value1 = POPL(); \
246 PUSHL(value1 OP value2); \
249 #define BINOPD(OP) { \
250 jdouble value2 = POPD(); \
251 jdouble value1 = POPD(); \
252 PUSHD(value1 OP value2); \
256 get1s (unsigned char* loc
)
258 return *(signed char*)loc
;
262 get1u (unsigned char* loc
)
268 get2s(unsigned char* loc
)
270 return (((jint
)*(signed char*)loc
) << 8) | ((jint
)*(loc
+1));
274 get2u (unsigned char* loc
)
276 return (((jint
)(*loc
)) << 8) | ((jint
)*(loc
+1));
280 get4 (unsigned char* loc
)
282 return (((jint
)(loc
[0])) << 24)
283 | (((jint
)(loc
[1])) << 16)
284 | (((jint
)(loc
[2])) << 8)
285 | (((jint
)(loc
[3])) << 0);
288 #define SAVE_PC() frame_desc.pc = pc
290 // We used to define this conditionally, depending on HANDLE_SEGV.
291 // However, that runs into a problem if a chunk in low memory is
292 // mapped and we try to look at a field near the end of a large
293 // object. See PR 26858 for details. It is, most likely, relatively
294 // inexpensive to simply do this check always.
295 #define NULLCHECK(X) \
296 do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
298 // Note that we can still conditionally define NULLARRAYCHECK, since
299 // we know that all uses of an array will first reference the length
300 // field, which is first -- and thus will trigger a SEGV.
302 #define NULLARRAYCHECK(X) SAVE_PC()
304 #define NULLARRAYCHECK(X) \
308 if ((X) == NULL) { throw_null_pointer_exception (); } \
312 #define ARRAYBOUNDSCHECK(array, index) \
315 if (((unsigned) index) >= (unsigned) (array->length)) \
316 _Jv_ThrowBadArrayIndex (index); \
320 _Jv_InterpMethod::run_normal (ffi_cif
*,
325 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
326 run (ret
, args
, _this
);
330 _Jv_InterpMethod::run_normal_debug (ffi_cif
*,
335 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
336 run_debug (ret
, args
, _this
);
340 _Jv_InterpMethod::run_synch_object (ffi_cif
*,
345 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
347 jobject rcv
= (jobject
) args
[0].ptr
;
348 JvSynchronize
mutex (rcv
);
350 run (ret
, args
, _this
);
354 _Jv_InterpMethod::run_synch_object_debug (ffi_cif
*,
359 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
361 jobject rcv
= (jobject
) args
[0].ptr
;
362 JvSynchronize
mutex (rcv
);
364 run_debug (ret
, args
, _this
);
368 _Jv_InterpMethod::run_class (ffi_cif
*,
373 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
374 _Jv_InitClass (_this
->defining_class
);
375 run (ret
, args
, _this
);
379 _Jv_InterpMethod::run_class_debug (ffi_cif
*,
384 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
385 _Jv_InitClass (_this
->defining_class
);
386 run_debug (ret
, args
, _this
);
390 _Jv_InterpMethod::run_synch_class (ffi_cif
*,
395 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
397 jclass sync
= _this
->defining_class
;
398 _Jv_InitClass (sync
);
399 JvSynchronize
mutex (sync
);
401 run (ret
, args
, _this
);
405 _Jv_InterpMethod::run_synch_class_debug (ffi_cif
*,
410 _Jv_InterpMethod
*_this
= (_Jv_InterpMethod
*) __this
;
412 jclass sync
= _this
->defining_class
;
413 _Jv_InitClass (sync
);
414 JvSynchronize
mutex (sync
);
416 run_debug (ret
, args
, _this
);
419 #ifdef DIRECT_THREADED
420 // "Compile" a method by turning it from bytecode to direct-threaded
423 _Jv_InterpMethod::compile (const void * const *insn_targets
)
425 insn_slot
*insns
= NULL
;
427 unsigned char *codestart
= bytecode ();
428 unsigned char *end
= codestart
+ code_length
;
429 _Jv_word
*pool_data
= defining_class
->constants
.data
;
431 #define SET_ONE(Field, Value) \
437 insns[next++].Field = Value; \
441 #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
442 #define SET_INT(Value) SET_ONE (int_val, Value)
443 #define SET_DATUM(Value) SET_ONE (datum, Value)
445 // Map from bytecode PC to slot in INSNS.
446 int *pc_mapping
= (int *) __builtin_alloca (sizeof (int) * code_length
);
447 for (int i
= 0; i
< code_length
; ++i
)
450 for (int i
= 0; i
< 2; ++i
)
452 jboolean first_pass
= i
== 0;
456 insns
= (insn_slot
*) _Jv_AllocBytes (sizeof (insn_slot
) * next
);
457 number_insn_slots
= next
;
461 unsigned char *pc
= codestart
;
464 int base_pc_val
= pc
- codestart
;
466 pc_mapping
[base_pc_val
] = next
;
468 java_opcode opcode
= (java_opcode
) *pc
++;
470 if (opcode
== op_nop
)
472 SET_INSN (insn_targets
[opcode
]);
613 case op_monitorenter
:
623 // No argument, nothing else to do.
627 SET_INT (get1s (pc
));
633 int index
= get1u (pc
);
635 // For an unresolved class we want to delay resolution
637 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
640 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
644 SET_DATUM (pool_data
[index
].o
);
660 SET_INT (get1u (pc
));
665 SET_INT (get1u (pc
));
666 SET_INT (get1s (pc
+ 1));
672 int index
= get2u (pc
);
674 // For an unresolved class we want to delay resolution
676 if (defining_class
->constants
.tags
[index
] == JV_CONSTANT_Class
)
679 SET_INSN (insn_targets
[int (op_jsr_w
) + 1]);
683 SET_DATUM (pool_data
[index
].o
);
689 int index
= get2u (pc
);
691 SET_DATUM (&pool_data
[index
]);
696 SET_INT (get2s (pc
));
708 case op_invokespecial
:
709 case op_invokestatic
:
710 case op_invokevirtual
:
711 SET_INT (get2u (pc
));
715 case op_multianewarray
:
716 SET_INT (get2u (pc
));
717 SET_INT (get1u (pc
+ 2));
740 int offset
= get2s (pc
);
743 int new_pc
= base_pc_val
+ offset
;
745 bool orig_was_goto
= opcode
== op_goto
;
747 // Thread jumps. We limit the loop count; this lets
748 // us avoid infinite loops if the bytecode contains
749 // such. `10' is arbitrary.
751 while (codestart
[new_pc
] == op_goto
&& count
-- > 0)
752 new_pc
+= get2s (&codestart
[new_pc
+ 1]);
754 // If the jump takes us to a `return' instruction and
755 // the original branch was an unconditional goto, then
756 // we hoist the return.
757 opcode
= (java_opcode
) codestart
[new_pc
];
759 && (opcode
== op_ireturn
|| opcode
== op_lreturn
760 || opcode
== op_freturn
|| opcode
== op_dreturn
761 || opcode
== op_areturn
|| opcode
== op_return
))
764 SET_INSN (insn_targets
[opcode
]);
767 SET_DATUM (&insns
[pc_mapping
[new_pc
]]);
773 while ((pc
- codestart
) % 4 != 0)
776 jint def
= get4 (pc
);
777 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
783 int high
= get4 (pc
);
787 for (int i
= low
; i
<= high
; ++i
)
789 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ get4 (pc
)]]);
795 case op_lookupswitch
:
797 while ((pc
- codestart
) % 4 != 0)
800 jint def
= get4 (pc
);
801 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ def
]]);
804 jint npairs
= get4 (pc
);
810 jint match
= get4 (pc
);
811 jint offset
= get4 (pc
+ 4);
813 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
819 case op_invokeinterface
:
821 jint index
= get2u (pc
);
823 // We ignore the next two bytes.
831 opcode
= (java_opcode
) get1u (pc
);
833 jint val
= get2u (pc
);
836 // We implement narrow and wide instructions using the
837 // same code in the interpreter. So we rewrite the
838 // instruction slot here.
840 insns
[next
- 1].insn
= (void *) insn_targets
[opcode
];
843 if (opcode
== op_iinc
)
845 SET_INT (get2s (pc
));
854 jint offset
= get4 (pc
);
856 SET_DATUM (&insns
[pc_mapping
[base_pc_val
+ offset
]]);
860 // Some "can't happen" cases that we include for
861 // error-checking purposes.
879 case op_getstatic_2s
:
880 case op_getstatic_2u
:
892 // Now update exceptions.
893 _Jv_InterpException
*exc
= exceptions ();
894 for (int i
= 0; i
< exc_count
; ++i
)
896 exc
[i
].start_pc
.p
= &insns
[pc_mapping
[exc
[i
].start_pc
.i
]];
897 exc
[i
].end_pc
.p
= &insns
[pc_mapping
[exc
[i
].end_pc
.i
]];
898 exc
[i
].handler_pc
.p
= &insns
[pc_mapping
[exc
[i
].handler_pc
.i
]];
899 // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
900 // during compilation.
902 = (_Jv_Linker::resolve_pool_entry (defining_class
,
903 exc
[i
].handler_type
.i
)).clazz
;
904 exc
[i
].handler_type
.p
= handler
;
907 // Translate entries in the LineNumberTable from bytecode PC's to direct
908 // threaded interpreter instruction values.
909 for (int i
= 0; i
< line_table_len
; i
++)
911 int byte_pc
= line_table
[i
].bytecode_pc
;
912 // It isn't worth throwing an exception if this table is
913 // corrupted, but at the same time we don't want a crash.
914 if (byte_pc
< 0 || byte_pc
>= code_length
)
916 line_table
[i
].pc
= &insns
[pc_mapping
[byte_pc
]];
921 if (breakpoint_insn
== NULL
)
923 bp_insn_slot
.insn
= const_cast<void *> (insn_targets
[op_breakpoint
]);
924 breakpoint_insn
= &bp_insn_slot
;
927 #endif /* DIRECT_THREADED */
929 /* Run the given method.
930 When args is NULL, don't run anything -- just compile it. */
932 _Jv_InterpMethod::run (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
935 #undef DEBUG_LOCALS_INSN
936 #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
938 #include "interpret-run.cc"
942 _Jv_InterpMethod::run_debug (void *retp
, ffi_raw
*args
, _Jv_InterpMethod
*meth
)
945 #undef DEBUG_LOCALS_INSN
946 #define DEBUG_LOCALS_INSN(s, t) \
949 frame_desc.locals_type[s] = t; \
953 #include "interpret-run.cc"
957 throw_internal_error (const char *msg
)
959 throw new java::lang::InternalError (JvNewStringLatin1 (msg
));
963 throw_incompatible_class_change_error (jstring msg
)
965 throw new java::lang::IncompatibleClassChangeError (msg
);
969 throw_null_pointer_exception ()
971 throw new java::lang::NullPointerException
;
974 /* Look up source code line number for given bytecode (or direct threaded
977 _Jv_InterpMethod::get_source_line(pc_t mpc
)
979 int line
= line_table_len
> 0 ? line_table
[0].line
: -1;
980 for (int i
= 1; i
< line_table_len
; i
++)
981 if (line_table
[i
].pc
> mpc
)
984 line
= line_table
[i
].line
;
989 /** Do static initialization for fields with a constant initializer */
991 _Jv_InitField (jobject obj
, jclass klass
, int index
)
993 using namespace java::lang::reflect
;
995 if (obj
!= 0 && klass
== 0)
996 klass
= obj
->getClass ();
998 if (!_Jv_IsInterpretedClass (klass
))
1001 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*)klass
->aux_info
;
1003 _Jv_Field
* field
= (&klass
->fields
[0]) + index
;
1005 if (index
> klass
->field_count
)
1006 throw_internal_error ("field out of range");
1008 int init
= iclass
->field_initializers
[index
];
1012 _Jv_Constants
*pool
= &klass
->constants
;
1013 int tag
= pool
->tags
[init
];
1015 if (! field
->isResolved ())
1016 throw_internal_error ("initializing unresolved field");
1018 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
1019 throw_internal_error ("initializing non-static field with no object");
1023 if ((field
->flags
& Modifier::STATIC
) != 0)
1024 addr
= (void*) field
->u
.addr
;
1026 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
1030 case JV_CONSTANT_String
:
1033 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
1034 pool
->data
[init
].string
= str
;
1035 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
1039 case JV_CONSTANT_ResolvedString
:
1040 if (! (field
->type
== &java::lang::String::class$
1041 || field
->type
== &java::lang::Class::class$
))
1042 throw_class_format_error ("string initialiser to non-string field");
1044 *(jstring
*)addr
= pool
->data
[init
].string
;
1047 case JV_CONSTANT_Integer
:
1049 int value
= pool
->data
[init
].i
;
1051 if (field
->type
== JvPrimClass (boolean
))
1052 *(jboolean
*)addr
= (jboolean
)value
;
1054 else if (field
->type
== JvPrimClass (byte
))
1055 *(jbyte
*)addr
= (jbyte
)value
;
1057 else if (field
->type
== JvPrimClass (char))
1058 *(jchar
*)addr
= (jchar
)value
;
1060 else if (field
->type
== JvPrimClass (short))
1061 *(jshort
*)addr
= (jshort
)value
;
1063 else if (field
->type
== JvPrimClass (int))
1064 *(jint
*)addr
= (jint
)value
;
1067 throw_class_format_error ("erroneous field initializer");
1071 case JV_CONSTANT_Long
:
1072 if (field
->type
!= JvPrimClass (long))
1073 throw_class_format_error ("erroneous field initializer");
1075 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
1078 case JV_CONSTANT_Float
:
1079 if (field
->type
!= JvPrimClass (float))
1080 throw_class_format_error ("erroneous field initializer");
1082 *(jfloat
*)addr
= pool
->data
[init
].f
;
1085 case JV_CONSTANT_Double
:
1086 if (field
->type
!= JvPrimClass (double))
1087 throw_class_format_error ("erroneous field initializer");
1089 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
1093 throw_class_format_error ("erroneous field initializer");
1097 inline static unsigned char*
1098 skip_one_type (unsigned char* ptr
)
1109 do { ch
= *ptr
++; } while (ch
!= ';');
1116 get_ffi_type_from_signature (unsigned char* ptr
)
1122 return &ffi_type_pointer
;
1126 // On some platforms a bool is a byte, on others an int.
1127 if (sizeof (jboolean
) == sizeof (jbyte
))
1128 return &ffi_type_sint8
;
1131 JvAssert (sizeof (jbyte
) == sizeof (jint
));
1132 return &ffi_type_sint32
;
1137 return &ffi_type_sint8
;
1141 return &ffi_type_uint16
;
1145 return &ffi_type_sint16
;
1149 return &ffi_type_sint32
;
1153 return &ffi_type_sint64
;
1157 return &ffi_type_float
;
1161 return &ffi_type_double
;
1165 return &ffi_type_void
;
1169 throw_internal_error ("unknown type in signature");
1172 /* this function yields the number of actual arguments, that is, if the
1173 * function is non-static, then one is added to the number of elements
1174 * found in the signature */
1177 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
1180 unsigned char *ptr
= (unsigned char*) signature
->chars();
1181 int arg_count
= staticp
? 0 : 1;
1183 /* first, count number of arguments */
1191 ptr
= skip_one_type (ptr
);
1198 /* This beast will build a cif, given the signature. Memory for
1199 * the cif itself and for the argument types must be allocated by the
1204 _Jv_init_cif (_Jv_Utf8Const
* signature
,
1208 ffi_type
**arg_types
,
1211 unsigned char *ptr
= (unsigned char*) signature
->chars();
1213 int arg_index
= 0; // arg number
1214 int item_count
= 0; // stack-item count
1219 arg_types
[arg_index
++] = &ffi_type_pointer
;
1229 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1231 if (*ptr
== 'J' || *ptr
== 'D')
1236 ptr
= skip_one_type (ptr
);
1241 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1243 ptr
= skip_one_type (ptr
);
1244 if (ptr
!= (unsigned char*)signature
->chars() + signature
->len())
1245 throw_internal_error ("did not find end of signature");
1247 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1248 arg_count
, rtype
, arg_types
) != FFI_OK
)
1249 throw_internal_error ("ffi_prep_cif failed");
1251 if (rtype_p
!= NULL
)
1257 #if FFI_NATIVE_RAW_API
1258 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
1259 # define FFI_RAW_SIZE ffi_raw_size
1261 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
1262 # define FFI_RAW_SIZE ffi_java_raw_size
1265 /* we put this one here, and not in interpret.cc because it
1266 * calls the utility routines _Jv_count_arguments
1267 * which are static to this module. The following struct defines the
1268 * layout we use for the stubs, it's only used in the ncode method. */
1271 ffi_raw_closure closure
;
1272 _Jv_ClosureList list
;
1274 ffi_type
*arg_types
[0];
1277 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
1280 _Jv_InterpMethod::ncode (jclass klass
)
1282 using namespace java::lang::reflect
;
1284 if (self
->ncode
!= 0)
1287 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1288 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1291 ncode_closure
*closure
=
1292 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1293 + arg_count
* sizeof (ffi_type
*),
1295 closure
->list
.registerClosure (klass
, closure
);
1297 _Jv_init_cif (self
->signature
,
1301 &closure
->arg_types
[0],
1304 ffi_closure_fun fun
;
1306 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1308 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1310 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1315 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class_debug
;
1317 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1322 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object_debug
;
1324 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1332 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class_debug
;
1334 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
1339 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal_debug
;
1341 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1345 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1356 /* Find the index of the given insn in the array of insn slots
1357 for this method. Returns -1 if not found. */
1359 _Jv_InterpMethod::insn_index (pc_t pc
)
1362 #ifdef DIRECT_THREADED
1363 jlong right
= number_insn_slots
;
1364 pc_t insns
= prepared
;
1366 jlong right
= code_length
;
1367 pc_t insns
= bytecode ();
1372 jlong mid
= (left
+ right
) / 2;
1373 if (&insns
[mid
] == pc
)
1376 if (pc
< &insns
[mid
])
1385 // Method to check if an exception is caught at some location in a method
1386 // (meth). Returns true if this method (meth) contains a catch block for the
1387 // exception (ex). False otherwise. If there is a catch block, it sets the pc
1388 // to the location of the beginning of the catch block.
1390 _Jv_InterpMethod::check_handler (pc_t
*pc
, _Jv_InterpMethod
*meth
,
1391 java::lang::Throwable
*ex
)
1393 #ifdef DIRECT_THREADED
1394 void *logical_pc
= (void *) ((insn_slot
*) (*pc
) - 1);
1396 int logical_pc
= (*pc
) - 1 - meth
->bytecode ();
1398 _Jv_InterpException
*exc
= meth
->exceptions ();
1399 jclass exc_class
= ex
->getClass ();
1401 for (int i
= 0; i
< meth
->exc_count
; i
++)
1403 if (PCVAL (exc
[i
].start_pc
) <= logical_pc
1404 && logical_pc
< PCVAL (exc
[i
].end_pc
))
1406 #ifdef DIRECT_THREADED
1407 jclass handler
= (jclass
) exc
[i
].handler_type
.p
;
1409 jclass handler
= NULL
;
1410 if (exc
[i
].handler_type
.i
!= 0)
1412 = (_Jv_Linker::resolve_pool_entry (meth
->defining_class
,
1414 #endif /* DIRECT_THREADED */
1415 if (handler
== NULL
|| handler
->isAssignableFrom (exc_class
))
1417 #ifdef DIRECT_THREADED
1418 (*pc
) = (insn_slot
*) exc
[i
].handler_pc
.p
;
1420 (*pc
) = meth
->bytecode () + exc
[i
].handler_pc
.i
;
1421 #endif /* DIRECT_THREADED */
1431 _Jv_InterpMethod::get_line_table (jlong
& start
, jlong
& end
,
1432 jintArray
& line_numbers
,
1433 jlongArray
& code_indices
)
1435 #ifdef DIRECT_THREADED
1436 /* For the DIRECT_THREADED case, if the method has not yet been
1437 * compiled, the linetable will change to insn slots instead of
1438 * bytecode PCs. It is probably easiest, in this case, to simply
1439 * compile the method and guarantee that we are using insn
1442 _Jv_CompileMethod (this);
1444 if (line_table_len
> 0)
1447 end
= number_insn_slots
;
1448 line_numbers
= JvNewIntArray (line_table_len
);
1449 code_indices
= JvNewLongArray (line_table_len
);
1451 jint
* lines
= elements (line_numbers
);
1452 jlong
* indices
= elements (code_indices
);
1453 for (int i
= 0; i
< line_table_len
; ++i
)
1455 lines
[i
] = line_table
[i
].line
;
1456 indices
[i
] = insn_index (line_table
[i
].pc
);
1459 #else // !DIRECT_THREADED
1460 if (line_table_len
> 0)
1464 line_numbers
= JvNewIntArray (line_table_len
);
1465 code_indices
= JvNewLongArray (line_table_len
);
1467 jint
* lines
= elements (line_numbers
);
1468 jlong
* indices
= elements (code_indices
);
1469 for (int i
= 0; i
< line_table_len
; ++i
)
1471 lines
[i
] = line_table
[i
].line
;
1472 indices
[i
] = (jlong
) line_table
[i
].bytecode_pc
;
1475 #endif // !DIRECT_THREADED
1479 _Jv_InterpMethod::get_local_var_table (char **name
, char **sig
,
1480 char **generic_sig
, jlong
*startloc
,
1481 jint
*length
, jint
*slot
,
1484 if (local_var_table
== NULL
)
1486 if (table_slot
>= local_var_table_len
)
1490 *name
= local_var_table
[table_slot
].name
;
1491 *sig
= local_var_table
[table_slot
].descriptor
;
1492 *generic_sig
= local_var_table
[table_slot
].descriptor
;
1494 *startloc
= static_cast<jlong
>
1495 (local_var_table
[table_slot
].bytecode_start_pc
);
1496 *length
= static_cast<jint
> (local_var_table
[table_slot
].length
);
1497 *slot
= static_cast<jint
> (local_var_table
[table_slot
].slot
);
1499 return local_var_table_len
- table_slot
-1;
1503 _Jv_InterpMethod::install_break (jlong index
)
1505 return set_insn (index
, breakpoint_insn
);
1509 _Jv_InterpMethod::get_insn (jlong index
)
1513 #ifdef DIRECT_THREADED
1514 if (index
>= number_insn_slots
|| index
< 0)
1518 #else // !DIRECT_THREADED
1519 if (index
>= code_length
|| index
< 0)
1522 code
= reinterpret_cast<pc_t
> (bytecode ());
1523 #endif // !DIRECT_THREADED
1525 return &code
[index
];
1529 _Jv_InterpMethod::set_insn (jlong index
, pc_t insn
)
1531 #ifdef DIRECT_THREADED
1532 if (index
>= number_insn_slots
|| index
< 0)
1535 pc_t code
= prepared
;
1536 code
[index
].insn
= insn
->insn
;
1537 #else // !DIRECT_THREADED
1538 if (index
>= code_length
|| index
< 0)
1541 pc_t code
= reinterpret_cast<pc_t
> (bytecode ());
1542 code
[index
] = *insn
;
1543 #endif // !DIRECT_THREADED
1545 return &code
[index
];
1549 _Jv_JNIMethod::ncode (jclass klass
)
1551 using namespace java::lang::reflect
;
1553 if (self
->ncode
!= 0)
1556 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1557 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1560 ncode_closure
*closure
=
1561 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
1562 + arg_count
* sizeof (ffi_type
*),
1564 closure
->list
.registerClosure (klass
, closure
);
1567 _Jv_init_cif (self
->signature
,
1571 &closure
->arg_types
[0],
1574 ffi_closure_fun fun
;
1576 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1578 // Initialize the argument types and CIF that represent the actual
1579 // underlying JNI function.
1581 if ((self
->accflags
& Modifier::STATIC
))
1583 jni_arg_types
= (ffi_type
**) _Jv_AllocBytes ((extra_args
+ arg_count
)
1584 * sizeof (ffi_type
*));
1586 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1587 if ((self
->accflags
& Modifier::STATIC
))
1588 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1589 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1590 arg_count
* sizeof (ffi_type
*));
1592 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1593 extra_args
+ arg_count
, rtype
,
1594 jni_arg_types
) != FFI_OK
)
1595 throw_internal_error ("ffi_prep_cif failed for JNI function");
1597 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1599 // FIXME: for now we assume that all native methods for
1600 // interpreted code use JNI.
1601 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1603 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1614 throw_class_format_error (jstring msg
)
1617 ? new java::lang::ClassFormatError (msg
)
1618 : new java::lang::ClassFormatError
);
1622 throw_class_format_error (const char *msg
)
1624 throw_class_format_error (JvNewStringLatin1 (msg
));
1630 _Jv_InterpreterEngine::do_verify (jclass klass
)
1632 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1633 for (int i
= 0; i
< klass
->method_count
; i
++)
1635 using namespace java::lang::reflect
;
1636 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1637 _Jv_ushort accflags
= klass
->methods
[i
].accflags
;
1638 if ((accflags
& (Modifier::NATIVE
| Modifier::ABSTRACT
)) == 0)
1640 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1641 _Jv_VerifyMethod (im
);
1647 _Jv_InterpreterEngine::do_create_ncode (jclass klass
)
1649 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1650 for (int i
= 0; i
< klass
->method_count
; i
++)
1652 // Just skip abstract methods. This is particularly important
1653 // because we don't resize the interpreted_methods array when
1654 // miranda methods are added to it.
1655 if ((klass
->methods
[i
].accflags
1656 & java::lang::reflect::Modifier::ABSTRACT
)
1660 _Jv_MethodBase
*imeth
= iclass
->interpreted_methods
[i
];
1662 if ((klass
->methods
[i
].accflags
& java::lang::reflect::Modifier::NATIVE
)
1665 // You might think we could use a virtual `ncode' method in
1666 // the _Jv_MethodBase and unify the native and non-native
1667 // cases. Well, we can't, because we don't allocate these
1668 // objects using `new', and thus they don't get a vtable.
1669 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
1670 klass
->methods
[i
].ncode
= jnim
->ncode (klass
);
1672 else if (imeth
!= 0) // it could be abstract
1674 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
1675 klass
->methods
[i
].ncode
= im
->ncode (klass
);
1681 _Jv_InterpreterEngine::do_get_closure_list (jclass klass
)
1683 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1685 if (!iclass
->closures
)
1686 iclass
->closures
= _Jv_ClosureListFinalizer ();
1688 return iclass
->closures
;
1692 _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass
,
1696 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1698 // Splitting the allocations here lets us scan reference fields and
1699 // avoid scanning non-reference fields. How reference fields are
1700 // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
1701 // means that this memory will be scanned conservatively (same
1702 // difference, since we know all the contents here are pointers).
1703 // Then we put pointers into this memory into the 'fields'
1704 // structure. Most of these are interior pointers, which is ok (but
1705 // even so the pointer to the first reference field will be used and
1706 // that is not an interior pointer). The 'fields' array is also
1707 // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
1708 // be scanned. A pointer to this array is held by Class and thus
1709 // seen by the collector.
1710 char *reference_fields
= (char *) _Jv_AllocRawObj (pointer_size
);
1711 char *non_reference_fields
= (char *) _Jv_AllocBytes (other_size
);
1713 for (int i
= 0; i
< klass
->field_count
; i
++)
1715 _Jv_Field
*field
= &klass
->fields
[i
];
1717 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
) == 0)
1720 char *base
= field
->isRef() ? reference_fields
: non_reference_fields
;
1721 field
->u
.addr
= base
+ field
->u
.boffset
;
1723 if (iclass
->field_initializers
[i
] != 0)
1725 _Jv_Linker::resolve_field (field
, klass
->loader
);
1726 _Jv_InitField (0, klass
, i
);
1730 // Now we don't need the field_initializers anymore, so let the
1731 // collector get rid of it.
1732 iclass
->field_initializers
= 0;
1735 _Jv_ResolvedMethod
*
1736 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method
*method
, jclass klass
,
1739 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1741 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1742 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1743 + arg_count
*sizeof (ffi_type
*));
1745 result
->stack_item_count
1746 = _Jv_init_cif (method
->signature
,
1750 &result
->arg_types
[0],
1753 result
->method
= method
;
1754 result
->klass
= klass
;
1760 _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass
)
1762 _Jv_InterpClass
*iclass
= (_Jv_InterpClass
*) klass
->aux_info
;
1763 for (int i
= 0; i
< klass
->method_count
; i
++)
1765 // Just skip abstract methods. This is particularly important
1766 // because we don't resize the interpreted_methods array when
1767 // miranda methods are added to it.
1768 if ((klass
->methods
[i
].accflags
1769 & java::lang::reflect::Modifier::ABSTRACT
)
1772 // Miranda method additions mean that the `methods' array moves.
1773 // We cache a pointer into this array, so we have to update.
1774 iclass
->interpreted_methods
[i
]->self
= &klass
->methods
[i
];
1778 #ifdef DIRECT_THREADED
1780 _Jv_CompileMethod (_Jv_InterpMethod
* method
)
1782 if (method
->prepared
== NULL
)
1785 _Jv_InterpMethod::run_debug (NULL
, NULL
, method
);
1787 _Jv_InterpMethod::run (NULL
, NULL
, method
);
1790 #endif // DIRECT_THREADED
1792 #endif // INTERPRETER