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