re PR libgcj/7587 (direct threaded interpreter not thread-safe)
[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 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 extern "C" {
27 #include <ffi.h>
28 }
29
30 extern inline jboolean
31 _Jv_IsInterpretedClass (jclass c)
32 {
33 return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0;
34 }
35
36 struct _Jv_ResolvedMethod;
37
38 void _Jv_InitInterpreter ();
39 void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
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 /* FIXME: this should really be defined in some more generic place */
48 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
49
50 /* the interpreter is written in C++, primarily because it makes it easy for
51 * the entire thing to be "friend" with class Class. */
52
53 class _Jv_InterpClass;
54 class _Jv_InterpMethod;
55
56 // Before a method is "compiled" we store values as the bytecode PC,
57 // an int. Afterwards we store them as pointers into the prepared
58 // code itself.
59 union _Jv_InterpPC
60 {
61 int i;
62 void *p;
63 };
64
65 class _Jv_InterpException
66 {
67 _Jv_InterpPC start_pc;
68 _Jv_InterpPC end_pc;
69 _Jv_InterpPC handler_pc;
70 _Jv_InterpPC handler_type;
71
72 friend class _Jv_ClassReader;
73 friend class _Jv_InterpMethod;
74 friend class _Jv_BytecodeVerifier;
75 };
76
77 // Base class for method representations. Subclasses are interpreted
78 // and JNI methods.
79 class _Jv_MethodBase
80 {
81 protected:
82 // The class which defined this method.
83 jclass defining_class;
84
85 // The method description.
86 _Jv_Method *self;
87
88 // Size of raw arguments.
89 _Jv_ushort args_raw_size;
90
91 // Chain of addresses to fill in. See _Jv_Defer_Resolution.
92 void *deferred;
93
94 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
95 friend void _Jv_PrepareClass(jclass);
96
97 public:
98 _Jv_Method *get_method ()
99 {
100 return self;
101 }
102 };
103
104 class _Jv_InterpMethod : public _Jv_MethodBase
105 {
106 _Jv_ushort max_stack;
107 _Jv_ushort max_locals;
108 int code_length;
109
110 _Jv_ushort exc_count;
111
112 void *prepared;
113
114 unsigned char* bytecode ()
115 {
116 return
117 ((unsigned char*)this)
118 + ROUND((sizeof (_Jv_InterpMethod)
119 + exc_count*sizeof (_Jv_InterpException)), 4);
120 }
121
122 _Jv_InterpException * exceptions ()
123 {
124 return (_Jv_InterpException*) (this+1);
125 }
126
127 static size_t size (int exc_count, int code_length)
128 {
129 return
130 ROUND ((sizeof (_Jv_InterpMethod)
131 + (exc_count * sizeof (_Jv_InterpException))), 4)
132 + code_length;
133 }
134
135 // return the method's invocation pointer (a stub).
136 void *ncode ();
137 void compile (const void * const *);
138
139 static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
140 static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
141 static void run_class (ffi_cif*, void*, ffi_raw*, void*);
142 static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
143
144 void run (void*, ffi_raw *);
145
146 public:
147 static void dump_object(jobject o);
148
149 friend class _Jv_ClassReader;
150 friend class _Jv_BytecodeVerifier;
151 friend class gnu::gcj::runtime::NameFinder;
152 friend class gnu::gcj::runtime::StackTrace;
153
154
155 friend void _Jv_PrepareClass(jclass);
156
157 #ifdef JV_MARKOBJ_DECL
158 friend JV_MARKOBJ_DECL;
159 #endif
160 };
161
162 class _Jv_InterpClass
163 {
164 _Jv_MethodBase **interpreted_methods;
165 _Jv_ushort *field_initializers;
166
167 friend class _Jv_ClassReader;
168 friend class _Jv_InterpMethod;
169 friend void _Jv_PrepareClass(jclass);
170 friend void _Jv_PrepareMissingMethods (jclass base2, jclass iface_class);
171 friend void _Jv_InitField (jobject, jclass, int);
172 #ifdef JV_MARKOBJ_DECL
173 friend JV_MARKOBJ_DECL;
174 #endif
175
176 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
177 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
178 };
179
180 // We have an interpreted class CL and we're trying to find the
181 // address of the ncode of a method METH. That interpreted class
182 // hasn't yet been prepared, so we defer fixups until they are ready.
183 // To do this, we create a chain of fixups that will be resolved by
184 // _Jv_PrepareClass.
185 extern inline void
186 _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **address)
187 {
188 int i;
189 jclass self = (jclass) cl;
190 _Jv_InterpClass *interp_cl = (_Jv_InterpClass*) self->aux_info;
191
192 for (i = 0; i < self->method_count; i++)
193 {
194 _Jv_Method *m = &self->methods[i];
195 if (m == meth)
196 {
197 _Jv_MethodBase *imeth = interp_cl->interpreted_methods[i];
198 *address = imeth->deferred;
199 imeth->deferred = address;
200 return;
201 }
202 }
203 return;
204 }
205
206 extern inline _Jv_MethodBase **
207 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
208 {
209 return klass->interpreted_methods;
210 }
211
212 struct _Jv_ResolvedMethod {
213 jint stack_item_count;
214 jint vtable_index;
215 jclass klass;
216 _Jv_Method* method;
217
218 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
219 // to mark the resolved method to hold on to the cif. Some memory could be
220 // saved by keeping a cache of cif's, since many will be the same.
221 ffi_cif cif;
222 ffi_type * arg_types[0];
223 };
224
225 class _Jv_JNIMethod : public _Jv_MethodBase
226 {
227 // The underlying function. If NULL we have to look for the
228 // function.
229 void *function;
230
231 // This is the CIF used by the JNI function.
232 ffi_cif jni_cif;
233
234 // These are the argument types used by the JNI function.
235 ffi_type **jni_arg_types;
236
237 // This function is used when making a JNI call from the interpreter.
238 static void call (ffi_cif *, void *, ffi_raw *, void *);
239
240 void *ncode ();
241
242 friend class _Jv_ClassReader;
243 friend void _Jv_PrepareClass(jclass);
244
245 public:
246 // FIXME: this is ugly.
247 void set_function (void *f)
248 {
249 function = f;
250 }
251 };
252
253 // A structure of this type is used to link together interpreter
254 // invocations on the stack.
255 struct _Jv_MethodChain
256 {
257 const _Jv_InterpMethod *self;
258 _Jv_MethodChain **ptr;
259 _Jv_MethodChain *next;
260
261 _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n)
262 {
263 self = s;
264 ptr = n;
265 next = *n;
266 *n = this;
267 }
268
269 ~_Jv_MethodChain ()
270 {
271 *ptr = next;
272 }
273 };
274
275 #endif /* INTERPRETER */
276
277 #endif /* __JAVA_INTERP_H__ */