1 // resolve.cc - Code for linking and resolving classes and pool entries.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 /* Author: Kresten Krab Thorup <krab@gnu.org> */
16 #include <java-interp.h>
21 #include <java-cpool.h>
22 #include <java/lang/Class.h>
23 #include <java/lang/String.h>
24 #include <java/lang/StringBuffer.h>
25 #include <java/lang/Thread.h>
26 #include <java/lang/InternalError.h>
27 #include <java/lang/VirtualMachineError.h>
28 #include <java/lang/NoSuchFieldError.h>
29 #include <java/lang/NoSuchMethodError.h>
30 #include <java/lang/ClassFormatError.h>
31 #include <java/lang/IllegalAccessError.h>
32 #include <java/lang/AbstractMethodError.h>
33 #include <java/lang/NoClassDefFoundError.h>
34 #include <java/lang/IncompatibleClassChangeError.h>
35 #include <java/lang/VMClassLoader.h>
36 #include <java/lang/reflect/Modifier.h>
41 _Jv_ResolveField (_Jv_Field
*field
, java::lang::ClassLoader
*loader
)
43 if (! field
->isResolved ())
45 _Jv_Utf8Const
*sig
= (_Jv_Utf8Const
*)field
->type
;
46 field
->type
= _Jv_FindClassFromSignature (sig
->data
, loader
);
47 field
->flags
&= ~_Jv_FIELD_UNRESOLVED_FLAG
;
53 static void throw_internal_error (char *msg
)
54 __attribute__ ((__noreturn__
));
55 static void throw_class_format_error (jstring msg
)
56 __attribute__ ((__noreturn__
));
57 static void throw_class_format_error (char *msg
)
58 __attribute__ ((__noreturn__
));
60 static int get_alignment_from_class (jclass
);
62 static _Jv_ResolvedMethod
*
63 _Jv_BuildResolvedMethod (_Jv_Method
*,
69 static void throw_incompatible_class_change_error (jstring msg
)
71 throw new java::lang::IncompatibleClassChangeError (msg
);
75 _Jv_ResolvePoolEntry (jclass klass
, int index
)
77 using namespace java::lang::reflect
;
79 _Jv_Constants
*pool
= &klass
->constants
;
81 if ((pool
->tags
[index
] & JV_CONSTANT_ResolvedFlag
) != 0)
82 return pool
->data
[index
];
84 switch (pool
->tags
[index
]) {
85 case JV_CONSTANT_Class
:
87 _Jv_Utf8Const
*name
= pool
->data
[index
].utf8
;
90 if (name
->data
[0] == '[')
91 found
= _Jv_FindClassFromSignature (&name
->data
[0],
94 found
= _Jv_FindClass (name
, klass
->loader
);
98 jstring str
= _Jv_NewStringUTF (name
->data
);
99 // This exception is specified in JLS 2nd Ed, section 5.1.
100 throw new java::lang::NoClassDefFoundError (str
);
103 if ((found
->accflags
& Modifier::PUBLIC
) == Modifier::PUBLIC
104 || (_Jv_ClassNameSamePackage (found
->name
,
107 pool
->data
[index
].clazz
= found
;
108 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
112 throw new java::lang::IllegalAccessError (found
->getName());
117 case JV_CONSTANT_String
:
120 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
121 pool
->data
[index
].o
= str
;
122 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
127 case JV_CONSTANT_Fieldref
:
129 _Jv_ushort class_index
, name_and_type_index
;
130 _Jv_loadIndexes (&pool
->data
[index
],
132 name_and_type_index
);
133 jclass owner
= (_Jv_ResolvePoolEntry (klass
, class_index
)).clazz
;
136 _Jv_InitClass (owner
);
138 _Jv_ushort name_index
, type_index
;
139 _Jv_loadIndexes (&pool
->data
[name_and_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
,
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
))
170 if (_Jv_CheckAccess (klass
, cls
, field
->flags
))
172 /* resove the field using the class' own loader
175 if (!field
->isResolved ())
176 _Jv_ResolveField (field
, cls
->loader
);
178 if (field_type
!= 0 && field
->type
!= field_type
)
179 throw new java::lang::LinkageError
181 ("field type mismatch with different loaders"));
184 goto end_of_field_search
;
188 throw new java::lang::IllegalAccessError
;
196 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
197 sb
->append(JvNewStringLatin1("field "));
198 sb
->append(owner
->getName());
199 sb
->append(JvNewStringLatin1("."));
200 sb
->append(_Jv_NewStringUTF(field_name
->data
));
201 sb
->append(JvNewStringLatin1(" was not found."));
202 throw_incompatible_class_change_error(sb
->toString());
205 pool
->data
[index
].field
= the_field
;
206 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
210 case JV_CONSTANT_Methodref
:
211 case JV_CONSTANT_InterfaceMethodref
:
213 _Jv_ushort class_index
, name_and_type_index
;
214 _Jv_loadIndexes (&pool
->data
[index
],
216 name_and_type_index
);
217 jclass owner
= (_Jv_ResolvePoolEntry (klass
, class_index
)).clazz
;
220 _Jv_InitClass (owner
);
222 _Jv_ushort name_index
, type_index
;
223 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
227 _Jv_Utf8Const
*method_name
= pool
->data
[name_index
].utf8
;
228 _Jv_Utf8Const
*method_signature
= pool
->data
[type_index
].utf8
;
230 _Jv_Method
*the_method
= 0;
231 jclass found_class
= 0;
233 // First search the class itself.
234 the_method
= _Jv_SearchMethodInClass (owner
, klass
,
235 method_name
, method_signature
);
240 goto end_of_method_search
;
243 // If we are resolving an interface method, search the
244 // interface's superinterfaces (A superinterface is not an
245 // interface's superclass - a superinterface is implemented by
247 if (pool
->tags
[index
] == JV_CONSTANT_InterfaceMethodref
)
252 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
* sizeof (jclass
*));
254 _Jv_GetInterfaces (owner
, &ifaces
);
256 for (int i
= 0; i
< ifaces
.count
; i
++)
258 jclass cls
= ifaces
.list
[i
];
259 the_method
= _Jv_SearchMethodInClass (cls
, klass
, method_name
,
268 _Jv_Free (ifaces
.list
);
271 goto end_of_method_search
;
274 // Finally, search superclasses.
275 for (jclass cls
= owner
->getSuperclass (); cls
!= 0;
276 cls
= cls
->getSuperclass ())
278 the_method
= _Jv_SearchMethodInClass (cls
, klass
,
279 method_name
, method_signature
);
287 end_of_method_search
:
289 // FIXME: if (cls->loader != klass->loader), then we
290 // must actually check that the types of arguments
291 // correspond. That is, for each argument type, and
292 // the return type, doing _Jv_FindClassFromSignature
293 // with either loader should produce the same result,
294 // i.e., exactly the same jclass object. JVMS 5.4.3.3
298 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
299 sb
->append(JvNewStringLatin1("method "));
300 sb
->append(owner
->getName());
301 sb
->append(JvNewStringLatin1("."));
302 sb
->append(_Jv_NewStringUTF(method_name
->data
));
303 sb
->append(JvNewStringLatin1(" was not found."));
304 throw new java::lang::NoSuchMethodError (sb
->toString());
307 int vtable_index
= -1;
308 if (pool
->tags
[index
] != JV_CONSTANT_InterfaceMethodref
)
309 vtable_index
= (jshort
)the_method
->index
;
311 pool
->data
[index
].rmethod
=
312 _Jv_BuildResolvedMethod(the_method
,
314 (the_method
->accflags
& Modifier::STATIC
) != 0,
316 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
322 return pool
->data
[index
];
325 // Find a method declared in the cls that is referenced from klass and
326 // perform access checks.
328 _Jv_SearchMethodInClass (jclass cls
, jclass klass
,
329 _Jv_Utf8Const
*method_name
,
330 _Jv_Utf8Const
*method_signature
)
332 using namespace java::lang::reflect
;
334 for (int i
= 0; i
< cls
->method_count
; i
++)
336 _Jv_Method
*method
= &cls
->methods
[i
];
337 if ( (!_Jv_equalUtf8Consts (method
->name
,
339 || (!_Jv_equalUtf8Consts (method
->signature
,
343 if (_Jv_CheckAccess (klass
, cls
, method
->accflags
))
346 throw new java::lang::IllegalAccessError
;
351 // A helper for _Jv_PrepareClass. This adds missing `Miranda methods'
354 _Jv_PrepareMissingMethods (jclass base2
, jclass iface_class
)
356 _Jv_InterpClass
*base
= reinterpret_cast<_Jv_InterpClass
*> (base2
);
357 for (int i
= 0; i
< iface_class
->interface_count
; ++i
)
359 for (int j
= 0; j
< iface_class
->interfaces
[i
]->method_count
; ++j
)
361 _Jv_Method
*meth
= &iface_class
->interfaces
[i
]->methods
[j
];
362 // Don't bother with <clinit>.
363 if (meth
->name
->data
[0] == '<')
365 _Jv_Method
*new_meth
= _Jv_LookupDeclaredMethod (base
, meth
->name
,
369 // We assume that such methods are very unlikely, so we
370 // just reallocate the method array each time one is
371 // found. This greatly simplifies the searching --
372 // otherwise we have to make sure that each such method
373 // found is really unique among all superinterfaces.
374 int new_count
= base
->method_count
+ 1;
376 = (_Jv_Method
*) _Jv_AllocBytes (sizeof (_Jv_Method
)
378 memcpy (new_m
, base
->methods
,
379 sizeof (_Jv_Method
) * base
->method_count
);
382 new_m
[base
->method_count
] = *meth
;
383 new_m
[base
->method_count
].index
= (_Jv_ushort
) -1;
384 new_m
[base
->method_count
].accflags
385 |= java::lang::reflect::Modifier::INVISIBLE
;
387 _Jv_MethodBase
**new_im
388 = (_Jv_MethodBase
**) _Jv_AllocBytes (sizeof (_Jv_MethodBase
*)
390 memcpy (new_im
, base
->interpreted_methods
,
391 sizeof (_Jv_MethodBase
*) * base
->method_count
);
393 base
->methods
= new_m
;
394 base
->interpreted_methods
= new_im
;
395 base
->method_count
= new_count
;
399 _Jv_PrepareMissingMethods (base
, iface_class
->interfaces
[i
]);
404 _Jv_PrepareClass(jclass klass
)
406 using namespace java::lang::reflect
;
409 * The job of this function is to: 1) assign storage to fields, and 2)
410 * build the vtable. static fields are assigned real memory, instance
411 * fields are assigned offsets.
413 * NOTE: we have a contract with the garbage collector here. Static
414 * reference fields must not be resolved, until after they have storage
415 * assigned which is the check used by the collector to see if it
416 * should indirect the static field reference and mark the object
419 * Most fields are resolved lazily (i.e. have their class-type
420 * assigned) when they are accessed the first time by calling as part
421 * of _Jv_ResolveField, which is allways called after _Jv_PrepareClass.
422 * Static fields with initializers are resolved as part of this
423 * function, as are fields with primitive types.
426 if (! _Jv_IsInterpretedClass (klass
))
429 if (klass
->state
>= JV_STATE_PREPARED
)
432 // Make sure super-class is linked. This involves taking a lock on
433 // the super class, so we use the Java method resolveClass, which
434 // will unlock it properly, should an exception happen. If there's
435 // no superclass, do nothing -- Object will already have been
438 if (klass
->superclass
)
439 java::lang::VMClassLoader::resolveClass (klass
->superclass
);
441 _Jv_InterpClass
*clz
= (_Jv_InterpClass
*)klass
;
443 /************ PART ONE: OBJECT LAYOUT ***************/
445 // Compute the alignment for this type by searching through the
446 // superclasses and finding the maximum required alignment. We
447 // could consider caching this in the Class.
448 int max_align
= __alignof__ (java::lang::Object
);
449 jclass super
= clz
->superclass
;
450 while (super
!= NULL
)
452 int num
= JvNumInstanceFields (super
);
453 _Jv_Field
*field
= JvGetFirstInstanceField (super
);
456 int field_align
= get_alignment_from_class (field
->type
);
457 if (field_align
> max_align
)
458 max_align
= field_align
;
462 super
= super
->superclass
;
468 // Although java.lang.Object is never interpreted, an interface can
469 // have a null superclass. Note that we have to lay out an
470 // interface because it might have static fields.
472 instance_size
= clz
->superclass
->size();
474 instance_size
= java::lang::Object::class$
.size();
476 for (int i
= 0; i
< clz
->field_count
; i
++)
481 _Jv_Field
*field
= &clz
->fields
[i
];
483 if (! field
->isRef ())
485 // it's safe to resolve the field here, since it's
486 // a primitive class, which does not cause loading to happen.
487 _Jv_ResolveField (field
, clz
->loader
);
489 field_size
= field
->type
->size ();
490 field_align
= get_alignment_from_class (field
->type
);
494 field_size
= sizeof (jobject
);
495 field_align
= __alignof__ (jobject
);
498 #ifndef COMPACT_FIELDS
499 field
->bsize
= field_size
;
502 if (field
->flags
& Modifier::STATIC
)
504 /* this computes an offset into a region we'll allocate
505 shortly, and then add this offset to the start address */
507 static_size
= ROUND (static_size
, field_align
);
508 field
->u
.boffset
= static_size
;
509 static_size
+= field_size
;
513 instance_size
= ROUND (instance_size
, field_align
);
514 field
->u
.boffset
= instance_size
;
515 instance_size
+= field_size
;
516 if (field_align
> max_align
)
517 max_align
= field_align
;
521 // Set the instance size for the class. Note that first we round it
522 // to the alignment required for this object; this keeps us in sync
523 // with our current ABI.
524 instance_size
= ROUND (instance_size
, max_align
);
525 clz
->size_in_bytes
= instance_size
;
527 // allocate static memory
528 if (static_size
!= 0)
530 char *static_data
= (char*)_Jv_AllocBytes (static_size
);
532 memset (static_data
, 0, static_size
);
534 for (int i
= 0; i
< clz
->field_count
; i
++)
536 _Jv_Field
*field
= &clz
->fields
[i
];
538 if ((field
->flags
& Modifier::STATIC
) != 0)
540 field
->u
.addr
= static_data
+ field
->u
.boffset
;
542 if (clz
->field_initializers
[i
] != 0)
544 _Jv_ResolveField (field
, clz
->loader
);
545 _Jv_InitField (0, clz
, i
);
550 // now we don't need the field_initializers anymore, so let the
551 // collector get rid of it!
553 clz
->field_initializers
= 0;
556 /************ PART TWO: VTABLE LAYOUT ***************/
558 /* preparation: build the vtable stubs (even interfaces can)
559 have code -- for static constructors. */
560 for (int i
= 0; i
< clz
->method_count
; i
++)
562 _Jv_MethodBase
*imeth
= clz
->interpreted_methods
[i
];
564 if ((clz
->methods
[i
].accflags
& Modifier::NATIVE
) != 0)
566 // You might think we could use a virtual `ncode' method in
567 // the _Jv_MethodBase and unify the native and non-native
568 // cases. Well, we can't, because we don't allocate these
569 // objects using `new', and thus they don't get a vtable.
570 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
571 clz
->methods
[i
].ncode
= jnim
->ncode ();
573 else if (imeth
!= 0) // it could be abstract
575 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
576 _Jv_VerifyMethod (im
);
577 clz
->methods
[i
].ncode
= im
->ncode ();
579 // Resolve ctable entries pointing to this method. See
580 // _Jv_Defer_Resolution.
581 void **code
= (void **)imeth
->deferred
;
584 void **target
= (void **)*code
;
585 *code
= clz
->methods
[i
].ncode
;
591 if ((clz
->accflags
& Modifier::INTERFACE
))
593 clz
->state
= JV_STATE_PREPARED
;
598 // A class might have so-called "Miranda methods". This is a method
599 // that is declared in an interface and not re-declared in an
600 // abstract class. Some compilers don't emit declarations for such
601 // methods in the class; this will give us problems since we expect
602 // a declaration for any method requiring a vtable entry. We handle
603 // this here by searching for such methods and constructing new
604 // internal declarations for them. We only need to do this for
606 if ((clz
->accflags
& Modifier::ABSTRACT
))
607 _Jv_PrepareMissingMethods (clz
, clz
);
609 clz
->vtable_method_count
= -1;
610 _Jv_MakeVTable (clz
);
612 /* wooha! we're done. */
613 clz
->state
= JV_STATE_PREPARED
;
617 /** Do static initialization for fields with a constant initializer */
619 _Jv_InitField (jobject obj
, jclass klass
, int index
)
621 using namespace java::lang::reflect
;
623 if (obj
!= 0 && klass
== 0)
624 klass
= obj
->getClass ();
626 if (!_Jv_IsInterpretedClass (klass
))
629 _Jv_InterpClass
*clz
= (_Jv_InterpClass
*)klass
;
631 _Jv_Field
* field
= (&clz
->fields
[0]) + index
;
633 if (index
> clz
->field_count
)
634 throw_internal_error ("field out of range");
636 int init
= clz
->field_initializers
[index
];
640 _Jv_Constants
*pool
= &clz
->constants
;
641 int tag
= pool
->tags
[init
];
643 if (! field
->isResolved ())
644 throw_internal_error ("initializing unresolved field");
646 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
647 throw_internal_error ("initializing non-static field with no object");
651 if ((field
->flags
& Modifier::STATIC
) != 0)
652 addr
= (void*) field
->u
.addr
;
654 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
658 case JV_CONSTANT_String
:
660 _Jv_MonitorEnter (clz
);
662 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
663 pool
->data
[init
].string
= str
;
664 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
665 _Jv_MonitorExit (clz
);
669 case JV_CONSTANT_ResolvedString
:
670 if (! (field
->type
== &StringClass
671 || field
->type
== &java::lang::Class::class$
))
672 throw_class_format_error ("string initialiser to non-string field");
674 *(jstring
*)addr
= pool
->data
[init
].string
;
677 case JV_CONSTANT_Integer
:
679 int value
= pool
->data
[init
].i
;
681 if (field
->type
== JvPrimClass (boolean
))
682 *(jboolean
*)addr
= (jboolean
)value
;
684 else if (field
->type
== JvPrimClass (byte
))
685 *(jbyte
*)addr
= (jbyte
)value
;
687 else if (field
->type
== JvPrimClass (char))
688 *(jchar
*)addr
= (jchar
)value
;
690 else if (field
->type
== JvPrimClass (short))
691 *(jshort
*)addr
= (jshort
)value
;
693 else if (field
->type
== JvPrimClass (int))
694 *(jint
*)addr
= (jint
)value
;
697 throw_class_format_error ("erroneous field initializer");
701 case JV_CONSTANT_Long
:
702 if (field
->type
!= JvPrimClass (long))
703 throw_class_format_error ("erroneous field initializer");
705 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
708 case JV_CONSTANT_Float
:
709 if (field
->type
!= JvPrimClass (float))
710 throw_class_format_error ("erroneous field initializer");
712 *(jfloat
*)addr
= pool
->data
[init
].f
;
715 case JV_CONSTANT_Double
:
716 if (field
->type
!= JvPrimClass (double))
717 throw_class_format_error ("erroneous field initializer");
719 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
723 throw_class_format_error ("erroneous field initializer");
733 #define ALIGNOF(TYPE) (__alignof__ (((aligner<TYPE> *) 0)->field))
735 // This returns the alignment of a type as it would appear in a
736 // structure. This can be different from the alignment of the type
737 // itself. For instance on x86 double is 8-aligned but struct{double}
740 get_alignment_from_class (jclass klass
)
742 if (klass
== JvPrimClass (byte
))
743 return ALIGNOF (jbyte
);
744 else if (klass
== JvPrimClass (short))
745 return ALIGNOF (jshort
);
746 else if (klass
== JvPrimClass (int))
747 return ALIGNOF (jint
);
748 else if (klass
== JvPrimClass (long))
749 return ALIGNOF (jlong
);
750 else if (klass
== JvPrimClass (boolean
))
751 return ALIGNOF (jboolean
);
752 else if (klass
== JvPrimClass (char))
753 return ALIGNOF (jchar
);
754 else if (klass
== JvPrimClass (float))
755 return ALIGNOF (jfloat
);
756 else if (klass
== JvPrimClass (double))
757 return ALIGNOF (jdouble
);
759 return ALIGNOF (jobject
);
763 inline static unsigned char*
764 skip_one_type (unsigned char* ptr
)
775 do { ch
= *ptr
++; } while (ch
!= ';');
782 get_ffi_type_from_signature (unsigned char* ptr
)
788 return &ffi_type_pointer
;
792 // On some platforms a bool is a byte, on others an int.
793 if (sizeof (jboolean
) == sizeof (jbyte
))
794 return &ffi_type_sint8
;
797 JvAssert (sizeof (jbyte
) == sizeof (jint
));
798 return &ffi_type_sint32
;
803 return &ffi_type_sint8
;
807 return &ffi_type_uint16
;
811 return &ffi_type_sint16
;
815 return &ffi_type_sint32
;
819 return &ffi_type_sint64
;
823 return &ffi_type_float
;
827 return &ffi_type_double
;
831 return &ffi_type_void
;
835 throw_internal_error ("unknown type in signature");
838 /* this function yields the number of actual arguments, that is, if the
839 * function is non-static, then one is added to the number of elements
840 * found in the signature */
843 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
846 unsigned char *ptr
= (unsigned char*) signature
->data
;
847 int arg_count
= staticp
? 0 : 1;
849 /* first, count number of arguments */
857 ptr
= skip_one_type (ptr
);
864 /* This beast will build a cif, given the signature. Memory for
865 * the cif itself and for the argument types must be allocated by the
870 init_cif (_Jv_Utf8Const
* signature
,
874 ffi_type
**arg_types
,
877 unsigned char *ptr
= (unsigned char*) signature
->data
;
879 int arg_index
= 0; // arg number
880 int item_count
= 0; // stack-item count
885 arg_types
[arg_index
++] = &ffi_type_pointer
;
895 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
897 if (*ptr
== 'J' || *ptr
== 'D')
902 ptr
= skip_one_type (ptr
);
907 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
909 ptr
= skip_one_type (ptr
);
910 if (ptr
!= (unsigned char*)signature
->data
+ signature
->length
)
911 throw_internal_error ("did not find end of signature");
913 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
914 arg_count
, rtype
, arg_types
) != FFI_OK
)
915 throw_internal_error ("ffi_prep_cif failed");
923 #if FFI_NATIVE_RAW_API
924 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
925 # define FFI_RAW_SIZE ffi_raw_size
927 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
928 # define FFI_RAW_SIZE ffi_java_raw_size
931 /* we put this one here, and not in interpret.cc because it
932 * calls the utility routines _Jv_count_arguments
933 * which are static to this module. The following struct defines the
934 * layout we use for the stubs, it's only used in the ncode method. */
937 ffi_raw_closure closure
;
939 ffi_type
*arg_types
[0];
942 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
945 _Jv_InterpMethod::ncode ()
947 using namespace java::lang::reflect
;
949 if (self
->ncode
!= 0)
952 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
953 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
955 ncode_closure
*closure
=
956 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
957 + arg_count
* sizeof (ffi_type
*));
959 init_cif (self
->signature
,
963 &closure
->arg_types
[0],
968 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
970 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
972 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
975 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
977 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
982 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
984 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
987 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
992 self
->ncode
= (void*)closure
;
997 _Jv_JNIMethod::ncode ()
999 using namespace java::lang::reflect
;
1001 if (self
->ncode
!= 0)
1004 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
1005 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
1007 ncode_closure
*closure
=
1008 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
1009 + arg_count
* sizeof (ffi_type
*));
1012 init_cif (self
->signature
,
1016 &closure
->arg_types
[0],
1019 ffi_closure_fun fun
;
1021 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
1023 // Initialize the argument types and CIF that represent the actual
1024 // underlying JNI function.
1026 if ((self
->accflags
& Modifier::STATIC
))
1028 jni_arg_types
= (ffi_type
**) _Jv_Malloc ((extra_args
+ arg_count
)
1029 * sizeof (ffi_type
*));
1031 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1032 if ((self
->accflags
& Modifier::STATIC
))
1033 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1034 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1035 arg_count
* sizeof (ffi_type
*));
1037 if (ffi_prep_cif (&jni_cif
, _Jv_platform_ffi_abi
,
1038 extra_args
+ arg_count
, rtype
,
1039 jni_arg_types
) != FFI_OK
)
1040 throw_internal_error ("ffi_prep_cif failed for JNI function");
1042 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1044 // FIXME: for now we assume that all native methods for
1045 // interpreted code use JNI.
1046 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1048 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1053 self
->ncode
= (void *) closure
;
1058 /* A _Jv_ResolvedMethod is what is put in the constant pool for a
1059 * MethodRef or InterfacemethodRef. */
1060 static _Jv_ResolvedMethod
*
1061 _Jv_BuildResolvedMethod (_Jv_Method
* method
,
1066 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1068 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1069 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1070 + arg_count
*sizeof (ffi_type
*));
1072 result
->stack_item_count
1073 = init_cif (method
->signature
,
1077 &result
->arg_types
[0],
1080 result
->vtable_index
= vtable_index
;
1081 result
->method
= method
;
1082 result
->klass
= klass
;
1089 throw_class_format_error (jstring msg
)
1092 ? new java::lang::ClassFormatError (msg
)
1093 : new java::lang::ClassFormatError
);
1097 throw_class_format_error (char *msg
)
1099 throw_class_format_error (JvNewStringLatin1 (msg
));
1103 throw_internal_error (char *msg
)
1105 throw new java::lang::InternalError (JvNewStringLatin1 (msg
));
1109 #endif /* INTERPRETER */