OSDN Git Service

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