Class.h (Class): Made _Jv_BytecodeVerifier a friend.
[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 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
17 #ifdef INTERPRETER
18
19 #pragma interface
20
21 #include <java/lang/Class.h>
22 #include <java/lang/ClassLoader.h>
23
24 extern "C" {
25 #include <ffi.h>
26 }
27
28 extern inline jboolean
29 _Jv_IsInterpretedClass (jclass c)
30 {
31 return (c->loader != 0);
32 }
33
34 struct _Jv_ResolvedMethod;
35
36 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
37 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
38 bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
39 bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
40 bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
41 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
42 void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
43
44 void _Jv_InitField (jobject, jclass, int);
45 void * _Jv_AllocMethodInvocation (jsize size);
46 int _Jv_count_arguments (_Jv_Utf8Const *signature,
47 jboolean staticp = true);
48 void _Jv_VerifyMethod (_Jv_InterpMethod *method);
49
50 /* FIXME: this should really be defined in some more generic place */
51 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
52
53 /* the interpreter is written in C++, primarily because it makes it easy for
54 * the entire thing to be "friend" with class Class. */
55
56 class _Jv_InterpClass;
57 class _Jv_InterpMethod;
58 class _Jv_InterpMethodInvocation;
59
60 class _Jv_InterpException
61 {
62 int start_pc;
63 int end_pc;
64 int handler_pc;
65 int handler_type;
66
67 friend class _Jv_ClassReader;
68 friend class _Jv_InterpMethod;
69 friend class _Jv_BytecodeVerifier;
70 };
71
72 // Base class for method representations. Subclasses are interpreted
73 // and JNI methods.
74 class _Jv_MethodBase
75 {
76 protected:
77 // The class which defined this method.
78 _Jv_InterpClass *defining_class;
79
80 // The method description.
81 _Jv_Method *self;
82
83 // Size of raw arguments.
84 _Jv_ushort args_raw_size;
85
86 public:
87 _Jv_Method *get_method ()
88 {
89 return self;
90 }
91 };
92
93 class _Jv_InterpMethod : public _Jv_MethodBase
94 {
95 _Jv_ushort max_stack;
96 _Jv_ushort max_locals;
97 int code_length;
98
99 _Jv_ushort exc_count;
100
101 unsigned char* bytecode ()
102 {
103 return
104 ((unsigned char*)this)
105 + ROUND((sizeof (_Jv_InterpMethod)
106 + exc_count*sizeof (_Jv_InterpException)), 4);
107 }
108
109 _Jv_InterpException * exceptions ()
110 {
111 return (_Jv_InterpException*) (this+1);
112 }
113
114 static size_t size (int exc_count, int code_length)
115 {
116 return
117 ROUND ((sizeof (_Jv_InterpMethod)
118 + (exc_count * sizeof (_Jv_InterpException))), 4)
119 + code_length;
120 }
121
122 // return the method's invocation pointer (a stub).
123 void *ncode ();
124 void continue1 (_Jv_InterpMethodInvocation *inv);
125
126 static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
127 static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
128 static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
129
130 inline jobject run (ffi_cif*, void*, ffi_raw*,
131 _Jv_InterpMethodInvocation*);
132
133 bool find_exception (jobject ex,
134 _Jv_InterpMethodInvocation *inv);
135
136 public:
137 static void dump_object(jobject o);
138
139 friend class _Jv_ClassReader;
140 friend class _Jv_InterpMethodInvocation;
141 friend class _Jv_BytecodeVerifier;
142
143 friend void _Jv_PrepareClass(jclass);
144 };
145
146 class _Jv_InterpMethodInvocation {
147 _Jv_InterpMethod *running;
148 _Jv_word *sp;
149 unsigned char *pc;
150 _Jv_word state[0];
151
152 _Jv_word* stack_base () { return &state[0]; }
153 _Jv_word* local_base () { return &state[running->max_stack]; }
154
155 friend class _Jv_InterpMethod;
156 };
157
158 class _Jv_InterpClass : public java::lang::Class
159 {
160 _Jv_MethodBase **interpreted_methods;
161 _Jv_ushort *field_initializers;
162
163 friend class _Jv_ClassReader;
164 friend class _Jv_InterpMethod;
165 friend void _Jv_PrepareClass(jclass);
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 void _Jv_PrepareClass(jclass);
212
213 public:
214 // FIXME: this is ugly.
215 void set_function (void *f)
216 {
217 function = f;
218 }
219 };
220
221 #endif /* INTERPRETER */
222
223 #endif /* __JAVA_INTERP_H__ */