OSDN Git Service

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