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
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
15 #include <java-interp.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>
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
;
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
*,
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 throw new java::lang::IncompatibleClassChangeError (msg
);
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
;
92 if (name
->data
[0] == '[')
93 found
= _Jv_FindClassFromSignature (&name
->data
[0],
96 found
= _Jv_FindClass (name
, klass
->loader
);
100 jstring str
= _Jv_NewStringUTF (name
->data
);
101 throw new java::lang::ClassNotFoundException (str
);
104 if ((found
->accflags
& Modifier::PUBLIC
) == Modifier::PUBLIC
105 || (_Jv_ClassNameSamePackage (found
->name
,
108 pool
->data
[index
].clazz
= found
;
109 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
113 throw new java::lang::IllegalAccessError (found
->getName());
118 case JV_CONSTANT_String
:
121 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
122 pool
->data
[index
].o
= str
;
123 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
128 case JV_CONSTANT_Fieldref
:
130 _Jv_ushort class_index
, name_and_type_index
;
131 _Jv_loadIndexes (&pool
->data
[index
],
133 name_and_type_index
);
134 jclass owner
= (_Jv_ResolvePoolEntry (klass
, class_index
)).clazz
;
137 _Jv_InitClass (owner
);
139 _Jv_ushort name_index
, type_index
;
140 _Jv_loadIndexes (&pool
->data
[name_and_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
,
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
))
171 // now, check field access.
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
,
181 /* resove the field using the class' own loader
184 if (!field
->isResolved ())
185 _Jv_ResolveField (field
, cls
->loader
);
187 if (field_type
!= 0 && field
->type
!= field_type
)
188 throw new java::lang::LinkageError
190 ("field type mismatch with different loaders"));
193 goto end_of_field_search
;
197 throw new java::lang::IllegalAccessError
;
205 jstring msg
= JvNewStringLatin1 ("field ");
206 msg
= msg
->concat (owner
->getName ());
207 msg
= msg
->concat (JvNewStringLatin1("."));
208 msg
= msg
->concat (_Jv_NewStringUTF (field_name
->data
));
209 msg
= msg
->concat (JvNewStringLatin1(" was not found."));
210 throw_incompatible_class_change_error (msg
);
213 pool
->data
[index
].field
= the_field
;
214 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
218 case JV_CONSTANT_Methodref
:
219 case JV_CONSTANT_InterfaceMethodref
:
221 _Jv_ushort class_index
, name_and_type_index
;
222 _Jv_loadIndexes (&pool
->data
[index
],
224 name_and_type_index
);
225 jclass owner
= (_Jv_ResolvePoolEntry (klass
, class_index
)).clazz
;
228 _Jv_InitClass (owner
);
230 _Jv_ushort name_index
, type_index
;
231 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
235 _Jv_Utf8Const
*method_name
= pool
->data
[name_index
].utf8
;
236 _Jv_Utf8Const
*method_signature
= pool
->data
[type_index
].utf8
;
238 int vtable_index
= -1;
239 _Jv_Method
*the_method
= 0;
240 jclass found_class
= 0;
242 // First search the class itself.
243 the_method
= _Jv_SearchMethodInClass (owner
, klass
,
244 method_name
, method_signature
);
249 goto end_of_method_search
;
252 // If we are resolving an interface method, search the interface's
253 // superinterfaces (A superinterface is not an interface's superclass -
254 // a superinterface is implemented by the interface).
255 if (pool
->tags
[index
] == JV_CONSTANT_InterfaceMethodref
)
260 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
* sizeof (jclass
*));
262 _Jv_GetInterfaces (owner
, &ifaces
);
264 for (int i
=0; i
< ifaces
.count
; i
++)
266 jclass cls
= ifaces
.list
[i
];
267 the_method
= _Jv_SearchMethodInClass (cls
, klass
, method_name
,
276 _Jv_Free (ifaces
.list
);
279 goto end_of_method_search
;
282 // Finally, search superclasses.
283 for (jclass cls
= owner
->getSuperclass (); cls
!= 0;
284 cls
= cls
->getSuperclass ())
286 the_method
= _Jv_SearchMethodInClass (cls
, klass
,
287 method_name
, method_signature
);
295 end_of_method_search
:
297 // FIXME: if (cls->loader != klass->loader), then we
298 // must actually check that the types of arguments
299 // correspond. That is, for each argument type, and
300 // the return type, doing _Jv_FindClassFromSignature
301 // with either loader should produce the same result,
302 // i.e., exactly the same jclass object. JVMS 5.4.3.3
304 if (pool
->tags
[index
] == JV_CONSTANT_InterfaceMethodref
)
307 vtable_index
= _Jv_DetermineVTableIndex
308 (found_class
, method_name
, method_signature
);
310 if (vtable_index
== METHOD_NOT_THERE
)
311 throw_incompatible_class_change_error
312 (JvNewStringLatin1 ("method not found"));
316 jstring msg
= JvNewStringLatin1 ("method ");
317 msg
= msg
->concat (owner
->getName ());
318 msg
= msg
->concat (JvNewStringLatin1("."));
319 msg
= msg
->concat (_Jv_NewStringUTF (method_name
->data
));
320 msg
= msg
->concat (JvNewStringLatin1(" was not found."));
321 throw new java::lang::NoSuchMethodError (msg
);
324 pool
->data
[index
].rmethod
=
325 _Jv_BuildResolvedMethod(the_method
,
327 (the_method
->accflags
& Modifier::STATIC
) != 0,
329 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
335 return pool
->data
[index
];
338 // Find a method declared in the cls that is referenced from klass and
339 // perform access checks.
341 _Jv_SearchMethodInClass (jclass cls
, jclass klass
,
342 _Jv_Utf8Const
*method_name
,
343 _Jv_Utf8Const
*method_signature
)
345 using namespace java::lang::reflect
;
347 for (int i
= 0; i
< cls
->method_count
; i
++)
349 _Jv_Method
*method
= &cls
->methods
[i
];
350 if ( (!_Jv_equalUtf8Consts (method
->name
,
352 || (!_Jv_equalUtf8Consts (method
->signature
,
357 || ((method
->accflags
& Modifier::PUBLIC
) != 0)
358 || (((method
->accflags
& Modifier::PROTECTED
) != 0)
359 && cls
->isAssignableFrom (klass
))
360 || (((method
->accflags
& Modifier::PRIVATE
) == 0)
361 && _Jv_ClassNameSamePackage (cls
->name
,
368 throw new java::lang::IllegalAccessError
;
374 /** FIXME: this is a terribly inefficient algorithm! It would improve
375 things if compiled classes to know vtable offset, and _Jv_Method had
378 Returns METHOD_NOT_THERE if this class does not declare the given method.
379 Returns METHOD_INACCESSIBLE if the given method does not appear in the
380 vtable, i.e., it is static, private, final or a constructor.
381 Otherwise, returns the vtable index. */
383 _Jv_DetermineVTableIndex (jclass klass
,
385 _Jv_Utf8Const
*signature
)
387 using namespace java::lang::reflect
;
389 jclass super_class
= klass
->getSuperclass ();
391 if (super_class
!= NULL
)
393 int prev
= _Jv_DetermineVTableIndex (super_class
,
396 if (prev
!= METHOD_NOT_THERE
)
400 /* at this point, we know that the super-class does not declare
401 * the method. Otherwise, the above call would have found it, and
402 * determined the result of this function (-1 or some positive
406 _Jv_Method
*meth
= _Jv_GetMethodLocal (klass
, name
, signature
);
408 /* now, if we do not declare this method, return zero */
410 return METHOD_NOT_THERE
;
412 /* so now, we know not only that the super class does not declare the
413 * method, but we do! So, this is a first declaration of the method. */
415 /* now, the checks for things that are declared in this class, but do
416 * not go into the vtable. There are three cases.
417 * 1) the method is static, private or final
418 * 2) the class itself is final, or
419 * 3) it is the method <init>
422 if ((meth
->accflags
& (Modifier::STATIC
424 | Modifier::FINAL
)) != 0
425 || (klass
->accflags
& Modifier::FINAL
) != 0
426 || _Jv_equalUtf8Consts (name
, init_name
))
427 return METHOD_INACCESSIBLE
;
429 /* reaching this point, we know for sure, that the method in question
430 * will be in the vtable. The question is where. */
432 /* the base offset, is where we will start assigning vtable
433 * indexes for this class. It is 0 for base classes
434 * and for non-base classes it is the
435 * number of entries in the super class' vtable. */
438 if (super_class
== 0)
441 base_offset
= super_class
->vtable_method_count
;
443 /* we will consider methods 0..this_method_index-1. And for each one,
444 * determine if it is new (i.e., if it appears in the super class),
445 * and if it should go in the vtable. If so, increment base_offset */
447 int this_method_index
= meth
- (&klass
->methods
[0]);
449 for (int i
= 0; i
< this_method_index
; i
++)
451 _Jv_Method
*m
= &klass
->methods
[i
];
453 /* fist some checks for things that surely do not go in the
456 if ((m
->accflags
& (Modifier::STATIC
| Modifier::PRIVATE
)) != 0)
458 if (_Jv_equalUtf8Consts (m
->name
, init_name
))
461 /* Then, we need to know if this method appears in the
462 superclass. (This is where this function gets expensive) */
463 _Jv_Method
*sm
= _Jv_LookupDeclaredMethod (super_class
,
467 /* if it was somehow declared in the superclass, skip this */
471 /* but if it is final, and not declared in the super class,
472 * then we also skip it */
473 if ((m
->accflags
& Modifier::FINAL
) != 0)
476 /* finally, we can assign the index of this method */
477 /* m->vtable_index = base_offset */
484 /* this is installed in place of abstract methods */
486 _Jv_abstractMethodError ()
488 throw new java::lang::AbstractMethodError
;
492 _Jv_PrepareClass(jclass klass
)
494 using namespace java::lang::reflect
;
497 * The job of this function is to: 1) assign storage to fields, and 2)
498 * build the vtable. static fields are assigned real memory, instance
499 * fields are assigned offsets.
501 * NOTE: we have a contract with the garbage collector here. Static
502 * reference fields must not be resolved, until after they have storage
503 * assigned which is the check used by the collector to see if it
504 * should indirect the static field reference and mark the object
507 * Most fields are resolved lazily (i.e. have their class-type
508 * assigned) when they are accessed the first time by calling as part
509 * of _Jv_ResolveField, which is allways called after _Jv_PrepareClass.
510 * Static fields with initializers are resolved as part of this
511 * function, as are fields with primitive types.
514 if (! _Jv_IsInterpretedClass (klass
))
517 if (klass
->state
>= JV_STATE_PREPARED
)
520 // make sure super-class is linked. This involves taking a lock on
521 // the super class, so we use the Java method resolveClass, which will
522 // unlock it properly, should an exception happen.
524 java::lang::ClassLoader::resolveClass0 (klass
->superclass
);
526 _Jv_InterpClass
*clz
= (_Jv_InterpClass
*)klass
;
528 /************ PART ONE: OBJECT LAYOUT ***************/
533 // java.lang.Object is never interpreted!
534 instance_size
= clz
->superclass
->size ();
537 for (int i
= 0; i
< clz
->field_count
; i
++)
542 _Jv_Field
*field
= &clz
->fields
[i
];
544 if (! field
->isRef ())
546 // it's safe to resolve the field here, since it's
547 // a primitive class, which does not cause loading to happen.
548 _Jv_ResolveField (field
, clz
->loader
);
550 field_size
= field
->type
->size ();
551 field_align
= get_alignment_from_class (field
->type
);
555 field_size
= sizeof (jobject
);
556 field_align
= __alignof__ (jobject
);
559 #ifndef COMPACT_FIELDS
560 field
->bsize
= field_size
;
563 if (field
->flags
& Modifier::STATIC
)
565 /* this computes an offset into a region we'll allocate
566 shortly, and then add this offset to the start address */
568 static_size
= ROUND (static_size
, field_align
);
569 field
->u
.boffset
= static_size
;
570 static_size
+= field_size
;
574 instance_size
= ROUND (instance_size
, field_align
);
575 field
->u
.boffset
= instance_size
;
576 instance_size
+= field_size
;
580 // set the instance size for the class
581 clz
->size_in_bytes
= instance_size
;
583 // allocate static memory
584 if (static_size
!= 0)
586 char *static_data
= (char*)_Jv_AllocBytes (static_size
);
588 memset (static_data
, 0, static_size
);
590 for (int i
= 0; i
< clz
->field_count
; i
++)
592 _Jv_Field
*field
= &clz
->fields
[i
];
594 if ((field
->flags
& Modifier::STATIC
) != 0)
596 field
->u
.addr
= static_data
+ field
->u
.boffset
;
598 if (clz
->field_initializers
[i
] != 0)
600 _Jv_ResolveField (field
, clz
->loader
);
601 _Jv_InitField (0, clz
, i
);
606 // now we don't need the field_initializers anymore, so let the
607 // collector get rid of it!
609 clz
->field_initializers
= 0;
612 /************ PART TWO: VTABLE LAYOUT ***************/
614 /* preparation: build the vtable stubs (even interfaces can)
615 have code -- for static constructors. */
616 for (int i
= 0; i
< clz
->method_count
; i
++)
618 _Jv_MethodBase
*imeth
= clz
->interpreted_methods
[i
];
620 if ((clz
->methods
[i
].accflags
& Modifier::NATIVE
) != 0)
622 // You might think we could use a virtual `ncode' method in
623 // the _Jv_MethodBase and unify the native and non-native
624 // cases. Well, we can't, because we don't allocate these
625 // objects using `new', and thus they don't get a vtable.
626 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
627 clz
->methods
[i
].ncode
= jnim
->ncode ();
629 else if (imeth
!= 0) // it could be abstract
631 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
632 clz
->methods
[i
].ncode
= im
->ncode ();
636 if (clz
->accflags
& Modifier::INTERFACE
)
638 clz
->state
= JV_STATE_PREPARED
;
643 /* Now onto the actual job: vtable layout. First, count how many new
645 int new_method_count
= 0;
647 jclass super_class
= clz
->getSuperclass ();
649 if (super_class
== 0)
650 throw_internal_error ("cannot handle interpreted base classes");
652 for (int i
= 0; i
< clz
->method_count
; i
++)
654 _Jv_Method
*this_meth
= &clz
->methods
[i
];
656 if ((this_meth
->accflags
& (Modifier::STATIC
| Modifier::PRIVATE
)) != 0
657 || _Jv_equalUtf8Consts (this_meth
->name
, init_name
))
659 /* skip this, it doesn't go in the vtable */
663 _Jv_Method
*orig_meth
= _Jv_LookupDeclaredMethod (super_class
,
665 this_meth
->signature
);
669 // new methods that are final, also don't go in the vtable
670 if ((this_meth
->accflags
& Modifier::FINAL
) != 0)
673 new_method_count
+= 1;
677 if ((orig_meth
->accflags
& (Modifier::STATIC
679 | Modifier::FINAL
)) != 0
680 || ((orig_meth
->accflags
& Modifier::ABSTRACT
) == 0
681 && (this_meth
->accflags
& Modifier::ABSTRACT
) != 0
682 && (klass
->accflags
& Modifier::ABSTRACT
) == 0))
684 clz
->state
= JV_STATE_ERROR
;
686 throw new java::lang::IncompatibleClassChangeError (clz
->getName ());
689 /* FIXME: At this point, if (loader != super_class->loader), we
690 * need to "impose class loader constraints" for the types
691 * involved in the signature of this method */
695 int vtable_count
= (super_class
->vtable_method_count
) + new_method_count
;
696 clz
->vtable_method_count
= vtable_count
;
698 /* allocate vtable structure */
699 _Jv_VTable
*vtable
= _Jv_VTable::new_vtable (vtable_count
);
701 vtable
->gc_descr
= _Jv_BuildGCDescr(clz
);
704 jclass effective_superclass
= super_class
;
706 /* If super_class is abstract or an interface it has no vtable.
707 We need to find a real one... */
708 while (effective_superclass
&& effective_superclass
->vtable
== NULL
)
709 effective_superclass
= effective_superclass
->superclass
;
711 /* copy super class' vtable entries. */
712 if (effective_superclass
&& effective_superclass
->vtable
)
713 for (int i
= 0; i
< effective_superclass
->vtable_method_count
; ++i
)
714 vtable
->set_method (i
, effective_superclass
->vtable
->get_method (i
));
717 /* now, install our own vtable entries, reprise... */
718 for (int i
= 0; i
< clz
->method_count
; i
++)
720 _Jv_Method
*this_meth
= &clz
->methods
[i
];
722 int index
= _Jv_DetermineVTableIndex (clz
,
724 this_meth
->signature
);
726 if (index
== METHOD_NOT_THERE
)
727 throw_internal_error ("method now found in own class");
729 if (index
!= METHOD_INACCESSIBLE
)
731 if (index
> clz
->vtable_method_count
)
732 throw_internal_error ("vtable problem...");
734 if (clz
->interpreted_methods
[i
] == 0)
735 vtable
->set_method(i
, (void*)&_Jv_abstractMethodError
);
737 vtable
->set_method(i
, this_meth
->ncode
);
741 /* finally, assign the vtable! */
742 clz
->vtable
= vtable
;
744 /* wooha! we're done. */
745 clz
->state
= JV_STATE_PREPARED
;
749 /** Do static initialization for fields with a constant initializer */
751 _Jv_InitField (jobject obj
, jclass klass
, int index
)
753 using namespace java::lang::reflect
;
755 if (obj
!= 0 && klass
== 0)
756 klass
= obj
->getClass ();
758 if (!_Jv_IsInterpretedClass (klass
))
761 _Jv_InterpClass
*clz
= (_Jv_InterpClass
*)klass
;
763 _Jv_Field
* field
= (&clz
->fields
[0]) + index
;
765 if (index
> clz
->field_count
)
766 throw_internal_error ("field out of range");
768 int init
= clz
->field_initializers
[index
];
772 _Jv_Constants
*pool
= &clz
->constants
;
773 int tag
= pool
->tags
[init
];
775 if (! field
->isResolved ())
776 throw_internal_error ("initializing unresolved field");
778 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
779 throw_internal_error ("initializing non-static field with no object");
783 if ((field
->flags
& Modifier::STATIC
) != 0)
784 addr
= (void*) field
->u
.addr
;
786 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
790 case JV_CONSTANT_String
:
792 _Jv_MonitorEnter (clz
);
794 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
795 pool
->data
[init
].string
= str
;
796 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
797 _Jv_MonitorExit (clz
);
801 case JV_CONSTANT_ResolvedString
:
802 if (! (field
->type
== &StringClass
803 || field
->type
== &java::lang::Class::class$
))
804 throw_class_format_error ("string initialiser to non-string field");
806 *(jstring
*)addr
= pool
->data
[init
].string
;
809 case JV_CONSTANT_Integer
:
811 int value
= pool
->data
[init
].i
;
813 if (field
->type
== JvPrimClass (boolean
))
814 *(jboolean
*)addr
= (jboolean
)value
;
816 else if (field
->type
== JvPrimClass (byte
))
817 *(jbyte
*)addr
= (jbyte
)value
;
819 else if (field
->type
== JvPrimClass (char))
820 *(jchar
*)addr
= (jchar
)value
;
822 else if (field
->type
== JvPrimClass (short))
823 *(jshort
*)addr
= (jshort
)value
;
825 else if (field
->type
== JvPrimClass (int))
826 *(jint
*)addr
= (jint
)value
;
829 throw_class_format_error ("erroneous field initializer");
833 case JV_CONSTANT_Long
:
834 if (field
->type
!= JvPrimClass (long))
835 throw_class_format_error ("erroneous field initializer");
837 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
840 case JV_CONSTANT_Float
:
841 if (field
->type
!= JvPrimClass (float))
842 throw_class_format_error ("erroneous field initializer");
844 *(jfloat
*)addr
= pool
->data
[init
].f
;
847 case JV_CONSTANT_Double
:
848 if (field
->type
!= JvPrimClass (double))
849 throw_class_format_error ("erroneous field initializer");
851 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
855 throw_class_format_error ("erroneous field initializer");
860 get_alignment_from_class (jclass klass
)
862 if (klass
== JvPrimClass (byte
))
863 return __alignof__ (jbyte
);
864 else if (klass
== JvPrimClass (short))
865 return __alignof__ (jshort
);
866 else if (klass
== JvPrimClass (int))
867 return __alignof__ (jint
);
868 else if (klass
== JvPrimClass (long))
869 return __alignof__ (jlong
);
870 else if (klass
== JvPrimClass (boolean
))
871 return __alignof__ (jboolean
);
872 else if (klass
== JvPrimClass (char))
873 return __alignof__ (jchar
);
874 else if (klass
== JvPrimClass (float))
875 return __alignof__ (jfloat
);
876 else if (klass
== JvPrimClass (double))
877 return __alignof__ (jdouble
);
879 return __alignof__ (jobject
);
883 inline static unsigned char*
884 skip_one_type (unsigned char* ptr
)
895 do { ch
= *ptr
++; } while (ch
!= ';');
902 get_ffi_type_from_signature (unsigned char* ptr
)
908 return &ffi_type_pointer
;
912 // On some platforms a bool is a byte, on others an int.
913 if (sizeof (jboolean
) == sizeof (jbyte
))
914 return &ffi_type_sint8
;
917 JvAssert (sizeof (jbyte
) == sizeof (jint
));
918 return &ffi_type_sint32
;
923 return &ffi_type_sint8
;
927 return &ffi_type_uint16
;
931 return &ffi_type_sint16
;
935 return &ffi_type_sint32
;
939 return &ffi_type_sint64
;
943 return &ffi_type_float
;
947 return &ffi_type_double
;
951 return &ffi_type_void
;
955 throw_internal_error ("unknown type in signature");
958 /* this function yields the number of actual arguments, that is, if the
959 * function is non-static, then one is added to the number of elements
960 * found in the signature */
963 count_arguments (_Jv_Utf8Const
*signature
,
966 unsigned char *ptr
= (unsigned char*) signature
->data
;
967 int arg_count
= staticp
? 0 : 1;
969 /* first, count number of arguments */
977 ptr
= skip_one_type (ptr
);
984 /* This beast will build a cif, given the signature. Memory for
985 * the cif itself and for the argument types must be allocated by the
990 init_cif (_Jv_Utf8Const
* signature
,
994 ffi_type
**arg_types
,
997 unsigned char *ptr
= (unsigned char*) signature
->data
;
999 int arg_index
= 0; // arg number
1000 int item_count
= 0; // stack-item count
1005 arg_types
[arg_index
++] = &ffi_type_pointer
;
1015 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
1017 if (*ptr
== 'J' || *ptr
== 'D')
1022 ptr
= skip_one_type (ptr
);
1027 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
1029 ptr
= skip_one_type (ptr
);
1030 if (ptr
!= (unsigned char*)signature
->data
+ signature
->length
)
1031 throw_internal_error ("did not find end of signature");
1033 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
1034 arg_count
, rtype
, arg_types
) != FFI_OK
)
1035 throw_internal_error ("ffi_prep_cif failed");
1037 if (rtype_p
!= NULL
)
1043 #if FFI_NATIVE_RAW_API
1044 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
1045 # define FFI_RAW_SIZE ffi_raw_size
1047 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
1048 # define FFI_RAW_SIZE ffi_java_raw_size
1051 /* we put this one here, and not in interpret.cc because it
1052 * calls the utility routines count_arguments
1053 * which are static to this module. The following struct defines the
1054 * layout we use for the stubs, it's only used in the ncode method. */
1057 ffi_raw_closure closure
;
1059 ffi_type
*arg_types
[0];
1062 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
1065 _Jv_InterpMethod::ncode ()
1067 using namespace java::lang::reflect
;
1069 if (self
->ncode
!= 0)
1072 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1073 int arg_count
= count_arguments (self
->signature
, staticp
);
1075 ncode_closure
*closure
=
1076 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
1077 + arg_count
* sizeof (ffi_type
*));
1079 init_cif (self
->signature
,
1083 &closure
->arg_types
[0],
1086 ffi_closure_fun fun
;
1088 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1090 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
1092 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
1095 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
1097 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
1101 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
1104 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1109 self
->ncode
= (void*)closure
;
1115 _Jv_JNIMethod::ncode ()
1117 using namespace java::lang::reflect
;
1119 if (self
->ncode
!= 0)
1122 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1123 int arg_count
= count_arguments (self
->signature
, staticp
);
1125 ncode_closure
*closure
=
1126 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
1127 + arg_count
* sizeof (ffi_type
*));
1130 init_cif (self
->signature
,
1134 &closure
->arg_types
[0],
1137 ffi_closure_fun fun
;
1139 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1141 // Initialize the argument types and CIF that represent the actual
1142 // underlying JNI function.
1144 if ((self
->accflags
& Modifier::STATIC
))
1146 jni_arg_types
= (ffi_type
**) _Jv_Malloc ((extra_args
+ arg_count
)
1147 * sizeof (ffi_type
*));
1149 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1150 if ((self
->accflags
& Modifier::STATIC
))
1151 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1152 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1153 arg_count
* sizeof (ffi_type
*));
1155 if (ffi_prep_cif (&jni_cif
, FFI_DEFAULT_ABI
,
1156 extra_args
+ arg_count
, rtype
,
1157 jni_arg_types
) != FFI_OK
)
1158 throw_internal_error ("ffi_prep_cif failed for JNI function");
1160 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1162 // FIXME: for now we assume that all native methods for
1163 // interpreted code use JNI.
1164 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1166 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1171 self
->ncode
= (void *) closure
;
1176 /* A _Jv_ResolvedMethod is what is put in the constant pool for a
1177 * MethodRef or InterfacemethodRef. */
1178 static _Jv_ResolvedMethod
*
1179 _Jv_BuildResolvedMethod (_Jv_Method
* method
,
1184 int arg_count
= count_arguments (method
->signature
, staticp
);
1186 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1187 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1188 + arg_count
*sizeof (ffi_type
*));
1190 result
->stack_item_count
1191 = init_cif (method
->signature
,
1195 &result
->arg_types
[0],
1198 result
->vtable_index
= vtable_index
;
1199 result
->method
= method
;
1200 result
->klass
= klass
;
1207 throw_class_format_error (jstring msg
)
1210 ? new java::lang::ClassFormatError (msg
)
1211 : new java::lang::ClassFormatError
);
1215 throw_class_format_error (char *msg
)
1217 throw_class_format_error (JvNewStringLatin1 (msg
));
1221 throw_internal_error (char *msg
)
1223 throw new java::lang::InternalError (JvNewStringLatin1 (msg
));
1227 #endif /* INTERPRETER */