1 /* src/vm/linker.c - class linker functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
29 Changes: Andreas Krall
35 $Id: linker.c 2186 2005-04-02 00:43:25Z edwin $
40 #include "mm/memory.h"
41 #include "native/native.h"
42 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/loader.h"
46 #include "vm/options.h"
47 #include "vm/statistics.h"
48 #include "vm/jit/codegen.inc.h"
51 /* global variables ***********************************************************/
53 static s4 interfaceindex; /* sequential numbering of interfaces */
57 /* primitivetype_table *********************************************************
59 Structure for primitive classes: contains the class for wrapping
60 the primitive type, the primitive class, the name of the class for
61 wrapping, the one character type signature and the name of the
64 CAUTION: Don't change the order of the types. This table is indexed
65 by the ARRAYTYPE_ constants (expcept ARRAYTYPE_OBJECT).
67 *******************************************************************************/
69 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
70 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
71 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
72 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
73 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
74 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
75 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
76 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
77 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
78 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
79 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
80 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
84 /* private functions **********************************************************/
86 static bool link_primitivetype_table(void);
87 static classinfo *link_class_intern(classinfo *c);
88 static arraydescriptor *link_array(classinfo *c);
89 static void linker_compute_class_values(classinfo *c);
90 static void linker_compute_subclasses(classinfo *c);
91 static void linker_addinterface(classinfo *c, classinfo *ic);
92 static s4 class_highestinterface(classinfo *c);
95 /* linker_init *****************************************************************
97 Initializes the linker subsystem.
99 *******************************************************************************/
101 bool linker_init(void)
103 /* reset interface index */
107 /* link important system classes */
109 if (!link_class(class_java_lang_Object))
112 if (!link_class(class_java_lang_String))
115 if (!link_class(class_java_lang_Cloneable))
118 if (!link_class(class_java_io_Serializable))
122 /* link classes for wrapping primitive types */
124 if (!link_class(class_java_lang_Void))
127 if (!link_class(class_java_lang_Boolean))
130 if (!link_class(class_java_lang_Byte))
133 if (!link_class(class_java_lang_Character))
136 if (!link_class(class_java_lang_Short))
139 if (!link_class(class_java_lang_Integer))
142 if (!link_class(class_java_lang_Long))
145 if (!link_class(class_java_lang_Float))
148 if (!link_class(class_java_lang_Double))
152 /* create pseudo classes used by the typechecker */
154 /* pseudo class for Arraystubs (extends java.lang.Object) */
156 pseudo_class_Arraystub->loaded = true;
157 pseudo_class_Arraystub->super.cls = class_java_lang_Object;
158 pseudo_class_Arraystub->interfacescount = 2;
159 pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
160 pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
161 pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
163 if (!link_class(pseudo_class_Arraystub))
166 /* pseudo class representing the null type */
168 pseudo_class_Null->loaded = true;
169 pseudo_class_Null->super.cls = class_java_lang_Object;
171 if (!link_class(pseudo_class_Null))
174 /* pseudo class representing new uninitialized objects */
176 pseudo_class_New->loaded = true;
177 pseudo_class_New->linked = true; /* XXX is this allright? */
178 pseudo_class_New->super.cls = class_java_lang_Object;
181 /* create classes representing primitive types */
183 if (!link_primitivetype_table())
187 /* Correct vftbl-entries (retarded loading and linking of class */
188 /* java/lang/String). */
190 stringtable_update();
196 /* link_primitivetype_table ****************************************************
198 Create classes representing primitive types.
200 *******************************************************************************/
202 static bool link_primitivetype_table(void)
207 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
209 if (!primitivetype_table[i].name)
212 /* create primitive class */
213 c = class_new_intern(utf_new_char(primitivetype_table[i].name));
214 c->classUsed = NOTUSED; /* not used initially CO-RT */
217 /* prevent loader from loading primitive class */
222 primitivetype_table[i].class_primitive = c;
224 /* create class for wrapping the primitive type */
225 c = class_new_intern(utf_new_char(primitivetype_table[i].wrapname));
226 primitivetype_table[i].class_wrap = c;
227 primitivetype_table[i].class_wrap->classUsed = NOTUSED; /* not used initially CO-RT */
228 primitivetype_table[i].class_wrap->impldBy = NULL;
230 /* create the primitive array class */
231 if (primitivetype_table[i].arrayname) {
232 c = class_new_intern(utf_new_char(primitivetype_table[i].arrayname));
233 primitivetype_table[i].arrayclass = c;
238 primitivetype_table[i].arrayvftbl = c->vftbl;
246 /* link_class ******************************************************************
248 Wrapper function for link_class_intern to ease monitor enter/exit
249 and exception handling.
251 *******************************************************************************/
253 classinfo *link_class(classinfo *c)
257 #if defined(USE_THREADS)
258 /* enter a monitor on the class */
260 builtin_monitorenter((java_objectheader *) c);
263 /* maybe the class is already linked */
265 #if defined(USE_THREADS)
266 builtin_monitorexit((java_objectheader *) c);
272 #if defined(STATISTICS)
275 if (getcompilingtime)
276 compilingtime_stop();
282 /* call the internal function */
283 r = link_class_intern(c);
285 /* if return value is NULL, we had a problem and the class is not linked */
289 #if defined(STATISTICS)
295 if (getcompilingtime)
296 compilingtime_start();
299 #if defined(USE_THREADS)
300 /* leave the monitor */
302 builtin_monitorexit((java_objectheader *) c);
309 /* link_class_intern ***********************************************************
311 Tries to link a class. The function calculates the length in bytes
312 that an instance of this class requires as well as the VTBL for
313 methods and interface methods.
315 *******************************************************************************/
317 static classinfo *link_class_intern(classinfo *c)
319 classinfo *super; /* super class */
320 classinfo *tc; /* temporary class variable */
321 s4 supervftbllength; /* vftbllegnth of super class */
322 s4 vftbllength; /* vftbllength of current class */
323 s4 interfacetablelength; /* interface table length */
324 vftbl_t *v; /* vftbl of current class */
325 s4 i; /* interface/method/field counter */
326 arraydescriptor *arraydesc; /* descriptor for array classes */
328 /* maybe the class is already linked */
332 /* the class must be loaded */
334 throw_cacao_exception_exit(string_java_lang_InternalError,
335 "Trying to link unloaded class");
338 log_message_class("Linking class: ", c);
340 /* ok, this class is somewhat linked */
345 /* check interfaces */
347 for (i = 0; i < c->interfacescount; i++) {
348 tc = c->interfaces[i].cls;
350 /* detect circularity */
354 new_exception_utfmessage(string_java_lang_ClassCircularityError,
360 if (!load_class_from_classloader(tc, c->classloader))
363 if (!(tc->flags & ACC_INTERFACE)) {
365 new_exception_message(string_java_lang_IncompatibleClassChangeError,
366 "Implementing class");
375 /* check super class */
377 super = c->super.cls;
379 if (super == NULL) { /* class java.lang.Object */
381 c->classUsed = USED; /* Object class is always used CO-RT*/
383 c->instancesize = sizeof(java_objectheader);
385 vftbllength = supervftbllength = 0;
390 /* detect circularity */
393 new_exception_utfmessage(string_java_lang_ClassCircularityError,
399 if (!load_class_from_classloader(super, c->classloader))
402 if (super->flags & ACC_INTERFACE) {
403 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
404 panic("Interface specified as super class");
407 /* Don't allow extending final classes */
408 if (super->flags & ACC_FINAL) {
410 new_exception_message(string_java_lang_VerifyError,
411 "Cannot inherit from final class");
416 if (!link_class(super))
419 /* handle array classes */
420 if (c->name->text[0] == '[')
421 if (!(arraydesc = link_array(c)))
424 if (c->flags & ACC_INTERFACE)
425 c->index = interfaceindex++;
427 c->index = super->index + 1;
429 c->instancesize = super->instancesize;
431 vftbllength = supervftbllength = super->vftbl->vftbllength;
433 c->finalizer = super->finalizer;
437 /* compute vftbl length */
439 for (i = 0; i < c->methodscount; i++) {
440 methodinfo *m = &(c->methods[i]);
442 if (!(m->flags & ACC_STATIC)) { /* is instance method */
448 for (j = 0; j < tc->methodscount; j++) {
449 if (method_canoverwrite(m, &(tc->methods[j]))) {
450 if (tc->methods[j].flags & ACC_PRIVATE)
451 goto notfoundvftblindex;
453 if (tc->methods[j].flags & ACC_FINAL) {
454 /* class a overrides final method . */
456 new_exception(string_java_lang_VerifyError);
460 m->vftblindex = tc->methods[j].vftblindex;
461 goto foundvftblindex;
469 m->vftblindex = (vftbllength++);
476 /* check interfaces of ABSTRACT class for unimplemented methods */
478 if (c->flags & ACC_ABSTRACT) {
481 s4 abstractmethodscount;
485 abstractmethodscount = 0;
487 for (i = 0; i < c->interfacescount; i++) {
488 ic = c->interfaces[i].cls;
490 for (j = 0; j < ic->methodscount; j++) {
491 im = &(ic->methods[j]);
493 /* skip `<clinit>' and `<init>' */
495 if (im->name == utf_clinit || im->name == utf_init)
501 for (k = 0; k < tc->methodscount; k++) {
502 if (method_canoverwrite(im, &(tc->methods[k])))
503 goto noabstractmethod;
509 abstractmethodscount++;
516 if (abstractmethodscount > 0) {
519 /* reallocate methods memory */
521 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
522 c->methodscount + abstractmethodscount);
524 for (i = 0; i < c->interfacescount; i++) {
525 ic = c->interfaces[i].cls;
527 for (j = 0; j < ic->methodscount; j++) {
528 im = &(ic->methods[j]);
530 /* skip `<clinit>' and `<init>' */
532 if (im->name == utf_clinit || im->name == utf_init)
538 for (k = 0; k < tc->methodscount; k++) {
539 if (method_canoverwrite(im, &(tc->methods[k])))
540 goto noabstractmethod2;
546 am = &(c->methods[c->methodscount]);
549 MCOPY(am, im, methodinfo, 1);
551 am->vftblindex = (vftbllength++);
562 #if defined(STATISTICS)
565 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
568 /* compute interfacetable length */
570 interfacetablelength = 0;
573 for (i = 0; i < tc->interfacescount; i++) {
574 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
575 if (h > interfacetablelength)
576 interfacetablelength = h;
581 /* allocate virtual function table */
583 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
584 sizeof(methodptr) * (vftbllength - 1) +
585 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
586 v = (vftbl_t *) (((methodptr *) v) +
587 (interfacetablelength - 1) * (interfacetablelength > 1));
588 c->header.vftbl = c->vftbl = v;
590 v->vftbllength = vftbllength;
591 v->interfacetablelength = interfacetablelength;
592 v->arraydesc = arraydesc;
594 /* store interface index in vftbl */
596 if (c->flags & ACC_INTERFACE)
597 v->baseval = -(c->index);
599 /* copy virtual function table of super class */
601 for (i = 0; i < supervftbllength; i++)
602 v->table[i] = super->vftbl->table[i];
604 /* add method stubs into virtual function table */
606 for (i = 0; i < c->methodscount; i++) {
607 methodinfo *m = &(c->methods[i]);
609 /* Methods in ABSTRACT classes from interfaces maybe already have a */
612 if (!m->stubroutine) {
613 if (!(m->flags & ACC_NATIVE)) {
614 m->stubroutine = createcompilerstub(m);
617 functionptr f = native_findfunction(c->name,
620 (m->flags & ACC_STATIC));
621 #if defined(STATIC_CLASSPATH)
624 m->stubroutine = createnativestub(f, m);
628 if (!(m->flags & ACC_STATIC))
629 v->table[m->vftblindex] = m->stubroutine;
632 /* compute instance size and offset of each field */
634 for (i = 0; i < c->fieldscount; i++) {
636 fieldinfo *f = &(c->fields[i]);
638 if (!(f->flags & ACC_STATIC)) {
639 dsize = desc_typesize(f->descriptor);
640 c->instancesize = ALIGN(c->instancesize, dsize);
641 f->offset = c->instancesize;
642 c->instancesize += dsize;
646 /* initialize interfacetable and interfacevftbllength */
648 v->interfacevftbllength = MNEW(s4, interfacetablelength);
650 #if defined(STATISTICS)
652 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
655 for (i = 0; i < interfacetablelength; i++) {
656 v->interfacevftbllength[i] = 0;
657 v->interfacetable[-i] = NULL;
662 for (tc = c; tc != NULL; tc = tc->super.cls)
663 for (i = 0; i < tc->interfacescount; i++)
664 linker_addinterface(c, tc->interfaces[i].cls);
666 /* add finalizer method (not for java.lang.Object) */
671 fi = class_findmethod(c, utf_finalize, utf_void__void);
674 if (!(fi->flags & ACC_STATIC))
680 linker_compute_subclasses(c);
683 log_message_class("Linking done class: ", c);
685 /* just return c to show that we didn't had a problem */
691 /* link_array ******************************************************************
693 This function is called by link_class to create the arraydescriptor
696 This function returns NULL if the array cannot be linked because
697 the component type has not been linked yet.
699 *******************************************************************************/
701 static arraydescriptor *link_array(classinfo *c)
703 classinfo *comp = NULL;
704 s4 namelen = c->name->blength;
705 arraydescriptor *desc;
708 /* Check the component type */
709 switch (c->name->text[1]) {
711 /* c is an array of arrays. */
712 comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
714 panic("Could not find component array class.");
718 /* c is an array of objects. */
719 comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
721 panic("Could not find component class.");
725 /* If the component type has not been linked, link it now */
726 if (comp && !comp->linked) {
728 if (!load_class_from_classloader(comp, c->classloader))
731 if (!link_class(comp))
735 /* Allocate the arraydescriptor */
736 desc = NEW(arraydescriptor);
739 /* c is an array of references */
740 desc->arraytype = ARRAYTYPE_OBJECT;
741 desc->componentsize = sizeof(void*);
742 desc->dataoffset = OFFSET(java_objectarray, data);
744 compvftbl = comp->vftbl;
746 panic("Component class has no vftbl");
747 desc->componentvftbl = compvftbl;
749 if (compvftbl->arraydesc) {
750 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
751 if (compvftbl->arraydesc->dimension >= 255)
752 panic("Creating array of dimension >255");
753 desc->dimension = compvftbl->arraydesc->dimension + 1;
754 desc->elementtype = compvftbl->arraydesc->elementtype;
757 desc->elementvftbl = compvftbl;
759 desc->elementtype = ARRAYTYPE_OBJECT;
763 /* c is an array of a primitive type */
764 switch (c->name->text[1]) {
766 desc->arraytype = ARRAYTYPE_BOOLEAN;
767 desc->dataoffset = OFFSET(java_booleanarray,data);
768 desc->componentsize = sizeof(u1);
772 desc->arraytype = ARRAYTYPE_BYTE;
773 desc->dataoffset = OFFSET(java_bytearray,data);
774 desc->componentsize = sizeof(u1);
778 desc->arraytype = ARRAYTYPE_CHAR;
779 desc->dataoffset = OFFSET(java_chararray,data);
780 desc->componentsize = sizeof(u2);
784 desc->arraytype = ARRAYTYPE_DOUBLE;
785 desc->dataoffset = OFFSET(java_doublearray,data);
786 desc->componentsize = sizeof(double);
790 desc->arraytype = ARRAYTYPE_FLOAT;
791 desc->dataoffset = OFFSET(java_floatarray,data);
792 desc->componentsize = sizeof(float);
796 desc->arraytype = ARRAYTYPE_INT;
797 desc->dataoffset = OFFSET(java_intarray,data);
798 desc->componentsize = sizeof(s4);
802 desc->arraytype = ARRAYTYPE_LONG;
803 desc->dataoffset = OFFSET(java_longarray,data);
804 desc->componentsize = sizeof(s8);
808 desc->arraytype = ARRAYTYPE_SHORT;
809 desc->dataoffset = OFFSET(java_shortarray,data);
810 desc->componentsize = sizeof(s2);
814 panic("Invalid array class name");
817 desc->componentvftbl = NULL;
818 desc->elementvftbl = NULL;
820 desc->elementtype = desc->arraytype;
827 /* linker_compute_subclasses ***************************************************
831 *******************************************************************************/
833 static void linker_compute_subclasses(classinfo *c)
835 #if defined(USE_THREADS)
836 #if defined(NATIVE_THREADS)
843 if (!(c->flags & ACC_INTERFACE)) {
848 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
849 c->nextsub = c->super.cls->sub;
850 c->super.cls->sub = c;
855 /* this is the java.lang.Object special case */
857 if (!class_java_lang_Object) {
858 linker_compute_class_values(c);
861 linker_compute_class_values(class_java_lang_Object);
864 #if defined(USE_THREADS)
865 #if defined(NATIVE_THREADS)
874 /* linker_compute_class_values *************************************************
878 *******************************************************************************/
880 static void linker_compute_class_values(classinfo *c)
884 c->vftbl->baseval = ++classvalue;
889 linker_compute_class_values(subs);
891 subs = subs->nextsub;
894 c->vftbl->diffval = classvalue - c->vftbl->baseval;
898 /* linker_addinterface *********************************************************
900 Is needed by link_class for adding a VTBL to a class. All
901 interfaces implemented by ic are added as well.
903 *******************************************************************************/
905 static void linker_addinterface(classinfo *c, classinfo *ic)
909 vftbl_t *v = c->vftbl;
911 if (i >= v->interfacetablelength)
912 panic ("Inernal error: interfacetable overflow");
914 if (v->interfacetable[-i])
917 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
918 v->interfacevftbllength[i] = 1;
919 v->interfacetable[-i] = MNEW(methodptr, 1);
920 v->interfacetable[-i][0] = NULL;
923 v->interfacevftbllength[i] = ic->methodscount;
924 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
926 #if defined(STATISTICS)
928 count_vftbl_len += sizeof(methodptr) *
929 (ic->methodscount + (ic->methodscount == 0));
932 for (j = 0; j < ic->methodscount; j++) {
936 for (m = 0; m < sc->methodscount; m++) {
937 methodinfo *mi = &(sc->methods[m]);
939 if (method_canoverwrite(mi, &(ic->methods[j]))) {
940 v->interfacetable[-i][j] = v->table[mi->vftblindex];
951 for (j = 0; j < ic->interfacescount; j++)
952 linker_addinterface(c, ic->interfaces[j].cls);
956 /* class_highestinterface ******************************************************
958 Used by the function link_class to determine the amount of memory
959 needed for the interface table.
961 *******************************************************************************/
963 static s4 class_highestinterface(classinfo *c)
969 /* check for ACC_INTERFACE bit already done in link_class_intern */
973 for (i = 0; i < c->interfacescount; i++) {
974 h2 = class_highestinterface(c->interfaces[i].cls);
984 /***************** Function: print_arraydescriptor ****************************
986 Debug helper for displaying an arraydescriptor
988 *******************************************************************************/
990 void print_arraydescriptor(FILE *file, arraydescriptor *desc)
993 fprintf(file, "<NULL>");
998 if (desc->componentvftbl) {
999 if (desc->componentvftbl->class)
1000 utf_fprint(file, desc->componentvftbl->class->name);
1002 fprintf(file, "<no classinfo>");
1008 if (desc->elementvftbl) {
1009 if (desc->elementvftbl->class)
1010 utf_fprint(file, desc->elementvftbl->class->name);
1012 fprintf(file, "<no classinfo>");
1016 fprintf(file, ",%d,%d,%d,%d}", desc->arraytype, desc->dimension,
1017 desc->dataoffset, desc->componentsize);
1022 * These are local overrides for various environment variables in Emacs.
1023 * Please do not remove this and leave it at the end of the file, where
1024 * Emacs will automagically detect them.
1025 * ---------------------------------------------------------------------
1028 * indent-tabs-mode: t