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 *);
41 void _Jv_InitField (jobject, jclass, int);
42 void * _Jv_AllocMethodInvocation (jsize size);
43 int _Jv_count_arguments (_Jv_Utf8Const *signature,
44 jboolean staticp = true);
45 void _Jv_VerifyMethod (_Jv_InterpMethod *method);
47 /* the interpreter is written in C++, primarily because it makes it easy for
48 * the entire thing to be "friend" with class Class. */
50 class _Jv_InterpClass;
51 class _Jv_InterpMethod;
53 // Before a method is "compiled" we store values as the bytecode PC,
54 // an int. Afterwards we store them as pointers into the prepared
62 class _Jv_InterpException
64 _Jv_InterpPC start_pc;
66 _Jv_InterpPC handler_pc;
67 _Jv_InterpPC handler_type;
69 friend class _Jv_ClassReader;
70 friend class _Jv_InterpMethod;
71 friend class _Jv_BytecodeVerifier;
74 // Base class for method representations. Subclasses are interpreted
79 // The class which defined this method.
80 jclass defining_class;
82 // The method description.
85 // Size of raw arguments.
86 _Jv_ushort args_raw_size;
88 friend class _Jv_InterpreterEngine;
91 _Jv_Method *get_method ()
97 // The type of the PC depends on whether we're doing direct threading
98 // or a more ordinary bytecode interpreter.
99 #ifdef DIRECT_THREADED
100 // Slot in the "compiled" form of the bytecode.
105 // An integer value used by an instruction.
107 // A pointer value used by an instruction.
111 typedef insn_slot *pc_t;
113 typedef unsigned char *pc_t;
117 // This structure holds the bytecode pc and corresponding source code
118 // line number. An array (plus length field) of this structure is put
119 // in each _Jv_InterpMethod and used to resolve the (internal) program
120 // counter of the interpreted method to an actual java source file
122 struct _Jv_LineTableEntry
132 class _Jv_InterpMethod : public _Jv_MethodBase
134 _Jv_ushort max_stack;
135 _Jv_ushort max_locals;
138 _Jv_ushort exc_count;
140 // Length of the line_table - when this is zero then line_table is NULL.
142 _Jv_LineTableEntry *line_table;
146 unsigned char* bytecode ()
149 ((unsigned char*)this)
150 + ROUND((sizeof (_Jv_InterpMethod)
151 + exc_count*sizeof (_Jv_InterpException)), 4);
154 _Jv_InterpException * exceptions ()
156 return (_Jv_InterpException*) (this+1);
159 static size_t size (int exc_count, int code_length)
162 ROUND ((sizeof (_Jv_InterpMethod)
163 + (exc_count * sizeof (_Jv_InterpException))), 4)
167 // return the method's invocation pointer (a stub).
169 void compile (const void * const *);
171 static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
172 static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
173 static void run_class (ffi_cif*, void*, ffi_raw*, void*);
174 static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
176 static void run (void*, ffi_raw *, _Jv_InterpMethod *);
178 // Returns source file line number for given PC value, or -1 if line
179 // number info is unavailable.
180 int get_source_line(pc_t mpc);
183 static void dump_object(jobject o);
185 friend class _Jv_ClassReader;
186 friend class _Jv_BytecodeVerifier;
187 friend class _Jv_StackTrace;
188 friend class _Jv_InterpreterEngine;
190 #ifdef JV_MARKOBJ_DECL
191 friend JV_MARKOBJ_DECL;
195 class _Jv_InterpClass
197 _Jv_MethodBase **interpreted_methods;
198 _Jv_ushort *field_initializers;
199 jstring source_file_name;
201 friend class _Jv_ClassReader;
202 friend class _Jv_InterpMethod;
203 friend class _Jv_StackTrace;
204 friend class _Jv_InterpreterEngine;
206 friend void _Jv_InitField (jobject, jclass, int);
207 #ifdef JV_MARKOBJ_DECL
208 friend JV_MARKOBJ_DECL;
211 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
214 extern inline _Jv_MethodBase **
215 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
217 return klass->interpreted_methods;
220 struct _Jv_ResolvedMethod {
221 jint stack_item_count;
226 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
227 // to mark the resolved method to hold on to the cif. Some memory could be
228 // saved by keeping a cache of cif's, since many will be the same.
230 ffi_type * arg_types[0];
233 class _Jv_JNIMethod : public _Jv_MethodBase
235 // The underlying function. If NULL we have to look for the
239 // This is the CIF used by the JNI function.
242 // These are the argument types used by the JNI function.
243 ffi_type **jni_arg_types;
245 // This function is used when making a JNI call from the interpreter.
246 static void call (ffi_cif *, void *, ffi_raw *, void *);
250 friend class _Jv_ClassReader;
251 friend class _Jv_InterpreterEngine;
253 #ifdef JV_MARKOBJ_DECL
254 friend JV_MARKOBJ_DECL;
258 // FIXME: this is ugly.
259 void set_function (void *f)
265 // The interpreted call stack, represented by a linked list of frames.
266 struct _Jv_InterpFrame
268 _Jv_InterpMethod *self;
269 _Jv_InterpFrame **ptr;
270 _Jv_InterpFrame *next;
273 _Jv_InterpFrame (_Jv_InterpMethod *s, _Jv_InterpFrame **n)
288 #endif /* INTERPRETER */
290 #endif /* __JAVA_INTERP_H__ */