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 2193 2005-04-02 19:33:43Z edwin $
41 #include "mm/memory.h"
42 #include "native/native.h"
43 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/loader.h"
47 #include "vm/options.h"
48 #include "vm/resolve.h"
49 #include "vm/statistics.h"
50 #include "vm/jit/codegen.inc.h"
53 /* global variables ***********************************************************/
55 static s4 interfaceindex; /* sequential numbering of interfaces */
59 /* primitivetype_table *********************************************************
61 Structure for primitive classes: contains the class for wrapping
62 the primitive type, the primitive class, the name of the class for
63 wrapping, the one character type signature and the name of the
66 CAUTION: Don't change the order of the types. This table is indexed
67 by the ARRAYTYPE_ constants (expcept ARRAYTYPE_OBJECT).
69 *******************************************************************************/
71 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
72 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
73 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
74 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
75 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
76 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
77 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
78 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
79 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
80 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
81 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
82 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
86 /* private functions **********************************************************/
88 static bool link_primitivetype_table(void);
89 static classinfo *link_class_intern(classinfo *c);
90 static arraydescriptor *link_array(classinfo *c);
91 static void linker_compute_class_values(classinfo *c);
92 static void linker_compute_subclasses(classinfo *c);
93 static void linker_addinterface(classinfo *c, classinfo *ic);
94 static s4 class_highestinterface(classinfo *c);
97 /* linker_init *****************************************************************
99 Initializes the linker subsystem.
101 *******************************************************************************/
103 bool linker_init(void)
105 /* reset interface index */
109 /* link important system classes */
111 if (!link_class(class_java_lang_Object))
114 if (!link_class(class_java_lang_String))
117 if (!link_class(class_java_lang_Cloneable))
120 if (!link_class(class_java_io_Serializable))
124 /* link classes for wrapping primitive types */
126 if (!link_class(class_java_lang_Void))
129 if (!link_class(class_java_lang_Boolean))
132 if (!link_class(class_java_lang_Byte))
135 if (!link_class(class_java_lang_Character))
138 if (!link_class(class_java_lang_Short))
141 if (!link_class(class_java_lang_Integer))
144 if (!link_class(class_java_lang_Long))
147 if (!link_class(class_java_lang_Float))
150 if (!link_class(class_java_lang_Double))
154 /* create pseudo classes used by the typechecker */
156 /* pseudo class for Arraystubs (extends java.lang.Object) */
158 pseudo_class_Arraystub = class_new_intern(utf_new_char("$ARRAYSTUB$"));
159 pseudo_class_Arraystub->loaded = true;
160 pseudo_class_Arraystub->super.cls = class_java_lang_Object;
161 pseudo_class_Arraystub->interfacescount = 2;
162 pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
163 pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
164 pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
166 if (!link_class(pseudo_class_Arraystub))
169 /* pseudo class representing the null type */
171 pseudo_class_Null = class_new_intern(utf_new_char("$NULL$"));
172 pseudo_class_Null->loaded = true;
173 pseudo_class_Null->super.cls = class_java_lang_Object;
175 if (!link_class(pseudo_class_Null))
178 /* pseudo class representing new uninitialized objects */
180 pseudo_class_New = class_new_intern(utf_new_char("$NEW$"));
181 pseudo_class_New->loaded = true;
182 pseudo_class_New->linked = true; /* XXX is this allright? */
183 pseudo_class_New->super.cls = class_java_lang_Object;
186 /* create classes representing primitive types */
188 if (!link_primitivetype_table())
192 /* Correct vftbl-entries (retarded loading and linking of class */
193 /* java/lang/String). */
195 stringtable_update();
201 /* link_primitivetype_table ****************************************************
203 Create classes representing primitive types.
205 *******************************************************************************/
207 static bool link_primitivetype_table(void)
212 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
214 if (!primitivetype_table[i].name)
217 /* create primitive class */
218 c = class_new_intern(utf_new_char(primitivetype_table[i].name));
219 c->classUsed = NOTUSED; /* not used initially CO-RT */
222 /* prevent loader from loading primitive class */
227 primitivetype_table[i].class_primitive = c;
229 /* create class for wrapping the primitive type */
230 if (!load_class_bootstrap(utf_new_char(primitivetype_table[i].wrapname),&c))
232 primitivetype_table[i].class_wrap = c;
233 primitivetype_table[i].class_wrap->classUsed = NOTUSED; /* not used initially CO-RT */
234 primitivetype_table[i].class_wrap->impldBy = NULL;
236 /* create the primitive array class */
237 if (primitivetype_table[i].arrayname) {
238 c = class_new_intern(utf_new_char(primitivetype_table[i].arrayname));
239 primitivetype_table[i].arrayclass = c;
244 primitivetype_table[i].arrayvftbl = c->vftbl;
252 /* link_class ******************************************************************
254 Wrapper function for link_class_intern to ease monitor enter/exit
255 and exception handling.
257 *******************************************************************************/
259 classinfo *link_class(classinfo *c)
263 #if defined(USE_THREADS)
264 /* enter a monitor on the class */
266 builtin_monitorenter((java_objectheader *) c);
269 /* maybe the class is already linked */
271 #if defined(USE_THREADS)
272 builtin_monitorexit((java_objectheader *) c);
278 #if defined(STATISTICS)
281 if (getcompilingtime)
282 compilingtime_stop();
288 /* call the internal function */
289 r = link_class_intern(c);
291 /* if return value is NULL, we had a problem and the class is not linked */
295 #if defined(STATISTICS)
301 if (getcompilingtime)
302 compilingtime_start();
305 #if defined(USE_THREADS)
306 /* leave the monitor */
308 builtin_monitorexit((java_objectheader *) c);
315 /* link_class_intern ***********************************************************
317 Tries to link a class. The function calculates the length in bytes
318 that an instance of this class requires as well as the VTBL for
319 methods and interface methods.
321 *******************************************************************************/
323 static classinfo *link_class_intern(classinfo *c)
325 classinfo *super; /* super class */
326 classinfo *tc; /* temporary class variable */
327 s4 supervftbllength; /* vftbllegnth of super class */
328 s4 vftbllength; /* vftbllength of current class */
329 s4 interfacetablelength; /* interface table length */
330 vftbl_t *v; /* vftbl of current class */
331 s4 i,j; /* interface/method/field counter */
332 arraydescriptor *arraydesc; /* descriptor for array classes */
334 /* maybe the class is already linked */
339 log_message_class("Linking class: ", c);
341 /* the class must be loaded */
343 throw_cacao_exception_exit(string_java_lang_InternalError,
344 "Trying to link unloaded class");
346 /* ok, this class is somewhat linked */
351 /* check interfaces */
353 for (i = 0; i < c->interfacescount; i++) {
354 /* resolve this super interface */
355 if (!resolve_classref_or_classinfo(NULL,c->interfaces[i],resolveEager,false,&tc))
357 c->interfaces[i].cls = tc;
359 /* detect circularity */
363 new_exception_utfmessage(string_java_lang_ClassCircularityError,
370 if (!(tc->flags & ACC_INTERFACE)) {
372 new_exception_message(string_java_lang_IncompatibleClassChangeError,
373 "Implementing class");
382 /* check super class */
385 if (c->super.any == NULL) { /* class java.lang.Object */
387 c->classUsed = USED; /* Object class is always used CO-RT*/
389 c->instancesize = sizeof(java_objectheader);
391 vftbllength = supervftbllength = 0;
396 /* resolve super class */
397 if (!resolve_classref_or_classinfo(NULL,c->super,resolveEager,false,&super))
399 c->super.cls = super;
401 /* detect circularity */
404 new_exception_utfmessage(string_java_lang_ClassCircularityError,
409 assert(super->loaded);
411 if (super->flags & ACC_INTERFACE) {
412 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
413 panic("Interface specified as super class");
416 /* Don't allow extending final classes */
417 if (super->flags & ACC_FINAL) {
419 new_exception_message(string_java_lang_VerifyError,
420 "Cannot inherit from final class");
425 if (!link_class(super))
428 /* handle array classes */
429 if (c->name->text[0] == '[')
430 if (!(arraydesc = link_array(c)))
433 if (c->flags & ACC_INTERFACE)
434 c->index = interfaceindex++;
436 c->index = super->index + 1;
438 c->instancesize = super->instancesize;
440 vftbllength = supervftbllength = super->vftbl->vftbllength;
442 c->finalizer = super->finalizer;
446 /* compute vftbl length */
448 for (i = 0; i < c->methodscount; i++) {
449 methodinfo *m = &(c->methods[i]);
451 if (!(m->flags & ACC_STATIC)) { /* is instance method */
457 for (j = 0; j < tc->methodscount; j++) {
458 if (method_canoverwrite(m, &(tc->methods[j]))) {
459 if (tc->methods[j].flags & ACC_PRIVATE)
460 goto notfoundvftblindex;
462 if (tc->methods[j].flags & ACC_FINAL) {
463 /* class a overrides final method . */
465 new_exception(string_java_lang_VerifyError);
469 m->vftblindex = tc->methods[j].vftblindex;
470 goto foundvftblindex;
478 m->vftblindex = (vftbllength++);
485 /* check interfaces of ABSTRACT class for unimplemented methods */
487 if (c->flags & ACC_ABSTRACT) {
490 s4 abstractmethodscount;
494 abstractmethodscount = 0;
496 for (i = 0; i < c->interfacescount; i++) {
497 ic = c->interfaces[i].cls;
499 for (j = 0; j < ic->methodscount; j++) {
500 im = &(ic->methods[j]);
502 /* skip `<clinit>' and `<init>' */
504 if (im->name == utf_clinit || im->name == utf_init)
510 for (k = 0; k < tc->methodscount; k++) {
511 if (method_canoverwrite(im, &(tc->methods[k])))
512 goto noabstractmethod;
518 abstractmethodscount++;
525 if (abstractmethodscount > 0) {
528 /* reallocate methods memory */
530 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
531 c->methodscount + abstractmethodscount);
533 for (i = 0; i < c->interfacescount; i++) {
534 ic = c->interfaces[i].cls;
536 for (j = 0; j < ic->methodscount; j++) {
537 im = &(ic->methods[j]);
539 /* skip `<clinit>' and `<init>' */
541 if (im->name == utf_clinit || im->name == utf_init)
547 for (k = 0; k < tc->methodscount; k++) {
548 if (method_canoverwrite(im, &(tc->methods[k])))
549 goto noabstractmethod2;
555 am = &(c->methods[c->methodscount]);
558 MCOPY(am, im, methodinfo, 1);
560 am->vftblindex = (vftbllength++);
571 #if defined(STATISTICS)
574 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
577 /* compute interfacetable length */
579 interfacetablelength = 0;
582 for (i = 0; i < tc->interfacescount; i++) {
583 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
584 if (h > interfacetablelength)
585 interfacetablelength = h;
590 /* allocate virtual function table */
592 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
593 sizeof(methodptr) * (vftbllength - 1) +
594 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
595 v = (vftbl_t *) (((methodptr *) v) +
596 (interfacetablelength - 1) * (interfacetablelength > 1));
597 c->header.vftbl = c->vftbl = v;
599 v->vftbllength = vftbllength;
600 v->interfacetablelength = interfacetablelength;
601 v->arraydesc = arraydesc;
603 /* store interface index in vftbl */
605 if (c->flags & ACC_INTERFACE)
606 v->baseval = -(c->index);
608 /* copy virtual function table of super class */
610 for (i = 0; i < supervftbllength; i++)
611 v->table[i] = super->vftbl->table[i];
613 /* add method stubs into virtual function table */
615 for (i = 0; i < c->methodscount; i++) {
616 methodinfo *m = &(c->methods[i]);
618 /* Methods in ABSTRACT classes from interfaces maybe already have a */
621 if (!m->stubroutine) {
622 if (!(m->flags & ACC_NATIVE)) {
623 m->stubroutine = createcompilerstub(m);
626 functionptr f = native_findfunction(c->name,
629 (m->flags & ACC_STATIC));
630 #if defined(STATIC_CLASSPATH)
633 m->stubroutine = createnativestub(f, m);
637 if (!(m->flags & ACC_STATIC))
638 v->table[m->vftblindex] = m->stubroutine;
641 /* compute instance size and offset of each field */
643 for (i = 0; i < c->fieldscount; i++) {
645 fieldinfo *f = &(c->fields[i]);
647 if (!(f->flags & ACC_STATIC)) {
648 dsize = desc_typesize(f->descriptor);
649 c->instancesize = ALIGN(c->instancesize, dsize);
650 f->offset = c->instancesize;
651 c->instancesize += dsize;
655 /* initialize interfacetable and interfacevftbllength */
657 v->interfacevftbllength = MNEW(s4, interfacetablelength);
659 #if defined(STATISTICS)
661 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
664 for (i = 0; i < interfacetablelength; i++) {
665 v->interfacevftbllength[i] = 0;
666 v->interfacetable[-i] = NULL;
671 for (tc = c; tc != NULL; tc = tc->super.cls)
672 for (i = 0; i < tc->interfacescount; i++)
673 linker_addinterface(c, tc->interfaces[i].cls);
675 /* add finalizer method (not for java.lang.Object) */
680 fi = class_findmethod(c, utf_finalize, utf_void__void);
683 if (!(fi->flags & ACC_STATIC))
687 /* resolve exception class references */
689 for (i = 0; i < c->methodscount; i++) {
690 methodinfo *m = c->methods + i;
691 for (j=0; j<m->exceptiontablelength; ++j) {
692 if (!m->exceptiontable[j].catchtype.any)
694 if (!resolve_classref_or_classinfo(NULL,m->exceptiontable[j].catchtype,
695 resolveEager,false,&(m->exceptiontable[j].catchtype.cls)))
698 for (j=0; j<m->thrownexceptionscount; ++j)
699 if (!resolve_classref_or_classinfo(NULL,m->thrownexceptions[j],
700 resolveEager,false,&(m->thrownexceptions[j].cls)))
706 linker_compute_subclasses(c);
709 log_message_class("Linking done class: ", c);
711 /* just return c to show that we didn't had a problem */
717 /* link_array ******************************************************************
719 This function is called by link_class to create the arraydescriptor
722 This function returns NULL if the array cannot be linked because
723 the component type has not been linked yet.
725 *******************************************************************************/
727 static arraydescriptor *link_array(classinfo *c)
729 classinfo *comp = NULL;
730 s4 namelen = c->name->blength;
731 arraydescriptor *desc;
734 /* Check the component type */
735 switch (c->name->text[1]) {
737 /* c is an array of arrays. */
738 comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
740 panic("Could not find component array class.");
744 /* c is an array of objects. */
745 comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
747 panic("Could not find component class.");
751 /* If the component type has not been linked, link it now */
752 if (comp && !comp->linked) {
753 assert(comp->loaded);
755 if (!link_class(comp))
759 /* Allocate the arraydescriptor */
760 desc = NEW(arraydescriptor);
763 /* c is an array of references */
764 desc->arraytype = ARRAYTYPE_OBJECT;
765 desc->componentsize = sizeof(void*);
766 desc->dataoffset = OFFSET(java_objectarray, data);
768 compvftbl = comp->vftbl;
770 panic("Component class has no vftbl");
771 desc->componentvftbl = compvftbl;
773 if (compvftbl->arraydesc) {
774 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
775 if (compvftbl->arraydesc->dimension >= 255)
776 panic("Creating array of dimension >255");
777 desc->dimension = compvftbl->arraydesc->dimension + 1;
778 desc->elementtype = compvftbl->arraydesc->elementtype;
781 desc->elementvftbl = compvftbl;
783 desc->elementtype = ARRAYTYPE_OBJECT;
787 /* c is an array of a primitive type */
788 switch (c->name->text[1]) {
790 desc->arraytype = ARRAYTYPE_BOOLEAN;
791 desc->dataoffset = OFFSET(java_booleanarray,data);
792 desc->componentsize = sizeof(u1);
796 desc->arraytype = ARRAYTYPE_BYTE;
797 desc->dataoffset = OFFSET(java_bytearray,data);
798 desc->componentsize = sizeof(u1);
802 desc->arraytype = ARRAYTYPE_CHAR;
803 desc->dataoffset = OFFSET(java_chararray,data);
804 desc->componentsize = sizeof(u2);
808 desc->arraytype = ARRAYTYPE_DOUBLE;
809 desc->dataoffset = OFFSET(java_doublearray,data);
810 desc->componentsize = sizeof(double);
814 desc->arraytype = ARRAYTYPE_FLOAT;
815 desc->dataoffset = OFFSET(java_floatarray,data);
816 desc->componentsize = sizeof(float);
820 desc->arraytype = ARRAYTYPE_INT;
821 desc->dataoffset = OFFSET(java_intarray,data);
822 desc->componentsize = sizeof(s4);
826 desc->arraytype = ARRAYTYPE_LONG;
827 desc->dataoffset = OFFSET(java_longarray,data);
828 desc->componentsize = sizeof(s8);
832 desc->arraytype = ARRAYTYPE_SHORT;
833 desc->dataoffset = OFFSET(java_shortarray,data);
834 desc->componentsize = sizeof(s2);
838 panic("Invalid array class name");
841 desc->componentvftbl = NULL;
842 desc->elementvftbl = NULL;
844 desc->elementtype = desc->arraytype;
851 /* linker_compute_subclasses ***************************************************
855 *******************************************************************************/
857 static void linker_compute_subclasses(classinfo *c)
859 #if defined(USE_THREADS)
860 #if defined(NATIVE_THREADS)
867 if (!(c->flags & ACC_INTERFACE)) {
872 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
873 c->nextsub = c->super.cls->sub;
874 c->super.cls->sub = c;
879 /* this is the java.lang.Object special case */
881 if (!class_java_lang_Object) {
882 linker_compute_class_values(c);
885 linker_compute_class_values(class_java_lang_Object);
888 #if defined(USE_THREADS)
889 #if defined(NATIVE_THREADS)
898 /* linker_compute_class_values *************************************************
902 *******************************************************************************/
904 static void linker_compute_class_values(classinfo *c)
908 c->vftbl->baseval = ++classvalue;
913 linker_compute_class_values(subs);
915 subs = subs->nextsub;
918 c->vftbl->diffval = classvalue - c->vftbl->baseval;
922 /* linker_addinterface *********************************************************
924 Is needed by link_class for adding a VTBL to a class. All
925 interfaces implemented by ic are added as well.
927 *******************************************************************************/
929 static void linker_addinterface(classinfo *c, classinfo *ic)
933 vftbl_t *v = c->vftbl;
935 if (i >= v->interfacetablelength)
936 panic ("Inernal error: interfacetable overflow");
938 if (v->interfacetable[-i])
941 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
942 v->interfacevftbllength[i] = 1;
943 v->interfacetable[-i] = MNEW(methodptr, 1);
944 v->interfacetable[-i][0] = NULL;
947 v->interfacevftbllength[i] = ic->methodscount;
948 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
950 #if defined(STATISTICS)
952 count_vftbl_len += sizeof(methodptr) *
953 (ic->methodscount + (ic->methodscount == 0));
956 for (j = 0; j < ic->methodscount; j++) {
960 for (m = 0; m < sc->methodscount; m++) {
961 methodinfo *mi = &(sc->methods[m]);
963 if (method_canoverwrite(mi, &(ic->methods[j]))) {
964 v->interfacetable[-i][j] = v->table[mi->vftblindex];
975 for (j = 0; j < ic->interfacescount; j++)
976 linker_addinterface(c, ic->interfaces[j].cls);
980 /* class_highestinterface ******************************************************
982 Used by the function link_class to determine the amount of memory
983 needed for the interface table.
985 *******************************************************************************/
987 static s4 class_highestinterface(classinfo *c)
993 /* check for ACC_INTERFACE bit already done in link_class_intern */
997 for (i = 0; i < c->interfacescount; i++) {
998 h2 = class_highestinterface(c->interfaces[i].cls);
1008 /***************** Function: print_arraydescriptor ****************************
1010 Debug helper for displaying an arraydescriptor
1012 *******************************************************************************/
1014 void print_arraydescriptor(FILE *file, arraydescriptor *desc)
1017 fprintf(file, "<NULL>");
1022 if (desc->componentvftbl) {
1023 if (desc->componentvftbl->class)
1024 utf_fprint(file, desc->componentvftbl->class->name);
1026 fprintf(file, "<no classinfo>");
1032 if (desc->elementvftbl) {
1033 if (desc->elementvftbl->class)
1034 utf_fprint(file, desc->elementvftbl->class->name);
1036 fprintf(file, "<no classinfo>");
1040 fprintf(file, ",%d,%d,%d,%d}", desc->arraytype, desc->dimension,
1041 desc->dataoffset, desc->componentsize);
1046 * These are local overrides for various environment variables in Emacs.
1047 * Please do not remove this and leave it at the end of the file, where
1048 * Emacs will automagically detect them.
1049 * ---------------------------------------------------------------------
1052 * indent-tabs-mode: t