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