* arm.md (arm_buneq, arm_bltgt): put '\' before ';' in output
[official-gcc.git] / libjava / resolve.cc
blobf55875509e3abd71b93d5d20171f701dd53a4fea
1 // resolve.cc - Code for linking and resolving classes and pool entries.
3 /* Copyright (C) 1999, 2000, 2001 , 2002 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 using namespace gcj;
37 void
38 _Jv_ResolveField (_Jv_Field *field, java::lang::ClassLoader *loader)
40 if (! field->isResolved ())
42 _Jv_Utf8Const *sig = (_Jv_Utf8Const*)field->type;
43 field->type = _Jv_FindClassFromSignature (sig->data, loader);
44 field->flags &= ~_Jv_FIELD_UNRESOLVED_FLAG;
48 #ifdef INTERPRETER
50 static void throw_internal_error (char *msg)
51 __attribute__ ((__noreturn__));
52 static void throw_class_format_error (jstring msg)
53 __attribute__ ((__noreturn__));
54 static void throw_class_format_error (char *msg)
55 __attribute__ ((__noreturn__));
57 // Exceptional return values for _Jv_DetermineVTableIndex
58 #define METHOD_NOT_THERE (-2)
59 #define METHOD_INACCESSIBLE (-1)
61 static int get_alignment_from_class (jclass);
63 static _Jv_ResolvedMethod*
64 _Jv_BuildResolvedMethod (_Jv_Method*,
65 jclass,
66 jboolean,
67 jint);
70 static void throw_incompatible_class_change_error (jstring msg)
72 throw new java::lang::IncompatibleClassChangeError (msg);
75 _Jv_word
76 _Jv_ResolvePoolEntry (jclass klass, int index)
78 using namespace java::lang::reflect;
80 _Jv_Constants *pool = &klass->constants;
82 if ((pool->tags[index] & JV_CONSTANT_ResolvedFlag) != 0)
83 return pool->data[index];
85 switch (pool->tags[index]) {
86 case JV_CONSTANT_Class:
88 _Jv_Utf8Const *name = pool->data[index].utf8;
90 jclass found;
91 if (name->data[0] == '[')
92 found = _Jv_FindClassFromSignature (&name->data[0],
93 klass->loader);
94 else
95 found = _Jv_FindClass (name, klass->loader);
97 if (! found)
99 jstring str = _Jv_NewStringUTF (name->data);
100 throw new java::lang::ClassNotFoundException (str);
103 if ((found->accflags & Modifier::PUBLIC) == Modifier::PUBLIC
104 || (_Jv_ClassNameSamePackage (found->name,
105 klass->name)))
107 pool->data[index].clazz = found;
108 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
110 else
112 throw new java::lang::IllegalAccessError (found->getName());
115 break;
117 case JV_CONSTANT_String:
119 jstring str;
120 str = _Jv_NewStringUtf8Const (pool->data[index].utf8);
121 pool->data[index].o = str;
122 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
124 break;
127 case JV_CONSTANT_Fieldref:
129 _Jv_ushort class_index, name_and_type_index;
130 _Jv_loadIndexes (&pool->data[index],
131 class_index,
132 name_and_type_index);
133 jclass owner = (_Jv_ResolvePoolEntry (klass, class_index)).clazz;
135 if (owner != klass)
136 _Jv_InitClass (owner);
138 _Jv_ushort name_index, type_index;
139 _Jv_loadIndexes (&pool->data[name_and_type_index],
140 name_index,
141 type_index);
143 _Jv_Utf8Const *field_name = pool->data[name_index].utf8;
144 _Jv_Utf8Const *field_type_name = pool->data[type_index].utf8;
146 // FIXME: The implementation of this function
147 // (_Jv_FindClassFromSignature) will generate an instance of
148 // _Jv_Utf8Const for each call if the field type is a class name
149 // (Lxx.yy.Z;). This may be too expensive to do for each and
150 // every fieldref being resolved. For now, we fix the problem by
151 // only doing it when we have a loader different from the class
152 // declaring the field.
154 jclass field_type = 0;
156 if (owner->loader != klass->loader)
157 field_type = _Jv_FindClassFromSignature (field_type_name->data,
158 klass->loader);
160 _Jv_Field* the_field = 0;
162 for (jclass cls = owner; cls != 0; cls = cls->getSuperclass ())
164 for (int i = 0; i < cls->field_count; i++)
166 _Jv_Field *field = &cls->fields[i];
167 if (! _Jv_equalUtf8Consts (field->name, field_name))
168 continue;
170 // now, check field access.
172 if ( (cls == klass)
173 || ((field->flags & Modifier::PUBLIC) != 0)
174 || (((field->flags & Modifier::PROTECTED) != 0)
175 && cls->isAssignableFrom (klass))
176 || (((field->flags & Modifier::PRIVATE) == 0)
177 && _Jv_ClassNameSamePackage (cls->name,
178 klass->name)))
180 /* resove the field using the class' own loader
181 if necessary */
183 if (!field->isResolved ())
184 _Jv_ResolveField (field, cls->loader);
186 if (field_type != 0 && field->type != field_type)
187 throw new java::lang::LinkageError
188 (JvNewStringLatin1
189 ("field type mismatch with different loaders"));
191 the_field = field;
192 goto end_of_field_search;
194 else
196 throw new java::lang::IllegalAccessError;
201 end_of_field_search:
202 if (the_field == 0)
204 jstring msg = JvNewStringLatin1 ("field ");
205 msg = msg->concat (owner->getName ());
206 msg = msg->concat (JvNewStringLatin1("."));
207 msg = msg->concat (_Jv_NewStringUTF (field_name->data));
208 msg = msg->concat (JvNewStringLatin1(" was not found."));
209 throw_incompatible_class_change_error (msg);
212 pool->data[index].field = the_field;
213 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
215 break;
217 case JV_CONSTANT_Methodref:
218 case JV_CONSTANT_InterfaceMethodref:
220 _Jv_ushort class_index, name_and_type_index;
221 _Jv_loadIndexes (&pool->data[index],
222 class_index,
223 name_and_type_index);
224 jclass owner = (_Jv_ResolvePoolEntry (klass, class_index)).clazz;
226 if (owner != klass)
227 _Jv_InitClass (owner);
229 _Jv_ushort name_index, type_index;
230 _Jv_loadIndexes (&pool->data[name_and_type_index],
231 name_index,
232 type_index);
234 _Jv_Utf8Const *method_name = pool->data[name_index].utf8;
235 _Jv_Utf8Const *method_signature = pool->data[type_index].utf8;
237 int vtable_index = -1;
238 _Jv_Method *the_method = 0;
239 jclass found_class = 0;
241 // First search the class itself.
242 the_method = _Jv_SearchMethodInClass (owner, klass,
243 method_name, method_signature);
245 if (the_method != 0)
247 found_class = owner;
248 goto end_of_method_search;
251 // If we are resolving an interface method, search the interface's
252 // superinterfaces (A superinterface is not an interface's superclass -
253 // a superinterface is implemented by the interface).
254 if (pool->tags[index] == JV_CONSTANT_InterfaceMethodref)
256 _Jv_ifaces ifaces;
257 ifaces.count = 0;
258 ifaces.len = 4;
259 ifaces.list = (jclass *) _Jv_Malloc (ifaces.len * sizeof (jclass *));
261 _Jv_GetInterfaces (owner, &ifaces);
263 for (int i=0; i < ifaces.count; i++)
265 jclass cls = ifaces.list[i];
266 the_method = _Jv_SearchMethodInClass (cls, klass, method_name,
267 method_signature);
268 if (the_method != 0)
270 found_class = cls;
271 break;
275 _Jv_Free (ifaces.list);
277 if (the_method != 0)
278 goto end_of_method_search;
281 // Finally, search superclasses.
282 for (jclass cls = owner->getSuperclass (); cls != 0;
283 cls = cls->getSuperclass ())
285 the_method = _Jv_SearchMethodInClass (cls, klass,
286 method_name, method_signature);
287 if (the_method != 0)
289 found_class = cls;
290 break;
294 end_of_method_search:
296 // FIXME: if (cls->loader != klass->loader), then we
297 // must actually check that the types of arguments
298 // correspond. That is, for each argument type, and
299 // the return type, doing _Jv_FindClassFromSignature
300 // with either loader should produce the same result,
301 // i.e., exactly the same jclass object. JVMS 5.4.3.3
303 if (pool->tags[index] == JV_CONSTANT_InterfaceMethodref)
304 vtable_index = -1;
305 else
306 vtable_index = _Jv_DetermineVTableIndex
307 (found_class, method_name, method_signature);
309 if (vtable_index == METHOD_NOT_THERE)
310 throw_incompatible_class_change_error
311 (JvNewStringLatin1 ("method not found"));
313 if (the_method == 0)
315 jstring msg = JvNewStringLatin1 ("method ");
316 msg = msg->concat (owner->getName ());
317 msg = msg->concat (JvNewStringLatin1("."));
318 msg = msg->concat (_Jv_NewStringUTF (method_name->data));
319 msg = msg->concat (JvNewStringLatin1(" was not found."));
320 throw new java::lang::NoSuchMethodError (msg);
323 pool->data[index].rmethod =
324 _Jv_BuildResolvedMethod(the_method,
325 found_class,
326 (the_method->accflags & Modifier::STATIC) != 0,
327 vtable_index);
328 pool->tags[index] |= JV_CONSTANT_ResolvedFlag;
330 break;
334 return pool->data[index];
337 // Find a method declared in the cls that is referenced from klass and
338 // perform access checks.
339 _Jv_Method *
340 _Jv_SearchMethodInClass (jclass cls, jclass klass,
341 _Jv_Utf8Const *method_name,
342 _Jv_Utf8Const *method_signature)
344 using namespace java::lang::reflect;
346 for (int i = 0; i < cls->method_count; i++)
348 _Jv_Method *method = &cls->methods[i];
349 if ( (!_Jv_equalUtf8Consts (method->name,
350 method_name))
351 || (!_Jv_equalUtf8Consts (method->signature,
352 method_signature)))
353 continue;
355 if (cls == klass
356 || ((method->accflags & Modifier::PUBLIC) != 0)
357 || (((method->accflags & Modifier::PROTECTED) != 0)
358 && cls->isAssignableFrom (klass))
359 || (((method->accflags & Modifier::PRIVATE) == 0)
360 && _Jv_ClassNameSamePackage (cls->name,
361 klass->name)))
363 return method;
365 else
367 throw new java::lang::IllegalAccessError;
370 return 0;
373 /** FIXME: this is a terribly inefficient algorithm! It would improve
374 things if compiled classes to know vtable offset, and _Jv_Method had
375 a field for this.
377 Returns METHOD_NOT_THERE if this class does not declare the given method.
378 Returns METHOD_INACCESSIBLE if the given method does not appear in the
379 vtable, i.e., it is static, private, final or a constructor.
380 Otherwise, returns the vtable index. */
381 int
382 _Jv_DetermineVTableIndex (jclass klass,
383 _Jv_Utf8Const *name,
384 _Jv_Utf8Const *signature)
386 using namespace java::lang::reflect;
388 jclass super_class = klass->getSuperclass ();
390 if (super_class != NULL)
392 int prev = _Jv_DetermineVTableIndex (super_class,
393 name,
394 signature);
395 if (prev != METHOD_NOT_THERE)
396 return prev;
399 /* at this point, we know that the super-class does not declare
400 * the method. Otherwise, the above call would have found it, and
401 * determined the result of this function (-1 or some positive
402 * number).
405 _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
407 /* now, if we do not declare this method, return zero */
408 if (meth == NULL)
409 return METHOD_NOT_THERE;
411 /* so now, we know not only that the super class does not declare the
412 * method, but we do! So, this is a first declaration of the method. */
414 /* now, the checks for things that are declared in this class, but do
415 * not go into the vtable. There are three cases.
416 * 1) the method is static, private or final
417 * 2) the class itself is final, or
418 * 3) it is the method <init>
421 if ((meth->accflags & (Modifier::STATIC
422 | Modifier::PRIVATE
423 | Modifier::FINAL)) != 0
424 || (klass->accflags & Modifier::FINAL) != 0
425 || _Jv_equalUtf8Consts (name, init_name))
426 return METHOD_INACCESSIBLE;
428 /* reaching this point, we know for sure, that the method in question
429 * will be in the vtable. The question is where. */
431 /* the base offset, is where we will start assigning vtable
432 * indexes for this class. It is 0 for base classes
433 * and for non-base classes it is the
434 * number of entries in the super class' vtable. */
436 int base_offset;
437 if (super_class == 0)
438 base_offset = 0;
439 else
440 base_offset = super_class->vtable_method_count;
442 /* we will consider methods 0..this_method_index-1. And for each one,
443 * determine if it is new (i.e., if it appears in the super class),
444 * and if it should go in the vtable. If so, increment base_offset */
446 int this_method_index = meth - (&klass->methods[0]);
448 for (int i = 0; i < this_method_index; i++)
450 _Jv_Method *m = &klass->methods[i];
452 /* fist some checks for things that surely do not go in the
453 * vtable */
455 if ((m->accflags & (Modifier::STATIC | Modifier::PRIVATE)) != 0)
456 continue;
457 if (_Jv_equalUtf8Consts (m->name, init_name))
458 continue;
460 /* Then, we need to know if this method appears in the
461 superclass. (This is where this function gets expensive) */
462 _Jv_Method *sm = _Jv_LookupDeclaredMethod (super_class,
463 m->name,
464 m->signature);
466 /* if it was somehow declared in the superclass, skip this */
467 if (sm != NULL)
468 continue;
470 /* but if it is final, and not declared in the super class,
471 * then we also skip it */
472 if ((m->accflags & Modifier::FINAL) != 0)
473 continue;
475 /* finally, we can assign the index of this method */
476 /* m->vtable_index = base_offset */
477 base_offset += 1;
480 return base_offset;
483 /* this is installed in place of abstract methods */
484 static void
485 _Jv_abstractMethodError ()
487 throw new java::lang::AbstractMethodError;
490 void
491 _Jv_PrepareClass(jclass klass)
493 using namespace java::lang::reflect;
496 * The job of this function is to: 1) assign storage to fields, and 2)
497 * build the vtable. static fields are assigned real memory, instance
498 * fields are assigned offsets.
500 * NOTE: we have a contract with the garbage collector here. Static
501 * reference fields must not be resolved, until after they have storage
502 * assigned which is the check used by the collector to see if it
503 * should indirect the static field reference and mark the object
504 * pointed to.
506 * Most fields are resolved lazily (i.e. have their class-type
507 * assigned) when they are accessed the first time by calling as part
508 * of _Jv_ResolveField, which is allways called after _Jv_PrepareClass.
509 * Static fields with initializers are resolved as part of this
510 * function, as are fields with primitive types.
513 if (! _Jv_IsInterpretedClass (klass))
514 return;
516 if (klass->state >= JV_STATE_PREPARED)
517 return;
519 // Make sure super-class is linked. This involves taking a lock on
520 // the super class, so we use the Java method resolveClass, which
521 // will unlock it properly, should an exception happen. If there's
522 // no superclass, do nothing -- Object will already have been
523 // resolved.
525 if (klass->superclass)
526 java::lang::ClassLoader::resolveClass0 (klass->superclass);
528 _Jv_InterpClass *clz = (_Jv_InterpClass*)klass;
530 /************ PART ONE: OBJECT LAYOUT ***************/
532 int instance_size;
533 int static_size;
535 // Although java.lang.Object is never interpreted, an interface can
536 // have a null superclass.
537 if (clz->superclass)
538 instance_size = clz->superclass->size();
539 else
540 instance_size = java::lang::Object::class$.size();
541 static_size = 0;
543 for (int i = 0; i < clz->field_count; i++)
545 int field_size;
546 int field_align;
548 _Jv_Field *field = &clz->fields[i];
550 if (! field->isRef ())
552 // it's safe to resolve the field here, since it's
553 // a primitive class, which does not cause loading to happen.
554 _Jv_ResolveField (field, clz->loader);
556 field_size = field->type->size ();
557 field_align = get_alignment_from_class (field->type);
559 else
561 field_size = sizeof (jobject);
562 field_align = __alignof__ (jobject);
565 #ifndef COMPACT_FIELDS
566 field->bsize = field_size;
567 #endif
569 if (field->flags & Modifier::STATIC)
571 /* this computes an offset into a region we'll allocate
572 shortly, and then add this offset to the start address */
574 static_size = ROUND (static_size, field_align);
575 field->u.boffset = static_size;
576 static_size += field_size;
578 else
580 instance_size = ROUND (instance_size, field_align);
581 field->u.boffset = instance_size;
582 instance_size += field_size;
586 // set the instance size for the class
587 clz->size_in_bytes = instance_size;
589 // allocate static memory
590 if (static_size != 0)
592 char *static_data = (char*)_Jv_AllocBytes (static_size);
594 memset (static_data, 0, static_size);
596 for (int i = 0; i < clz->field_count; i++)
598 _Jv_Field *field = &clz->fields[i];
600 if ((field->flags & Modifier::STATIC) != 0)
602 field->u.addr = static_data + field->u.boffset;
604 if (clz->field_initializers[i] != 0)
606 _Jv_ResolveField (field, clz->loader);
607 _Jv_InitField (0, clz, i);
612 // now we don't need the field_initializers anymore, so let the
613 // collector get rid of it!
615 clz->field_initializers = 0;
618 /************ PART TWO: VTABLE LAYOUT ***************/
620 /* preparation: build the vtable stubs (even interfaces can)
621 have code -- for static constructors. */
622 for (int i = 0; i < clz->method_count; i++)
624 _Jv_MethodBase *imeth = clz->interpreted_methods[i];
626 if ((clz->methods[i].accflags & Modifier::NATIVE) != 0)
628 // You might think we could use a virtual `ncode' method in
629 // the _Jv_MethodBase and unify the native and non-native
630 // cases. Well, we can't, because we don't allocate these
631 // objects using `new', and thus they don't get a vtable.
632 _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
633 clz->methods[i].ncode = jnim->ncode ();
635 else if (imeth != 0) // it could be abstract
637 _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
638 _Jv_VerifyMethod (im);
639 clz->methods[i].ncode = im->ncode ();
643 if (clz->accflags & Modifier::INTERFACE)
645 clz->state = JV_STATE_PREPARED;
646 clz->notifyAll ();
647 return;
650 /* Now onto the actual job: vtable layout. First, count how many new
651 methods we have */
652 int new_method_count = 0;
654 jclass super_class = clz->getSuperclass ();
656 for (int i = 0; i < clz->method_count; i++)
658 _Jv_Method *this_meth = &clz->methods[i];
660 if ((this_meth->accflags & (Modifier::STATIC | Modifier::PRIVATE)) != 0
661 || _Jv_equalUtf8Consts (this_meth->name, init_name))
663 /* skip this, it doesn't go in the vtable */
664 continue;
667 _Jv_Method *orig_meth = _Jv_LookupDeclaredMethod (super_class,
668 this_meth->name,
669 this_meth->signature);
671 if (orig_meth == 0)
673 // new methods that are final, also don't go in the vtable
674 if ((this_meth->accflags & Modifier::FINAL) != 0)
675 continue;
677 new_method_count += 1;
678 continue;
681 if ((orig_meth->accflags & (Modifier::STATIC
682 | Modifier::PRIVATE
683 | Modifier::FINAL)) != 0
684 || ((orig_meth->accflags & Modifier::ABSTRACT) == 0
685 && (this_meth->accflags & Modifier::ABSTRACT) != 0
686 && (klass->accflags & Modifier::ABSTRACT) == 0))
688 clz->state = JV_STATE_ERROR;
689 clz->notifyAll ();
690 throw new java::lang::IncompatibleClassChangeError (clz->getName ());
693 /* FIXME: At this point, if (loader != super_class->loader), we
694 * need to "impose class loader constraints" for the types
695 * involved in the signature of this method */
698 /* determine size */
699 int vtable_count = (super_class->vtable_method_count) + new_method_count;
700 clz->vtable_method_count = vtable_count;
702 /* allocate vtable structure */
703 _Jv_VTable *vtable = _Jv_VTable::new_vtable (vtable_count);
704 vtable->clas = clz;
705 vtable->gc_descr = _Jv_BuildGCDescr(clz);
708 jclass effective_superclass = super_class;
710 /* If super_class is abstract or an interface it has no vtable.
711 We need to find a real one... */
712 while (effective_superclass && effective_superclass->vtable == NULL)
713 effective_superclass = effective_superclass->superclass;
715 /* If we ended up without a superclass, use Object. */
716 if (! effective_superclass)
717 effective_superclass = &java::lang::Object::class$;
719 /* copy super class' vtable entries. */
720 if (effective_superclass && effective_superclass->vtable)
721 for (int i = 0; i < effective_superclass->vtable_method_count; ++i)
722 vtable->set_method (i, effective_superclass->vtable->get_method (i));
725 /* now, install our own vtable entries, reprise... */
726 for (int i = 0; i < clz->method_count; i++)
728 _Jv_Method *this_meth = &clz->methods[i];
730 int index = _Jv_DetermineVTableIndex (clz,
731 this_meth->name,
732 this_meth->signature);
734 if (index == METHOD_NOT_THERE)
735 throw_internal_error ("method now found in own class");
737 if (index != METHOD_INACCESSIBLE)
739 if (index > clz->vtable_method_count)
740 throw_internal_error ("vtable problem...");
742 if (clz->interpreted_methods[i] == 0)
743 vtable->set_method(index, (void*)&_Jv_abstractMethodError);
744 else
745 vtable->set_method(index, this_meth->ncode);
749 /* finally, assign the vtable! */
750 clz->vtable = vtable;
752 /* wooha! we're done. */
753 clz->state = JV_STATE_PREPARED;
754 clz->notifyAll ();
757 /** Do static initialization for fields with a constant initializer */
758 void
759 _Jv_InitField (jobject obj, jclass klass, int index)
761 using namespace java::lang::reflect;
763 if (obj != 0 && klass == 0)
764 klass = obj->getClass ();
766 if (!_Jv_IsInterpretedClass (klass))
767 return;
769 _Jv_InterpClass *clz = (_Jv_InterpClass*)klass;
771 _Jv_Field * field = (&clz->fields[0]) + index;
773 if (index > clz->field_count)
774 throw_internal_error ("field out of range");
776 int init = clz->field_initializers[index];
777 if (init == 0)
778 return;
780 _Jv_Constants *pool = &clz->constants;
781 int tag = pool->tags[init];
783 if (! field->isResolved ())
784 throw_internal_error ("initializing unresolved field");
786 if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
787 throw_internal_error ("initializing non-static field with no object");
789 void *addr = 0;
791 if ((field->flags & Modifier::STATIC) != 0)
792 addr = (void*) field->u.addr;
793 else
794 addr = (void*) (((char*)obj) + field->u.boffset);
796 switch (tag)
798 case JV_CONSTANT_String:
800 _Jv_MonitorEnter (clz);
801 jstring str;
802 str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
803 pool->data[init].string = str;
804 pool->tags[init] = JV_CONSTANT_ResolvedString;
805 _Jv_MonitorExit (clz);
807 /* fall through */
809 case JV_CONSTANT_ResolvedString:
810 if (! (field->type == &StringClass
811 || field->type == &java::lang::Class::class$))
812 throw_class_format_error ("string initialiser to non-string field");
814 *(jstring*)addr = pool->data[init].string;
815 break;
817 case JV_CONSTANT_Integer:
819 int value = pool->data[init].i;
821 if (field->type == JvPrimClass (boolean))
822 *(jboolean*)addr = (jboolean)value;
824 else if (field->type == JvPrimClass (byte))
825 *(jbyte*)addr = (jbyte)value;
827 else if (field->type == JvPrimClass (char))
828 *(jchar*)addr = (jchar)value;
830 else if (field->type == JvPrimClass (short))
831 *(jshort*)addr = (jshort)value;
833 else if (field->type == JvPrimClass (int))
834 *(jint*)addr = (jint)value;
836 else
837 throw_class_format_error ("erroneous field initializer");
839 break;
841 case JV_CONSTANT_Long:
842 if (field->type != JvPrimClass (long))
843 throw_class_format_error ("erroneous field initializer");
845 *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
846 break;
848 case JV_CONSTANT_Float:
849 if (field->type != JvPrimClass (float))
850 throw_class_format_error ("erroneous field initializer");
852 *(jfloat*)addr = pool->data[init].f;
853 break;
855 case JV_CONSTANT_Double:
856 if (field->type != JvPrimClass (double))
857 throw_class_format_error ("erroneous field initializer");
859 *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
860 break;
862 default:
863 throw_class_format_error ("erroneous field initializer");
867 static int
868 get_alignment_from_class (jclass klass)
870 if (klass == JvPrimClass (byte))
871 return __alignof__ (jbyte);
872 else if (klass == JvPrimClass (short))
873 return __alignof__ (jshort);
874 else if (klass == JvPrimClass (int))
875 return __alignof__ (jint);
876 else if (klass == JvPrimClass (long))
877 return __alignof__ (jlong);
878 else if (klass == JvPrimClass (boolean))
879 return __alignof__ (jboolean);
880 else if (klass == JvPrimClass (char))
881 return __alignof__ (jchar);
882 else if (klass == JvPrimClass (float))
883 return __alignof__ (jfloat);
884 else if (klass == JvPrimClass (double))
885 return __alignof__ (jdouble);
886 else
887 return __alignof__ (jobject);
891 inline static unsigned char*
892 skip_one_type (unsigned char* ptr)
894 int ch = *ptr++;
896 while (ch == '[')
898 ch = *ptr++;
901 if (ch == 'L')
903 do { ch = *ptr++; } while (ch != ';');
906 return ptr;
909 static ffi_type*
910 get_ffi_type_from_signature (unsigned char* ptr)
912 switch (*ptr)
914 case 'L':
915 case '[':
916 return &ffi_type_pointer;
917 break;
919 case 'Z':
920 // On some platforms a bool is a byte, on others an int.
921 if (sizeof (jboolean) == sizeof (jbyte))
922 return &ffi_type_sint8;
923 else
925 JvAssert (sizeof (jbyte) == sizeof (jint));
926 return &ffi_type_sint32;
928 break;
930 case 'B':
931 return &ffi_type_sint8;
932 break;
934 case 'C':
935 return &ffi_type_uint16;
936 break;
938 case 'S':
939 return &ffi_type_sint16;
940 break;
942 case 'I':
943 return &ffi_type_sint32;
944 break;
946 case 'J':
947 return &ffi_type_sint64;
948 break;
950 case 'F':
951 return &ffi_type_float;
952 break;
954 case 'D':
955 return &ffi_type_double;
956 break;
958 case 'V':
959 return &ffi_type_void;
960 break;
963 throw_internal_error ("unknown type in signature");
966 /* this function yields the number of actual arguments, that is, if the
967 * function is non-static, then one is added to the number of elements
968 * found in the signature */
970 int
971 _Jv_count_arguments (_Jv_Utf8Const *signature,
972 jboolean staticp)
974 unsigned char *ptr = (unsigned char*) signature->data;
975 int arg_count = staticp ? 0 : 1;
977 /* first, count number of arguments */
979 // skip '('
980 ptr++;
982 // count args
983 while (*ptr != ')')
985 ptr = skip_one_type (ptr);
986 arg_count += 1;
989 return arg_count;
992 /* This beast will build a cif, given the signature. Memory for
993 * the cif itself and for the argument types must be allocated by the
994 * caller.
997 static int
998 init_cif (_Jv_Utf8Const* signature,
999 int arg_count,
1000 jboolean staticp,
1001 ffi_cif *cif,
1002 ffi_type **arg_types,
1003 ffi_type **rtype_p)
1005 unsigned char *ptr = (unsigned char*) signature->data;
1007 int arg_index = 0; // arg number
1008 int item_count = 0; // stack-item count
1010 // setup receiver
1011 if (!staticp)
1013 arg_types[arg_index++] = &ffi_type_pointer;
1014 item_count += 1;
1017 // skip '('
1018 ptr++;
1020 // assign arg types
1021 while (*ptr != ')')
1023 arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
1025 if (*ptr == 'J' || *ptr == 'D')
1026 item_count += 2;
1027 else
1028 item_count += 1;
1030 ptr = skip_one_type (ptr);
1033 // skip ')'
1034 ptr++;
1035 ffi_type *rtype = get_ffi_type_from_signature (ptr);
1037 ptr = skip_one_type (ptr);
1038 if (ptr != (unsigned char*)signature->data + signature->length)
1039 throw_internal_error ("did not find end of signature");
1041 if (ffi_prep_cif (cif, FFI_DEFAULT_ABI,
1042 arg_count, rtype, arg_types) != FFI_OK)
1043 throw_internal_error ("ffi_prep_cif failed");
1045 if (rtype_p != NULL)
1046 *rtype_p = rtype;
1048 return item_count;
1051 #if FFI_NATIVE_RAW_API
1052 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1053 # define FFI_RAW_SIZE ffi_raw_size
1054 #else
1055 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1056 # define FFI_RAW_SIZE ffi_java_raw_size
1057 #endif
1059 /* we put this one here, and not in interpret.cc because it
1060 * calls the utility routines _Jv_count_arguments
1061 * which are static to this module. The following struct defines the
1062 * layout we use for the stubs, it's only used in the ncode method. */
1064 typedef struct {
1065 ffi_raw_closure closure;
1066 ffi_cif cif;
1067 ffi_type *arg_types[0];
1068 } ncode_closure;
1070 typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*);
1072 void *
1073 _Jv_InterpMethod::ncode ()
1075 using namespace java::lang::reflect;
1077 if (self->ncode != 0)
1078 return self->ncode;
1080 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1081 int arg_count = _Jv_count_arguments (self->signature, staticp);
1083 ncode_closure *closure =
1084 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1085 + arg_count * sizeof (ffi_type*));
1087 init_cif (self->signature,
1088 arg_count,
1089 staticp,
1090 &closure->cif,
1091 &closure->arg_types[0],
1092 NULL);
1094 ffi_closure_fun fun;
1096 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1098 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
1100 if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
1102 if (staticp)
1103 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
1104 else
1105 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
1107 else
1109 fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
1112 FFI_PREP_RAW_CLOSURE (&closure->closure,
1113 &closure->cif,
1114 fun,
1115 (void*)this);
1117 self->ncode = (void*)closure;
1118 return self->ncode;
1122 void *
1123 _Jv_JNIMethod::ncode ()
1125 using namespace java::lang::reflect;
1127 if (self->ncode != 0)
1128 return self->ncode;
1130 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
1131 int arg_count = _Jv_count_arguments (self->signature, staticp);
1133 ncode_closure *closure =
1134 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
1135 + arg_count * sizeof (ffi_type*));
1137 ffi_type *rtype;
1138 init_cif (self->signature,
1139 arg_count,
1140 staticp,
1141 &closure->cif,
1142 &closure->arg_types[0],
1143 &rtype);
1145 ffi_closure_fun fun;
1147 args_raw_size = FFI_RAW_SIZE (&closure->cif);
1149 // Initialize the argument types and CIF that represent the actual
1150 // underlying JNI function.
1151 int extra_args = 1;
1152 if ((self->accflags & Modifier::STATIC))
1153 ++extra_args;
1154 jni_arg_types = (ffi_type **) _Jv_Malloc ((extra_args + arg_count)
1155 * sizeof (ffi_type *));
1156 int offset = 0;
1157 jni_arg_types[offset++] = &ffi_type_pointer;
1158 if ((self->accflags & Modifier::STATIC))
1159 jni_arg_types[offset++] = &ffi_type_pointer;
1160 memcpy (&jni_arg_types[offset], &closure->arg_types[0],
1161 arg_count * sizeof (ffi_type *));
1163 if (ffi_prep_cif (&jni_cif, FFI_DEFAULT_ABI,
1164 extra_args + arg_count, rtype,
1165 jni_arg_types) != FFI_OK)
1166 throw_internal_error ("ffi_prep_cif failed for JNI function");
1168 JvAssert ((self->accflags & Modifier::NATIVE) != 0);
1170 // FIXME: for now we assume that all native methods for
1171 // interpreted code use JNI.
1172 fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
1174 FFI_PREP_RAW_CLOSURE (&closure->closure,
1175 &closure->cif,
1176 fun,
1177 (void*) this);
1179 self->ncode = (void *) closure;
1180 return self->ncode;
1184 /* A _Jv_ResolvedMethod is what is put in the constant pool for a
1185 * MethodRef or InterfacemethodRef. */
1186 static _Jv_ResolvedMethod*
1187 _Jv_BuildResolvedMethod (_Jv_Method* method,
1188 jclass klass,
1189 jboolean staticp,
1190 jint vtable_index)
1192 int arg_count = _Jv_count_arguments (method->signature, staticp);
1194 _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
1195 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
1196 + arg_count*sizeof (ffi_type*));
1198 result->stack_item_count
1199 = init_cif (method->signature,
1200 arg_count,
1201 staticp,
1202 &result->cif,
1203 &result->arg_types[0],
1204 NULL);
1206 result->vtable_index = vtable_index;
1207 result->method = method;
1208 result->klass = klass;
1210 return result;
1214 static void
1215 throw_class_format_error (jstring msg)
1217 throw (msg
1218 ? new java::lang::ClassFormatError (msg)
1219 : new java::lang::ClassFormatError);
1222 static void
1223 throw_class_format_error (char *msg)
1225 throw_class_format_error (JvNewStringLatin1 (msg));
1228 static void
1229 throw_internal_error (char *msg)
1231 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
1235 #endif /* INTERPRETER */