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 2169 2005-03-31 15:50:57Z twisti $
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, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
75 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
76 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
77 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
78 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
82 /* private functions **********************************************************/
84 static bool link_primitivetype_table(void);
85 static classinfo *link_class_intern(classinfo *c);
86 static arraydescriptor *link_array(classinfo *c);
87 static void linker_compute_class_values(classinfo *c);
88 static void linker_compute_subclasses(classinfo *c);
89 static void linker_addinterface(classinfo *c, classinfo *ic);
90 static s4 class_highestinterface(classinfo *c);
93 /* linker_init *****************************************************************
95 Initializes the linker subsystem.
97 *******************************************************************************/
99 bool linker_init(void)
101 /* reset interface index */
105 /* link important system classes */
107 if (!link_class(class_java_lang_Object))
110 if (!link_class(class_java_lang_String))
113 if (!link_class(class_java_lang_Cloneable))
116 if (!link_class(class_java_io_Serializable))
120 /* link classes for wrapping primitive types */
122 if (!link_class(class_java_lang_Void))
125 if (!link_class(class_java_lang_Boolean))
128 if (!link_class(class_java_lang_Byte))
131 if (!link_class(class_java_lang_Character))
134 if (!link_class(class_java_lang_Short))
137 if (!link_class(class_java_lang_Integer))
140 if (!link_class(class_java_lang_Long))
143 if (!link_class(class_java_lang_Float))
146 if (!link_class(class_java_lang_Double))
150 /* create pseudo classes used by the typechecker */
152 /* pseudo class for Arraystubs (extends java.lang.Object) */
154 pseudo_class_Arraystub->loaded = true;
155 pseudo_class_Arraystub->super = class_java_lang_Object;
156 pseudo_class_Arraystub->interfacescount = 2;
157 pseudo_class_Arraystub->interfaces = MNEW(classinfo*, 2);
158 pseudo_class_Arraystub->interfaces[0] = class_java_lang_Cloneable;
159 pseudo_class_Arraystub->interfaces[1] = class_java_io_Serializable;
161 if (!link_class(pseudo_class_Arraystub))
164 /* pseudo class representing the null type */
166 pseudo_class_Null->loaded = true;
167 pseudo_class_Null->super = class_java_lang_Object;
169 if (!link_class(pseudo_class_Null))
172 /* pseudo class representing new uninitialized objects */
174 pseudo_class_New->loaded = true;
175 pseudo_class_New->linked = true;
176 pseudo_class_New->super = class_java_lang_Object;
179 /* create classes representing primitive types */
181 if (!link_primitivetype_table())
185 /* Correct vftbl-entries (retarded loading and linking of class */
186 /* java/lang/String). */
188 stringtable_update();
194 /* link_primitivetype_table ****************************************************
196 Create classes representing primitive types.
198 *******************************************************************************/
200 static bool link_primitivetype_table(void)
205 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
206 /* create primitive class */
207 c = class_new_intern(utf_new_char(primitivetype_table[i].name));
208 c->classUsed = NOTUSED; /* not used initially CO-RT */
211 /* prevent loader from loading primitive class */
216 primitivetype_table[i].class_primitive = c;
218 /* create class for wrapping the primitive type */
219 c = class_new_intern(utf_new_char(primitivetype_table[i].wrapname));
220 primitivetype_table[i].class_wrap = c;
221 primitivetype_table[i].class_wrap->classUsed = NOTUSED; /* not used initially CO-RT */
222 primitivetype_table[i].class_wrap->impldBy = NULL;
224 /* create the primitive array class */
225 if (primitivetype_table[i].arrayname) {
226 c = class_new_intern(utf_new_char(primitivetype_table[i].arrayname));
227 primitivetype_table[i].arrayclass = c;
232 primitivetype_table[i].arrayvftbl = c->vftbl;
240 /* link_class ******************************************************************
242 Wrapper function for link_class_intern to ease monitor enter/exit
243 and exception handling.
245 *******************************************************************************/
247 classinfo *link_class(classinfo *c)
251 #if defined(USE_THREADS)
252 /* enter a monitor on the class */
254 builtin_monitorenter((java_objectheader *) c);
257 /* maybe the class is already linked */
259 #if defined(USE_THREADS)
260 builtin_monitorexit((java_objectheader *) c);
266 #if defined(STATISTICS)
269 if (getcompilingtime)
270 compilingtime_stop();
276 /* call the internal function */
277 r = link_class_intern(c);
279 /* if return value is NULL, we had a problem and the class is not linked */
283 #if defined(STATISTICS)
289 if (getcompilingtime)
290 compilingtime_start();
293 #if defined(USE_THREADS)
294 /* leave the monitor */
296 builtin_monitorexit((java_objectheader *) c);
303 /* link_class_intern ***********************************************************
305 Tries to link a class. The function calculates the length in bytes
306 that an instance of this class requires as well as the VTBL for
307 methods and interface methods.
309 *******************************************************************************/
311 static classinfo *link_class_intern(classinfo *c)
313 classinfo *super; /* super class */
314 classinfo *tc; /* temporary class variable */
315 s4 supervftbllength; /* vftbllegnth of super class */
316 s4 vftbllength; /* vftbllength of current class */
317 s4 interfacetablelength; /* interface table length */
318 vftbl_t *v; /* vftbl of current class */
319 s4 i; /* interface/method/field counter */
320 arraydescriptor *arraydesc; /* descriptor for array classes */
322 /* maybe the class is already linked */
326 /* the class must be loaded */
328 throw_cacao_exception_exit(string_java_lang_InternalError,
329 "Trying to link unloaded class");
332 log_message_class("Linking class: ", c);
334 /* ok, this class is somewhat linked */
339 /* check interfaces */
341 for (i = 0; i < c->interfacescount; i++) {
342 tc = c->interfaces[i];
344 /* detect circularity */
348 new_exception_utfmessage(string_java_lang_ClassCircularityError,
354 if (!load_class_from_classloader(tc, c->classloader))
357 if (!(tc->flags & ACC_INTERFACE)) {
359 new_exception_message(string_java_lang_IncompatibleClassChangeError,
360 "Implementing class");
369 /* check super class */
373 if (super == NULL) { /* class java.lang.Object */
375 c->classUsed = USED; /* Object class is always used CO-RT*/
377 c->instancesize = sizeof(java_objectheader);
379 vftbllength = supervftbllength = 0;
384 /* detect circularity */
387 new_exception_utfmessage(string_java_lang_ClassCircularityError,
393 if (!load_class_from_classloader(super, c->classloader))
396 if (super->flags & ACC_INTERFACE) {
397 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
398 panic("Interface specified as super class");
401 /* Don't allow extending final classes */
402 if (super->flags & ACC_FINAL) {
404 new_exception_message(string_java_lang_VerifyError,
405 "Cannot inherit from final class");
410 if (!link_class(super))
413 /* handle array classes */
414 if (c->name->text[0] == '[')
415 if (!(arraydesc = link_array(c)))
418 if (c->flags & ACC_INTERFACE)
419 c->index = interfaceindex++;
421 c->index = super->index + 1;
423 c->instancesize = super->instancesize;
425 vftbllength = supervftbllength = super->vftbl->vftbllength;
427 c->finalizer = super->finalizer;
431 /* compute vftbl length */
433 for (i = 0; i < c->methodscount; i++) {
434 methodinfo *m = &(c->methods[i]);
436 if (!(m->flags & ACC_STATIC)) { /* is instance method */
442 for (j = 0; j < tc->methodscount; j++) {
443 if (method_canoverwrite(m, &(tc->methods[j]))) {
444 if (tc->methods[j].flags & ACC_PRIVATE)
445 goto notfoundvftblindex;
447 if (tc->methods[j].flags & ACC_FINAL) {
448 /* class a overrides final method . */
450 new_exception(string_java_lang_VerifyError);
454 m->vftblindex = tc->methods[j].vftblindex;
455 goto foundvftblindex;
463 m->vftblindex = (vftbllength++);
470 /* check interfaces of ABSTRACT class for unimplemented methods */
472 if (c->flags & ACC_ABSTRACT) {
475 s4 abstractmethodscount;
479 abstractmethodscount = 0;
481 for (i = 0; i < c->interfacescount; i++) {
482 ic = c->interfaces[i];
484 for (j = 0; j < ic->methodscount; j++) {
485 im = &(ic->methods[j]);
487 /* skip `<clinit>' and `<init>' */
489 if (im->name == utf_clinit || im->name == utf_init)
495 for (k = 0; k < tc->methodscount; k++) {
496 if (method_canoverwrite(im, &(tc->methods[k])))
497 goto noabstractmethod;
503 abstractmethodscount++;
510 if (abstractmethodscount > 0) {
513 /* reallocate methods memory */
515 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
516 c->methodscount + abstractmethodscount);
518 for (i = 0; i < c->interfacescount; i++) {
519 ic = c->interfaces[i];
521 for (j = 0; j < ic->methodscount; j++) {
522 im = &(ic->methods[j]);
524 /* skip `<clinit>' and `<init>' */
526 if (im->name == utf_clinit || im->name == utf_init)
532 for (k = 0; k < tc->methodscount; k++) {
533 if (method_canoverwrite(im, &(tc->methods[k])))
534 goto noabstractmethod2;
540 am = &(c->methods[c->methodscount]);
543 MCOPY(am, im, methodinfo, 1);
545 am->vftblindex = (vftbllength++);
556 #if defined(STATISTICS)
559 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
562 /* compute interfacetable length */
564 interfacetablelength = 0;
567 for (i = 0; i < tc->interfacescount; i++) {
568 s4 h = class_highestinterface(tc->interfaces[i]) + 1;
569 if (h > interfacetablelength)
570 interfacetablelength = h;
575 /* allocate virtual function table */
577 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
578 sizeof(methodptr) * (vftbllength - 1) +
579 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
580 v = (vftbl_t *) (((methodptr *) v) +
581 (interfacetablelength - 1) * (interfacetablelength > 1));
582 c->header.vftbl = c->vftbl = v;
584 v->vftbllength = vftbllength;
585 v->interfacetablelength = interfacetablelength;
586 v->arraydesc = arraydesc;
588 /* store interface index in vftbl */
590 if (c->flags & ACC_INTERFACE)
591 v->baseval = -(c->index);
593 /* copy virtual function table of super class */
595 for (i = 0; i < supervftbllength; i++)
596 v->table[i] = super->vftbl->table[i];
598 /* add method stubs into virtual function table */
600 for (i = 0; i < c->methodscount; i++) {
601 methodinfo *m = &(c->methods[i]);
603 /* Methods in ABSTRACT classes from interfaces maybe already have a */
606 if (!m->stubroutine) {
607 if (!(m->flags & ACC_NATIVE)) {
608 m->stubroutine = createcompilerstub(m);
611 functionptr f = native_findfunction(c->name,
614 (m->flags & ACC_STATIC));
615 #if defined(STATIC_CLASSPATH)
618 m->stubroutine = createnativestub(f, m);
622 if (!(m->flags & ACC_STATIC))
623 v->table[m->vftblindex] = m->stubroutine;
626 /* compute instance size and offset of each field */
628 for (i = 0; i < c->fieldscount; i++) {
630 fieldinfo *f = &(c->fields[i]);
632 if (!(f->flags & ACC_STATIC)) {
633 dsize = desc_typesize(f->descriptor);
634 c->instancesize = ALIGN(c->instancesize, dsize);
635 f->offset = c->instancesize;
636 c->instancesize += dsize;
640 /* initialize interfacetable and interfacevftbllength */
642 v->interfacevftbllength = MNEW(s4, interfacetablelength);
644 #if defined(STATISTICS)
646 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
649 for (i = 0; i < interfacetablelength; i++) {
650 v->interfacevftbllength[i] = 0;
651 v->interfacetable[-i] = NULL;
656 for (tc = c; tc != NULL; tc = tc->super)
657 for (i = 0; i < tc->interfacescount; i++)
658 linker_addinterface(c, tc->interfaces[i]);
660 /* add finalizer method (not for java.lang.Object) */
665 fi = class_findmethod(c, utf_finalize, utf_void__void);
668 if (!(fi->flags & ACC_STATIC))
674 linker_compute_subclasses(c);
677 log_message_class("Linking done class: ", c);
679 /* just return c to show that we didn't had a problem */
685 /* link_array ******************************************************************
687 This function is called by link_class to create the arraydescriptor
690 This function returns NULL if the array cannot be linked because
691 the component type has not been linked yet.
693 *******************************************************************************/
695 static arraydescriptor *link_array(classinfo *c)
697 classinfo *comp = NULL;
698 s4 namelen = c->name->blength;
699 arraydescriptor *desc;
702 /* Check the component type */
703 switch (c->name->text[1]) {
705 /* c is an array of arrays. */
706 comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
708 panic("Could not find component array class.");
712 /* c is an array of objects. */
713 comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
715 panic("Could not find component class.");
719 /* If the component type has not been linked, link it now */
720 if (comp && !comp->linked) {
722 if (!load_class_from_classloader(comp, c->classloader))
725 if (!link_class(comp))
729 /* Allocate the arraydescriptor */
730 desc = NEW(arraydescriptor);
733 /* c is an array of references */
734 desc->arraytype = ARRAYTYPE_OBJECT;
735 desc->componentsize = sizeof(void*);
736 desc->dataoffset = OFFSET(java_objectarray, data);
738 compvftbl = comp->vftbl;
740 panic("Component class has no vftbl");
741 desc->componentvftbl = compvftbl;
743 if (compvftbl->arraydesc) {
744 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
745 if (compvftbl->arraydesc->dimension >= 255)
746 panic("Creating array of dimension >255");
747 desc->dimension = compvftbl->arraydesc->dimension + 1;
748 desc->elementtype = compvftbl->arraydesc->elementtype;
751 desc->elementvftbl = compvftbl;
753 desc->elementtype = ARRAYTYPE_OBJECT;
757 /* c is an array of a primitive type */
758 switch (c->name->text[1]) {
760 desc->arraytype = ARRAYTYPE_BOOLEAN;
761 desc->dataoffset = OFFSET(java_booleanarray,data);
762 desc->componentsize = sizeof(u1);
766 desc->arraytype = ARRAYTYPE_BYTE;
767 desc->dataoffset = OFFSET(java_bytearray,data);
768 desc->componentsize = sizeof(u1);
772 desc->arraytype = ARRAYTYPE_CHAR;
773 desc->dataoffset = OFFSET(java_chararray,data);
774 desc->componentsize = sizeof(u2);
778 desc->arraytype = ARRAYTYPE_DOUBLE;
779 desc->dataoffset = OFFSET(java_doublearray,data);
780 desc->componentsize = sizeof(double);
784 desc->arraytype = ARRAYTYPE_FLOAT;
785 desc->dataoffset = OFFSET(java_floatarray,data);
786 desc->componentsize = sizeof(float);
790 desc->arraytype = ARRAYTYPE_INT;
791 desc->dataoffset = OFFSET(java_intarray,data);
792 desc->componentsize = sizeof(s4);
796 desc->arraytype = ARRAYTYPE_LONG;
797 desc->dataoffset = OFFSET(java_longarray,data);
798 desc->componentsize = sizeof(s8);
802 desc->arraytype = ARRAYTYPE_SHORT;
803 desc->dataoffset = OFFSET(java_shortarray,data);
804 desc->componentsize = sizeof(s2);
808 panic("Invalid array class name");
811 desc->componentvftbl = NULL;
812 desc->elementvftbl = NULL;
814 desc->elementtype = desc->arraytype;
821 /* linker_compute_subclasses ***************************************************
825 *******************************************************************************/
827 static void linker_compute_subclasses(classinfo *c)
829 #if defined(USE_THREADS)
830 #if defined(NATIVE_THREADS)
837 if (!(c->flags & ACC_INTERFACE)) {
842 if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
843 c->nextsub = c->super->sub;
849 /* this is the java.lang.Object special case */
851 if (!class_java_lang_Object) {
852 linker_compute_class_values(c);
855 linker_compute_class_values(class_java_lang_Object);
858 #if defined(USE_THREADS)
859 #if defined(NATIVE_THREADS)
868 /* linker_compute_class_values *************************************************
872 *******************************************************************************/
874 static void linker_compute_class_values(classinfo *c)
878 c->vftbl->baseval = ++classvalue;
883 linker_compute_class_values(subs);
885 subs = subs->nextsub;
888 c->vftbl->diffval = classvalue - c->vftbl->baseval;
892 /* linker_addinterface *********************************************************
894 Is needed by link_class for adding a VTBL to a class. All
895 interfaces implemented by ic are added as well.
897 *******************************************************************************/
899 static void linker_addinterface(classinfo *c, classinfo *ic)
903 vftbl_t *v = c->vftbl;
905 if (i >= v->interfacetablelength)
906 panic ("Inernal error: interfacetable overflow");
908 if (v->interfacetable[-i])
911 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
912 v->interfacevftbllength[i] = 1;
913 v->interfacetable[-i] = MNEW(methodptr, 1);
914 v->interfacetable[-i][0] = NULL;
917 v->interfacevftbllength[i] = ic->methodscount;
918 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
920 #if defined(STATISTICS)
922 count_vftbl_len += sizeof(methodptr) *
923 (ic->methodscount + (ic->methodscount == 0));
926 for (j = 0; j < ic->methodscount; j++) {
930 for (m = 0; m < sc->methodscount; m++) {
931 methodinfo *mi = &(sc->methods[m]);
933 if (method_canoverwrite(mi, &(ic->methods[j]))) {
934 v->interfacetable[-i][j] = v->table[mi->vftblindex];
945 for (j = 0; j < ic->interfacescount; j++)
946 linker_addinterface(c, ic->interfaces[j]);
950 /* class_highestinterface ******************************************************
952 Used by the function link_class to determine the amount of memory
953 needed for the interface table.
955 *******************************************************************************/
957 static s4 class_highestinterface(classinfo *c)
963 /* check for ACC_INTERFACE bit already done in link_class_intern */
967 for (i = 0; i < c->interfacescount; i++) {
968 h2 = class_highestinterface(c->interfaces[i]);
979 * These are local overrides for various environment variables in Emacs.
980 * Please do not remove this and leave it at the end of the file, where
981 * Emacs will automagically detect them.
982 * ---------------------------------------------------------------------
985 * indent-tabs-mode: t