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