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