1 // java-interp.h - Header file for the bytecode interpreter. -*- c++ -*-
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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
37 struct _Jv_ResolvedMethod
;
39 void _Jv_InitInterpreter ();
40 void _Jv_DefineClass (jclass
, jbyteArray
, jint
, jint
,
41 java::security::ProtectionDomain
*,
44 void _Jv_InitField (jobject
, jclass
, int);
45 void * _Jv_AllocMethodInvocation (jsize size
);
46 int _Jv_count_arguments (_Jv_Utf8Const
*signature
,
47 jboolean staticp
= true);
48 void _Jv_VerifyMethod (_Jv_InterpMethod
*method
);
49 void _Jv_CompileMethod (_Jv_InterpMethod
* method
);
51 /* the interpreter is written in C++, primarily because it makes it easy for
52 * the entire thing to be "friend" with class Class. */
54 class _Jv_InterpClass
;
55 class _Jv_InterpMethod
;
57 // Before a method is "compiled" we store values as the bytecode PC,
58 // an int. Afterwards we store them as pointers into the prepared
66 class _Jv_InterpException
68 _Jv_InterpPC start_pc
;
70 _Jv_InterpPC handler_pc
;
71 _Jv_InterpPC handler_type
;
73 friend class _Jv_ClassReader
;
74 friend class _Jv_InterpMethod
;
75 friend class _Jv_BytecodeVerifier
;
78 // Base class for method representations. Subclasses are interpreted
83 // The class which defined this method.
84 jclass defining_class
;
86 // The method description.
89 // Size of raw arguments.
90 _Jv_ushort args_raw_size
;
92 friend class _Jv_InterpreterEngine
;
95 _Jv_Method
*get_method ()
101 // The type of the PC depends on whether we're doing direct threading
102 // or a more ordinary bytecode interpreter.
103 #ifdef DIRECT_THREADED
104 // Slot in the "compiled" form of the bytecode.
109 // An integer value used by an instruction.
111 // A pointer value used by an instruction.
115 typedef insn_slot
*pc_t
;
117 typedef unsigned char *pc_t
;
121 // This structure holds the bytecode pc and corresponding source code
122 // line number. An array (plus length field) of this structure is put
123 // in each _Jv_InterpMethod and used to resolve the (internal) program
124 // counter of the interpreted method to an actual java source file
126 struct _Jv_LineTableEntry
136 class _Jv_InterpMethod
: public _Jv_MethodBase
138 _Jv_ushort max_stack
;
139 _Jv_ushort max_locals
;
142 _Jv_ushort exc_count
;
145 // Length of the line_table - when this is zero then line_table is NULL.
147 _Jv_LineTableEntry
*line_table
;
150 int number_insn_slots
;
152 unsigned char* bytecode ()
155 ((unsigned char*)this)
156 + ROUND((sizeof (_Jv_InterpMethod
)
157 + exc_count
*sizeof (_Jv_InterpException
)), 4);
160 _Jv_InterpException
* exceptions ()
162 return (_Jv_InterpException
*) (this+1);
165 static size_t size (int exc_count
, int code_length
)
168 ROUND ((sizeof (_Jv_InterpMethod
)
169 + (exc_count
* sizeof (_Jv_InterpException
))), 4)
173 // return the method's invocation pointer (a stub).
175 void compile (const void * const *);
177 static void run_normal (ffi_cif
*, void*, ffi_raw
*, void*);
178 static void run_synch_object (ffi_cif
*, void*, ffi_raw
*, void*);
179 static void run_class (ffi_cif
*, void*, ffi_raw
*, void*);
180 static void run_synch_class (ffi_cif
*, void*, ffi_raw
*, void*);
182 static void run (void*, ffi_raw
*, _Jv_InterpMethod
*);
184 // Returns source file line number for given PC value, or -1 if line
185 // number info is unavailable.
186 int get_source_line(pc_t mpc
);
188 #ifdef DIRECT_THREADED
189 // Convenience function for indexing bytecode PC/insn slots in
190 // line tables for JDWP
191 jlong
insn_index (pc_t pc
);
195 static void dump_object(jobject o
);
197 /* Get the line table for this method.
198 * start is the lowest index in the method
199 * end is the highest index in the method
200 * line_numbers is an array to hold the list of source line numbers
201 * code_indices is an array to hold the corresponding list of code indices
203 void get_line_table (jlong
& start
, jlong
& end
, jintArray
& line_numbers
,
204 jlongArray
& code_indices
);
206 #ifdef DIRECT_THREADED
207 friend void _Jv_CompileMethod (_Jv_InterpMethod
*);
210 friend class _Jv_ClassReader
;
211 friend class _Jv_BytecodeVerifier
;
212 friend class _Jv_StackTrace
;
213 friend class _Jv_InterpreterEngine
;
215 #ifdef JV_MARKOBJ_DECL
216 friend JV_MARKOBJ_DECL
;
220 class _Jv_InterpClass
222 _Jv_MethodBase
**interpreted_methods
;
223 _Jv_ushort
*field_initializers
;
224 jstring source_file_name
;
226 friend class _Jv_ClassReader
;
227 friend class _Jv_InterpMethod
;
228 friend class _Jv_StackTrace
;
229 friend class _Jv_InterpreterEngine
;
231 friend void _Jv_InitField (jobject
, jclass
, int);
232 #ifdef JV_MARKOBJ_DECL
233 friend JV_MARKOBJ_DECL
;
236 friend _Jv_MethodBase
** _Jv_GetFirstMethod (_Jv_InterpClass
*klass
);
239 extern inline _Jv_MethodBase
**
240 _Jv_GetFirstMethod (_Jv_InterpClass
*klass
)
242 return klass
->interpreted_methods
;
245 struct _Jv_ResolvedMethod
247 jint stack_item_count
;
252 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
253 // to mark the resolved method to hold on to the cif. Some memory could be
254 // saved by keeping a cache of cif's, since many will be the same.
256 ffi_type
* arg_types
[0];
259 class _Jv_JNIMethod
: public _Jv_MethodBase
261 // The underlying function. If NULL we have to look for the
265 // This is the CIF used by the JNI function.
268 // These are the argument types used by the JNI function.
269 ffi_type
**jni_arg_types
;
271 // This function is used when making a JNI call from the interpreter.
272 static void call (ffi_cif
*, void *, ffi_raw
*, void *);
276 friend class _Jv_ClassReader
;
277 friend class _Jv_InterpreterEngine
;
279 #ifdef JV_MARKOBJ_DECL
280 friend JV_MARKOBJ_DECL
;
284 // FIXME: this is ugly.
285 void set_function (void *f
)
291 // The interpreted call stack, represented by a linked list of frames.
292 struct _Jv_InterpFrame
294 _Jv_InterpMethod
*self
;
295 java::lang::Thread
*thread
;
296 _Jv_InterpFrame
*next
;
299 _Jv_InterpFrame (_Jv_InterpMethod
*s
, java::lang::Thread
*thr
)
303 next
= (_Jv_InterpFrame
*) thr
->interp_frame
;
304 thr
->interp_frame
= (gnu::gcj::RawData
*) this;
310 thread
->interp_frame
= (gnu::gcj::RawData
*) next
;
314 #endif /* INTERPRETER */
316 #endif /* __JAVA_INTERP_H__ */