java-interp.h (insn_index): New declaration.
[gcc.git] / libjava / include / java-interp.h
1 // java-interp.h - Header file for the bytecode interpreter. -*- c++ -*-
2
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #ifndef __JAVA_INTERP_H__
12 #define __JAVA_INTERP_H__
13
14 #include <jvm.h>
15 #include <java-cpool.h>
16 #include <gnu/gcj/runtime/NameFinder.h>
17
18 #ifdef INTERPRETER
19
20 #pragma interface
21
22 #include <java/lang/Class.h>
23 #include <java/lang/ClassLoader.h>
24 #include <java/lang/reflect/Modifier.h>
25
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
30
31 extern "C" {
32 #include <ffi.h>
33 }
34
35 struct _Jv_ResolvedMethod;
36
37 void _Jv_InitInterpreter ();
38 void _Jv_DefineClass (jclass, jbyteArray, jint, jint,
39 java::security::ProtectionDomain *,
40 _Jv_Utf8Const **);
41
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);
48
49 /* the interpreter is written in C++, primarily because it makes it easy for
50 * the entire thing to be "friend" with class Class. */
51
52 class _Jv_InterpClass;
53 class _Jv_InterpMethod;
54
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
57 // code itself.
58 union _Jv_InterpPC
59 {
60 int i;
61 void *p;
62 };
63
64 class _Jv_InterpException
65 {
66 _Jv_InterpPC start_pc;
67 _Jv_InterpPC end_pc;
68 _Jv_InterpPC handler_pc;
69 _Jv_InterpPC handler_type;
70
71 friend class _Jv_ClassReader;
72 friend class _Jv_InterpMethod;
73 friend class _Jv_BytecodeVerifier;
74 };
75
76 // Base class for method representations. Subclasses are interpreted
77 // and JNI methods.
78 class _Jv_MethodBase
79 {
80 protected:
81 // The class which defined this method.
82 jclass defining_class;
83
84 // The method description.
85 _Jv_Method *self;
86
87 // Size of raw arguments.
88 _Jv_ushort args_raw_size;
89
90 friend class _Jv_InterpreterEngine;
91
92 public:
93 _Jv_Method *get_method ()
94 {
95 return self;
96 }
97 };
98
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.
103 union insn_slot
104 {
105 // Address of code.
106 void *insn;
107 // An integer value used by an instruction.
108 jint int_val;
109 // A pointer value used by an instruction.
110 void *datum;
111 };
112
113 typedef insn_slot *pc_t;
114 #else
115 typedef unsigned char *pc_t;
116 #endif
117
118
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
123 // line.
124 struct _Jv_LineTableEntry
125 {
126 union
127 {
128 pc_t pc;
129 int bytecode_pc;
130 };
131 int line;
132 };
133
134 class _Jv_InterpMethod : public _Jv_MethodBase
135 {
136 _Jv_ushort max_stack;
137 _Jv_ushort max_locals;
138 int code_length;
139
140 _Jv_ushort exc_count;
141 bool is_15;
142
143 // Length of the line_table - when this is zero then line_table is NULL.
144 int line_table_len;
145 _Jv_LineTableEntry *line_table;
146
147 void *prepared;
148 int number_insn_slots;
149
150 unsigned char* bytecode ()
151 {
152 return
153 ((unsigned char*)this)
154 + ROUND((sizeof (_Jv_InterpMethod)
155 + exc_count*sizeof (_Jv_InterpException)), 4);
156 }
157
158 _Jv_InterpException * exceptions ()
159 {
160 return (_Jv_InterpException*) (this+1);
161 }
162
163 static size_t size (int exc_count, int code_length)
164 {
165 return
166 ROUND ((sizeof (_Jv_InterpMethod)
167 + (exc_count * sizeof (_Jv_InterpException))), 4)
168 + code_length;
169 }
170
171 // return the method's invocation pointer (a stub).
172 void *ncode ();
173 void compile (const void * const *);
174
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*);
179
180 static void run (void*, ffi_raw *, _Jv_InterpMethod *);
181
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);
185
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);
190 #endif
191
192 public:
193 static void dump_object(jobject o);
194
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
200 */
201 void get_line_table (jlong& start, jlong& end, jintArray& line_numbers,
202 jlongArray& code_indices);
203
204 #ifdef DIRECT_THREADED
205 friend void _Jv_CompileMethod (_Jv_InterpMethod*);
206 #endif
207
208 friend class _Jv_ClassReader;
209 friend class _Jv_BytecodeVerifier;
210 friend class _Jv_StackTrace;
211 friend class _Jv_InterpreterEngine;
212
213 #ifdef JV_MARKOBJ_DECL
214 friend JV_MARKOBJ_DECL;
215 #endif
216 };
217
218 class _Jv_InterpClass
219 {
220 _Jv_MethodBase **interpreted_methods;
221 _Jv_ushort *field_initializers;
222 jstring source_file_name;
223
224 friend class _Jv_ClassReader;
225 friend class _Jv_InterpMethod;
226 friend class _Jv_StackTrace;
227 friend class _Jv_InterpreterEngine;
228
229 friend void _Jv_InitField (jobject, jclass, int);
230 #ifdef JV_MARKOBJ_DECL
231 friend JV_MARKOBJ_DECL;
232 #endif
233
234 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
235 };
236
237 extern inline _Jv_MethodBase **
238 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
239 {
240 return klass->interpreted_methods;
241 }
242
243 struct _Jv_ResolvedMethod
244 {
245 jint stack_item_count;
246 jint vtable_index;
247 jclass klass;
248 _Jv_Method* method;
249
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.
253 ffi_cif cif;
254 ffi_type * arg_types[0];
255 };
256
257 class _Jv_JNIMethod : public _Jv_MethodBase
258 {
259 // The underlying function. If NULL we have to look for the
260 // function.
261 void *function;
262
263 // This is the CIF used by the JNI function.
264 ffi_cif jni_cif;
265
266 // These are the argument types used by the JNI function.
267 ffi_type **jni_arg_types;
268
269 // This function is used when making a JNI call from the interpreter.
270 static void call (ffi_cif *, void *, ffi_raw *, void *);
271
272 void *ncode ();
273
274 friend class _Jv_ClassReader;
275 friend class _Jv_InterpreterEngine;
276
277 #ifdef JV_MARKOBJ_DECL
278 friend JV_MARKOBJ_DECL;
279 #endif
280
281 public:
282 // FIXME: this is ugly.
283 void set_function (void *f)
284 {
285 function = f;
286 }
287 };
288
289 // The interpreted call stack, represented by a linked list of frames.
290 struct _Jv_InterpFrame
291 {
292 _Jv_InterpMethod *self;
293 _Jv_InterpFrame **ptr;
294 _Jv_InterpFrame *next;
295 pc_t pc;
296
297 _Jv_InterpFrame (_Jv_InterpMethod *s, _Jv_InterpFrame **n)
298 {
299 self = s;
300 ptr = n;
301 next = *n;
302 *n = this;
303 pc = NULL;
304 }
305
306 ~_Jv_InterpFrame ()
307 {
308 *ptr = next;
309 }
310 };
311
312 #endif /* INTERPRETER */
313
314 #endif /* __JAVA_INTERP_H__ */