1 // java-interp.h - Header file for the bytecode interpreter. -*- c++ -*-
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 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>
26 // Define this to get the direct-threaded interpreter. If undefined,
27 // we revert to a basic bytecode interpreter. The former is faster
28 // but uses more memory.
29 #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
);
48 /* the interpreter is written in C++, primarily because it makes it easy for
49 * the entire thing to be "friend" with class Class. */
51 class _Jv_InterpClass
;
52 class _Jv_InterpMethod
;
54 // Before a method is "compiled" we store values as the bytecode PC,
55 // an int. Afterwards we store them as pointers into the prepared
63 class _Jv_InterpException
65 _Jv_InterpPC start_pc
;
67 _Jv_InterpPC handler_pc
;
68 _Jv_InterpPC handler_type
;
70 friend class _Jv_ClassReader
;
71 friend class _Jv_InterpMethod
;
72 friend class _Jv_BytecodeVerifier
;
75 // Base class for method representations. Subclasses are interpreted
80 // The class which defined this method.
81 jclass defining_class
;
83 // The method description.
86 // Size of raw arguments.
87 _Jv_ushort args_raw_size
;
89 friend class _Jv_InterpreterEngine
;
92 _Jv_Method
*get_method ()
98 // The type of the PC depends on whether we're doing direct threading
99 // or a more ordinary bytecode interpreter.
100 #ifdef DIRECT_THREADED
101 // Slot in the "compiled" form of the bytecode.
106 // An integer value used by an instruction.
108 // A pointer value used by an instruction.
112 typedef insn_slot
*pc_t
;
114 typedef unsigned char *pc_t
;
118 // This structure holds the bytecode pc and corresponding source code
119 // line number. An array (plus length field) of this structure is put
120 // in each _Jv_InterpMethod and used to resolve the (internal) program
121 // counter of the interpreted method to an actual java source file
123 struct _Jv_LineTableEntry
133 class _Jv_InterpMethod
: public _Jv_MethodBase
135 _Jv_ushort max_stack
;
136 _Jv_ushort max_locals
;
139 _Jv_ushort exc_count
;
142 // Length of the line_table - when this is zero then line_table is NULL.
144 _Jv_LineTableEntry
*line_table
;
148 unsigned char* bytecode ()
151 ((unsigned char*)this)
152 + ROUND((sizeof (_Jv_InterpMethod
)
153 + exc_count
*sizeof (_Jv_InterpException
)), 4);
156 _Jv_InterpException
* exceptions ()
158 return (_Jv_InterpException
*) (this+1);
161 static size_t size (int exc_count
, int code_length
)
164 ROUND ((sizeof (_Jv_InterpMethod
)
165 + (exc_count
* sizeof (_Jv_InterpException
))), 4)
169 // return the method's invocation pointer (a stub).
171 void compile (const void * const *);
173 static void run_normal (ffi_cif
*, void*, ffi_raw
*, void*);
174 static void run_synch_object (ffi_cif
*, void*, ffi_raw
*, void*);
175 static void run_class (ffi_cif
*, void*, ffi_raw
*, void*);
176 static void run_synch_class (ffi_cif
*, void*, ffi_raw
*, void*);
178 static void run (void*, ffi_raw
*, _Jv_InterpMethod
*);
180 // Returns source file line number for given PC value, or -1 if line
181 // number info is unavailable.
182 int get_source_line(pc_t mpc
);
185 static void dump_object(jobject o
);
187 friend class _Jv_ClassReader
;
188 friend class _Jv_BytecodeVerifier
;
189 friend class _Jv_StackTrace
;
190 friend class _Jv_InterpreterEngine
;
192 #ifdef JV_MARKOBJ_DECL
193 friend JV_MARKOBJ_DECL
;
197 class _Jv_InterpClass
199 _Jv_MethodBase
**interpreted_methods
;
200 _Jv_ushort
*field_initializers
;
201 jstring source_file_name
;
203 friend class _Jv_ClassReader
;
204 friend class _Jv_InterpMethod
;
205 friend class _Jv_StackTrace
;
206 friend class _Jv_InterpreterEngine
;
208 friend void _Jv_InitField (jobject
, jclass
, int);
209 #ifdef JV_MARKOBJ_DECL
210 friend JV_MARKOBJ_DECL
;
213 friend _Jv_MethodBase
** _Jv_GetFirstMethod (_Jv_InterpClass
*klass
);
216 extern inline _Jv_MethodBase
**
217 _Jv_GetFirstMethod (_Jv_InterpClass
*klass
)
219 return klass
->interpreted_methods
;
222 struct _Jv_ResolvedMethod
224 jint stack_item_count
;
229 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
230 // to mark the resolved method to hold on to the cif. Some memory could be
231 // saved by keeping a cache of cif's, since many will be the same.
233 ffi_type
* arg_types
[0];
236 class _Jv_JNIMethod
: public _Jv_MethodBase
238 // The underlying function. If NULL we have to look for the
242 // This is the CIF used by the JNI function.
245 // These are the argument types used by the JNI function.
246 ffi_type
**jni_arg_types
;
248 // This function is used when making a JNI call from the interpreter.
249 static void call (ffi_cif
*, void *, ffi_raw
*, void *);
253 friend class _Jv_ClassReader
;
254 friend class _Jv_InterpreterEngine
;
256 #ifdef JV_MARKOBJ_DECL
257 friend JV_MARKOBJ_DECL
;
261 // FIXME: this is ugly.
262 void set_function (void *f
)
268 // The interpreted call stack, represented by a linked list of frames.
269 struct _Jv_InterpFrame
271 _Jv_InterpMethod
*self
;
272 _Jv_InterpFrame
**ptr
;
273 _Jv_InterpFrame
*next
;
276 _Jv_InterpFrame (_Jv_InterpMethod
*s
, _Jv_InterpFrame
**n
)
291 #endif /* INTERPRETER */
293 #endif /* __JAVA_INTERP_H__ */