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