OSDN Git Service

* configure.in, configure: enable dwarf2-exception-style
[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  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 void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
37
38 void _Jv_InitField (jobject, jclass, int);
39 void * _Jv_AllocMethodInvocation (jsize size);
40 int  _Jv_count_arguments (_Jv_Utf8Const *signature,
41                           jboolean staticp = true);
42 void _Jv_VerifyMethod (_Jv_InterpMethod *method);
43
44 /* FIXME: this should really be defined in some more generic place */
45 #define ROUND(V, A) (((((unsigned) (V))-1) | ((A)-1))+1)
46
47 /* the interpreter is written in C++, primarily because it makes it easy for
48  * the entire thing to be "friend" with class Class. */
49
50 class _Jv_InterpClass;
51 class _Jv_InterpMethod;
52 class _Jv_InterpMethodInvocation;
53
54 class _Jv_InterpException
55 {
56   int  start_pc;
57   int  end_pc;
58   int  handler_pc;
59   int  handler_type;
60
61   friend class _Jv_ClassReader;
62   friend class _Jv_InterpMethod;
63   friend class _Jv_BytecodeVerifier;
64 };
65
66 // Base class for method representations.  Subclasses are interpreted
67 // and JNI methods.
68 class _Jv_MethodBase
69 {
70 protected:
71   // The class which defined this method.
72   _Jv_InterpClass *defining_class;
73
74   // The method description.
75   _Jv_Method *self;
76
77   // Size of raw arguments.
78   _Jv_ushort args_raw_size;
79
80 public:
81   _Jv_Method *get_method ()
82   {
83     return self;
84   }
85 };
86
87 class _Jv_InterpMethod : public _Jv_MethodBase
88 {
89   _Jv_ushort       max_stack;
90   _Jv_ushort       max_locals;
91   int              code_length;
92
93   _Jv_ushort       exc_count;
94
95   unsigned char* bytecode () 
96   {
97     return 
98       ((unsigned char*)this) 
99       + ROUND((sizeof (_Jv_InterpMethod)
100                + exc_count*sizeof (_Jv_InterpException)), 4);
101   }
102     
103   _Jv_InterpException * exceptions ()
104   {
105     return (_Jv_InterpException*) (this+1);
106   }
107
108   static size_t size (int exc_count, int code_length)
109   {
110     return 
111       ROUND ((sizeof (_Jv_InterpMethod) 
112               + (exc_count * sizeof (_Jv_InterpException))), 4)
113       + code_length;
114   }
115
116   // return the method's invocation pointer (a stub).
117   void *ncode ();
118   void continue1 (_Jv_InterpMethodInvocation *inv);
119
120   static void run_normal (ffi_cif*, void*, ffi_raw*, void*);
121   static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*);
122   static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*);
123
124   inline jobject run (ffi_cif*, void*, ffi_raw*, 
125                       _Jv_InterpMethodInvocation*);
126
127   bool find_exception (jobject ex,
128                        _Jv_InterpMethodInvocation *inv);
129
130  public:
131   static void dump_object(jobject o);
132
133   friend class _Jv_ClassReader;
134   friend class _Jv_InterpMethodInvocation;
135   friend class _Jv_BytecodeVerifier;
136
137   friend void _Jv_PrepareClass(jclass);
138 };
139
140 class _Jv_InterpMethodInvocation {
141   _Jv_InterpMethod *running;
142   _Jv_word         *sp;
143   unsigned char    *pc;
144   _Jv_word          state[0];
145
146   _Jv_word*         stack_base () { return &state[0]; }
147   _Jv_word*         local_base () { return &state[running->max_stack]; }
148
149   friend class _Jv_InterpMethod;
150 };
151   
152 class _Jv_InterpClass : public java::lang::Class
153 {
154   _Jv_MethodBase **interpreted_methods;
155   _Jv_ushort        *field_initializers;
156
157   friend class _Jv_ClassReader;
158   friend class _Jv_InterpMethod;
159   friend void  _Jv_PrepareClass(jclass);
160   friend void  _Jv_InitField (jobject, jclass, int);
161 #ifdef JV_MARKOBJ_DECL
162   friend JV_MARKOBJ_DECL;
163 #endif
164
165   friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass);
166 };
167
168 extern inline _Jv_MethodBase **
169 _Jv_GetFirstMethod (_Jv_InterpClass *klass)
170 {
171   return klass->interpreted_methods;
172 }
173
174 struct _Jv_ResolvedMethod {
175   jint            stack_item_count;     
176   jint            vtable_index; 
177   jclass          klass;
178   _Jv_Method*     method;
179
180   // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs
181   // to mark the resolved method to hold on to the cif.  Some memory could be
182   // saved by keeping a cache of cif's, since many will be the same.
183   ffi_cif         cif;
184   ffi_type *      arg_types[0];
185 };
186
187 class _Jv_JNIMethod : public _Jv_MethodBase
188 {
189   // The underlying function.  If NULL we have to look for the
190   // function.
191   void *function;
192
193   // This is the CIF used by the JNI function.
194   ffi_cif jni_cif;
195
196   // These are the argument types used by the JNI function.
197   ffi_type **jni_arg_types;
198
199   // This function is used when making a JNI call from the interpreter.
200   static void call (ffi_cif *, void *, ffi_raw *, void *);
201
202   void *ncode ();
203
204   friend class _Jv_ClassReader;
205   friend void _Jv_PrepareClass(jclass);
206
207 public:
208   // FIXME: this is ugly.
209   void set_function (void *f)
210   {
211     function = f;
212   }
213 };
214
215 #endif /* INTERPRETER */
216
217 #endif /* __JAVA_INTERP_H__ */