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