* Merged gcj-abi-2-dev-branch to trunk.
[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 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 java::security::ProtectionDomain *);
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
48 /* FIXME: this should really be defined in some more generic place */
49 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
50
51 /* the interpreter is written in C++, primarily because it makes it easy for
52 * the entire thing to be "friend" with class Class. */
53
54 class _Jv_InterpClass;
55 class _Jv_InterpMethod;
56
57 // Before a method is "compiled" we store values as the bytecode PC,
58 // an int. Afterwards we store them as pointers into the prepared
59 // code itself.
60 union _Jv_InterpPC
61 {
62 int i;
63 void *p;
64 };
65
66 class _Jv_InterpException
67 {
68 _Jv_InterpPC start_pc;
69 _Jv_InterpPC end_pc;
70 _Jv_InterpPC handler_pc;
71 _Jv_InterpPC handler_type;
72
73 friend class _Jv_ClassReader;
74 friend class _Jv_InterpMethod;
75 friend class _Jv_BytecodeVerifier;
76 };
77
78 // Base class for method representations. Subclasses are interpreted
79 // and JNI methods.
80 class _Jv_MethodBase
81 {
82 protected:
83 // The class which defined this method.
84 jclass defining_class;
85
86 // The method description.
87 _Jv_Method *self;
88
89 // Size of raw arguments.
90 _Jv_ushort args_raw_size;
91
92 friend class _Jv_InterpreterEngine;
93
94 public:
95 _Jv_Method *get_method ()
96 {
97 return self;
98 }
99 };
100
101 class _Jv_InterpMethod : public _Jv_MethodBase
102 {
103 _Jv_ushort max_stack;
104 _Jv_ushort max_locals;
105 int code_length;
106
107 _Jv_ushort exc_count;
108
109 void *prepared;
110
111 unsigned char* bytecode ()
112 {
113 return
114 ((unsigned char*)this)
115 + ROUND((sizeof (_Jv_InterpMethod)
116 + exc_count*sizeof (_Jv_InterpException)), 4);
117 }
118
119 _Jv_InterpException * exceptions ()
120 {
121 return (_Jv_InterpException*) (this+1);
122 }
123
124 static size_t size (int exc_count, int code_length)
125 {
126 return
127 ROUND ((sizeof (_Jv_InterpMethod)
128 + (exc_count * sizeof (_Jv_InterpException))), 4)
129 + code_length;
130 }
131
132 // return the method's invocation pointer (a stub).
133 void *ncode ();
134 void compile (const void * const *);
135
136 static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
137 static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
138 static void run_class (ffi_cif*, void*, ffi_raw*, void*);
139 static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
140
141 void run (void*, ffi_raw *);
142
143 public:
144 static void dump_object(jobject o);
145
146 friend class _Jv_ClassReader;
147 friend class _Jv_BytecodeVerifier;
148 friend class gnu::gcj::runtime::NameFinder;
149 friend class gnu::gcj::runtime::StackTrace;
150 friend class _Jv_InterpreterEngine;
151
152
153 #ifdef JV_MARKOBJ_DECL
154 friend JV_MARKOBJ_DECL;
155 #endif
156 };
157
158 class _Jv_InterpClass
159 {
160 _Jv_MethodBase **interpreted_methods;
161 _Jv_ushort *field_initializers;
162
163 friend class _Jv_ClassReader;
164 friend class _Jv_InterpMethod;
165 friend class _Jv_InterpreterEngine;
166 friend void _Jv_InitField (jobject, jclass, int);
167 #ifdef JV_MARKOBJ_DECL
168 friend JV_MARKOBJ_DECL;
169 #endif
170
171 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
172 };
173
174 extern inline _Jv_MethodBase **
175 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
176 {
177 return klass->interpreted_methods;
178 }
179
180 struct _Jv_ResolvedMethod {
181 jint stack_item_count;
182 jint vtable_index;
183 jclass klass;
184 _Jv_Method* method;
185
186 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
187 // to mark the resolved method to hold on to the cif. Some memory could be
188 // saved by keeping a cache of cif's, since many will be the same.
189 ffi_cif cif;
190 ffi_type * arg_types[0];
191 };
192
193 class _Jv_JNIMethod : public _Jv_MethodBase
194 {
195 // The underlying function. If NULL we have to look for the
196 // function.
197 void *function;
198
199 // This is the CIF used by the JNI function.
200 ffi_cif jni_cif;
201
202 // These are the argument types used by the JNI function.
203 ffi_type **jni_arg_types;
204
205 // This function is used when making a JNI call from the interpreter.
206 static void call (ffi_cif *, void *, ffi_raw *, void *);
207
208 void *ncode ();
209
210 friend class _Jv_ClassReader;
211 friend class _Jv_InterpreterEngine;
212
213 #ifdef JV_MARKOBJ_DECL
214 friend JV_MARKOBJ_DECL;
215 #endif
216
217 public:
218 // FIXME: this is ugly.
219 void set_function (void *f)
220 {
221 function = f;
222 }
223 };
224
225 // A structure of this type is used to link together interpreter
226 // invocations on the stack.
227 struct _Jv_MethodChain
228 {
229 const _Jv_InterpMethod *self;
230 _Jv_MethodChain **ptr;
231 _Jv_MethodChain *next;
232
233 _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n)
234 {
235 self = s;
236 ptr = n;
237 next = *n;
238 *n = this;
239 }
240
241 ~_Jv_MethodChain ()
242 {
243 *ptr = next;
244 }
245 };
246
247 #endif /* INTERPRETER */
248
249 #endif /* __JAVA_INTERP_H__ */