+/* we put this one here, and not in interpret.cc because it
+ * calls the utility routines _Jv_count_arguments
+ * which are static to this module. The following struct defines the
+ * layout we use for the stubs, it's only used in the ncode method. */
+
+typedef struct {
+ ffi_raw_closure closure;
+ ffi_cif cif;
+ ffi_type *arg_types[0];
+} ncode_closure;
+
+typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
+
+void *
+_Jv_InterpMethod::ncode ()
+{
+ using namespace java::lang::reflect;
+
+ if (self->ncode != 0)
+ return self->ncode;
+
+ jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
+ int arg_count = _Jv_count_arguments (self->signature, staticp);
+
+ ncode_closure *closure =
+ (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
+ + arg_count * sizeof (ffi_type*));
+
+ init_cif (self->signature,
+ arg_count,
+ staticp,
+ &closure->cif,
+ &closure->arg_types[0],
+ NULL);
+
+ ffi_closure_fun fun;
+
+ args_raw_size = FFI_RAW_SIZE (&closure->cif);
+
+ JvAssert ((self->accflags & Modifier::NATIVE) == 0);
+
+ if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
+ {
+ if (staticp)
+ fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
+ else
+ fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
+ }
+ else
+ {
+ if (staticp)
+ fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
+ else
+ fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
+ }
+
+ FFI_PREP_RAW_CLOSURE (&closure->closure,
+ &closure->cif,
+ fun,
+ (void*)this);
+
+ self->ncode = (void*)closure;
+ return self->ncode;
+}
+
+void *
+_Jv_JNIMethod::ncode ()
+{
+ using namespace java::lang::reflect;
+
+ if (self->ncode != 0)
+ return self->ncode;
+
+ jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
+ int arg_count = _Jv_count_arguments (self->signature, staticp);
+
+ ncode_closure *closure =
+ (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
+ + arg_count * sizeof (ffi_type*));
+
+ ffi_type *rtype;
+ init_cif (self->signature,
+ arg_count,
+ staticp,
+ &closure->cif,
+ &closure->arg_types[0],
+ &rtype);
+
+ ffi_closure_fun fun;
+
+ args_raw_size = FFI_RAW_SIZE (&closure->cif);
+
+ // Initialize the argument types and CIF that represent the actual
+ // underlying JNI function.
+ int extra_args = 1;
+ if ((self->accflags & Modifier::STATIC))
+ ++extra_args;
+ jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
+ * sizeof (ffi_type *));
+ int offset = 0;
+ jni_arg_types[offset++] = &ffi_type_pointer;
+ if ((self->accflags & Modifier::STATIC))
+ jni_arg_types[offset++] = &ffi_type_pointer;
+ memcpy (&jni_arg_types[offset], &closure->arg_types[0],
+ arg_count * sizeof (ffi_type *));
+
+ if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
+ extra_args + arg_count, rtype,
+ jni_arg_types) != FFI_OK)
+ throw_internal_error ("ffi_prep_cif failed for JNI function");
+
+ JvAssert ((self->accflags & Modifier::NATIVE) != 0);
+
+ // FIXME: for now we assume that all native methods for
+ // interpreted code use JNI.
+ fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
+
+ FFI_PREP_RAW_CLOSURE (&closure->closure,
+ &closure->cif,
+ fun,
+ (void*) this);
+
+ self->ncode = (void *) closure;
+ return self->ncode;
+}
+
+static void
+throw_class_format_error (jstring msg)
+{
+ throw (msg
+ ? new java::lang::ClassFormatError (msg)
+ : new java::lang::ClassFormatError);
+}
+
+static void
+throw_class_format_error (char *msg)
+{
+ throw_class_format_error (JvNewStringLatin1 (msg));
+}
+
+\f
+
+void
+_Jv_InterpreterEngine::do_verify (jclass klass)
+{
+ _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+ for (int i = 0; i < klass->method_count; i++)
+ {
+ using namespace java::lang::reflect;
+ _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
+ _Jv_ushort accflags = klass->methods[i].accflags;
+ if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
+ {
+ _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
+ _Jv_VerifyMethod (im);
+ }
+ }
+}
+
+void
+_Jv_InterpreterEngine::do_create_ncode (jclass klass)
+{
+ _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+ for (int i = 0; i < klass->method_count; i++)
+ {
+ // Just skip abstract methods. This is particularly important
+ // because we don't resize the interpreted_methods array when
+ // miranda methods are added to it.
+ if ((klass->methods[i].accflags
+ & java::lang::reflect::Modifier::ABSTRACT)
+ != 0)
+ continue;
+
+ _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
+
+ if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
+ != 0)
+ {
+ // You might think we could use a virtual `ncode' method in
+ // the _Jv_MethodBase and unify the native and non-native
+ // cases. Well, we can't, because we don't allocate these
+ // objects using `new', and thus they don't get a vtable.
+ _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
+ klass->methods[i].ncode = jnim->ncode ();
+ }
+ else if (imeth != 0) // it could be abstract
+ {
+ _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
+ klass->methods[i].ncode = im->ncode ();
+ }
+ }
+}
+
+void
+_Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
+ int static_size)
+{
+ _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+
+ char *static_data = (char *) _Jv_AllocBytes (static_size);
+
+ for (int i = 0; i < klass->field_count; i++)
+ {
+ _Jv_Field *field = &klass->fields[i];
+
+ if ((field->flags & java::lang::reflect::Modifier::STATIC) != 0)
+ {
+ field->u.addr = static_data + field->u.boffset;
+
+ if (iclass->field_initializers[i] != 0)
+ {
+ _Jv_Linker::resolve_field (field, klass->loader);
+ _Jv_InitField (0, klass, i);
+ }
+ }
+ }
+
+ // Now we don't need the field_initializers anymore, so let the
+ // collector get rid of it.
+ iclass->field_initializers = 0;
+}
+
+_Jv_ResolvedMethod *
+_Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
+ jboolean staticp, jint vtable_index)
+{
+ int arg_count = _Jv_count_arguments (method->signature, staticp);
+
+ _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
+ _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
+ + arg_count*sizeof (ffi_type*));
+
+ result->stack_item_count
+ = init_cif (method->signature,
+ arg_count,
+ staticp,
+ &result->cif,
+ &result->arg_types[0],
+ NULL);
+
+ result->vtable_index = vtable_index;
+ result->method = method;
+ result->klass = klass;
+
+ return result;
+}
+
+void
+_Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
+{
+ _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
+ for (int i = 0; i < klass->method_count; i++)
+ {
+ // Just skip abstract methods. This is particularly important
+ // because we don't resize the interpreted_methods array when
+ // miranda methods are added to it.
+ if ((klass->methods[i].accflags
+ & java::lang::reflect::Modifier::ABSTRACT)
+ != 0)
+ continue;
+ // Miranda method additions mean that the `methods' array moves.
+ // We cache a pointer into this array, so we have to update.
+ iclass->interpreted_methods[i]->self = &klass->methods[i];
+ }
+}