Revert these changes:
[official-gcc.git] / libjava / resolve.cc
blobe202da042e6fa78e9a22c80079a80f5faac20db4
1 // resolve.cc - Code for linking and resolving classes and pool entries.
3 /* Copyright (C) 1999, 2000, 2001 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/Thread.h>
24 #include <java/lang/InternalError.h>
25 #include <java/lang/VirtualMachineError.h>
26 #include <java/lang/NoSuchFieldError.h>
27 #include <java/lang/NoSuchMethodError.h>
28 #include <java/lang/ClassFormatError.h>
29 #include <java/lang/IllegalAccessError.h>
30 #include <java/lang/AbstractMethodError.h>
31 #include <java/lang/ClassNotFoundException.h>
32 #include <java/lang/IncompatibleClassChangeError.h>
33 #include <java/lang/reflect/Modifier.h>
35 void
36 _Jv_ResolveField (_Jv_Field *field, java::lang::ClassLoader *loader)
38 if (! field->isResolved ())
40 _Jv_Utf8Const *sig = (_Jv_Utf8Const*)field->type;
41 field->type = _Jv_FindClassFromSignature (sig->data, loader);
42 field->flags &= ~_Jv_FIELD_UNRESOLVED_FLAG;
46 #ifdef INTERPRETER
48 static void throw_internal_error (char *msg)
49 __attribute__ ((__noreturn__));
50 static void throw_class_format_error (jstring msg)
51 __attribute__ ((__noreturn__));
52 static void throw_class_format_error (char *msg)
53 __attribute__ ((__noreturn__));
55 // Exceptional return values for _Jv_DetermineVTableIndex
56 #define METHOD_NOT_THERE (-2)
57 #define METHOD_INACCESSIBLE (-1)
59 static int get_alignment_from_class (jclass);
61 static _Jv_ResolvedMethod*
62 _Jv_BuildResolvedMethod (_Jv_Method*,
63 jclass,
64 jboolean,
65 jint);
68 // We need to know the name of a constructor.
69 static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
71 static void throw_incompatible_class_change_error (jstring msg)
73 JvThrow (new java::lang::IncompatibleClassChangeError (msg));
76 _Jv_word
77 _Jv_ResolvePoolEntry (jclass klass, int index)
79 using namespace java::lang::reflect;
81 _Jv_Constants *pool = &klass->constants;
83 if ((pool->tags[index] & JV_CONSTANT_ResolvedFlag) != 0)
84 return pool->data[index];
86 switch (pool->tags[index]) {
87 case JV_CONSTANT_Class:
89 _Jv_Utf8Const *name = pool->data[index].utf8;
91 jclass found;
92 if (name->data[0] == '[')
93 found = _Jv_FindClassFromSignature (&name->data[0],
94 klass->loader);
95 else
96 found = _Jv_FindClass (name, klass->loader);
98 if (! found)
100 jstring str = _Jv_NewStringUTF (name->data);
101 JvThrow (new java::lang::ClassNotFoundException (str));
104 if ((found->accflags & Modifier::PUBLIC) == Modifier::PUBLIC
105 || (_Jv_ClassNameSamePackage (found->name,
106 klass->name)))
108 pool->data[index].clazz = found;
109 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
111 else
113 JvThrow (new java::lang::IllegalAccessError (found->getName()));
116 break;
118 case JV_CONSTANT_String:
120 jstring str;
121 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
122 pool->data[index].o = str;
123 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
125 break;
128 case JV_CONSTANT_Fieldref:
130 _Jv_ushort class_index, name_and_type_index;
131 _Jv_loadIndexes (&pool->data[index],
132 class_index,
133 name_and_type_index);
134 jclass owner = (_Jv_ResolvePoolEntry (klass, class_index)).clazz;
136 if (owner != klass)
137 _Jv_InitClass (owner);
139 _Jv_ushort name_index, type_index;
140 _Jv_loadIndexes (&pool->data[name_and_type_index],
141 name_index,
142 type_index);
144 _Jv_Utf8Const *field_name = pool->data[name_index].utf8;
145 _Jv_Utf8Const *field_type_name = pool->data[type_index].utf8;
147 // FIXME: The implementation of this function
148 // (_Jv_FindClassFromSignature) will generate an instance of
149 // _Jv_Utf8Const for each call if the field type is a class name
150 // (Lxx.yy.Z;). This may be too expensive to do for each and
151 // every fieldref being resolved. For now, we fix the problem by
152 // only doing it when we have a loader different from the class
153 // declaring the field.
155 jclass field_type = 0;
157 if (owner->loader != klass->loader)
158 field_type = _Jv_FindClassFromSignature (field_type_name->data,
159 klass->loader);
161 _Jv_Field* the_field = 0;
163 for (jclass cls = owner; cls != 0; cls = cls->getSuperclass ())
165 for (int i = 0; i < cls->field_count; i++)
167 _Jv_Field *field = &cls->fields[i];
168 if (! _Jv_equalUtf8Consts (field->name, field_name))
169 continue;
171 // now, check field access.
173 if ( (cls == klass)
174 || ((field->flags & Modifier::PUBLIC) != 0)
175 || (((field->flags & Modifier::PROTECTED) != 0)
176 && cls->isAssignableFrom (klass))
177 || (((field->flags & Modifier::PRIVATE) == 0)
178 && _Jv_ClassNameSamePackage (cls->name,
179 klass->name)))
181 /* resove the field using the class' own loader
182 if necessary */
184 if (!field->isResolved ())
185 _Jv_ResolveField (field, cls->loader);
187 if (field_type != 0 && field->type != field_type)
188 JvThrow
189 (new java::lang::LinkageError
190 (JvNewStringLatin1
191 ("field type mismatch with different loaders")));
193 the_field = field;
194 goto end_of_field_search;
196 else
198 JvThrow (new java::lang::IllegalAccessError);
203 end_of_field_search:
204 if (the_field == 0)
206 jstring msg = JvNewStringLatin1 ("field ");
207 msg = msg->concat (owner->getName ());
208 msg = msg->concat (JvNewStringLatin1("."));
209 msg = msg->concat (_Jv_NewStringUTF (field_name->data));
210 msg = msg->concat (JvNewStringLatin1(" was not found."));
211 throw_incompatible_class_change_error (msg);
214 pool->data[index].field = the_field;
215 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
217 break;
219 case JV_CONSTANT_Methodref:
220 case JV_CONSTANT_InterfaceMethodref:
222 _Jv_ushort class_index, name_and_type_index;
223 _Jv_loadIndexes (&pool->data[index],
224 class_index,
225 name_and_type_index);
226 jclass owner = (_Jv_ResolvePoolEntry (klass, class_index)).clazz;
228 if (owner != klass)
229 _Jv_InitClass (owner);
231 _Jv_ushort name_index, type_index;
232 _Jv_loadIndexes (&pool->data[name_and_type_index],
233 name_index,
234 type_index);
236 _Jv_Utf8Const *method_name = pool->data[name_index].utf8;
237 _Jv_Utf8Const *method_signature = pool->data[type_index].utf8;
239 int vtable_index = -1;
240 _Jv_Method *the_method = 0;
241 jclass found_class = 0;
243 // First search the class itself.
244 the_method = _Jv_SearchMethodInClass (owner, klass,
245 method_name, method_signature);
247 if (the_method != 0)
249 found_class = owner;
250 goto end_of_method_search;
253 // If we are resolving an interface method, search the interface's
254 // superinterfaces (A superinterface is not an interface's superclass -
255 // a superinterface is implemented by the interface).
256 if (pool->tags[index] == JV_CONSTANT_InterfaceMethodref)
258 _Jv_ifaces ifaces;
259 ifaces.count = 0;
260 ifaces.len = 4;
261 ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
263 _Jv_GetInterfaces (owner, &ifaces);
265 for (int i=0; i < ifaces.count; i++)
267 jclass cls = ifaces.list[i];
268 the_method = _Jv_SearchMethodInClass (cls, klass, method_name,
269 method_signature);
270 if (the_method != 0)
272 found_class = cls;
273 break;
277 _Jv_Free (ifaces.list);
279 if (the_method != 0)
280 goto end_of_method_search;
283 // Finally, search superclasses.
284 for (jclass cls = owner->getSuperclass (); cls != 0;
285 cls = cls->getSuperclass ())
287 the_method = _Jv_SearchMethodInClass (cls, klass,
288 method_name, method_signature);
289 if (the_method != 0)
291 found_class = cls;
292 break;
296 end_of_method_search:
298 // FIXME: if (cls->loader != klass->loader), then we
299 // must actually check that the types of arguments
300 // correspond. That is, for each argument type, and
301 // the return type, doing _Jv_FindClassFromSignature
302 // with either loader should produce the same result,
303 // i.e., exactly the same jclass object. JVMS 5.4.3.3
305 if (pool->tags[index] == JV_CONSTANT_InterfaceMethodref)
306 vtable_index = -1;
307 else
308 vtable_index = _Jv_DetermineVTableIndex
309 (found_class, method_name, method_signature);
311 if (vtable_index == METHOD_NOT_THERE)
312 throw_incompatible_class_change_error
313 (JvNewStringLatin1 ("method not found"));
315 if (the_method == 0)
317 jstring msg = JvNewStringLatin1 ("method ");
318 msg = msg->concat (owner->getName ());
319 msg = msg->concat (JvNewStringLatin1("."));
320 msg = msg->concat (_Jv_NewStringUTF (method_name->data));
321 msg = msg->concat (JvNewStringLatin1(" was not found."));
322 JvThrow(new java::lang::NoSuchMethodError (msg));
325 pool->data[index].rmethod =
326 _Jv_BuildResolvedMethod(the_method,
327 found_class,
328 (the_method->accflags & Modifier::STATIC) != 0,
329 vtable_index);
330 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
332 break;
336 return pool->data[index];
339 // Find a method declared in the cls that is referenced from klass and
340 // perform access checks.
341 _Jv_Method *
342 _Jv_SearchMethodInClass (jclass cls, jclass klass,
343 _Jv_Utf8Const *method_name,
344 _Jv_Utf8Const *method_signature)
346 using namespace java::lang::reflect;
348 for (int i = 0; i < cls->method_count; i++)
350 _Jv_Method *method = &cls->methods[i];
351 if ( (!_Jv_equalUtf8Consts (method->name,
352 method_name))
353 || (!_Jv_equalUtf8Consts (method->signature,
354 method_signature)))
355 continue;
357 if (cls == klass
358 || ((method->accflags & Modifier::PUBLIC) != 0)
359 || (((method->accflags & Modifier::PROTECTED) != 0)
360 && cls->isAssignableFrom (klass))
361 || (((method->accflags & Modifier::PRIVATE) == 0)
362 && _Jv_ClassNameSamePackage (cls->name,
363 klass->name)))
365 return method;
367 else
369 JvThrow (new java::lang::IllegalAccessError);
372 return 0;
375 /** FIXME: this is a terribly inefficient algorithm! It would improve
376 things if compiled classes to know vtable offset, and _Jv_Method had
377 a field for this.
379 Returns METHOD_NOT_THERE if this class does not declare the given method.
380 Returns METHOD_INACCESSIBLE if the given method does not appear in the
381 vtable, i.e., it is static, private, final or a constructor.
382 Otherwise, returns the vtable index. */
383 int
384 _Jv_DetermineVTableIndex (jclass klass,
385 _Jv_Utf8Const *name,
386 _Jv_Utf8Const *signature)
388 using namespace java::lang::reflect;
390 jclass super_class = klass->getSuperclass ();
392 if (super_class != NULL)
394 int prev = _Jv_DetermineVTableIndex (super_class,
395 name,
396 signature);
397 if (prev != METHOD_NOT_THERE)
398 return prev;
401 /* at this point, we know that the super-class does not declare
402 * the method. Otherwise, the above call would have found it, and
403 * determined the result of this function (-1 or some positive
404 * number).
407 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
409 /* now, if we do not declare this method, return zero */
410 if (meth == NULL)
411 return METHOD_NOT_THERE;
413 /* so now, we know not only that the super class does not declare the
414 * method, but we do! So, this is a first declaration of the method. */
416 /* now, the checks for things that are declared in this class, but do
417 * not go into the vtable. There are three cases.
418 * 1) the method is static, private or final
419 * 2) the class itself is final, or
420 * 3) it is the method <init>
423 if ((meth->accflags & (Modifier::STATIC
424 | Modifier::PRIVATE
425 | Modifier::FINAL)) != 0
426 || (klass->accflags & Modifier::FINAL) != 0
427 || _Jv_equalUtf8Consts (name, init_name))
428 return METHOD_INACCESSIBLE;
430 /* reaching this point, we know for sure, that the method in question
431 * will be in the vtable. The question is where. */
433 /* the base offset, is where we will start assigning vtable
434 * indexes for this class. It is 0 for base classes
435 * and for non-base classes it is the
436 * number of entries in the super class' vtable. */
438 int base_offset;
439 if (super_class == 0)
440 base_offset = 0;
441 else
442 base_offset = super_class->vtable_method_count;
444 /* we will consider methods 0..this_method_index-1. And for each one,
445 * determine if it is new (i.e., if it appears in the super class),
446 * and if it should go in the vtable. If so, increment base_offset */
448 int this_method_index = meth - (&klass->methods[0]);
450 for (int i = 0; i < this_method_index; i++)
452 _Jv_Method *m = &klass->methods[i];
454 /* fist some checks for things that surely do not go in the
455 * vtable */
457 if ((m->accflags & (Modifier::STATIC | Modifier::PRIVATE)) != 0)
458 continue;
459 if (_Jv_equalUtf8Consts (m->name, init_name))
460 continue;
462 /* Then, we need to know if this method appears in the
463 superclass. (This is where this function gets expensive) */
464 _Jv_Method *sm = _Jv_LookupDeclaredMethod (super_class,
465 m->name,
466 m->signature);
468 /* if it was somehow declared in the superclass, skip this */
469 if (sm != NULL)
470 continue;
472 /* but if it is final, and not declared in the super class,
473 * then we also skip it */
474 if ((m->accflags & Modifier::FINAL) != 0)
475 continue;
477 /* finally, we can assign the index of this method */
478 /* m->vtable_index = base_offset */
479 base_offset += 1;
482 return base_offset;
485 /* this is installed in place of abstract methods */
486 static void
487 _Jv_abstractMethodError ()
489 JvThrow (new java::lang::AbstractMethodError);
492 void
493 _Jv_PrepareClass(jclass klass)
495 using namespace java::lang::reflect;
498 * The job of this function is to: 1) assign storage to fields, and 2)
499 * build the vtable. static fields are assigned real memory, instance
500 * fields are assigned offsets.
502 * NOTE: we have a contract with the garbage collector here. Static
503 * reference fields must not be resolved, until after they have storage
504 * assigned which is the check used by the collector to see if it
505 * should indirect the static field reference and mark the object
506 * pointed to.
508 * Most fields are resolved lazily (i.e. have their class-type
509 * assigned) when they are accessed the first time by calling as part
510 * of _Jv_ResolveField, which is allways called after _Jv_PrepareClass.
511 * Static fields with initializers are resolved as part of this
512 * function, as are fields with primitive types.
515 if (! _Jv_IsInterpretedClass (klass))
516 return;
518 if (klass->state >= JV_STATE_PREPARED)
519 return;
521 // make sure super-class is linked. This involves taking a lock on
522 // the super class, so we use the Java method resolveClass, which will
523 // unlock it properly, should an exception happen.
525 java::lang::ClassLoader::resolveClass0 (klass->superclass);
527 _Jv_InterpClass *clz = (_Jv_InterpClass*)klass;
529 /************ PART ONE: OBJECT LAYOUT ***************/
531 int instance_size;
532 int static_size;
534 // java.lang.Object is never interpreted!
535 instance_size = clz->superclass->size ();
536 static_size = 0;
538 for (int i = 0; i < clz->field_count; i++)
540 int field_size;
541 int field_align;
543 _Jv_Field *field = &clz->fields[i];
545 if (! field->isRef ())
547 // it's safe to resolve the field here, since it's
548 // a primitive class, which does not cause loading to happen.
549 _Jv_ResolveField (field, clz->loader);
551 field_size = field->type->size ();
552 field_align = get_alignment_from_class (field->type);
554 else
556 field_size = sizeof (jobject);
557 field_align = __alignof__ (jobject);
560 #ifndef COMPACT_FIELDS
561 field->bsize = field_size;
562 #endif
564 if (field->flags & Modifier::STATIC)
566 /* this computes an offset into a region we'll allocate
567 shortly, and then add this offset to the start address */
569 static_size = ROUND (static_size, field_align);
570 field->u.boffset = static_size;
571 static_size += field_size;
573 else
575 instance_size = ROUND (instance_size, field_align);
576 field->u.boffset = instance_size;
577 instance_size += field_size;
581 // set the instance size for the class
582 clz->size_in_bytes = instance_size;
584 // allocate static memory
585 if (static_size != 0)
587 char *static_data = (char*)_Jv_AllocBytesChecked (static_size);
589 memset (static_data, 0, static_size);
591 for (int i = 0; i < clz->field_count; i++)
593 _Jv_Field *field = &clz->fields[i];
595 if ((field->flags & Modifier::STATIC) != 0)
597 field->u.addr = static_data + field->u.boffset;
599 if (clz->field_initializers[i] != 0)
601 _Jv_ResolveField (field, clz->loader);
602 _Jv_InitField (0, clz, i);
607 // now we don't need the field_initializers anymore, so let the
608 // collector get rid of it!
610 clz->field_initializers = 0;
613 /************ PART TWO: VTABLE LAYOUT ***************/
615 /* preparation: build the vtable stubs (even interfaces can)
616 have code -- for static constructors. */
617 for (int i = 0; i < clz->method_count; i++)
619 _Jv_MethodBase *imeth = clz->interpreted_methods[i];
621 if ((clz->methods[i].accflags & Modifier::NATIVE) != 0)
623 // You might think we could use a virtual `ncode' method in
624 // the _Jv_MethodBase and unify the native and non-native
625 // cases. Well, we can't, because we don't allocate these
626 // objects using `new', and thus they don't get a vtable.
627 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
628 clz->methods[i].ncode = jnim->ncode ();
630 else if (imeth != 0) // it could be abstract
632 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
633 clz->methods[i].ncode = im->ncode ();
637 if (clz->accflags & Modifier::INTERFACE)
639 clz->state = JV_STATE_PREPARED;
640 clz->notifyAll ();
641 return;
644 /* Now onto the actual job: vtable layout. First, count how many new
645 methods we have */
646 int new_method_count = 0;
648 jclass super_class = clz->getSuperclass ();
650 if (super_class == 0)
651 throw_internal_error ("cannot handle interpreted base classes");
653 for (int i = 0; i < clz->method_count; i++)
655 _Jv_Method *this_meth = &clz->methods[i];
657 if ((this_meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) != 0
658 || _Jv_equalUtf8Consts (this_meth->name, init_name))
660 /* skip this, it doesn't go in the vtable */
661 continue;
664 _Jv_Method *orig_meth = _Jv_LookupDeclaredMethod (super_class,
665 this_meth->name,
666 this_meth->signature);
668 if (orig_meth == 0)
670 // new methods that are final, also don't go in the vtable
671 if ((this_meth->accflags & Modifier::FINAL) != 0)
672 continue;
674 new_method_count += 1;
675 continue;
678 if ((orig_meth->accflags & (Modifier::STATIC
679 | Modifier::PRIVATE
680 | Modifier::FINAL)) != 0
681 || ((orig_meth->accflags & Modifier::ABSTRACT) == 0
682 && (this_meth->accflags & Modifier::ABSTRACT) != 0
683 && (klass->accflags & Modifier::ABSTRACT) == 0))
685 clz->state = JV_STATE_ERROR;
686 clz->notifyAll ();
687 JvThrow (new java::lang::IncompatibleClassChangeError
688 (clz->getName ()));
691 /* FIXME: At this point, if (loader != super_class->loader), we
692 * need to "impose class loader constraints" for the types
693 * involved in the signature of this method */
696 /* determine size */
697 int vtable_count = (super_class->vtable_method_count) + new_method_count;
698 clz->vtable_method_count = vtable_count;
700 /* allocate vtable structure */
701 _Jv_VTable *vtable = _Jv_VTable::new_vtable (vtable_count);
702 vtable->clas = clz;
703 vtable->gc_descr = _Jv_BuildGCDescr(clz);
706 jclass effective_superclass = super_class;
708 /* If super_class is abstract or an interface it has no vtable.
709 We need to find a real one... */
710 while (effective_superclass && effective_superclass->vtable == NULL)
711 effective_superclass = effective_superclass->superclass;
713 /* copy super class' vtable entries. */
714 if (effective_superclass && effective_superclass->vtable)
715 for (int i = 0; i < effective_superclass->vtable_method_count; ++i)
716 vtable->set_method (i, effective_superclass->vtable->get_method (i));
719 /* now, install our own vtable entries, reprise... */
720 for (int i = 0; i < clz->method_count; i++)
722 _Jv_Method *this_meth = &clz->methods[i];
724 int index = _Jv_DetermineVTableIndex (clz,
725 this_meth->name,
726 this_meth->signature);
728 if (index == METHOD_NOT_THERE)
729 throw_internal_error ("method now found in own class");
731 if (index != METHOD_INACCESSIBLE)
733 if (index > clz->vtable_method_count)
734 throw_internal_error ("vtable problem...");
736 if (clz->interpreted_methods[i] == 0)
737 vtable->set_method(index, (void*)&_Jv_abstractMethodError);
738 else
739 vtable->set_method(index, this_meth->ncode);
743 /* finally, assign the vtable! */
744 clz->vtable = vtable;
746 /* wooha! we're done. */
747 clz->state = JV_STATE_PREPARED;
748 clz->notifyAll ();
751 /** Do static initialization for fields with a constant initializer */
752 void
753 _Jv_InitField (jobject obj, jclass klass, int index)
755 using namespace java::lang::reflect;
757 if (obj != 0 && klass == 0)
758 klass = obj->getClass ();
760 if (!_Jv_IsInterpretedClass (klass))
761 return;
763 _Jv_InterpClass *clz = (_Jv_InterpClass*)klass;
765 _Jv_Field * field = (&clz->fields[0]) + index;
767 if (index > clz->field_count)
768 throw_internal_error ("field out of range");
770 int init = clz->field_initializers[index];
771 if (init == 0)
772 return;
774 _Jv_Constants *pool = &clz->constants;
775 int tag = pool->tags[init];
777 if (! field->isResolved ())
778 throw_internal_error ("initializing unresolved field");
780 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
781 throw_internal_error ("initializing non-static field with no object");
783 void *addr = 0;
785 if ((field->flags & Modifier::STATIC) != 0)
786 addr = (void*) field->u.addr;
787 else
788 addr = (void*) (((char*)obj) + field->u.boffset);
790 switch (tag)
792 case JV_CONSTANT_String:
794 _Jv_MonitorEnter (clz);
795 jstring str;
796 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
797 pool->data[init].string = str;
798 pool->tags[init] = JV_CONSTANT_ResolvedString;
799 _Jv_MonitorExit (clz);
801 /* fall through */
803 case JV_CONSTANT_ResolvedString:
804 if (! (field->type == &StringClass
805 || field->type == &java::lang::Class::class$))
806 throw_class_format_error ("string initialiser to non-string field");
808 *(jstring*)addr = pool->data[init].string;
809 break;
811 case JV_CONSTANT_Integer:
813 int value = pool->data[init].i;
815 if (field->type == JvPrimClass (boolean))
816 *(jboolean*)addr = (jboolean)value;
818 else if (field->type == JvPrimClass (byte))
819 *(jbyte*)addr = (jbyte)value;
821 else if (field->type == JvPrimClass (char))
822 *(jchar*)addr = (jchar)value;
824 else if (field->type == JvPrimClass (short))
825 *(jshort*)addr = (jshort)value;
827 else if (field->type == JvPrimClass (int))
828 *(jint*)addr = (jint)value;
830 else
831 throw_class_format_error ("erroneous field initializer");
833 break;
835 case JV_CONSTANT_Long:
836 if (field->type != JvPrimClass (long))
837 throw_class_format_error ("erroneous field initializer");
839 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
840 break;
842 case JV_CONSTANT_Float:
843 if (field->type != JvPrimClass (float))
844 throw_class_format_error ("erroneous field initializer");
846 *(jfloat*)addr = pool->data[init].f;
847 break;
849 case JV_CONSTANT_Double:
850 if (field->type != JvPrimClass (double))
851 throw_class_format_error ("erroneous field initializer");
853 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
854 break;
856 default:
857 throw_class_format_error ("erroneous field initializer");
861 static int
862 get_alignment_from_class (jclass klass)
864 if (klass == JvPrimClass (byte))
865 return __alignof__ (jbyte);
866 else if (klass == JvPrimClass (short))
867 return __alignof__ (jshort);
868 else if (klass == JvPrimClass (int))
869 return __alignof__ (jint);
870 else if (klass == JvPrimClass (long))
871 return __alignof__ (jlong);
872 else if (klass == JvPrimClass (boolean))
873 return __alignof__ (jboolean);
874 else if (klass == JvPrimClass (char))
875 return __alignof__ (jchar);
876 else if (klass == JvPrimClass (float))
877 return __alignof__ (jfloat);
878 else if (klass == JvPrimClass (double))
879 return __alignof__ (jdouble);
880 else
881 return __alignof__ (jobject);
885 inline static unsigned char*
886 skip_one_type (unsigned char* ptr)
888 int ch = *ptr++;
890 while (ch == '[')
892 ch = *ptr++;
895 if (ch == 'L')
897 do { ch = *ptr++; } while (ch != ';');
900 return ptr;
903 static ffi_type*
904 get_ffi_type_from_signature (unsigned char* ptr)
906 switch (*ptr)
908 case 'L':
909 case '[':
910 return &ffi_type_pointer;
911 break;
913 case 'Z':
914 // On some platforms a bool is a byte, on others an int.
915 if (sizeof (jboolean) == sizeof (jbyte))
916 return &ffi_type_sint8;
917 else
919 JvAssert (sizeof (jbyte) == sizeof (jint));
920 return &ffi_type_sint32;
922 break;
924 case 'B':
925 return &ffi_type_sint8;
926 break;
928 case 'C':
929 return &ffi_type_uint16;
930 break;
932 case 'S':
933 return &ffi_type_sint16;
934 break;
936 case 'I':
937 return &ffi_type_sint32;
938 break;
940 case 'J':
941 return &ffi_type_sint64;
942 break;
944 case 'F':
945 return &ffi_type_float;
946 break;
948 case 'D':
949 return &ffi_type_double;
950 break;
952 case 'V':
953 return &ffi_type_void;
954 break;
957 throw_internal_error ("unknown type in signature");
960 /* this function yields the number of actual arguments, that is, if the
961 * function is non-static, then one is added to the number of elements
962 * found in the signature */
964 static int
965 count_arguments (_Jv_Utf8Const *signature,
966 jboolean staticp)
968 unsigned char *ptr = (unsigned char*) signature->data;
969 int arg_count = staticp ? 0 : 1;
971 /* first, count number of arguments */
973 // skip '('
974 ptr++;
976 // count args
977 while (*ptr != ')')
979 ptr = skip_one_type (ptr);
980 arg_count += 1;
983 return arg_count;
986 /* This beast will build a cif, given the signature. Memory for
987 * the cif itself and for the argument types must be allocated by the
988 * caller.
991 static int
992 init_cif (_Jv_Utf8Const* signature,
993 int arg_count,
994 jboolean staticp,
995 ffi_cif *cif,
996 ffi_type **arg_types,
997 ffi_type **rtype_p)
999 unsigned char *ptr = (unsigned char*) signature->data;
1001 int arg_index = 0; // arg number
1002 int item_count = 0; // stack-item count
1004 // setup receiver
1005 if (!staticp)
1007 arg_types[arg_index++] = &ffi_type_pointer;
1008 item_count += 1;
1011 // skip '('
1012 ptr++;
1014 // assign arg types
1015 while (*ptr != ')')
1017 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1019 if (*ptr == 'J' || *ptr == 'D')
1020 item_count += 2;
1021 else
1022 item_count += 1;
1024 ptr = skip_one_type (ptr);
1027 // skip ')'
1028 ptr++;
1029 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1031 ptr = skip_one_type (ptr);
1032 if (ptr != (unsigned char*)signature->data + signature->length)
1033 throw_internal_error ("did not find end of signature");
1035 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1036 arg_count, rtype, arg_types) != FFI_OK)
1037 throw_internal_error ("ffi_prep_cif failed");
1039 if (rtype_p != NULL)
1040 *rtype_p = rtype;
1042 return item_count;
1045 #if FFI_NATIVE_RAW_API
1046 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1047 # define FFI_RAW_SIZE ffi_raw_size
1048 #else
1049 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1050 # define FFI_RAW_SIZE ffi_java_raw_size
1051 #endif
1053 /* we put this one here, and not in interpret.cc because it
1054 * calls the utility routines count_arguments
1055 * which are static to this module. The following struct defines the
1056 * layout we use for the stubs, it's only used in the ncode method. */
1058 typedef struct {
1059 ffi_raw_closure closure;
1060 ffi_cif cif;
1061 ffi_type *arg_types[0];
1062 } ncode_closure;
1064 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1066 void *
1067 _Jv_InterpMethod::ncode ()
1069 using namespace java::lang::reflect;
1071 if (self->ncode != 0)
1072 return self->ncode;
1074 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1075 int arg_count = count_arguments (self->signature, staticp);
1077 ncode_closure *closure =
1078 (ncode_closure*)_Jv_AllocBytesChecked (sizeof (ncode_closure)
1079 + arg_count * sizeof (ffi_type*));
1081 init_cif (self->signature,
1082 arg_count,
1083 staticp,
1084 &closure->cif,
1085 &closure->arg_types[0],
1086 NULL);
1088 ffi_closure_fun fun;
1090 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1092 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1094 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1096 if (staticp)
1097 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1098 else
1099 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1101 else
1103 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1106 FFI_PREP_RAW_CLOSURE (&closure->closure,
1107 &closure->cif,
1108 fun,
1109 (void*)this);
1111 self->ncode = (void*)closure;
1112 return self->ncode;
1116 void *
1117 _Jv_JNIMethod::ncode ()
1119 using namespace java::lang::reflect;
1121 if (self->ncode != 0)
1122 return self->ncode;
1124 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1125 int arg_count = count_arguments (self->signature, staticp);
1127 ncode_closure *closure =
1128 (ncode_closure*)_Jv_AllocBytesChecked (sizeof (ncode_closure)
1129 + arg_count * sizeof (ffi_type*));
1131 ffi_type *rtype;
1132 init_cif (self->signature,
1133 arg_count,
1134 staticp,
1135 &closure->cif,
1136 &closure->arg_types[0],
1137 &rtype);
1139 ffi_closure_fun fun;
1141 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1143 // Initialize the argument types and CIF that represent the actual
1144 // underlying JNI function.
1145 int extra_args = 1;
1146 if ((self->accflags & Modifier::STATIC))
1147 ++extra_args;
1148 jni_arg_types = (ffi_type **) _Jv_Malloc ((extra_args + arg_count)
1149 * sizeof (ffi_type *));
1150 int offset = 0;
1151 jni_arg_types[offset++] = &ffi_type_pointer;
1152 if ((self->accflags & Modifier::STATIC))
1153 jni_arg_types[offset++] = &ffi_type_pointer;
1154 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1155 arg_count * sizeof (ffi_type *));
1157 if (ffi_prep_cif (&jni_cif, FFI_DEFAULT_ABI,
1158 extra_args + arg_count, rtype,
1159 jni_arg_types) != FFI_OK)
1160 throw_internal_error ("ffi_prep_cif failed for JNI function");
1162 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1164 // FIXME: for now we assume that all native methods for
1165 // interpreted code use JNI.
1166 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1168 FFI_PREP_RAW_CLOSURE (&closure->closure,
1169 &closure->cif,
1170 fun,
1171 (void*) this);
1173 self->ncode = (void *) closure;
1174 return self->ncode;
1178 /* A _Jv_ResolvedMethod is what is put in the constant pool for a
1179 * MethodRef or InterfacemethodRef. */
1180 static _Jv_ResolvedMethod*
1181 _Jv_BuildResolvedMethod (_Jv_Method* method,
1182 jclass klass,
1183 jboolean staticp,
1184 jint vtable_index)
1186 int arg_count = count_arguments (method->signature, staticp);
1188 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1189 _Jv_AllocBytesChecked (sizeof (_Jv_ResolvedMethod)
1190 + arg_count*sizeof (ffi_type*));
1192 result->stack_item_count
1193 = init_cif (method->signature,
1194 arg_count,
1195 staticp,
1196 &result->cif,
1197 &result->arg_types[0],
1198 NULL);
1200 result->vtable_index = vtable_index;
1201 result->method = method;
1202 result->klass = klass;
1204 return result;
1208 static void
1209 throw_class_format_error (jstring msg)
1211 if (msg == 0)
1212 JvThrow (new java::lang::ClassFormatError);
1213 else
1214 JvThrow (new java::lang::ClassFormatError (msg));
1217 static void
1218 throw_class_format_error (char *msg)
1220 throw_class_format_error (JvNewStringLatin1 (msg));
1223 static void
1224 throw_internal_error (char *msg)
1226 JvThrow
1227 (new java::lang::InternalError (JvNewStringLatin1 (msg)));
1231 #endif /* INTERPRETER */