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