1 /* src/vmcore/linker.c - class linker functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: linker.c 7463 2007-03-06 00:02:04Z edwin $
36 #include "mm/memory.h"
37 #include "native/native.h"
39 #if defined(ENABLE_THREADS)
40 # include "threads/native/lock.h"
42 # include "threads/none/lock.h"
45 #include "toolbox/logging.h"
47 #include "vm/access.h"
48 #include "vm/exceptions.h"
49 #include "vm/stringlocal.h"
52 #include "vm/jit/asmpart.h"
54 #include "vmcore/class.h"
55 #include "vmcore/classcache.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
58 #include "vmcore/resolve.h"
59 #include "vmcore/rt-timing.h"
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
65 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
66 #define INLINELOG(code) do { if (opt_inline_debug_log) { code } } while (0)
68 #define INLINELOG(code)
72 /* global variables ***********************************************************/
74 static s4 interfaceindex; /* sequential numbering of interfaces */
78 /* primitivetype_table *********************************************************
80 Structure for primitive classes: contains the class for wrapping
81 the primitive type, the primitive class, the name of the class for
82 wrapping, the one character type signature and the name of the
85 CAUTION: Don't change the order of the types. This table is indexed
86 by the ARRAYTYPE_ constants (except ARRAYTYPE_OBJECT).
88 *******************************************************************************/
90 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
91 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
92 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
93 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
94 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
95 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
96 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
97 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
98 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
99 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
100 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
101 #if defined(ENABLE_JAVASE)
102 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
104 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
109 /* private functions **********************************************************/
111 static bool link_primitivetype_table(void);
112 static classinfo *link_class_intern(classinfo *c);
113 static arraydescriptor *link_array(classinfo *c);
114 static void linker_compute_class_values(classinfo *c);
115 static void linker_compute_subclasses(classinfo *c);
116 static bool linker_addinterface(classinfo *c, classinfo *ic);
117 static s4 class_highestinterface(classinfo *c);
120 /* linker_init *****************************************************************
122 Initializes the linker subsystem.
124 *******************************************************************************/
126 bool linker_init(void)
128 /* reset interface index */
132 /* link java.lang.Class as first class of the system, because we
133 need it's vftbl for all other classes so we can use a class as
136 if (!link_class(class_java_lang_Class))
139 /* now set the header.vftbl of all classes which were created
140 before java.lang.Class was linked */
142 class_postset_header_vftbl();
145 /* link important system classes */
147 if (!link_class(class_java_lang_Object))
150 if (!link_class(class_java_lang_String))
153 #if defined(ENABLE_JAVASE)
154 if (!link_class(class_java_lang_Cloneable))
157 if (!link_class(class_java_io_Serializable))
162 /* link classes for wrapping primitive types */
164 #if defined(ENABLE_JAVASE)
165 if (!link_class(class_java_lang_Void))
169 if (!link_class(class_java_lang_Boolean))
172 if (!link_class(class_java_lang_Byte))
175 if (!link_class(class_java_lang_Character))
178 if (!link_class(class_java_lang_Short))
181 if (!link_class(class_java_lang_Integer))
184 if (!link_class(class_java_lang_Long))
187 if (!link_class(class_java_lang_Float))
190 if (!link_class(class_java_lang_Double))
194 /* load some other important classes */
196 #if defined(ENABLE_JAVASE)
197 if (!link_class(class_java_lang_ClassLoader))
200 if (!link_class(class_java_lang_SecurityManager))
204 if (!link_class(class_java_lang_System))
207 if (!link_class(class_java_lang_Thread))
210 #if defined(ENABLE_JAVASE)
211 if (!link_class(class_java_lang_ThreadGroup))
215 #if defined(WITH_CLASSPATH_GNU)
216 if (!link_class(class_java_lang_VMSystem))
219 if (!link_class(class_java_lang_VMThread))
224 /* some classes which may be used more often */
226 #if defined(ENABLE_JAVASE)
227 if (!link_class(class_java_lang_StackTraceElement))
230 if (!link_class(class_java_lang_reflect_Constructor))
233 if (!link_class(class_java_lang_reflect_Field))
236 if (!link_class(class_java_lang_reflect_Method))
239 if (!link_class(class_java_security_PrivilegedAction))
242 if (!link_class(class_java_util_Vector))
245 if (!link_class(arrayclass_java_lang_Object))
250 /* create pseudo classes used by the typechecker */
252 /* pseudo class for Arraystubs (extends java.lang.Object) */
254 pseudo_class_Arraystub =
255 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
256 pseudo_class_Arraystub->state |= CLASS_LOADED;
257 pseudo_class_Arraystub->super.cls = class_java_lang_Object;
259 #if defined(ENABLE_JAVASE)
260 pseudo_class_Arraystub->interfacescount = 2;
261 pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
262 pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
263 pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
264 #elif defined(ENABLE_JAVAME_CLDC1_1)
265 pseudo_class_Arraystub->interfacescount = 0;
266 pseudo_class_Arraystub->interfaces = NULL;
269 if (!classcache_store_unique(pseudo_class_Arraystub)) {
270 log_text("could not cache pseudo_class_Arraystub");
274 if (!link_class(pseudo_class_Arraystub))
277 /* pseudo class representing the null type */
279 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
280 pseudo_class_Null->state |= CLASS_LOADED;
281 pseudo_class_Null->super.cls = class_java_lang_Object;
283 if (!classcache_store_unique(pseudo_class_Null))
284 vm_abort("linker_init: could not cache pseudo_class_Null");
286 if (!link_class(pseudo_class_Null))
289 /* pseudo class representing new uninitialized objects */
291 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
292 pseudo_class_New->state |= CLASS_LOADED;
293 pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
294 pseudo_class_New->super.cls = class_java_lang_Object;
296 if (!classcache_store_unique(pseudo_class_New))
297 vm_abort("linker_init: could not cache pseudo_class_New");
299 /* create classes representing primitive types */
301 if (!link_primitivetype_table())
305 /* Correct vftbl-entries (retarded loading and linking of class
306 java/lang/String). */
308 stringtable_update();
314 /* link_primitivetype_table ****************************************************
316 Create classes representing primitive types.
318 *******************************************************************************/
320 static bool link_primitivetype_table(void)
326 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
329 if (!primitivetype_table[i].name)
332 /* create primitive class */
334 c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
336 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
338 /* prevent loader from loading primitive class */
340 c->state |= CLASS_LOADED;
342 /* INFO: don't put primitive classes into the classcache */
347 primitivetype_table[i].class_primitive = c;
349 /* create class for wrapping the primitive type */
351 u = utf_new_char(primitivetype_table[i].wrapname);
353 if (!(c = load_class_bootstrap(u)))
356 primitivetype_table[i].class_wrap = c;
358 /* create the primitive array class */
360 if (primitivetype_table[i].arrayname) {
361 u = utf_new_char(primitivetype_table[i].arrayname);
362 c = class_create_classinfo(u);
363 c = load_newly_created_array(c, NULL);
367 primitivetype_table[i].arrayclass = c;
369 assert(c->state & CLASS_LOADED);
371 if (!(c->state & CLASS_LINKED))
375 primitivetype_table[i].arrayvftbl = c->vftbl;
383 /* link_class ******************************************************************
385 Wrapper function for link_class_intern to ease monitor enter/exit
386 and exception handling.
388 *******************************************************************************/
390 classinfo *link_class(classinfo *c)
393 #if defined(ENABLE_RT_TIMING)
394 struct timespec time_start, time_end;
397 RT_TIMING_GET_TIME(time_start);
400 exceptions_throw_nullpointerexception();
404 LOCK_MONITOR_ENTER(c);
406 /* maybe the class is already linked */
408 if (c->state & CLASS_LINKED) {
409 LOCK_MONITOR_EXIT(c);
414 #if defined(ENABLE_STATISTICS)
417 if (opt_getcompilingtime)
418 compilingtime_stop();
420 if (opt_getloadingtime)
424 /* call the internal function */
426 r = link_class_intern(c);
428 /* if return value is NULL, we had a problem and the class is not linked */
431 c->state &= ~CLASS_LINKING;
433 #if defined(ENABLE_STATISTICS)
436 if (opt_getloadingtime)
439 if (opt_getcompilingtime)
440 compilingtime_start();
443 LOCK_MONITOR_EXIT(c);
445 RT_TIMING_GET_TIME(time_end);
447 RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
453 /* linker_overwrite_method *****************************************************
455 Overwrite a method with another one, update method flags and check
459 mg................the general method being overwritten
460 ms................the overwriting (more specialized) method
461 wl................worklist where to add invalidated methods
464 true..............everything ok
465 false.............an exception has been thrown
467 *******************************************************************************/
469 static bool linker_overwrite_method(methodinfo *mg,
471 method_worklist **wl)
479 /* overriding a final method is illegal */
481 if (mg->flags & ACC_FINAL) {
482 exceptions_throw_verifyerror(mg, "Overriding final method");
486 /* method ms overwrites method mg */
488 #if defined(ENABLE_VERIFIER)
489 /* Add loading constraints (for the more general types of method mg). */
490 /* Not for <init>, as it is not invoked virtually. */
492 if ((ms->name != utf_init)
493 && !classcache_add_constraints_for_params(
494 cs->classloader, cg->classloader, mg))
500 /* inherit the vftbl index, and record the overwriting */
502 ms->vftblindex = mg->vftblindex;
505 /* update flags and check assumptions */
506 /* <init> methods are a special case, as they are never dispatched dynamically */
508 if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
510 if (mg->flags & ACC_METHOD_IMPLEMENTED) {
511 /* this adds another implementation */
513 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
515 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
517 method_break_assumption_monomorphic(mg, wl);
520 /* this is the first implementation */
522 mg->flags |= ACC_METHOD_IMPLEMENTED;
524 INLINELOG( printf("becomes implemented: "); method_println(mg); );
529 } while (mg != NULL);
536 /* link_class_intern ***********************************************************
538 Tries to link a class. The function calculates the length in bytes
539 that an instance of this class requires as well as the VTBL for
540 methods and interface methods.
542 *******************************************************************************/
544 static classinfo *link_class_intern(classinfo *c)
546 classinfo *super; /* super class */
547 classinfo *tc; /* temporary class variable */
548 s4 supervftbllength; /* vftbllegnth of super class */
549 s4 vftbllength; /* vftbllength of current class */
550 s4 interfacetablelength; /* interface table length */
551 vftbl_t *v; /* vftbl of current class */
552 s4 i; /* interface/method/field counter */
553 arraydescriptor *arraydesc; /* descriptor for array classes */
554 method_worklist *worklist; /* worklist for recompilation */
555 #if defined(ENABLE_RT_TIMING)
556 struct timespec time_start, time_resolving, time_compute_vftbl,
557 time_abstract, time_compute_iftbl, time_fill_vftbl,
558 time_offsets, time_fill_iftbl, time_finalizer,
562 RT_TIMING_GET_TIME(time_start);
564 /* the class is already linked */
566 if (c->state & CLASS_LINKED)
571 log_message_class("Linking class: ", c);
574 /* the class must be loaded */
576 /* XXX should this be a specific exception? */
577 assert(c->state & CLASS_LOADED);
579 /* cache the self-reference of this class */
580 /* we do this for cases where the defining loader of the class */
581 /* has not yet been recorded as an initiating loader for the class */
582 /* this is needed so subsequent code can assume that self-refs */
583 /* will always resolve lazily */
584 /* No need to do it for the bootloader - it is always registered */
585 /* as initiating loader for the classes it loads. */
587 classcache_store(c->classloader,c,false);
589 /* this class is currently linking */
591 c->state |= CLASS_LINKING;
596 /* check interfaces */
598 for (i = 0; i < c->interfacescount; i++) {
599 /* resolve this super interface */
601 if ((tc = resolve_classref_or_classinfo_eager(c->interfaces[i], true)) == NULL)
604 c->interfaces[i].cls = tc;
606 /* detect circularity */
609 exceptions_throw_classcircularityerror(c);
613 assert(tc->state & CLASS_LOADED);
615 if (!(tc->flags & ACC_INTERFACE)) {
616 exceptions_throw_incompatibleclasschangeerror(tc,
617 "Implementing class");
621 if (!(tc->state & CLASS_LINKED))
626 /* check super class */
630 if (c->super.any == NULL) { /* class java.lang.Object */
632 c->instancesize = sizeof(java_objectheader);
634 vftbllength = supervftbllength = 0;
639 /* resolve super class */
641 if ((super = resolve_classref_or_classinfo_eager(c->super, true)) == NULL)
644 c->super.cls = super;
646 /* detect circularity */
649 exceptions_throw_classcircularityerror(c);
653 assert(super->state & CLASS_LOADED);
655 if (super->flags & ACC_INTERFACE) {
656 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
657 log_text("Interface specified as super class");
661 /* Don't allow extending final classes */
663 if (super->flags & ACC_FINAL) {
664 exceptions_throw_verifyerror(NULL,
665 "Cannot inherit from final class");
669 /* link the superclass if necessary */
671 if (!(super->state & CLASS_LINKED))
672 if (!link_class(super))
675 /* OR the ACC_CLASS_HAS_POINTERS flag */
677 c->flags |= (super->flags & ACC_CLASS_HAS_POINTERS);
679 /* handle array classes */
681 if (c->name->text[0] == '[')
682 if (!(arraydesc = link_array(c)))
685 if (c->flags & ACC_INTERFACE)
686 c->index = interfaceindex++;
688 c->index = super->index + 1;
690 c->instancesize = super->instancesize;
692 vftbllength = supervftbllength = super->vftbl->vftbllength;
694 c->finalizer = super->finalizer;
696 RT_TIMING_GET_TIME(time_resolving);
699 /* compute vftbl length */
701 for (i = 0; i < c->methodscount; i++) {
702 methodinfo *m = &(c->methods[i]);
704 if (!(m->flags & ACC_STATIC)) { /* is instance method */
710 for (j = 0; j < tc->methodscount; j++) {
711 if (method_canoverwrite(m, &(tc->methods[j]))) {
712 if (tc->methods[j].flags & ACC_PRIVATE)
713 goto notfoundvftblindex;
715 /* package-private methods in other packages */
716 /* must not be overridden */
717 /* (see Java Language Specification 8.4.8.1) */
718 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
719 && !SAME_PACKAGE(c,tc) )
721 goto notfoundvftblindex;
724 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
727 goto foundvftblindex;
735 m->vftblindex = (vftbllength++);
740 RT_TIMING_GET_TIME(time_compute_vftbl);
743 /* Check all interfaces of an abstract class (maybe be an
744 interface too) for unimplemented methods. Such methods are
745 called miranda-methods and are marked with the ACC_MIRANDA
746 flag. VMClass.getDeclaredMethods does not return such
749 if (c->flags & ACC_ABSTRACT) {
752 s4 abstractmethodscount;
756 abstractmethodscount = 0;
758 /* check all interfaces of the abstract class */
760 for (i = 0; i < c->interfacescount; i++) {
761 ic = c->interfaces[i].cls;
763 for (j = 0; j < ic->methodscount; j++) {
764 im = &(ic->methods[j]);
766 /* skip `<clinit>' and `<init>' */
768 if ((im->name == utf_clinit) || (im->name == utf_init))
771 for (tc = c; tc != NULL; tc = tc->super.cls) {
772 for (k = 0; k < tc->methodscount; k++) {
773 if (method_canoverwrite(im, &(tc->methods[k])))
774 goto noabstractmethod;
778 abstractmethodscount++;
785 if (abstractmethodscount > 0) {
788 /* reallocate methods memory */
790 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
791 c->methodscount + abstractmethodscount);
793 for (i = 0; i < c->interfacescount; i++) {
794 ic = c->interfaces[i].cls;
796 for (j = 0; j < ic->methodscount; j++) {
797 im = &(ic->methods[j]);
799 /* skip `<clinit>' and `<init>' */
801 if ((im->name == utf_clinit) || (im->name == utf_init))
804 for (tc = c; tc != NULL; tc = tc->super.cls) {
805 for (k = 0; k < tc->methodscount; k++) {
806 if (method_canoverwrite(im, &(tc->methods[k])))
807 goto noabstractmethod2;
811 /* Copy the method found into the new c->methods
812 array and tag it as miranda-method. */
814 am = &(c->methods[c->methodscount]);
817 MCOPY(am, im, methodinfo, 1);
819 am->vftblindex = (vftbllength++);
821 am->flags |= ACC_MIRANDA;
829 RT_TIMING_GET_TIME(time_abstract);
832 #if defined(ENABLE_STATISTICS)
835 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
838 /* compute interfacetable length */
840 interfacetablelength = 0;
842 for (tc = c; tc != NULL; tc = tc->super.cls) {
843 for (i = 0; i < tc->interfacescount; i++) {
844 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
846 if (h > interfacetablelength)
847 interfacetablelength = h;
850 RT_TIMING_GET_TIME(time_compute_iftbl);
852 /* allocate virtual function table */
854 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
855 sizeof(methodptr) * (vftbllength - 1) +
856 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
857 v = (vftbl_t *) (((methodptr *) v) +
858 (interfacetablelength - 1) * (interfacetablelength > 1));
862 v->vftbllength = vftbllength;
863 v->interfacetablelength = interfacetablelength;
864 v->arraydesc = arraydesc;
866 /* store interface index in vftbl */
868 if (c->flags & ACC_INTERFACE)
869 v->baseval = -(c->index);
871 /* copy virtual function table of super class */
873 for (i = 0; i < supervftbllength; i++)
874 v->table[i] = super->vftbl->table[i];
876 /* Fill the remaining vftbl slots with the AbstractMethodError
877 stub (all after the super class slots, because they are already
880 for (; i < vftbllength; i++) {
881 #if defined(ENABLE_JIT)
882 # if defined(ENABLE_INTRP)
884 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
887 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
889 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
893 /* add method stubs into virtual function table */
895 for (i = 0; i < c->methodscount; i++) {
896 methodinfo *m = &(c->methods[i]);
898 assert(m->stubroutine == NULL);
900 /* Don't create a compiler stub for abstract methods as they
901 throw an AbstractMethodError with the default stub in the
902 vftbl. This entry is simply copied by sub-classes. */
904 if (m->flags & ACC_ABSTRACT)
907 #if defined(ENABLE_JIT)
908 # if defined(ENABLE_INTRP)
910 m->stubroutine = intrp_createcompilerstub(m);
913 m->stubroutine = createcompilerstub(m);
915 m->stubroutine = intrp_createcompilerstub(m);
918 /* static methods are not in the vftbl */
920 if (m->flags & ACC_STATIC)
923 /* insert the stubroutine into the vftbl */
925 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
927 RT_TIMING_GET_TIME(time_fill_vftbl);
929 /* compute instance size and offset of each field */
931 for (i = 0; i < c->fieldscount; i++) {
933 fieldinfo *f = &(c->fields[i]);
935 if (!(f->flags & ACC_STATIC)) {
936 dsize = descriptor_typesize(f->parseddesc);
938 #if defined(__I386__) || defined(__ARM__)
939 /* On i386 and ARM we align double and s8 fields to
940 4-bytes. This matches what GCC does for struct
941 members. We must do the same as gcc here because the
942 offsets in native header structs like java_lang_Double
943 must match the offsets of the Java fields
944 (eg. java.lang.Double.value). */
946 c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
948 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
951 f->offset = c->instancesize;
952 c->instancesize += dsize;
955 RT_TIMING_GET_TIME(time_offsets);
957 /* initialize interfacetable and interfacevftbllength */
959 v->interfacevftbllength = MNEW(s4, interfacetablelength);
961 #if defined(ENABLE_STATISTICS)
963 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
966 for (i = 0; i < interfacetablelength; i++) {
967 v->interfacevftbllength[i] = 0;
968 v->interfacetable[-i] = NULL;
973 for (tc = c; tc != NULL; tc = tc->super.cls)
974 for (i = 0; i < tc->interfacescount; i++)
975 if (!linker_addinterface(c, tc->interfaces[i].cls))
978 RT_TIMING_GET_TIME(time_fill_iftbl);
980 /* add finalizer method (not for java.lang.Object) */
985 fi = class_findmethod(c, utf_finalize, utf_void__void);
988 if (!(fi->flags & ACC_STATIC))
991 RT_TIMING_GET_TIME(time_finalizer);
995 linker_compute_subclasses(c);
997 RT_TIMING_GET_TIME(time_subclasses);
999 /* revert the linking state and class is linked */
1001 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1003 /* check worklist */
1005 /* XXX must this also be done in case of exception? */
1007 while (worklist != NULL) {
1008 method_worklist *wi = worklist;
1010 worklist = worklist->next;
1012 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1013 jit_invalidate_code(wi->m);
1015 /* XXX put worklist into dump memory? */
1016 FREE(wi, method_worklist);
1019 #if !defined(NDEBUG)
1021 log_message_class("Linking done class: ", c);
1024 RT_TIMING_TIME_DIFF(time_start ,time_resolving ,RT_TIMING_LINK_RESOLVE);
1025 RT_TIMING_TIME_DIFF(time_resolving ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
1026 RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract ,RT_TIMING_LINK_ABSTRACT);
1027 RT_TIMING_TIME_DIFF(time_abstract ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
1028 RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl ,RT_TIMING_LINK_F_VFTBL);
1029 RT_TIMING_TIME_DIFF(time_fill_vftbl ,time_offsets ,RT_TIMING_LINK_OFFSETS);
1030 RT_TIMING_TIME_DIFF(time_offsets ,time_fill_iftbl ,RT_TIMING_LINK_F_IFTBL);
1031 RT_TIMING_TIME_DIFF(time_fill_iftbl ,time_finalizer ,RT_TIMING_LINK_FINALIZER);
1032 RT_TIMING_TIME_DIFF(time_finalizer ,time_subclasses ,RT_TIMING_LINK_SUBCLASS);
1034 /* just return c to show that we didn't had a problem */
1040 /* link_array ******************************************************************
1042 This function is called by link_class to create the arraydescriptor
1045 This function returns NULL if the array cannot be linked because
1046 the component type has not been linked yet.
1048 *******************************************************************************/
1050 static arraydescriptor *link_array(classinfo *c)
1054 arraydescriptor *desc;
1059 namelen = c->name->blength;
1061 /* Check the component type */
1063 switch (c->name->text[1]) {
1065 /* c is an array of arrays. */
1066 u = utf_new(c->name->text + 1, namelen - 1);
1067 if (!(comp = load_class_from_classloader(u, c->classloader)))
1072 /* c is an array of objects. */
1073 u = utf_new(c->name->text + 2, namelen - 3);
1074 if (!(comp = load_class_from_classloader(u, c->classloader)))
1079 /* If the component type has not been linked, link it now */
1081 assert(!comp || (comp->state & CLASS_LOADED));
1083 if (comp && !(comp->state & CLASS_LINKED))
1084 if (!link_class(comp))
1087 /* Allocate the arraydescriptor */
1089 desc = NEW(arraydescriptor);
1092 /* c is an array of references */
1093 desc->arraytype = ARRAYTYPE_OBJECT;
1094 desc->componentsize = sizeof(void*);
1095 desc->dataoffset = OFFSET(java_objectarray, data);
1097 compvftbl = comp->vftbl;
1100 log_text("Component class has no vftbl");
1104 desc->componentvftbl = compvftbl;
1106 if (compvftbl->arraydesc) {
1107 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1109 if (compvftbl->arraydesc->dimension >= 255) {
1110 log_text("Creating array of dimension >255");
1114 desc->dimension = compvftbl->arraydesc->dimension + 1;
1115 desc->elementtype = compvftbl->arraydesc->elementtype;
1118 desc->elementvftbl = compvftbl;
1119 desc->dimension = 1;
1120 desc->elementtype = ARRAYTYPE_OBJECT;
1124 /* c is an array of a primitive type */
1125 switch (c->name->text[1]) {
1127 desc->arraytype = ARRAYTYPE_BOOLEAN;
1128 desc->dataoffset = OFFSET(java_booleanarray,data);
1129 desc->componentsize = sizeof(u1);
1133 desc->arraytype = ARRAYTYPE_BYTE;
1134 desc->dataoffset = OFFSET(java_bytearray,data);
1135 desc->componentsize = sizeof(u1);
1139 desc->arraytype = ARRAYTYPE_CHAR;
1140 desc->dataoffset = OFFSET(java_chararray,data);
1141 desc->componentsize = sizeof(u2);
1145 desc->arraytype = ARRAYTYPE_DOUBLE;
1146 desc->dataoffset = OFFSET(java_doublearray,data);
1147 desc->componentsize = sizeof(double);
1151 desc->arraytype = ARRAYTYPE_FLOAT;
1152 desc->dataoffset = OFFSET(java_floatarray,data);
1153 desc->componentsize = sizeof(float);
1157 desc->arraytype = ARRAYTYPE_INT;
1158 desc->dataoffset = OFFSET(java_intarray,data);
1159 desc->componentsize = sizeof(s4);
1163 desc->arraytype = ARRAYTYPE_LONG;
1164 desc->dataoffset = OFFSET(java_longarray,data);
1165 desc->componentsize = sizeof(s8);
1169 desc->arraytype = ARRAYTYPE_SHORT;
1170 desc->dataoffset = OFFSET(java_shortarray,data);
1171 desc->componentsize = sizeof(s2);
1175 exceptions_throw_noclassdeffounderror(c->name);
1179 desc->componentvftbl = NULL;
1180 desc->elementvftbl = NULL;
1181 desc->dimension = 1;
1182 desc->elementtype = desc->arraytype;
1189 /* linker_compute_subclasses ***************************************************
1193 *******************************************************************************/
1195 static void linker_compute_subclasses(classinfo *c)
1197 #if defined(ENABLE_THREADS)
1201 if (!(c->flags & ACC_INTERFACE)) {
1206 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
1207 c->nextsub = c->super.cls->sub;
1208 c->super.cls->sub = c;
1213 /* compute class values */
1215 linker_compute_class_values(class_java_lang_Object);
1217 #if defined(ENABLE_THREADS)
1223 /* linker_compute_class_values *************************************************
1227 *******************************************************************************/
1229 static void linker_compute_class_values(classinfo *c)
1233 c->vftbl->baseval = ++classvalue;
1238 linker_compute_class_values(subs);
1240 subs = subs->nextsub;
1243 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1247 /* linker_addinterface *********************************************************
1249 Is needed by link_class for adding a VTBL to a class. All
1250 interfaces implemented by ic are added as well.
1253 true.........everything ok
1254 false........an exception has been thrown
1256 *******************************************************************************/
1258 static bool linker_addinterface(classinfo *c, classinfo *ic)
1269 if (i >= v->interfacetablelength)
1270 vm_abort("Internal error: interfacetable overflow");
1272 /* if this interface has already been added, return immediately */
1274 if (v->interfacetable[-i] != NULL)
1277 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1278 v->interfacevftbllength[i] = 1;
1279 v->interfacetable[-i] = MNEW(methodptr, 1);
1280 v->interfacetable[-i][0] = NULL;
1283 v->interfacevftbllength[i] = ic->methodscount;
1284 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1286 #if defined(ENABLE_STATISTICS)
1288 count_vftbl_len += sizeof(methodptr) *
1289 (ic->methodscount + (ic->methodscount == 0));
1292 for (j = 0; j < ic->methodscount; j++) {
1293 for (sc = c; sc != NULL; sc = sc->super.cls) {
1294 for (k = 0; k < sc->methodscount; k++) {
1295 m = &(sc->methods[k]);
1297 if (method_canoverwrite(m, &(ic->methods[j]))) {
1298 /* method m overwrites the (abstract) method */
1299 #if defined(ENABLE_VERIFIER)
1300 /* Add loading constraints (for the more
1301 general types of the method
1303 if (!classcache_add_constraints_for_params(
1304 c->classloader, ic->classloader,
1311 /* XXX taken from gcj */
1312 /* check for ACC_STATIC: IncompatibleClassChangeError */
1314 /* check for !ACC_PUBLIC: IllegalAccessError */
1316 /* check for ACC_ABSTRACT: AbstracMethodError,
1317 not sure about that one */
1319 v->interfacetable[-i][j] = v->table[m->vftblindex];
1325 /* If no method was found, insert the AbstractMethodError
1328 #if defined(ENABLE_JIT)
1329 # if defined(ENABLE_INTRP)
1331 v->interfacetable[-i][j] =
1332 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1335 v->interfacetable[-i][j] =
1336 (methodptr) (ptrint) &asm_abstractmethoderror;
1338 v->interfacetable[-i][j] =
1339 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1347 /* add superinterfaces of this interface */
1349 for (j = 0; j < ic->interfacescount; j++)
1350 if (!linker_addinterface(c, ic->interfaces[j].cls))
1359 /* class_highestinterface ******************************************************
1361 Used by the function link_class to determine the amount of memory
1362 needed for the interface table.
1364 *******************************************************************************/
1366 static s4 class_highestinterface(classinfo *c)
1372 /* check for ACC_INTERFACE bit already done in link_class_intern */
1376 for (i = 0; i < c->interfacescount; i++) {
1377 h2 = class_highestinterface(c->interfaces[i].cls);
1388 * These are local overrides for various environment variables in Emacs.
1389 * Please do not remove this and leave it at the end of the file, where
1390 * Emacs will automagically detect them.
1391 * ---------------------------------------------------------------------
1394 * indent-tabs-mode: t
1398 * vim:noexpandtab:sw=4:ts=4: