]> gcc.gnu.org Git - gcc.git/blame - libjava/include/java-interp.h
libiberty.h (XNEW, [...]): Move here from libcpp/internal.h.
[gcc.git] / libjava / include / java-interp.h
CommitLineData
58eb6e7c
AG
1// java-interp.h - Header file for the bytecode interpreter. -*- c++ -*-
2
4c98b1b0 3/* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
58eb6e7c
AG
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#ifndef __JAVA_INTERP_H__
12#define __JAVA_INTERP_H__
13
58eb6e7c
AG
14#include <jvm.h>
15#include <java-cpool.h>
3308c46e 16#include <gnu/gcj/runtime/NameFinder.h>
58eb6e7c
AG
17
18#ifdef INTERPRETER
19
20#pragma interface
21
22#include <java/lang/Class.h>
23#include <java/lang/ClassLoader.h>
a1aba4f9 24#include <java/lang/reflect/Modifier.h>
58eb6e7c
AG
25
26extern "C" {
27#include <ffi.h>
28}
29
30extern inline jboolean
31_Jv_IsInterpretedClass (jclass c)
32{
a1aba4f9 33 return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0;
58eb6e7c
AG
34}
35
36struct _Jv_ResolvedMethod;
37
58eb6e7c 38void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
58eb6e7c
AG
39
40void _Jv_InitField (jobject, jclass, int);
41void * _Jv_AllocMethodInvocation (jsize size);
a12fe13d
TT
42int _Jv_count_arguments (_Jv_Utf8Const *signature,
43 jboolean staticp = true);
44void _Jv_VerifyMethod (_Jv_InterpMethod *method);
58eb6e7c
AG
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
52class _Jv_InterpClass;
53class _Jv_InterpMethod;
fdae83ab
TT
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.
58union _Jv_InterpPC
59{
60 int i;
61 void *p;
62};
58eb6e7c 63
a12fe13d
TT
64class _Jv_InterpException
65{
fdae83ab
TT
66 _Jv_InterpPC start_pc;
67 _Jv_InterpPC end_pc;
68 _Jv_InterpPC handler_pc;
69 _Jv_InterpPC handler_type;
58eb6e7c
AG
70
71 friend class _Jv_ClassReader;
72 friend class _Jv_InterpMethod;
a12fe13d 73 friend class _Jv_BytecodeVerifier;
58eb6e7c
AG
74};
75
f39b788a
TT
76// Base class for method representations. Subclasses are interpreted
77// and JNI methods.
78class _Jv_MethodBase
79{
80protected:
81 // The class which defined this method.
f5310108 82 jclass defining_class;
f39b788a
TT
83
84 // The method description.
85 _Jv_Method *self;
d348bda4
TT
86
87 // Size of raw arguments.
88 _Jv_ushort args_raw_size;
8ade4771 89
90471585
AH
90 // Chain of addresses to fill in. See _Jv_Defer_Resolution.
91 void *deferred;
92
93 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
94 friend void _Jv_PrepareClass(jclass);
95
8ade4771
TT
96public:
97 _Jv_Method *get_method ()
98 {
99 return self;
100 }
f39b788a
TT
101};
102
facc279f
TT
103class _Jv_InterpMethod : public _Jv_MethodBase
104{
58eb6e7c
AG
105 _Jv_ushort max_stack;
106 _Jv_ushort max_locals;
107 int code_length;
108
109 _Jv_ushort exc_count;
58eb6e7c 110
fdae83ab
TT
111 void *prepared;
112
58eb6e7c
AG
113 unsigned char* bytecode ()
114 {
115 return
116 ((unsigned char*)this)
117 + ROUND((sizeof (_Jv_InterpMethod)
118 + exc_count*sizeof (_Jv_InterpException)), 4);
119 }
fdae83ab 120
58eb6e7c
AG
121 _Jv_InterpException * exceptions ()
122 {
123 return (_Jv_InterpException*) (this+1);
124 }
125
126 static size_t size (int exc_count, int code_length)
127 {
128 return
129 ROUND ((sizeof (_Jv_InterpMethod)
130 + (exc_count * sizeof (_Jv_InterpException))), 4)
131 + code_length;
132 }
133
134 // return the method's invocation pointer (a stub).
135 void *ncode ();
fdae83ab 136 void compile (const void * const *);
58eb6e7c 137
7941ceab
AG
138 static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
139 static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
4c98b1b0 140 static void run_class (ffi_cif*, void*, ffi_raw*, void*);
7941ceab 141 static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
58eb6e7c 142
fdae83ab 143 void run (void*, ffi_raw *);
58eb6e7c
AG
144
145 public:
146 static void dump_object(jobject o);
147
148 friend class _Jv_ClassReader;
a12fe13d 149 friend class _Jv_BytecodeVerifier;
3308c46e 150 friend class gnu::gcj::runtime::NameFinder;
421f9e60 151 friend class gnu::gcj::runtime::StackTrace;
f5310108 152
58eb6e7c
AG
153
154 friend void _Jv_PrepareClass(jclass);
b4d0051b
TT
155
156#ifdef JV_MARKOBJ_DECL
157 friend JV_MARKOBJ_DECL;
158#endif
58eb6e7c
AG
159};
160
f5310108 161class _Jv_InterpClass
58eb6e7c 162{
facc279f 163 _Jv_MethodBase **interpreted_methods;
58eb6e7c
AG
164 _Jv_ushort *field_initializers;
165
166 friend class _Jv_ClassReader;
167 friend class _Jv_InterpMethod;
168 friend void _Jv_PrepareClass(jclass);
a1aba4f9 169 friend void _Jv_PrepareMissingMethods (jclass base2, jclass iface_class);
58eb6e7c 170 friend void _Jv_InitField (jobject, jclass, int);
470042c7
TT
171#ifdef JV_MARKOBJ_DECL
172 friend JV_MARKOBJ_DECL;
173#endif
8ade4771
TT
174
175 friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
90471585 176 friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
58eb6e7c
AG
177};
178
90471585
AH
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.
184extern inline void
185_Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **address)
186{
187 int i;
f5310108
BM
188 jclass self = (jclass) cl;
189 _Jv_InterpClass *interp_cl = (_Jv_InterpClass*) self->aux_info;
190
90471585
AH
191 for (i = 0; i < self->method_count; i++)
192 {
193 _Jv_Method *m = &self->methods[i];
194 if (m == meth)
195 {
f5310108 196 _Jv_MethodBase *imeth = interp_cl->interpreted_methods[i];
90471585
AH
197 *address = imeth->deferred;
198 imeth->deferred = address;
199 return;
200 }
201 }
202 return;
203}
204
8ade4771
TT
205extern inline _Jv_MethodBase **
206_Jv_GetFirstMethod (_Jv_InterpClass *klass)
207{
208 return klass->interpreted_methods;
209}
210
58eb6e7c
AG
211struct _Jv_ResolvedMethod {
212 jint stack_item_count;
213 jint vtable_index;
214 jclass klass;
215 _Jv_Method* method;
216
217 // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
218 // to mark the resolved method to hold on to the cif. Some memory could be
219 // saved by keeping a cache of cif's, since many will be the same.
220 ffi_cif cif;
221 ffi_type * arg_types[0];
222};
223
facc279f
TT
224class _Jv_JNIMethod : public _Jv_MethodBase
225{
226 // The underlying function. If NULL we have to look for the
227 // function.
228 void *function;
229
d348bda4
TT
230 // This is the CIF used by the JNI function.
231 ffi_cif jni_cif;
232
233 // These are the argument types used by the JNI function.
234 ffi_type **jni_arg_types;
235
facc279f
TT
236 // This function is used when making a JNI call from the interpreter.
237 static void call (ffi_cif *, void *, ffi_raw *, void *);
238
239 void *ncode ();
240
241 friend class _Jv_ClassReader;
242 friend void _Jv_PrepareClass(jclass);
8ade4771
TT
243
244public:
245 // FIXME: this is ugly.
246 void set_function (void *f)
247 {
248 function = f;
249 }
facc279f
TT
250};
251
3308c46e
TT
252// A structure of this type is used to link together interpreter
253// invocations on the stack.
254struct _Jv_MethodChain
255{
256 const _Jv_InterpMethod *self;
257 _Jv_MethodChain **ptr;
258 _Jv_MethodChain *next;
259
260 _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n)
261 {
262 self = s;
263 ptr = n;
264 next = *n;
265 *n = this;
266 }
267
268 ~_Jv_MethodChain ()
269 {
270 *ptr = next;
271 }
272};
273
f39b788a
TT
274#endif /* INTERPRETER */
275
58eb6e7c 276#endif /* __JAVA_INTERP_H__ */
This page took 0.367513 seconds and 5 git commands to generate.