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
);
194 /* Get the line table for this method.
195 * start is the lowest index in the method
196 * end is the highest index in the method
197 * line_numbers is an array to hold the list of source line numbers
198 * code_indices is an array to hold the corresponding list of code indices
200 void get_line_table (jlong
& start
, jlong
& end
, jintArray
& line_numbers
,
201 jlongArray
& code_indices
);
203 #ifdef DIRECT_THREADED
204 friend void _Jv_CompileMethod (_Jv_InterpMethod
*);
207 friend class _Jv_ClassReader
;
208 friend class _Jv_BytecodeVerifier
;
209 friend class _Jv_StackTrace
;
210 friend class _Jv_InterpreterEngine
;
212 #ifdef JV_MARKOBJ_DECL
213 friend JV_MARKOBJ_DECL
;
217 class _Jv_InterpClass
219 _Jv_MethodBase
**interpreted_methods
;
220 _Jv_ushort
*field_initializers
;
221 jstring source_file_name
;
223 friend class _Jv_ClassReader
;
224 friend class _Jv_InterpMethod
;
225 friend class _Jv_StackTrace
;
226 friend class _Jv_InterpreterEngine
;
228 friend void _Jv_InitField (jobject
, jclass
, int);
229 #ifdef JV_MARKOBJ_DECL
230 friend JV_MARKOBJ_DECL
;
233 friend _Jv_MethodBase
** _Jv_GetFirstMethod (_Jv_InterpClass
*klass
);
236 extern inline _Jv_MethodBase
**
237 _Jv_GetFirstMethod (_Jv_InterpClass
*klass
)
239 return klass
->interpreted_methods
;
242 struct _Jv_ResolvedMethod
244 jint stack_item_count
;
248 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
249 // to mark the resolved method to hold on to the cif. Some memory could be
250 // saved by keeping a cache of cif's, since many will be the same.
252 ffi_type
* arg_types
[0];
255 class _Jv_JNIMethod
: public _Jv_MethodBase
257 // The underlying function. If NULL we have to look for the
261 // This is the CIF used by the JNI function.
264 // These are the argument types used by the JNI function.
265 ffi_type
**jni_arg_types
;
267 // This function is used when making a JNI call from the interpreter.
268 static void call (ffi_cif
*, void *, ffi_raw
*, void *);
272 friend class _Jv_ClassReader
;
273 friend class _Jv_InterpreterEngine
;
275 #ifdef JV_MARKOBJ_DECL
276 friend JV_MARKOBJ_DECL
;
280 // FIXME: this is ugly.
281 void set_function (void *f
)
287 // The interpreted call stack, represented by a linked list of frames.
288 struct _Jv_InterpFrame
290 _Jv_InterpMethod
*self
;
291 java::lang::Thread
*thread
;
292 _Jv_InterpFrame
*next
;
295 _Jv_InterpFrame (_Jv_InterpMethod
*s
, java::lang::Thread
*thr
)
299 next
= (_Jv_InterpFrame
*) thr
->interp_frame
;
300 thr
->interp_frame
= (gnu::gcj::RawData
*) this;
306 thread
->interp_frame
= (gnu::gcj::RawData
*) next
;
310 #endif /* INTERPRETER */
312 #endif /* __JAVA_INTERP_H__ */