* Merged with b0dbbda15813.
[cacao.git] / src / vm / linker.cpp
1 /* src/vm/linker.c - class linker functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "vm/types.h"
32
33 #include "mm/memory.h"
34
35 #include "native/native.hpp"
36
37 #include "threads/lock.hpp"
38 #include "threads/mutex.hpp"
39
40 #include "toolbox/logging.h"
41
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"
53 #include "vm/vm.hpp"
54
55 #include "vm/jit/asmpart.h"
56 #include "vm/jit/stubs.hpp"
57
58
59 /* debugging macros ***********************************************************/
60
61 #if !defined(NDEBUG)
62 # define TRACELINKCLASS(c) \
63     do { \
64         if (opt_TraceLinkClass) { \
65             log_start(); \
66             log_print("[Linking "); \
67             class_print((c)); \
68             log_print("]"); \
69             log_finish(); \
70         } \
71     } while (0)
72 #else
73 # define TRACELINKCLASS(c)
74 #endif
75
76
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);
80
81 #if defined(ENABLE_STATISTICS)
82 # include "vm/statistics.h"
83 #endif
84
85 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
86 #define INLINELOG(code)  do { if (opt_TraceInlining) { code } } while (0)
87 #else
88 #define INLINELOG(code)
89 #endif
90
91
92 /* global variables ***********************************************************/
93
94 static s4 interfaceindex;       /* sequential numbering of interfaces         */
95 static s4 classvalue;
96
97 Mutex *linker_classrenumber_mutex;
98
99 #if defined(__cplusplus)
100 extern "C" {
101 #endif
102
103 /* private functions **********************************************************/
104
105 static classinfo *link_class_intern(classinfo *c);
106 static arraydescriptor *link_array(classinfo *c);
107 static void linker_compute_class_values(classinfo *c);
108 static void linker_compute_subclasses(classinfo *c);
109 static bool linker_addinterface(classinfo *c, classinfo *ic);
110 static s4 class_highestinterface(classinfo *c);
111
112
113 /* linker_init *****************************************************************
114
115    Initializes the linker subsystem and links classes required for the
116    primitive table.
117
118 *******************************************************************************/
119
120 void linker_preinit(void)
121 {
122         TRACESUBSYSTEMINITIALIZATION("linker_preinit");
123
124         /* Reset interface index. */
125
126         interfaceindex = 0;
127
128 #if defined(ENABLE_THREADS)
129         /* create the global mutex */
130
131         linker_classrenumber_mutex = new Mutex();
132 #endif
133
134         /* Link the most basic classes. */
135
136         if (!link_class(class_java_lang_Object))
137                 vm_abort("linker_preinit: linking java/lang/Object failed");
138
139 #if defined(ENABLE_JAVASE)
140         if (!link_class(class_java_lang_Cloneable))
141                 vm_abort("linker_preinit: linking java/lang/Cloneable failed");
142
143         if (!link_class(class_java_io_Serializable))
144                 vm_abort("linker_preinit: linking java/io/Serializable failed");
145 #endif
146 }
147
148
149 /* linker_init *****************************************************************
150
151    Links all classes required in the VM.
152
153 *******************************************************************************/
154
155 void linker_init(void)
156 {
157         TRACESUBSYSTEMINITIALIZATION("linker_init");
158
159         /* Link java.lang.Class as first class of the system, because we
160        need it's vftbl for all other classes so we can use a class as
161        object. */
162
163         if (!link_class(class_java_lang_Class))
164                 vm_abort("linker_init: linking java/lang/Class failed");
165
166         /* Now set the header.vftbl of all classes which were created
167        before java.lang.Class was linked. */
168
169         class_postset_header_vftbl();
170
171         /* Link primitive-type wrapping classes. */
172
173 #if defined(ENABLE_JAVASE)
174         if (!link_class(class_java_lang_Void))
175                 vm_abort("linker_init: linking failed");
176 #endif
177
178         if (!link_class(class_java_lang_Boolean))
179                 vm_abort("linker_init: linking failed");
180
181         if (!link_class(class_java_lang_Byte))
182                 vm_abort("linker_init: linking failed");
183
184         if (!link_class(class_java_lang_Character))
185                 vm_abort("linker_init: linking failed");
186
187         if (!link_class(class_java_lang_Short))
188                 vm_abort("linker_init: linking failed");
189
190         if (!link_class(class_java_lang_Integer))
191                 vm_abort("linker_init: linking failed");
192
193         if (!link_class(class_java_lang_Long))
194                 vm_abort("linker_init: linking failed");
195
196         if (!link_class(class_java_lang_Float))
197                 vm_abort("linker_init: linking failed");
198
199         if (!link_class(class_java_lang_Double))
200                 vm_abort("linker_init: linking failed");
201
202         /* Link important system classes. */
203
204         if (!link_class(class_java_lang_String))
205                 vm_abort("linker_init: linking java/lang/String failed");
206
207 #if defined(ENABLE_JAVASE)
208         if (!link_class(class_java_lang_ClassLoader))
209                 vm_abort("linker_init: linking failed");
210
211         if (!link_class(class_java_lang_SecurityManager))
212                 vm_abort("linker_init: linking failed");
213 #endif
214
215         if (!link_class(class_java_lang_System))
216                 vm_abort("linker_init: linking failed");
217
218         if (!link_class(class_java_lang_Thread))
219                 vm_abort("linker_init: linking failed");
220
221 #if defined(ENABLE_JAVASE)
222         if (!link_class(class_java_lang_ThreadGroup))
223                 vm_abort("linker_init: linking failed");
224 #endif
225
226         if (!link_class(class_java_lang_Throwable))
227                 vm_abort("linker_init: linking failed");
228
229 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
230         if (!link_class(class_java_lang_VMSystem))
231                 vm_abort("linker_init: linking failed");
232
233         if (!link_class(class_java_lang_VMThread))
234                 vm_abort("linker_init: linking failed");
235
236         if (!link_class(class_java_lang_VMThrowable))
237                 vm_abort("linker_init: linking failed");
238 #endif
239
240         /* Important system exceptions. */
241
242         if (!link_class(class_java_lang_Exception))
243                 vm_abort("linker_init: linking failed");
244
245         if (!link_class(class_java_lang_ClassNotFoundException))
246                 vm_abort("linker_init: linking failed");
247
248         if (!link_class(class_java_lang_RuntimeException))
249                 vm_abort("linker_init: linking failed");
250
251         /* some classes which may be used more often */
252
253 #if defined(ENABLE_JAVASE)
254         if (!link_class(class_java_lang_StackTraceElement))
255                 vm_abort("linker_init: linking failed");
256
257         if (!link_class(class_java_lang_reflect_Constructor))
258                 vm_abort("linker_init: linking failed");
259
260         if (!link_class(class_java_lang_reflect_Field))
261                 vm_abort("linker_init: linking failed");
262
263         if (!link_class(class_java_lang_reflect_Method))
264                 vm_abort("linker_init: linking failed");
265
266 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
267         if (!link_class(class_java_lang_reflect_VMConstructor))
268                 vm_abort("linker_init: linking failed");
269
270         if (!link_class(class_java_lang_reflect_VMField))
271                 vm_abort("linker_init: linking failed");
272
273         if (!link_class(class_java_lang_reflect_VMMethod))
274                 vm_abort("linker_init: linking failed");
275 # endif
276
277         if (!link_class(class_java_security_PrivilegedAction))
278                 vm_abort("linker_init: linking failed");
279
280         if (!link_class(class_java_util_Vector))
281                 vm_abort("linker_init: linking failed");
282
283         if (!link_class(class_java_util_HashMap))
284                 vm_abort("linker_init: linking failed");
285
286 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
287         if (!link_class(class_sun_misc_Signal))
288                 vm_abort("linker_init: linking failed");
289
290         if (!link_class(class_sun_reflect_MagicAccessorImpl))
291                 vm_abort("linker_init: linking failed");
292 # endif
293
294         if (!link_class(arrayclass_java_lang_Object))
295                 vm_abort("linker_init: linking failed");
296 #endif
297
298
299         /* create pseudo classes used by the typechecker */
300
301     /* pseudo class for Arraystubs (extends java.lang.Object) */
302
303         pseudo_class_Arraystub                   =
304                 class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
305         pseudo_class_Arraystub->state           |= CLASS_LOADED;
306         pseudo_class_Arraystub->super            = class_java_lang_Object;
307
308 #if defined(ENABLE_JAVASE)
309
310         pseudo_class_Arraystub->interfacescount  = 2;
311         pseudo_class_Arraystub->interfaces       = MNEW(classinfo*, 2);
312         pseudo_class_Arraystub->interfaces[0]    = class_java_lang_Cloneable;
313         pseudo_class_Arraystub->interfaces[1]    = class_java_io_Serializable;
314
315 #elif defined(ENABLE_JAVAME_CLDC1_1)
316
317         pseudo_class_Arraystub->interfacescount    = 0;
318         pseudo_class_Arraystub->interfaces         = NULL;
319
320 #else
321 # error unknown Java configuration
322 #endif
323
324         if (!classcache_store_unique(pseudo_class_Arraystub))
325                 vm_abort("linker_init: could not cache pseudo_class_Arraystub");
326
327         if (!link_class(pseudo_class_Arraystub))
328                 vm_abort("linker_init: linking pseudo_class_Arraystub failed");
329
330         /* pseudo class representing the null type */
331
332         pseudo_class_Null         = class_create_classinfo(utf_new_char("$NULL$"));
333         pseudo_class_Null->state |= CLASS_LOADED;
334         pseudo_class_Null->super  = class_java_lang_Object;
335
336         if (!classcache_store_unique(pseudo_class_Null))
337                 vm_abort("linker_init: could not cache pseudo_class_Null");
338
339         if (!link_class(pseudo_class_Null))
340                 vm_abort("linker_init: linking failed");
341
342         /* pseudo class representing new uninitialized objects */
343     
344         pseudo_class_New         = class_create_classinfo(utf_new_char("$NEW$"));
345         pseudo_class_New->state |= CLASS_LOADED;
346         pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
347         pseudo_class_New->super  = class_java_lang_Object;
348
349         if (!classcache_store_unique(pseudo_class_New))
350                 vm_abort("linker_init: could not cache pseudo_class_New");
351
352         /* Correct vftbl-entries (retarded loading and linking of class
353            java/lang/String). */
354
355         stringtable_update();
356 }
357
358
359 /* link_class ******************************************************************
360
361    Wrapper function for link_class_intern to ease monitor enter/exit
362    and exception handling.
363
364 *******************************************************************************/
365
366 classinfo *link_class(classinfo *c)
367 {
368         classinfo *r;
369 #if defined(ENABLE_RT_TIMING)
370         struct timespec time_start, time_end;
371 #endif
372
373         RT_TIMING_GET_TIME(time_start);
374
375         if (c == NULL) {
376                 exceptions_throw_nullpointerexception();
377                 return NULL;
378         }
379
380         LOCK_MONITOR_ENTER(c);
381
382         /* Maybe the class is currently linking or is already linked.*/
383
384         if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
385                 LOCK_MONITOR_EXIT(c);
386
387                 return c;
388         }
389
390 #if defined(ENABLE_STATISTICS)
391         /* measure time */
392
393         if (opt_getcompilingtime)
394                 compilingtime_stop();
395
396         if (opt_getloadingtime)
397                 loadingtime_start();
398 #endif
399
400         /* call the internal function */
401
402         r = link_class_intern(c);
403
404         /* If return value is NULL, we had a problem and the class is not
405            linked. */
406
407         if (r == NULL)
408                 c->state &= ~CLASS_LINKING;
409
410 #if defined(ENABLE_STATISTICS)
411         /* measure time */
412
413         if (opt_getloadingtime)
414                 loadingtime_stop();
415
416         if (opt_getcompilingtime)
417                 compilingtime_start();
418 #endif
419
420         LOCK_MONITOR_EXIT(c);
421
422         RT_TIMING_GET_TIME(time_end);
423
424         RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
425
426         return r;
427 }
428
429
430 /* linker_overwrite_method *****************************************************
431
432    Overwrite a method with another one, update method flags and check
433    assumptions.
434
435    IN:
436       mg................the general method being overwritten
437           ms................the overwriting (more specialized) method
438           wl................worklist where to add invalidated methods
439
440    RETURN VALUE:
441       true..............everything ok
442           false.............an exception has been thrown
443
444 *******************************************************************************/
445
446 static bool linker_overwrite_method(methodinfo *mg,
447                                                                         methodinfo *ms,
448                                                                         method_worklist **wl)
449 {
450         classinfo *cg;
451         classinfo *cs;
452
453         cg = mg->clazz;
454         cs = ms->clazz;
455
456         /* overriding a final method is illegal */
457
458         if (mg->flags & ACC_FINAL) {
459                 exceptions_throw_verifyerror(mg, "Overriding final method");
460                 return false;
461         }
462
463         /* method ms overwrites method mg */
464
465 #if defined(ENABLE_VERIFIER)
466         /* Add loading constraints (for the more general types of method mg). */
467         /* Not for <init>, as it is not invoked virtually.                    */
468
469         if ((ms->name != utf_init)
470                         && !classcache_add_constraints_for_params(
471                                 cs->classloader, cg->classloader, mg))
472         {
473                 return false;
474         }
475 #endif
476
477         /* inherit the vftbl index, and record the overwriting */
478
479         ms->vftblindex = mg->vftblindex;
480         ms->overwrites = mg;
481
482         /* update flags and check assumptions */
483         /* <init> methods are a special case, as they are never dispatched dynamically */
484
485         if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
486                 do {
487
488 #if defined(ENABLE_TLH)
489                         if (mg->flags & ACC_METHOD_MONOMORPHY_USED) {
490                                 printf("%s/%s is evil! the siner is %s/%s\n", mg->clazz->name->text, mg->name->text,
491                                         ms->clazz->name->text, ms->name->text);
492                                 ms->flags |= ACC_METHOD_PARENT_MONOMORPHY_USED;                                 
493                         }
494 #endif
495
496                         if (mg->flags & ACC_METHOD_IMPLEMENTED) {
497                                 /* this adds another implementation */
498
499                                 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
500
501                                 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
502
503                                 method_break_assumption_monomorphic(mg, wl);
504                         }
505                         else {
506                                 /* this is the first implementation */
507
508                                 mg->flags |= ACC_METHOD_IMPLEMENTED;
509
510                                 INLINELOG( printf("becomes implemented: "); method_println(mg); );
511                         }
512
513                         ms = mg;
514                         mg = mg->overwrites;
515                 } while (mg != NULL);
516         }
517
518         return true;
519 }
520
521
522 /* link_class_intern ***********************************************************
523
524    Tries to link a class. The function calculates the length in bytes
525    that an instance of this class requires as well as the VTBL for
526    methods and interface methods.
527         
528 *******************************************************************************/
529
530 static classinfo *link_class_intern(classinfo *c)
531 {
532         classinfo *super;             /* super class                              */
533         classinfo *tc;                /* temporary class variable                 */
534         s4 supervftbllength;          /* vftbllegnth of super class               */
535         s4 vftbllength;               /* vftbllength of current class             */
536         s4 interfacetablelength;      /* interface table length                   */
537         vftbl_t *v;                   /* vftbl of current class                   */
538         s4 i;                         /* interface/method/field counter           */
539         arraydescriptor *arraydesc;   /* descriptor for array classes             */
540         method_worklist *worklist;    /* worklist for recompilation               */
541 #if defined(ENABLE_RT_TIMING)
542         struct timespec time_start, time_resolving, time_compute_vftbl,
543                                         time_abstract, time_compute_iftbl, time_fill_vftbl,
544                                         time_offsets, time_fill_iftbl, time_finalizer,
545                                         time_subclasses;
546 #endif
547
548         RT_TIMING_GET_TIME(time_start);
549
550         TRACELINKCLASS(c);
551
552         /* the class must be loaded */
553
554         /* XXX should this be a specific exception? */
555         assert(c->state & CLASS_LOADED);
556
557         /* This is check in link_class. */
558
559         assert(!(c->state & CLASS_LINKED));
560
561         /* cache the self-reference of this class                          */
562         /* we do this for cases where the defining loader of the class     */
563         /* has not yet been recorded as an initiating loader for the class */
564         /* this is needed so subsequent code can assume that self-refs     */
565         /* will always resolve lazily                                      */
566         /* No need to do it for the bootloader - it is always registered   */
567         /* as initiating loader for the classes it loads.                  */
568         if (c->classloader)
569                 classcache_store(c->classloader,c,false);
570
571         /* this class is currently linking */
572
573         c->state |= CLASS_LINKING;
574
575         arraydesc = NULL;
576         worklist = NULL;
577
578         /* Link the super interfaces. */
579
580         for (i = 0; i < c->interfacescount; i++) {
581                 tc = c->interfaces[i];
582
583                 if (!(tc->state & CLASS_LINKED))
584                         if (!link_class(tc))
585                                 return NULL;
586         }
587         
588         /* check super class */
589
590         super = NULL;
591
592         /* Check for java/lang/Object. */
593
594         if (c->super == NULL) {
595                 c->index = 0;
596                 c->instancesize = sizeof(java_object_t);
597                 
598                 vftbllength = supervftbllength = 0;
599
600                 c->finalizer = NULL;
601         }
602         else {
603                 /* Get super class. */
604
605                 super = c->super;
606
607                 /* Link the super class if necessary. */
608                 
609                 if (!(super->state & CLASS_LINKED))
610                         if (!link_class(super))
611                                 return NULL;
612
613                 /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
614                    flags. */
615
616                 c->flags |= (super->flags &
617                                          (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
618
619                 /* handle array classes */
620
621                 if (c->name->text[0] == '[')
622                         if (!(arraydesc = link_array(c)))
623                                 return NULL;
624
625                 if (c->flags & ACC_INTERFACE)
626                         c->index = interfaceindex++;
627                 else
628                         c->index = super->index + 1;
629                 
630                 c->instancesize = super->instancesize;
631
632                 vftbllength = supervftbllength = super->vftbl->vftbllength;
633                 
634                 c->finalizer = super->finalizer;
635         }
636         RT_TIMING_GET_TIME(time_resolving);
637
638
639         /* compute vftbl length */
640
641         for (i = 0; i < c->methodscount; i++) {
642                 methodinfo *m = &(c->methods[i]);
643
644                 if (!(m->flags & ACC_STATIC)) { /* is instance method */
645                         tc = super;
646
647                         while (tc) {
648                                 s4 j;
649
650                                 for (j = 0; j < tc->methodscount; j++) {
651                                         if (method_canoverwrite(m, &(tc->methods[j]))) {
652                                                 if (tc->methods[j].flags & ACC_PRIVATE)
653                                                         goto notfoundvftblindex;
654
655                                                 /* package-private methods in other packages */
656                                                 /* must not be overridden                    */
657                                                 /* (see Java Language Specification 8.4.8.1) */
658                                                 if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED)) 
659                                                          && !SAME_PACKAGE(c,tc) ) 
660                                                 {
661                                                     goto notfoundvftblindex;
662                                                 }
663
664                                                 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
665                                                         return NULL;
666
667                                                 goto foundvftblindex;
668                                         }
669                                 }
670
671                                 tc = tc->super;
672                         }
673
674                 notfoundvftblindex:
675                         m->vftblindex = (vftbllength++);
676                 foundvftblindex:
677                         ;
678                 }
679         }
680         RT_TIMING_GET_TIME(time_compute_vftbl);
681
682
683         /* Check all interfaces of an abstract class (maybe be an
684            interface too) for unimplemented methods.  Such methods are
685            called miranda-methods and are marked with the ACC_MIRANDA
686            flag.  VMClass.getDeclaredMethods does not return such
687            methods. */
688
689         if (c->flags & ACC_ABSTRACT) {
690                 classinfo  *ic;
691                 methodinfo *im;
692                 s4 abstractmethodscount;
693                 s4 j;
694                 s4 k;
695
696                 abstractmethodscount = 0;
697
698                 /* check all interfaces of the abstract class */
699
700                 for (i = 0; i < c->interfacescount; i++) {
701                         ic = c->interfaces[i];
702
703                         for (j = 0; j < ic->methodscount; j++) {
704                                 im = &(ic->methods[j]);
705
706                                 /* skip `<clinit>' and `<init>' */
707
708                                 if ((im->name == utf_clinit) || (im->name == utf_init))
709                                         continue;
710
711                                 for (tc = c; tc != NULL; tc = tc->super) {
712                                         for (k = 0; k < tc->methodscount; k++) {
713                                                 if (method_canoverwrite(im, &(tc->methods[k])))
714                                                         goto noabstractmethod;
715                                         }
716                                 }
717
718                                 abstractmethodscount++;
719
720                         noabstractmethod:
721                                 ;
722                         }
723                 }
724
725                 if (abstractmethodscount > 0) {
726                         methodinfo *am;
727
728                         /* reallocate methods memory */
729
730                         c->methods = (methodinfo*) MREALLOC(c->methods, methodinfo, c->methodscount,
731                                                                   c->methodscount + abstractmethodscount);
732
733                         for (i = 0; i < c->interfacescount; i++) {
734                                 ic = c->interfaces[i];
735
736                                 for (j = 0; j < ic->methodscount; j++) {
737                                         im = &(ic->methods[j]);
738
739                                         /* skip `<clinit>' and `<init>' */
740
741                                         if ((im->name == utf_clinit) || (im->name == utf_init))
742                                                 continue;
743
744                                         for (tc = c; tc != NULL; tc = tc->super) {
745                                                 for (k = 0; k < tc->methodscount; k++) {
746                                                         if (method_canoverwrite(im, &(tc->methods[k])))
747                                                                 goto noabstractmethod2;
748                                                 }
749                                         }
750
751                                         /* Copy the method found into the new c->methods
752                                            array and tag it as miranda-method. */
753
754                                         am = &(c->methods[c->methodscount]);
755                                         c->methodscount++;
756
757                                         MCOPY(am, im, methodinfo, 1);
758
759                                         am->vftblindex  = (vftbllength++);
760                                         am->clazz       = c;
761                                         am->flags      |= ACC_MIRANDA;
762
763                                 noabstractmethod2:
764                                         ;
765                                 }
766                         }
767                 }
768         }
769         RT_TIMING_GET_TIME(time_abstract);
770
771
772 #if defined(ENABLE_STATISTICS)
773         if (opt_stat)
774                 count_vftbl_len +=
775                         sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
776 #endif
777
778         /* compute interfacetable length */
779
780         interfacetablelength = 0;
781
782         for (tc = c; tc != NULL; tc = tc->super) {
783                 for (i = 0; i < tc->interfacescount; i++) {
784                         s4 h = class_highestinterface(tc->interfaces[i]) + 1;
785
786                         if (h > interfacetablelength)
787                                 interfacetablelength = h;
788                 }
789         }
790         RT_TIMING_GET_TIME(time_compute_iftbl);
791
792         /* allocate virtual function table */
793
794         v = (vftbl_t *) mem_alloc(sizeof(vftbl_t) +
795                                                           sizeof(methodptr) * (vftbllength - 1) +
796                                                           sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
797         v = (vftbl_t *) (((methodptr *) v) +
798                                          (interfacetablelength - 1) * (interfacetablelength > 1));
799
800         c->vftbl                = v;
801         v->clazz                = c;
802         v->vftbllength          = vftbllength;
803         v->interfacetablelength = interfacetablelength;
804         v->arraydesc            = arraydesc;
805
806         /* store interface index in vftbl */
807
808         if (c->flags & ACC_INTERFACE)
809                 v->baseval = -(c->index);
810
811         /* copy virtual function table of super class */
812
813         for (i = 0; i < supervftbllength; i++) 
814                 v->table[i] = super->vftbl->table[i];
815
816         /* Fill the remaining vftbl slots with the AbstractMethodError
817            stub (all after the super class slots, because they are already
818            initialized). */
819
820         for (; i < vftbllength; i++) {
821 #if defined(ENABLE_JIT)
822 # if defined(ENABLE_INTRP)
823                 if (opt_intrp)
824                         v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
825                 else
826 # endif
827                         v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
828 #else
829                 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
830 #endif
831         }
832
833         /* add method stubs into virtual function table */
834
835         for (i = 0; i < c->methodscount; i++) {
836                 methodinfo *m = &(c->methods[i]);
837
838                 assert(m->stubroutine == NULL);
839
840                 /* Don't create a compiler stub for abstract methods as they
841                    throw an AbstractMethodError with the default stub in the
842                    vftbl.  This entry is simply copied by sub-classes. */
843
844                 if (m->flags & ACC_ABSTRACT)
845                         continue;
846
847 #if defined(ENABLE_JIT)
848 # if defined(ENABLE_INTRP)
849                 if (opt_intrp)
850                         m->stubroutine = intrp_createcompilerstub(m);
851                 else
852 #endif
853                         m->stubroutine = (u1*) CompilerStub::generate(m);
854 #else
855                 m->stubroutine = intrp_createcompilerstub(m);
856 #endif
857
858                 /* static methods are not in the vftbl */
859
860                 if (m->flags & ACC_STATIC)
861                         continue;
862
863                 /* insert the stubroutine into the vftbl */
864
865                 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
866         }
867         RT_TIMING_GET_TIME(time_fill_vftbl);
868
869         /* compute instance size and offset of each field */
870         
871         for (i = 0; i < c->fieldscount; i++) {
872                 s4 dsize;
873                 fieldinfo *f = &(c->fields[i]);
874                 
875                 if (!(f->flags & ACC_STATIC)) {
876                         dsize = descriptor_typesize(f->parseddesc);
877                         c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
878                         f->offset = c->instancesize;
879                         c->instancesize += dsize;
880                 }
881         }
882         RT_TIMING_GET_TIME(time_offsets);
883
884         /* initialize interfacetable and interfacevftbllength */
885
886         v->interfacevftbllength = MNEW(s4, interfacetablelength);
887
888 #if defined(ENABLE_STATISTICS)
889         if (opt_stat)
890                 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
891 #endif
892
893         for (i = 0; i < interfacetablelength; i++) {
894                 v->interfacevftbllength[i] = 0;
895                 v->interfacetable[-i] = NULL;
896         }
897
898         /* add interfaces */
899
900         for (tc = c; tc != NULL; tc = tc->super)
901                 for (i = 0; i < tc->interfacescount; i++)
902                         if (!linker_addinterface(c, tc->interfaces[i]))
903                                 return NULL;
904
905         RT_TIMING_GET_TIME(time_fill_iftbl);
906
907         /* add finalizer method (not for java.lang.Object) */
908
909         if (super) {
910                 methodinfo *fi;
911
912                 fi = class_findmethod(c, utf_finalize, utf_void__void);
913
914                 if (fi)
915                         if (!(fi->flags & ACC_STATIC))
916                                 c->finalizer = fi;
917         }
918         RT_TIMING_GET_TIME(time_finalizer);
919
920         /* final tasks */
921
922         linker_compute_subclasses(c);
923
924         RT_TIMING_GET_TIME(time_subclasses);
925
926         /* revert the linking state and class is linked */
927
928         c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
929
930         /* check worklist */
931
932         /* XXX must this also be done in case of exception? */
933
934         while (worklist != NULL) {
935                 method_worklist *wi = worklist;
936
937                 worklist = worklist->next;
938
939                 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
940                 jit_invalidate_code(wi->m);
941
942                 /* XXX put worklist into dump memory? */
943                 FREE(wi, method_worklist);
944         }
945
946         RT_TIMING_TIME_DIFF(time_start        ,time_resolving    ,RT_TIMING_LINK_RESOLVE);
947         RT_TIMING_TIME_DIFF(time_resolving    ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
948         RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract     ,RT_TIMING_LINK_ABSTRACT);
949         RT_TIMING_TIME_DIFF(time_abstract     ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
950         RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl   ,RT_TIMING_LINK_F_VFTBL);
951         RT_TIMING_TIME_DIFF(time_fill_vftbl   ,time_offsets      ,RT_TIMING_LINK_OFFSETS);
952         RT_TIMING_TIME_DIFF(time_offsets      ,time_fill_iftbl   ,RT_TIMING_LINK_F_IFTBL);
953         RT_TIMING_TIME_DIFF(time_fill_iftbl   ,time_finalizer    ,RT_TIMING_LINK_FINALIZER);
954         RT_TIMING_TIME_DIFF(time_finalizer    ,time_subclasses   ,RT_TIMING_LINK_SUBCLASS);
955
956         /* just return c to show that we didn't had a problem */
957
958         return c;
959 }
960
961
962 /* link_array ******************************************************************
963
964    This function is called by link_class to create the arraydescriptor
965    for an array class.
966
967    This function returns NULL if the array cannot be linked because
968    the component type has not been linked yet.
969
970 *******************************************************************************/
971
972 static arraydescriptor *link_array(classinfo *c)
973 {
974         classinfo       *comp;
975         s4               namelen;
976         arraydescriptor *desc;
977         vftbl_t         *compvftbl;
978         utf             *u;
979
980         comp = NULL;
981         namelen = c->name->blength;
982
983         /* Check the component type */
984
985         switch (c->name->text[1]) {
986         case '[':
987                 /* c is an array of arrays. */
988                 u = utf_new(c->name->text + 1, namelen - 1);
989                 if (!(comp = load_class_from_classloader(u, c->classloader)))
990                         return NULL;
991                 break;
992
993         case 'L':
994                 /* c is an array of objects. */
995                 u = utf_new(c->name->text + 2, namelen - 3);
996                 if (!(comp = load_class_from_classloader(u, c->classloader)))
997                         return NULL;
998                 break;
999         }
1000
1001         /* If the component type has not been linked, link it now */
1002
1003         assert(!comp || (comp->state & CLASS_LOADED));
1004
1005         if (comp && !(comp->state & CLASS_LINKED))
1006                 if (!link_class(comp))
1007                         return NULL;
1008
1009         /* Allocate the arraydescriptor */
1010
1011         desc = NEW(arraydescriptor);
1012
1013         if (comp) {
1014                 /* c is an array of references */
1015                 desc->arraytype = ARRAYTYPE_OBJECT;
1016                 desc->componentsize = sizeof(void*);
1017                 desc->dataoffset = OFFSET(java_objectarray_t, data);
1018                 
1019                 compvftbl = comp->vftbl;
1020
1021                 if (!compvftbl) {
1022                         log_text("Component class has no vftbl");
1023                         assert(0);
1024                 }
1025
1026                 desc->componentvftbl = compvftbl;
1027                 
1028                 if (compvftbl->arraydesc) {
1029                         desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1030
1031                         if (compvftbl->arraydesc->dimension >= 255) {
1032                                 log_text("Creating array of dimension >255");
1033                                 assert(0);
1034                         }
1035
1036                         desc->dimension = compvftbl->arraydesc->dimension + 1;
1037                         desc->elementtype = compvftbl->arraydesc->elementtype;
1038
1039                 } else {
1040                         desc->elementvftbl = compvftbl;
1041                         desc->dimension = 1;
1042                         desc->elementtype = ARRAYTYPE_OBJECT;
1043                 }
1044
1045         } else {
1046                 /* c is an array of a primitive type */
1047                 switch (c->name->text[1]) {
1048                 case 'Z':
1049                         desc->arraytype = ARRAYTYPE_BOOLEAN;
1050                         desc->dataoffset = OFFSET(java_booleanarray_t,data);
1051                         desc->componentsize = sizeof(u1);
1052                         break;
1053
1054                 case 'B':
1055                         desc->arraytype = ARRAYTYPE_BYTE;
1056                         desc->dataoffset = OFFSET(java_bytearray_t,data);
1057                         desc->componentsize = sizeof(u1);
1058                         break;
1059
1060                 case 'C':
1061                         desc->arraytype = ARRAYTYPE_CHAR;
1062                         desc->dataoffset = OFFSET(java_chararray_t,data);
1063                         desc->componentsize = sizeof(u2);
1064                         break;
1065
1066                 case 'D':
1067                         desc->arraytype = ARRAYTYPE_DOUBLE;
1068                         desc->dataoffset = OFFSET(java_doublearray_t,data);
1069                         desc->componentsize = sizeof(double);
1070                         break;
1071
1072                 case 'F':
1073                         desc->arraytype = ARRAYTYPE_FLOAT;
1074                         desc->dataoffset = OFFSET(java_floatarray_t,data);
1075                         desc->componentsize = sizeof(float);
1076                         break;
1077
1078                 case 'I':
1079                         desc->arraytype = ARRAYTYPE_INT;
1080                         desc->dataoffset = OFFSET(java_intarray_t,data);
1081                         desc->componentsize = sizeof(s4);
1082                         break;
1083
1084                 case 'J':
1085                         desc->arraytype = ARRAYTYPE_LONG;
1086                         desc->dataoffset = OFFSET(java_longarray_t,data);
1087                         desc->componentsize = sizeof(s8);
1088                         break;
1089
1090                 case 'S':
1091                         desc->arraytype = ARRAYTYPE_SHORT;
1092                         desc->dataoffset = OFFSET(java_shortarray_t,data);
1093                         desc->componentsize = sizeof(s2);
1094                         break;
1095
1096                 default:
1097                         exceptions_throw_noclassdeffounderror(c->name);
1098                         return NULL;
1099                 }
1100                 
1101                 desc->componentvftbl = NULL;
1102                 desc->elementvftbl = NULL;
1103                 desc->dimension = 1;
1104                 desc->elementtype = desc->arraytype;
1105         }
1106
1107         return desc;
1108 }
1109
1110
1111 /* linker_compute_subclasses ***************************************************
1112
1113    XXX
1114
1115    ATTENTION: DO NOT REMOVE ANY OF THE LOCKING MECHANISMS BELOW:
1116    This function needs to take the class renumber lock and stop the
1117    world during class renumbering. The lock is used in C code which
1118    is not that performance critical. Whereas JIT code uses critical
1119    sections to atomically access the class values.
1120
1121 *******************************************************************************/
1122
1123 static void linker_compute_subclasses(classinfo *c)
1124 {
1125         linker_classrenumber_mutex->lock();
1126
1127         if (!(c->flags & ACC_INTERFACE)) {
1128                 c->nextsub = NULL;
1129                 c->sub     = NULL;
1130         }
1131
1132         if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
1133                 c->nextsub    = c->super->sub;
1134                 c->super->sub = c;
1135         }
1136
1137         classvalue = 0;
1138
1139         /* compute class values */
1140
1141         linker_compute_class_values(class_java_lang_Object);
1142
1143         linker_classrenumber_mutex->unlock();
1144 }
1145
1146
1147 /* linker_compute_class_values *************************************************
1148
1149    XXX
1150
1151 *******************************************************************************/
1152
1153 static void linker_compute_class_values(classinfo *c)
1154 {
1155         classinfo *subs;
1156
1157         c->vftbl->baseval = ++classvalue;
1158
1159         subs = c->sub;
1160
1161         while (subs) {
1162                 linker_compute_class_values(subs);
1163
1164                 subs = subs->nextsub;
1165         }
1166
1167         c->vftbl->diffval = classvalue - c->vftbl->baseval;
1168 }
1169
1170
1171 /* linker_addinterface *********************************************************
1172
1173    Is needed by link_class for adding a VTBL to a class. All
1174    interfaces implemented by ic are added as well.
1175
1176    RETURN VALUE:
1177       true.........everything ok
1178           false........an exception has been thrown
1179
1180 *******************************************************************************/
1181
1182 static bool linker_addinterface(classinfo *c, classinfo *ic)
1183 {
1184         s4          j, k;
1185         vftbl_t    *v;
1186         s4          i;
1187         classinfo  *sc;
1188         methodinfo *m;
1189
1190         v = c->vftbl;
1191         i = ic->index;
1192
1193         if (i >= v->interfacetablelength)
1194                 vm_abort("Internal error: interfacetable overflow");
1195
1196         /* if this interface has already been added, return immediately */
1197
1198         if (v->interfacetable[-i] != NULL)
1199                 return true;
1200
1201         if (ic->methodscount == 0) {  /* fake entry needed for subtype test */
1202                 v->interfacevftbllength[i] = 1;
1203                 v->interfacetable[-i]      = MNEW(methodptr, 1);
1204                 v->interfacetable[-i][0]   = NULL;
1205         }
1206         else {
1207                 v->interfacevftbllength[i] = ic->methodscount;
1208                 v->interfacetable[-i]      = MNEW(methodptr, ic->methodscount);
1209
1210 #if defined(ENABLE_STATISTICS)
1211                 if (opt_stat)
1212                         count_vftbl_len += sizeof(methodptr) *
1213                                 (ic->methodscount + (ic->methodscount == 0));
1214 #endif
1215
1216                 for (j = 0; j < ic->methodscount; j++) {
1217                         for (sc = c; sc != NULL; sc = sc->super) {
1218                                 for (k = 0; k < sc->methodscount; k++) {
1219                                         m = &(sc->methods[k]);
1220
1221                                         if (method_canoverwrite(m, &(ic->methods[j]))) {
1222                                                 /* method m overwrites the (abstract) method */
1223 #if defined(ENABLE_VERIFIER)
1224                                                 /* Add loading constraints (for the more
1225                                                    general types of the method
1226                                                    ic->methods[j]).  */
1227                                                 if (!classcache_add_constraints_for_params(
1228                                                                         c->classloader, ic->classloader,
1229                                                                         &(ic->methods[j])))
1230                                                 {
1231                                                         return false;
1232                                                 }
1233 #endif
1234
1235                                                 /* XXX taken from gcj */
1236                                                 /* check for ACC_STATIC: IncompatibleClassChangeError */
1237
1238                                                 /* check for !ACC_PUBLIC: IllegalAccessError */
1239
1240                                                 /* check for ACC_ABSTRACT: AbstracMethodError,
1241                                                    not sure about that one */
1242
1243                                                 v->interfacetable[-i][j] = v->table[m->vftblindex];
1244                                                 goto foundmethod;
1245                                         }
1246                                 }
1247                         }
1248
1249                         /* If no method was found, insert the AbstractMethodError
1250                            stub. */
1251
1252 #if defined(ENABLE_JIT)
1253 # if defined(ENABLE_INTRP)
1254                         if (opt_intrp)
1255                                 v->interfacetable[-i][j] =
1256                                         (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1257                         else
1258 # endif
1259                                 v->interfacetable[-i][j] =
1260                                         (methodptr) (ptrint) &asm_abstractmethoderror;
1261 #else
1262                         v->interfacetable[-i][j] =
1263                                 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1264 #endif
1265
1266                 foundmethod:
1267                         ;
1268                 }
1269         }
1270
1271         /* add superinterfaces of this interface */
1272
1273         for (j = 0; j < ic->interfacescount; j++)
1274                 if (!linker_addinterface(c, ic->interfaces[j]))
1275                         return false;
1276
1277         /* everything ok */
1278
1279         return true;
1280 }
1281
1282
1283 /* class_highestinterface ******************************************************
1284
1285    Used by the function link_class to determine the amount of memory
1286    needed for the interface table.
1287
1288 *******************************************************************************/
1289
1290 static s4 class_highestinterface(classinfo *c)
1291 {
1292         s4 h;
1293         s4 h2;
1294         s4 i;
1295         
1296     /* check for ACC_INTERFACE bit already done in link_class_intern */
1297
1298     h = c->index;
1299
1300         for (i = 0; i < c->interfacescount; i++) {
1301                 h2 = class_highestinterface(c->interfaces[i]);
1302
1303                 if (h2 > h)
1304                         h = h2;
1305         }
1306
1307         return h;
1308 }
1309
1310 #if defined(__cplusplus)
1311 }
1312 #endif
1313
1314 /*
1315  * These are local overrides for various environment variables in Emacs.
1316  * Please do not remove this and leave it at the end of the file, where
1317  * Emacs will automagically detect them.
1318  * ---------------------------------------------------------------------
1319  * Local variables:
1320  * mode: c
1321  * indent-tabs-mode: t
1322  * c-basic-offset: 4
1323  * tab-width: 4
1324  * End:
1325  * vim:noexpandtab:sw=4:ts=4:
1326  */