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 7450 2007-03-04 19:13:29Z 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 (!resolve_classref_or_classinfo(NULL, c->interfaces[i], resolveEager,
605 c->interfaces[i].cls = tc;
607 /* detect circularity */
610 exceptions_throw_classcircularityerror(c);
614 assert(tc->state & CLASS_LOADED);
616 if (!(tc->flags & ACC_INTERFACE)) {
617 exceptions_throw_incompatibleclasschangeerror(tc,
618 "Implementing class");
622 if (!(tc->state & CLASS_LINKED))
627 /* check super class */
631 if (c->super.any == NULL) { /* class java.lang.Object */
633 c->instancesize = sizeof(java_objectheader);
635 vftbllength = supervftbllength = 0;
640 /* resolve super class */
642 if (!resolve_classref_or_classinfo(NULL, c->super, resolveEager, true, false,
645 c->super.cls = super;
647 /* detect circularity */
650 exceptions_throw_classcircularityerror(c);
654 assert(super->state & CLASS_LOADED);
656 if (super->flags & ACC_INTERFACE) {
657 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
658 log_text("Interface specified as super class");
662 /* Don't allow extending final classes */
664 if (super->flags & ACC_FINAL) {
665 exceptions_throw_verifyerror(NULL,
666 "Cannot inherit from final class");
670 /* link the superclass if necessary */
672 if (!(super->state & CLASS_LINKED))
673 if (!link_class(super))
676 /* OR the ACC_CLASS_HAS_POINTERS flag */
678 c->flags |= (super->flags & ACC_CLASS_HAS_POINTERS);
680 /* handle array classes */
682 if (c->name->text[0] == '[')
683 if (!(arraydesc = link_array(c)))
686 if (c->flags & ACC_INTERFACE)
687 c->index = interfaceindex++;
689 c->index = super->index + 1;
691 c->instancesize = super->instancesize;
693 vftbllength = supervftbllength = super->vftbl->vftbllength;
695 c->finalizer = super->finalizer;
697 RT_TIMING_GET_TIME(time_resolving);
700 /* compute vftbl length */
702 for (i = 0; i < c->methodscount; i++) {
703 methodinfo *m = &(c->methods[i]);
705 if (!(m->flags & ACC_STATIC)) { /* is instance method */
711 for (j = 0; j < tc->methodscount; j++) {
712 if (method_canoverwrite(m, &(tc->methods[j]))) {
713 if (tc->methods[j].flags & ACC_PRIVATE)
714 goto notfoundvftblindex;
716 /* package-private methods in other packages */
717 /* must not be overridden */
718 /* (see Java Language Specification 8.4.8.1) */
719 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
720 && !SAME_PACKAGE(c,tc) )
722 goto notfoundvftblindex;
725 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
728 goto foundvftblindex;
736 m->vftblindex = (vftbllength++);
741 RT_TIMING_GET_TIME(time_compute_vftbl);
744 /* Check all interfaces of an abstract class (maybe be an
745 interface too) for unimplemented methods. Such methods are
746 called miranda-methods and are marked with the ACC_MIRANDA
747 flag. VMClass.getDeclaredMethods does not return such
750 if (c->flags & ACC_ABSTRACT) {
753 s4 abstractmethodscount;
757 abstractmethodscount = 0;
759 /* check all interfaces of the abstract class */
761 for (i = 0; i < c->interfacescount; i++) {
762 ic = c->interfaces[i].cls;
764 for (j = 0; j < ic->methodscount; j++) {
765 im = &(ic->methods[j]);
767 /* skip `<clinit>' and `<init>' */
769 if ((im->name == utf_clinit) || (im->name == utf_init))
772 for (tc = c; tc != NULL; tc = tc->super.cls) {
773 for (k = 0; k < tc->methodscount; k++) {
774 if (method_canoverwrite(im, &(tc->methods[k])))
775 goto noabstractmethod;
779 abstractmethodscount++;
786 if (abstractmethodscount > 0) {
789 /* reallocate methods memory */
791 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
792 c->methodscount + abstractmethodscount);
794 for (i = 0; i < c->interfacescount; i++) {
795 ic = c->interfaces[i].cls;
797 for (j = 0; j < ic->methodscount; j++) {
798 im = &(ic->methods[j]);
800 /* skip `<clinit>' and `<init>' */
802 if ((im->name == utf_clinit) || (im->name == utf_init))
805 for (tc = c; tc != NULL; tc = tc->super.cls) {
806 for (k = 0; k < tc->methodscount; k++) {
807 if (method_canoverwrite(im, &(tc->methods[k])))
808 goto noabstractmethod2;
812 /* Copy the method found into the new c->methods
813 array and tag it as miranda-method. */
815 am = &(c->methods[c->methodscount]);
818 MCOPY(am, im, methodinfo, 1);
820 am->vftblindex = (vftbllength++);
822 am->flags |= ACC_MIRANDA;
830 RT_TIMING_GET_TIME(time_abstract);
833 #if defined(ENABLE_STATISTICS)
836 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
839 /* compute interfacetable length */
841 interfacetablelength = 0;
843 for (tc = c; tc != NULL; tc = tc->super.cls) {
844 for (i = 0; i < tc->interfacescount; i++) {
845 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
847 if (h > interfacetablelength)
848 interfacetablelength = h;
851 RT_TIMING_GET_TIME(time_compute_iftbl);
853 /* allocate virtual function table */
855 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
856 sizeof(methodptr) * (vftbllength - 1) +
857 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
858 v = (vftbl_t *) (((methodptr *) v) +
859 (interfacetablelength - 1) * (interfacetablelength > 1));
863 v->vftbllength = vftbllength;
864 v->interfacetablelength = interfacetablelength;
865 v->arraydesc = arraydesc;
867 /* store interface index in vftbl */
869 if (c->flags & ACC_INTERFACE)
870 v->baseval = -(c->index);
872 /* copy virtual function table of super class */
874 for (i = 0; i < supervftbllength; i++)
875 v->table[i] = super->vftbl->table[i];
877 /* Fill the remaining vftbl slots with the AbstractMethodError
878 stub (all after the super class slots, because they are already
881 for (; i < vftbllength; i++) {
882 #if defined(ENABLE_JIT)
883 # if defined(ENABLE_INTRP)
885 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
888 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
890 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
894 /* add method stubs into virtual function table */
896 for (i = 0; i < c->methodscount; i++) {
897 methodinfo *m = &(c->methods[i]);
899 assert(m->stubroutine == NULL);
901 /* Don't create a compiler stub for abstract methods as they
902 throw an AbstractMethodError with the default stub in the
903 vftbl. This entry is simply copied by sub-classes. */
905 if (m->flags & ACC_ABSTRACT)
908 #if defined(ENABLE_JIT)
909 # if defined(ENABLE_INTRP)
911 m->stubroutine = intrp_createcompilerstub(m);
914 m->stubroutine = createcompilerstub(m);
916 m->stubroutine = intrp_createcompilerstub(m);
919 /* static methods are not in the vftbl */
921 if (m->flags & ACC_STATIC)
924 /* insert the stubroutine into the vftbl */
926 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
928 RT_TIMING_GET_TIME(time_fill_vftbl);
930 /* compute instance size and offset of each field */
932 for (i = 0; i < c->fieldscount; i++) {
934 fieldinfo *f = &(c->fields[i]);
936 if (!(f->flags & ACC_STATIC)) {
937 dsize = descriptor_typesize(f->parseddesc);
939 #if defined(__I386__) || defined(__ARM__)
940 /* On i386 and ARM we align double and s8 fields to
941 4-bytes. This matches what GCC does for struct
942 members. We must do the same as gcc here because the
943 offsets in native header structs like java_lang_Double
944 must match the offsets of the Java fields
945 (eg. java.lang.Double.value). */
947 c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
949 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
952 f->offset = c->instancesize;
953 c->instancesize += dsize;
956 RT_TIMING_GET_TIME(time_offsets);
958 /* initialize interfacetable and interfacevftbllength */
960 v->interfacevftbllength = MNEW(s4, interfacetablelength);
962 #if defined(ENABLE_STATISTICS)
964 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
967 for (i = 0; i < interfacetablelength; i++) {
968 v->interfacevftbllength[i] = 0;
969 v->interfacetable[-i] = NULL;
974 for (tc = c; tc != NULL; tc = tc->super.cls)
975 for (i = 0; i < tc->interfacescount; i++)
976 if (!linker_addinterface(c, tc->interfaces[i].cls))
979 RT_TIMING_GET_TIME(time_fill_iftbl);
981 /* add finalizer method (not for java.lang.Object) */
986 fi = class_findmethod(c, utf_finalize, utf_void__void);
989 if (!(fi->flags & ACC_STATIC))
992 RT_TIMING_GET_TIME(time_finalizer);
996 linker_compute_subclasses(c);
998 RT_TIMING_GET_TIME(time_subclasses);
1000 /* revert the linking state and class is linked */
1002 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1004 /* check worklist */
1006 /* XXX must this also be done in case of exception? */
1008 while (worklist != NULL) {
1009 method_worklist *wi = worklist;
1011 worklist = worklist->next;
1013 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1014 jit_invalidate_code(wi->m);
1016 /* XXX put worklist into dump memory? */
1017 FREE(wi, method_worklist);
1020 #if !defined(NDEBUG)
1022 log_message_class("Linking done class: ", c);
1025 RT_TIMING_TIME_DIFF(time_start ,time_resolving ,RT_TIMING_LINK_RESOLVE);
1026 RT_TIMING_TIME_DIFF(time_resolving ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
1027 RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract ,RT_TIMING_LINK_ABSTRACT);
1028 RT_TIMING_TIME_DIFF(time_abstract ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
1029 RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl ,RT_TIMING_LINK_F_VFTBL);
1030 RT_TIMING_TIME_DIFF(time_fill_vftbl ,time_offsets ,RT_TIMING_LINK_OFFSETS);
1031 RT_TIMING_TIME_DIFF(time_offsets ,time_fill_iftbl ,RT_TIMING_LINK_F_IFTBL);
1032 RT_TIMING_TIME_DIFF(time_fill_iftbl ,time_finalizer ,RT_TIMING_LINK_FINALIZER);
1033 RT_TIMING_TIME_DIFF(time_finalizer ,time_subclasses ,RT_TIMING_LINK_SUBCLASS);
1035 /* just return c to show that we didn't had a problem */
1041 /* link_array ******************************************************************
1043 This function is called by link_class to create the arraydescriptor
1046 This function returns NULL if the array cannot be linked because
1047 the component type has not been linked yet.
1049 *******************************************************************************/
1051 static arraydescriptor *link_array(classinfo *c)
1055 arraydescriptor *desc;
1060 namelen = c->name->blength;
1062 /* Check the component type */
1064 switch (c->name->text[1]) {
1066 /* c is an array of arrays. */
1067 u = utf_new(c->name->text + 1, namelen - 1);
1068 if (!(comp = load_class_from_classloader(u, c->classloader)))
1073 /* c is an array of objects. */
1074 u = utf_new(c->name->text + 2, namelen - 3);
1075 if (!(comp = load_class_from_classloader(u, c->classloader)))
1080 /* If the component type has not been linked, link it now */
1082 assert(!comp || (comp->state & CLASS_LOADED));
1084 if (comp && !(comp->state & CLASS_LINKED))
1085 if (!link_class(comp))
1088 /* Allocate the arraydescriptor */
1090 desc = NEW(arraydescriptor);
1093 /* c is an array of references */
1094 desc->arraytype = ARRAYTYPE_OBJECT;
1095 desc->componentsize = sizeof(void*);
1096 desc->dataoffset = OFFSET(java_objectarray, data);
1098 compvftbl = comp->vftbl;
1101 log_text("Component class has no vftbl");
1105 desc->componentvftbl = compvftbl;
1107 if (compvftbl->arraydesc) {
1108 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1110 if (compvftbl->arraydesc->dimension >= 255) {
1111 log_text("Creating array of dimension >255");
1115 desc->dimension = compvftbl->arraydesc->dimension + 1;
1116 desc->elementtype = compvftbl->arraydesc->elementtype;
1119 desc->elementvftbl = compvftbl;
1120 desc->dimension = 1;
1121 desc->elementtype = ARRAYTYPE_OBJECT;
1125 /* c is an array of a primitive type */
1126 switch (c->name->text[1]) {
1128 desc->arraytype = ARRAYTYPE_BOOLEAN;
1129 desc->dataoffset = OFFSET(java_booleanarray,data);
1130 desc->componentsize = sizeof(u1);
1134 desc->arraytype = ARRAYTYPE_BYTE;
1135 desc->dataoffset = OFFSET(java_bytearray,data);
1136 desc->componentsize = sizeof(u1);
1140 desc->arraytype = ARRAYTYPE_CHAR;
1141 desc->dataoffset = OFFSET(java_chararray,data);
1142 desc->componentsize = sizeof(u2);
1146 desc->arraytype = ARRAYTYPE_DOUBLE;
1147 desc->dataoffset = OFFSET(java_doublearray,data);
1148 desc->componentsize = sizeof(double);
1152 desc->arraytype = ARRAYTYPE_FLOAT;
1153 desc->dataoffset = OFFSET(java_floatarray,data);
1154 desc->componentsize = sizeof(float);
1158 desc->arraytype = ARRAYTYPE_INT;
1159 desc->dataoffset = OFFSET(java_intarray,data);
1160 desc->componentsize = sizeof(s4);
1164 desc->arraytype = ARRAYTYPE_LONG;
1165 desc->dataoffset = OFFSET(java_longarray,data);
1166 desc->componentsize = sizeof(s8);
1170 desc->arraytype = ARRAYTYPE_SHORT;
1171 desc->dataoffset = OFFSET(java_shortarray,data);
1172 desc->componentsize = sizeof(s2);
1176 exceptions_throw_noclassdeffounderror(c->name);
1180 desc->componentvftbl = NULL;
1181 desc->elementvftbl = NULL;
1182 desc->dimension = 1;
1183 desc->elementtype = desc->arraytype;
1190 /* linker_compute_subclasses ***************************************************
1194 *******************************************************************************/
1196 static void linker_compute_subclasses(classinfo *c)
1198 #if defined(ENABLE_THREADS)
1202 if (!(c->flags & ACC_INTERFACE)) {
1207 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
1208 c->nextsub = c->super.cls->sub;
1209 c->super.cls->sub = c;
1214 /* compute class values */
1216 linker_compute_class_values(class_java_lang_Object);
1218 #if defined(ENABLE_THREADS)
1224 /* linker_compute_class_values *************************************************
1228 *******************************************************************************/
1230 static void linker_compute_class_values(classinfo *c)
1234 c->vftbl->baseval = ++classvalue;
1239 linker_compute_class_values(subs);
1241 subs = subs->nextsub;
1244 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1248 /* linker_addinterface *********************************************************
1250 Is needed by link_class for adding a VTBL to a class. All
1251 interfaces implemented by ic are added as well.
1254 true.........everything ok
1255 false........an exception has been thrown
1257 *******************************************************************************/
1259 static bool linker_addinterface(classinfo *c, classinfo *ic)
1270 if (i >= v->interfacetablelength)
1271 vm_abort("Internal error: interfacetable overflow");
1273 /* if this interface has already been added, return immediately */
1275 if (v->interfacetable[-i] != NULL)
1278 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1279 v->interfacevftbllength[i] = 1;
1280 v->interfacetable[-i] = MNEW(methodptr, 1);
1281 v->interfacetable[-i][0] = NULL;
1284 v->interfacevftbllength[i] = ic->methodscount;
1285 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1287 #if defined(ENABLE_STATISTICS)
1289 count_vftbl_len += sizeof(methodptr) *
1290 (ic->methodscount + (ic->methodscount == 0));
1293 for (j = 0; j < ic->methodscount; j++) {
1294 for (sc = c; sc != NULL; sc = sc->super.cls) {
1295 for (k = 0; k < sc->methodscount; k++) {
1296 m = &(sc->methods[k]);
1298 if (method_canoverwrite(m, &(ic->methods[j]))) {
1299 /* method m overwrites the (abstract) method */
1300 #if defined(ENABLE_VERIFIER)
1301 /* Add loading constraints (for the more
1302 general types of the method
1304 if (!classcache_add_constraints_for_params(
1305 c->classloader, ic->classloader,
1312 /* XXX taken from gcj */
1313 /* check for ACC_STATIC: IncompatibleClassChangeError */
1315 /* check for !ACC_PUBLIC: IllegalAccessError */
1317 /* check for ACC_ABSTRACT: AbstracMethodError,
1318 not sure about that one */
1320 v->interfacetable[-i][j] = v->table[m->vftblindex];
1326 /* If no method was found, insert the AbstractMethodError
1329 #if defined(ENABLE_JIT)
1330 # if defined(ENABLE_INTRP)
1332 v->interfacetable[-i][j] =
1333 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1336 v->interfacetable[-i][j] =
1337 (methodptr) (ptrint) &asm_abstractmethoderror;
1339 v->interfacetable[-i][j] =
1340 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1348 /* add superinterfaces of this interface */
1350 for (j = 0; j < ic->interfacescount; j++)
1351 if (!linker_addinterface(c, ic->interfaces[j].cls))
1360 /* class_highestinterface ******************************************************
1362 Used by the function link_class to determine the amount of memory
1363 needed for the interface table.
1365 *******************************************************************************/
1367 static s4 class_highestinterface(classinfo *c)
1373 /* check for ACC_INTERFACE bit already done in link_class_intern */
1377 for (i = 0; i < c->interfacescount; i++) {
1378 h2 = class_highestinterface(c->interfaces[i].cls);
1389 * These are local overrides for various environment variables in Emacs.
1390 * Please do not remove this and leave it at the end of the file, where
1391 * Emacs will automagically detect them.
1392 * ---------------------------------------------------------------------
1395 * indent-tabs-mode: t
1399 * vim:noexpandtab:sw=4:ts=4: