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