1 /* src/vm/linker.cpp - class linker functions
3 Copyright (C) 1996, 2011
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/hook.hpp"
49 #include "vm/loader.hpp"
50 #include "vm/options.h"
51 #include "vm/primitive.hpp"
52 #include "vm/rt-timing.h"
53 #include "vm/string.hpp"
56 #include "vm/jit/asmpart.h"
57 #include "vm/jit/stubs.hpp"
63 /* debugging macros ***********************************************************/
66 # define TRACELINKCLASS(c) \
68 if (opt_TraceLinkClass) { \
70 log_print("[Linking "); \
77 # define TRACELINKCLASS(c)
81 /* #include "vm/resolve.hpp" */
82 /* copied prototype to avoid bootstrapping problem: */
83 classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool checkaccess);
85 #if defined(ENABLE_STATISTICS)
86 # include "vm/statistics.h"
89 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
90 #define INLINELOG(code) do { if (opt_TraceInlining) { code } } while (0)
92 #define INLINELOG(code)
96 /* global variables ***********************************************************/
98 static s4 interfaceindex; /* sequential numbering of interfaces */
101 #if !USES_NEW_SUBTYPE
102 Mutex *linker_classrenumber_lock;
105 #if defined(__cplusplus)
109 /* private functions **********************************************************/
111 static classinfo *link_class_intern(classinfo *c);
112 static arraydescriptor *link_array(classinfo *c);
113 static void linker_compute_class_values(classinfo *c);
114 static void linker_compute_subclasses(classinfo *c);
115 static bool linker_addinterface(classinfo *c, classinfo *ic);
116 static s4 class_highestinterface(classinfo *c);
119 typedef std::vector<std::pair<java_object_t**, utf*> > deferred_strings_vec_t;
120 static deferred_strings_vec_t deferred_strings;
122 /* linker_init *****************************************************************
124 Initializes the linker subsystem and links classes required for the
127 *******************************************************************************/
129 void linker_preinit(void)
131 TRACESUBSYSTEMINITIALIZATION("linker_preinit");
133 /* Reset interface index. */
137 #if defined(ENABLE_THREADS) && !USES_NEW_SUBTYPE
138 /* create the global mutex */
140 linker_classrenumber_lock = new Mutex();
143 /* Link the most basic classes. */
145 if (!link_class(class_java_lang_Object))
146 vm_abort("linker_preinit: linking java/lang/Object failed");
148 #if defined(ENABLE_JAVASE)
149 if (!link_class(class_java_lang_Cloneable))
150 vm_abort("linker_preinit: linking java/lang/Cloneable failed");
152 if (!link_class(class_java_io_Serializable))
153 vm_abort("linker_preinit: linking java/io/Serializable failed");
158 /* linker_init *****************************************************************
160 Links all classes required in the VM.
162 *******************************************************************************/
164 void linker_init(void)
166 TRACESUBSYSTEMINITIALIZATION("linker_init");
168 /* Link java.lang.Class as first class of the system, because we
169 need it's vftbl for all other classes so we can use a class as
172 if (!link_class(class_java_lang_Class))
173 vm_abort("linker_init: linking java/lang/Class failed");
175 /* Now set the header.vftbl of all classes which were created
176 before java.lang.Class was linked. */
178 class_postset_header_vftbl();
180 /* Link primitive-type wrapping classes. */
182 #if defined(ENABLE_JAVASE)
183 if (!link_class(class_java_lang_Void))
184 vm_abort("linker_init: linking failed");
187 if (!link_class(class_java_lang_Boolean))
188 vm_abort("linker_init: linking failed");
190 if (!link_class(class_java_lang_Byte))
191 vm_abort("linker_init: linking failed");
193 if (!link_class(class_java_lang_Character))
194 vm_abort("linker_init: linking failed");
196 if (!link_class(class_java_lang_Short))
197 vm_abort("linker_init: linking failed");
199 if (!link_class(class_java_lang_Integer))
200 vm_abort("linker_init: linking failed");
202 if (!link_class(class_java_lang_Long))
203 vm_abort("linker_init: linking failed");
205 if (!link_class(class_java_lang_Float))
206 vm_abort("linker_init: linking failed");
208 if (!link_class(class_java_lang_Double))
209 vm_abort("linker_init: linking failed");
211 /* Link important system classes. */
213 if (!link_class(class_java_lang_String))
214 vm_abort("linker_init: linking java/lang/String failed");
216 #if defined(ENABLE_JAVASE)
217 if (!link_class(class_java_lang_ClassLoader))
218 vm_abort("linker_init: linking failed");
220 if (!link_class(class_java_lang_SecurityManager))
221 vm_abort("linker_init: linking failed");
224 if (!link_class(class_java_lang_System))
225 vm_abort("linker_init: linking failed");
227 if (!link_class(class_java_lang_Thread))
228 vm_abort("linker_init: linking failed");
230 #if defined(ENABLE_JAVASE)
231 if (!link_class(class_java_lang_ThreadGroup))
232 vm_abort("linker_init: linking failed");
235 if (!link_class(class_java_lang_Throwable))
236 vm_abort("linker_init: linking failed");
238 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
239 if (!link_class(class_java_lang_VMSystem))
240 vm_abort("linker_init: linking failed");
242 if (!link_class(class_java_lang_VMThread))
243 vm_abort("linker_init: linking failed");
245 if (!link_class(class_java_lang_VMThrowable))
246 vm_abort("linker_init: linking failed");
249 /* Important system exceptions. */
251 if (!link_class(class_java_lang_Exception))
252 vm_abort("linker_init: linking failed");
254 if (!link_class(class_java_lang_ClassNotFoundException))
255 vm_abort("linker_init: linking failed");
257 if (!link_class(class_java_lang_RuntimeException))
258 vm_abort("linker_init: linking failed");
260 /* some classes which may be used more often */
262 #if defined(ENABLE_JAVASE)
263 if (!link_class(class_java_lang_StackTraceElement))
264 vm_abort("linker_init: linking failed");
266 if (!link_class(class_java_lang_reflect_Constructor))
267 vm_abort("linker_init: linking failed");
269 if (!link_class(class_java_lang_reflect_Field))
270 vm_abort("linker_init: linking failed");
272 if (!link_class(class_java_lang_reflect_Method))
273 vm_abort("linker_init: linking failed");
275 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
276 if (!link_class(class_java_lang_reflect_VMConstructor))
277 vm_abort("linker_init: linking failed");
279 if (!link_class(class_java_lang_reflect_VMField))
280 vm_abort("linker_init: linking failed");
282 if (!link_class(class_java_lang_reflect_VMMethod))
283 vm_abort("linker_init: linking failed");
286 if (!link_class(class_java_security_PrivilegedAction))
287 vm_abort("linker_init: linking failed");
289 if (!link_class(class_java_util_Vector))
290 vm_abort("linker_init: linking failed");
292 if (!link_class(class_java_util_HashMap))
293 vm_abort("linker_init: linking failed");
295 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
296 if (!link_class(class_sun_misc_Signal))
297 vm_abort("linker_init: linking failed");
299 if (!link_class(class_sun_reflect_MagicAccessorImpl))
300 vm_abort("linker_init: linking failed");
302 if (!link_class(class_sun_reflect_MethodAccessorImpl))
303 vm_abort("linker_init: linking failed");
305 if (!link_class(class_sun_reflect_ConstructorAccessorImpl))
306 vm_abort("linker_init: linking failed");
309 if (!link_class(arrayclass_java_lang_Object))
310 vm_abort("linker_init: linking failed");
314 /* create pseudo classes used by the typechecker */
316 /* pseudo class for Arraystubs (extends java.lang.Object) */
318 pseudo_class_Arraystub =
319 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
320 pseudo_class_Arraystub->state |= CLASS_LOADED;
321 pseudo_class_Arraystub->super = class_java_lang_Object;
323 #if defined(ENABLE_JAVASE)
325 pseudo_class_Arraystub->interfacescount = 2;
326 pseudo_class_Arraystub->interfaces = MNEW(classinfo*, 2);
327 pseudo_class_Arraystub->interfaces[0] = class_java_lang_Cloneable;
328 pseudo_class_Arraystub->interfaces[1] = class_java_io_Serializable;
330 #elif defined(ENABLE_JAVAME_CLDC1_1)
332 pseudo_class_Arraystub->interfacescount = 0;
333 pseudo_class_Arraystub->interfaces = NULL;
336 # error unknown Java configuration
339 if (!classcache_store_unique(pseudo_class_Arraystub))
340 vm_abort("linker_init: could not cache pseudo_class_Arraystub");
342 if (!link_class(pseudo_class_Arraystub))
343 vm_abort("linker_init: linking pseudo_class_Arraystub failed");
345 /* pseudo class representing the null type */
347 pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
348 pseudo_class_Null->state |= CLASS_LOADED;
349 pseudo_class_Null->super = class_java_lang_Object;
351 if (!classcache_store_unique(pseudo_class_Null))
352 vm_abort("linker_init: could not cache pseudo_class_Null");
354 if (!link_class(pseudo_class_Null))
355 vm_abort("linker_init: linking failed");
357 /* pseudo class representing new uninitialized objects */
359 pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
360 pseudo_class_New->state |= CLASS_LOADED;
361 pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
362 pseudo_class_New->super = class_java_lang_Object;
364 if (!classcache_store_unique(pseudo_class_New))
365 vm_abort("linker_init: could not cache pseudo_class_New");
367 /* Correct vftbl-entries (retarded loading and linking of class
368 java/lang/String). */
370 stringtable_update();
374 /* link_class ******************************************************************
376 Wrapper function for link_class_intern to ease monitor enter/exit
377 and exception handling.
379 *******************************************************************************/
381 classinfo *link_class(classinfo *c)
384 #if defined(ENABLE_RT_TIMING)
385 struct timespec time_start, time_end;
388 RT_TIMING_GET_TIME(time_start);
391 exceptions_throw_nullpointerexception();
395 LOCK_MONITOR_ENTER(c);
397 /* Maybe the class is currently linking or is already linked.*/
399 if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
400 LOCK_MONITOR_EXIT(c);
405 #if defined(ENABLE_STATISTICS)
408 if (opt_getcompilingtime)
409 compilingtime_stop();
411 if (opt_getloadingtime)
415 /* call the internal function */
417 r = link_class_intern(c);
419 /* If return value is NULL, we had a problem and the class is not
423 c->state &= ~CLASS_LINKING;
425 #if defined(ENABLE_STATISTICS)
428 if (opt_getloadingtime)
431 if (opt_getcompilingtime)
432 compilingtime_start();
435 LOCK_MONITOR_EXIT(c);
437 RT_TIMING_GET_TIME(time_end);
439 RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
441 // Hook point just after a class was linked.
442 if (!Hook::class_linked(r))
449 /* linker_overwrite_method *****************************************************
451 Overwrite a method with another one, update method flags and check
455 mg................the general method being overwritten
456 ms................the overwriting (more specialized) method
457 wl................worklist where to add invalidated methods
460 true..............everything ok
461 false.............an exception has been thrown
463 *******************************************************************************/
465 static bool linker_overwrite_method(methodinfo *mg,
467 method_worklist **wl)
475 /* overriding a final method is illegal */
477 if (mg->flags & ACC_FINAL) {
478 exceptions_throw_verifyerror(mg, "Overriding final method");
482 /* method ms overwrites method mg */
484 #if defined(ENABLE_VERIFIER)
485 /* Add loading constraints (for the more general types of method mg). */
486 /* Not for <init>, as it is not invoked virtually. */
488 if ((ms->name != utf_init)
489 && !classcache_add_constraints_for_params(
490 cs->classloader, cg->classloader, mg))
496 /* inherit the vftbl index, and record the overwriting */
498 ms->vftblindex = mg->vftblindex;
501 /* update flags and check assumptions */
502 /* <init> methods are a special case, as they are never dispatched dynamically */
504 if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
507 #if defined(ENABLE_TLH)
508 if (mg->flags & ACC_METHOD_MONOMORPHY_USED) {
509 printf("%s/%s is evil! the siner is %s/%s\n", mg->clazz->name->text, mg->name->text,
510 ms->clazz->name->text, ms->name->text);
511 ms->flags |= ACC_METHOD_PARENT_MONOMORPHY_USED;
515 if (mg->flags & ACC_METHOD_IMPLEMENTED) {
516 /* this adds another implementation */
518 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
520 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
522 method_break_assumption_monomorphic(mg, wl);
525 /* this is the first implementation */
527 mg->flags |= ACC_METHOD_IMPLEMENTED;
529 INLINELOG( printf("becomes implemented: "); method_println(mg); );
534 } while (mg != NULL);
542 /* build_display ***************************************************************
544 Builds the entire display for a class. This entails filling the fixed part
545 as well as allocating and initializing the overflow part.
547 See Cliff Click and John Rose: Fast subtype checking in the Hotspot JVM.
549 *******************************************************************************/
551 static classinfo *build_display(classinfo *c)
559 if (c->vftbl->arraydesc) {
560 arraydescriptor *a = c->vftbl->arraydesc;
561 if (a->elementvftbl && a->elementvftbl->clazz->super) {
562 classinfo *cls = a->elementvftbl->clazz->super;
564 for (n=0; n<a->dimension; n++)
565 cls = class_array_of(cls, true);
569 if (a->componentvftbl && a->elementvftbl) {
570 super = a->componentvftbl->clazz;
574 /* Normal classes. */
578 if (!link_class(super))
580 depth = super->vftbl->subtype_depth + 1;
582 /* java.lang.Object doesn't have a super class. */
585 /* Now copy super's display, append c->vftbl and initialize the remaining fields. */
586 if (depth >= DISPLAY_SIZE) {
587 c->vftbl->subtype_overflow = MNEW(vftbl_t *, depth - DISPLAY_SIZE + 1);
588 #if defined(ENABLE_STATISTICS)
590 count_vftbl_len += sizeof(vftbl_t*) * (depth - DISPLAY_SIZE + 1);
592 memcpy(c->vftbl->subtype_overflow, super->vftbl->subtype_overflow, sizeof(vftbl_t*) * (depth - DISPLAY_SIZE));
593 c->vftbl->subtype_overflow[depth - DISPLAY_SIZE] = c->vftbl;
594 depth_fixed = DISPLAY_SIZE;
598 c->vftbl->subtype_display[depth] = c->vftbl;
602 memcpy(c->vftbl->subtype_display, super->vftbl->subtype_display, sizeof(vftbl_t*) * depth_fixed);
603 for (i=depth_fixed+1; i<=DISPLAY_SIZE; i++)
604 c->vftbl->subtype_display[i] = NULL;
605 c->vftbl->subtype_offset = OFFSET(vftbl_t, subtype_display[0]) + sizeof(vftbl_t*) * depth_fixed;
606 c->vftbl->subtype_depth = depth;
612 /* link_class_intern ***********************************************************
614 Tries to link a class. The function calculates the length in bytes
615 that an instance of this class requires as well as the VTBL for
616 methods and interface methods.
618 *******************************************************************************/
620 static classinfo *link_class_intern(classinfo *c)
622 classinfo *super; /* super class */
623 classinfo *tc; /* temporary class variable */
624 s4 supervftbllength; /* vftbllegnth of super class */
625 s4 vftbllength; /* vftbllength of current class */
626 s4 interfacetablelength; /* interface table length */
627 vftbl_t *v; /* vftbl of current class */
628 s4 i; /* interface/method/field counter */
629 arraydescriptor *arraydesc; /* descriptor for array classes */
630 method_worklist *worklist; /* worklist for recompilation */
631 #if defined(ENABLE_RT_TIMING)
632 struct timespec time_start, time_resolving, time_compute_vftbl,
633 time_abstract, time_compute_iftbl, time_fill_vftbl,
634 time_offsets, time_fill_iftbl, time_finalizer,
638 RT_TIMING_GET_TIME(time_start);
642 /* the class must be loaded */
644 /* XXX should this be a specific exception? */
645 assert(c->state & CLASS_LOADED);
647 /* This is check in link_class. */
649 assert(!(c->state & CLASS_LINKED));
651 /* cache the self-reference of this class */
652 /* we do this for cases where the defining loader of the class */
653 /* has not yet been recorded as an initiating loader for the class */
654 /* this is needed so subsequent code can assume that self-refs */
655 /* will always resolve lazily */
656 /* No need to do it for the bootloader - it is always registered */
657 /* as initiating loader for the classes it loads. */
659 classcache_store(c->classloader,c,false);
661 /* this class is currently linking */
663 c->state |= CLASS_LINKING;
668 /* Link the super interfaces. */
670 for (i = 0; i < c->interfacescount; i++) {
671 tc = c->interfaces[i];
673 if (!(tc->state & CLASS_LINKED))
678 /* check super class */
682 /* Check for java/lang/Object. */
684 if (c->super == NULL) {
686 c->instancesize = sizeof(java_object_t);
688 vftbllength = supervftbllength = 0;
693 /* Get super class. */
697 /* Link the super class if necessary. */
699 if (!(super->state & CLASS_LINKED))
700 if (!link_class(super))
703 /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
706 c->flags |= (super->flags &
707 (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
709 /* handle array classes */
711 if (c->name->text[0] == '[')
712 if (!(arraydesc = link_array(c)))
715 if (c->flags & ACC_INTERFACE)
716 c->index = interfaceindex++;
718 c->index = super->index + 1;
720 c->instancesize = super->instancesize;
722 vftbllength = supervftbllength = super->vftbl->vftbllength;
724 c->finalizer = super->finalizer;
726 RT_TIMING_GET_TIME(time_resolving);
729 /* compute vftbl length */
731 for (i = 0; i < c->methodscount; i++) {
732 methodinfo *m = &(c->methods[i]);
734 if (!(m->flags & ACC_STATIC)) { /* is instance method */
740 for (j = 0; j < tc->methodscount; j++) {
741 if (method_canoverwrite(m, &(tc->methods[j]))) {
742 if (tc->methods[j].flags & ACC_PRIVATE)
743 goto notfoundvftblindex;
745 /* package-private methods in other packages */
746 /* must not be overridden */
747 /* (see Java Language Specification 8.4.8.1) */
748 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED))
749 && !SAME_PACKAGE(c,tc) )
751 goto notfoundvftblindex;
754 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
757 goto foundvftblindex;
765 m->vftblindex = (vftbllength++);
770 RT_TIMING_GET_TIME(time_compute_vftbl);
773 /* Check all interfaces of an abstract class (maybe be an
774 interface too) for unimplemented methods. Such methods are
775 called miranda-methods and are marked with the ACC_MIRANDA
776 flag. VMClass.getDeclaredMethods does not return such
779 if (c->flags & ACC_ABSTRACT) {
782 s4 abstractmethodscount;
786 abstractmethodscount = 0;
788 /* check all interfaces of the abstract class */
790 for (i = 0; i < c->interfacescount; i++) {
791 ic = c->interfaces[i];
793 for (j = 0; j < ic->methodscount; j++) {
794 im = &(ic->methods[j]);
796 /* skip `<clinit>' and `<init>' */
798 if ((im->name == utf_clinit) || (im->name == utf_init))
801 for (tc = c; tc != NULL; tc = tc->super) {
802 for (k = 0; k < tc->methodscount; k++) {
803 if (method_canoverwrite(im, &(tc->methods[k])))
804 goto noabstractmethod;
808 abstractmethodscount++;
815 if (abstractmethodscount > 0) {
818 /* reallocate methods memory */
820 c->methods = (methodinfo*) MREALLOC(c->methods, methodinfo, c->methodscount,
821 c->methodscount + abstractmethodscount);
823 for (i = 0; i < c->interfacescount; i++) {
824 ic = c->interfaces[i];
826 for (j = 0; j < ic->methodscount; j++) {
827 im = &(ic->methods[j]);
829 /* skip `<clinit>' and `<init>' */
831 if ((im->name == utf_clinit) || (im->name == utf_init))
834 for (tc = c; tc != NULL; tc = tc->super) {
835 for (k = 0; k < tc->methodscount; k++) {
836 if (method_canoverwrite(im, &(tc->methods[k])))
837 goto noabstractmethod2;
841 /* Copy the method found into the new c->methods
842 array and tag it as miranda-method. */
844 am = &(c->methods[c->methodscount]);
847 MCOPY(am, im, methodinfo, 1);
849 am->vftblindex = (vftbllength++);
851 am->flags |= ACC_MIRANDA;
859 RT_TIMING_GET_TIME(time_abstract);
862 #if defined(ENABLE_STATISTICS)
865 sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
868 /* compute interfacetable length */
870 interfacetablelength = 0;
872 for (tc = c; tc != NULL; tc = tc->super) {
873 for (i = 0; i < tc->interfacescount; i++) {
874 s4 h = class_highestinterface(tc->interfaces[i]) + 1;
876 if (h > interfacetablelength)
877 interfacetablelength = h;
880 RT_TIMING_GET_TIME(time_compute_iftbl);
882 /* allocate virtual function table */
884 v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
885 sizeof(methodptr) * (vftbllength - 1) +
886 sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
887 v = (vftbl_t *) (((methodptr *) v) +
888 (interfacetablelength - 1) * (interfacetablelength > 1));
892 v->vftbllength = vftbllength;
893 v->interfacetablelength = interfacetablelength;
894 v->arraydesc = arraydesc;
896 /* store interface index in vftbl */
898 if (c->flags & ACC_INTERFACE)
899 v->baseval = -(c->index);
901 /* copy virtual function table of super class */
903 for (i = 0; i < supervftbllength; i++)
904 v->table[i] = super->vftbl->table[i];
906 /* Fill the remaining vftbl slots with the AbstractMethodError
907 stub (all after the super class slots, because they are already
910 for (; i < vftbllength; i++) {
911 #if defined(ENABLE_JIT)
912 # if defined(ENABLE_INTRP)
914 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
917 v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
919 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
923 /* add method stubs into virtual function table */
925 for (i = 0; i < c->methodscount; i++) {
926 methodinfo *m = &(c->methods[i]);
928 assert(m->stubroutine == NULL);
930 /* Don't create a compiler stub for abstract methods as they
931 throw an AbstractMethodError with the default stub in the
932 vftbl. This entry is simply copied by sub-classes. */
934 if (m->flags & ACC_ABSTRACT)
937 #if defined(ENABLE_JIT)
938 # if defined(ENABLE_INTRP)
940 m->stubroutine = intrp_createcompilerstub(m);
943 m->stubroutine = (u1*) CompilerStub::generate(m);
945 m->stubroutine = intrp_createcompilerstub(m);
948 /* static methods are not in the vftbl */
950 if (m->flags & ACC_STATIC)
953 /* insert the stubroutine into the vftbl */
955 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
957 RT_TIMING_GET_TIME(time_fill_vftbl);
959 /* compute instance size and offset of each field */
961 for (i = 0; i < c->fieldscount; i++) {
963 fieldinfo *f = &(c->fields[i]);
965 if (!(f->flags & ACC_STATIC)) {
966 dsize = descriptor_typesize(f->parseddesc);
967 c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
968 f->offset = c->instancesize;
969 c->instancesize += dsize;
972 RT_TIMING_GET_TIME(time_offsets);
974 /* initialize interfacetable and interfacevftbllength */
976 v->interfacevftbllength = MNEW(s4, interfacetablelength);
978 #if defined(ENABLE_STATISTICS)
980 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
983 for (i = 0; i < interfacetablelength; i++) {
984 v->interfacevftbllength[i] = 0;
985 v->interfacetable[-i] = NULL;
990 for (tc = c; tc != NULL; tc = tc->super)
991 for (i = 0; i < tc->interfacescount; i++)
992 if (!linker_addinterface(c, tc->interfaces[i]))
995 RT_TIMING_GET_TIME(time_fill_iftbl);
997 /* add finalizer method (not for java.lang.Object) */
1002 fi = class_findmethod(c, utf_finalize, utf_void__void);
1005 if (!(fi->flags & ACC_STATIC))
1008 RT_TIMING_GET_TIME(time_finalizer);
1012 linker_compute_subclasses(c);
1014 /* FIXME: this is completely useless now */
1015 RT_TIMING_GET_TIME(time_subclasses);
1017 #if USES_NEW_SUBTYPE
1018 if (!build_display(c))
1022 /* revert the linking state and class is linked */
1024 c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1026 /* check worklist */
1028 /* XXX must this also be done in case of exception? */
1030 while (worklist != NULL) {
1031 method_worklist *wi = worklist;
1033 worklist = worklist->next;
1035 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1036 jit_invalidate_code(wi->m);
1038 /* XXX put worklist into dump memory? */
1039 FREE(wi, method_worklist);
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_t, 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 exceptions_throw_illegalargumentexception();
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_t,data);
1147 desc->componentsize = sizeof(u1);
1151 desc->arraytype = ARRAYTYPE_BYTE;
1152 desc->dataoffset = OFFSET(java_bytearray_t,data);
1153 desc->componentsize = sizeof(u1);
1157 desc->arraytype = ARRAYTYPE_CHAR;
1158 desc->dataoffset = OFFSET(java_chararray_t,data);
1159 desc->componentsize = sizeof(u2);
1163 desc->arraytype = ARRAYTYPE_DOUBLE;
1164 desc->dataoffset = OFFSET(java_doublearray_t,data);
1165 desc->componentsize = sizeof(double);
1169 desc->arraytype = ARRAYTYPE_FLOAT;
1170 desc->dataoffset = OFFSET(java_floatarray_t,data);
1171 desc->componentsize = sizeof(float);
1175 desc->arraytype = ARRAYTYPE_INT;
1176 desc->dataoffset = OFFSET(java_intarray_t,data);
1177 desc->componentsize = sizeof(s4);
1181 desc->arraytype = ARRAYTYPE_LONG;
1182 desc->dataoffset = OFFSET(java_longarray_t,data);
1183 desc->componentsize = sizeof(s8);
1187 desc->arraytype = ARRAYTYPE_SHORT;
1188 desc->dataoffset = OFFSET(java_shortarray_t,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;
1206 /* linker_create_string_later **************************************************
1208 A hack so we can initialize java.lang.String objects during initialization.
1210 *******************************************************************************/
1211 void linker_create_string_later(java_object_t **a, utf *u)
1213 deferred_strings.push_back(std::make_pair(a, u));
1216 void linker_initialize_deferred_strings()
1218 deferred_strings_vec_t::const_iterator it = deferred_strings.begin();
1219 for (; it != deferred_strings.end(); ++it)
1220 *it->first = literalstring_new(it->second);
1221 deferred_strings.clear();
1225 /* linker_compute_subclasses ***************************************************
1229 ATTENTION: DO NOT REMOVE ANY OF THE LOCKING MECHANISMS BELOW:
1230 This function needs to take the class renumber lock and stop the
1231 world during class renumbering. The lock is used in C code which
1232 is not that performance critical. Whereas JIT code uses critical
1233 sections to atomically access the class values.
1235 *******************************************************************************/
1237 static void linker_compute_subclasses(classinfo *c)
1240 LOCK_CLASSRENUMBER_LOCK;
1242 if (!(c->flags & ACC_INTERFACE)) {
1245 #if USES_NEW_SUBTYPE
1246 c->vftbl->baseval = 1; /* so it does not look like an interface */
1250 if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
1251 c->nextsub = c->super->sub;
1257 #if !USES_NEW_SUBTYPE
1258 /* compute class values */
1260 linker_compute_class_values(class_java_lang_Object);
1263 UNLOCK_CLASSRENUMBER_LOCK;
1268 /* linker_compute_class_values *************************************************
1272 *******************************************************************************/
1274 static void linker_compute_class_values(classinfo *c)
1278 c->vftbl->baseval = ++classvalue;
1283 linker_compute_class_values(subs);
1285 subs = subs->nextsub;
1288 c->vftbl->diffval = classvalue - c->vftbl->baseval;
1292 /* linker_addinterface *********************************************************
1294 Is needed by link_class for adding a VTBL to a class. All
1295 interfaces implemented by ic are added as well.
1298 true.........everything ok
1299 false........an exception has been thrown
1301 *******************************************************************************/
1303 static bool linker_addinterface(classinfo *c, classinfo *ic)
1314 if (i >= v->interfacetablelength)
1315 vm_abort("Internal error: interfacetable overflow");
1317 /* if this interface has already been added, return immediately */
1319 if (v->interfacetable[-i] != NULL)
1322 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1323 v->interfacevftbllength[i] = 1;
1324 v->interfacetable[-i] = MNEW(methodptr, 1);
1325 v->interfacetable[-i][0] = NULL;
1328 v->interfacevftbllength[i] = ic->methodscount;
1329 v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1331 #if defined(ENABLE_STATISTICS)
1333 count_vftbl_len += sizeof(methodptr) *
1334 (ic->methodscount + (ic->methodscount == 0));
1337 for (j = 0; j < ic->methodscount; j++) {
1338 for (sc = c; sc != NULL; sc = sc->super) {
1339 for (k = 0; k < sc->methodscount; k++) {
1340 m = &(sc->methods[k]);
1342 if (method_canoverwrite(m, &(ic->methods[j]))) {
1343 /* method m overwrites the (abstract) method */
1344 #if defined(ENABLE_VERIFIER)
1345 /* Add loading constraints (for the more
1346 general types of the method
1348 if (!classcache_add_constraints_for_params(
1349 c->classloader, ic->classloader,
1356 /* XXX taken from gcj */
1357 /* check for ACC_STATIC: IncompatibleClassChangeError */
1359 /* check for !ACC_PUBLIC: IllegalAccessError */
1361 /* check for ACC_ABSTRACT: AbstracMethodError,
1362 not sure about that one */
1364 v->interfacetable[-i][j] = v->table[m->vftblindex];
1370 /* If no method was found, insert the AbstractMethodError
1373 #if defined(ENABLE_JIT)
1374 # if defined(ENABLE_INTRP)
1376 v->interfacetable[-i][j] =
1377 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1380 v->interfacetable[-i][j] =
1381 (methodptr) (ptrint) &asm_abstractmethoderror;
1383 v->interfacetable[-i][j] =
1384 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1392 /* add superinterfaces of this interface */
1394 for (j = 0; j < ic->interfacescount; j++)
1395 if (!linker_addinterface(c, ic->interfaces[j]))
1404 /* class_highestinterface ******************************************************
1406 Used by the function link_class to determine the amount of memory
1407 needed for the interface table.
1409 *******************************************************************************/
1411 static s4 class_highestinterface(classinfo *c)
1417 /* check for ACC_INTERFACE bit already done in link_class_intern */
1421 for (i = 0; i < c->interfacescount; i++) {
1422 h2 = class_highestinterface(c->interfaces[i]);
1431 #if defined(__cplusplus)
1436 * These are local overrides for various environment variables in Emacs.
1437 * Please do not remove this and leave it at the end of the file, where
1438 * Emacs will automagically detect them.
1439 * ---------------------------------------------------------------------
1442 * indent-tabs-mode: t
1446 * vim:noexpandtab:sw=4:ts=4: