1 /* src/vmcore/linker.c - class linker functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "mm/memory.h"
35 #include "native/native.h"
37 #include "threads/lock-common.h"
39 #include "toolbox/logging.h"
41 #include "vm/access.h"
43 #include "vm/exceptions.hpp"
44 #include "vm/primitive.hpp"
45 #include "vm/string.hpp"
48 #include "vm/jit_interface.h"
50 #include "vmcore/class.h"
51 #include "vmcore/classcache.h"
52 #include "vmcore/globals.hpp"
53 #include "vmcore/loader.h"
54 #include "vmcore/options.h"
55 #include "vmcore/rt-timing.h"
58 /* debugging macros ***********************************************************/
61 # define TRACELINKCLASS(c) \
63 if (opt_TraceLinkClass) { \
65 log_print("[Linking "); \
72 # define TRACELINKCLASS(c)
76 /* #include "vm/resolve.h" */
77 /* copied prototype to avoid bootstrapping problem: */
78 classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool checkaccess);
80 #if defined(ENABLE_STATISTICS)
81 # include "vmcore/statistics.h"
84 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
85 #define INLINELOG(code) do { if (opt_TraceInlining) { code } } while (0)
87 #define INLINELOG(code)
91 /* global variables ***********************************************************/
93 static s4 interfaceindex; /* sequential numbering of interfaces */
96 java_object_t *linker_classrenumber_lock;
99 /* private functions **********************************************************/
101 static classinfo *link_class_intern(classinfo *c);
102 static arraydescriptor *link_array(classinfo *c);
103 static void linker_compute_class_values(classinfo *c);
104 static void linker_compute_subclasses(classinfo *c);
105 static bool linker_addinterface(classinfo *c, classinfo *ic);
106 static s4 class_highestinterface(classinfo *c);
109 /* dummy structures for alinment checks ***************************************/
111 typedef struct dummy_alignment_long_t dummy_alignment_long_t;
112 typedef struct dummy_alignment_double_t dummy_alignment_double_t;
114 struct dummy_alignment_long_t {
119 struct dummy_alignment_double_t {
125 /* linker_init *****************************************************************
127 Initializes the linker subsystem and links classes required for the
130 *******************************************************************************/
132 void linker_preinit(void)
134 TRACESUBSYSTEMINITIALIZATION("linker_preinit");
136 /* Check for if alignment for long and double matches what we
137 assume for the current architecture. */
139 #if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__)) || defined(__M68K__)
140 /* Define a define here which is later checked when we use this
143 # define LINKER_ALIGNMENT_LONG_DOUBLE 4
145 if (OFFSET(dummy_alignment_long_t, l) != 4)
146 vm_abort("linker_preinit: long alignment is different from what assumed: %d != %d",
147 OFFSET(dummy_alignment_long_t, l), 4);
149 if (OFFSET(dummy_alignment_double_t, d) != 4)
150 vm_abort("linker_preinit: double alignment is different from what assumed: %d != %d",
151 OFFSET(dummy_alignment_double_t, d), 4);
154 # define LINKER_ALIGNMENT_LONG_DOUBLE 8
156 if (OFFSET(dummy_alignment_long_t, l) != 8)
157 vm_abort("linker_preinit: long alignment is different from what assumed: %d != %d",
158 OFFSET(dummy_alignment_long_t, l), 8);
160 if (OFFSET(dummy_alignment_double_t, d) != 8)
161 vm_abort("linker_preinit: double alignment is different from what assumed: %d != %d",
162 OFFSET(dummy_alignment_double_t, d), 8);
165 /* Reset interface index. */
169 #if defined(ENABLE_THREADS)
170 /* create the global lock object */
172 linker_classrenumber_lock = NEW(java_object_t);
174 LOCK_INIT_OBJECT_LOCK(linker_classrenumber_lock);
177 /* Link the most basic classes. */
179 if (!link_class(class_java_lang_Object))
180 vm_abort("linker_preinit: linking java/lang/Object failed");
182 #if defined(ENABLE_JAVASE)
183 if (!link_class(class_java_lang_Cloneable))
184 vm_abort("linker_preinit: linking java/lang/Cloneable failed");
186 if (!link_class(class_java_io_Serializable))
187 vm_abort("linker_preinit: linking java/io/Serializable failed");
192 /* linker_init *****************************************************************
194 Links all classes required in the VM.
196 *******************************************************************************/
198 void linker_init(void)
200 TRACESUBSYSTEMINITIALIZATION("linker_init");
202 /* Link java.lang.Class as first class of the system, because we
203 need it's vftbl for all other classes so we can use a class as
206 if (!link_class(class_java_lang_Class))
207 vm_abort("linker_init: linking java/lang/Class failed");
209 /* Now set the header.vftbl of all classes which were created
210 before java.lang.Class was linked. */
212 class_postset_header_vftbl();
214 /* Link primitive-type wrapping classes. */
216 #if defined(ENABLE_JAVASE)
217 if (!link_class(class_java_lang_Void))
218 vm_abort("linker_init: linking failed");
221 if (!link_class(class_java_lang_Boolean))
222 vm_abort("linker_init: linking failed");
224 if (!link_class(class_java_lang_Byte))
225 vm_abort("linker_init: linking failed");
227 if (!link_class(class_java_lang_Character))
228 vm_abort("linker_init: linking failed");
230 if (!link_class(class_java_lang_Short))
231 vm_abort("linker_init: linking failed");
233 if (!link_class(class_java_lang_Integer))
234 vm_abort("linker_init: linking failed");
236 if (!link_class(class_java_lang_Long))
237 vm_abort("linker_init: linking failed");
239 if (!link_class(class_java_lang_Float))
240 vm_abort("linker_init: linking failed");
242 if (!link_class(class_java_lang_Double))
243 vm_abort("linker_init: linking failed");
245 /* Link important system classes. */
247 if (!link_class(class_java_lang_String))
248 vm_abort("linker_init: linking java/lang/String failed");
250 #if defined(ENABLE_JAVASE)
251 if (!link_class(class_java_lang_ClassLoader))
252 vm_abort("linker_init: linking failed");
254 if (!link_class(class_java_lang_SecurityManager))
255 vm_abort("linker_init: linking failed");
258 if (!link_class(class_java_lang_System))
259 vm_abort("linker_init: linking failed");
261 if (!link_class(class_java_lang_Thread))
262 vm_abort("linker_init: linking failed");
264 #if defined(ENABLE_JAVASE)
265 if (!link_class(class_java_lang_ThreadGroup))
266 vm_abort("linker_init: linking failed");
269 if (!link_class(class_java_lang_Throwable))
270 vm_abort("linker_init: linking failed");
272 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
273 if (!link_class(class_java_lang_VMSystem))
274 vm_abort("linker_init: linking failed");
276 if (!link_class(class_java_lang_VMThread))
277 vm_abort("linker_init: linking failed");
279 if (!link_class(class_java_lang_VMThrowable))
280 vm_abort("linker_init: linking failed");
283 /* Important system exceptions. */
285 if (!link_class(class_java_lang_Exception))
286 vm_abort("linker_init: linking failed");
288 if (!link_class(class_java_lang_ClassNotFoundException))
289 vm_abort("linker_init: linking failed");
291 if (!link_class(class_java_lang_RuntimeException))
292 vm_abort("linker_init: linking failed");
294 /* some classes which may be used more often */
296 #if defined(ENABLE_JAVASE)
297 if (!link_class(class_java_lang_StackTraceElement))
298 vm_abort("linker_init: linking failed");
300 if (!link_class(class_java_lang_reflect_Constructor))
301 vm_abort("linker_init: linking failed");
303 if (!link_class(class_java_lang_reflect_Field))
304 vm_abort("linker_init: linking failed");
306 if (!link_class(class_java_lang_reflect_Method))
307 vm_abort("linker_init: linking failed");
309 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
310 if (!link_class(class_java_lang_reflect_VMConstructor))
311 vm_abort("linker_init: linking failed");
313 if (!link_class(class_java_lang_reflect_VMField))
314 vm_abort("linker_init: linking failed");
316 if (!link_class(class_java_lang_reflect_VMMethod))
317 vm_abort("linker_init: linking failed");
320 if (!link_class(class_java_security_PrivilegedAction))
321 vm_abort("linker_init: linking failed");
323 if (!link_class(class_java_util_Vector))
324 vm_abort("linker_init: linking failed");
326 if (!link_class(class_java_util_HashMap))
327 vm_abort("linker_init: linking failed");
329 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
330 if (!link_class(class_sun_misc_Signal))
331 vm_abort("linker_init: linking failed");
333 if (!link_class(class_sun_reflect_MagicAccessorImpl))
334 vm_abort("linker_init: linking failed");
337 if (!link_class(arrayclass_java_lang_Object))
338 vm_abort("linker_init: linking failed");
342 /* create pseudo classes used by the typechecker */
344 /* pseudo class for Arraystubs (extends java.lang.Object) */
346 pseudo_class_Arraystub =
347 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
348 pseudo_class_Arraystub->state |= CLASS_LOADED;
349 pseudo_class_Arraystub->super = class_java_lang_Object;
351 #if defined(ENABLE_JAVASE)
353 pseudo_class_Arraystub->interfacescount = 2;
354 pseudo_class_Arraystub->interfaces = MNEW(classinfo*, 2);
355 pseudo_class_Arraystub->interfaces[0] = class_java_lang_Cloneable;
356 pseudo_class_Arraystub->interfaces[1] = class_java_io_Serializable;
358 #elif defined(ENABLE_JAVAME_CLDC1_1)
360 pseudo_class_Arraystub->interfacescount = 0;
361 pseudo_class_Arraystub->interfaces = NULL;
364 # error unknown Java configuration
367 if (!classcache_store_unique(pseudo_class_Arraystub))
368 vm_abort("linker_init: could not cache pseudo_class_Arraystub");
370 if (!link_class(pseudo_class_Arraystub))
371 vm_abort("linker_init: linking pseudo_class_Arraystub failed");
373 /* pseudo class representing the null type */
375 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
376 pseudo_class_Null->state |= CLASS_LOADED;
377 pseudo_class_Null->super = class_java_lang_Object;
379 if (!classcache_store_unique(pseudo_class_Null))
380 vm_abort("linker_init: could not cache pseudo_class_Null");
382 if (!link_class(pseudo_class_Null))
383 vm_abort("linker_init: linking failed");
385 /* pseudo class representing new uninitialized objects */
387 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
388 pseudo_class_New->state |= CLASS_LOADED;
389 pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
390 pseudo_class_New->super = class_java_lang_Object;
392 if (!classcache_store_unique(pseudo_class_New))
393 vm_abort("linker_init: could not cache pseudo_class_New");
395 /* Correct vftbl-entries (retarded loading and linking of class
396 java/lang/String). */
398 stringtable_update();
402 /* link_class ******************************************************************
404 Wrapper function for link_class_intern to ease monitor enter/exit
405 and exception handling.
407 *******************************************************************************/
409 classinfo *link_class(classinfo *c)
412 #if defined(ENABLE_RT_TIMING)
413 struct timespec time_start, time_end;
416 RT_TIMING_GET_TIME(time_start);
419 exceptions_throw_nullpointerexception();
423 LOCK_MONITOR_ENTER(c);
425 /* Maybe the class is currently linking or is already linked.*/
427 if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
428 LOCK_MONITOR_EXIT(c);
433 #if defined(ENABLE_STATISTICS)
436 if (opt_getcompilingtime)
437 compilingtime_stop();
439 if (opt_getloadingtime)
443 /* call the internal function */
445 r = link_class_intern(c);
447 /* If return value is NULL, we had a problem and the class is not
451 c->state &= ~CLASS_LINKING;
453 #if defined(ENABLE_STATISTICS)
456 if (opt_getloadingtime)
459 if (opt_getcompilingtime)
460 compilingtime_start();
463 LOCK_MONITOR_EXIT(c);
465 RT_TIMING_GET_TIME(time_end);
467 RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
473 /* linker_overwrite_method *****************************************************
475 Overwrite a method with another one, update method flags and check
479 mg................the general method being overwritten
480 ms................the overwriting (more specialized) method
481 wl................worklist where to add invalidated methods
484 true..............everything ok
485 false.............an exception has been thrown
487 *******************************************************************************/
489 static bool linker_overwrite_method(methodinfo *mg,
491 method_worklist **wl)
499 /* overriding a final method is illegal */
501 if (mg->flags & ACC_FINAL) {
502 exceptions_throw_verifyerror(mg, "Overriding final method");
506 /* method ms overwrites method mg */
508 #if defined(ENABLE_VERIFIER)
509 /* Add loading constraints (for the more general types of method mg). */
510 /* Not for <init>, as it is not invoked virtually. */
512 if ((ms->name != utf_init)
513 && !classcache_add_constraints_for_params(
514 cs->classloader, cg->classloader, mg))
520 /* inherit the vftbl index, and record the overwriting */
522 ms->vftblindex = mg->vftblindex;
525 /* update flags and check assumptions */
526 /* <init> methods are a special case, as they are never dispatched dynamically */
528 if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
531 #if defined(ENABLE_TLH)
532 if (mg->flags & ACC_METHOD_MONOMORPHY_USED) {
533 printf("%s/%s is evil! the siner is %s/%s\n", mg->clazz->name->text, mg->name->text,
534 ms->clazz->name->text, ms->name->text);
535 ms->flags |= ACC_METHOD_PARENT_MONOMORPHY_USED;
539 if (mg->flags & ACC_METHOD_IMPLEMENTED) {
540 /* this adds another implementation */
542 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
544 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
546 method_break_assumption_monomorphic(mg, wl);
549 /* this is the first implementation */
551 mg->flags |= ACC_METHOD_IMPLEMENTED;
553 INLINELOG( printf("becomes implemented: "); method_println(mg); );
558 } while (mg != NULL);
565 /* link_class_intern ***********************************************************
567 Tries to link a class. The function calculates the length in bytes
568 that an instance of this class requires as well as the VTBL for
569 methods and interface methods.
571 *******************************************************************************/
573 static classinfo *link_class_intern(classinfo *c)
575 classinfo *super; /* super class */
576 classinfo *tc; /* temporary class variable */
577 s4 supervftbllength; /* vftbllegnth of super class */
578 s4 vftbllength; /* vftbllength of current class */
579 s4 interfacetablelength; /* interface table length */
580 vftbl_t *v; /* vftbl of current class */
581 s4 i; /* interface/method/field counter */
582 arraydescriptor *arraydesc; /* descriptor for array classes */
583 method_worklist *worklist; /* worklist for recompilation */
584 #if defined(ENABLE_RT_TIMING)
585 struct timespec time_start, time_resolving, time_compute_vftbl,
586 time_abstract, time_compute_iftbl, time_fill_vftbl,
587 time_offsets, time_fill_iftbl, time_finalizer,
591 RT_TIMING_GET_TIME(time_start);
595 /* the class must be loaded */
597 /* XXX should this be a specific exception? */
598 assert(c->state & CLASS_LOADED);
600 /* This is check in link_class. */
602 assert(!(c->state & CLASS_LINKED));
604 /* cache the self-reference of this class */
605 /* we do this for cases where the defining loader of the class */
606 /* has not yet been recorded as an initiating loader for the class */
607 /* this is needed so subsequent code can assume that self-refs */
608 /* will always resolve lazily */
609 /* No need to do it for the bootloader - it is always registered */
610 /* as initiating loader for the classes it loads. */
612 classcache_store(c->classloader,c,false);
614 /* this class is currently linking */
616 c->state |= CLASS_LINKING;
621 /* Link the super interfaces. */
623 for (i = 0; i < c->interfacescount; i++) {
624 tc = c->interfaces[i];
626 if (!(tc->state & CLASS_LINKED))
631 /* check super class */
635 /* Check for java/lang/Object. */
637 if (c->super == NULL) {
639 c->instancesize = sizeof(java_object_t);
641 vftbllength = supervftbllength = 0;
646 /* Get super class. */
650 /* Link the super class if necessary. */
652 if (!(super->state & CLASS_LINKED))
653 if (!link_class(super))
656 /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
659 c->flags |= (super->flags &
660 (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
662 /* handle array classes */
664 if (c->name->text[0] == '[')
665 if (!(arraydesc = link_array(c)))
668 if (c->flags & ACC_INTERFACE)
669 c->index = interfaceindex++;
671 c->index = super->index + 1;
673 c->instancesize = super->instancesize;
675 vftbllength = supervftbllength = super->vftbl->vftbllength;
677 c->finalizer = super->finalizer;
679 RT_TIMING_GET_TIME(time_resolving);
682 /* compute vftbl length */
684 for (i = 0; i < c->methodscount; i++) {
685 methodinfo *m = &(c->methods[i]);
687 if (!(m->flags & ACC_STATIC)) { /* is instance method */
693 for (j = 0; j < tc->methodscount; j++) {
694 if (method_canoverwrite(m, &(tc->methods[j]))) {
695 if (tc->methods[j].flags & ACC_PRIVATE)
696 goto notfoundvftblindex;
698 /* package-private methods in other packages */
699 /* must not be overridden */
700 /* (see Java Language Specification 8.4.8.1) */
701 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
702 && !SAME_PACKAGE(c,tc) )
704 goto notfoundvftblindex;
707 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
710 goto foundvftblindex;
718 m->vftblindex = (vftbllength++);
723 RT_TIMING_GET_TIME(time_compute_vftbl);
726 /* Check all interfaces of an abstract class (maybe be an
727 interface too) for unimplemented methods. Such methods are
728 called miranda-methods and are marked with the ACC_MIRANDA
729 flag. VMClass.getDeclaredMethods does not return such
732 if (c->flags & ACC_ABSTRACT) {
735 s4 abstractmethodscount;
739 abstractmethodscount = 0;
741 /* check all interfaces of the abstract class */
743 for (i = 0; i < c->interfacescount; i++) {
744 ic = c->interfaces[i];
746 for (j = 0; j < ic->methodscount; j++) {
747 im = &(ic->methods[j]);
749 /* skip `<clinit>' and `<init>' */
751 if ((im->name == utf_clinit) || (im->name == utf_init))
754 for (tc = c; tc != NULL; tc = tc->super) {
755 for (k = 0; k < tc->methodscount; k++) {
756 if (method_canoverwrite(im, &(tc->methods[k])))
757 goto noabstractmethod;
761 abstractmethodscount++;
768 if (abstractmethodscount > 0) {
771 /* reallocate methods memory */
773 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
774 c->methodscount + abstractmethodscount);
776 for (i = 0; i < c->interfacescount; i++) {
777 ic = c->interfaces[i];
779 for (j = 0; j < ic->methodscount; j++) {
780 im = &(ic->methods[j]);
782 /* skip `<clinit>' and `<init>' */
784 if ((im->name == utf_clinit) || (im->name == utf_init))
787 for (tc = c; tc != NULL; tc = tc->super) {
788 for (k = 0; k < tc->methodscount; k++) {
789 if (method_canoverwrite(im, &(tc->methods[k])))
790 goto noabstractmethod2;
794 /* Copy the method found into the new c->methods
795 array and tag it as miranda-method. */
797 am = &(c->methods[c->methodscount]);
800 MCOPY(am, im, methodinfo, 1);
802 am->vftblindex = (vftbllength++);
804 am->flags |= ACC_MIRANDA;
812 RT_TIMING_GET_TIME(time_abstract);
815 #if defined(ENABLE_STATISTICS)
818 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
821 /* compute interfacetable length */
823 interfacetablelength = 0;
825 for (tc = c; tc != NULL; tc = tc->super) {
826 for (i = 0; i < tc->interfacescount; i++) {
827 s4 h = class_highestinterface(tc->interfaces[i]) + 1;
829 if (h > interfacetablelength)
830 interfacetablelength = h;
833 RT_TIMING_GET_TIME(time_compute_iftbl);
835 /* allocate virtual function table */
837 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
838 sizeof(methodptr) * (vftbllength - 1) +
839 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
840 v = (vftbl_t *) (((methodptr *) v) +
841 (interfacetablelength - 1) * (interfacetablelength > 1));
845 v->vftbllength = vftbllength;
846 v->interfacetablelength = interfacetablelength;
847 v->arraydesc = arraydesc;
849 /* store interface index in vftbl */
851 if (c->flags & ACC_INTERFACE)
852 v->baseval = -(c->index);
854 /* copy virtual function table of super class */
856 for (i = 0; i < supervftbllength; i++)
857 v->table[i] = super->vftbl->table[i];
859 /* Fill the remaining vftbl slots with the AbstractMethodError
860 stub (all after the super class slots, because they are already
863 for (; i < vftbllength; i++) {
864 #if defined(ENABLE_JIT)
865 # if defined(ENABLE_INTRP)
867 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
870 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
872 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
876 /* add method stubs into virtual function table */
878 for (i = 0; i < c->methodscount; i++) {
879 methodinfo *m = &(c->methods[i]);
881 assert(m->stubroutine == NULL);
883 /* Don't create a compiler stub for abstract methods as they
884 throw an AbstractMethodError with the default stub in the
885 vftbl. This entry is simply copied by sub-classes. */
887 if (m->flags & ACC_ABSTRACT)
890 #if defined(ENABLE_JIT)
891 # if defined(ENABLE_INTRP)
893 m->stubroutine = intrp_createcompilerstub(m);
896 m->stubroutine = codegen_generate_stub_compiler(m);
898 m->stubroutine = intrp_createcompilerstub(m);
901 /* static methods are not in the vftbl */
903 if (m->flags & ACC_STATIC)
906 /* insert the stubroutine into the vftbl */
908 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
910 RT_TIMING_GET_TIME(time_fill_vftbl);
912 /* compute instance size and offset of each field */
914 for (i = 0; i < c->fieldscount; i++) {
916 fieldinfo *f = &(c->fields[i]);
918 if (!(f->flags & ACC_STATIC)) {
919 dsize = descriptor_typesize(f->parseddesc);
921 #if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__)) || defined(__M68K__)
922 /* On some architectures and configurations we need to
923 align long (int64_t) and double fields to 4-bytes to
924 match what GCC does for struct members. We must do the
925 same as GCC here because the offsets in native header
926 structs like java_lang_Double must match the offsets of
927 the Java fields (eg. java.lang.Double.value). */
929 # if LINKER_ALIGNMENT_LONG_DOUBLE != 4
930 # error alignment of long and double is not 4
933 c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
936 # if LINKER_ALIGNMENT_LONG_DOUBLE != 8
937 # error alignment of long and double is not 8
940 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
943 f->offset = c->instancesize;
944 c->instancesize += dsize;
947 RT_TIMING_GET_TIME(time_offsets);
949 /* initialize interfacetable and interfacevftbllength */
951 v->interfacevftbllength = MNEW(s4, interfacetablelength);
953 #if defined(ENABLE_STATISTICS)
955 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
958 for (i = 0; i < interfacetablelength; i++) {
959 v->interfacevftbllength[i] = 0;
960 v->interfacetable[-i] = NULL;
965 for (tc = c; tc != NULL; tc = tc->super)
966 for (i = 0; i < tc->interfacescount; i++)
967 if (!linker_addinterface(c, tc->interfaces[i]))
970 RT_TIMING_GET_TIME(time_fill_iftbl);
972 /* add finalizer method (not for java.lang.Object) */
977 fi = class_findmethod(c, utf_finalize, utf_void__void);
980 if (!(fi->flags & ACC_STATIC))
983 RT_TIMING_GET_TIME(time_finalizer);
987 linker_compute_subclasses(c);
989 RT_TIMING_GET_TIME(time_subclasses);
991 /* revert the linking state and class is linked */
993 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
997 /* XXX must this also be done in case of exception? */
999 while (worklist != NULL) {
1000 method_worklist *wi = worklist;
1002 worklist = worklist->next;
1004 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1005 jit_invalidate_code(wi->m);
1007 /* XXX put worklist into dump memory? */
1008 FREE(wi, method_worklist);
1011 RT_TIMING_TIME_DIFF(time_start ,time_resolving ,RT_TIMING_LINK_RESOLVE);
1012 RT_TIMING_TIME_DIFF(time_resolving ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
1013 RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract ,RT_TIMING_LINK_ABSTRACT);
1014 RT_TIMING_TIME_DIFF(time_abstract ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
1015 RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl ,RT_TIMING_LINK_F_VFTBL);
1016 RT_TIMING_TIME_DIFF(time_fill_vftbl ,time_offsets ,RT_TIMING_LINK_OFFSETS);
1017 RT_TIMING_TIME_DIFF(time_offsets ,time_fill_iftbl ,RT_TIMING_LINK_F_IFTBL);
1018 RT_TIMING_TIME_DIFF(time_fill_iftbl ,time_finalizer ,RT_TIMING_LINK_FINALIZER);
1019 RT_TIMING_TIME_DIFF(time_finalizer ,time_subclasses ,RT_TIMING_LINK_SUBCLASS);
1021 /* just return c to show that we didn't had a problem */
1027 /* link_array ******************************************************************
1029 This function is called by link_class to create the arraydescriptor
1032 This function returns NULL if the array cannot be linked because
1033 the component type has not been linked yet.
1035 *******************************************************************************/
1037 static arraydescriptor *link_array(classinfo *c)
1041 arraydescriptor *desc;
1046 namelen = c->name->blength;
1048 /* Check the component type */
1050 switch (c->name->text[1]) {
1052 /* c is an array of arrays. */
1053 u = utf_new(c->name->text + 1, namelen - 1);
1054 if (!(comp = load_class_from_classloader(u, c->classloader)))
1059 /* c is an array of objects. */
1060 u = utf_new(c->name->text + 2, namelen - 3);
1061 if (!(comp = load_class_from_classloader(u, c->classloader)))
1066 /* If the component type has not been linked, link it now */
1068 assert(!comp || (comp->state & CLASS_LOADED));
1070 if (comp && !(comp->state & CLASS_LINKED))
1071 if (!link_class(comp))
1074 /* Allocate the arraydescriptor */
1076 desc = NEW(arraydescriptor);
1079 /* c is an array of references */
1080 desc->arraytype = ARRAYTYPE_OBJECT;
1081 desc->componentsize = sizeof(void*);
1082 desc->dataoffset = OFFSET(java_objectarray_t, data);
1084 compvftbl = comp->vftbl;
1087 log_text("Component class has no vftbl");
1091 desc->componentvftbl = compvftbl;
1093 if (compvftbl->arraydesc) {
1094 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1096 if (compvftbl->arraydesc->dimension >= 255) {
1097 log_text("Creating array of dimension >255");
1101 desc->dimension = compvftbl->arraydesc->dimension + 1;
1102 desc->elementtype = compvftbl->arraydesc->elementtype;
1105 desc->elementvftbl = compvftbl;
1106 desc->dimension = 1;
1107 desc->elementtype = ARRAYTYPE_OBJECT;
1111 /* c is an array of a primitive type */
1112 switch (c->name->text[1]) {
1114 desc->arraytype = ARRAYTYPE_BOOLEAN;
1115 desc->dataoffset = OFFSET(java_booleanarray_t,data);
1116 desc->componentsize = sizeof(u1);
1120 desc->arraytype = ARRAYTYPE_BYTE;
1121 desc->dataoffset = OFFSET(java_bytearray_t,data);
1122 desc->componentsize = sizeof(u1);
1126 desc->arraytype = ARRAYTYPE_CHAR;
1127 desc->dataoffset = OFFSET(java_chararray_t,data);
1128 desc->componentsize = sizeof(u2);
1132 desc->arraytype = ARRAYTYPE_DOUBLE;
1133 desc->dataoffset = OFFSET(java_doublearray_t,data);
1134 desc->componentsize = sizeof(double);
1138 desc->arraytype = ARRAYTYPE_FLOAT;
1139 desc->dataoffset = OFFSET(java_floatarray_t,data);
1140 desc->componentsize = sizeof(float);
1144 desc->arraytype = ARRAYTYPE_INT;
1145 desc->dataoffset = OFFSET(java_intarray_t,data);
1146 desc->componentsize = sizeof(s4);
1150 desc->arraytype = ARRAYTYPE_LONG;
1151 desc->dataoffset = OFFSET(java_longarray_t,data);
1152 desc->componentsize = sizeof(s8);
1156 desc->arraytype = ARRAYTYPE_SHORT;
1157 desc->dataoffset = OFFSET(java_shortarray_t,data);
1158 desc->componentsize = sizeof(s2);
1162 exceptions_throw_noclassdeffounderror(c->name);
1166 desc->componentvftbl = NULL;
1167 desc->elementvftbl = NULL;
1168 desc->dimension = 1;
1169 desc->elementtype = desc->arraytype;
1176 /* linker_compute_subclasses ***************************************************
1180 ATTENTION: DO NOT REMOVE ANY OF THE LOCKING MECHANISMS BELOW:
1181 This function needs to take the class renumber lock and stop the
1182 world during class renumbering. The lock is used in C code which
1183 is not that performance critical. Whereas JIT code uses critical
1184 sections to atomically access the class values.
1186 *******************************************************************************/
1188 static void linker_compute_subclasses(classinfo *c)
1190 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1192 if (!(c->flags & ACC_INTERFACE)) {
1197 if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
1198 c->nextsub = c->super->sub;
1204 /* compute class values */
1206 linker_compute_class_values(class_java_lang_Object);
1208 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1212 /* linker_compute_class_values *************************************************
1216 *******************************************************************************/
1218 static void linker_compute_class_values(classinfo *c)
1222 c->vftbl->baseval = ++classvalue;
1227 linker_compute_class_values(subs);
1229 subs = subs->nextsub;
1232 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1236 /* linker_addinterface *********************************************************
1238 Is needed by link_class for adding a VTBL to a class. All
1239 interfaces implemented by ic are added as well.
1242 true.........everything ok
1243 false........an exception has been thrown
1245 *******************************************************************************/
1247 static bool linker_addinterface(classinfo *c, classinfo *ic)
1258 if (i >= v->interfacetablelength)
1259 vm_abort("Internal error: interfacetable overflow");
1261 /* if this interface has already been added, return immediately */
1263 if (v->interfacetable[-i] != NULL)
1266 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1267 v->interfacevftbllength[i] = 1;
1268 v->interfacetable[-i] = MNEW(methodptr, 1);
1269 v->interfacetable[-i][0] = NULL;
1272 v->interfacevftbllength[i] = ic->methodscount;
1273 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1275 #if defined(ENABLE_STATISTICS)
1277 count_vftbl_len += sizeof(methodptr) *
1278 (ic->methodscount + (ic->methodscount == 0));
1281 for (j = 0; j < ic->methodscount; j++) {
1282 for (sc = c; sc != NULL; sc = sc->super) {
1283 for (k = 0; k < sc->methodscount; k++) {
1284 m = &(sc->methods[k]);
1286 if (method_canoverwrite(m, &(ic->methods[j]))) {
1287 /* method m overwrites the (abstract) method */
1288 #if defined(ENABLE_VERIFIER)
1289 /* Add loading constraints (for the more
1290 general types of the method
1292 if (!classcache_add_constraints_for_params(
1293 c->classloader, ic->classloader,
1300 /* XXX taken from gcj */
1301 /* check for ACC_STATIC: IncompatibleClassChangeError */
1303 /* check for !ACC_PUBLIC: IllegalAccessError */
1305 /* check for ACC_ABSTRACT: AbstracMethodError,
1306 not sure about that one */
1308 v->interfacetable[-i][j] = v->table[m->vftblindex];
1314 /* If no method was found, insert the AbstractMethodError
1317 #if defined(ENABLE_JIT)
1318 # if defined(ENABLE_INTRP)
1320 v->interfacetable[-i][j] =
1321 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1324 v->interfacetable[-i][j] =
1325 (methodptr) (ptrint) &asm_abstractmethoderror;
1327 v->interfacetable[-i][j] =
1328 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1336 /* add superinterfaces of this interface */
1338 for (j = 0; j < ic->interfacescount; j++)
1339 if (!linker_addinterface(c, ic->interfaces[j]))
1348 /* class_highestinterface ******************************************************
1350 Used by the function link_class to determine the amount of memory
1351 needed for the interface table.
1353 *******************************************************************************/
1355 static s4 class_highestinterface(classinfo *c)
1361 /* check for ACC_INTERFACE bit already done in link_class_intern */
1365 for (i = 0; i < c->interfacescount; i++) {
1366 h2 = class_highestinterface(c->interfaces[i]);
1377 * These are local overrides for various environment variables in Emacs.
1378 * Please do not remove this and leave it at the end of the file, where
1379 * Emacs will automagically detect them.
1380 * ---------------------------------------------------------------------
1383 * indent-tabs-mode: t
1387 * vim:noexpandtab:sw=4:ts=4: