2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / include / java-interp.h
blob94acfae281f40facb02abd13a34b871f07695791
1 // java-interp.h - Header file for the bytecode interpreter. -*- c++ -*-
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003 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
9 details. */
11 #ifndef __JAVA_INTERP_H__
12 #define __JAVA_INTERP_H__
14 #include <jvm.h>
15 #include <java-cpool.h>
16 #include <gnu/gcj/runtime/NameFinder.h>
18 #ifdef INTERPRETER
20 #pragma interface
22 #include <java/lang/Class.h>
23 #include <java/lang/ClassLoader.h>
24 #include <java/lang/reflect/Modifier.h>
25 #include <gnu/gcj/runtime/StackTrace.h>
27 extern "C" {
28 #include <ffi.h>
31 extern inline jboolean
32 _Jv_IsInterpretedClass (jclass c)
34 return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0;
37 struct _Jv_ResolvedMethod;
39 void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
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 /* FIXME: this should really be defined in some more generic place */
48 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
50 /* the interpreter is written in C++, primarily because it makes it easy for
51 * the entire thing to be "friend" with class Class. */
53 class _Jv_InterpClass;
54 class _Jv_InterpMethod;
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
61 int i;
62 void *p;
65 class _Jv_InterpException
67 _Jv_InterpPC start_pc;
68 _Jv_InterpPC end_pc;
69 _Jv_InterpPC handler_pc;
70 _Jv_InterpPC handler_type;
72 friend class _Jv_ClassReader;
73 friend class _Jv_InterpMethod;
74 friend class _Jv_BytecodeVerifier;
77 // Base class for method representations. Subclasses are interpreted
78 // and JNI methods.
79 class _Jv_MethodBase
81 protected:
82 // The class which defined this method.
83 _Jv_InterpClass *defining_class;
85 // The method description.
86 _Jv_Method *self;
88 // Size of raw arguments.
89 _Jv_ushort args_raw_size;
91 // Chain of addresses to fill in. See _Jv_Defer_Resolution.
92 void *deferred;
94 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
95 friend void _Jv_PrepareClass(jclass);
97 public:
98 _Jv_Method *get_method ()
100 return self;
104 class _Jv_InterpMethod : public _Jv_MethodBase
106 _Jv_ushort max_stack;
107 _Jv_ushort max_locals;
108 int code_length;
110 _Jv_ushort exc_count;
112 void *prepared;
114 unsigned char* bytecode ()
116 return
117 ((unsigned char*)this)
118 + ROUND((sizeof (_Jv_InterpMethod)
119 + exc_count*sizeof (_Jv_InterpException)), 4);
122 _Jv_InterpException * exceptions ()
124 return (_Jv_InterpException*) (this+1);
127 static size_t size (int exc_count, int code_length)
129 return
130 ROUND ((sizeof (_Jv_InterpMethod)
131 + (exc_count * sizeof (_Jv_InterpException))), 4)
132 + code_length;
135 // return the method's invocation pointer (a stub).
136 void *ncode ();
137 void compile (const void * const *);
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*);
144 void run (void*, ffi_raw *);
146 public:
147 static void dump_object(jobject o);
149 friend class _Jv_ClassReader;
150 friend class _Jv_BytecodeVerifier;
151 friend class gnu::gcj::runtime::NameFinder;
152 friend class gnu::gcj::runtime::StackTrace;
154 friend void _Jv_PrepareClass(jclass);
156 #ifdef JV_MARKOBJ_DECL
157 friend JV_MARKOBJ_DECL;
158 #endif
161 class _Jv_InterpClass : public java::lang::Class
163 _Jv_MethodBase **interpreted_methods;
164 _Jv_ushort *field_initializers;
166 friend class _Jv_ClassReader;
167 friend class _Jv_InterpMethod;
168 friend void _Jv_PrepareClass(jclass);
169 friend void _Jv_PrepareMissingMethods (jclass base2, jclass iface_class);
170 friend void _Jv_InitField (jobject, jclass, int);
171 #ifdef JV_MARKOBJ_DECL
172 friend JV_MARKOBJ_DECL;
173 #endif
175 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
176 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
179 // We have an interpreted class CL and we're trying to find the
180 // address of the ncode of a method METH. That interpreted class
181 // hasn't yet been prepared, so we defer fixups until they are ready.
182 // To do this, we create a chain of fixups that will be resolved by
183 // _Jv_PrepareClass.
184 extern inline void
185 _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **address)
187 int i;
188 _Jv_InterpClass *self = (_Jv_InterpClass *)cl;
189 for (i = 0; i < self->method_count; i++)
191 _Jv_Method *m = &self->methods[i];
192 if (m == meth)
194 _Jv_MethodBase *imeth = self->interpreted_methods[i];
195 *address = imeth->deferred;
196 imeth->deferred = address;
197 return;
200 return;
203 extern inline _Jv_MethodBase **
204 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
206 return klass->interpreted_methods;
209 struct _Jv_ResolvedMethod {
210 jint stack_item_count;
211 jint vtable_index;
212 jclass klass;
213 _Jv_Method* method;
215 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
216 // to mark the resolved method to hold on to the cif. Some memory could be
217 // saved by keeping a cache of cif's, since many will be the same.
218 ffi_cif cif;
219 ffi_type * arg_types[0];
222 class _Jv_JNIMethod : public _Jv_MethodBase
224 // The underlying function. If NULL we have to look for the
225 // function.
226 void *function;
228 // This is the CIF used by the JNI function.
229 ffi_cif jni_cif;
231 // These are the argument types used by the JNI function.
232 ffi_type **jni_arg_types;
234 // This function is used when making a JNI call from the interpreter.
235 static void call (ffi_cif *, void *, ffi_raw *, void *);
237 void *ncode ();
239 friend class _Jv_ClassReader;
240 friend void _Jv_PrepareClass(jclass);
242 public:
243 // FIXME: this is ugly.
244 void set_function (void *f)
246 function = f;
250 // A structure of this type is used to link together interpreter
251 // invocations on the stack.
252 struct _Jv_MethodChain
254 const _Jv_InterpMethod *self;
255 _Jv_MethodChain **ptr;
256 _Jv_MethodChain *next;
258 _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n)
260 self = s;
261 ptr = n;
262 next = *n;
263 *n = this;
266 ~_Jv_MethodChain ()
268 *ptr = next;
272 #endif /* INTERPRETER */
274 #endif /* __JAVA_INTERP_H__ */