1 // java-interp.h - Header file for the bytecode interpreter. -*- c++ -*-
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 #ifndef __JAVA_INTERP_H__
12 #define __JAVA_INTERP_H__
15 #include <java-cpool.h>
16 #include <gnu/gcj/runtime/NameFinder.h>
22 #include <java/lang/Class.h>
23 #include <java/lang/ClassLoader.h>
24 #include <java/lang/reflect/Modifier.h>
25 #include <java/lang/Thread.h>
26 #include <gnu/gcj/RawData.h>
28 // Define this to get the direct-threaded interpreter. If undefined,
29 // we revert to a basic bytecode interpreter. The former is faster
30 // but uses more memory.
31 #define DIRECT_THREADED
35 struct _Jv_ResolvedMethod
;
37 void _Jv_InitInterpreter ();
38 void _Jv_DefineClass (jclass
, jbyteArray
, jint
, jint
,
39 java::security::ProtectionDomain
*,
42 void _Jv_InitField (jobject
, jclass
, int);
43 void * _Jv_AllocMethodInvocation (jsize size
);
44 int _Jv_count_arguments (_Jv_Utf8Const
*signature
,
45 jboolean staticp
= true);
46 void _Jv_VerifyMethod (_Jv_InterpMethod
*method
);
47 void _Jv_CompileMethod (_Jv_InterpMethod
* method
);
48 int _Jv_init_cif (_Jv_Utf8Const
* signature
,
55 /* the interpreter is written in C++, primarily because it makes it easy for
56 * the entire thing to be "friend" with class Class. */
58 class _Jv_InterpClass
;
59 class _Jv_InterpMethod
;
61 // Before a method is "compiled" we store values as the bytecode PC,
62 // an int. Afterwards we store them as pointers into the prepared
70 class _Jv_InterpException
72 _Jv_InterpPC start_pc
;
74 _Jv_InterpPC handler_pc
;
75 _Jv_InterpPC handler_type
;
77 friend class _Jv_ClassReader
;
78 friend class _Jv_InterpMethod
;
79 friend class _Jv_BytecodeVerifier
;
82 // Base class for method representations. Subclasses are interpreted
87 // The class which defined this method.
88 jclass defining_class
;
90 // The method description.
93 // Size of raw arguments.
94 _Jv_ushort args_raw_size
;
96 friend class _Jv_InterpreterEngine
;
99 _Jv_Method
*get_method ()
105 // The type of the PC depends on whether we're doing direct threading
106 // or a more ordinary bytecode interpreter.
107 #ifdef DIRECT_THREADED
108 // Slot in the "compiled" form of the bytecode.
113 // An integer value used by an instruction.
115 // A pointer value used by an instruction.
119 typedef insn_slot
*pc_t
;
121 typedef unsigned char *pc_t
;
125 // This structure holds the bytecode pc and corresponding source code
126 // line number. An array (plus length field) of this structure is put
127 // in each _Jv_InterpMethod and used to resolve the (internal) program
128 // counter of the interpreted method to an actual java source file
130 struct _Jv_LineTableEntry
140 class _Jv_InterpMethod
: public _Jv_MethodBase
142 // Breakpoint instruction
143 static pc_t breakpoint_insn
;
144 #ifdef DIRECT_THREADED
145 static insn_slot bp_insn_slot
;
147 static unsigned char bp_insn_opcode
;
150 _Jv_ushort max_stack
;
151 _Jv_ushort max_locals
;
154 _Jv_ushort exc_count
;
157 // Length of the line_table - when this is zero then line_table is NULL.
159 _Jv_LineTableEntry
*line_table
;
162 int number_insn_slots
;
164 unsigned char* bytecode ()
167 ((unsigned char*)this)
168 + ROUND((sizeof (_Jv_InterpMethod
)
169 + exc_count
*sizeof (_Jv_InterpException
)), 4);
172 _Jv_InterpException
* exceptions ()
174 return (_Jv_InterpException
*) (this+1);
177 static size_t size (int exc_count
, int code_length
)
180 ROUND ((sizeof (_Jv_InterpMethod
)
181 + (exc_count
* sizeof (_Jv_InterpException
))), 4)
185 // return the method's invocation pointer (a stub).
187 void compile (const void * const *);
189 static void run_normal (ffi_cif
*, void*, ffi_raw
*, void*);
190 static void run_synch_object (ffi_cif
*, void*, ffi_raw
*, void*);
191 static void run_class (ffi_cif
*, void*, ffi_raw
*, void*);
192 static void run_synch_class (ffi_cif
*, void*, ffi_raw
*, void*);
194 static void run_normal_debug (ffi_cif
*, void*, ffi_raw
*, void*);
195 static void run_synch_object_debug (ffi_cif
*, void*, ffi_raw
*, void*);
196 static void run_class_debug (ffi_cif
*, void*, ffi_raw
*, void*);
197 static void run_synch_class_debug (ffi_cif
*, void*, ffi_raw
*, void*);
199 static void run (void *, ffi_raw
*, _Jv_InterpMethod
*);
200 static void run_debug (void *, ffi_raw
*, _Jv_InterpMethod
*);
204 // Returns source file line number for given PC value, or -1 if line
205 // number info is unavailable.
206 int get_source_line(pc_t mpc
);
210 // Convenience function for indexing bytecode PC/insn slots in
211 // line tables for JDWP
212 jlong
insn_index (pc_t pc
);
214 /* Get the line table for this method.
215 * start is the lowest index in the method
216 * end is the highest index in the method
217 * line_numbers is an array to hold the list of source line numbers
218 * code_indices is an array to hold the corresponding list of code indices
220 void get_line_table (jlong
& start
, jlong
& end
, jintArray
& line_numbers
,
221 jlongArray
& code_indices
);
223 int get_max_locals ()
225 return static_cast<int> (max_locals
);
228 /* Installs a break instruction at the given code index. Returns
229 the pc_t of the breakpoint or NULL if index is invalid. */
230 pc_t
install_break (jlong index
);
232 // Gets the instruction at the given index
233 pc_t
get_insn (jlong index
);
235 /* Writes the given instruction at the given code index. Returns
236 the insn or NULL if index is invalid. */
237 pc_t
set_insn (jlong index
, pc_t insn
);
239 #ifdef DIRECT_THREADED
240 friend void _Jv_CompileMethod (_Jv_InterpMethod
*);
243 friend class _Jv_ClassReader
;
244 friend class _Jv_BytecodeVerifier
;
245 friend class _Jv_StackTrace
;
246 friend class _Jv_InterpreterEngine
;
248 #ifdef JV_MARKOBJ_DECL
249 friend JV_MARKOBJ_DECL
;
253 class _Jv_InterpClass
255 _Jv_MethodBase
**interpreted_methods
;
256 _Jv_ushort
*field_initializers
;
257 jstring source_file_name
;
259 friend class _Jv_ClassReader
;
260 friend class _Jv_InterpMethod
;
261 friend class _Jv_StackTrace
;
262 friend class _Jv_InterpreterEngine
;
264 friend void _Jv_InitField (jobject
, jclass
, int);
265 #ifdef JV_MARKOBJ_DECL
266 friend JV_MARKOBJ_DECL
;
269 friend _Jv_MethodBase
** _Jv_GetFirstMethod (_Jv_InterpClass
*klass
);
272 extern inline _Jv_MethodBase
**
273 _Jv_GetFirstMethod (_Jv_InterpClass
*klass
)
275 return klass
->interpreted_methods
;
278 struct _Jv_ResolvedMethod
280 jint stack_item_count
;
284 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
285 // to mark the resolved method to hold on to the cif. Some memory could be
286 // saved by keeping a cache of cif's, since many will be the same.
288 ffi_type
* arg_types
[0];
291 class _Jv_JNIMethod
: public _Jv_MethodBase
293 // The underlying function. If NULL we have to look for the
297 // This is the CIF used by the JNI function.
300 // These are the argument types used by the JNI function.
301 ffi_type
**jni_arg_types
;
303 // This function is used when making a JNI call from the interpreter.
304 static void call (ffi_cif
*, void *, ffi_raw
*, void *);
308 friend class _Jv_ClassReader
;
309 friend class _Jv_InterpreterEngine
;
311 #ifdef JV_MARKOBJ_DECL
312 friend JV_MARKOBJ_DECL
;
316 // FIXME: this is ugly.
317 void set_function (void *f
)
330 // The composite call stack as represented by a linked list of frames
334 java::lang::Thread
*thread
;
338 _Jv_MethodBase
*self
;
340 _Jv_Method
*proxyMethod
;
343 //The full list of frames, JNI and interpreted
345 _Jv_FrameType frame_type
;
347 _Jv_Frame (_Jv_MethodBase
*s
, java::lang::Thread
*thr
, _Jv_FrameType type
)
351 next
= (_Jv_Frame
*) thr
->frame
;
352 thr
->frame
= (gnu::gcj::RawData
*) this;
358 thread
->frame
= (gnu::gcj::RawData
*) next
;
365 for (f
= this; f
!= NULL
; f
= f
->next
)
372 // An interpreted frame in the call stack
373 class _Jv_InterpFrame
: public _Jv_Frame
377 // Keep the purely interpreted list around so as not to break backtraces
378 _Jv_InterpFrame
*next_interp
;
386 //Debug info for local variables.
390 // Object pointer for this frame ("this")
393 _Jv_InterpFrame (void *meth
, java::lang::Thread
*thr
, jclass proxyCls
= NULL
)
394 : _Jv_Frame (reinterpret_cast<_Jv_MethodBase
*> (meth
), thr
,
397 next_interp
= (_Jv_InterpFrame
*) thr
->interp_frame
;
398 proxyClass
= proxyCls
;
399 thr
->interp_frame
= (gnu::gcj::RawData
*) this;
405 thread
->interp_frame
= (gnu::gcj::RawData
*) next_interp
;
408 jobject
get_this_ptr ()
414 // A native frame in the call stack really just a placeholder
415 class _Jv_NativeFrame
: public _Jv_Frame
419 _Jv_NativeFrame (_Jv_JNIMethod
*s
, java::lang::Thread
*thr
)
420 : _Jv_Frame (s
, thr
, frame_native
)
425 #endif /* INTERPRETER */
427 #endif /* __JAVA_INTERP_H__ */