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