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> */
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/StringBuffer.h>
24 #include <java/lang/Thread.h>
25 #include <java/lang/InternalError.h>
26 #include <java/lang/VirtualMachineError.h>
27 #include <java/lang/NoSuchFieldError.h>
28 #include <java/lang/NoSuchMethodError.h>
29 #include <java/lang/ClassFormatError.h>
30 #include <java/lang/IllegalAccessError.h>
31 #include <java/lang/AbstractMethodError.h>
32 #include <java/lang/NoClassDefFoundError.h>
33 #include <java/lang/IncompatibleClassChangeError.h>
34 #include <java/lang/reflect/Modifier.h>
39 _Jv_ResolveField (_Jv_Field
*field
, java::lang::ClassLoader
*loader
)
41 if (! field
->isResolved ())
43 _Jv_Utf8Const
*sig
= (_Jv_Utf8Const
*)field
->type
;
44 field
->type
= _Jv_FindClassFromSignature (sig
->data
, loader
);
45 field
->flags
&= ~_Jv_FIELD_UNRESOLVED_FLAG
;
51 static void throw_internal_error (char *msg
)
52 __attribute__ ((__noreturn__
));
53 static void throw_class_format_error (jstring msg
)
54 __attribute__ ((__noreturn__
));
55 static void throw_class_format_error (char *msg
)
56 __attribute__ ((__noreturn__
));
58 static int get_alignment_from_class (jclass
);
60 static _Jv_ResolvedMethod
*
61 _Jv_BuildResolvedMethod (_Jv_Method
*,
67 static void throw_incompatible_class_change_error (jstring msg
)
69 throw new java::lang::IncompatibleClassChangeError (msg
);
73 _Jv_ResolvePoolEntry (jclass klass
, int index
)
75 using namespace java::lang::reflect
;
77 _Jv_Constants
*pool
= &klass
->constants
;
79 if ((pool
->tags
[index
] & JV_CONSTANT_ResolvedFlag
) != 0)
80 return pool
->data
[index
];
82 switch (pool
->tags
[index
]) {
83 case JV_CONSTANT_Class
:
85 _Jv_Utf8Const
*name
= pool
->data
[index
].utf8
;
88 if (name
->data
[0] == '[')
89 found
= _Jv_FindClassFromSignature (&name
->data
[0],
92 found
= _Jv_FindClass (name
, klass
->loader
);
96 jstring str
= _Jv_NewStringUTF (name
->data
);
97 // This exception is specified in JLS 2nd Ed, section 5.1.
98 throw new java::lang::NoClassDefFoundError (str
);
101 if ((found
->accflags
& Modifier::PUBLIC
) == Modifier::PUBLIC
102 || (_Jv_ClassNameSamePackage (found
->name
,
105 pool
->data
[index
].clazz
= found
;
106 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
110 throw new java::lang::IllegalAccessError (found
->getName());
115 case JV_CONSTANT_String
:
118 str
= _Jv_NewStringUtf8Const (pool
->data
[index
].utf8
);
119 pool
->data
[index
].o
= str
;
120 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
125 case JV_CONSTANT_Fieldref
:
127 _Jv_ushort class_index
, name_and_type_index
;
128 _Jv_loadIndexes (&pool
->data
[index
],
130 name_and_type_index
);
131 jclass owner
= (_Jv_ResolvePoolEntry (klass
, class_index
)).clazz
;
134 _Jv_InitClass (owner
);
136 _Jv_ushort name_index
, type_index
;
137 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
141 _Jv_Utf8Const
*field_name
= pool
->data
[name_index
].utf8
;
142 _Jv_Utf8Const
*field_type_name
= pool
->data
[type_index
].utf8
;
144 // FIXME: The implementation of this function
145 // (_Jv_FindClassFromSignature) will generate an instance of
146 // _Jv_Utf8Const for each call if the field type is a class name
147 // (Lxx.yy.Z;). This may be too expensive to do for each and
148 // every fieldref being resolved. For now, we fix the problem by
149 // only doing it when we have a loader different from the class
150 // declaring the field.
152 jclass field_type
= 0;
154 if (owner
->loader
!= klass
->loader
)
155 field_type
= _Jv_FindClassFromSignature (field_type_name
->data
,
158 _Jv_Field
* the_field
= 0;
160 for (jclass cls
= owner
; cls
!= 0; cls
= cls
->getSuperclass ())
162 for (int i
= 0; i
< cls
->field_count
; i
++)
164 _Jv_Field
*field
= &cls
->fields
[i
];
165 if (! _Jv_equalUtf8Consts (field
->name
, field_name
))
168 // now, check field access.
171 || ((field
->flags
& Modifier::PUBLIC
) != 0)
172 || (((field
->flags
& Modifier::PROTECTED
) != 0)
173 && cls
->isAssignableFrom (klass
))
174 || (((field
->flags
& Modifier::PRIVATE
) == 0)
175 && _Jv_ClassNameSamePackage (cls
->name
,
178 /* resove the field using the class' own loader
181 if (!field
->isResolved ())
182 _Jv_ResolveField (field
, cls
->loader
);
184 if (field_type
!= 0 && field
->type
!= field_type
)
185 throw new java::lang::LinkageError
187 ("field type mismatch with different loaders"));
190 goto end_of_field_search
;
194 throw new java::lang::IllegalAccessError
;
202 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
203 sb
->append(JvNewStringLatin1("field "));
204 sb
->append(owner
->getName());
205 sb
->append(JvNewStringLatin1("."));
206 sb
->append(_Jv_NewStringUTF(field_name
->data
));
207 sb
->append(JvNewStringLatin1(" was not found."));
208 throw_incompatible_class_change_error(sb
->toString());
211 pool
->data
[index
].field
= the_field
;
212 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
216 case JV_CONSTANT_Methodref
:
217 case JV_CONSTANT_InterfaceMethodref
:
219 _Jv_ushort class_index
, name_and_type_index
;
220 _Jv_loadIndexes (&pool
->data
[index
],
222 name_and_type_index
);
223 jclass owner
= (_Jv_ResolvePoolEntry (klass
, class_index
)).clazz
;
226 _Jv_InitClass (owner
);
228 _Jv_ushort name_index
, type_index
;
229 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
233 _Jv_Utf8Const
*method_name
= pool
->data
[name_index
].utf8
;
234 _Jv_Utf8Const
*method_signature
= pool
->data
[type_index
].utf8
;
236 _Jv_Method
*the_method
= 0;
237 jclass found_class
= 0;
239 // First search the class itself.
240 the_method
= _Jv_SearchMethodInClass (owner
, klass
,
241 method_name
, method_signature
);
246 goto end_of_method_search
;
249 // If we are resolving an interface method, search the
250 // interface's superinterfaces (A superinterface is not an
251 // interface's superclass - a superinterface is implemented by
253 if (pool
->tags
[index
] == JV_CONSTANT_InterfaceMethodref
)
258 ifaces
.list
= (jclass
*) _Jv_Malloc (ifaces
.len
* sizeof (jclass
*));
260 _Jv_GetInterfaces (owner
, &ifaces
);
262 for (int i
= 0; i
< ifaces
.count
; i
++)
264 jclass cls
= ifaces
.list
[i
];
265 the_method
= _Jv_SearchMethodInClass (cls
, klass
, method_name
,
274 _Jv_Free (ifaces
.list
);
277 goto end_of_method_search
;
280 // Finally, search superclasses.
281 for (jclass cls
= owner
->getSuperclass (); cls
!= 0;
282 cls
= cls
->getSuperclass ())
284 the_method
= _Jv_SearchMethodInClass (cls
, klass
,
285 method_name
, method_signature
);
293 end_of_method_search
:
295 // FIXME: if (cls->loader != klass->loader), then we
296 // must actually check that the types of arguments
297 // correspond. That is, for each argument type, and
298 // the return type, doing _Jv_FindClassFromSignature
299 // with either loader should produce the same result,
300 // i.e., exactly the same jclass object. JVMS 5.4.3.3
304 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer();
305 sb
->append(JvNewStringLatin1("method "));
306 sb
->append(owner
->getName());
307 sb
->append(JvNewStringLatin1("."));
308 sb
->append(_Jv_NewStringUTF(method_name
->data
));
309 sb
->append(JvNewStringLatin1(" was not found."));
310 throw new java::lang::NoSuchMethodError (sb
->toString());
313 int vtable_index
= -1;
314 if (pool
->tags
[index
] != JV_CONSTANT_InterfaceMethodref
)
315 vtable_index
= (jshort
)the_method
->index
;
317 pool
->data
[index
].rmethod
=
318 _Jv_BuildResolvedMethod(the_method
,
320 (the_method
->accflags
& Modifier::STATIC
) != 0,
322 pool
->tags
[index
] |= JV_CONSTANT_ResolvedFlag
;
328 return pool
->data
[index
];
331 // Find a method declared in the cls that is referenced from klass and
332 // perform access checks.
334 _Jv_SearchMethodInClass (jclass cls
, jclass klass
,
335 _Jv_Utf8Const
*method_name
,
336 _Jv_Utf8Const
*method_signature
)
338 using namespace java::lang::reflect
;
340 for (int i
= 0; i
< cls
->method_count
; i
++)
342 _Jv_Method
*method
= &cls
->methods
[i
];
343 if ( (!_Jv_equalUtf8Consts (method
->name
,
345 || (!_Jv_equalUtf8Consts (method
->signature
,
350 || ((method
->accflags
& Modifier::PUBLIC
) != 0)
351 || (((method
->accflags
& Modifier::PROTECTED
) != 0)
352 && cls
->isAssignableFrom (klass
))
353 || (((method
->accflags
& Modifier::PRIVATE
) == 0)
354 && _Jv_ClassNameSamePackage (cls
->name
,
361 throw new java::lang::IllegalAccessError
;
367 // A helper for _Jv_PrepareClass. This adds missing `Miranda methods'
370 _Jv_PrepareMissingMethods (jclass base2
, jclass iface_class
)
372 _Jv_InterpClass
*base
= reinterpret_cast<_Jv_InterpClass
*> (base2
);
373 for (int i
= 0; i
< iface_class
->interface_count
; ++i
)
375 for (int j
= 0; j
< iface_class
->interfaces
[i
]->method_count
; ++j
)
377 _Jv_Method
*meth
= &iface_class
->interfaces
[i
]->methods
[j
];
378 // Don't bother with <clinit>.
379 if (meth
->name
->data
[0] == '<')
381 _Jv_Method
*new_meth
= _Jv_LookupDeclaredMethod (base
, meth
->name
,
385 // We assume that such methods are very unlikely, so we
386 // just reallocate the method array each time one is
387 // found. This greatly simplifies the searching --
388 // otherwise we have to make sure that each such method
389 // found is really unique among all superinterfaces.
390 int new_count
= base
->method_count
+ 1;
392 = (_Jv_Method
*) _Jv_AllocBytes (sizeof (_Jv_Method
)
394 memcpy (new_m
, base
->methods
,
395 sizeof (_Jv_Method
) * base
->method_count
);
398 new_m
[base
->method_count
] = *meth
;
399 new_m
[base
->method_count
].index
= (_Jv_ushort
) -1;
400 new_m
[base
->method_count
].accflags
401 |= java::lang::reflect::Modifier::INVISIBLE
;
403 _Jv_MethodBase
**new_im
404 = (_Jv_MethodBase
**) _Jv_AllocBytes (sizeof (_Jv_MethodBase
*)
406 memcpy (new_im
, base
->interpreted_methods
,
407 sizeof (_Jv_MethodBase
*) * base
->method_count
);
409 base
->methods
= new_m
;
410 base
->interpreted_methods
= new_im
;
411 base
->method_count
= new_count
;
415 _Jv_PrepareMissingMethods (base
, iface_class
->interfaces
[i
]);
420 _Jv_PrepareClass(jclass klass
)
422 using namespace java::lang::reflect
;
425 * The job of this function is to: 1) assign storage to fields, and 2)
426 * build the vtable. static fields are assigned real memory, instance
427 * fields are assigned offsets.
429 * NOTE: we have a contract with the garbage collector here. Static
430 * reference fields must not be resolved, until after they have storage
431 * assigned which is the check used by the collector to see if it
432 * should indirect the static field reference and mark the object
435 * Most fields are resolved lazily (i.e. have their class-type
436 * assigned) when they are accessed the first time by calling as part
437 * of _Jv_ResolveField, which is allways called after _Jv_PrepareClass.
438 * Static fields with initializers are resolved as part of this
439 * function, as are fields with primitive types.
442 if (! _Jv_IsInterpretedClass (klass
))
445 if (klass
->state
>= JV_STATE_PREPARED
)
448 // Make sure super-class is linked. This involves taking a lock on
449 // the super class, so we use the Java method resolveClass, which
450 // will unlock it properly, should an exception happen. If there's
451 // no superclass, do nothing -- Object will already have been
454 if (klass
->superclass
)
455 java::lang::ClassLoader::resolveClass0 (klass
->superclass
);
457 _Jv_InterpClass
*clz
= (_Jv_InterpClass
*)klass
;
459 /************ PART ONE: OBJECT LAYOUT ***************/
464 // Although java.lang.Object is never interpreted, an interface can
465 // have a null superclass.
467 instance_size
= clz
->superclass
->size();
469 instance_size
= java::lang::Object::class$
.size();
472 for (int i
= 0; i
< clz
->field_count
; i
++)
477 _Jv_Field
*field
= &clz
->fields
[i
];
479 if (! field
->isRef ())
481 // it's safe to resolve the field here, since it's
482 // a primitive class, which does not cause loading to happen.
483 _Jv_ResolveField (field
, clz
->loader
);
485 field_size
= field
->type
->size ();
486 field_align
= get_alignment_from_class (field
->type
);
490 field_size
= sizeof (jobject
);
491 field_align
= __alignof__ (jobject
);
494 #ifndef COMPACT_FIELDS
495 field
->bsize
= field_size
;
498 if (field
->flags
& Modifier::STATIC
)
500 /* this computes an offset into a region we'll allocate
501 shortly, and then add this offset to the start address */
503 static_size
= ROUND (static_size
, field_align
);
504 field
->u
.boffset
= static_size
;
505 static_size
+= field_size
;
509 instance_size
= ROUND (instance_size
, field_align
);
510 field
->u
.boffset
= instance_size
;
511 instance_size
+= field_size
;
515 // set the instance size for the class
516 clz
->size_in_bytes
= instance_size
;
518 // allocate static memory
519 if (static_size
!= 0)
521 char *static_data
= (char*)_Jv_AllocBytes (static_size
);
523 memset (static_data
, 0, static_size
);
525 for (int i
= 0; i
< clz
->field_count
; i
++)
527 _Jv_Field
*field
= &clz
->fields
[i
];
529 if ((field
->flags
& Modifier::STATIC
) != 0)
531 field
->u
.addr
= static_data
+ field
->u
.boffset
;
533 if (clz
->field_initializers
[i
] != 0)
535 _Jv_ResolveField (field
, clz
->loader
);
536 _Jv_InitField (0, clz
, i
);
541 // now we don't need the field_initializers anymore, so let the
542 // collector get rid of it!
544 clz
->field_initializers
= 0;
547 /************ PART TWO: VTABLE LAYOUT ***************/
549 /* preparation: build the vtable stubs (even interfaces can)
550 have code -- for static constructors. */
551 for (int i
= 0; i
< clz
->method_count
; i
++)
553 _Jv_MethodBase
*imeth
= clz
->interpreted_methods
[i
];
555 if ((clz
->methods
[i
].accflags
& Modifier::NATIVE
) != 0)
557 // You might think we could use a virtual `ncode' method in
558 // the _Jv_MethodBase and unify the native and non-native
559 // cases. Well, we can't, because we don't allocate these
560 // objects using `new', and thus they don't get a vtable.
561 _Jv_JNIMethod
*jnim
= reinterpret_cast<_Jv_JNIMethod
*> (imeth
);
562 clz
->methods
[i
].ncode
= jnim
->ncode ();
564 else if (imeth
!= 0) // it could be abstract
566 _Jv_InterpMethod
*im
= reinterpret_cast<_Jv_InterpMethod
*> (imeth
);
567 _Jv_VerifyMethod (im
);
568 clz
->methods
[i
].ncode
= im
->ncode ();
572 if ((clz
->accflags
& Modifier::INTERFACE
))
574 clz
->state
= JV_STATE_PREPARED
;
579 // A class might have so-called "Miranda methods". This is a method
580 // that is declared in an interface and not re-declared in an
581 // abstract class. Some compilers don't emit declarations for such
582 // methods in the class; this will give us problems since we expect
583 // a declaration for any method requiring a vtable entry. We handle
584 // this here by searching for such methods and constructing new
585 // internal declarations for them. We only need to do this for
587 if ((clz
->accflags
& Modifier::ABSTRACT
))
588 _Jv_PrepareMissingMethods (clz
, clz
);
590 clz
->vtable_method_count
= -1;
591 _Jv_MakeVTable (clz
);
593 /* wooha! we're done. */
594 clz
->state
= JV_STATE_PREPARED
;
598 /** Do static initialization for fields with a constant initializer */
600 _Jv_InitField (jobject obj
, jclass klass
, int index
)
602 using namespace java::lang::reflect
;
604 if (obj
!= 0 && klass
== 0)
605 klass
= obj
->getClass ();
607 if (!_Jv_IsInterpretedClass (klass
))
610 _Jv_InterpClass
*clz
= (_Jv_InterpClass
*)klass
;
612 _Jv_Field
* field
= (&clz
->fields
[0]) + index
;
614 if (index
> clz
->field_count
)
615 throw_internal_error ("field out of range");
617 int init
= clz
->field_initializers
[index
];
621 _Jv_Constants
*pool
= &clz
->constants
;
622 int tag
= pool
->tags
[init
];
624 if (! field
->isResolved ())
625 throw_internal_error ("initializing unresolved field");
627 if (obj
==0 && ((field
->flags
& Modifier::STATIC
) == 0))
628 throw_internal_error ("initializing non-static field with no object");
632 if ((field
->flags
& Modifier::STATIC
) != 0)
633 addr
= (void*) field
->u
.addr
;
635 addr
= (void*) (((char*)obj
) + field
->u
.boffset
);
639 case JV_CONSTANT_String
:
641 _Jv_MonitorEnter (clz
);
643 str
= _Jv_NewStringUtf8Const (pool
->data
[init
].utf8
);
644 pool
->data
[init
].string
= str
;
645 pool
->tags
[init
] = JV_CONSTANT_ResolvedString
;
646 _Jv_MonitorExit (clz
);
650 case JV_CONSTANT_ResolvedString
:
651 if (! (field
->type
== &StringClass
652 || field
->type
== &java::lang::Class::class$
))
653 throw_class_format_error ("string initialiser to non-string field");
655 *(jstring
*)addr
= pool
->data
[init
].string
;
658 case JV_CONSTANT_Integer
:
660 int value
= pool
->data
[init
].i
;
662 if (field
->type
== JvPrimClass (boolean
))
663 *(jboolean
*)addr
= (jboolean
)value
;
665 else if (field
->type
== JvPrimClass (byte
))
666 *(jbyte
*)addr
= (jbyte
)value
;
668 else if (field
->type
== JvPrimClass (char))
669 *(jchar
*)addr
= (jchar
)value
;
671 else if (field
->type
== JvPrimClass (short))
672 *(jshort
*)addr
= (jshort
)value
;
674 else if (field
->type
== JvPrimClass (int))
675 *(jint
*)addr
= (jint
)value
;
678 throw_class_format_error ("erroneous field initializer");
682 case JV_CONSTANT_Long
:
683 if (field
->type
!= JvPrimClass (long))
684 throw_class_format_error ("erroneous field initializer");
686 *(jlong
*)addr
= _Jv_loadLong (&pool
->data
[init
]);
689 case JV_CONSTANT_Float
:
690 if (field
->type
!= JvPrimClass (float))
691 throw_class_format_error ("erroneous field initializer");
693 *(jfloat
*)addr
= pool
->data
[init
].f
;
696 case JV_CONSTANT_Double
:
697 if (field
->type
!= JvPrimClass (double))
698 throw_class_format_error ("erroneous field initializer");
700 *(jdouble
*)addr
= _Jv_loadDouble (&pool
->data
[init
]);
704 throw_class_format_error ("erroneous field initializer");
709 get_alignment_from_class (jclass klass
)
711 if (klass
== JvPrimClass (byte
))
712 return __alignof__ (jbyte
);
713 else if (klass
== JvPrimClass (short))
714 return __alignof__ (jshort
);
715 else if (klass
== JvPrimClass (int))
716 return __alignof__ (jint
);
717 else if (klass
== JvPrimClass (long))
718 return __alignof__ (jlong
);
719 else if (klass
== JvPrimClass (boolean
))
720 return __alignof__ (jboolean
);
721 else if (klass
== JvPrimClass (char))
722 return __alignof__ (jchar
);
723 else if (klass
== JvPrimClass (float))
724 return __alignof__ (jfloat
);
725 else if (klass
== JvPrimClass (double))
726 return __alignof__ (jdouble
);
728 return __alignof__ (jobject
);
732 inline static unsigned char*
733 skip_one_type (unsigned char* ptr
)
744 do { ch
= *ptr
++; } while (ch
!= ';');
751 get_ffi_type_from_signature (unsigned char* ptr
)
757 return &ffi_type_pointer
;
761 // On some platforms a bool is a byte, on others an int.
762 if (sizeof (jboolean
) == sizeof (jbyte
))
763 return &ffi_type_sint8
;
766 JvAssert (sizeof (jbyte
) == sizeof (jint
));
767 return &ffi_type_sint32
;
772 return &ffi_type_sint8
;
776 return &ffi_type_uint16
;
780 return &ffi_type_sint16
;
784 return &ffi_type_sint32
;
788 return &ffi_type_sint64
;
792 return &ffi_type_float
;
796 return &ffi_type_double
;
800 return &ffi_type_void
;
804 throw_internal_error ("unknown type in signature");
807 /* this function yields the number of actual arguments, that is, if the
808 * function is non-static, then one is added to the number of elements
809 * found in the signature */
812 _Jv_count_arguments (_Jv_Utf8Const
*signature
,
815 unsigned char *ptr
= (unsigned char*) signature
->data
;
816 int arg_count
= staticp
? 0 : 1;
818 /* first, count number of arguments */
826 ptr
= skip_one_type (ptr
);
833 /* This beast will build a cif, given the signature. Memory for
834 * the cif itself and for the argument types must be allocated by the
839 init_cif (_Jv_Utf8Const
* signature
,
843 ffi_type
**arg_types
,
846 unsigned char *ptr
= (unsigned char*) signature
->data
;
848 int arg_index
= 0; // arg number
849 int item_count
= 0; // stack-item count
854 arg_types
[arg_index
++] = &ffi_type_pointer
;
864 arg_types
[arg_index
++] = get_ffi_type_from_signature (ptr
);
866 if (*ptr
== 'J' || *ptr
== 'D')
871 ptr
= skip_one_type (ptr
);
876 ffi_type
*rtype
= get_ffi_type_from_signature (ptr
);
878 ptr
= skip_one_type (ptr
);
879 if (ptr
!= (unsigned char*)signature
->data
+ signature
->length
)
880 throw_internal_error ("did not find end of signature");
882 if (ffi_prep_cif (cif
, FFI_DEFAULT_ABI
,
883 arg_count
, rtype
, arg_types
) != FFI_OK
)
884 throw_internal_error ("ffi_prep_cif failed");
892 #if FFI_NATIVE_RAW_API
893 # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure
894 # define FFI_RAW_SIZE ffi_raw_size
896 # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure
897 # define FFI_RAW_SIZE ffi_java_raw_size
900 /* we put this one here, and not in interpret.cc because it
901 * calls the utility routines _Jv_count_arguments
902 * which are static to this module. The following struct defines the
903 * layout we use for the stubs, it's only used in the ncode method. */
906 ffi_raw_closure closure
;
908 ffi_type
*arg_types
[0];
911 typedef void (*ffi_closure_fun
) (ffi_cif
*,void*,ffi_raw
*,void*);
914 _Jv_InterpMethod::ncode ()
916 using namespace java::lang::reflect
;
918 if (self
->ncode
!= 0)
921 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
922 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
924 ncode_closure
*closure
=
925 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
926 + arg_count
* sizeof (ffi_type
*));
928 init_cif (self
->signature
,
932 &closure
->arg_types
[0],
937 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
939 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
941 if ((self
->accflags
& Modifier::SYNCHRONIZED
) != 0)
944 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_class
;
946 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_synch_object
;
951 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_class
;
953 fun
= (ffi_closure_fun
)&_Jv_InterpMethod::run_normal
;
956 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
961 self
->ncode
= (void*)closure
;
966 _Jv_JNIMethod::ncode ()
968 using namespace java::lang::reflect
;
970 if (self
->ncode
!= 0)
973 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
974 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
976 ncode_closure
*closure
=
977 (ncode_closure
*)_Jv_AllocBytes (sizeof (ncode_closure
)
978 + arg_count
* sizeof (ffi_type
*));
981 init_cif (self
->signature
,
985 &closure
->arg_types
[0],
990 args_raw_size
= FFI_RAW_SIZE (&closure
->cif
);
992 // Initialize the argument types and CIF that represent the actual
993 // underlying JNI function.
995 if ((self
->accflags
& Modifier::STATIC
))
997 jni_arg_types
= (ffi_type
**) _Jv_Malloc ((extra_args
+ arg_count
)
998 * sizeof (ffi_type
*));
1000 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1001 if ((self
->accflags
& Modifier::STATIC
))
1002 jni_arg_types
[offset
++] = &ffi_type_pointer
;
1003 memcpy (&jni_arg_types
[offset
], &closure
->arg_types
[0],
1004 arg_count
* sizeof (ffi_type
*));
1006 // NOTE: This must agree with the JNICALL definition in jni.h
1008 #define FFI_JNI_ABI FFI_STDCALL
1010 #define FFI_JNI_ABI FFI_DEFAULT_ABI
1013 if (ffi_prep_cif (&jni_cif
, FFI_JNI_ABI
,
1014 extra_args
+ arg_count
, rtype
,
1015 jni_arg_types
) != FFI_OK
)
1016 throw_internal_error ("ffi_prep_cif failed for JNI function");
1018 JvAssert ((self
->accflags
& Modifier::NATIVE
) != 0);
1020 // FIXME: for now we assume that all native methods for
1021 // interpreted code use JNI.
1022 fun
= (ffi_closure_fun
) &_Jv_JNIMethod::call
;
1024 FFI_PREP_RAW_CLOSURE (&closure
->closure
,
1029 self
->ncode
= (void *) closure
;
1034 /* A _Jv_ResolvedMethod is what is put in the constant pool for a
1035 * MethodRef or InterfacemethodRef. */
1036 static _Jv_ResolvedMethod
*
1037 _Jv_BuildResolvedMethod (_Jv_Method
* method
,
1042 int arg_count
= _Jv_count_arguments (method
->signature
, staticp
);
1044 _Jv_ResolvedMethod
* result
= (_Jv_ResolvedMethod
*)
1045 _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod
)
1046 + arg_count
*sizeof (ffi_type
*));
1048 result
->stack_item_count
1049 = init_cif (method
->signature
,
1053 &result
->arg_types
[0],
1056 result
->vtable_index
= vtable_index
;
1057 result
->method
= method
;
1058 result
->klass
= klass
;
1065 throw_class_format_error (jstring msg
)
1068 ? new java::lang::ClassFormatError (msg
)
1069 : new java::lang::ClassFormatError
);
1073 throw_class_format_error (char *msg
)
1075 throw_class_format_error (JvNewStringLatin1 (msg
));
1079 throw_internal_error (char *msg
)
1081 throw new java::lang::InternalError (JvNewStringLatin1 (msg
));
1085 #endif /* INTERPRETER */