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