java-interp.h (class _Jv_InterpMethod): Added JV_MARKOBJ_DECL.
[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 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 <gnu/gcj/runtime/StackTrace.h>
25
26 extern "C" {
27 #include <ffi.h>
28 }
29
30 extern inline jboolean
31 _Jv_IsInterpretedClass (jclass c)
32 {
33 return (c->loader != 0);
34 }
35
36 struct _Jv_ResolvedMethod;
37
38 void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
39
40 void _Jv_InitField (jobject, jclass, int);
41 void * _Jv_AllocMethodInvocation (jsize size);
42 int _Jv_count_arguments (_Jv_Utf8Const *signature,
43 jboolean staticp = true);
44 void _Jv_VerifyMethod (_Jv_InterpMethod *method);
45
46 /* FIXME: this should really be defined in some more generic place */
47 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
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 _Jv_InterpClass *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 public:
91 _Jv_Method *get_method ()
92 {
93 return self;
94 }
95 };
96
97 class _Jv_InterpMethod : public _Jv_MethodBase
98 {
99 _Jv_ushort max_stack;
100 _Jv_ushort max_locals;
101 int code_length;
102
103 _Jv_ushort exc_count;
104
105 void *prepared;
106
107 unsigned char* bytecode ()
108 {
109 return
110 ((unsigned char*)this)
111 + ROUND((sizeof (_Jv_InterpMethod)
112 + exc_count*sizeof (_Jv_InterpException)), 4);
113 }
114
115 _Jv_InterpException * exceptions ()
116 {
117 return (_Jv_InterpException*) (this+1);
118 }
119
120 static size_t size (int exc_count, int code_length)
121 {
122 return
123 ROUND ((sizeof (_Jv_InterpMethod)
124 + (exc_count * sizeof (_Jv_InterpException))), 4)
125 + code_length;
126 }
127
128 // return the method's invocation pointer (a stub).
129 void *ncode ();
130 void compile (const void * const *);
131
132 static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
133 static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
134 static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
135
136 void run (void*, ffi_raw *);
137
138 public:
139 static void dump_object(jobject o);
140
141 friend class _Jv_ClassReader;
142 friend class _Jv_BytecodeVerifier;
143 friend class gnu::gcj::runtime::NameFinder;
144 friend class gnu::gcj::runtime::StackTrace;
145
146 friend void _Jv_PrepareClass(jclass);
147
148 #ifdef JV_MARKOBJ_DECL
149 friend JV_MARKOBJ_DECL;
150 #endif
151 };
152
153 class _Jv_InterpClass : public java::lang::Class
154 {
155 _Jv_MethodBase **interpreted_methods;
156 _Jv_ushort *field_initializers;
157
158 friend class _Jv_ClassReader;
159 friend class _Jv_InterpMethod;
160 friend void _Jv_PrepareClass(jclass);
161 friend void _Jv_InitField (jobject, jclass, int);
162 #ifdef JV_MARKOBJ_DECL
163 friend JV_MARKOBJ_DECL;
164 #endif
165
166 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
167 };
168
169 extern inline _Jv_MethodBase **
170 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
171 {
172 return klass->interpreted_methods;
173 }
174
175 struct _Jv_ResolvedMethod {
176 jint stack_item_count;
177 jint vtable_index;
178 jclass klass;
179 _Jv_Method* method;
180
181 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
182 // to mark the resolved method to hold on to the cif. Some memory could be
183 // saved by keeping a cache of cif's, since many will be the same.
184 ffi_cif cif;
185 ffi_type * arg_types[0];
186 };
187
188 class _Jv_JNIMethod : public _Jv_MethodBase
189 {
190 // The underlying function. If NULL we have to look for the
191 // function.
192 void *function;
193
194 // This is the CIF used by the JNI function.
195 ffi_cif jni_cif;
196
197 // These are the argument types used by the JNI function.
198 ffi_type **jni_arg_types;
199
200 // This function is used when making a JNI call from the interpreter.
201 static void call (ffi_cif *, void *, ffi_raw *, void *);
202
203 void *ncode ();
204
205 friend class _Jv_ClassReader;
206 friend void _Jv_PrepareClass(jclass);
207
208 public:
209 // FIXME: this is ugly.
210 void set_function (void *f)
211 {
212 function = f;
213 }
214 };
215
216 // A structure of this type is used to link together interpreter
217 // invocations on the stack.
218 struct _Jv_MethodChain
219 {
220 const _Jv_InterpMethod *self;
221 _Jv_MethodChain **ptr;
222 _Jv_MethodChain *next;
223
224 _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n)
225 {
226 self = s;
227 ptr = n;
228 next = *n;
229 *n = this;
230 }
231
232 ~_Jv_MethodChain ()
233 {
234 *ptr = next;
235 }
236 };
237
238 #endif /* INTERPRETER */
239
240 #endif /* __JAVA_INTERP_H__ */