OSDN Git Service

* include/java-interp.h (_Jv_InterpClass): Declare
[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, 2007  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 int _Jv_init_cif (_Jv_Utf8Const* signature,
49                   int arg_count,
50                   jboolean staticp,
51                   ffi_cif *cif,
52                   ffi_type **arg_types,
53                   ffi_type **rtype_p);
54
55 /* the interpreter is written in C++, primarily because it makes it easy for
56  * the entire thing to be "friend" with class Class. */
57
58 class _Jv_InterpClass;
59 class _Jv_InterpMethod;
60
61 // Before a method is "compiled" we store values as the bytecode PC,
62 // an int.  Afterwards we store them as pointers into the prepared
63 // code itself.
64 union _Jv_InterpPC
65 {
66   int i;
67   void *p;
68 };
69
70 class _Jv_InterpException
71 {
72   _Jv_InterpPC start_pc;
73   _Jv_InterpPC end_pc;
74   _Jv_InterpPC handler_pc;
75   _Jv_InterpPC handler_type;
76
77   friend class _Jv_ClassReader;
78   friend class _Jv_InterpMethod;
79   friend class _Jv_BytecodeVerifier;
80 };
81
82 // Base class for method representations.  Subclasses are interpreted
83 // and JNI methods.
84 class _Jv_MethodBase
85 {
86 protected:
87   // The class which defined this method.
88   jclass defining_class;
89
90   // The method description.
91   _Jv_Method *self;
92
93   // Size of raw arguments.
94   _Jv_ushort args_raw_size;
95
96   friend class _Jv_InterpreterEngine;
97
98 public:
99   _Jv_Method *get_method ()
100   {
101     return self;
102   }
103 };
104
105 // The type of the PC depends on whether we're doing direct threading
106 // or a more ordinary bytecode interpreter.
107 #ifdef DIRECT_THREADED
108 // Slot in the "compiled" form of the bytecode.
109 union insn_slot
110 {
111   // Address of code.
112   void *insn;
113   // An integer value used by an instruction.
114   jint int_val;
115   // A pointer value used by an instruction.
116   void *datum;
117 };
118
119 typedef insn_slot *pc_t;
120 #else
121 typedef unsigned char *pc_t;
122 #endif
123
124
125 // This structure holds the bytecode pc and corresponding source code
126 // line number.  An array (plus length field) of this structure is put
127 // in each _Jv_InterpMethod and used to resolve the (internal) program
128 // counter of the interpreted method to an actual java source file
129 // line.
130 struct  _Jv_LineTableEntry
131 {
132   union
133   {
134     pc_t pc;
135     int bytecode_pc;
136   };
137   int line;
138 };
139
140 // This structure holds local variable information.
141 // The pc value is the first pc where the variable must have a value and it
142 // must continue to have a value until (start_pc + length).
143 // The name is the variable name, and the descriptor contains type information.
144 // The slot is the index in the local variable array of this method, long and
145 // double occupy slot and slot+1.
146 struct _Jv_LocalVarTableEntry
147 {
148   int bytecode_start_pc;
149   int length;
150   char *name;
151   char *descriptor;
152   int slot;
153 };
154
155 class _Jv_InterpMethod : public _Jv_MethodBase
156 {
157   // Breakpoint instruction
158   static pc_t breakpoint_insn;
159 #ifdef DIRECT_THREADED
160   static insn_slot bp_insn_slot;
161 #else
162   static unsigned char bp_insn_opcode;
163 #endif
164
165   _Jv_ushort       max_stack;
166   _Jv_ushort       max_locals;
167   int              code_length;
168
169   _Jv_ushort       exc_count;
170   bool             is_15;
171
172   // Length of the line_table - when this is zero then line_table is NULL.
173   int line_table_len;  
174   _Jv_LineTableEntry *line_table;
175   
176   // The local variable table length and the table itself
177   int local_var_table_len;
178   _Jv_LocalVarTableEntry *local_var_table;
179
180   pc_t prepared;
181   int number_insn_slots;
182
183   unsigned char* bytecode () 
184   {
185     return 
186       ((unsigned char*)this) 
187       + ROUND((sizeof (_Jv_InterpMethod)
188                + exc_count*sizeof (_Jv_InterpException)), 4);
189   }
190
191   _Jv_InterpException * exceptions ()
192   {
193     return (_Jv_InterpException*) (this+1);
194   }
195
196   static size_t size (int exc_count, int code_length)
197   {
198     return 
199       ROUND ((sizeof (_Jv_InterpMethod) 
200               + (exc_count * sizeof (_Jv_InterpException))), 4)
201       + code_length;
202   }
203
204   // return the method's invocation pointer (a stub).
205   void *ncode (jclass);
206   void compile (const void * const *);
207
208   static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
209   static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
210   static void run_class (ffi_cif*, void*, ffi_raw*, void*);
211   static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
212   
213   static void run_normal_debug (ffi_cif*, void*, ffi_raw*, void*);
214   static void run_synch_object_debug (ffi_cif*, void*, ffi_raw*, void*);
215   static void run_class_debug (ffi_cif*, void*, ffi_raw*, void*);
216   static void run_synch_class_debug (ffi_cif*, void*, ffi_raw*, void*);
217
218   static void run (void *, ffi_raw *, _Jv_InterpMethod *);
219   static void run_debug (void *, ffi_raw *, _Jv_InterpMethod *);
220   
221
222   
223   // Returns source file line number for given PC value, or -1 if line
224   // number info is unavailable.
225   int get_source_line(pc_t mpc);
226
227    public:
228
229   // Convenience function for indexing bytecode PC/insn slots in
230   // line tables for JDWP
231   jlong insn_index (pc_t pc);
232   
233   // Helper function used to check if there is a handler for an exception
234   // present at this code index
235   jboolean check_handler (pc_t *pc, _Jv_InterpMethod *meth,
236                      java::lang::Throwable *ex);
237    
238   /* Get the line table for this method.
239    * start  is the lowest index in the method
240    * end    is the  highest index in the method
241    * line_numbers is an array to hold the list of source line numbers
242    * code_indices is an array to hold the corresponding list of code indices
243    */
244   void get_line_table (jlong& start, jlong& end, jintArray& line_numbers,
245                        jlongArray& code_indices);
246   
247   int get_max_locals ()
248   {
249     return static_cast<int> (max_locals);
250   }
251   
252   /* Get info for a local variable of this method.
253    * If there is no loca_var_table for this method it will return -1.
254    * table_slot  indicates which slot in the local_var_table to get, if there is
255    * no variable at this location it will return 0.
256    * Otherwise, it will return the number of table slots after the selected
257    * slot, indexed from 0.
258    * 
259    * Example: there are 5 slots in the table, you request slot 0 so it will
260    * return 4.
261    */
262   int get_local_var_table (char **name, char **sig, char **generic_sig,
263                            jlong *startloc, jint *length, jint *slot,
264                            int table_slot);
265
266   /* Installs a break instruction at the given code index. Returns
267      the pc_t of the breakpoint or NULL if index is invalid. */
268   pc_t install_break (jlong index);
269
270   // Gets the instruction at the given index
271   pc_t get_insn (jlong index);
272
273   /* Writes the given instruction at the given code index. Returns
274      the insn or NULL if index is invalid. */
275   pc_t set_insn (jlong index, pc_t insn);
276
277 #ifdef DIRECT_THREADED
278   friend void _Jv_CompileMethod (_Jv_InterpMethod*);
279 #endif
280   
281   friend class _Jv_ClassReader;
282   friend class _Jv_BytecodeVerifier;
283   friend class _Jv_StackTrace;
284   friend class _Jv_InterpreterEngine;
285
286 #ifdef JV_MARKOBJ_DECL
287   friend JV_MARKOBJ_DECL;
288 #endif
289 };
290
291 class _Jv_InterpClass
292 {
293   _Jv_MethodBase **interpreted_methods;
294   _Jv_ushort     *field_initializers;
295   jstring source_file_name;
296   _Jv_ClosureList **closures;
297
298   friend class _Jv_ClassReader;
299   friend class _Jv_InterpMethod;
300   friend class _Jv_StackTrace;
301   friend class _Jv_InterpreterEngine;
302
303   friend void  _Jv_InitField (jobject, jclass, int);
304 #ifdef JV_MARKOBJ_DECL
305   friend JV_MARKOBJ_DECL;
306 #endif
307
308   friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
309   friend jstring _Jv_GetInterpClassSourceFile (jclass);
310 };
311
312 extern inline _Jv_MethodBase **
313 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
314 {
315   return klass->interpreted_methods;
316 }
317
318 struct _Jv_ResolvedMethod
319 {
320   jint            stack_item_count;     
321   jclass          klass;
322   _Jv_Method*     method;
323
324   // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
325   // to mark the resolved method to hold on to the cif.  Some memory could be
326   // saved by keeping a cache of cif's, since many will be the same.
327   ffi_cif         cif;
328   ffi_type *      arg_types[0];
329 };
330
331 class _Jv_JNIMethod : public _Jv_MethodBase
332 {
333   // The underlying function.  If NULL we have to look for the
334   // function.
335   void *function;
336
337   // This is the CIF used by the JNI function.
338   ffi_cif jni_cif;
339
340   // These are the argument types used by the JNI function.
341   ffi_type **jni_arg_types;
342
343   // This function is used when making a JNI call from the interpreter.
344   static void call (ffi_cif *, void *, ffi_raw *, void *);
345
346   void *ncode (jclass);
347
348   friend class _Jv_ClassReader;
349   friend class _Jv_InterpreterEngine;
350
351 #ifdef JV_MARKOBJ_DECL
352   friend JV_MARKOBJ_DECL;
353 #endif
354
355 public:
356   // FIXME: this is ugly.
357   void set_function (void *f)
358   {
359     function = f;
360   }
361 };
362
363 enum _Jv_FrameType
364 {
365   frame_native,
366   frame_interpreter,
367   frame_proxy
368 };
369
370 //  The composite call stack as represented by a linked list of frames
371 class _Jv_Frame
372 {
373 public:
374   java::lang::Thread *thread;
375
376   union
377   {
378     _Jv_MethodBase *self;
379     void *meth;
380     _Jv_Method *proxyMethod;
381   };
382   
383   //The full list of frames, JNI and interpreted
384   _Jv_Frame *next;
385   _Jv_FrameType frame_type;
386   
387   _Jv_Frame (_Jv_MethodBase *s, java::lang::Thread *thr, _Jv_FrameType type)
388   {
389     self = s;
390     frame_type = type;
391     next = (_Jv_Frame *) thr->frame;
392     thr->frame = (gnu::gcj::RawData *) this;
393     thread = thr;
394   }
395
396   ~_Jv_Frame ()
397   {
398     thread->frame = (gnu::gcj::RawData *) next;
399   }
400
401   int depth ()
402   {
403     int depth = 0;
404     struct _Jv_Frame *f;
405     for (f = this; f != NULL; f = f->next)
406       ++depth;
407
408     return depth;
409   }
410 };
411
412 // An interpreted frame in the call stack
413 class _Jv_InterpFrame : public _Jv_Frame
414 {
415 public:
416   
417   // Keep the purely interpreted list around so as not to break backtraces
418   _Jv_InterpFrame *next_interp;
419   
420   union
421   {
422     pc_t pc;
423     jclass proxyClass;
424   };
425
426   //Debug info for local variables.
427   _Jv_word *locals;
428   char *locals_type;
429
430   // Object pointer for this frame ("this")
431   jobject obj_ptr;
432
433   _Jv_InterpFrame (void *meth, java::lang::Thread *thr, jclass proxyCls = NULL)
434   : _Jv_Frame (reinterpret_cast<_Jv_MethodBase *> (meth), thr,
435                      frame_interpreter)
436   {
437     next_interp = (_Jv_InterpFrame *) thr->interp_frame;
438     proxyClass = proxyCls;
439     thr->interp_frame = (gnu::gcj::RawData *) this;
440     obj_ptr = NULL;
441   }
442
443   ~_Jv_InterpFrame ()
444   {
445     thread->interp_frame = (gnu::gcj::RawData *) next_interp;
446   }
447
448   jobject get_this_ptr ()
449   {
450     return obj_ptr;
451   } 
452 };
453
454 // A native frame in the call stack really just a placeholder
455 class _Jv_NativeFrame : public _Jv_Frame
456 {
457 public:
458
459   _Jv_NativeFrame (_Jv_JNIMethod *s, java::lang::Thread *thr)
460   : _Jv_Frame (s, thr, frame_native)
461   {
462   }
463 };
464
465 #endif /* INTERPRETER */
466
467 #endif /* __JAVA_INTERP_H__ */