Merge from subtype.
[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 #if !USES_NEW_SUBTYPE
98 Mutex *linker_classrenumber_lock;
99 #endif
100
101 #if defined(__cplusplus)
102 extern "C" {
103 #endif
104
105 /* private functions **********************************************************/
106
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);
113
114
115 /* linker_init *****************************************************************
116
117    Initializes the linker subsystem and links classes required for the
118    primitive table.
119
120 *******************************************************************************/
121
122 void linker_preinit(void)
123 {
124         TRACESUBSYSTEMINITIALIZATION("linker_preinit");
125
126         /* Reset interface index. */
127
128         interfaceindex = 0;
129
130 #if defined(ENABLE_THREADS) && !USES_NEW_SUBTYPE
131         /* create the global mutex */
132
133         linker_classrenumber_lock = new Mutex();
134 #endif
135
136         /* Link the most basic classes. */
137
138         if (!link_class(class_java_lang_Object))
139                 vm_abort("linker_preinit: linking java/lang/Object failed");
140
141 #if defined(ENABLE_JAVASE)
142         if (!link_class(class_java_lang_Cloneable))
143                 vm_abort("linker_preinit: linking java/lang/Cloneable failed");
144
145         if (!link_class(class_java_io_Serializable))
146                 vm_abort("linker_preinit: linking java/io/Serializable failed");
147 #endif
148 }
149
150
151 /* linker_init *****************************************************************
152
153    Links all classes required in the VM.
154
155 *******************************************************************************/
156
157 void linker_init(void)
158 {
159         TRACESUBSYSTEMINITIALIZATION("linker_init");
160
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
163        object. */
164
165         if (!link_class(class_java_lang_Class))
166                 vm_abort("linker_init: linking java/lang/Class failed");
167
168         /* Now set the header.vftbl of all classes which were created
169        before java.lang.Class was linked. */
170
171         class_postset_header_vftbl();
172
173         /* Link primitive-type wrapping classes. */
174
175 #if defined(ENABLE_JAVASE)
176         if (!link_class(class_java_lang_Void))
177                 vm_abort("linker_init: linking failed");
178 #endif
179
180         if (!link_class(class_java_lang_Boolean))
181                 vm_abort("linker_init: linking failed");
182
183         if (!link_class(class_java_lang_Byte))
184                 vm_abort("linker_init: linking failed");
185
186         if (!link_class(class_java_lang_Character))
187                 vm_abort("linker_init: linking failed");
188
189         if (!link_class(class_java_lang_Short))
190                 vm_abort("linker_init: linking failed");
191
192         if (!link_class(class_java_lang_Integer))
193                 vm_abort("linker_init: linking failed");
194
195         if (!link_class(class_java_lang_Long))
196                 vm_abort("linker_init: linking failed");
197
198         if (!link_class(class_java_lang_Float))
199                 vm_abort("linker_init: linking failed");
200
201         if (!link_class(class_java_lang_Double))
202                 vm_abort("linker_init: linking failed");
203
204         /* Link important system classes. */
205
206         if (!link_class(class_java_lang_String))
207                 vm_abort("linker_init: linking java/lang/String failed");
208
209 #if defined(ENABLE_JAVASE)
210         if (!link_class(class_java_lang_ClassLoader))
211                 vm_abort("linker_init: linking failed");
212
213         if (!link_class(class_java_lang_SecurityManager))
214                 vm_abort("linker_init: linking failed");
215 #endif
216
217         if (!link_class(class_java_lang_System))
218                 vm_abort("linker_init: linking failed");
219
220         if (!link_class(class_java_lang_Thread))
221                 vm_abort("linker_init: linking failed");
222
223 #if defined(ENABLE_JAVASE)
224         if (!link_class(class_java_lang_ThreadGroup))
225                 vm_abort("linker_init: linking failed");
226 #endif
227
228         if (!link_class(class_java_lang_Throwable))
229                 vm_abort("linker_init: linking failed");
230
231 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
232         if (!link_class(class_java_lang_VMSystem))
233                 vm_abort("linker_init: linking failed");
234
235         if (!link_class(class_java_lang_VMThread))
236                 vm_abort("linker_init: linking failed");
237
238         if (!link_class(class_java_lang_VMThrowable))
239                 vm_abort("linker_init: linking failed");
240 #endif
241
242         /* Important system exceptions. */
243
244         if (!link_class(class_java_lang_Exception))
245                 vm_abort("linker_init: linking failed");
246
247         if (!link_class(class_java_lang_ClassNotFoundException))
248                 vm_abort("linker_init: linking failed");
249
250         if (!link_class(class_java_lang_RuntimeException))
251                 vm_abort("linker_init: linking failed");
252
253         /* some classes which may be used more often */
254
255 #if defined(ENABLE_JAVASE)
256         if (!link_class(class_java_lang_StackTraceElement))
257                 vm_abort("linker_init: linking failed");
258
259         if (!link_class(class_java_lang_reflect_Constructor))
260                 vm_abort("linker_init: linking failed");
261
262         if (!link_class(class_java_lang_reflect_Field))
263                 vm_abort("linker_init: linking failed");
264
265         if (!link_class(class_java_lang_reflect_Method))
266                 vm_abort("linker_init: linking failed");
267
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");
271
272         if (!link_class(class_java_lang_reflect_VMField))
273                 vm_abort("linker_init: linking failed");
274
275         if (!link_class(class_java_lang_reflect_VMMethod))
276                 vm_abort("linker_init: linking failed");
277 # endif
278
279         if (!link_class(class_java_security_PrivilegedAction))
280                 vm_abort("linker_init: linking failed");
281
282         if (!link_class(class_java_util_Vector))
283                 vm_abort("linker_init: linking failed");
284
285         if (!link_class(class_java_util_HashMap))
286                 vm_abort("linker_init: linking failed");
287
288 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
289         if (!link_class(class_sun_misc_Signal))
290                 vm_abort("linker_init: linking failed");
291
292         if (!link_class(class_sun_reflect_MagicAccessorImpl))
293                 vm_abort("linker_init: linking failed");
294 # endif
295
296         if (!link_class(arrayclass_java_lang_Object))
297                 vm_abort("linker_init: linking failed");
298 #endif
299
300
301         /* create pseudo classes used by the typechecker */
302
303     /* pseudo class for Arraystubs (extends java.lang.Object) */
304
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;
309
310 #if defined(ENABLE_JAVASE)
311
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;
316
317 #elif defined(ENABLE_JAVAME_CLDC1_1)
318
319         pseudo_class_Arraystub->interfacescount    = 0;
320         pseudo_class_Arraystub->interfaces         = NULL;
321
322 #else
323 # error unknown Java configuration
324 #endif
325
326         if (!classcache_store_unique(pseudo_class_Arraystub))
327                 vm_abort("linker_init: could not cache pseudo_class_Arraystub");
328
329         if (!link_class(pseudo_class_Arraystub))
330                 vm_abort("linker_init: linking pseudo_class_Arraystub failed");
331
332         /* pseudo class representing the null type */
333
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;
337
338         if (!classcache_store_unique(pseudo_class_Null))
339                 vm_abort("linker_init: could not cache pseudo_class_Null");
340
341         if (!link_class(pseudo_class_Null))
342                 vm_abort("linker_init: linking failed");
343
344         /* pseudo class representing new uninitialized objects */
345     
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;
350
351         if (!classcache_store_unique(pseudo_class_New))
352                 vm_abort("linker_init: could not cache pseudo_class_New");
353
354         /* Correct vftbl-entries (retarded loading and linking of class
355            java/lang/String). */
356
357         stringtable_update();
358 }
359
360
361 /* link_class ******************************************************************
362
363    Wrapper function for link_class_intern to ease monitor enter/exit
364    and exception handling.
365
366 *******************************************************************************/
367
368 classinfo *link_class(classinfo *c)
369 {
370         classinfo *r;
371 #if defined(ENABLE_RT_TIMING)
372         struct timespec time_start, time_end;
373 #endif
374
375         RT_TIMING_GET_TIME(time_start);
376
377         if (c == NULL) {
378                 exceptions_throw_nullpointerexception();
379                 return NULL;
380         }
381
382         LOCK_MONITOR_ENTER(c);
383
384         /* Maybe the class is currently linking or is already linked.*/
385
386         if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
387                 LOCK_MONITOR_EXIT(c);
388
389                 return c;
390         }
391
392 #if defined(ENABLE_STATISTICS)
393         /* measure time */
394
395         if (opt_getcompilingtime)
396                 compilingtime_stop();
397
398         if (opt_getloadingtime)
399                 loadingtime_start();
400 #endif
401
402         /* call the internal function */
403
404         r = link_class_intern(c);
405
406         /* If return value is NULL, we had a problem and the class is not
407            linked. */
408
409         if (r == NULL)
410                 c->state &= ~CLASS_LINKING;
411
412 #if defined(ENABLE_STATISTICS)
413         /* measure time */
414
415         if (opt_getloadingtime)
416                 loadingtime_stop();
417
418         if (opt_getcompilingtime)
419                 compilingtime_start();
420 #endif
421
422         LOCK_MONITOR_EXIT(c);
423
424         RT_TIMING_GET_TIME(time_end);
425
426         RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
427
428         return r;
429 }
430
431
432 /* linker_overwrite_method *****************************************************
433
434    Overwrite a method with another one, update method flags and check
435    assumptions.
436
437    IN:
438       mg................the general method being overwritten
439           ms................the overwriting (more specialized) method
440           wl................worklist where to add invalidated methods
441
442    RETURN VALUE:
443       true..............everything ok
444           false.............an exception has been thrown
445
446 *******************************************************************************/
447
448 static bool linker_overwrite_method(methodinfo *mg,
449                                                                         methodinfo *ms,
450                                                                         method_worklist **wl)
451 {
452         classinfo *cg;
453         classinfo *cs;
454
455         cg = mg->clazz;
456         cs = ms->clazz;
457
458         /* overriding a final method is illegal */
459
460         if (mg->flags & ACC_FINAL) {
461                 exceptions_throw_verifyerror(mg, "Overriding final method");
462                 return false;
463         }
464
465         /* method ms overwrites method mg */
466
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.                    */
470
471         if ((ms->name != utf_init)
472                         && !classcache_add_constraints_for_params(
473                                 cs->classloader, cg->classloader, mg))
474         {
475                 return false;
476         }
477 #endif
478
479         /* inherit the vftbl index, and record the overwriting */
480
481         ms->vftblindex = mg->vftblindex;
482         ms->overwrites = mg;
483
484         /* update flags and check assumptions */
485         /* <init> methods are a special case, as they are never dispatched dynamically */
486
487         if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
488                 do {
489
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;                                 
495                         }
496 #endif
497
498                         if (mg->flags & ACC_METHOD_IMPLEMENTED) {
499                                 /* this adds another implementation */
500
501                                 mg->flags &= ~ACC_METHOD_MONOMORPHIC;
502
503                                 INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
504
505                                 method_break_assumption_monomorphic(mg, wl);
506                         }
507                         else {
508                                 /* this is the first implementation */
509
510                                 mg->flags |= ACC_METHOD_IMPLEMENTED;
511
512                                 INLINELOG( printf("becomes implemented: "); method_println(mg); );
513                         }
514
515                         ms = mg;
516                         mg = mg->overwrites;
517                 } while (mg != NULL);
518         }
519
520         return true;
521 }
522
523
524 #if USES_NEW_SUBTYPE
525 /* build_display ***************************************************************
526
527    Builds the entire display for a class. This entails filling the fixed part
528    as well as allocating and initializing the overflow part.
529
530    See Cliff Click and John Rose: Fast subtype checking in the Hotspot JVM.
531
532 *******************************************************************************/
533
534 static classinfo *build_display(classinfo *c)
535 {
536         int depth, i;
537         int depth_fixed;
538         classinfo *super;
539
540         do {
541                 /* Handle arrays. */
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;
546                                 int n;
547                                 for (n=0; n<a->dimension; n++)
548                                         cls = class_array_of(cls, true);
549                                 super = cls;
550                                 break;
551                         }
552                         if (a->componentvftbl && a->elementvftbl) {
553                                 super = a->componentvftbl->clazz;
554                                 break;
555                         }
556                 }
557                 /* Normal classes. */
558                 super = c->super;
559         } while (false);
560         if (super) {
561                 if (!link_class(super))
562                         return NULL;
563                 depth = super->vftbl->subtype_depth + 1;
564         } else
565                 /* java.lang.Object doesn't have a super class. */
566                 depth = 0;
567
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)
572                 if (opt_stat)
573                         count_vftbl_len += sizeof(vftbl_t*) * (depth - DISPLAY_SIZE + 1);
574 #endif
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;
578         }
579         else {
580                 depth_fixed = depth;
581                 c->vftbl->subtype_display[depth] = c->vftbl;
582         }
583
584         if (super)
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;
590
591         return c;
592 }
593 #endif
594
595 /* link_class_intern ***********************************************************
596
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.
600         
601 *******************************************************************************/
602
603 static classinfo *link_class_intern(classinfo *c)
604 {
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,
618                                         time_subclasses;
619 #endif
620
621         RT_TIMING_GET_TIME(time_start);
622
623         TRACELINKCLASS(c);
624
625         /* the class must be loaded */
626
627         /* XXX should this be a specific exception? */
628         assert(c->state & CLASS_LOADED);
629
630         /* This is check in link_class. */
631
632         assert(!(c->state & CLASS_LINKED));
633
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.                  */
641         if (c->classloader)
642                 classcache_store(c->classloader,c,false);
643
644         /* this class is currently linking */
645
646         c->state |= CLASS_LINKING;
647
648         arraydesc = NULL;
649         worklist = NULL;
650
651         /* Link the super interfaces. */
652
653         for (i = 0; i < c->interfacescount; i++) {
654                 tc = c->interfaces[i];
655
656                 if (!(tc->state & CLASS_LINKED))
657                         if (!link_class(tc))
658                                 return NULL;
659         }
660         
661         /* check super class */
662
663         super = NULL;
664
665         /* Check for java/lang/Object. */
666
667         if (c->super == NULL) {
668                 c->index = 0;
669                 c->instancesize = sizeof(java_object_t);
670                 
671                 vftbllength = supervftbllength = 0;
672
673                 c->finalizer = NULL;
674         }
675         else {
676                 /* Get super class. */
677
678                 super = c->super;
679
680                 /* Link the super class if necessary. */
681                 
682                 if (!(super->state & CLASS_LINKED))
683                         if (!link_class(super))
684                                 return NULL;
685
686                 /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
687                    flags. */
688
689                 c->flags |= (super->flags &
690                                          (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
691
692                 /* handle array classes */
693
694                 if (c->name->text[0] == '[')
695                         if (!(arraydesc = link_array(c)))
696                                 return NULL;
697
698                 if (c->flags & ACC_INTERFACE)
699                         c->index = interfaceindex++;
700                 else
701                         c->index = super->index + 1;
702                 
703                 c->instancesize = super->instancesize;
704
705                 vftbllength = supervftbllength = super->vftbl->vftbllength;
706                 
707                 c->finalizer = super->finalizer;
708         }
709         RT_TIMING_GET_TIME(time_resolving);
710
711
712         /* compute vftbl length */
713
714         for (i = 0; i < c->methodscount; i++) {
715                 methodinfo *m = &(c->methods[i]);
716
717                 if (!(m->flags & ACC_STATIC)) { /* is instance method */
718                         tc = super;
719
720                         while (tc) {
721                                 s4 j;
722
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;
727
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) ) 
733                                                 {
734                                                     goto notfoundvftblindex;
735                                                 }
736
737                                                 if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
738                                                         return NULL;
739
740                                                 goto foundvftblindex;
741                                         }
742                                 }
743
744                                 tc = tc->super;
745                         }
746
747                 notfoundvftblindex:
748                         m->vftblindex = (vftbllength++);
749                 foundvftblindex:
750                         ;
751                 }
752         }
753         RT_TIMING_GET_TIME(time_compute_vftbl);
754
755
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
760            methods. */
761
762         if (c->flags & ACC_ABSTRACT) {
763                 classinfo  *ic;
764                 methodinfo *im;
765                 s4 abstractmethodscount;
766                 s4 j;
767                 s4 k;
768
769                 abstractmethodscount = 0;
770
771                 /* check all interfaces of the abstract class */
772
773                 for (i = 0; i < c->interfacescount; i++) {
774                         ic = c->interfaces[i];
775
776                         for (j = 0; j < ic->methodscount; j++) {
777                                 im = &(ic->methods[j]);
778
779                                 /* skip `<clinit>' and `<init>' */
780
781                                 if ((im->name == utf_clinit) || (im->name == utf_init))
782                                         continue;
783
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;
788                                         }
789                                 }
790
791                                 abstractmethodscount++;
792
793                         noabstractmethod:
794                                 ;
795                         }
796                 }
797
798                 if (abstractmethodscount > 0) {
799                         methodinfo *am;
800
801                         /* reallocate methods memory */
802
803                         c->methods = (methodinfo*) MREALLOC(c->methods, methodinfo, c->methodscount,
804                                                                   c->methodscount + abstractmethodscount);
805
806                         for (i = 0; i < c->interfacescount; i++) {
807                                 ic = c->interfaces[i];
808
809                                 for (j = 0; j < ic->methodscount; j++) {
810                                         im = &(ic->methods[j]);
811
812                                         /* skip `<clinit>' and `<init>' */
813
814                                         if ((im->name == utf_clinit) || (im->name == utf_init))
815                                                 continue;
816
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;
821                                                 }
822                                         }
823
824                                         /* Copy the method found into the new c->methods
825                                            array and tag it as miranda-method. */
826
827                                         am = &(c->methods[c->methodscount]);
828                                         c->methodscount++;
829
830                                         MCOPY(am, im, methodinfo, 1);
831
832                                         am->vftblindex  = (vftbllength++);
833                                         am->clazz       = c;
834                                         am->flags      |= ACC_MIRANDA;
835
836                                 noabstractmethod2:
837                                         ;
838                                 }
839                         }
840                 }
841         }
842         RT_TIMING_GET_TIME(time_abstract);
843
844
845 #if defined(ENABLE_STATISTICS)
846         if (opt_stat)
847                 count_vftbl_len +=
848                         sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
849 #endif
850
851         /* compute interfacetable length */
852
853         interfacetablelength = 0;
854
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;
858
859                         if (h > interfacetablelength)
860                                 interfacetablelength = h;
861                 }
862         }
863         RT_TIMING_GET_TIME(time_compute_iftbl);
864
865         /* allocate virtual function table */
866
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));
872
873         c->vftbl                = v;
874         v->clazz                = c;
875         v->vftbllength          = vftbllength;
876         v->interfacetablelength = interfacetablelength;
877         v->arraydesc            = arraydesc;
878
879         /* store interface index in vftbl */
880
881         if (c->flags & ACC_INTERFACE)
882                 v->baseval = -(c->index);
883
884         /* copy virtual function table of super class */
885
886         for (i = 0; i < supervftbllength; i++) 
887                 v->table[i] = super->vftbl->table[i];
888
889         /* Fill the remaining vftbl slots with the AbstractMethodError
890            stub (all after the super class slots, because they are already
891            initialized). */
892
893         for (; i < vftbllength; i++) {
894 #if defined(ENABLE_JIT)
895 # if defined(ENABLE_INTRP)
896                 if (opt_intrp)
897                         v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
898                 else
899 # endif
900                         v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
901 #else
902                 v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
903 #endif
904         }
905
906         /* add method stubs into virtual function table */
907
908         for (i = 0; i < c->methodscount; i++) {
909                 methodinfo *m = &(c->methods[i]);
910
911                 assert(m->stubroutine == NULL);
912
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. */
916
917                 if (m->flags & ACC_ABSTRACT)
918                         continue;
919
920 #if defined(ENABLE_JIT)
921 # if defined(ENABLE_INTRP)
922                 if (opt_intrp)
923                         m->stubroutine = intrp_createcompilerstub(m);
924                 else
925 #endif
926                         m->stubroutine = (u1*) CompilerStub::generate(m);
927 #else
928                 m->stubroutine = intrp_createcompilerstub(m);
929 #endif
930
931                 /* static methods are not in the vftbl */
932
933                 if (m->flags & ACC_STATIC)
934                         continue;
935
936                 /* insert the stubroutine into the vftbl */
937
938                 v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
939         }
940         RT_TIMING_GET_TIME(time_fill_vftbl);
941
942         /* compute instance size and offset of each field */
943         
944         for (i = 0; i < c->fieldscount; i++) {
945                 s4 dsize;
946                 fieldinfo *f = &(c->fields[i]);
947                 
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;
953                 }
954         }
955         RT_TIMING_GET_TIME(time_offsets);
956
957         /* initialize interfacetable and interfacevftbllength */
958
959         v->interfacevftbllength = MNEW(s4, interfacetablelength);
960
961 #if defined(ENABLE_STATISTICS)
962         if (opt_stat)
963                 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
964 #endif
965
966         for (i = 0; i < interfacetablelength; i++) {
967                 v->interfacevftbllength[i] = 0;
968                 v->interfacetable[-i] = NULL;
969         }
970
971         /* add interfaces */
972
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]))
976                                 return NULL;
977
978         RT_TIMING_GET_TIME(time_fill_iftbl);
979
980         /* add finalizer method (not for java.lang.Object) */
981
982         if (super) {
983                 methodinfo *fi;
984
985                 fi = class_findmethod(c, utf_finalize, utf_void__void);
986
987                 if (fi)
988                         if (!(fi->flags & ACC_STATIC))
989                                 c->finalizer = fi;
990         }
991         RT_TIMING_GET_TIME(time_finalizer);
992
993         /* final tasks */
994
995         linker_compute_subclasses(c);
996
997         /* FIXME: this is completely useless now */
998         RT_TIMING_GET_TIME(time_subclasses);
999
1000 #if USES_NEW_SUBTYPE
1001         if (!build_display(c))
1002                 return NULL;
1003 #endif
1004
1005         /* revert the linking state and class is linked */
1006
1007         c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
1008
1009         /* check worklist */
1010
1011         /* XXX must this also be done in case of exception? */
1012
1013         while (worklist != NULL) {
1014                 method_worklist *wi = worklist;
1015
1016                 worklist = worklist->next;
1017
1018                 INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
1019                 jit_invalidate_code(wi->m);
1020
1021                 /* XXX put worklist into dump memory? */
1022                 FREE(wi, method_worklist);
1023         }
1024
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);
1034
1035         /* just return c to show that we didn't had a problem */
1036
1037         return c;
1038 }
1039
1040
1041 /* link_array ******************************************************************
1042
1043    This function is called by link_class to create the arraydescriptor
1044    for an array class.
1045
1046    This function returns NULL if the array cannot be linked because
1047    the component type has not been linked yet.
1048
1049 *******************************************************************************/
1050
1051 static arraydescriptor *link_array(classinfo *c)
1052 {
1053         classinfo       *comp;
1054         s4               namelen;
1055         arraydescriptor *desc;
1056         vftbl_t         *compvftbl;
1057         utf             *u;
1058
1059         comp = NULL;
1060         namelen = c->name->blength;
1061
1062         /* Check the component type */
1063
1064         switch (c->name->text[1]) {
1065         case '[':
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)))
1069                         return NULL;
1070                 break;
1071
1072         case 'L':
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)))
1076                         return NULL;
1077                 break;
1078         }
1079
1080         /* If the component type has not been linked, link it now */
1081
1082         assert(!comp || (comp->state & CLASS_LOADED));
1083
1084         if (comp && !(comp->state & CLASS_LINKED))
1085                 if (!link_class(comp))
1086                         return NULL;
1087
1088         /* Allocate the arraydescriptor */
1089
1090         desc = NEW(arraydescriptor);
1091
1092         if (comp) {
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);
1097                 
1098                 compvftbl = comp->vftbl;
1099
1100                 if (!compvftbl) {
1101                         log_text("Component class has no vftbl");
1102                         assert(0);
1103                 }
1104
1105                 desc->componentvftbl = compvftbl;
1106                 
1107                 if (compvftbl->arraydesc) {
1108                         desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1109
1110                         if (compvftbl->arraydesc->dimension >= 255) {
1111                                 log_text("Creating array of dimension >255");
1112                                 assert(0);
1113                         }
1114
1115                         desc->dimension = compvftbl->arraydesc->dimension + 1;
1116                         desc->elementtype = compvftbl->arraydesc->elementtype;
1117
1118                 } else {
1119                         desc->elementvftbl = compvftbl;
1120                         desc->dimension = 1;
1121                         desc->elementtype = ARRAYTYPE_OBJECT;
1122                 }
1123
1124         } else {
1125                 /* c is an array of a primitive type */
1126                 switch (c->name->text[1]) {
1127                 case 'Z':
1128                         desc->arraytype = ARRAYTYPE_BOOLEAN;
1129                         desc->dataoffset = OFFSET(java_booleanarray_t,data);
1130                         desc->componentsize = sizeof(u1);
1131                         break;
1132
1133                 case 'B':
1134                         desc->arraytype = ARRAYTYPE_BYTE;
1135                         desc->dataoffset = OFFSET(java_bytearray_t,data);
1136                         desc->componentsize = sizeof(u1);
1137                         break;
1138
1139                 case 'C':
1140                         desc->arraytype = ARRAYTYPE_CHAR;
1141                         desc->dataoffset = OFFSET(java_chararray_t,data);
1142                         desc->componentsize = sizeof(u2);
1143                         break;
1144
1145                 case 'D':
1146                         desc->arraytype = ARRAYTYPE_DOUBLE;
1147                         desc->dataoffset = OFFSET(java_doublearray_t,data);
1148                         desc->componentsize = sizeof(double);
1149                         break;
1150
1151                 case 'F':
1152                         desc->arraytype = ARRAYTYPE_FLOAT;
1153                         desc->dataoffset = OFFSET(java_floatarray_t,data);
1154                         desc->componentsize = sizeof(float);
1155                         break;
1156
1157                 case 'I':
1158                         desc->arraytype = ARRAYTYPE_INT;
1159                         desc->dataoffset = OFFSET(java_intarray_t,data);
1160                         desc->componentsize = sizeof(s4);
1161                         break;
1162
1163                 case 'J':
1164                         desc->arraytype = ARRAYTYPE_LONG;
1165                         desc->dataoffset = OFFSET(java_longarray_t,data);
1166                         desc->componentsize = sizeof(s8);
1167                         break;
1168
1169                 case 'S':
1170                         desc->arraytype = ARRAYTYPE_SHORT;
1171                         desc->dataoffset = OFFSET(java_shortarray_t,data);
1172                         desc->componentsize = sizeof(s2);
1173                         break;
1174
1175                 default:
1176                         exceptions_throw_noclassdeffounderror(c->name);
1177                         return NULL;
1178                 }
1179                 
1180                 desc->componentvftbl = NULL;
1181                 desc->elementvftbl = NULL;
1182                 desc->dimension = 1;
1183                 desc->elementtype = desc->arraytype;
1184         }
1185
1186         return desc;
1187 }
1188
1189
1190 /* linker_compute_subclasses ***************************************************
1191
1192    XXX
1193
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.
1199
1200 *******************************************************************************/
1201
1202 static void linker_compute_subclasses(classinfo *c)
1203 {
1204
1205         LOCK_CLASSRENUMBER_LOCK;
1206
1207         if (!(c->flags & ACC_INTERFACE)) {
1208                 c->nextsub = NULL;
1209                 c->sub     = NULL;
1210 #if USES_NEW_SUBTYPE
1211                 c->vftbl->baseval = 1; /* so it does not look like an interface */
1212 #endif
1213         }
1214
1215         if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
1216                 c->nextsub    = c->super->sub;
1217                 c->super->sub = c;
1218         }
1219
1220         classvalue = 0;
1221
1222 #if !USES_NEW_SUBTYPE
1223         /* compute class values */
1224
1225         linker_compute_class_values(class_java_lang_Object);
1226 #endif
1227
1228         UNLOCK_CLASSRENUMBER_LOCK;
1229
1230 }
1231
1232
1233 /* linker_compute_class_values *************************************************
1234
1235    XXX
1236
1237 *******************************************************************************/
1238
1239 static void linker_compute_class_values(classinfo *c)
1240 {
1241         classinfo *subs;
1242
1243         c->vftbl->baseval = ++classvalue;
1244
1245         subs = c->sub;
1246
1247         while (subs) {
1248                 linker_compute_class_values(subs);
1249
1250                 subs = subs->nextsub;
1251         }
1252
1253         c->vftbl->diffval = classvalue - c->vftbl->baseval;
1254 }
1255
1256
1257 /* linker_addinterface *********************************************************
1258
1259    Is needed by link_class for adding a VTBL to a class. All
1260    interfaces implemented by ic are added as well.
1261
1262    RETURN VALUE:
1263       true.........everything ok
1264           false........an exception has been thrown
1265
1266 *******************************************************************************/
1267
1268 static bool linker_addinterface(classinfo *c, classinfo *ic)
1269 {
1270         s4          j, k;
1271         vftbl_t    *v;
1272         s4          i;
1273         classinfo  *sc;
1274         methodinfo *m;
1275
1276         v = c->vftbl;
1277         i = ic->index;
1278
1279         if (i >= v->interfacetablelength)
1280                 vm_abort("Internal error: interfacetable overflow");
1281
1282         /* if this interface has already been added, return immediately */
1283
1284         if (v->interfacetable[-i] != NULL)
1285                 return true;
1286
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;
1291         }
1292         else {
1293                 v->interfacevftbllength[i] = ic->methodscount;
1294                 v->interfacetable[-i]      = MNEW(methodptr, ic->methodscount);
1295
1296 #if defined(ENABLE_STATISTICS)
1297                 if (opt_stat)
1298                         count_vftbl_len += sizeof(methodptr) *
1299                                 (ic->methodscount + (ic->methodscount == 0));
1300 #endif
1301
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]);
1306
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
1312                                                    ic->methods[j]).  */
1313                                                 if (!classcache_add_constraints_for_params(
1314                                                                         c->classloader, ic->classloader,
1315                                                                         &(ic->methods[j])))
1316                                                 {
1317                                                         return false;
1318                                                 }
1319 #endif
1320
1321                                                 /* XXX taken from gcj */
1322                                                 /* check for ACC_STATIC: IncompatibleClassChangeError */
1323
1324                                                 /* check for !ACC_PUBLIC: IllegalAccessError */
1325
1326                                                 /* check for ACC_ABSTRACT: AbstracMethodError,
1327                                                    not sure about that one */
1328
1329                                                 v->interfacetable[-i][j] = v->table[m->vftblindex];
1330                                                 goto foundmethod;
1331                                         }
1332                                 }
1333                         }
1334
1335                         /* If no method was found, insert the AbstractMethodError
1336                            stub. */
1337
1338 #if defined(ENABLE_JIT)
1339 # if defined(ENABLE_INTRP)
1340                         if (opt_intrp)
1341                                 v->interfacetable[-i][j] =
1342                                         (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1343                         else
1344 # endif
1345                                 v->interfacetable[-i][j] =
1346                                         (methodptr) (ptrint) &asm_abstractmethoderror;
1347 #else
1348                         v->interfacetable[-i][j] =
1349                                 (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
1350 #endif
1351
1352                 foundmethod:
1353                         ;
1354                 }
1355         }
1356
1357         /* add superinterfaces of this interface */
1358
1359         for (j = 0; j < ic->interfacescount; j++)
1360                 if (!linker_addinterface(c, ic->interfaces[j]))
1361                         return false;
1362
1363         /* everything ok */
1364
1365         return true;
1366 }
1367
1368
1369 /* class_highestinterface ******************************************************
1370
1371    Used by the function link_class to determine the amount of memory
1372    needed for the interface table.
1373
1374 *******************************************************************************/
1375
1376 static s4 class_highestinterface(classinfo *c)
1377 {
1378         s4 h;
1379         s4 h2;
1380         s4 i;
1381         
1382     /* check for ACC_INTERFACE bit already done in link_class_intern */
1383
1384     h = c->index;
1385
1386         for (i = 0; i < c->interfacescount; i++) {
1387                 h2 = class_highestinterface(c->interfaces[i]);
1388
1389                 if (h2 > h)
1390                         h = h2;
1391         }
1392
1393         return h;
1394 }
1395
1396 #if defined(__cplusplus)
1397 }
1398 #endif
1399
1400 /*
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  * ---------------------------------------------------------------------
1405  * Local variables:
1406  * mode: c
1407  * indent-tabs-mode: t
1408  * c-basic-offset: 4
1409  * tab-width: 4
1410  * End:
1411  * vim:noexpandtab:sw=4:ts=4:
1412  */