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