1 // java-interp.h - Header file for the bytecode interpreter.  -*- c++ -*-
2 
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003  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 #include <gnu/gcj/runtime/StackTrace.h>
26 
27 extern "C" {
28 #include <ffi.h>
29 }
30 
31 extern inline jboolean
_Jv_IsInterpretedClass(jclass c)32 _Jv_IsInterpretedClass (jclass c)
33 {
34   return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0;
35 }
36 
37 struct _Jv_ResolvedMethod;
38 
39 void _Jv_InitInterpreter ();
40 void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
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   _Jv_InterpClass *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   // Chain of addresses to fill in.  See _Jv_Defer_Resolution.
93   void *deferred;
94 
95   friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
96   friend void _Jv_PrepareClass(jclass);
97 
98 public:
get_method()99   _Jv_Method *get_method ()
100   {
101     return self;
102   }
103 };
104 
105 class _Jv_InterpMethod : public _Jv_MethodBase
106 {
107   _Jv_ushort       max_stack;
108   _Jv_ushort       max_locals;
109   int              code_length;
110 
111   _Jv_ushort       exc_count;
112 
113   void *prepared;
114 
bytecode()115   unsigned char* bytecode ()
116   {
117     return
118       ((unsigned char*)this)
119       + ROUND((sizeof (_Jv_InterpMethod)
120 	       + exc_count*sizeof (_Jv_InterpException)), 4);
121   }
122 
exceptions()123   _Jv_InterpException * exceptions ()
124   {
125     return (_Jv_InterpException*) (this+1);
126   }
127 
size(int exc_count,int code_length)128   static size_t size (int exc_count, int code_length)
129   {
130     return
131       ROUND ((sizeof (_Jv_InterpMethod)
132 	      + (exc_count * sizeof (_Jv_InterpException))), 4)
133       + code_length;
134   }
135 
136   // return the method's invocation pointer (a stub).
137   void *ncode ();
138   void compile (const void * const *);
139 
140   static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
141   static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
142   static void run_class (ffi_cif*, void*, ffi_raw*, void*);
143   static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
144 
145   void run (void*, ffi_raw *);
146 
147  public:
148   static void dump_object(jobject o);
149 
150   friend class _Jv_ClassReader;
151   friend class _Jv_BytecodeVerifier;
152   friend class gnu::gcj::runtime::NameFinder;
153   friend class gnu::gcj::runtime::StackTrace;
154 
155   friend void _Jv_PrepareClass(jclass);
156 
157 #ifdef JV_MARKOBJ_DECL
158   friend JV_MARKOBJ_DECL;
159 #endif
160 };
161 
162 class _Jv_InterpClass : public java::lang::Class
163 {
164   _Jv_MethodBase **interpreted_methods;
165   _Jv_ushort        *field_initializers;
166 
167   friend class _Jv_ClassReader;
168   friend class _Jv_InterpMethod;
169   friend void  _Jv_PrepareClass(jclass);
170   friend void  _Jv_PrepareMissingMethods (jclass base2, jclass iface_class);
171   friend void  _Jv_InitField (jobject, jclass, int);
172 #ifdef JV_MARKOBJ_DECL
173   friend JV_MARKOBJ_DECL;
174 #endif
175 
176   friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
177   friend void _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **);
178 };
179 
180 // We have an interpreted class CL and we're trying to find the
181 // address of the ncode of a method METH.  That interpreted class
182 // hasn't yet been prepared, so we defer fixups until they are ready.
183 // To do this, we create a chain of fixups that will be resolved by
184 // _Jv_PrepareClass.
185 extern inline void
_Jv_Defer_Resolution(void * cl,_Jv_Method * meth,void ** address)186 _Jv_Defer_Resolution (void *cl, _Jv_Method *meth, void **address)
187 {
188   int i;
189   _Jv_InterpClass *self = (_Jv_InterpClass *)cl;
190   for (i = 0; i < self->method_count; i++)
191     {
192       _Jv_Method *m = &self->methods[i];
193       if (m == meth)
194 	{
195 	  _Jv_MethodBase *imeth = self->interpreted_methods[i];
196 	  *address = imeth->deferred;
197 	  imeth->deferred = address;
198 	  return;
199 	}
200     }
201   return;
202 }
203 
204 extern inline _Jv_MethodBase **
_Jv_GetFirstMethod(_Jv_InterpClass * klass)205 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
206 {
207   return klass->interpreted_methods;
208 }
209 
210 struct _Jv_ResolvedMethod {
211   jint            stack_item_count;
212   jint            vtable_index;
213   jclass          klass;
214   _Jv_Method*     method;
215 
216   // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
217   // to mark the resolved method to hold on to the cif.  Some memory could be
218   // saved by keeping a cache of cif's, since many will be the same.
219   ffi_cif         cif;
220   ffi_type *      arg_types[0];
221 };
222 
223 class _Jv_JNIMethod : public _Jv_MethodBase
224 {
225   // The underlying function.  If NULL we have to look for the
226   // function.
227   void *function;
228 
229   // This is the CIF used by the JNI function.
230   ffi_cif jni_cif;
231 
232   // These are the argument types used by the JNI function.
233   ffi_type **jni_arg_types;
234 
235   // This function is used when making a JNI call from the interpreter.
236   static void call (ffi_cif *, void *, ffi_raw *, void *);
237 
238   void *ncode ();
239 
240   friend class _Jv_ClassReader;
241   friend void _Jv_PrepareClass(jclass);
242 
243 public:
244   // FIXME: this is ugly.
set_function(void * f)245   void set_function (void *f)
246   {
247     function = f;
248   }
249 };
250 
251 // A structure of this type is used to link together interpreter
252 // invocations on the stack.
253 struct _Jv_MethodChain
254 {
255   const _Jv_InterpMethod *self;
256   _Jv_MethodChain **ptr;
257   _Jv_MethodChain *next;
258 
_Jv_MethodChain_Jv_MethodChain259   _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n)
260   {
261     self = s;
262     ptr = n;
263     next = *n;
264     *n = this;
265   }
266 
~_Jv_MethodChain_Jv_MethodChain267   ~_Jv_MethodChain ()
268   {
269     *ptr = next;
270   }
271 };
272 
273 #endif /* INTERPRETER */
274 
275 #endif /* __JAVA_INTERP_H__ */
276