link.cc (ensure_class_linked): Removed #ifdef.
[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 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
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);
46
47 /* the interpreter is written in C++, primarily because it makes it easy for
48 * the entire thing to be "friend" with class Class. */
49
50 class _Jv_InterpClass;
51 class _Jv_InterpMethod;
52
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
55 // code itself.
56 union _Jv_InterpPC
57 {
58 int i;
59 void *p;
60 };
61
62 class _Jv_InterpException
63 {
64 _Jv_InterpPC start_pc;
65 _Jv_InterpPC end_pc;
66 _Jv_InterpPC handler_pc;
67 _Jv_InterpPC handler_type;
68
69 friend class _Jv_ClassReader;
70 friend class _Jv_InterpMethod;
71 friend class _Jv_BytecodeVerifier;
72 };
73
74 // Base class for method representations. Subclasses are interpreted
75 // and JNI methods.
76 class _Jv_MethodBase
77 {
78 protected:
79 // The class which defined this method.
80 jclass defining_class;
81
82 // The method description.
83 _Jv_Method *self;
84
85 // Size of raw arguments.
86 _Jv_ushort args_raw_size;
87
88 friend class _Jv_InterpreterEngine;
89
90 public:
91 _Jv_Method *get_method ()
92 {
93 return self;
94 }
95 };
96
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.
101 union insn_slot
102 {
103 // Address of code.
104 void *insn;
105 // An integer value used by an instruction.
106 jint int_val;
107 // A pointer value used by an instruction.
108 void *datum;
109 };
110
111 typedef insn_slot *pc_t;
112 #else
113 typedef unsigned char *pc_t;
114 #endif
115
116
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
121 // line.
122 struct _Jv_LineTableEntry
123 {
124 union
125 {
126 pc_t pc;
127 int bytecode_pc;
128 };
129 int line;
130 };
131
132 class _Jv_InterpMethod : public _Jv_MethodBase
133 {
134 _Jv_ushort max_stack;
135 _Jv_ushort max_locals;
136 int code_length;
137
138 _Jv_ushort exc_count;
139
140 // Length of the line_table - when this is zero then line_table is NULL.
141 int line_table_len;
142 _Jv_LineTableEntry *line_table;
143
144 void *prepared;
145
146 unsigned char* bytecode ()
147 {
148 return
149 ((unsigned char*)this)
150 + ROUND((sizeof (_Jv_InterpMethod)
151 + exc_count*sizeof (_Jv_InterpException)), 4);
152 }
153
154 _Jv_InterpException * exceptions ()
155 {
156 return (_Jv_InterpException*) (this+1);
157 }
158
159 static size_t size (int exc_count, int code_length)
160 {
161 return
162 ROUND ((sizeof (_Jv_InterpMethod)
163 + (exc_count * sizeof (_Jv_InterpException))), 4)
164 + code_length;
165 }
166
167 // return the method's invocation pointer (a stub).
168 void *ncode ();
169 void compile (const void * const *);
170
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*);
175
176 static void run (void*, ffi_raw *, _Jv_InterpMethod *);
177
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);
181
182 public:
183 static void dump_object(jobject o);
184
185 friend class _Jv_ClassReader;
186 friend class _Jv_BytecodeVerifier;
187 friend class _Jv_StackTrace;
188 friend class _Jv_InterpreterEngine;
189
190 #ifdef JV_MARKOBJ_DECL
191 friend JV_MARKOBJ_DECL;
192 #endif
193 };
194
195 class _Jv_InterpClass
196 {
197 _Jv_MethodBase **interpreted_methods;
198 _Jv_ushort *field_initializers;
199 jstring source_file_name;
200
201 friend class _Jv_ClassReader;
202 friend class _Jv_InterpMethod;
203 friend class _Jv_StackTrace;
204 friend class _Jv_InterpreterEngine;
205
206 friend void _Jv_InitField (jobject, jclass, int);
207 #ifdef JV_MARKOBJ_DECL
208 friend JV_MARKOBJ_DECL;
209 #endif
210
211 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
212 };
213
214 extern inline _Jv_MethodBase **
215 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
216 {
217 return klass->interpreted_methods;
218 }
219
220 struct _Jv_ResolvedMethod {
221 jint stack_item_count;
222 jint vtable_index;
223 jclass klass;
224 _Jv_Method* method;
225
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.
229 ffi_cif cif;
230 ffi_type * arg_types[0];
231 };
232
233 class _Jv_JNIMethod : public _Jv_MethodBase
234 {
235 // The underlying function. If NULL we have to look for the
236 // function.
237 void *function;
238
239 // This is the CIF used by the JNI function.
240 ffi_cif jni_cif;
241
242 // These are the argument types used by the JNI function.
243 ffi_type **jni_arg_types;
244
245 // This function is used when making a JNI call from the interpreter.
246 static void call (ffi_cif *, void *, ffi_raw *, void *);
247
248 void *ncode ();
249
250 friend class _Jv_ClassReader;
251 friend class _Jv_InterpreterEngine;
252
253 #ifdef JV_MARKOBJ_DECL
254 friend JV_MARKOBJ_DECL;
255 #endif
256
257 public:
258 // FIXME: this is ugly.
259 void set_function (void *f)
260 {
261 function = f;
262 }
263 };
264
265 // The interpreted call stack, represented by a linked list of frames.
266 struct _Jv_InterpFrame
267 {
268 _Jv_InterpMethod *self;
269 _Jv_InterpFrame **ptr;
270 _Jv_InterpFrame *next;
271 pc_t pc;
272
273 _Jv_InterpFrame (_Jv_InterpMethod *s, _Jv_InterpFrame **n)
274 {
275 self = s;
276 ptr = n;
277 next = *n;
278 *n = this;
279 pc = NULL;
280 }
281
282 ~_Jv_InterpFrame ()
283 {
284 *ptr = next;
285 }
286 };
287
288 #endif /* INTERPRETER */
289
290 #endif /* __JAVA_INTERP_H__ */