| 1 | // java-interp.h - Header file for the bytecode interpreter.  -*- c++ -*- | 
|---|
| 2 |  | 
|---|
| 3 | /* Copyright (C) 1999, 2000, 2001, 2002  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 | #include <gnu/gcj/runtime/NameFinder.h> | 
|---|
| 17 |  | 
|---|
| 18 | #ifdef INTERPRETER | 
|---|
| 19 |  | 
|---|
| 20 | #pragma interface | 
|---|
| 21 |  | 
|---|
| 22 | #include <java/lang/Class.h> | 
|---|
| 23 | #include <java/lang/ClassLoader.h> | 
|---|
| 24 | #include <java/lang/reflect/Modifier.h> | 
|---|
| 25 | #include <gnu/gcj/runtime/StackTrace.h> | 
|---|
| 26 |  | 
|---|
| 27 | extern "C" { | 
|---|
| 28 | #include <ffi.h> | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | extern inline jboolean | 
|---|
| 32 | _Jv_IsInterpretedClass (jclass c) | 
|---|
| 33 | { | 
|---|
| 34 | return (c->accflags & java::lang::reflect::Modifier::INTERPRETED) != 0; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | struct _Jv_ResolvedMethod; | 
|---|
| 38 |  | 
|---|
| 39 | void _Jv_DefineClass (jclass, jbyteArray, jint, jint); | 
|---|
| 40 |  | 
|---|
| 41 | void _Jv_InitField (jobject, jclass, int); | 
|---|
| 42 | void * _Jv_AllocMethodInvocation (jsize size); | 
|---|
| 43 | int  _Jv_count_arguments (_Jv_Utf8Const *signature, | 
|---|
| 44 | jboolean staticp = true); | 
|---|
| 45 | void _Jv_VerifyMethod (_Jv_InterpMethod *method); | 
|---|
| 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 |  | 
|---|
| 56 | // Before a method is "compiled" we store values as the bytecode PC, | 
|---|
| 57 | // an int.  Afterwards we store them as pointers into the prepared | 
|---|
| 58 | // code itself. | 
|---|
| 59 | union _Jv_InterpPC | 
|---|
| 60 | { | 
|---|
| 61 | int i; | 
|---|
| 62 | void *p; | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | class _Jv_InterpException | 
|---|
| 66 | { | 
|---|
| 67 | _Jv_InterpPC start_pc; | 
|---|
| 68 | _Jv_InterpPC end_pc; | 
|---|
| 69 | _Jv_InterpPC handler_pc; | 
|---|
| 70 | _Jv_InterpPC handler_type; | 
|---|
| 71 |  | 
|---|
| 72 | friend class _Jv_ClassReader; | 
|---|
| 73 | friend class _Jv_InterpMethod; | 
|---|
| 74 | friend class _Jv_BytecodeVerifier; | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | // Base class for method representations.  Subclasses are interpreted | 
|---|
| 78 | // and JNI methods. | 
|---|
| 79 | class _Jv_MethodBase | 
|---|
| 80 | { | 
|---|
| 81 | protected: | 
|---|
| 82 | // The class which defined this method. | 
|---|
| 83 | _Jv_InterpClass *defining_class; | 
|---|
| 84 |  | 
|---|
| 85 | // The method description. | 
|---|
| 86 | _Jv_Method *self; | 
|---|
| 87 |  | 
|---|
| 88 | // Size of raw arguments. | 
|---|
| 89 | _Jv_ushort args_raw_size; | 
|---|
| 90 |  | 
|---|
| 91 | public: | 
|---|
| 92 | _Jv_Method *get_method () | 
|---|
| 93 | { | 
|---|
| 94 | return self; | 
|---|
| 95 | } | 
|---|
| 96 | }; | 
|---|
| 97 |  | 
|---|
| 98 | class _Jv_InterpMethod : public _Jv_MethodBase | 
|---|
| 99 | { | 
|---|
| 100 | _Jv_ushort       max_stack; | 
|---|
| 101 | _Jv_ushort       max_locals; | 
|---|
| 102 | int              code_length; | 
|---|
| 103 |  | 
|---|
| 104 | _Jv_ushort       exc_count; | 
|---|
| 105 |  | 
|---|
| 106 | void *prepared; | 
|---|
| 107 |  | 
|---|
| 108 | unsigned char* bytecode () | 
|---|
| 109 | { | 
|---|
| 110 | return | 
|---|
| 111 | ((unsigned char*)this) | 
|---|
| 112 | + ROUND((sizeof (_Jv_InterpMethod) | 
|---|
| 113 | + exc_count*sizeof (_Jv_InterpException)), 4); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | _Jv_InterpException * exceptions () | 
|---|
| 117 | { | 
|---|
| 118 | return (_Jv_InterpException*) (this+1); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | static size_t size (int exc_count, int code_length) | 
|---|
| 122 | { | 
|---|
| 123 | return | 
|---|
| 124 | ROUND ((sizeof (_Jv_InterpMethod) | 
|---|
| 125 | + (exc_count * sizeof (_Jv_InterpException))), 4) | 
|---|
| 126 | + code_length; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | // return the method's invocation pointer (a stub). | 
|---|
| 130 | void *ncode (); | 
|---|
| 131 | void compile (const void * const *); | 
|---|
| 132 |  | 
|---|
| 133 | static void run_normal (ffi_cif*, void*, ffi_raw*, void*); | 
|---|
| 134 | static void run_synch_object (ffi_cif*, void*, ffi_raw*, void*); | 
|---|
| 135 | static void run_synch_class (ffi_cif*, void*, ffi_raw*, void*); | 
|---|
| 136 |  | 
|---|
| 137 | void run (void*, ffi_raw *); | 
|---|
| 138 |  | 
|---|
| 139 | public: | 
|---|
| 140 | static void dump_object(jobject o); | 
|---|
| 141 |  | 
|---|
| 142 | friend class _Jv_ClassReader; | 
|---|
| 143 | friend class _Jv_BytecodeVerifier; | 
|---|
| 144 | friend class gnu::gcj::runtime::NameFinder; | 
|---|
| 145 | friend class gnu::gcj::runtime::StackTrace; | 
|---|
| 146 |  | 
|---|
| 147 | friend void _Jv_PrepareClass(jclass); | 
|---|
| 148 |  | 
|---|
| 149 | #ifdef JV_MARKOBJ_DECL | 
|---|
| 150 | friend JV_MARKOBJ_DECL; | 
|---|
| 151 | #endif | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 | class _Jv_InterpClass : public java::lang::Class | 
|---|
| 155 | { | 
|---|
| 156 | _Jv_MethodBase **interpreted_methods; | 
|---|
| 157 | _Jv_ushort        *field_initializers; | 
|---|
| 158 |  | 
|---|
| 159 | friend class _Jv_ClassReader; | 
|---|
| 160 | friend class _Jv_InterpMethod; | 
|---|
| 161 | friend void  _Jv_PrepareClass(jclass); | 
|---|
| 162 | friend void  _Jv_PrepareMissingMethods (jclass base2, jclass iface_class); | 
|---|
| 163 | friend void  _Jv_InitField (jobject, jclass, int); | 
|---|
| 164 | #ifdef JV_MARKOBJ_DECL | 
|---|
| 165 | friend JV_MARKOBJ_DECL; | 
|---|
| 166 | #endif | 
|---|
| 167 |  | 
|---|
| 168 | friend _Jv_MethodBase ** _Jv_GetFirstMethod (_Jv_InterpClass *klass); | 
|---|
| 169 | }; | 
|---|
| 170 |  | 
|---|
| 171 | extern inline _Jv_MethodBase ** | 
|---|
| 172 | _Jv_GetFirstMethod (_Jv_InterpClass *klass) | 
|---|
| 173 | { | 
|---|
| 174 | return klass->interpreted_methods; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | struct _Jv_ResolvedMethod { | 
|---|
| 178 | jint            stack_item_count; | 
|---|
| 179 | jint            vtable_index; | 
|---|
| 180 | jclass          klass; | 
|---|
| 181 | _Jv_Method*     method; | 
|---|
| 182 |  | 
|---|
| 183 | // a resolved method holds the cif in-line, so that _Jv_MarkObj just needs | 
|---|
| 184 | // to mark the resolved method to hold on to the cif.  Some memory could be | 
|---|
| 185 | // saved by keeping a cache of cif's, since many will be the same. | 
|---|
| 186 | ffi_cif         cif; | 
|---|
| 187 | ffi_type *      arg_types[0]; | 
|---|
| 188 | }; | 
|---|
| 189 |  | 
|---|
| 190 | class _Jv_JNIMethod : public _Jv_MethodBase | 
|---|
| 191 | { | 
|---|
| 192 | // The underlying function.  If NULL we have to look for the | 
|---|
| 193 | // function. | 
|---|
| 194 | void *function; | 
|---|
| 195 |  | 
|---|
| 196 | // This is the CIF used by the JNI function. | 
|---|
| 197 | ffi_cif jni_cif; | 
|---|
| 198 |  | 
|---|
| 199 | // These are the argument types used by the JNI function. | 
|---|
| 200 | ffi_type **jni_arg_types; | 
|---|
| 201 |  | 
|---|
| 202 | // This function is used when making a JNI call from the interpreter. | 
|---|
| 203 | static void call (ffi_cif *, void *, ffi_raw *, void *); | 
|---|
| 204 |  | 
|---|
| 205 | void *ncode (); | 
|---|
| 206 |  | 
|---|
| 207 | friend class _Jv_ClassReader; | 
|---|
| 208 | friend void _Jv_PrepareClass(jclass); | 
|---|
| 209 |  | 
|---|
| 210 | public: | 
|---|
| 211 | // FIXME: this is ugly. | 
|---|
| 212 | void set_function (void *f) | 
|---|
| 213 | { | 
|---|
| 214 | function = f; | 
|---|
| 215 | } | 
|---|
| 216 | }; | 
|---|
| 217 |  | 
|---|
| 218 | // A structure of this type is used to link together interpreter | 
|---|
| 219 | // invocations on the stack. | 
|---|
| 220 | struct _Jv_MethodChain | 
|---|
| 221 | { | 
|---|
| 222 | const _Jv_InterpMethod *self; | 
|---|
| 223 | _Jv_MethodChain **ptr; | 
|---|
| 224 | _Jv_MethodChain *next; | 
|---|
| 225 |  | 
|---|
| 226 | _Jv_MethodChain (const _Jv_InterpMethod *s, _Jv_MethodChain **n) | 
|---|
| 227 | { | 
|---|
| 228 | self = s; | 
|---|
| 229 | ptr = n; | 
|---|
| 230 | next = *n; | 
|---|
| 231 | *n = this; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | ~_Jv_MethodChain () | 
|---|
| 235 | { | 
|---|
| 236 | *ptr = next; | 
|---|
| 237 | } | 
|---|
| 238 | }; | 
|---|
| 239 |  | 
|---|
| 240 | #endif /* INTERPRETER */ | 
|---|
| 241 |  | 
|---|
| 242 | #endif /* __JAVA_INTERP_H__ */ | 
|---|