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
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
);
49 /* the interpreter is written in C++, primarily because it makes it easy for
50 * the entire thing to be "friend" with class Class. */
52 class _Jv_InterpClass
;
53 class _Jv_InterpMethod
;
55 // Before a method is "compiled" we store values as the bytecode PC,
56 // an int. Afterwards we store them as pointers into the prepared
64 class _Jv_InterpException
66 _Jv_InterpPC start_pc
;
68 _Jv_InterpPC handler_pc
;
69 _Jv_InterpPC handler_type
;
71 friend class _Jv_ClassReader
;
72 friend class _Jv_InterpMethod
;
73 friend class _Jv_BytecodeVerifier
;
76 // Base class for method representations. Subclasses are interpreted
81 // The class which defined this method.
82 jclass defining_class
;
84 // The method description.
87 // Size of raw arguments.
88 _Jv_ushort args_raw_size
;
90 friend class _Jv_InterpreterEngine
;
93 _Jv_Method
*get_method ()
99 // The type of the PC depends on whether we're doing direct threading
100 // or a more ordinary bytecode interpreter.
101 #ifdef DIRECT_THREADED
102 // Slot in the "compiled" form of the bytecode.
107 // An integer value used by an instruction.
109 // A pointer value used by an instruction.
113 typedef insn_slot
*pc_t
;
115 typedef unsigned char *pc_t
;
119 // This structure holds the bytecode pc and corresponding source code
120 // line number. An array (plus length field) of this structure is put
121 // in each _Jv_InterpMethod and used to resolve the (internal) program
122 // counter of the interpreted method to an actual java source file
124 struct _Jv_LineTableEntry
134 class _Jv_InterpMethod
: public _Jv_MethodBase
136 _Jv_ushort max_stack
;
137 _Jv_ushort max_locals
;
140 _Jv_ushort exc_count
;
143 // Length of the line_table - when this is zero then line_table is NULL.
145 _Jv_LineTableEntry
*line_table
;
148 int number_insn_slots
;
150 unsigned char* bytecode ()
153 ((unsigned char*)this)
154 + ROUND((sizeof (_Jv_InterpMethod
)
155 + exc_count
*sizeof (_Jv_InterpException
)), 4);
158 _Jv_InterpException
* exceptions ()
160 return (_Jv_InterpException
*) (this+1);
163 static size_t size (int exc_count
, int code_length
)
166 ROUND ((sizeof (_Jv_InterpMethod
)
167 + (exc_count
* sizeof (_Jv_InterpException
))), 4)
171 // return the method's invocation pointer (a stub).
173 void compile (const void * const *);
175 static void run_normal (ffi_cif
*, void*, ffi_raw
*, void*);
176 static void run_synch_object (ffi_cif
*, void*, ffi_raw
*, void*);
177 static void run_class (ffi_cif
*, void*, ffi_raw
*, void*);
178 static void run_synch_class (ffi_cif
*, void*, ffi_raw
*, void*);
180 static void run (void*, ffi_raw
*, _Jv_InterpMethod
*);
182 // Returns source file line number for given PC value, or -1 if line
183 // number info is unavailable.
184 int get_source_line(pc_t mpc
);
186 #ifdef DIRECT_THREADED
187 // Convenience function for indexing bytecode PC/insn slots in
188 // line tables for JDWP
189 jlong
insn_index (pc_t pc
);
193 static void dump_object(jobject o
);
195 /* Get the line table for this method.
196 * start is the lowest index in the method
197 * end is the highest index in the method
198 * line_numbers is an array to hold the list of source line numbers
199 * code_indices is an array to hold the corresponding list of code indices
201 void get_line_table (jlong
& start
, jlong
& end
, jintArray
& line_numbers
,
202 jlongArray
& code_indices
);
204 #ifdef DIRECT_THREADED
205 friend void _Jv_CompileMethod (_Jv_InterpMethod
*);
208 friend class _Jv_ClassReader
;
209 friend class _Jv_BytecodeVerifier
;
210 friend class _Jv_StackTrace
;
211 friend class _Jv_InterpreterEngine
;
213 #ifdef JV_MARKOBJ_DECL
214 friend JV_MARKOBJ_DECL
;
218 class _Jv_InterpClass
220 _Jv_MethodBase
**interpreted_methods
;
221 _Jv_ushort
*field_initializers
;
222 jstring source_file_name
;
224 friend class _Jv_ClassReader
;
225 friend class _Jv_InterpMethod
;
226 friend class _Jv_StackTrace
;
227 friend class _Jv_InterpreterEngine
;
229 friend void _Jv_InitField (jobject
, jclass
, int);
230 #ifdef JV_MARKOBJ_DECL
231 friend JV_MARKOBJ_DECL
;
234 friend _Jv_MethodBase
** _Jv_GetFirstMethod (_Jv_InterpClass
*klass
);
237 extern inline _Jv_MethodBase
**
238 _Jv_GetFirstMethod (_Jv_InterpClass
*klass
)
240 return klass
->interpreted_methods
;
243 struct _Jv_ResolvedMethod
245 jint stack_item_count
;
250 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
251 // to mark the resolved method to hold on to the cif. Some memory could be
252 // saved by keeping a cache of cif's, since many will be the same.
254 ffi_type
* arg_types
[0];
257 class _Jv_JNIMethod
: public _Jv_MethodBase
259 // The underlying function. If NULL we have to look for the
263 // This is the CIF used by the JNI function.
266 // These are the argument types used by the JNI function.
267 ffi_type
**jni_arg_types
;
269 // This function is used when making a JNI call from the interpreter.
270 static void call (ffi_cif
*, void *, ffi_raw
*, void *);
274 friend class _Jv_ClassReader
;
275 friend class _Jv_InterpreterEngine
;
277 #ifdef JV_MARKOBJ_DECL
278 friend JV_MARKOBJ_DECL
;
282 // FIXME: this is ugly.
283 void set_function (void *f
)
289 // The interpreted call stack, represented by a linked list of frames.
290 struct _Jv_InterpFrame
292 _Jv_InterpMethod
*self
;
293 java::lang::Thread
*thread
;
294 _Jv_InterpFrame
*next
;
297 _Jv_InterpFrame (_Jv_InterpMethod
*s
, java::lang::Thread
*thr
)
301 next
= (_Jv_InterpFrame
*) thr
->interp_frame
;
302 thr
->interp_frame
= (gnu::gcj::RawData
*) this;
308 thread
->interp_frame
= (gnu::gcj::RawData
*) next
;
312 #endif /* INTERPRETER */
314 #endif /* __JAVA_INTERP_H__ */