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 2501 2005-05-23 08:19:53Z twisti $
42 #include "mm/memory.h"
43 #include "native/native.h"
44 #include "vm/builtin.h"
46 #include "vm/classcache.h"
47 #include "vm/exceptions.h"
48 #include "vm/loader.h"
49 #include "vm/options.h"
50 #include "vm/resolve.h"
51 #include "vm/statistics.h"
52 #include "vm/stringlocal.h"
53 #include "vm/jit/codegen.inc.h"
56 /* global variables ***********************************************************/
58 static s4 interfaceindex; /* sequential numbering of interfaces */
62 /* primitivetype_table *********************************************************
64 Structure for primitive classes: contains the class for wrapping
65 the primitive type, the primitive class, the name of the class for
66 wrapping, the one character type signature and the name of the
69 CAUTION: Don't change the order of the types. This table is indexed
70 by the ARRAYTYPE_ constants (expcept ARRAYTYPE_OBJECT).
72 *******************************************************************************/
74 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
75 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
76 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
77 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
78 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
79 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
80 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
81 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
82 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
83 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
84 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
85 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
89 /* private functions **********************************************************/
91 static bool link_primitivetype_table(void);
92 static classinfo *link_class_intern(classinfo *c);
93 static arraydescriptor *link_array(classinfo *c);
94 static void linker_compute_class_values(classinfo *c);
95 static void linker_compute_subclasses(classinfo *c);
96 static void linker_addinterface(classinfo *c, classinfo *ic);
97 static s4 class_highestinterface(classinfo *c);
100 /* linker_init *****************************************************************
102 Initializes the linker subsystem.
104 *******************************************************************************/
106 bool linker_init(void)
108 /* reset interface index */
112 /* link important system classes */
114 if (!link_class(class_java_lang_Object))
117 if (!link_class(class_java_lang_String))
120 if (!link_class(class_java_lang_Cloneable))
123 if (!link_class(class_java_io_Serializable))
127 /* link classes for wrapping primitive types */
129 if (!link_class(class_java_lang_Void))
132 if (!link_class(class_java_lang_Boolean))
135 if (!link_class(class_java_lang_Byte))
138 if (!link_class(class_java_lang_Character))
141 if (!link_class(class_java_lang_Short))
144 if (!link_class(class_java_lang_Integer))
147 if (!link_class(class_java_lang_Long))
150 if (!link_class(class_java_lang_Float))
153 if (!link_class(class_java_lang_Double))
157 /* load some other important classes */
159 if (!link_class(class_java_lang_Class))
162 if (!link_class(class_java_lang_ClassLoader))
165 if (!link_class(class_java_lang_SecurityManager))
168 if (!link_class(class_java_lang_System))
171 if (!link_class(class_java_lang_ThreadGroup))
175 if (!link_class(class_java_util_Vector))
179 /* create pseudo classes used by the typechecker */
181 /* pseudo class for Arraystubs (extends java.lang.Object) */
183 pseudo_class_Arraystub =
184 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
185 pseudo_class_Arraystub->loaded = true;
186 pseudo_class_Arraystub->super.cls = class_java_lang_Object;
187 pseudo_class_Arraystub->interfacescount = 2;
188 pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
189 pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
190 pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
192 if (!classcache_store(NULL, pseudo_class_Arraystub)) {
193 log_text("could not cache pseudo_class_Arraystub");
197 if (!link_class(pseudo_class_Arraystub))
200 /* pseudo class representing the null type */
202 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
203 pseudo_class_Null->loaded = true;
204 pseudo_class_Null->super.cls = class_java_lang_Object;
206 if (!classcache_store(NULL, pseudo_class_Null)) {
207 log_text("could not cache pseudo_class_Null");
211 if (!link_class(pseudo_class_Null))
214 /* pseudo class representing new uninitialized objects */
216 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
217 pseudo_class_New->loaded = true;
218 pseudo_class_New->linked = true; /* XXX is this allright? */
219 pseudo_class_New->super.cls = class_java_lang_Object;
221 if (!classcache_store(NULL, pseudo_class_New)) {
222 log_text("could not cache pseudo_class_New");
226 /* create classes representing primitive types */
228 if (!link_primitivetype_table())
232 /* Correct vftbl-entries (retarded loading and linking of class */
233 /* java/lang/String). */
235 stringtable_update();
241 /* link_primitivetype_table ****************************************************
243 Create classes representing primitive types.
245 *******************************************************************************/
247 static bool link_primitivetype_table(void)
252 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
254 if (!primitivetype_table[i].name)
257 /* create primitive class */
258 c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
259 c->classUsed = NOTUSED; /* not used initially CO-RT */
262 /* prevent loader from loading primitive class */
264 if (!classcache_store(NULL,c)) {
265 log_text("Could not cache primitive class");
271 primitivetype_table[i].class_primitive = c;
273 /* create class for wrapping the primitive type */
274 if (!load_class_bootstrap(utf_new_char(primitivetype_table[i].wrapname),&c))
276 primitivetype_table[i].class_wrap = c;
277 primitivetype_table[i].class_wrap->classUsed = NOTUSED; /* not used initially CO-RT */
278 primitivetype_table[i].class_wrap->impldBy = NULL;
280 /* create the primitive array class */
281 if (primitivetype_table[i].arrayname) {
282 c = class_create_classinfo(utf_new_char(primitivetype_table[i].arrayname));
283 if (!load_newly_created_array(c, NULL))
285 primitivetype_table[i].arrayclass = c;
290 primitivetype_table[i].arrayvftbl = c->vftbl;
298 /* link_class ******************************************************************
300 Wrapper function for link_class_intern to ease monitor enter/exit
301 and exception handling.
303 *******************************************************************************/
305 classinfo *link_class(classinfo *c)
310 *exceptionptr = new_nullpointerexception();
314 #if defined(USE_THREADS)
315 /* enter a monitor on the class */
317 builtin_monitorenter((java_objectheader *) c);
320 /* maybe the class is already linked */
322 #if defined(USE_THREADS)
323 builtin_monitorexit((java_objectheader *) c);
329 #if defined(STATISTICS)
332 if (getcompilingtime)
333 compilingtime_stop();
339 /* call the internal function */
340 r = link_class_intern(c);
342 /* if return value is NULL, we had a problem and the class is not linked */
346 #if defined(STATISTICS)
352 if (getcompilingtime)
353 compilingtime_start();
356 #if defined(USE_THREADS)
357 /* leave the monitor */
359 builtin_monitorexit((java_objectheader *) c);
366 /* link_class_intern ***********************************************************
368 Tries to link a class. The function calculates the length in bytes
369 that an instance of this class requires as well as the VTBL for
370 methods and interface methods.
372 *******************************************************************************/
374 static classinfo *link_class_intern(classinfo *c)
376 classinfo *super; /* super class */
377 classinfo *tc; /* temporary class variable */
378 s4 supervftbllength; /* vftbllegnth of super class */
379 s4 vftbllength; /* vftbllength of current class */
380 s4 interfacetablelength; /* interface table length */
381 vftbl_t *v; /* vftbl of current class */
382 s4 i,j; /* interface/method/field counter */
383 arraydescriptor *arraydesc; /* descriptor for array classes */
385 /* maybe the class is already linked */
390 log_message_class("Linking class: ", c);
392 /* the class must be loaded */
394 throw_cacao_exception_exit(string_java_lang_InternalError,
395 "Trying to link unloaded class");
397 /* ok, this class is somewhat linked */
402 /* check interfaces */
404 for (i = 0; i < c->interfacescount; i++) {
405 /* resolve this super interface */
406 if (!resolve_classref_or_classinfo(NULL,c->interfaces[i],resolveEager,false,&tc))
408 c->interfaces[i].cls = tc;
410 /* detect circularity */
414 new_exception_utfmessage(string_java_lang_ClassCircularityError,
421 if (!(tc->flags & ACC_INTERFACE)) {
423 new_exception_message(string_java_lang_IncompatibleClassChangeError,
424 "Implementing class");
433 /* check super class */
436 if (c->super.any == NULL) { /* class java.lang.Object */
438 c->classUsed = USED; /* Object class is always used CO-RT*/
440 c->instancesize = sizeof(java_objectheader);
442 vftbllength = supervftbllength = 0;
447 /* resolve super class */
448 if (!resolve_classref_or_classinfo(NULL,c->super,resolveEager,false,&super))
450 c->super.cls = super;
452 /* detect circularity */
455 new_exception_utfmessage(string_java_lang_ClassCircularityError,
460 assert(super->loaded);
462 if (super->flags & ACC_INTERFACE) {
463 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
464 log_text("Interface specified as super class");
468 /* Don't allow extending final classes */
469 if (super->flags & ACC_FINAL) {
471 new_exception_message(string_java_lang_VerifyError,
472 "Cannot inherit from final class");
477 if (!link_class(super))
480 /* handle array classes */
481 if (c->name->text[0] == '[')
482 if (!(arraydesc = link_array(c)))
485 if (c->flags & ACC_INTERFACE)
486 c->index = interfaceindex++;
488 c->index = super->index + 1;
490 c->instancesize = super->instancesize;
492 vftbllength = supervftbllength = super->vftbl->vftbllength;
494 c->finalizer = super->finalizer;
498 /* compute vftbl length */
500 for (i = 0; i < c->methodscount; i++) {
501 methodinfo *m = &(c->methods[i]);
503 if (!(m->flags & ACC_STATIC)) { /* is instance method */
509 for (j = 0; j < tc->methodscount; j++) {
510 if (method_canoverwrite(m, &(tc->methods[j]))) {
511 if (tc->methods[j].flags & ACC_PRIVATE)
512 goto notfoundvftblindex;
514 if (tc->methods[j].flags & ACC_FINAL) {
515 /* class a overrides final method . */
517 new_exception(string_java_lang_VerifyError);
521 m->vftblindex = tc->methods[j].vftblindex;
522 goto foundvftblindex;
530 m->vftblindex = (vftbllength++);
537 /* check interfaces of ABSTRACT class for unimplemented methods */
539 if (c->flags & ACC_ABSTRACT) {
542 s4 abstractmethodscount;
546 abstractmethodscount = 0;
548 for (i = 0; i < c->interfacescount; i++) {
549 ic = c->interfaces[i].cls;
551 for (j = 0; j < ic->methodscount; j++) {
552 im = &(ic->methods[j]);
554 /* skip `<clinit>' and `<init>' */
556 if (im->name == utf_clinit || im->name == utf_init)
562 for (k = 0; k < tc->methodscount; k++) {
563 if (method_canoverwrite(im, &(tc->methods[k])))
564 goto noabstractmethod;
570 abstractmethodscount++;
577 if (abstractmethodscount > 0) {
580 /* reallocate methods memory */
582 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
583 c->methodscount + abstractmethodscount);
585 for (i = 0; i < c->interfacescount; i++) {
586 ic = c->interfaces[i].cls;
588 for (j = 0; j < ic->methodscount; j++) {
589 im = &(ic->methods[j]);
591 /* skip `<clinit>' and `<init>' */
593 if (im->name == utf_clinit || im->name == utf_init)
599 for (k = 0; k < tc->methodscount; k++) {
600 if (method_canoverwrite(im, &(tc->methods[k])))
601 goto noabstractmethod2;
607 am = &(c->methods[c->methodscount]);
610 MCOPY(am, im, methodinfo, 1);
612 am->vftblindex = (vftbllength++);
623 #if defined(STATISTICS)
626 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
629 /* compute interfacetable length */
631 interfacetablelength = 0;
634 for (i = 0; i < tc->interfacescount; i++) {
635 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
636 if (h > interfacetablelength)
637 interfacetablelength = h;
642 /* allocate virtual function table */
644 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
645 sizeof(methodptr) * (vftbllength - 1) +
646 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
647 v = (vftbl_t *) (((methodptr *) v) +
648 (interfacetablelength - 1) * (interfacetablelength > 1));
649 c->header.vftbl = c->vftbl = v;
651 v->vftbllength = vftbllength;
652 v->interfacetablelength = interfacetablelength;
653 v->arraydesc = arraydesc;
655 /* store interface index in vftbl */
657 if (c->flags & ACC_INTERFACE)
658 v->baseval = -(c->index);
660 /* copy virtual function table of super class */
662 for (i = 0; i < supervftbllength; i++)
663 v->table[i] = super->vftbl->table[i];
665 /* add method stubs into virtual function table */
667 for (i = 0; i < c->methodscount; i++) {
668 methodinfo *m = &(c->methods[i]);
670 /* Methods in ABSTRACT classes from interfaces maybe already have a */
673 if (!m->stubroutine) {
674 if (!(m->flags & ACC_NATIVE)) {
675 m->stubroutine = createcompilerstub(m);
678 functionptr f = native_findfunction(c->name,
681 (m->flags & ACC_STATIC));
682 #if defined(STATIC_CLASSPATH)
685 m->stubroutine = createnativestub(f, m);
689 if (!(m->flags & ACC_STATIC))
690 v->table[m->vftblindex] = m->stubroutine;
693 /* compute instance size and offset of each field */
695 for (i = 0; i < c->fieldscount; i++) {
697 fieldinfo *f = &(c->fields[i]);
699 if (!(f->flags & ACC_STATIC)) {
700 dsize = desc_typesize(f->descriptor);
701 c->instancesize = ALIGN(c->instancesize, dsize);
702 f->offset = c->instancesize;
703 c->instancesize += dsize;
707 /* initialize interfacetable and interfacevftbllength */
709 v->interfacevftbllength = MNEW(s4, interfacetablelength);
711 #if defined(STATISTICS)
713 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
716 for (i = 0; i < interfacetablelength; i++) {
717 v->interfacevftbllength[i] = 0;
718 v->interfacetable[-i] = NULL;
723 for (tc = c; tc != NULL; tc = tc->super.cls)
724 for (i = 0; i < tc->interfacescount; i++)
725 linker_addinterface(c, tc->interfaces[i].cls);
727 /* add finalizer method (not for java.lang.Object) */
732 fi = class_findmethod(c, utf_finalize, utf_void__void);
735 if (!(fi->flags & ACC_STATIC))
739 /* resolve exception class references */
741 for (i = 0; i < c->methodscount; i++) {
742 methodinfo *m = c->methods + i;
743 for (j=0; j<m->exceptiontablelength; ++j) {
744 if (!m->exceptiontable[j].catchtype.any)
746 if (!resolve_classref_or_classinfo(NULL,m->exceptiontable[j].catchtype,
747 resolveEager,false,&(m->exceptiontable[j].catchtype.cls)))
750 for (j=0; j<m->thrownexceptionscount; ++j)
751 if (!resolve_classref_or_classinfo(NULL,m->thrownexceptions[j],
752 resolveEager,false,&(m->thrownexceptions[j].cls)))
758 linker_compute_subclasses(c);
761 log_message_class("Linking done class: ", c);
763 /* just return c to show that we didn't had a problem */
769 /* link_array ******************************************************************
771 This function is called by link_class to create the arraydescriptor
774 This function returns NULL if the array cannot be linked because
775 the component type has not been linked yet.
777 *******************************************************************************/
779 static arraydescriptor *link_array(classinfo *c)
781 classinfo *comp = NULL;
782 s4 namelen = c->name->blength;
783 arraydescriptor *desc;
786 /* Check the component type */
787 switch (c->name->text[1]) {
789 /* c is an array of arrays. */
790 if (!load_class_from_classloader(utf_new_intern(c->name->text + 1, namelen - 1),
791 c->classloader,&comp))
796 /* c is an array of objects. */
797 if (!load_class_from_classloader(utf_new_intern(c->name->text + 2, namelen - 3),
798 c->classloader,&comp))
803 /* If the component type has not been linked, link it now */
804 assert(!comp || comp->loaded);
805 if (comp && !comp->linked) {
807 if (!link_class(comp))
811 /* Allocate the arraydescriptor */
812 desc = NEW(arraydescriptor);
815 /* c is an array of references */
816 desc->arraytype = ARRAYTYPE_OBJECT;
817 desc->componentsize = sizeof(void*);
818 desc->dataoffset = OFFSET(java_objectarray, data);
820 compvftbl = comp->vftbl;
823 log_text("Component class has no vftbl");
827 desc->componentvftbl = compvftbl;
829 if (compvftbl->arraydesc) {
830 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
832 if (compvftbl->arraydesc->dimension >= 255) {
833 log_text("Creating array of dimension >255");
837 desc->dimension = compvftbl->arraydesc->dimension + 1;
838 desc->elementtype = compvftbl->arraydesc->elementtype;
841 desc->elementvftbl = compvftbl;
843 desc->elementtype = ARRAYTYPE_OBJECT;
847 /* c is an array of a primitive type */
848 switch (c->name->text[1]) {
850 desc->arraytype = ARRAYTYPE_BOOLEAN;
851 desc->dataoffset = OFFSET(java_booleanarray,data);
852 desc->componentsize = sizeof(u1);
856 desc->arraytype = ARRAYTYPE_BYTE;
857 desc->dataoffset = OFFSET(java_bytearray,data);
858 desc->componentsize = sizeof(u1);
862 desc->arraytype = ARRAYTYPE_CHAR;
863 desc->dataoffset = OFFSET(java_chararray,data);
864 desc->componentsize = sizeof(u2);
868 desc->arraytype = ARRAYTYPE_DOUBLE;
869 desc->dataoffset = OFFSET(java_doublearray,data);
870 desc->componentsize = sizeof(double);
874 desc->arraytype = ARRAYTYPE_FLOAT;
875 desc->dataoffset = OFFSET(java_floatarray,data);
876 desc->componentsize = sizeof(float);
880 desc->arraytype = ARRAYTYPE_INT;
881 desc->dataoffset = OFFSET(java_intarray,data);
882 desc->componentsize = sizeof(s4);
886 desc->arraytype = ARRAYTYPE_LONG;
887 desc->dataoffset = OFFSET(java_longarray,data);
888 desc->componentsize = sizeof(s8);
892 desc->arraytype = ARRAYTYPE_SHORT;
893 desc->dataoffset = OFFSET(java_shortarray,data);
894 desc->componentsize = sizeof(s2);
898 log_text("Invalid array class name");
902 desc->componentvftbl = NULL;
903 desc->elementvftbl = NULL;
905 desc->elementtype = desc->arraytype;
912 /* linker_compute_subclasses ***************************************************
916 *******************************************************************************/
918 static void linker_compute_subclasses(classinfo *c)
920 #if defined(USE_THREADS)
921 #if defined(NATIVE_THREADS)
928 if (!(c->flags & ACC_INTERFACE)) {
933 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
934 c->nextsub = c->super.cls->sub;
935 c->super.cls->sub = c;
940 /* compute class values */
942 linker_compute_class_values(class_java_lang_Object);
944 #if defined(USE_THREADS)
945 #if defined(NATIVE_THREADS)
954 /* linker_compute_class_values *************************************************
958 *******************************************************************************/
960 static void linker_compute_class_values(classinfo *c)
964 c->vftbl->baseval = ++classvalue;
969 linker_compute_class_values(subs);
971 subs = subs->nextsub;
974 c->vftbl->diffval = classvalue - c->vftbl->baseval;
978 /* linker_addinterface *********************************************************
980 Is needed by link_class for adding a VTBL to a class. All
981 interfaces implemented by ic are added as well.
983 *******************************************************************************/
985 static void linker_addinterface(classinfo *c, classinfo *ic)
989 vftbl_t *v = c->vftbl;
991 if (i >= v->interfacetablelength) {
992 log_text("Inernal error: interfacetable overflow");
996 if (v->interfacetable[-i])
999 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1000 v->interfacevftbllength[i] = 1;
1001 v->interfacetable[-i] = MNEW(methodptr, 1);
1002 v->interfacetable[-i][0] = NULL;
1005 v->interfacevftbllength[i] = ic->methodscount;
1006 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1008 #if defined(STATISTICS)
1010 count_vftbl_len += sizeof(methodptr) *
1011 (ic->methodscount + (ic->methodscount == 0));
1014 for (j = 0; j < ic->methodscount; j++) {
1018 for (m = 0; m < sc->methodscount; m++) {
1019 methodinfo *mi = &(sc->methods[m]);
1021 if (method_canoverwrite(mi, &(ic->methods[j]))) {
1022 v->interfacetable[-i][j] = v->table[mi->vftblindex];
1033 for (j = 0; j < ic->interfacescount; j++)
1034 linker_addinterface(c, ic->interfaces[j].cls);
1038 /* class_highestinterface ******************************************************
1040 Used by the function link_class to determine the amount of memory
1041 needed for the interface table.
1043 *******************************************************************************/
1045 static s4 class_highestinterface(classinfo *c)
1051 /* check for ACC_INTERFACE bit already done in link_class_intern */
1055 for (i = 0; i < c->interfacescount; i++) {
1056 h2 = class_highestinterface(c->interfaces[i].cls);
1067 * These are local overrides for various environment variables in Emacs.
1068 * Please do not remove this and leave it at the end of the file, where
1069 * Emacs will automagically detect them.
1070 * ---------------------------------------------------------------------
1073 * indent-tabs-mode: t