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 7271 2007-02-01 15:27:28Z twisti $
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 extern bool inline_debug_log;
67 #define INLINELOG(code) do { if (inline_debug_log) { code } } while (0)
69 #define INLINELOG(code)
73 /* global variables ***********************************************************/
75 static s4 interfaceindex; /* sequential numbering of interfaces */
79 /* primitivetype_table *********************************************************
81 Structure for primitive classes: contains the class for wrapping
82 the primitive type, the primitive class, the name of the class for
83 wrapping, the one character type signature and the name of the
86 CAUTION: Don't change the order of the types. This table is indexed
87 by the ARRAYTYPE_ constants (except ARRAYTYPE_OBJECT).
89 *******************************************************************************/
91 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
92 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
93 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
94 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
95 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
96 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
97 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
98 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
99 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
100 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
101 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
102 #if defined(ENABLE_JAVASE)
103 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
105 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
110 /* private functions **********************************************************/
112 static bool link_primitivetype_table(void);
113 static classinfo *link_class_intern(classinfo *c);
114 static arraydescriptor *link_array(classinfo *c);
115 static void linker_compute_class_values(classinfo *c);
116 static void linker_compute_subclasses(classinfo *c);
117 static bool linker_addinterface(classinfo *c, classinfo *ic);
118 static s4 class_highestinterface(classinfo *c);
121 /* linker_init *****************************************************************
123 Initializes the linker subsystem.
125 *******************************************************************************/
127 bool linker_init(void)
129 /* reset interface index */
133 /* link java.lang.Class as first class of the system, because we
134 need it's vftbl for all other classes so we can use a class as
137 if (!link_class(class_java_lang_Class))
140 /* now set the header.vftbl of all classes which were created
141 before java.lang.Class was linked */
143 class_postset_header_vftbl();
146 /* link important system classes */
148 if (!link_class(class_java_lang_Object))
151 if (!link_class(class_java_lang_String))
154 #if defined(ENABLE_JAVASE)
155 if (!link_class(class_java_lang_Cloneable))
158 if (!link_class(class_java_io_Serializable))
163 /* link classes for wrapping primitive types */
165 #if defined(ENABLE_JAVASE)
166 if (!link_class(class_java_lang_Void))
170 if (!link_class(class_java_lang_Boolean))
173 if (!link_class(class_java_lang_Byte))
176 if (!link_class(class_java_lang_Character))
179 if (!link_class(class_java_lang_Short))
182 if (!link_class(class_java_lang_Integer))
185 if (!link_class(class_java_lang_Long))
188 if (!link_class(class_java_lang_Float))
191 if (!link_class(class_java_lang_Double))
195 /* load some other important classes */
197 #if defined(ENABLE_JAVASE)
198 if (!link_class(class_java_lang_ClassLoader))
201 if (!link_class(class_java_lang_SecurityManager))
205 if (!link_class(class_java_lang_System))
208 if (!link_class(class_java_lang_Thread))
211 #if defined(ENABLE_JAVASE)
212 if (!link_class(class_java_lang_ThreadGroup))
216 #if defined(WITH_CLASSPATH_GNU)
217 if (!link_class(class_java_lang_VMSystem))
220 if (!link_class(class_java_lang_VMThread))
225 /* some classes which may be used more often */
227 #if defined(ENABLE_JAVASE)
228 if (!link_class(class_java_lang_StackTraceElement))
231 if (!link_class(class_java_lang_reflect_Constructor))
234 if (!link_class(class_java_lang_reflect_Field))
237 if (!link_class(class_java_lang_reflect_Method))
240 if (!link_class(class_java_security_PrivilegedAction))
243 if (!link_class(class_java_util_Vector))
246 if (!link_class(arrayclass_java_lang_Object))
251 /* create pseudo classes used by the typechecker */
253 /* pseudo class for Arraystubs (extends java.lang.Object) */
255 pseudo_class_Arraystub =
256 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
257 pseudo_class_Arraystub->state |= CLASS_LOADED;
258 pseudo_class_Arraystub->super.cls = class_java_lang_Object;
260 #if defined(ENABLE_JAVASE)
261 pseudo_class_Arraystub->interfacescount = 2;
262 pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
263 pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
264 pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
265 #elif defined(ENABLE_JAVAME_CLDC1_1)
266 pseudo_class_Arraystub->interfacescount = 0;
267 pseudo_class_Arraystub->interfaces = NULL;
270 if (!classcache_store_unique(pseudo_class_Arraystub)) {
271 log_text("could not cache pseudo_class_Arraystub");
275 if (!link_class(pseudo_class_Arraystub))
278 /* pseudo class representing the null type */
280 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
281 pseudo_class_Null->state |= CLASS_LOADED;
282 pseudo_class_Null->super.cls = class_java_lang_Object;
284 if (!classcache_store_unique(pseudo_class_Null))
285 vm_abort("linker_init: could not cache pseudo_class_Null");
287 if (!link_class(pseudo_class_Null))
290 /* pseudo class representing new uninitialized objects */
292 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
293 pseudo_class_New->state |= CLASS_LOADED;
294 pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
295 pseudo_class_New->super.cls = class_java_lang_Object;
297 if (!classcache_store_unique(pseudo_class_New))
298 vm_abort("linker_init: could not cache pseudo_class_New");
300 /* create classes representing primitive types */
302 if (!link_primitivetype_table())
306 /* Correct vftbl-entries (retarded loading and linking of class
307 java/lang/String). */
309 stringtable_update();
315 /* link_primitivetype_table ****************************************************
317 Create classes representing primitive types.
319 *******************************************************************************/
321 static bool link_primitivetype_table(void)
327 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
330 if (!primitivetype_table[i].name)
333 /* create primitive class */
335 c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
337 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
339 /* prevent loader from loading primitive class */
341 c->state |= CLASS_LOADED;
343 /* INFO: don't put primitive classes into the classcache */
348 primitivetype_table[i].class_primitive = c;
350 /* create class for wrapping the primitive type */
352 u = utf_new_char(primitivetype_table[i].wrapname);
354 if (!(c = load_class_bootstrap(u)))
357 primitivetype_table[i].class_wrap = c;
359 /* create the primitive array class */
361 if (primitivetype_table[i].arrayname) {
362 u = utf_new_char(primitivetype_table[i].arrayname);
363 c = class_create_classinfo(u);
364 c = load_newly_created_array(c, NULL);
368 primitivetype_table[i].arrayclass = c;
370 assert(c->state & CLASS_LOADED);
372 if (!(c->state & CLASS_LINKED))
376 primitivetype_table[i].arrayvftbl = c->vftbl;
384 /* link_class ******************************************************************
386 Wrapper function for link_class_intern to ease monitor enter/exit
387 and exception handling.
389 *******************************************************************************/
391 classinfo *link_class(classinfo *c)
394 #if defined(ENABLE_RT_TIMING)
395 struct timespec time_start, time_end;
398 RT_TIMING_GET_TIME(time_start);
401 exceptions_throw_nullpointerexception();
405 LOCK_MONITOR_ENTER(c);
407 /* maybe the class is already linked */
409 if (c->state & CLASS_LINKED) {
410 LOCK_MONITOR_EXIT(c);
415 #if defined(ENABLE_STATISTICS)
418 if (opt_getcompilingtime)
419 compilingtime_stop();
421 if (opt_getloadingtime)
425 /* call the internal function */
427 r = link_class_intern(c);
429 /* if return value is NULL, we had a problem and the class is not linked */
432 c->state &= ~CLASS_LINKING;
434 #if defined(ENABLE_STATISTICS)
437 if (opt_getloadingtime)
440 if (opt_getcompilingtime)
441 compilingtime_start();
444 LOCK_MONITOR_EXIT(c);
446 RT_TIMING_GET_TIME(time_end);
448 RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
454 /* linker_overwrite_method *****************************************************
456 Overwrite a method with another one, update method flags and check
460 mg................the general method being overwritten
461 ms................the overwriting (more specialized) method
462 wl................worklist where to add invalidated methods
465 true..............everything ok
466 false.............an exception has been thrown
468 *******************************************************************************/
470 static bool linker_overwrite_method(methodinfo *mg,
472 method_worklist **wl)
480 /* overriding a final method is illegal */
482 if (mg->flags & ACC_FINAL) {
483 exceptions_throw_verifyerror(mg, "Overriding final method");
487 /* method ms overwrites method mg */
489 #if defined(ENABLE_VERIFIER)
490 /* Add loading constraints (for the more general types of method mg). */
491 /* Not for <init>, as it is not invoked virtually. */
493 if ((ms->name != utf_init)
494 && !classcache_add_constraints_for_params(
495 cs->classloader, cg->classloader, mg))
501 /* inherit the vftbl index, and record the overwriting */
503 ms->vftblindex = mg->vftblindex;
506 /* update flags and check assumptions */
507 /* <init> methods are a special case, as they are never dispatched dynamically */
509 if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
511 if (mg->flags & ACC_METHOD_IMPLEMENTED) {
512 /* this adds another implementation */
514 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
516 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
518 method_break_assumption_monomorphic(mg, wl);
521 /* this is the first implementation */
523 mg->flags |= ACC_METHOD_IMPLEMENTED;
525 INLINELOG( printf("becomes implemented: "); method_println(mg); );
530 } while (mg != NULL);
537 /* link_class_intern ***********************************************************
539 Tries to link a class. The function calculates the length in bytes
540 that an instance of this class requires as well as the VTBL for
541 methods and interface methods.
543 *******************************************************************************/
545 static classinfo *link_class_intern(classinfo *c)
547 classinfo *super; /* super class */
548 classinfo *tc; /* temporary class variable */
549 s4 supervftbllength; /* vftbllegnth of super class */
550 s4 vftbllength; /* vftbllength of current class */
551 s4 interfacetablelength; /* interface table length */
552 vftbl_t *v; /* vftbl of current class */
553 s4 i; /* interface/method/field counter */
554 arraydescriptor *arraydesc; /* descriptor for array classes */
555 method_worklist *worklist; /* worklist for recompilation */
556 #if defined(ENABLE_RT_TIMING)
557 struct timespec time_start, time_resolving, time_compute_vftbl,
558 time_abstract, time_compute_iftbl, time_fill_vftbl,
559 time_offsets, time_fill_iftbl, time_finalizer,
563 RT_TIMING_GET_TIME(time_start);
565 /* the class is already linked */
567 if (c->state & CLASS_LINKED)
572 log_message_class("Linking class: ", c);
575 /* the class must be loaded */
577 /* XXX should this be a specific exception? */
578 assert(c->state & CLASS_LOADED);
580 /* cache the self-reference of this class */
581 /* we do this for cases where the defining loader of the class */
582 /* has not yet been recorded as an initiating loader for the class */
583 /* this is needed so subsequent code can assume that self-refs */
584 /* will always resolve lazily */
585 /* No need to do it for the bootloader - it is always registered */
586 /* as initiating loader for the classes it loads. */
588 classcache_store(c->classloader,c,false);
590 /* this class is currently linking */
592 c->state |= CLASS_LINKING;
597 /* check interfaces */
599 for (i = 0; i < c->interfacescount; i++) {
600 /* resolve this super interface */
602 if (!resolve_classref_or_classinfo(NULL, c->interfaces[i], resolveEager,
606 c->interfaces[i].cls = tc;
608 /* detect circularity */
611 exceptions_throw_classcircularityerror(c);
615 assert(tc->state & CLASS_LOADED);
617 if (!(tc->flags & ACC_INTERFACE)) {
618 exceptions_throw_incompatibleclasschangeerror(tc,
619 "Implementing class");
623 if (!(tc->state & CLASS_LINKED))
628 /* check super class */
632 if (c->super.any == NULL) { /* class java.lang.Object */
634 c->instancesize = sizeof(java_objectheader);
636 vftbllength = supervftbllength = 0;
641 /* resolve super class */
643 if (!resolve_classref_or_classinfo(NULL, c->super, resolveEager, true, false,
646 c->super.cls = super;
648 /* detect circularity */
651 exceptions_throw_classcircularityerror(c);
655 assert(super->state & CLASS_LOADED);
657 if (super->flags & ACC_INTERFACE) {
658 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
659 log_text("Interface specified as super class");
663 /* Don't allow extending final classes */
665 if (super->flags & ACC_FINAL) {
666 exceptions_throw_verifyerror(NULL,
667 "Cannot inherit from final class");
671 /* link the superclass if necessary */
673 if (!(super->state & CLASS_LINKED))
674 if (!link_class(super))
677 /* OR the ACC_CLASS_HAS_POINTERS flag */
679 c->flags |= (super->flags & ACC_CLASS_HAS_POINTERS);
681 /* handle array classes */
683 if (c->name->text[0] == '[')
684 if (!(arraydesc = link_array(c)))
687 if (c->flags & ACC_INTERFACE)
688 c->index = interfaceindex++;
690 c->index = super->index + 1;
692 c->instancesize = super->instancesize;
694 vftbllength = supervftbllength = super->vftbl->vftbllength;
696 c->finalizer = super->finalizer;
698 RT_TIMING_GET_TIME(time_resolving);
701 /* compute vftbl length */
703 for (i = 0; i < c->methodscount; i++) {
704 methodinfo *m = &(c->methods[i]);
706 if (!(m->flags & ACC_STATIC)) { /* is instance method */
712 for (j = 0; j < tc->methodscount; j++) {
713 if (method_canoverwrite(m, &(tc->methods[j]))) {
714 if (tc->methods[j].flags & ACC_PRIVATE)
715 goto notfoundvftblindex;
717 /* package-private methods in other packages */
718 /* must not be overridden */
719 /* (see Java Language Specification 8.4.8.1) */
720 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
721 && !SAME_PACKAGE(c,tc) )
723 goto notfoundvftblindex;
726 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
729 goto foundvftblindex;
737 m->vftblindex = (vftbllength++);
742 RT_TIMING_GET_TIME(time_compute_vftbl);
745 /* Check all interfaces of an abstract class (maybe be an
746 interface too) for unimplemented methods. Such methods are
747 called miranda-methods and are marked with the ACC_MIRANDA
748 flag. VMClass.getDeclaredMethods does not return such
751 if (c->flags & ACC_ABSTRACT) {
754 s4 abstractmethodscount;
758 abstractmethodscount = 0;
760 /* check all interfaces of the abstract class */
762 for (i = 0; i < c->interfacescount; i++) {
763 ic = c->interfaces[i].cls;
765 for (j = 0; j < ic->methodscount; j++) {
766 im = &(ic->methods[j]);
768 /* skip `<clinit>' and `<init>' */
770 if ((im->name == utf_clinit) || (im->name == utf_init))
773 for (tc = c; tc != NULL; tc = tc->super.cls) {
774 for (k = 0; k < tc->methodscount; k++) {
775 if (method_canoverwrite(im, &(tc->methods[k])))
776 goto noabstractmethod;
780 abstractmethodscount++;
787 if (abstractmethodscount > 0) {
790 /* reallocate methods memory */
792 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
793 c->methodscount + abstractmethodscount);
795 for (i = 0; i < c->interfacescount; i++) {
796 ic = c->interfaces[i].cls;
798 for (j = 0; j < ic->methodscount; j++) {
799 im = &(ic->methods[j]);
801 /* skip `<clinit>' and `<init>' */
803 if ((im->name == utf_clinit) || (im->name == utf_init))
806 for (tc = c; tc != NULL; tc = tc->super.cls) {
807 for (k = 0; k < tc->methodscount; k++) {
808 if (method_canoverwrite(im, &(tc->methods[k])))
809 goto noabstractmethod2;
813 /* Copy the method found into the new c->methods
814 array and tag it as miranda-method. */
816 am = &(c->methods[c->methodscount]);
819 MCOPY(am, im, methodinfo, 1);
821 am->vftblindex = (vftbllength++);
823 am->flags |= ACC_MIRANDA;
831 RT_TIMING_GET_TIME(time_abstract);
834 #if defined(ENABLE_STATISTICS)
837 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
840 /* compute interfacetable length */
842 interfacetablelength = 0;
844 for (tc = c; tc != NULL; tc = tc->super.cls) {
845 for (i = 0; i < tc->interfacescount; i++) {
846 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
848 if (h > interfacetablelength)
849 interfacetablelength = h;
852 RT_TIMING_GET_TIME(time_compute_iftbl);
854 /* allocate virtual function table */
856 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
857 sizeof(methodptr) * (vftbllength - 1) +
858 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
859 v = (vftbl_t *) (((methodptr *) v) +
860 (interfacetablelength - 1) * (interfacetablelength > 1));
864 v->vftbllength = vftbllength;
865 v->interfacetablelength = interfacetablelength;
866 v->arraydesc = arraydesc;
868 /* store interface index in vftbl */
870 if (c->flags & ACC_INTERFACE)
871 v->baseval = -(c->index);
873 /* copy virtual function table of super class */
875 for (i = 0; i < supervftbllength; i++)
876 v->table[i] = super->vftbl->table[i];
878 /* Fill the remaining vftbl slots with the AbstractMethodError
879 stub (all after the super class slots, because they are already
882 for (; i < vftbllength; i++) {
883 #if defined(ENABLE_JIT)
884 # if defined(ENABLE_INTRP)
886 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
889 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
891 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
895 /* add method stubs into virtual function table */
897 for (i = 0; i < c->methodscount; i++) {
898 methodinfo *m = &(c->methods[i]);
900 assert(m->stubroutine == NULL);
902 /* Don't create a compiler stub for abstract methods as they
903 throw an AbstractMethodError with the default stub in the
904 vftbl. This entry is simply copied by sub-classes. */
906 if (m->flags & ACC_ABSTRACT)
909 #if defined(ENABLE_JIT)
910 # if defined(ENABLE_INTRP)
912 m->stubroutine = intrp_createcompilerstub(m);
915 m->stubroutine = createcompilerstub(m);
917 m->stubroutine = intrp_createcompilerstub(m);
920 /* static methods are not in the vftbl */
922 if (m->flags & ACC_STATIC)
925 /* insert the stubroutine into the vftbl */
927 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
929 RT_TIMING_GET_TIME(time_fill_vftbl);
931 /* compute instance size and offset of each field */
933 for (i = 0; i < c->fieldscount; i++) {
935 fieldinfo *f = &(c->fields[i]);
937 if (!(f->flags & ACC_STATIC)) {
938 dsize = descriptor_typesize(f->parseddesc);
940 #if defined(__I386__) || defined(__ARM__)
941 /* On i386 and ARM we align double and s8 fields to
942 4-bytes. This matches what GCC does for struct
943 members. We must do the same as gcc here because the
944 offsets in native header structs like java_lang_Double
945 must match the offsets of the Java fields
946 (eg. java.lang.Double.value). */
948 c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
950 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
953 f->offset = c->instancesize;
954 c->instancesize += dsize;
957 RT_TIMING_GET_TIME(time_offsets);
959 /* initialize interfacetable and interfacevftbllength */
961 v->interfacevftbllength = MNEW(s4, interfacetablelength);
963 #if defined(ENABLE_STATISTICS)
965 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
968 for (i = 0; i < interfacetablelength; i++) {
969 v->interfacevftbllength[i] = 0;
970 v->interfacetable[-i] = NULL;
975 for (tc = c; tc != NULL; tc = tc->super.cls)
976 for (i = 0; i < tc->interfacescount; i++)
977 if (!linker_addinterface(c, tc->interfaces[i].cls))
980 RT_TIMING_GET_TIME(time_fill_iftbl);
982 /* add finalizer method (not for java.lang.Object) */
987 fi = class_findmethod(c, utf_finalize, utf_void__void);
990 if (!(fi->flags & ACC_STATIC))
993 RT_TIMING_GET_TIME(time_finalizer);
997 linker_compute_subclasses(c);
999 RT_TIMING_GET_TIME(time_subclasses);
1001 /* revert the linking state and class is linked */
1003 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1005 /* check worklist */
1007 /* XXX must this also be done in case of exception? */
1009 while (worklist != NULL) {
1010 method_worklist *wi = worklist;
1012 worklist = worklist->next;
1014 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1015 jit_invalidate_code(wi->m);
1017 /* XXX put worklist into dump memory? */
1018 FREE(wi, method_worklist);
1021 #if !defined(NDEBUG)
1023 log_message_class("Linking done class: ", c);
1026 RT_TIMING_TIME_DIFF(time_start ,time_resolving ,RT_TIMING_LINK_RESOLVE);
1027 RT_TIMING_TIME_DIFF(time_resolving ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
1028 RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract ,RT_TIMING_LINK_ABSTRACT);
1029 RT_TIMING_TIME_DIFF(time_abstract ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
1030 RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl ,RT_TIMING_LINK_F_VFTBL);
1031 RT_TIMING_TIME_DIFF(time_fill_vftbl ,time_offsets ,RT_TIMING_LINK_OFFSETS);
1032 RT_TIMING_TIME_DIFF(time_offsets ,time_fill_iftbl ,RT_TIMING_LINK_F_IFTBL);
1033 RT_TIMING_TIME_DIFF(time_fill_iftbl ,time_finalizer ,RT_TIMING_LINK_FINALIZER);
1034 RT_TIMING_TIME_DIFF(time_finalizer ,time_subclasses ,RT_TIMING_LINK_SUBCLASS);
1036 /* just return c to show that we didn't had a problem */
1042 /* link_array ******************************************************************
1044 This function is called by link_class to create the arraydescriptor
1047 This function returns NULL if the array cannot be linked because
1048 the component type has not been linked yet.
1050 *******************************************************************************/
1052 static arraydescriptor *link_array(classinfo *c)
1056 arraydescriptor *desc;
1061 namelen = c->name->blength;
1063 /* Check the component type */
1065 switch (c->name->text[1]) {
1067 /* c is an array of arrays. */
1068 u = utf_new(c->name->text + 1, namelen - 1);
1069 if (!(comp = load_class_from_classloader(u, c->classloader)))
1074 /* c is an array of objects. */
1075 u = utf_new(c->name->text + 2, namelen - 3);
1076 if (!(comp = load_class_from_classloader(u, c->classloader)))
1081 /* If the component type has not been linked, link it now */
1083 assert(!comp || (comp->state & CLASS_LOADED));
1085 if (comp && !(comp->state & CLASS_LINKED))
1086 if (!link_class(comp))
1089 /* Allocate the arraydescriptor */
1091 desc = NEW(arraydescriptor);
1094 /* c is an array of references */
1095 desc->arraytype = ARRAYTYPE_OBJECT;
1096 desc->componentsize = sizeof(void*);
1097 desc->dataoffset = OFFSET(java_objectarray, data);
1099 compvftbl = comp->vftbl;
1102 log_text("Component class has no vftbl");
1106 desc->componentvftbl = compvftbl;
1108 if (compvftbl->arraydesc) {
1109 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1111 if (compvftbl->arraydesc->dimension >= 255) {
1112 log_text("Creating array of dimension >255");
1116 desc->dimension = compvftbl->arraydesc->dimension + 1;
1117 desc->elementtype = compvftbl->arraydesc->elementtype;
1120 desc->elementvftbl = compvftbl;
1121 desc->dimension = 1;
1122 desc->elementtype = ARRAYTYPE_OBJECT;
1126 /* c is an array of a primitive type */
1127 switch (c->name->text[1]) {
1129 desc->arraytype = ARRAYTYPE_BOOLEAN;
1130 desc->dataoffset = OFFSET(java_booleanarray,data);
1131 desc->componentsize = sizeof(u1);
1135 desc->arraytype = ARRAYTYPE_BYTE;
1136 desc->dataoffset = OFFSET(java_bytearray,data);
1137 desc->componentsize = sizeof(u1);
1141 desc->arraytype = ARRAYTYPE_CHAR;
1142 desc->dataoffset = OFFSET(java_chararray,data);
1143 desc->componentsize = sizeof(u2);
1147 desc->arraytype = ARRAYTYPE_DOUBLE;
1148 desc->dataoffset = OFFSET(java_doublearray,data);
1149 desc->componentsize = sizeof(double);
1153 desc->arraytype = ARRAYTYPE_FLOAT;
1154 desc->dataoffset = OFFSET(java_floatarray,data);
1155 desc->componentsize = sizeof(float);
1159 desc->arraytype = ARRAYTYPE_INT;
1160 desc->dataoffset = OFFSET(java_intarray,data);
1161 desc->componentsize = sizeof(s4);
1165 desc->arraytype = ARRAYTYPE_LONG;
1166 desc->dataoffset = OFFSET(java_longarray,data);
1167 desc->componentsize = sizeof(s8);
1171 desc->arraytype = ARRAYTYPE_SHORT;
1172 desc->dataoffset = OFFSET(java_shortarray,data);
1173 desc->componentsize = sizeof(s2);
1177 exceptions_throw_noclassdeffounderror(c->name);
1181 desc->componentvftbl = NULL;
1182 desc->elementvftbl = NULL;
1183 desc->dimension = 1;
1184 desc->elementtype = desc->arraytype;
1191 /* linker_compute_subclasses ***************************************************
1195 *******************************************************************************/
1197 static void linker_compute_subclasses(classinfo *c)
1199 #if defined(ENABLE_THREADS)
1203 if (!(c->flags & ACC_INTERFACE)) {
1208 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
1209 c->nextsub = c->super.cls->sub;
1210 c->super.cls->sub = c;
1215 /* compute class values */
1217 linker_compute_class_values(class_java_lang_Object);
1219 #if defined(ENABLE_THREADS)
1225 /* linker_compute_class_values *************************************************
1229 *******************************************************************************/
1231 static void linker_compute_class_values(classinfo *c)
1235 c->vftbl->baseval = ++classvalue;
1240 linker_compute_class_values(subs);
1242 subs = subs->nextsub;
1245 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1249 /* linker_addinterface *********************************************************
1251 Is needed by link_class for adding a VTBL to a class. All
1252 interfaces implemented by ic are added as well.
1255 true.........everything ok
1256 false........an exception has been thrown
1258 *******************************************************************************/
1260 static bool linker_addinterface(classinfo *c, classinfo *ic)
1271 if (i >= v->interfacetablelength)
1272 vm_abort("Internal error: interfacetable overflow");
1274 /* if this interface has already been added, return immediately */
1276 if (v->interfacetable[-i] != NULL)
1279 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1280 v->interfacevftbllength[i] = 1;
1281 v->interfacetable[-i] = MNEW(methodptr, 1);
1282 v->interfacetable[-i][0] = NULL;
1285 v->interfacevftbllength[i] = ic->methodscount;
1286 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1288 #if defined(ENABLE_STATISTICS)
1290 count_vftbl_len += sizeof(methodptr) *
1291 (ic->methodscount + (ic->methodscount == 0));
1294 for (j = 0; j < ic->methodscount; j++) {
1295 for (sc = c; sc != NULL; sc = sc->super.cls) {
1296 for (k = 0; k < sc->methodscount; k++) {
1297 m = &(sc->methods[k]);
1299 if (method_canoverwrite(m, &(ic->methods[j]))) {
1300 /* method m overwrites the (abstract) method */
1301 #if defined(ENABLE_VERIFIER)
1302 /* Add loading constraints (for the more
1303 general types of the method
1305 if (!classcache_add_constraints_for_params(
1306 c->classloader, ic->classloader,
1313 /* XXX taken from gcj */
1314 /* check for ACC_STATIC: IncompatibleClassChangeError */
1316 /* check for !ACC_PUBLIC: IllegalAccessError */
1318 /* check for ACC_ABSTRACT: AbstracMethodError,
1319 not sure about that one */
1321 v->interfacetable[-i][j] = v->table[m->vftblindex];
1327 /* If no method was found, insert the AbstractMethodError
1330 #if defined(ENABLE_JIT)
1331 # if defined(ENABLE_INTRP)
1333 v->interfacetable[-i][j] =
1334 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1337 v->interfacetable[-i][j] =
1338 (methodptr) (ptrint) &asm_abstractmethoderror;
1340 v->interfacetable[-i][j] =
1341 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1349 /* add superinterfaces of this interface */
1351 for (j = 0; j < ic->interfacescount; j++)
1352 if (!linker_addinterface(c, ic->interfaces[j].cls))
1361 /* class_highestinterface ******************************************************
1363 Used by the function link_class to determine the amount of memory
1364 needed for the interface table.
1366 *******************************************************************************/
1368 static s4 class_highestinterface(classinfo *c)
1374 /* check for ACC_INTERFACE bit already done in link_class_intern */
1378 for (i = 0; i < c->interfacescount; i++) {
1379 h2 = class_highestinterface(c->interfaces[i].cls);
1390 * These are local overrides for various environment variables in Emacs.
1391 * Please do not remove this and leave it at the end of the file, where
1392 * Emacs will automagically detect them.
1393 * ---------------------------------------------------------------------
1396 * indent-tabs-mode: t
1400 * vim:noexpandtab:sw=4:ts=4: