1 /* src/vm/linker.c - class linker functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "mm/memory.hpp"
35 #include "native/native.hpp"
37 #include "threads/lock.hpp"
38 #include "threads/mutex.hpp"
40 #include "toolbox/logging.hpp"
42 #include "vm/access.hpp"
43 #include "vm/array.hpp"
44 #include "vm/class.hpp"
45 #include "vm/classcache.hpp"
46 #include "vm/exceptions.hpp"
47 #include "vm/globals.hpp"
48 #include "vm/loader.hpp"
49 #include "vm/options.h"
50 #include "vm/primitive.hpp"
51 #include "vm/rt-timing.h"
52 #include "vm/string.hpp"
55 #include "vm/jit/asmpart.h"
56 #include "vm/jit/stubs.hpp"
59 /* debugging macros ***********************************************************/
62 # define TRACELINKCLASS(c) \
64 if (opt_TraceLinkClass) { \
66 log_print("[Linking "); \
73 # define TRACELINKCLASS(c)
77 /* #include "vm/resolve.hpp" */
78 /* copied prototype to avoid bootstrapping problem: */
79 classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool checkaccess);
81 #if defined(ENABLE_STATISTICS)
82 # include "vm/statistics.h"
85 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
86 #define INLINELOG(code) do { if (opt_TraceInlining) { code } } while (0)
88 #define INLINELOG(code)
92 /* global variables ***********************************************************/
94 static s4 interfaceindex; /* sequential numbering of interfaces */
98 Mutex *linker_classrenumber_lock;
101 #if defined(__cplusplus)
105 /* private functions **********************************************************/
107 static classinfo *link_class_intern(classinfo *c);
108 static arraydescriptor *link_array(classinfo *c);
109 static void linker_compute_class_values(classinfo *c);
110 static void linker_compute_subclasses(classinfo *c);
111 static bool linker_addinterface(classinfo *c, classinfo *ic);
112 static s4 class_highestinterface(classinfo *c);
115 /* linker_init *****************************************************************
117 Initializes the linker subsystem and links classes required for the
120 *******************************************************************************/
122 void linker_preinit(void)
124 TRACESUBSYSTEMINITIALIZATION("linker_preinit");
126 /* Reset interface index. */
130 #if defined(ENABLE_THREADS) && !USES_NEW_SUBTYPE
131 /* create the global mutex */
133 linker_classrenumber_lock = new Mutex();
136 /* Link the most basic classes. */
138 if (!link_class(class_java_lang_Object))
139 vm_abort("linker_preinit: linking java/lang/Object failed");
141 #if defined(ENABLE_JAVASE)
142 if (!link_class(class_java_lang_Cloneable))
143 vm_abort("linker_preinit: linking java/lang/Cloneable failed");
145 if (!link_class(class_java_io_Serializable))
146 vm_abort("linker_preinit: linking java/io/Serializable failed");
151 /* linker_init *****************************************************************
153 Links all classes required in the VM.
155 *******************************************************************************/
157 void linker_init(void)
159 TRACESUBSYSTEMINITIALIZATION("linker_init");
161 /* Link java.lang.Class as first class of the system, because we
162 need it's vftbl for all other classes so we can use a class as
165 if (!link_class(class_java_lang_Class))
166 vm_abort("linker_init: linking java/lang/Class failed");
168 /* Now set the header.vftbl of all classes which were created
169 before java.lang.Class was linked. */
171 class_postset_header_vftbl();
173 /* Link primitive-type wrapping classes. */
175 #if defined(ENABLE_JAVASE)
176 if (!link_class(class_java_lang_Void))
177 vm_abort("linker_init: linking failed");
180 if (!link_class(class_java_lang_Boolean))
181 vm_abort("linker_init: linking failed");
183 if (!link_class(class_java_lang_Byte))
184 vm_abort("linker_init: linking failed");
186 if (!link_class(class_java_lang_Character))
187 vm_abort("linker_init: linking failed");
189 if (!link_class(class_java_lang_Short))
190 vm_abort("linker_init: linking failed");
192 if (!link_class(class_java_lang_Integer))
193 vm_abort("linker_init: linking failed");
195 if (!link_class(class_java_lang_Long))
196 vm_abort("linker_init: linking failed");
198 if (!link_class(class_java_lang_Float))
199 vm_abort("linker_init: linking failed");
201 if (!link_class(class_java_lang_Double))
202 vm_abort("linker_init: linking failed");
204 /* Link important system classes. */
206 if (!link_class(class_java_lang_String))
207 vm_abort("linker_init: linking java/lang/String failed");
209 #if defined(ENABLE_JAVASE)
210 if (!link_class(class_java_lang_ClassLoader))
211 vm_abort("linker_init: linking failed");
213 if (!link_class(class_java_lang_SecurityManager))
214 vm_abort("linker_init: linking failed");
217 if (!link_class(class_java_lang_System))
218 vm_abort("linker_init: linking failed");
220 if (!link_class(class_java_lang_Thread))
221 vm_abort("linker_init: linking failed");
223 #if defined(ENABLE_JAVASE)
224 if (!link_class(class_java_lang_ThreadGroup))
225 vm_abort("linker_init: linking failed");
228 if (!link_class(class_java_lang_Throwable))
229 vm_abort("linker_init: linking failed");
231 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
232 if (!link_class(class_java_lang_VMSystem))
233 vm_abort("linker_init: linking failed");
235 if (!link_class(class_java_lang_VMThread))
236 vm_abort("linker_init: linking failed");
238 if (!link_class(class_java_lang_VMThrowable))
239 vm_abort("linker_init: linking failed");
242 /* Important system exceptions. */
244 if (!link_class(class_java_lang_Exception))
245 vm_abort("linker_init: linking failed");
247 if (!link_class(class_java_lang_ClassNotFoundException))
248 vm_abort("linker_init: linking failed");
250 if (!link_class(class_java_lang_RuntimeException))
251 vm_abort("linker_init: linking failed");
253 /* some classes which may be used more often */
255 #if defined(ENABLE_JAVASE)
256 if (!link_class(class_java_lang_StackTraceElement))
257 vm_abort("linker_init: linking failed");
259 if (!link_class(class_java_lang_reflect_Constructor))
260 vm_abort("linker_init: linking failed");
262 if (!link_class(class_java_lang_reflect_Field))
263 vm_abort("linker_init: linking failed");
265 if (!link_class(class_java_lang_reflect_Method))
266 vm_abort("linker_init: linking failed");
268 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
269 if (!link_class(class_java_lang_reflect_VMConstructor))
270 vm_abort("linker_init: linking failed");
272 if (!link_class(class_java_lang_reflect_VMField))
273 vm_abort("linker_init: linking failed");
275 if (!link_class(class_java_lang_reflect_VMMethod))
276 vm_abort("linker_init: linking failed");
279 if (!link_class(class_java_security_PrivilegedAction))
280 vm_abort("linker_init: linking failed");
282 if (!link_class(class_java_util_Vector))
283 vm_abort("linker_init: linking failed");
285 if (!link_class(class_java_util_HashMap))
286 vm_abort("linker_init: linking failed");
288 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
289 if (!link_class(class_sun_misc_Signal))
290 vm_abort("linker_init: linking failed");
292 if (!link_class(class_sun_reflect_MagicAccessorImpl))
293 vm_abort("linker_init: linking failed");
296 if (!link_class(arrayclass_java_lang_Object))
297 vm_abort("linker_init: linking failed");
301 /* create pseudo classes used by the typechecker */
303 /* pseudo class for Arraystubs (extends java.lang.Object) */
305 pseudo_class_Arraystub =
306 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
307 pseudo_class_Arraystub->state |= CLASS_LOADED;
308 pseudo_class_Arraystub->super = class_java_lang_Object;
310 #if defined(ENABLE_JAVASE)
312 pseudo_class_Arraystub->interfacescount = 2;
313 pseudo_class_Arraystub->interfaces = MNEW(classinfo*, 2);
314 pseudo_class_Arraystub->interfaces[0] = class_java_lang_Cloneable;
315 pseudo_class_Arraystub->interfaces[1] = class_java_io_Serializable;
317 #elif defined(ENABLE_JAVAME_CLDC1_1)
319 pseudo_class_Arraystub->interfacescount = 0;
320 pseudo_class_Arraystub->interfaces = NULL;
323 # error unknown Java configuration
326 if (!classcache_store_unique(pseudo_class_Arraystub))
327 vm_abort("linker_init: could not cache pseudo_class_Arraystub");
329 if (!link_class(pseudo_class_Arraystub))
330 vm_abort("linker_init: linking pseudo_class_Arraystub failed");
332 /* pseudo class representing the null type */
334 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
335 pseudo_class_Null->state |= CLASS_LOADED;
336 pseudo_class_Null->super = class_java_lang_Object;
338 if (!classcache_store_unique(pseudo_class_Null))
339 vm_abort("linker_init: could not cache pseudo_class_Null");
341 if (!link_class(pseudo_class_Null))
342 vm_abort("linker_init: linking failed");
344 /* pseudo class representing new uninitialized objects */
346 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
347 pseudo_class_New->state |= CLASS_LOADED;
348 pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
349 pseudo_class_New->super = class_java_lang_Object;
351 if (!classcache_store_unique(pseudo_class_New))
352 vm_abort("linker_init: could not cache pseudo_class_New");
354 /* Correct vftbl-entries (retarded loading and linking of class
355 java/lang/String). */
357 stringtable_update();
361 /* link_class ******************************************************************
363 Wrapper function for link_class_intern to ease monitor enter/exit
364 and exception handling.
366 *******************************************************************************/
368 classinfo *link_class(classinfo *c)
371 #if defined(ENABLE_RT_TIMING)
372 struct timespec time_start, time_end;
375 RT_TIMING_GET_TIME(time_start);
378 exceptions_throw_nullpointerexception();
382 LOCK_MONITOR_ENTER(c);
384 /* Maybe the class is currently linking or is already linked.*/
386 if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
387 LOCK_MONITOR_EXIT(c);
392 #if defined(ENABLE_STATISTICS)
395 if (opt_getcompilingtime)
396 compilingtime_stop();
398 if (opt_getloadingtime)
402 /* call the internal function */
404 r = link_class_intern(c);
406 /* If return value is NULL, we had a problem and the class is not
410 c->state &= ~CLASS_LINKING;
412 #if defined(ENABLE_STATISTICS)
415 if (opt_getloadingtime)
418 if (opt_getcompilingtime)
419 compilingtime_start();
422 LOCK_MONITOR_EXIT(c);
424 RT_TIMING_GET_TIME(time_end);
426 RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
432 /* linker_overwrite_method *****************************************************
434 Overwrite a method with another one, update method flags and check
438 mg................the general method being overwritten
439 ms................the overwriting (more specialized) method
440 wl................worklist where to add invalidated methods
443 true..............everything ok
444 false.............an exception has been thrown
446 *******************************************************************************/
448 static bool linker_overwrite_method(methodinfo *mg,
450 method_worklist **wl)
458 /* overriding a final method is illegal */
460 if (mg->flags & ACC_FINAL) {
461 exceptions_throw_verifyerror(mg, "Overriding final method");
465 /* method ms overwrites method mg */
467 #if defined(ENABLE_VERIFIER)
468 /* Add loading constraints (for the more general types of method mg). */
469 /* Not for <init>, as it is not invoked virtually. */
471 if ((ms->name != utf_init)
472 && !classcache_add_constraints_for_params(
473 cs->classloader, cg->classloader, mg))
479 /* inherit the vftbl index, and record the overwriting */
481 ms->vftblindex = mg->vftblindex;
484 /* update flags and check assumptions */
485 /* <init> methods are a special case, as they are never dispatched dynamically */
487 if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
490 #if defined(ENABLE_TLH)
491 if (mg->flags & ACC_METHOD_MONOMORPHY_USED) {
492 printf("%s/%s is evil! the siner is %s/%s\n", mg->clazz->name->text, mg->name->text,
493 ms->clazz->name->text, ms->name->text);
494 ms->flags |= ACC_METHOD_PARENT_MONOMORPHY_USED;
498 if (mg->flags & ACC_METHOD_IMPLEMENTED) {
499 /* this adds another implementation */
501 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
503 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
505 method_break_assumption_monomorphic(mg, wl);
508 /* this is the first implementation */
510 mg->flags |= ACC_METHOD_IMPLEMENTED;
512 INLINELOG( printf("becomes implemented: "); method_println(mg); );
517 } while (mg != NULL);
525 /* build_display ***************************************************************
527 Builds the entire display for a class. This entails filling the fixed part
528 as well as allocating and initializing the overflow part.
530 See Cliff Click and John Rose: Fast subtype checking in the Hotspot JVM.
532 *******************************************************************************/
534 static classinfo *build_display(classinfo *c)
542 if (c->vftbl->arraydesc) {
543 arraydescriptor *a = c->vftbl->arraydesc;
544 if (a->elementvftbl && a->elementvftbl->clazz->super) {
545 classinfo *cls = a->elementvftbl->clazz->super;
547 for (n=0; n<a->dimension; n++)
548 cls = class_array_of(cls, true);
552 if (a->componentvftbl && a->elementvftbl) {
553 super = a->componentvftbl->clazz;
557 /* Normal classes. */
561 if (!link_class(super))
563 depth = super->vftbl->subtype_depth + 1;
565 /* java.lang.Object doesn't have a super class. */
568 /* Now copy super's display, append c->vftbl and initialize the remaining fields. */
569 if (depth >= DISPLAY_SIZE) {
570 c->vftbl->subtype_overflow = MNEW(vftbl_t *, depth - DISPLAY_SIZE + 1);
571 #if defined(ENABLE_STATISTICS)
573 count_vftbl_len += sizeof(vftbl_t*) * (depth - DISPLAY_SIZE + 1);
575 memcpy(c->vftbl->subtype_overflow, super->vftbl->subtype_overflow, sizeof(vftbl_t*) * (depth - DISPLAY_SIZE));
576 c->vftbl->subtype_overflow[depth - DISPLAY_SIZE] = c->vftbl;
577 depth_fixed = DISPLAY_SIZE;
581 c->vftbl->subtype_display[depth] = c->vftbl;
585 memcpy(c->vftbl->subtype_display, super->vftbl->subtype_display, sizeof(vftbl_t*) * depth_fixed);
586 for (i=depth_fixed+1; i<=DISPLAY_SIZE; i++)
587 c->vftbl->subtype_display[i] = NULL;
588 c->vftbl->subtype_offset = OFFSET(vftbl_t, subtype_display[0]) + sizeof(vftbl_t*) * depth_fixed;
589 c->vftbl->subtype_depth = depth;
595 /* link_class_intern ***********************************************************
597 Tries to link a class. The function calculates the length in bytes
598 that an instance of this class requires as well as the VTBL for
599 methods and interface methods.
601 *******************************************************************************/
603 static classinfo *link_class_intern(classinfo *c)
605 classinfo *super; /* super class */
606 classinfo *tc; /* temporary class variable */
607 s4 supervftbllength; /* vftbllegnth of super class */
608 s4 vftbllength; /* vftbllength of current class */
609 s4 interfacetablelength; /* interface table length */
610 vftbl_t *v; /* vftbl of current class */
611 s4 i; /* interface/method/field counter */
612 arraydescriptor *arraydesc; /* descriptor for array classes */
613 method_worklist *worklist; /* worklist for recompilation */
614 #if defined(ENABLE_RT_TIMING)
615 struct timespec time_start, time_resolving, time_compute_vftbl,
616 time_abstract, time_compute_iftbl, time_fill_vftbl,
617 time_offsets, time_fill_iftbl, time_finalizer,
621 RT_TIMING_GET_TIME(time_start);
625 /* the class must be loaded */
627 /* XXX should this be a specific exception? */
628 assert(c->state & CLASS_LOADED);
630 /* This is check in link_class. */
632 assert(!(c->state & CLASS_LINKED));
634 /* cache the self-reference of this class */
635 /* we do this for cases where the defining loader of the class */
636 /* has not yet been recorded as an initiating loader for the class */
637 /* this is needed so subsequent code can assume that self-refs */
638 /* will always resolve lazily */
639 /* No need to do it for the bootloader - it is always registered */
640 /* as initiating loader for the classes it loads. */
642 classcache_store(c->classloader,c,false);
644 /* this class is currently linking */
646 c->state |= CLASS_LINKING;
651 /* Link the super interfaces. */
653 for (i = 0; i < c->interfacescount; i++) {
654 tc = c->interfaces[i];
656 if (!(tc->state & CLASS_LINKED))
661 /* check super class */
665 /* Check for java/lang/Object. */
667 if (c->super == NULL) {
669 c->instancesize = sizeof(java_object_t);
671 vftbllength = supervftbllength = 0;
676 /* Get super class. */
680 /* Link the super class if necessary. */
682 if (!(super->state & CLASS_LINKED))
683 if (!link_class(super))
686 /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
689 c->flags |= (super->flags &
690 (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
692 /* handle array classes */
694 if (c->name->text[0] == '[')
695 if (!(arraydesc = link_array(c)))
698 if (c->flags & ACC_INTERFACE)
699 c->index = interfaceindex++;
701 c->index = super->index + 1;
703 c->instancesize = super->instancesize;
705 vftbllength = supervftbllength = super->vftbl->vftbllength;
707 c->finalizer = super->finalizer;
709 RT_TIMING_GET_TIME(time_resolving);
712 /* compute vftbl length */
714 for (i = 0; i < c->methodscount; i++) {
715 methodinfo *m = &(c->methods[i]);
717 if (!(m->flags & ACC_STATIC)) { /* is instance method */
723 for (j = 0; j < tc->methodscount; j++) {
724 if (method_canoverwrite(m, &(tc->methods[j]))) {
725 if (tc->methods[j].flags & ACC_PRIVATE)
726 goto notfoundvftblindex;
728 /* package-private methods in other packages */
729 /* must not be overridden */
730 /* (see Java Language Specification 8.4.8.1) */
731 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
732 && !SAME_PACKAGE(c,tc) )
734 goto notfoundvftblindex;
737 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
740 goto foundvftblindex;
748 m->vftblindex = (vftbllength++);
753 RT_TIMING_GET_TIME(time_compute_vftbl);
756 /* Check all interfaces of an abstract class (maybe be an
757 interface too) for unimplemented methods. Such methods are
758 called miranda-methods and are marked with the ACC_MIRANDA
759 flag. VMClass.getDeclaredMethods does not return such
762 if (c->flags & ACC_ABSTRACT) {
765 s4 abstractmethodscount;
769 abstractmethodscount = 0;
771 /* check all interfaces of the abstract class */
773 for (i = 0; i < c->interfacescount; i++) {
774 ic = c->interfaces[i];
776 for (j = 0; j < ic->methodscount; j++) {
777 im = &(ic->methods[j]);
779 /* skip `<clinit>' and `<init>' */
781 if ((im->name == utf_clinit) || (im->name == utf_init))
784 for (tc = c; tc != NULL; tc = tc->super) {
785 for (k = 0; k < tc->methodscount; k++) {
786 if (method_canoverwrite(im, &(tc->methods[k])))
787 goto noabstractmethod;
791 abstractmethodscount++;
798 if (abstractmethodscount > 0) {
801 /* reallocate methods memory */
803 c->methods = (methodinfo*) MREALLOC(c->methods, methodinfo, c->methodscount,
804 c->methodscount + abstractmethodscount);
806 for (i = 0; i < c->interfacescount; i++) {
807 ic = c->interfaces[i];
809 for (j = 0; j < ic->methodscount; j++) {
810 im = &(ic->methods[j]);
812 /* skip `<clinit>' and `<init>' */
814 if ((im->name == utf_clinit) || (im->name == utf_init))
817 for (tc = c; tc != NULL; tc = tc->super) {
818 for (k = 0; k < tc->methodscount; k++) {
819 if (method_canoverwrite(im, &(tc->methods[k])))
820 goto noabstractmethod2;
824 /* Copy the method found into the new c->methods
825 array and tag it as miranda-method. */
827 am = &(c->methods[c->methodscount]);
830 MCOPY(am, im, methodinfo, 1);
832 am->vftblindex = (vftbllength++);
834 am->flags |= ACC_MIRANDA;
842 RT_TIMING_GET_TIME(time_abstract);
845 #if defined(ENABLE_STATISTICS)
848 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
851 /* compute interfacetable length */
853 interfacetablelength = 0;
855 for (tc = c; tc != NULL; tc = tc->super) {
856 for (i = 0; i < tc->interfacescount; i++) {
857 s4 h = class_highestinterface(tc->interfaces[i]) + 1;
859 if (h > interfacetablelength)
860 interfacetablelength = h;
863 RT_TIMING_GET_TIME(time_compute_iftbl);
865 /* allocate virtual function table */
867 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
868 sizeof(methodptr) * (vftbllength - 1) +
869 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
870 v = (vftbl_t *) (((methodptr *) v) +
871 (interfacetablelength - 1) * (interfacetablelength > 1));
875 v->vftbllength = vftbllength;
876 v->interfacetablelength = interfacetablelength;
877 v->arraydesc = arraydesc;
879 /* store interface index in vftbl */
881 if (c->flags & ACC_INTERFACE)
882 v->baseval = -(c->index);
884 /* copy virtual function table of super class */
886 for (i = 0; i < supervftbllength; i++)
887 v->table[i] = super->vftbl->table[i];
889 /* Fill the remaining vftbl slots with the AbstractMethodError
890 stub (all after the super class slots, because they are already
893 for (; i < vftbllength; i++) {
894 #if defined(ENABLE_JIT)
895 # if defined(ENABLE_INTRP)
897 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
900 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
902 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
906 /* add method stubs into virtual function table */
908 for (i = 0; i < c->methodscount; i++) {
909 methodinfo *m = &(c->methods[i]);
911 assert(m->stubroutine == NULL);
913 /* Don't create a compiler stub for abstract methods as they
914 throw an AbstractMethodError with the default stub in the
915 vftbl. This entry is simply copied by sub-classes. */
917 if (m->flags & ACC_ABSTRACT)
920 #if defined(ENABLE_JIT)
921 # if defined(ENABLE_INTRP)
923 m->stubroutine = intrp_createcompilerstub(m);
926 m->stubroutine = (u1*) CompilerStub::generate(m);
928 m->stubroutine = intrp_createcompilerstub(m);
931 /* static methods are not in the vftbl */
933 if (m->flags & ACC_STATIC)
936 /* insert the stubroutine into the vftbl */
938 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
940 RT_TIMING_GET_TIME(time_fill_vftbl);
942 /* compute instance size and offset of each field */
944 for (i = 0; i < c->fieldscount; i++) {
946 fieldinfo *f = &(c->fields[i]);
948 if (!(f->flags & ACC_STATIC)) {
949 dsize = descriptor_typesize(f->parseddesc);
950 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
951 f->offset = c->instancesize;
952 c->instancesize += dsize;
955 RT_TIMING_GET_TIME(time_offsets);
957 /* initialize interfacetable and interfacevftbllength */
959 v->interfacevftbllength = MNEW(s4, interfacetablelength);
961 #if defined(ENABLE_STATISTICS)
963 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
966 for (i = 0; i < interfacetablelength; i++) {
967 v->interfacevftbllength[i] = 0;
968 v->interfacetable[-i] = NULL;
973 for (tc = c; tc != NULL; tc = tc->super)
974 for (i = 0; i < tc->interfacescount; i++)
975 if (!linker_addinterface(c, tc->interfaces[i]))
978 RT_TIMING_GET_TIME(time_fill_iftbl);
980 /* add finalizer method (not for java.lang.Object) */
985 fi = class_findmethod(c, utf_finalize, utf_void__void);
988 if (!(fi->flags & ACC_STATIC))
991 RT_TIMING_GET_TIME(time_finalizer);
995 linker_compute_subclasses(c);
997 /* FIXME: this is completely useless now */
998 RT_TIMING_GET_TIME(time_subclasses);
1000 #if USES_NEW_SUBTYPE
1001 if (!build_display(c))
1005 /* revert the linking state and class is linked */
1007 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1009 /* check worklist */
1011 /* XXX must this also be done in case of exception? */
1013 while (worklist != NULL) {
1014 method_worklist *wi = worklist;
1016 worklist = worklist->next;
1018 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1019 jit_invalidate_code(wi->m);
1021 /* XXX put worklist into dump memory? */
1022 FREE(wi, method_worklist);
1025 RT_TIMING_TIME_DIFF(time_start ,time_resolving ,RT_TIMING_LINK_RESOLVE);
1026 RT_TIMING_TIME_DIFF(time_resolving ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
1027 RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract ,RT_TIMING_LINK_ABSTRACT);
1028 RT_TIMING_TIME_DIFF(time_abstract ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
1029 RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl ,RT_TIMING_LINK_F_VFTBL);
1030 RT_TIMING_TIME_DIFF(time_fill_vftbl ,time_offsets ,RT_TIMING_LINK_OFFSETS);
1031 RT_TIMING_TIME_DIFF(time_offsets ,time_fill_iftbl ,RT_TIMING_LINK_F_IFTBL);
1032 RT_TIMING_TIME_DIFF(time_fill_iftbl ,time_finalizer ,RT_TIMING_LINK_FINALIZER);
1033 RT_TIMING_TIME_DIFF(time_finalizer ,time_subclasses ,RT_TIMING_LINK_SUBCLASS);
1035 /* just return c to show that we didn't had a problem */
1041 /* link_array ******************************************************************
1043 This function is called by link_class to create the arraydescriptor
1046 This function returns NULL if the array cannot be linked because
1047 the component type has not been linked yet.
1049 *******************************************************************************/
1051 static arraydescriptor *link_array(classinfo *c)
1055 arraydescriptor *desc;
1060 namelen = c->name->blength;
1062 /* Check the component type */
1064 switch (c->name->text[1]) {
1066 /* c is an array of arrays. */
1067 u = utf_new(c->name->text + 1, namelen - 1);
1068 if (!(comp = load_class_from_classloader(u, c->classloader)))
1073 /* c is an array of objects. */
1074 u = utf_new(c->name->text + 2, namelen - 3);
1075 if (!(comp = load_class_from_classloader(u, c->classloader)))
1080 /* If the component type has not been linked, link it now */
1082 assert(!comp || (comp->state & CLASS_LOADED));
1084 if (comp && !(comp->state & CLASS_LINKED))
1085 if (!link_class(comp))
1088 /* Allocate the arraydescriptor */
1090 desc = NEW(arraydescriptor);
1093 /* c is an array of references */
1094 desc->arraytype = ARRAYTYPE_OBJECT;
1095 desc->componentsize = sizeof(void*);
1096 desc->dataoffset = OFFSET(java_objectarray_t, data);
1098 compvftbl = comp->vftbl;
1101 log_text("Component class has no vftbl");
1105 desc->componentvftbl = compvftbl;
1107 if (compvftbl->arraydesc) {
1108 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1110 if (compvftbl->arraydesc->dimension >= 255) {
1111 exceptions_throw_illegalargumentexception();
1115 desc->dimension = compvftbl->arraydesc->dimension + 1;
1116 desc->elementtype = compvftbl->arraydesc->elementtype;
1119 desc->elementvftbl = compvftbl;
1120 desc->dimension = 1;
1121 desc->elementtype = ARRAYTYPE_OBJECT;
1125 /* c is an array of a primitive type */
1126 switch (c->name->text[1]) {
1128 desc->arraytype = ARRAYTYPE_BOOLEAN;
1129 desc->dataoffset = OFFSET(java_booleanarray_t,data);
1130 desc->componentsize = sizeof(u1);
1134 desc->arraytype = ARRAYTYPE_BYTE;
1135 desc->dataoffset = OFFSET(java_bytearray_t,data);
1136 desc->componentsize = sizeof(u1);
1140 desc->arraytype = ARRAYTYPE_CHAR;
1141 desc->dataoffset = OFFSET(java_chararray_t,data);
1142 desc->componentsize = sizeof(u2);
1146 desc->arraytype = ARRAYTYPE_DOUBLE;
1147 desc->dataoffset = OFFSET(java_doublearray_t,data);
1148 desc->componentsize = sizeof(double);
1152 desc->arraytype = ARRAYTYPE_FLOAT;
1153 desc->dataoffset = OFFSET(java_floatarray_t,data);
1154 desc->componentsize = sizeof(float);
1158 desc->arraytype = ARRAYTYPE_INT;
1159 desc->dataoffset = OFFSET(java_intarray_t,data);
1160 desc->componentsize = sizeof(s4);
1164 desc->arraytype = ARRAYTYPE_LONG;
1165 desc->dataoffset = OFFSET(java_longarray_t,data);
1166 desc->componentsize = sizeof(s8);
1170 desc->arraytype = ARRAYTYPE_SHORT;
1171 desc->dataoffset = OFFSET(java_shortarray_t,data);
1172 desc->componentsize = sizeof(s2);
1176 exceptions_throw_noclassdeffounderror(c->name);
1180 desc->componentvftbl = NULL;
1181 desc->elementvftbl = NULL;
1182 desc->dimension = 1;
1183 desc->elementtype = desc->arraytype;
1190 /* linker_compute_subclasses ***************************************************
1194 ATTENTION: DO NOT REMOVE ANY OF THE LOCKING MECHANISMS BELOW:
1195 This function needs to take the class renumber lock and stop the
1196 world during class renumbering. The lock is used in C code which
1197 is not that performance critical. Whereas JIT code uses critical
1198 sections to atomically access the class values.
1200 *******************************************************************************/
1202 static void linker_compute_subclasses(classinfo *c)
1205 LOCK_CLASSRENUMBER_LOCK;
1207 if (!(c->flags & ACC_INTERFACE)) {
1210 #if USES_NEW_SUBTYPE
1211 c->vftbl->baseval = 1; /* so it does not look like an interface */
1215 if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
1216 c->nextsub = c->super->sub;
1222 #if !USES_NEW_SUBTYPE
1223 /* compute class values */
1225 linker_compute_class_values(class_java_lang_Object);
1228 UNLOCK_CLASSRENUMBER_LOCK;
1233 /* linker_compute_class_values *************************************************
1237 *******************************************************************************/
1239 static void linker_compute_class_values(classinfo *c)
1243 c->vftbl->baseval = ++classvalue;
1248 linker_compute_class_values(subs);
1250 subs = subs->nextsub;
1253 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1257 /* linker_addinterface *********************************************************
1259 Is needed by link_class for adding a VTBL to a class. All
1260 interfaces implemented by ic are added as well.
1263 true.........everything ok
1264 false........an exception has been thrown
1266 *******************************************************************************/
1268 static bool linker_addinterface(classinfo *c, classinfo *ic)
1279 if (i >= v->interfacetablelength)
1280 vm_abort("Internal error: interfacetable overflow");
1282 /* if this interface has already been added, return immediately */
1284 if (v->interfacetable[-i] != NULL)
1287 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1288 v->interfacevftbllength[i] = 1;
1289 v->interfacetable[-i] = MNEW(methodptr, 1);
1290 v->interfacetable[-i][0] = NULL;
1293 v->interfacevftbllength[i] = ic->methodscount;
1294 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1296 #if defined(ENABLE_STATISTICS)
1298 count_vftbl_len += sizeof(methodptr) *
1299 (ic->methodscount + (ic->methodscount == 0));
1302 for (j = 0; j < ic->methodscount; j++) {
1303 for (sc = c; sc != NULL; sc = sc->super) {
1304 for (k = 0; k < sc->methodscount; k++) {
1305 m = &(sc->methods[k]);
1307 if (method_canoverwrite(m, &(ic->methods[j]))) {
1308 /* method m overwrites the (abstract) method */
1309 #if defined(ENABLE_VERIFIER)
1310 /* Add loading constraints (for the more
1311 general types of the method
1313 if (!classcache_add_constraints_for_params(
1314 c->classloader, ic->classloader,
1321 /* XXX taken from gcj */
1322 /* check for ACC_STATIC: IncompatibleClassChangeError */
1324 /* check for !ACC_PUBLIC: IllegalAccessError */
1326 /* check for ACC_ABSTRACT: AbstracMethodError,
1327 not sure about that one */
1329 v->interfacetable[-i][j] = v->table[m->vftblindex];
1335 /* If no method was found, insert the AbstractMethodError
1338 #if defined(ENABLE_JIT)
1339 # if defined(ENABLE_INTRP)
1341 v->interfacetable[-i][j] =
1342 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1345 v->interfacetable[-i][j] =
1346 (methodptr) (ptrint) &asm_abstractmethoderror;
1348 v->interfacetable[-i][j] =
1349 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1357 /* add superinterfaces of this interface */
1359 for (j = 0; j < ic->interfacescount; j++)
1360 if (!linker_addinterface(c, ic->interfaces[j]))
1369 /* class_highestinterface ******************************************************
1371 Used by the function link_class to determine the amount of memory
1372 needed for the interface table.
1374 *******************************************************************************/
1376 static s4 class_highestinterface(classinfo *c)
1382 /* check for ACC_INTERFACE bit already done in link_class_intern */
1386 for (i = 0; i < c->interfacescount; i++) {
1387 h2 = class_highestinterface(c->interfaces[i]);
1396 #if defined(__cplusplus)
1401 * These are local overrides for various environment variables in Emacs.
1402 * Please do not remove this and leave it at the end of the file, where
1403 * Emacs will automagically detect them.
1404 * ---------------------------------------------------------------------
1407 * indent-tabs-mode: t
1411 * vim:noexpandtab:sw=4:ts=4: