1 /* src/vmcore/linker.c - class linker functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: linker.c 7813 2007-04-25 19:20:13Z twisti $
36 #include "mm/memory.h"
38 #include "native/native.h"
40 #include "threads/lock-common.h"
42 #include "toolbox/logging.h"
44 #include "vm/access.h"
45 #include "vm/exceptions.h"
46 #include "vm/stringlocal.h"
49 #include "vm/jit_interface.h"
51 #include "vmcore/class.h"
52 #include "vmcore/classcache.h"
53 #include "vmcore/loader.h"
54 #include "vmcore/options.h"
55 #include "vmcore/rt-timing.h"
57 /* #include "vm/resolve.h" */
58 /* copied prototype to avoid bootstrapping problem: */
59 classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool checkaccess);
61 #if defined(ENABLE_STATISTICS)
62 # include "vmcore/statistics.h"
65 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
66 #define INLINELOG(code) do { if (opt_inline_debug_log) { code } } while (0)
68 #define INLINELOG(code)
72 /* global variables ***********************************************************/
74 static s4 interfaceindex; /* sequential numbering of interfaces */
77 java_objectheader *linker_classrenumber_lock;
80 /* primitivetype_table *********************************************************
82 Structure for primitive classes: contains the class for wrapping
83 the primitive type, the primitive class, the name of the class for
84 wrapping, the one character type signature and the name of the
87 CAUTION: Don't change the order of the types. This table is indexed
88 by the ARRAYTYPE_ constants (except ARRAYTYPE_OBJECT).
90 *******************************************************************************/
92 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
93 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
94 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
95 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
96 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
97 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
98 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
99 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
100 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
101 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
102 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
103 #if defined(ENABLE_JAVASE)
104 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }
106 { NULL, NULL, NULL, 0 , NULL , NULL, NULL, NULL },
111 /* private functions **********************************************************/
113 static bool link_primitivetype_table(void);
114 static classinfo *link_class_intern(classinfo *c);
115 static arraydescriptor *link_array(classinfo *c);
116 static void linker_compute_class_values(classinfo *c);
117 static void linker_compute_subclasses(classinfo *c);
118 static bool linker_addinterface(classinfo *c, classinfo *ic);
119 static s4 class_highestinterface(classinfo *c);
122 /* linker_init *****************************************************************
124 Initializes the linker subsystem.
126 *******************************************************************************/
128 bool linker_init(void)
130 /* reset interface index */
134 #if defined(ENABLE_THREADS)
135 /* create the global lock object */
137 linker_classrenumber_lock = NEW(java_objectheader);
139 LOCK_INIT_OBJECT_LOCK(linker_classrenumber_lock);
142 /* link java.lang.Class as first class of the system, because we
143 need it's vftbl for all other classes so we can use a class as
146 if (!link_class(class_java_lang_Class))
149 /* now set the header.vftbl of all classes which were created
150 before java.lang.Class was linked */
152 class_postset_header_vftbl();
155 /* link important system classes */
157 if (!link_class(class_java_lang_Object))
160 if (!link_class(class_java_lang_String))
163 #if defined(ENABLE_JAVASE)
164 if (!link_class(class_java_lang_Cloneable))
167 if (!link_class(class_java_io_Serializable))
172 /* link classes for wrapping primitive types */
174 #if defined(ENABLE_JAVASE)
175 if (!link_class(class_java_lang_Void))
179 if (!link_class(class_java_lang_Boolean))
182 if (!link_class(class_java_lang_Byte))
185 if (!link_class(class_java_lang_Character))
188 if (!link_class(class_java_lang_Short))
191 if (!link_class(class_java_lang_Integer))
194 if (!link_class(class_java_lang_Long))
197 if (!link_class(class_java_lang_Float))
200 if (!link_class(class_java_lang_Double))
204 /* load some other important classes */
206 #if defined(ENABLE_JAVASE)
207 if (!link_class(class_java_lang_ClassLoader))
210 if (!link_class(class_java_lang_SecurityManager))
214 if (!link_class(class_java_lang_System))
217 if (!link_class(class_java_lang_Thread))
220 #if defined(ENABLE_JAVASE)
221 if (!link_class(class_java_lang_ThreadGroup))
225 #if defined(WITH_CLASSPATH_GNU)
226 if (!link_class(class_java_lang_VMSystem))
229 if (!link_class(class_java_lang_VMThread))
234 /* some classes which may be used more often */
236 #if defined(ENABLE_JAVASE)
237 if (!link_class(class_java_lang_StackTraceElement))
240 if (!link_class(class_java_lang_reflect_Constructor))
243 if (!link_class(class_java_lang_reflect_Field))
246 if (!link_class(class_java_lang_reflect_Method))
249 if (!link_class(class_java_security_PrivilegedAction))
252 if (!link_class(class_java_util_Vector))
255 if (!link_class(arrayclass_java_lang_Object))
260 /* create pseudo classes used by the typechecker */
262 /* pseudo class for Arraystubs (extends java.lang.Object) */
264 pseudo_class_Arraystub =
265 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
266 pseudo_class_Arraystub->state |= CLASS_LOADED;
267 pseudo_class_Arraystub->super.cls = class_java_lang_Object;
269 #if defined(ENABLE_JAVASE)
270 pseudo_class_Arraystub->interfacescount = 2;
271 pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
272 pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
273 pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
274 #elif defined(ENABLE_JAVAME_CLDC1_1)
275 pseudo_class_Arraystub->interfacescount = 0;
276 pseudo_class_Arraystub->interfaces = NULL;
279 if (!classcache_store_unique(pseudo_class_Arraystub)) {
280 log_text("could not cache pseudo_class_Arraystub");
284 if (!link_class(pseudo_class_Arraystub))
287 /* pseudo class representing the null type */
289 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
290 pseudo_class_Null->state |= CLASS_LOADED;
291 pseudo_class_Null->super.cls = class_java_lang_Object;
293 if (!classcache_store_unique(pseudo_class_Null))
294 vm_abort("linker_init: could not cache pseudo_class_Null");
296 if (!link_class(pseudo_class_Null))
299 /* pseudo class representing new uninitialized objects */
301 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
302 pseudo_class_New->state |= CLASS_LOADED;
303 pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
304 pseudo_class_New->super.cls = class_java_lang_Object;
306 if (!classcache_store_unique(pseudo_class_New))
307 vm_abort("linker_init: could not cache pseudo_class_New");
309 /* create classes representing primitive types */
311 if (!link_primitivetype_table())
315 /* Correct vftbl-entries (retarded loading and linking of class
316 java/lang/String). */
318 stringtable_update();
324 /* link_primitivetype_table ****************************************************
326 Create classes representing primitive types.
328 *******************************************************************************/
330 static bool link_primitivetype_table(void)
336 for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
339 if (!primitivetype_table[i].name)
342 /* create primitive class */
344 c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
346 /* primitive classes don't have a super class */
350 /* set flags and mark it as primitive class */
352 c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT | ACC_CLASS_PRIMITIVE;
354 /* prevent loader from loading primitive class */
356 c->state |= CLASS_LOADED;
358 /* INFO: don't put primitive classes into the classcache */
363 primitivetype_table[i].class_primitive = c;
365 /* create class for wrapping the primitive type */
367 u = utf_new_char(primitivetype_table[i].wrapname);
368 c = load_class_bootstrap(u);
373 primitivetype_table[i].class_wrap = c;
375 /* create the primitive array class */
377 if (primitivetype_table[i].arrayname) {
378 u = utf_new_char(primitivetype_table[i].arrayname);
379 c = class_create_classinfo(u);
380 c = load_newly_created_array(c, NULL);
385 primitivetype_table[i].arrayclass = c;
387 assert(c->state & CLASS_LOADED);
389 if (!(c->state & CLASS_LINKED))
393 primitivetype_table[i].arrayvftbl = c->vftbl;
401 /* link_class ******************************************************************
403 Wrapper function for link_class_intern to ease monitor enter/exit
404 and exception handling.
406 *******************************************************************************/
408 classinfo *link_class(classinfo *c)
411 #if defined(ENABLE_RT_TIMING)
412 struct timespec time_start, time_end;
415 RT_TIMING_GET_TIME(time_start);
418 exceptions_throw_nullpointerexception();
422 LOCK_MONITOR_ENTER(c);
424 /* maybe the class is already linked */
426 if (c->state & CLASS_LINKED) {
427 LOCK_MONITOR_EXIT(c);
432 #if defined(ENABLE_STATISTICS)
435 if (opt_getcompilingtime)
436 compilingtime_stop();
438 if (opt_getloadingtime)
442 /* call the internal function */
444 r = link_class_intern(c);
446 /* if return value is NULL, we had a problem and the class is not linked */
449 c->state &= ~CLASS_LINKING;
451 #if defined(ENABLE_STATISTICS)
454 if (opt_getloadingtime)
457 if (opt_getcompilingtime)
458 compilingtime_start();
461 LOCK_MONITOR_EXIT(c);
463 RT_TIMING_GET_TIME(time_end);
465 RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
471 /* linker_overwrite_method *****************************************************
473 Overwrite a method with another one, update method flags and check
477 mg................the general method being overwritten
478 ms................the overwriting (more specialized) method
479 wl................worklist where to add invalidated methods
482 true..............everything ok
483 false.............an exception has been thrown
485 *******************************************************************************/
487 static bool linker_overwrite_method(methodinfo *mg,
489 method_worklist **wl)
497 /* overriding a final method is illegal */
499 if (mg->flags & ACC_FINAL) {
500 exceptions_throw_verifyerror(mg, "Overriding final method");
504 /* method ms overwrites method mg */
506 #if defined(ENABLE_VERIFIER)
507 /* Add loading constraints (for the more general types of method mg). */
508 /* Not for <init>, as it is not invoked virtually. */
510 if ((ms->name != utf_init)
511 && !classcache_add_constraints_for_params(
512 cs->classloader, cg->classloader, mg))
518 /* inherit the vftbl index, and record the overwriting */
520 ms->vftblindex = mg->vftblindex;
523 /* update flags and check assumptions */
524 /* <init> methods are a special case, as they are never dispatched dynamically */
526 if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
528 if (mg->flags & ACC_METHOD_IMPLEMENTED) {
529 /* this adds another implementation */
531 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
533 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
535 method_break_assumption_monomorphic(mg, wl);
538 /* this is the first implementation */
540 mg->flags |= ACC_METHOD_IMPLEMENTED;
542 INLINELOG( printf("becomes implemented: "); method_println(mg); );
547 } while (mg != NULL);
554 /* link_class_intern ***********************************************************
556 Tries to link a class. The function calculates the length in bytes
557 that an instance of this class requires as well as the VTBL for
558 methods and interface methods.
560 *******************************************************************************/
562 static classinfo *link_class_intern(classinfo *c)
564 classinfo *super; /* super class */
565 classinfo *tc; /* temporary class variable */
566 s4 supervftbllength; /* vftbllegnth of super class */
567 s4 vftbllength; /* vftbllength of current class */
568 s4 interfacetablelength; /* interface table length */
569 vftbl_t *v; /* vftbl of current class */
570 s4 i; /* interface/method/field counter */
571 arraydescriptor *arraydesc; /* descriptor for array classes */
572 method_worklist *worklist; /* worklist for recompilation */
573 #if defined(ENABLE_RT_TIMING)
574 struct timespec time_start, time_resolving, time_compute_vftbl,
575 time_abstract, time_compute_iftbl, time_fill_vftbl,
576 time_offsets, time_fill_iftbl, time_finalizer,
580 RT_TIMING_GET_TIME(time_start);
582 /* the class is already linked */
584 if (c->state & CLASS_LINKED)
589 log_message_class("Linking class: ", c);
592 /* the class must be loaded */
594 /* XXX should this be a specific exception? */
595 assert(c->state & CLASS_LOADED);
597 /* cache the self-reference of this class */
598 /* we do this for cases where the defining loader of the class */
599 /* has not yet been recorded as an initiating loader for the class */
600 /* this is needed so subsequent code can assume that self-refs */
601 /* will always resolve lazily */
602 /* No need to do it for the bootloader - it is always registered */
603 /* as initiating loader for the classes it loads. */
605 classcache_store(c->classloader,c,false);
607 /* this class is currently linking */
609 c->state |= CLASS_LINKING;
614 /* check interfaces */
616 for (i = 0; i < c->interfacescount; i++) {
617 /* resolve this super interface */
619 if ((tc = resolve_classref_or_classinfo_eager(c->interfaces[i], true)) == NULL)
622 c->interfaces[i].cls = tc;
624 /* detect circularity */
627 exceptions_throw_classcircularityerror(c);
631 assert(tc->state & CLASS_LOADED);
633 if (!(tc->flags & ACC_INTERFACE)) {
634 exceptions_throw_incompatibleclasschangeerror(tc,
635 "Implementing class");
639 if (!(tc->state & CLASS_LINKED))
644 /* check super class */
648 if (c->super.any == NULL) { /* class java.lang.Object */
650 c->instancesize = sizeof(java_objectheader);
652 vftbllength = supervftbllength = 0;
657 /* resolve super class */
659 if ((super = resolve_classref_or_classinfo_eager(c->super, true)) == NULL)
662 c->super.cls = super;
664 /* detect circularity */
667 exceptions_throw_classcircularityerror(c);
671 assert(super->state & CLASS_LOADED);
673 if (super->flags & ACC_INTERFACE) {
674 /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
675 log_text("Interface specified as super class");
679 /* Don't allow extending final classes */
681 if (super->flags & ACC_FINAL) {
682 exceptions_throw_verifyerror(NULL,
683 "Cannot inherit from final class");
687 /* link the superclass if necessary */
689 if (!(super->state & CLASS_LINKED))
690 if (!link_class(super))
693 /* OR the ACC_CLASS_HAS_POINTERS flag */
695 c->flags |= (super->flags & ACC_CLASS_HAS_POINTERS);
697 /* handle array classes */
699 if (c->name->text[0] == '[')
700 if (!(arraydesc = link_array(c)))
703 if (c->flags & ACC_INTERFACE)
704 c->index = interfaceindex++;
706 c->index = super->index + 1;
708 c->instancesize = super->instancesize;
710 vftbllength = supervftbllength = super->vftbl->vftbllength;
712 c->finalizer = super->finalizer;
714 RT_TIMING_GET_TIME(time_resolving);
717 /* compute vftbl length */
719 for (i = 0; i < c->methodscount; i++) {
720 methodinfo *m = &(c->methods[i]);
722 if (!(m->flags & ACC_STATIC)) { /* is instance method */
728 for (j = 0; j < tc->methodscount; j++) {
729 if (method_canoverwrite(m, &(tc->methods[j]))) {
730 if (tc->methods[j].flags & ACC_PRIVATE)
731 goto notfoundvftblindex;
733 /* package-private methods in other packages */
734 /* must not be overridden */
735 /* (see Java Language Specification 8.4.8.1) */
736 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
737 && !SAME_PACKAGE(c,tc) )
739 goto notfoundvftblindex;
742 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
745 goto foundvftblindex;
753 m->vftblindex = (vftbllength++);
758 RT_TIMING_GET_TIME(time_compute_vftbl);
761 /* Check all interfaces of an abstract class (maybe be an
762 interface too) for unimplemented methods. Such methods are
763 called miranda-methods and are marked with the ACC_MIRANDA
764 flag. VMClass.getDeclaredMethods does not return such
767 if (c->flags & ACC_ABSTRACT) {
770 s4 abstractmethodscount;
774 abstractmethodscount = 0;
776 /* check all interfaces of the abstract class */
778 for (i = 0; i < c->interfacescount; i++) {
779 ic = c->interfaces[i].cls;
781 for (j = 0; j < ic->methodscount; j++) {
782 im = &(ic->methods[j]);
784 /* skip `<clinit>' and `<init>' */
786 if ((im->name == utf_clinit) || (im->name == utf_init))
789 for (tc = c; tc != NULL; tc = tc->super.cls) {
790 for (k = 0; k < tc->methodscount; k++) {
791 if (method_canoverwrite(im, &(tc->methods[k])))
792 goto noabstractmethod;
796 abstractmethodscount++;
803 if (abstractmethodscount > 0) {
806 /* reallocate methods memory */
808 c->methods = MREALLOC(c->methods, methodinfo, c->methodscount,
809 c->methodscount + abstractmethodscount);
811 for (i = 0; i < c->interfacescount; i++) {
812 ic = c->interfaces[i].cls;
814 for (j = 0; j < ic->methodscount; j++) {
815 im = &(ic->methods[j]);
817 /* skip `<clinit>' and `<init>' */
819 if ((im->name == utf_clinit) || (im->name == utf_init))
822 for (tc = c; tc != NULL; tc = tc->super.cls) {
823 for (k = 0; k < tc->methodscount; k++) {
824 if (method_canoverwrite(im, &(tc->methods[k])))
825 goto noabstractmethod2;
829 /* Copy the method found into the new c->methods
830 array and tag it as miranda-method. */
832 am = &(c->methods[c->methodscount]);
835 MCOPY(am, im, methodinfo, 1);
837 am->vftblindex = (vftbllength++);
839 am->flags |= ACC_MIRANDA;
847 RT_TIMING_GET_TIME(time_abstract);
850 #if defined(ENABLE_STATISTICS)
853 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
856 /* compute interfacetable length */
858 interfacetablelength = 0;
860 for (tc = c; tc != NULL; tc = tc->super.cls) {
861 for (i = 0; i < tc->interfacescount; i++) {
862 s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
864 if (h > interfacetablelength)
865 interfacetablelength = h;
868 RT_TIMING_GET_TIME(time_compute_iftbl);
870 /* allocate virtual function table */
872 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
873 sizeof(methodptr) * (vftbllength - 1) +
874 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
875 v = (vftbl_t *) (((methodptr *) v) +
876 (interfacetablelength - 1) * (interfacetablelength > 1));
880 v->vftbllength = vftbllength;
881 v->interfacetablelength = interfacetablelength;
882 v->arraydesc = arraydesc;
884 /* store interface index in vftbl */
886 if (c->flags & ACC_INTERFACE)
887 v->baseval = -(c->index);
889 /* copy virtual function table of super class */
891 for (i = 0; i < supervftbllength; i++)
892 v->table[i] = super->vftbl->table[i];
894 /* Fill the remaining vftbl slots with the AbstractMethodError
895 stub (all after the super class slots, because they are already
898 for (; i < vftbllength; i++) {
899 #if defined(ENABLE_JIT)
900 # if defined(ENABLE_INTRP)
902 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
905 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
907 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
911 /* add method stubs into virtual function table */
913 for (i = 0; i < c->methodscount; i++) {
914 methodinfo *m = &(c->methods[i]);
916 assert(m->stubroutine == NULL);
918 /* Don't create a compiler stub for abstract methods as they
919 throw an AbstractMethodError with the default stub in the
920 vftbl. This entry is simply copied by sub-classes. */
922 if (m->flags & ACC_ABSTRACT)
925 #if defined(ENABLE_JIT)
926 # if defined(ENABLE_INTRP)
928 m->stubroutine = intrp_createcompilerstub(m);
931 m->stubroutine = codegen_generate_stub_compiler(m);
933 m->stubroutine = intrp_createcompilerstub(m);
936 /* static methods are not in the vftbl */
938 if (m->flags & ACC_STATIC)
941 /* insert the stubroutine into the vftbl */
943 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
945 RT_TIMING_GET_TIME(time_fill_vftbl);
947 /* compute instance size and offset of each field */
949 for (i = 0; i < c->fieldscount; i++) {
951 fieldinfo *f = &(c->fields[i]);
953 if (!(f->flags & ACC_STATIC)) {
954 dsize = descriptor_typesize(f->parseddesc);
956 #if defined(__I386__) || defined(__ARM__)
957 /* On i386 and ARM we align double and s8 fields to
958 4-bytes. This matches what GCC does for struct
959 members. We must do the same as gcc here because the
960 offsets in native header structs like java_lang_Double
961 must match the offsets of the Java fields
962 (eg. java.lang.Double.value). */
964 c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
966 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
969 f->offset = c->instancesize;
970 c->instancesize += dsize;
973 RT_TIMING_GET_TIME(time_offsets);
975 /* initialize interfacetable and interfacevftbllength */
977 v->interfacevftbllength = MNEW(s4, interfacetablelength);
979 #if defined(ENABLE_STATISTICS)
981 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
984 for (i = 0; i < interfacetablelength; i++) {
985 v->interfacevftbllength[i] = 0;
986 v->interfacetable[-i] = NULL;
991 for (tc = c; tc != NULL; tc = tc->super.cls)
992 for (i = 0; i < tc->interfacescount; i++)
993 if (!linker_addinterface(c, tc->interfaces[i].cls))
996 RT_TIMING_GET_TIME(time_fill_iftbl);
998 /* add finalizer method (not for java.lang.Object) */
1003 fi = class_findmethod(c, utf_finalize, utf_void__void);
1006 if (!(fi->flags & ACC_STATIC))
1009 RT_TIMING_GET_TIME(time_finalizer);
1013 linker_compute_subclasses(c);
1015 RT_TIMING_GET_TIME(time_subclasses);
1017 /* revert the linking state and class is linked */
1019 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1021 /* check worklist */
1023 /* XXX must this also be done in case of exception? */
1025 while (worklist != NULL) {
1026 method_worklist *wi = worklist;
1028 worklist = worklist->next;
1030 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1031 jit_invalidate_code(wi->m);
1033 /* XXX put worklist into dump memory? */
1034 FREE(wi, method_worklist);
1037 #if !defined(NDEBUG)
1039 log_message_class("Linking done class: ", c);
1042 RT_TIMING_TIME_DIFF(time_start ,time_resolving ,RT_TIMING_LINK_RESOLVE);
1043 RT_TIMING_TIME_DIFF(time_resolving ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
1044 RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract ,RT_TIMING_LINK_ABSTRACT);
1045 RT_TIMING_TIME_DIFF(time_abstract ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
1046 RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl ,RT_TIMING_LINK_F_VFTBL);
1047 RT_TIMING_TIME_DIFF(time_fill_vftbl ,time_offsets ,RT_TIMING_LINK_OFFSETS);
1048 RT_TIMING_TIME_DIFF(time_offsets ,time_fill_iftbl ,RT_TIMING_LINK_F_IFTBL);
1049 RT_TIMING_TIME_DIFF(time_fill_iftbl ,time_finalizer ,RT_TIMING_LINK_FINALIZER);
1050 RT_TIMING_TIME_DIFF(time_finalizer ,time_subclasses ,RT_TIMING_LINK_SUBCLASS);
1052 /* just return c to show that we didn't had a problem */
1058 /* link_array ******************************************************************
1060 This function is called by link_class to create the arraydescriptor
1063 This function returns NULL if the array cannot be linked because
1064 the component type has not been linked yet.
1066 *******************************************************************************/
1068 static arraydescriptor *link_array(classinfo *c)
1072 arraydescriptor *desc;
1077 namelen = c->name->blength;
1079 /* Check the component type */
1081 switch (c->name->text[1]) {
1083 /* c is an array of arrays. */
1084 u = utf_new(c->name->text + 1, namelen - 1);
1085 if (!(comp = load_class_from_classloader(u, c->classloader)))
1090 /* c is an array of objects. */
1091 u = utf_new(c->name->text + 2, namelen - 3);
1092 if (!(comp = load_class_from_classloader(u, c->classloader)))
1097 /* If the component type has not been linked, link it now */
1099 assert(!comp || (comp->state & CLASS_LOADED));
1101 if (comp && !(comp->state & CLASS_LINKED))
1102 if (!link_class(comp))
1105 /* Allocate the arraydescriptor */
1107 desc = NEW(arraydescriptor);
1110 /* c is an array of references */
1111 desc->arraytype = ARRAYTYPE_OBJECT;
1112 desc->componentsize = sizeof(void*);
1113 desc->dataoffset = OFFSET(java_objectarray, data);
1115 compvftbl = comp->vftbl;
1118 log_text("Component class has no vftbl");
1122 desc->componentvftbl = compvftbl;
1124 if (compvftbl->arraydesc) {
1125 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1127 if (compvftbl->arraydesc->dimension >= 255) {
1128 log_text("Creating array of dimension >255");
1132 desc->dimension = compvftbl->arraydesc->dimension + 1;
1133 desc->elementtype = compvftbl->arraydesc->elementtype;
1136 desc->elementvftbl = compvftbl;
1137 desc->dimension = 1;
1138 desc->elementtype = ARRAYTYPE_OBJECT;
1142 /* c is an array of a primitive type */
1143 switch (c->name->text[1]) {
1145 desc->arraytype = ARRAYTYPE_BOOLEAN;
1146 desc->dataoffset = OFFSET(java_booleanarray,data);
1147 desc->componentsize = sizeof(u1);
1151 desc->arraytype = ARRAYTYPE_BYTE;
1152 desc->dataoffset = OFFSET(java_bytearray,data);
1153 desc->componentsize = sizeof(u1);
1157 desc->arraytype = ARRAYTYPE_CHAR;
1158 desc->dataoffset = OFFSET(java_chararray,data);
1159 desc->componentsize = sizeof(u2);
1163 desc->arraytype = ARRAYTYPE_DOUBLE;
1164 desc->dataoffset = OFFSET(java_doublearray,data);
1165 desc->componentsize = sizeof(double);
1169 desc->arraytype = ARRAYTYPE_FLOAT;
1170 desc->dataoffset = OFFSET(java_floatarray,data);
1171 desc->componentsize = sizeof(float);
1175 desc->arraytype = ARRAYTYPE_INT;
1176 desc->dataoffset = OFFSET(java_intarray,data);
1177 desc->componentsize = sizeof(s4);
1181 desc->arraytype = ARRAYTYPE_LONG;
1182 desc->dataoffset = OFFSET(java_longarray,data);
1183 desc->componentsize = sizeof(s8);
1187 desc->arraytype = ARRAYTYPE_SHORT;
1188 desc->dataoffset = OFFSET(java_shortarray,data);
1189 desc->componentsize = sizeof(s2);
1193 exceptions_throw_noclassdeffounderror(c->name);
1197 desc->componentvftbl = NULL;
1198 desc->elementvftbl = NULL;
1199 desc->dimension = 1;
1200 desc->elementtype = desc->arraytype;
1207 /* linker_compute_subclasses ***************************************************
1211 *******************************************************************************/
1213 static void linker_compute_subclasses(classinfo *c)
1215 LOCK_MONITOR_ENTER(linker_classrenumber_lock);
1217 if (!(c->flags & ACC_INTERFACE)) {
1222 if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
1223 c->nextsub = c->super.cls->sub;
1224 c->super.cls->sub = c;
1229 /* compute class values */
1231 linker_compute_class_values(class_java_lang_Object);
1233 LOCK_MONITOR_EXIT(linker_classrenumber_lock);
1237 /* linker_compute_class_values *************************************************
1241 *******************************************************************************/
1243 static void linker_compute_class_values(classinfo *c)
1247 c->vftbl->baseval = ++classvalue;
1252 linker_compute_class_values(subs);
1254 subs = subs->nextsub;
1257 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1261 /* linker_addinterface *********************************************************
1263 Is needed by link_class for adding a VTBL to a class. All
1264 interfaces implemented by ic are added as well.
1267 true.........everything ok
1268 false........an exception has been thrown
1270 *******************************************************************************/
1272 static bool linker_addinterface(classinfo *c, classinfo *ic)
1283 if (i >= v->interfacetablelength)
1284 vm_abort("Internal error: interfacetable overflow");
1286 /* if this interface has already been added, return immediately */
1288 if (v->interfacetable[-i] != NULL)
1291 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1292 v->interfacevftbllength[i] = 1;
1293 v->interfacetable[-i] = MNEW(methodptr, 1);
1294 v->interfacetable[-i][0] = NULL;
1297 v->interfacevftbllength[i] = ic->methodscount;
1298 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1300 #if defined(ENABLE_STATISTICS)
1302 count_vftbl_len += sizeof(methodptr) *
1303 (ic->methodscount + (ic->methodscount == 0));
1306 for (j = 0; j < ic->methodscount; j++) {
1307 for (sc = c; sc != NULL; sc = sc->super.cls) {
1308 for (k = 0; k < sc->methodscount; k++) {
1309 m = &(sc->methods[k]);
1311 if (method_canoverwrite(m, &(ic->methods[j]))) {
1312 /* method m overwrites the (abstract) method */
1313 #if defined(ENABLE_VERIFIER)
1314 /* Add loading constraints (for the more
1315 general types of the method
1317 if (!classcache_add_constraints_for_params(
1318 c->classloader, ic->classloader,
1325 /* XXX taken from gcj */
1326 /* check for ACC_STATIC: IncompatibleClassChangeError */
1328 /* check for !ACC_PUBLIC: IllegalAccessError */
1330 /* check for ACC_ABSTRACT: AbstracMethodError,
1331 not sure about that one */
1333 v->interfacetable[-i][j] = v->table[m->vftblindex];
1339 /* If no method was found, insert the AbstractMethodError
1342 #if defined(ENABLE_JIT)
1343 # if defined(ENABLE_INTRP)
1345 v->interfacetable[-i][j] =
1346 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1349 v->interfacetable[-i][j] =
1350 (methodptr) (ptrint) &asm_abstractmethoderror;
1352 v->interfacetable[-i][j] =
1353 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1361 /* add superinterfaces of this interface */
1363 for (j = 0; j < ic->interfacescount; j++)
1364 if (!linker_addinterface(c, ic->interfaces[j].cls))
1373 /* class_highestinterface ******************************************************
1375 Used by the function link_class to determine the amount of memory
1376 needed for the interface table.
1378 *******************************************************************************/
1380 static s4 class_highestinterface(classinfo *c)
1386 /* check for ACC_INTERFACE bit already done in link_class_intern */
1390 for (i = 0; i < c->interfacescount; i++) {
1391 h2 = class_highestinterface(c->interfaces[i].cls);
1402 * These are local overrides for various environment variables in Emacs.
1403 * Please do not remove this and leave it at the end of the file, where
1404 * Emacs will automagically detect them.
1405 * ---------------------------------------------------------------------
1408 * indent-tabs-mode: t
1412 * vim:noexpandtab:sw=4:ts=4: