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 2101 2005-03-28 21:59:45Z twisti $
40 #include "mm/memory.h"
41 #include "native/native.h"
42 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/options.h"
46 #include "vm/statistics.h"
47 #include "vm/jit/codegen.inc.h"
50 /* global variables ***********************************************************/
52 static s4 interfaceindex; /* sequential numbering of interfaces */
56 /* private functions **********************************************************/
58 static classinfo *link_class_intern(classinfo *c);
59 static arraydescriptor *link_array(classinfo *c);
60 static void linker_compute_class_values(classinfo *c);
61 static void linker_compute_subclasses(classinfo *c);
62 static void linker_addinterface(classinfo *c, classinfo *ic);
63 static s4 class_highestinterface(classinfo *c);
66 /* linker_init *****************************************************************
68 Initializes the linker subsystem.
70 *******************************************************************************/
72 void linker_init(void)
74 /* reset interface index */
80 /* link_class ******************************************************************
82 Wrapper function for link_class_intern to ease monitor enter/exit
83 and exception handling.
85 *******************************************************************************/
87 classinfo *class_link(classinfo *c)
91 #if defined(USE_THREADS)
92 /* enter a monitor on the class */
94 builtin_monitorenter((java_objectheader *) c);
97 /* maybe the class is already linked */
99 #if defined(USE_THREADS)
100 builtin_monitorexit((java_objectheader *) c);
106 #if defined(STATISTICS)
109 if (getcompilingtime)
110 compilingtime_stop();
116 /* call the internal function */
117 r = link_class_intern(c);
119 /* if return value is NULL, we had a problem and the class is not linked */
123 #if defined(STATISTICS)
129 if (getcompilingtime)
130 compilingtime_start();
133 #if defined(USE_THREADS)
134 /* leave the monitor */
136 builtin_monitorexit((java_objectheader *) c);
143 /* link_class_intern ***********************************************************
145 Tries to link a class. The function calculates the length in bytes
146 that an instance of this class requires as well as the VTBL for
147 methods and interface methods.
149 *******************************************************************************/
151 static classinfo *link_class_intern(classinfo *c)
153 classinfo *super; /* super class */
154 classinfo *tc; /* temporary class variable */
155 s4 supervftbllength; /* vftbllegnth of super class */
156 s4 vftbllength; /* vftbllength of current class */
157 s4 interfacetablelength; /* interface table length */
158 vftbl_t *v; /* vftbl of current class */
159 s4 i; /* interface/method/field counter */
160 arraydescriptor *arraydesc; /* descriptor for array classes */
162 /* maybe the class is already linked */
166 /* maybe the class is not loaded */
168 /* if (!class_load(c)) */
170 panic("class_link_intern: class not loaded");
173 log_message_class("Linking class: ", c);
175 /* ok, this class is somewhat linked */
180 /* check interfaces */
182 for (i = 0; i < c->interfacescount; i++) {
183 tc = c->interfaces[i];
185 /* detect circularity */
189 new_exception_utfmessage(string_java_lang_ClassCircularityError,
195 if (!class_load_extern(c, tc))
198 if (!(tc->flags & ACC_INTERFACE)) {
200 new_exception_message(string_java_lang_IncompatibleClassChangeError,
201 "Implementing class");
210 /* check super class */
214 if (super == NULL) { /* class java.lang.Object */
216 c->classUsed = USED; /* Object class is always used CO-RT*/
218 c->instancesize = sizeof(java_objectheader);
220 vftbllength = supervftbllength = 0;
225 /* detect circularity */
228 new_exception_utfmessage(string_java_lang_ClassCircularityError,
234 if (!class_load_extern(c, super))
237 if (super->flags & ACC_INTERFACE) {
238 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
239 panic("Interface specified as super class");
242 /* Don't allow extending final classes */
243 if (super->flags & ACC_FINAL) {
245 new_exception_message(string_java_lang_VerifyError,
246 "Cannot inherit from final class");
251 if (!class_link(super))
254 /* handle array classes */
255 if (c->name->text[0] == '[')
256 if (!(arraydesc = link_array(c)))
259 if (c->flags & ACC_INTERFACE)
260 c->index = interfaceindex++;
262 c->index = super->index + 1;
264 c->instancesize = super->instancesize;
266 vftbllength = supervftbllength = super->vftbl->vftbllength;
268 c->finalizer = super->finalizer;
272 /* compute vftbl length */
274 for (i = 0; i < c->methodscount; i++) {
275 methodinfo *m = &(c->methods[i]);
277 if (!(m->flags & ACC_STATIC)) { /* is instance method */
283 for (j = 0; j < tc->methodscount; j++) {
284 if (method_canoverwrite(m, &(tc->methods[j]))) {
285 if (tc->methods[j].flags & ACC_PRIVATE)
286 goto notfoundvftblindex;
288 if (tc->methods[j].flags & ACC_FINAL) {
289 /* class a overrides final method . */
291 new_exception(string_java_lang_VerifyError);
295 m->vftblindex = tc->methods[j].vftblindex;
296 goto foundvftblindex;
304 m->vftblindex = (vftbllength++);
311 /* check interfaces for unimplemented methods in ABSTRACT class */
313 if (c->flags & ACC_ABSTRACT) {
316 s4 abstractmethodscount;
320 abstractmethodscount = 0;
322 for (i = 0; i < c->interfacescount; i++) {
323 ic = c->interfaces[i];
325 for (j = 0; j < ic->methodscount; j++) {
326 im = &(ic->methods[j]);
331 for (k = 0; k < tc->methodscount; k++) {
332 if (method_canoverwrite(im, &(tc->methods[k])))
333 goto noabstractmethod;
339 abstractmethodscount++;
346 if (abstractmethodscount > 0) {
349 /* reallocate methods memory */
351 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
352 c->methodscount + abstractmethodscount);
354 for (i = 0; i < c->interfacescount; i++) {
355 ic = c->interfaces[i];
357 for (j = 0; j < ic->methodscount; j++) {
358 im = &(ic->methods[j]);
363 for (k = 0; k < tc->methodscount; k++) {
364 if (method_canoverwrite(im, &(tc->methods[k])))
365 goto noabstractmethod2;
371 am = &(c->methods[c->methodscount]);
374 MCOPY(am, im, methodinfo, 1);
376 am->vftblindex = (vftbllength++);
387 #if defined(STATISTICS)
390 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
393 /* compute interfacetable length */
395 interfacetablelength = 0;
398 for (i = 0; i < tc->interfacescount; i++) {
399 s4 h = class_highestinterface(tc->interfaces[i]) + 1;
400 if (h > interfacetablelength)
401 interfacetablelength = h;
406 /* allocate virtual function table */
408 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
409 sizeof(methodptr) * (vftbllength - 1) +
410 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
411 v = (vftbl_t *) (((methodptr *) v) +
412 (interfacetablelength - 1) * (interfacetablelength > 1));
413 c->header.vftbl = c->vftbl = v;
415 v->vftbllength = vftbllength;
416 v->interfacetablelength = interfacetablelength;
417 v->arraydesc = arraydesc;
419 /* store interface index in vftbl */
421 if (c->flags & ACC_INTERFACE)
422 v->baseval = -(c->index);
424 /* copy virtual function table of super class */
426 for (i = 0; i < supervftbllength; i++)
427 v->table[i] = super->vftbl->table[i];
429 /* add method stubs into virtual function table */
431 for (i = 0; i < c->methodscount; i++) {
432 methodinfo *m = &(c->methods[i]);
435 if (!m->stubroutine) {
436 if (!(m->flags & ACC_NATIVE)) {
437 m->stubroutine = createcompilerstub(m);
440 functionptr f = native_findfunction(c->name,
441 m->name, m->descriptor,
442 (m->flags & ACC_STATIC));
443 #if defined(STATIC_CLASSPATH)
446 m->stubroutine = createnativestub(f, m);
451 if (!(m->flags & ACC_STATIC))
452 v->table[m->vftblindex] = m->stubroutine;
455 /* compute instance size and offset of each field */
457 for (i = 0; i < c->fieldscount; i++) {
459 fieldinfo *f = &(c->fields[i]);
461 if (!(f->flags & ACC_STATIC)) {
462 dsize = desc_typesize(f->descriptor);
463 c->instancesize = ALIGN(c->instancesize, dsize);
464 f->offset = c->instancesize;
465 c->instancesize += dsize;
469 /* initialize interfacetable and interfacevftbllength */
471 v->interfacevftbllength = MNEW(s4, interfacetablelength);
473 #if defined(STATISTICS)
475 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
478 for (i = 0; i < interfacetablelength; i++) {
479 v->interfacevftbllength[i] = 0;
480 v->interfacetable[-i] = NULL;
485 for (tc = c; tc != NULL; tc = tc->super)
486 for (i = 0; i < tc->interfacescount; i++)
487 linker_addinterface(c, tc->interfaces[i]);
489 /* add finalizer method (not for java.lang.Object) */
494 fi = class_findmethod(c, utf_finalize, utf_void__void);
497 if (!(fi->flags & ACC_STATIC))
503 linker_compute_subclasses(c);
506 log_message_class("Linking done class: ", c);
508 /* just return c to show that we didn't had a problem */
514 /* link_array ******************************************************************
516 This function is called by link_class to create the arraydescriptor
519 This function returns NULL if the array cannot be linked because
520 the component type has not been linked yet.
522 *******************************************************************************/
524 static arraydescriptor *link_array(classinfo *c)
526 classinfo *comp = NULL;
527 s4 namelen = c->name->blength;
528 arraydescriptor *desc;
531 /* Check the component type */
532 switch (c->name->text[1]) {
534 /* c is an array of arrays. */
535 comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
537 panic("Could not find component array class.");
541 /* c is an array of objects. */
542 comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
544 panic("Could not find component class.");
548 /* If the component type has not been linked, link it now */
549 if (comp && !comp->linked) {
551 if (!class_load_extern(c, comp))
554 if (!class_link(comp))
558 /* Allocate the arraydescriptor */
559 desc = NEW(arraydescriptor);
562 /* c is an array of references */
563 desc->arraytype = ARRAYTYPE_OBJECT;
564 desc->componentsize = sizeof(void*);
565 desc->dataoffset = OFFSET(java_objectarray, data);
567 compvftbl = comp->vftbl;
569 panic("Component class has no vftbl");
570 desc->componentvftbl = compvftbl;
572 if (compvftbl->arraydesc) {
573 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
574 if (compvftbl->arraydesc->dimension >= 255)
575 panic("Creating array of dimension >255");
576 desc->dimension = compvftbl->arraydesc->dimension + 1;
577 desc->elementtype = compvftbl->arraydesc->elementtype;
580 desc->elementvftbl = compvftbl;
582 desc->elementtype = ARRAYTYPE_OBJECT;
586 /* c is an array of a primitive type */
587 switch (c->name->text[1]) {
589 desc->arraytype = ARRAYTYPE_BOOLEAN;
590 desc->dataoffset = OFFSET(java_booleanarray,data);
591 desc->componentsize = sizeof(u1);
595 desc->arraytype = ARRAYTYPE_BYTE;
596 desc->dataoffset = OFFSET(java_bytearray,data);
597 desc->componentsize = sizeof(u1);
601 desc->arraytype = ARRAYTYPE_CHAR;
602 desc->dataoffset = OFFSET(java_chararray,data);
603 desc->componentsize = sizeof(u2);
607 desc->arraytype = ARRAYTYPE_DOUBLE;
608 desc->dataoffset = OFFSET(java_doublearray,data);
609 desc->componentsize = sizeof(double);
613 desc->arraytype = ARRAYTYPE_FLOAT;
614 desc->dataoffset = OFFSET(java_floatarray,data);
615 desc->componentsize = sizeof(float);
619 desc->arraytype = ARRAYTYPE_INT;
620 desc->dataoffset = OFFSET(java_intarray,data);
621 desc->componentsize = sizeof(s4);
625 desc->arraytype = ARRAYTYPE_LONG;
626 desc->dataoffset = OFFSET(java_longarray,data);
627 desc->componentsize = sizeof(s8);
631 desc->arraytype = ARRAYTYPE_SHORT;
632 desc->dataoffset = OFFSET(java_shortarray,data);
633 desc->componentsize = sizeof(s2);
637 panic("Invalid array class name");
640 desc->componentvftbl = NULL;
641 desc->elementvftbl = NULL;
643 desc->elementtype = desc->arraytype;
650 /* linker_compute_subclasses ***************************************************
654 *******************************************************************************/
656 static void linker_compute_subclasses(classinfo *c)
658 #if defined(USE_THREADS)
659 #if defined(NATIVE_THREADS)
666 if (!(c->flags & ACC_INTERFACE)) {
671 if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
672 c->nextsub = c->super->sub;
678 /* this is the java.lang.Object special case */
680 if (!class_java_lang_Object) {
681 linker_compute_class_values(c);
684 linker_compute_class_values(class_java_lang_Object);
687 #if defined(USE_THREADS)
688 #if defined(NATIVE_THREADS)
697 /* linker_compute_class_values *************************************************
701 *******************************************************************************/
703 static void linker_compute_class_values(classinfo *c)
707 c->vftbl->baseval = ++classvalue;
712 linker_compute_class_values(subs);
714 subs = subs->nextsub;
717 c->vftbl->diffval = classvalue - c->vftbl->baseval;
721 /* linker_addinterface *********************************************************
723 Is needed by link_class for adding a VTBL to a class. All
724 interfaces implemented by ic are added as well.
726 *******************************************************************************/
728 static void linker_addinterface(classinfo *c, classinfo *ic)
732 vftbl_t *v = c->vftbl;
734 if (i >= v->interfacetablelength)
735 panic ("Inernal error: interfacetable overflow");
737 if (v->interfacetable[-i])
740 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
741 v->interfacevftbllength[i] = 1;
742 v->interfacetable[-i] = MNEW(methodptr, 1);
743 v->interfacetable[-i][0] = NULL;
746 v->interfacevftbllength[i] = ic->methodscount;
747 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
749 #if defined(STATISTICS)
751 count_vftbl_len += sizeof(methodptr) *
752 (ic->methodscount + (ic->methodscount == 0));
755 for (j = 0; j < ic->methodscount; j++) {
759 for (m = 0; m < sc->methodscount; m++) {
760 methodinfo *mi = &(sc->methods[m]);
762 if (method_canoverwrite(mi, &(ic->methods[j]))) {
763 v->interfacetable[-i][j] = v->table[mi->vftblindex];
774 for (j = 0; j < ic->interfacescount; j++)
775 linker_addinterface(c, ic->interfaces[j]);
779 /* class_highestinterface ******************************************************
781 Used by the function link_class to determine the amount of memory
782 needed for the interface table.
784 *******************************************************************************/
786 static s4 class_highestinterface(classinfo *c)
792 /* check for ACC_INTERFACE bit already done in class_link_intern */
796 for (i = 0; i < c->interfacescount; i++) {
797 h2 = class_highestinterface(c->interfaces[i]);
808 * These are local overrides for various environment variables in Emacs.
809 * Please do not remove this and leave it at the end of the file, where
810 * Emacs will automagically detect them.
811 * ---------------------------------------------------------------------
814 * indent-tabs-mode: t