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