Patch for PR 9255
[official-gcc.git] / libjava / resolve.cc
blobaa17ea9c5f0b696f9bf5fd7fcc550a11afef7521
1 // resolve.cc - Code for linking and resolving classes and pool entries.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
13 #include <config.h>
15 #include <java-interp.h>
17 #include <jvm.h>
18 #include <gcj/cni.h>
19 #include <string.h>
20 #include <java-cpool.h>
21 #include <java/lang/Class.h>
22 #include <java/lang/String.h>
23 #include <java/lang/StringBuffer.h>
24 #include <java/lang/Thread.h>
25 #include <java/lang/InternalError.h>
26 #include <java/lang/VirtualMachineError.h>
27 #include <java/lang/NoSuchFieldError.h>
28 #include <java/lang/NoSuchMethodError.h>
29 #include <java/lang/ClassFormatError.h>
30 #include <java/lang/IllegalAccessError.h>
31 #include <java/lang/AbstractMethodError.h>
32 #include <java/lang/NoClassDefFoundError.h>
33 #include <java/lang/IncompatibleClassChangeError.h>
34 #include <java/lang/reflect/Modifier.h>
36 using namespace gcj;
38 void
39 _Jv_ResolveField (_Jv_Field *field, java::lang::ClassLoader *loader)
41 if (! field->isResolved ())
43 _Jv_Utf8Const *sig = (_Jv_Utf8Const*)field->type;
44 field->type = _Jv_FindClassFromSignature (sig->data, loader);
45 field->flags &= ~_Jv_FIELD_UNRESOLVED_FLAG;
49 #ifdef INTERPRETER
51 static void throw_internal_error (char *msg)
52 __attribute__ ((__noreturn__));
53 static void throw_class_format_error (jstring msg)
54 __attribute__ ((__noreturn__));
55 static void throw_class_format_error (char *msg)
56 __attribute__ ((__noreturn__));
58 static int get_alignment_from_class (jclass);
60 static _Jv_ResolvedMethod*
61 _Jv_BuildResolvedMethod (_Jv_Method*,
62 jclass,
63 jboolean,
64 jint);
67 static void throw_incompatible_class_change_error (jstring msg)
69 throw new java::lang::IncompatibleClassChangeError (msg);
72 _Jv_word
73 _Jv_ResolvePoolEntry (jclass klass, int index)
75 using namespace java::lang::reflect;
77 _Jv_Constants *pool = &klass->constants;
79 if ((pool->tags[index] & JV_CONSTANT_ResolvedFlag) != 0)
80 return pool->data[index];
82 switch (pool->tags[index]) {
83 case JV_CONSTANT_Class:
85 _Jv_Utf8Const *name = pool->data[index].utf8;
87 jclass found;
88 if (name->data[0] == '[')
89 found = _Jv_FindClassFromSignature (&name->data[0],
90 klass->loader);
91 else
92 found = _Jv_FindClass (name, klass->loader);
94 if (! found)
96 jstring str = _Jv_NewStringUTF (name->data);
97 // This exception is specified in JLS 2nd Ed, section 5.1.
98 throw new java::lang::NoClassDefFoundError (str);
101 if ((found->accflags & Modifier::PUBLIC) == Modifier::PUBLIC
102 || (_Jv_ClassNameSamePackage (found->name,
103 klass->name)))
105 pool->data[index].clazz = found;
106 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
108 else
110 throw new java::lang::IllegalAccessError (found->getName());
113 break;
115 case JV_CONSTANT_String:
117 jstring str;
118 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
119 pool->data[index].o = str;
120 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
122 break;
125 case JV_CONSTANT_Fieldref:
127 _Jv_ushort class_index, name_and_type_index;
128 _Jv_loadIndexes (&pool->data[index],
129 class_index,
130 name_and_type_index);
131 jclass owner = (_Jv_ResolvePoolEntry (klass, class_index)).clazz;
133 if (owner != klass)
134 _Jv_InitClass (owner);
136 _Jv_ushort name_index, type_index;
137 _Jv_loadIndexes (&pool->data[name_and_type_index],
138 name_index,
139 type_index);
141 _Jv_Utf8Const *field_name = pool->data[name_index].utf8;
142 _Jv_Utf8Const *field_type_name = pool->data[type_index].utf8;
144 // FIXME: The implementation of this function
145 // (_Jv_FindClassFromSignature) will generate an instance of
146 // _Jv_Utf8Const for each call if the field type is a class name
147 // (Lxx.yy.Z;). This may be too expensive to do for each and
148 // every fieldref being resolved. For now, we fix the problem by
149 // only doing it when we have a loader different from the class
150 // declaring the field.
152 jclass field_type = 0;
154 if (owner->loader != klass->loader)
155 field_type = _Jv_FindClassFromSignature (field_type_name->data,
156 klass->loader);
158 _Jv_Field* the_field = 0;
160 for (jclass cls = owner; cls != 0; cls = cls->getSuperclass ())
162 for (int i = 0; i < cls->field_count; i++)
164 _Jv_Field *field = &cls->fields[i];
165 if (! _Jv_equalUtf8Consts (field->name, field_name))
166 continue;
168 // now, check field access.
170 if ( (cls == klass)
171 || ((field->flags & Modifier::PUBLIC) != 0)
172 || (((field->flags & Modifier::PROTECTED) != 0)
173 && cls->isAssignableFrom (klass))
174 || (((field->flags & Modifier::PRIVATE) == 0)
175 && _Jv_ClassNameSamePackage (cls->name,
176 klass->name)))
178 /* resove the field using the class' own loader
179 if necessary */
181 if (!field->isResolved ())
182 _Jv_ResolveField (field, cls->loader);
184 if (field_type != 0 && field->type != field_type)
185 throw new java::lang::LinkageError
186 (JvNewStringLatin1
187 ("field type mismatch with different loaders"));
189 the_field = field;
190 goto end_of_field_search;
192 else
194 throw new java::lang::IllegalAccessError;
199 end_of_field_search:
200 if (the_field == 0)
202 java::lang::StringBuffer *sb = new java::lang::StringBuffer();
203 sb->append(JvNewStringLatin1("field "));
204 sb->append(owner->getName());
205 sb->append(JvNewStringLatin1("."));
206 sb->append(_Jv_NewStringUTF(field_name->data));
207 sb->append(JvNewStringLatin1(" was not found."));
208 throw_incompatible_class_change_error(sb->toString());
211 pool->data[index].field = the_field;
212 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
214 break;
216 case JV_CONSTANT_Methodref:
217 case JV_CONSTANT_InterfaceMethodref:
219 _Jv_ushort class_index, name_and_type_index;
220 _Jv_loadIndexes (&pool->data[index],
221 class_index,
222 name_and_type_index);
223 jclass owner = (_Jv_ResolvePoolEntry (klass, class_index)).clazz;
225 if (owner != klass)
226 _Jv_InitClass (owner);
228 _Jv_ushort name_index, type_index;
229 _Jv_loadIndexes (&pool->data[name_and_type_index],
230 name_index,
231 type_index);
233 _Jv_Utf8Const *method_name = pool->data[name_index].utf8;
234 _Jv_Utf8Const *method_signature = pool->data[type_index].utf8;
236 _Jv_Method *the_method = 0;
237 jclass found_class = 0;
239 // First search the class itself.
240 the_method = _Jv_SearchMethodInClass (owner, klass,
241 method_name, method_signature);
243 if (the_method != 0)
245 found_class = owner;
246 goto end_of_method_search;
249 // If we are resolving an interface method, search the
250 // interface's superinterfaces (A superinterface is not an
251 // interface's superclass - a superinterface is implemented by
252 // the interface).
253 if (pool->tags[index] == JV_CONSTANT_InterfaceMethodref)
255 _Jv_ifaces ifaces;
256 ifaces.count = 0;
257 ifaces.len = 4;
258 ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
260 _Jv_GetInterfaces (owner, &ifaces);
262 for (int i = 0; i < ifaces.count; i++)
264 jclass cls = ifaces.list[i];
265 the_method = _Jv_SearchMethodInClass (cls, klass, method_name,
266 method_signature);
267 if (the_method != 0)
269 found_class = cls;
270 break;
274 _Jv_Free (ifaces.list);
276 if (the_method != 0)
277 goto end_of_method_search;
280 // Finally, search superclasses.
281 for (jclass cls = owner->getSuperclass (); cls != 0;
282 cls = cls->getSuperclass ())
284 the_method = _Jv_SearchMethodInClass (cls, klass,
285 method_name, method_signature);
286 if (the_method != 0)
288 found_class = cls;
289 break;
293 end_of_method_search:
295 // FIXME: if (cls->loader != klass->loader), then we
296 // must actually check that the types of arguments
297 // correspond. That is, for each argument type, and
298 // the return type, doing _Jv_FindClassFromSignature
299 // with either loader should produce the same result,
300 // i.e., exactly the same jclass object. JVMS 5.4.3.3
302 if (the_method == 0)
304 java::lang::StringBuffer *sb = new java::lang::StringBuffer();
305 sb->append(JvNewStringLatin1("method "));
306 sb->append(owner->getName());
307 sb->append(JvNewStringLatin1("."));
308 sb->append(_Jv_NewStringUTF(method_name->data));
309 sb->append(JvNewStringLatin1(" was not found."));
310 throw new java::lang::NoSuchMethodError (sb->toString());
313 int vtable_index = -1;
314 if (pool->tags[index] != JV_CONSTANT_InterfaceMethodref)
315 vtable_index = (jshort)the_method->index;
317 pool->data[index].rmethod =
318 _Jv_BuildResolvedMethod(the_method,
319 found_class,
320 (the_method->accflags & Modifier::STATIC) != 0,
321 vtable_index);
322 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
324 break;
328 return pool->data[index];
331 // Find a method declared in the cls that is referenced from klass and
332 // perform access checks.
333 _Jv_Method *
334 _Jv_SearchMethodInClass (jclass cls, jclass klass,
335 _Jv_Utf8Const *method_name,
336 _Jv_Utf8Const *method_signature)
338 using namespace java::lang::reflect;
340 for (int i = 0; i < cls->method_count; i++)
342 _Jv_Method *method = &cls->methods[i];
343 if ( (!_Jv_equalUtf8Consts (method->name,
344 method_name))
345 || (!_Jv_equalUtf8Consts (method->signature,
346 method_signature)))
347 continue;
349 if (cls == klass
350 || ((method->accflags & Modifier::PUBLIC) != 0)
351 || (((method->accflags & Modifier::PROTECTED) != 0)
352 && cls->isAssignableFrom (klass))
353 || (((method->accflags & Modifier::PRIVATE) == 0)
354 && _Jv_ClassNameSamePackage (cls->name,
355 klass->name)))
357 return method;
359 else
361 throw new java::lang::IllegalAccessError;
364 return 0;
367 // A helper for _Jv_PrepareClass. This adds missing `Miranda methods'
368 // to a class.
369 void
370 _Jv_PrepareMissingMethods (jclass base2, jclass iface_class)
372 _Jv_InterpClass *base = reinterpret_cast<_Jv_InterpClass *> (base2);
373 for (int i = 0; i < iface_class->interface_count; ++i)
375 for (int j = 0; j < iface_class->interfaces[i]->method_count; ++j)
377 _Jv_Method *meth = &iface_class->interfaces[i]->methods[j];
378 // Don't bother with <clinit>.
379 if (meth->name->data[0] == '<')
380 continue;
381 _Jv_Method *new_meth = _Jv_LookupDeclaredMethod (base, meth->name,
382 meth->signature);
383 if (! new_meth)
385 // We assume that such methods are very unlikely, so we
386 // just reallocate the method array each time one is
387 // found. This greatly simplifies the searching --
388 // otherwise we have to make sure that each such method
389 // found is really unique among all superinterfaces.
390 int new_count = base->method_count + 1;
391 _Jv_Method *new_m
392 = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method)
393 * new_count);
394 memcpy (new_m, base->methods,
395 sizeof (_Jv_Method) * base->method_count);
397 // Add new method.
398 new_m[base->method_count] = *meth;
399 new_m[base->method_count].index = (_Jv_ushort) -1;
400 new_m[base->method_count].accflags
401 |= java::lang::reflect::Modifier::INVISIBLE;
403 _Jv_MethodBase **new_im
404 = (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *)
405 * new_count);
406 memcpy (new_im, base->interpreted_methods,
407 sizeof (_Jv_MethodBase *) * base->method_count);
409 base->methods = new_m;
410 base->interpreted_methods = new_im;
411 base->method_count = new_count;
415 _Jv_PrepareMissingMethods (base, iface_class->interfaces[i]);
419 void
420 _Jv_PrepareClass(jclass klass)
422 using namespace java::lang::reflect;
425 * The job of this function is to: 1) assign storage to fields, and 2)
426 * build the vtable. static fields are assigned real memory, instance
427 * fields are assigned offsets.
429 * NOTE: we have a contract with the garbage collector here. Static
430 * reference fields must not be resolved, until after they have storage
431 * assigned which is the check used by the collector to see if it
432 * should indirect the static field reference and mark the object
433 * pointed to.
435 * Most fields are resolved lazily (i.e. have their class-type
436 * assigned) when they are accessed the first time by calling as part
437 * of _Jv_ResolveField, which is allways called after _Jv_PrepareClass.
438 * Static fields with initializers are resolved as part of this
439 * function, as are fields with primitive types.
442 if (! _Jv_IsInterpretedClass (klass))
443 return;
445 if (klass->state >= JV_STATE_PREPARED)
446 return;
448 // Make sure super-class is linked. This involves taking a lock on
449 // the super class, so we use the Java method resolveClass, which
450 // will unlock it properly, should an exception happen. If there's
451 // no superclass, do nothing -- Object will already have been
452 // resolved.
454 if (klass->superclass)
455 java::lang::ClassLoader::resolveClass0 (klass->superclass);
457 _Jv_InterpClass *clz = (_Jv_InterpClass*)klass;
459 /************ PART ONE: OBJECT LAYOUT ***************/
461 int instance_size;
462 int static_size;
464 // Although java.lang.Object is never interpreted, an interface can
465 // have a null superclass.
466 if (clz->superclass)
467 instance_size = clz->superclass->size();
468 else
469 instance_size = java::lang::Object::class$.size();
470 static_size = 0;
472 for (int i = 0; i < clz->field_count; i++)
474 int field_size;
475 int field_align;
477 _Jv_Field *field = &clz->fields[i];
479 if (! field->isRef ())
481 // it's safe to resolve the field here, since it's
482 // a primitive class, which does not cause loading to happen.
483 _Jv_ResolveField (field, clz->loader);
485 field_size = field->type->size ();
486 field_align = get_alignment_from_class (field->type);
488 else
490 field_size = sizeof (jobject);
491 field_align = __alignof__ (jobject);
494 #ifndef COMPACT_FIELDS
495 field->bsize = field_size;
496 #endif
498 if (field->flags & Modifier::STATIC)
500 /* this computes an offset into a region we'll allocate
501 shortly, and then add this offset to the start address */
503 static_size = ROUND (static_size, field_align);
504 field->u.boffset = static_size;
505 static_size += field_size;
507 else
509 instance_size = ROUND (instance_size, field_align);
510 field->u.boffset = instance_size;
511 instance_size += field_size;
515 // set the instance size for the class
516 clz->size_in_bytes = instance_size;
518 // allocate static memory
519 if (static_size != 0)
521 char *static_data = (char*)_Jv_AllocBytes (static_size);
523 memset (static_data, 0, static_size);
525 for (int i = 0; i < clz->field_count; i++)
527 _Jv_Field *field = &clz->fields[i];
529 if ((field->flags & Modifier::STATIC) != 0)
531 field->u.addr = static_data + field->u.boffset;
533 if (clz->field_initializers[i] != 0)
535 _Jv_ResolveField (field, clz->loader);
536 _Jv_InitField (0, clz, i);
541 // now we don't need the field_initializers anymore, so let the
542 // collector get rid of it!
544 clz->field_initializers = 0;
547 /************ PART TWO: VTABLE LAYOUT ***************/
549 /* preparation: build the vtable stubs (even interfaces can)
550 have code -- for static constructors. */
551 for (int i = 0; i < clz->method_count; i++)
553 _Jv_MethodBase *imeth = clz->interpreted_methods[i];
555 if ((clz->methods[i].accflags & Modifier::NATIVE) != 0)
557 // You might think we could use a virtual `ncode' method in
558 // the _Jv_MethodBase and unify the native and non-native
559 // cases. Well, we can't, because we don't allocate these
560 // objects using `new', and thus they don't get a vtable.
561 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
562 clz->methods[i].ncode = jnim->ncode ();
564 else if (imeth != 0) // it could be abstract
566 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
567 _Jv_VerifyMethod (im);
568 clz->methods[i].ncode = im->ncode ();
572 if ((clz->accflags & Modifier::INTERFACE))
574 clz->state = JV_STATE_PREPARED;
575 clz->notifyAll ();
576 return;
579 // A class might have so-called "Miranda methods". This is a method
580 // that is declared in an interface and not re-declared in an
581 // abstract class. Some compilers don't emit declarations for such
582 // methods in the class; this will give us problems since we expect
583 // a declaration for any method requiring a vtable entry. We handle
584 // this here by searching for such methods and constructing new
585 // internal declarations for them. We only need to do this for
586 // abstract classes.
587 if ((clz->accflags & Modifier::ABSTRACT))
588 _Jv_PrepareMissingMethods (clz, clz);
590 clz->vtable_method_count = -1;
591 _Jv_MakeVTable (clz);
593 /* wooha! we're done. */
594 clz->state = JV_STATE_PREPARED;
595 clz->notifyAll ();
598 /** Do static initialization for fields with a constant initializer */
599 void
600 _Jv_InitField (jobject obj, jclass klass, int index)
602 using namespace java::lang::reflect;
604 if (obj != 0 && klass == 0)
605 klass = obj->getClass ();
607 if (!_Jv_IsInterpretedClass (klass))
608 return;
610 _Jv_InterpClass *clz = (_Jv_InterpClass*)klass;
612 _Jv_Field * field = (&clz->fields[0]) + index;
614 if (index > clz->field_count)
615 throw_internal_error ("field out of range");
617 int init = clz->field_initializers[index];
618 if (init == 0)
619 return;
621 _Jv_Constants *pool = &clz->constants;
622 int tag = pool->tags[init];
624 if (! field->isResolved ())
625 throw_internal_error ("initializing unresolved field");
627 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
628 throw_internal_error ("initializing non-static field with no object");
630 void *addr = 0;
632 if ((field->flags & Modifier::STATIC) != 0)
633 addr = (void*) field->u.addr;
634 else
635 addr = (void*) (((char*)obj) + field->u.boffset);
637 switch (tag)
639 case JV_CONSTANT_String:
641 _Jv_MonitorEnter (clz);
642 jstring str;
643 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
644 pool->data[init].string = str;
645 pool->tags[init] = JV_CONSTANT_ResolvedString;
646 _Jv_MonitorExit (clz);
648 /* fall through */
650 case JV_CONSTANT_ResolvedString:
651 if (! (field->type == &StringClass
652 || field->type == &java::lang::Class::class$))
653 throw_class_format_error ("string initialiser to non-string field");
655 *(jstring*)addr = pool->data[init].string;
656 break;
658 case JV_CONSTANT_Integer:
660 int value = pool->data[init].i;
662 if (field->type == JvPrimClass (boolean))
663 *(jboolean*)addr = (jboolean)value;
665 else if (field->type == JvPrimClass (byte))
666 *(jbyte*)addr = (jbyte)value;
668 else if (field->type == JvPrimClass (char))
669 *(jchar*)addr = (jchar)value;
671 else if (field->type == JvPrimClass (short))
672 *(jshort*)addr = (jshort)value;
674 else if (field->type == JvPrimClass (int))
675 *(jint*)addr = (jint)value;
677 else
678 throw_class_format_error ("erroneous field initializer");
680 break;
682 case JV_CONSTANT_Long:
683 if (field->type != JvPrimClass (long))
684 throw_class_format_error ("erroneous field initializer");
686 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
687 break;
689 case JV_CONSTANT_Float:
690 if (field->type != JvPrimClass (float))
691 throw_class_format_error ("erroneous field initializer");
693 *(jfloat*)addr = pool->data[init].f;
694 break;
696 case JV_CONSTANT_Double:
697 if (field->type != JvPrimClass (double))
698 throw_class_format_error ("erroneous field initializer");
700 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
701 break;
703 default:
704 throw_class_format_error ("erroneous field initializer");
708 static int
709 get_alignment_from_class (jclass klass)
711 if (klass == JvPrimClass (byte))
712 return __alignof__ (jbyte);
713 else if (klass == JvPrimClass (short))
714 return __alignof__ (jshort);
715 else if (klass == JvPrimClass (int))
716 return __alignof__ (jint);
717 else if (klass == JvPrimClass (long))
718 return __alignof__ (jlong);
719 else if (klass == JvPrimClass (boolean))
720 return __alignof__ (jboolean);
721 else if (klass == JvPrimClass (char))
722 return __alignof__ (jchar);
723 else if (klass == JvPrimClass (float))
724 return __alignof__ (jfloat);
725 else if (klass == JvPrimClass (double))
726 return __alignof__ (jdouble);
727 else
728 return __alignof__ (jobject);
732 inline static unsigned char*
733 skip_one_type (unsigned char* ptr)
735 int ch = *ptr++;
737 while (ch == '[')
739 ch = *ptr++;
742 if (ch == 'L')
744 do { ch = *ptr++; } while (ch != ';');
747 return ptr;
750 static ffi_type*
751 get_ffi_type_from_signature (unsigned char* ptr)
753 switch (*ptr)
755 case 'L':
756 case '[':
757 return &ffi_type_pointer;
758 break;
760 case 'Z':
761 // On some platforms a bool is a byte, on others an int.
762 if (sizeof (jboolean) == sizeof (jbyte))
763 return &ffi_type_sint8;
764 else
766 JvAssert (sizeof (jbyte) == sizeof (jint));
767 return &ffi_type_sint32;
769 break;
771 case 'B':
772 return &ffi_type_sint8;
773 break;
775 case 'C':
776 return &ffi_type_uint16;
777 break;
779 case 'S':
780 return &ffi_type_sint16;
781 break;
783 case 'I':
784 return &ffi_type_sint32;
785 break;
787 case 'J':
788 return &ffi_type_sint64;
789 break;
791 case 'F':
792 return &ffi_type_float;
793 break;
795 case 'D':
796 return &ffi_type_double;
797 break;
799 case 'V':
800 return &ffi_type_void;
801 break;
804 throw_internal_error ("unknown type in signature");
807 /* this function yields the number of actual arguments, that is, if the
808 * function is non-static, then one is added to the number of elements
809 * found in the signature */
811 int
812 _Jv_count_arguments (_Jv_Utf8Const *signature,
813 jboolean staticp)
815 unsigned char *ptr = (unsigned char*) signature->data;
816 int arg_count = staticp ? 0 : 1;
818 /* first, count number of arguments */
820 // skip '('
821 ptr++;
823 // count args
824 while (*ptr != ')')
826 ptr = skip_one_type (ptr);
827 arg_count += 1;
830 return arg_count;
833 /* This beast will build a cif, given the signature. Memory for
834 * the cif itself and for the argument types must be allocated by the
835 * caller.
838 static int
839 init_cif (_Jv_Utf8Const* signature,
840 int arg_count,
841 jboolean staticp,
842 ffi_cif *cif,
843 ffi_type **arg_types,
844 ffi_type **rtype_p)
846 unsigned char *ptr = (unsigned char*) signature->data;
848 int arg_index = 0; // arg number
849 int item_count = 0; // stack-item count
851 // setup receiver
852 if (!staticp)
854 arg_types[arg_index++] = &ffi_type_pointer;
855 item_count += 1;
858 // skip '('
859 ptr++;
861 // assign arg types
862 while (*ptr != ')')
864 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
866 if (*ptr == 'J' || *ptr == 'D')
867 item_count += 2;
868 else
869 item_count += 1;
871 ptr = skip_one_type (ptr);
874 // skip ')'
875 ptr++;
876 ffi_type *rtype = get_ffi_type_from_signature (ptr);
878 ptr = skip_one_type (ptr);
879 if (ptr != (unsigned char*)signature->data + signature->length)
880 throw_internal_error ("did not find end of signature");
882 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
883 arg_count, rtype, arg_types) != FFI_OK)
884 throw_internal_error ("ffi_prep_cif failed");
886 if (rtype_p != NULL)
887 *rtype_p = rtype;
889 return item_count;
892 #if FFI_NATIVE_RAW_API
893 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
894 # define FFI_RAW_SIZE ffi_raw_size
895 #else
896 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
897 # define FFI_RAW_SIZE ffi_java_raw_size
898 #endif
900 /* we put this one here, and not in interpret.cc because it
901 * calls the utility routines _Jv_count_arguments
902 * which are static to this module. The following struct defines the
903 * layout we use for the stubs, it's only used in the ncode method. */
905 typedef struct {
906 ffi_raw_closure closure;
907 ffi_cif cif;
908 ffi_type *arg_types[0];
909 } ncode_closure;
911 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
913 void *
914 _Jv_InterpMethod::ncode ()
916 using namespace java::lang::reflect;
918 if (self->ncode != 0)
919 return self->ncode;
921 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
922 int arg_count = _Jv_count_arguments (self->signature, staticp);
924 ncode_closure *closure =
925 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
926 + arg_count * sizeof (ffi_type*));
928 init_cif (self->signature,
929 arg_count,
930 staticp,
931 &closure->cif,
932 &closure->arg_types[0],
933 NULL);
935 ffi_closure_fun fun;
937 args_raw_size = FFI_RAW_SIZE (&closure->cif);
939 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
941 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
943 if (staticp)
944 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
945 else
946 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
948 else
950 if (staticp)
951 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
952 else
953 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
956 FFI_PREP_RAW_CLOSURE (&closure->closure,
957 &closure->cif,
958 fun,
959 (void*)this);
961 self->ncode = (void*)closure;
962 return self->ncode;
965 void *
966 _Jv_JNIMethod::ncode ()
968 using namespace java::lang::reflect;
970 if (self->ncode != 0)
971 return self->ncode;
973 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
974 int arg_count = _Jv_count_arguments (self->signature, staticp);
976 ncode_closure *closure =
977 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
978 + arg_count * sizeof (ffi_type*));
980 ffi_type *rtype;
981 init_cif (self->signature,
982 arg_count,
983 staticp,
984 &closure->cif,
985 &closure->arg_types[0],
986 &rtype);
988 ffi_closure_fun fun;
990 args_raw_size = FFI_RAW_SIZE (&closure->cif);
992 // Initialize the argument types and CIF that represent the actual
993 // underlying JNI function.
994 int extra_args = 1;
995 if ((self->accflags & Modifier::STATIC))
996 ++extra_args;
997 jni_arg_types = (ffi_type **) _Jv_Malloc ((extra_args + arg_count)
998 * sizeof (ffi_type *));
999 int offset = 0;
1000 jni_arg_types[offset++] = &ffi_type_pointer;
1001 if ((self->accflags & Modifier::STATIC))
1002 jni_arg_types[offset++] = &ffi_type_pointer;
1003 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1004 arg_count * sizeof (ffi_type *));
1006 // NOTE: This must agree with the JNICALL definition in jni.h
1007 #ifdef WIN32
1008 #define FFI_JNI_ABI FFI_STDCALL
1009 #else
1010 #define FFI_JNI_ABI FFI_DEFAULT_ABI
1011 #endif
1013 if (ffi_prep_cif (&jni_cif, FFI_JNI_ABI,
1014 extra_args + arg_count, rtype,
1015 jni_arg_types) != FFI_OK)
1016 throw_internal_error ("ffi_prep_cif failed for JNI function");
1018 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1020 // FIXME: for now we assume that all native methods for
1021 // interpreted code use JNI.
1022 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1024 FFI_PREP_RAW_CLOSURE (&closure->closure,
1025 &closure->cif,
1026 fun,
1027 (void*) this);
1029 self->ncode = (void *) closure;
1030 return self->ncode;
1034 /* A _Jv_ResolvedMethod is what is put in the constant pool for a
1035 * MethodRef or InterfacemethodRef. */
1036 static _Jv_ResolvedMethod*
1037 _Jv_BuildResolvedMethod (_Jv_Method* method,
1038 jclass klass,
1039 jboolean staticp,
1040 jint vtable_index)
1042 int arg_count = _Jv_count_arguments (method->signature, staticp);
1044 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1045 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1046 + arg_count*sizeof (ffi_type*));
1048 result->stack_item_count
1049 = init_cif (method->signature,
1050 arg_count,
1051 staticp,
1052 &result->cif,
1053 &result->arg_types[0],
1054 NULL);
1056 result->vtable_index = vtable_index;
1057 result->method = method;
1058 result->klass = klass;
1060 return result;
1064 static void
1065 throw_class_format_error (jstring msg)
1067 throw (msg
1068 ? new java::lang::ClassFormatError (msg)
1069 : new java::lang::ClassFormatError);
1072 static void
1073 throw_class_format_error (char *msg)
1075 throw_class_format_error (JvNewStringLatin1 (msg));
1078 static void
1079 throw_internal_error (char *msg)
1081 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
1085 #endif /* INTERPRETER */