OSDN Git Service

PR other/26208
[pf3gnuchains/gcc-fork.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, 2005, 2006  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 <java/lang/Thread.h>
26 #include <gnu/gcj/RawData.h>
27
28 // Define this to get the direct-threaded interpreter.  If undefined,
29 // we revert to a basic bytecode interpreter.  The former is faster
30 // but uses more memory.
31 #define DIRECT_THREADED
32
33 extern "C" {
34 #include <ffi.h>
35 }
36
37 struct _Jv_ResolvedMethod;
38
39 void _Jv_InitInterpreter ();
40 void _Jv_DefineClass (jclass, jbyteArray, jint, jint,
41                       java::security::ProtectionDomain *,
42                       _Jv_Utf8Const **);
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 void _Jv_CompileMethod (_Jv_InterpMethod* method);
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 // The type of the PC depends on whether we're doing direct threading
102 // or a more ordinary bytecode interpreter.
103 #ifdef DIRECT_THREADED
104 // Slot in the "compiled" form of the bytecode.
105 union insn_slot
106 {
107   // Address of code.
108   void *insn;
109   // An integer value used by an instruction.
110   jint int_val;
111   // A pointer value used by an instruction.
112   void *datum;
113 };
114
115 typedef insn_slot *pc_t;
116 #else
117 typedef unsigned char *pc_t;
118 #endif
119
120
121 // This structure holds the bytecode pc and corresponding source code
122 // line number.  An array (plus length field) of this structure is put
123 // in each _Jv_InterpMethod and used to resolve the (internal) program
124 // counter of the interpreted method to an actual java source file
125 // line.
126 struct  _Jv_LineTableEntry
127 {
128   union
129   {
130     pc_t pc;
131     int bytecode_pc;
132   };
133   int line;
134 };
135
136 class _Jv_InterpMethod : public _Jv_MethodBase
137 {
138   _Jv_ushort       max_stack;
139   _Jv_ushort       max_locals;
140   int              code_length;
141
142   _Jv_ushort       exc_count;
143   bool             is_15;
144
145   // Length of the line_table - when this is zero then line_table is NULL.
146   int line_table_len;  
147   _Jv_LineTableEntry *line_table;
148
149   void *prepared;
150   int number_insn_slots;
151
152   unsigned char* bytecode () 
153   {
154     return 
155       ((unsigned char*)this) 
156       + ROUND((sizeof (_Jv_InterpMethod)
157                + exc_count*sizeof (_Jv_InterpException)), 4);
158   }
159
160   _Jv_InterpException * exceptions ()
161   {
162     return (_Jv_InterpException*) (this+1);
163   }
164
165   static size_t size (int exc_count, int code_length)
166   {
167     return 
168       ROUND ((sizeof (_Jv_InterpMethod) 
169               + (exc_count * sizeof (_Jv_InterpException))), 4)
170       + code_length;
171   }
172
173   // return the method's invocation pointer (a stub).
174   void *ncode ();
175   void compile (const void * const *);
176
177   static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
178   static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
179   static void run_class (ffi_cif*, void*, ffi_raw*, void*);
180   static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
181
182   static void run (void*, ffi_raw *, _Jv_InterpMethod *);
183
184   // Returns source file line number for given PC value, or -1 if line
185   // number info is unavailable.
186   int get_source_line(pc_t mpc);
187
188 #ifdef DIRECT_THREADED
189   // Convenience function for indexing bytecode PC/insn slots in
190   // line tables for JDWP
191   jlong insn_index (pc_t pc);
192 #endif
193
194  public:
195   static void dump_object(jobject o);
196
197   /* Get the line table for this method.
198    * start  is the lowest index in the method
199    * end    is the  highest index in the method
200    * line_numbers is an array to hold the list of source line numbers
201    * code_indices is an array to hold the corresponding list of code indices
202    */
203   void get_line_table (jlong& start, jlong& end, jintArray& line_numbers,
204                        jlongArray& code_indices);
205
206 #ifdef DIRECT_THREADED
207   friend void _Jv_CompileMethod (_Jv_InterpMethod*);
208 #endif
209   
210   friend class _Jv_ClassReader;
211   friend class _Jv_BytecodeVerifier;
212   friend class _Jv_StackTrace;
213   friend class _Jv_InterpreterEngine;
214
215 #ifdef JV_MARKOBJ_DECL
216   friend JV_MARKOBJ_DECL;
217 #endif
218 };
219
220 class _Jv_InterpClass
221 {
222   _Jv_MethodBase **interpreted_methods;
223   _Jv_ushort     *field_initializers;
224   jstring source_file_name;
225
226   friend class _Jv_ClassReader;
227   friend class _Jv_InterpMethod;
228   friend class _Jv_StackTrace;
229   friend class _Jv_InterpreterEngine;
230
231   friend void  _Jv_InitField (jobject, jclass, int);
232 #ifdef JV_MARKOBJ_DECL
233   friend JV_MARKOBJ_DECL;
234 #endif
235
236   friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
237 };
238
239 extern inline _Jv_MethodBase **
240 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
241 {
242   return klass->interpreted_methods;
243 }
244
245 struct _Jv_ResolvedMethod
246 {
247   jint            stack_item_count;     
248   jint            vtable_index; 
249   jclass          klass;
250   _Jv_Method*     method;
251
252   // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
253   // to mark the resolved method to hold on to the cif.  Some memory could be
254   // saved by keeping a cache of cif's, since many will be the same.
255   ffi_cif         cif;
256   ffi_type *      arg_types[0];
257 };
258
259 class _Jv_JNIMethod : public _Jv_MethodBase
260 {
261   // The underlying function.  If NULL we have to look for the
262   // function.
263   void *function;
264
265   // This is the CIF used by the JNI function.
266   ffi_cif jni_cif;
267
268   // These are the argument types used by the JNI function.
269   ffi_type **jni_arg_types;
270
271   // This function is used when making a JNI call from the interpreter.
272   static void call (ffi_cif *, void *, ffi_raw *, void *);
273
274   void *ncode ();
275
276   friend class _Jv_ClassReader;
277   friend class _Jv_InterpreterEngine;
278
279 #ifdef JV_MARKOBJ_DECL
280   friend JV_MARKOBJ_DECL;
281 #endif
282
283 public:
284   // FIXME: this is ugly.
285   void set_function (void *f)
286   {
287     function = f;
288   }
289 };
290
291 // The interpreted call stack, represented by a linked list of frames.
292 struct _Jv_InterpFrame
293 {
294   _Jv_InterpMethod *self;
295   java::lang::Thread *thread;
296   _Jv_InterpFrame *next;
297   pc_t pc;
298
299   _Jv_InterpFrame (_Jv_InterpMethod *s, java::lang::Thread *thr)
300   {
301     self = s;
302     thread = thr;
303     next = (_Jv_InterpFrame *) thr->interp_frame;
304     thr->interp_frame = (gnu::gcj::RawData *) this;
305     pc = NULL;
306   }
307
308   ~_Jv_InterpFrame ()
309   {
310     thread->interp_frame = (gnu::gcj::RawData *) next;
311   }
312 };
313
314 #endif /* INTERPRETER */
315
316 #endif /* __JAVA_INTERP_H__ */