1 /* loader.c - class loader functions
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
28 Changes: Andreas Krall
32 $Id: loader.c 687 2003-12-04 22:29:54Z edwin $
47 #include "toolbox/memory.h"
48 #include "toolbox/loging.h"
49 #include "threads/thread.h"
60 /* global variables ***********************************************************/
62 bool opt_rt = false; /* true if RTA parse should be used RT-CO */
63 bool opt_xta = false; /* true if XTA parse should be used XTA-CO */
64 bool opt_vta = false; /* true if VTA parse should be used VTA-CO */
66 int count_class_infos = 0; /* variables for measurements */
67 int count_const_pool_len = 0;
68 int count_vftbl_len = 0;
69 int count_all_methods = 0;
70 int count_vmcode_len = 0;
71 int count_extable_len = 0;
72 int count_class_loads = 0;
73 int count_class_inits = 0;
75 bool loadverbose = false; /* switches for debug messages */
76 bool linkverbose = false;
77 bool initverbose = false;
79 bool makeinitializations = true;
81 bool getloadingtime = false; /* to measure the runtime */
82 long int loadingtime = 0;
84 static s4 interfaceindex; /* sequential numbering of interfaces */
86 list unloadedclasses; /* list of all referenced but not loaded classes */
87 list unlinkedclasses; /* list of all loaded but not linked classes */
88 list linkedclasses; /* list of all completely linked classes */
91 /* utf-symbols for pointer comparison of frequently used strings */
93 static utf *utf_innerclasses; /* InnerClasses */
94 static utf *utf_constantvalue; /* ConstantValue */
95 static utf *utf_code; /* Code */
96 static utf *utf_finalize; /* finalize */
97 static utf *utf_fidesc; /* ()V changed */
98 static utf *utf_clinit; /* <clinit> */
99 static utf *utf_initsystemclass; /* initializeSystemClass */
100 static utf *utf_systemclass; /* java/lang/System */
101 static utf *utf_vmclassloader; /*java/lang/VMClassLoader*/
102 static utf *utf_vmclass; /*java/lang/VMClassLoader*/
103 static utf *utf_initialize;
104 static utf *utf_initializedesc;
108 static unzFile uf = 0;
118 /* important system classes ***************************************************/
120 classinfo *class_java_lang_Object;
121 classinfo *class_java_lang_String;
123 classinfo *class_java_lang_Throwable;
124 classinfo *class_java_lang_Cloneable;
125 classinfo *class_java_io_Serializable;
127 /* Pseudo classes for the typechecker */
128 classinfo *pseudo_class_Arraystub = NULL;
129 classinfo *pseudo_class_Null = NULL;
130 vftbl *pseudo_class_Arraystub_vftbl = NULL;
133 /* These are made static so they cannot be used for throwing in native */
135 static classinfo *class_java_lang_ClassCastException;
136 static classinfo *class_java_lang_NullPointerException;
137 static classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
138 static classinfo *class_java_lang_NegativeArraySizeException;
139 static classinfo *class_java_lang_OutOfMemoryError;
140 static classinfo *class_java_lang_ArithmeticException;
141 static classinfo *class_java_lang_ArrayStoreException;
142 static classinfo *class_java_lang_ThreadDeath;
144 static methodinfo method_clone_array;
146 static int loader_inited = 0;
148 /******************************************************************************
150 structure for primitive classes: contains the class for wrapping the
151 primitive type, the primitive class, the name of the class for wrapping,
152 the one character type signature and the name of the primitive class
154 ******************************************************************************/
156 /* CAUTION: Don't change the order of the types. This table is indexed
157 * by the ARRAYTYPE_ constants (expcept ARRAYTYPE_OBJECT).
159 primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
160 { NULL, NULL, "java/lang/Integer", 'I', "int" , "[I", NULL, NULL },
161 { NULL, NULL, "java/lang/Long", 'J', "long" , "[J", NULL, NULL },
162 { NULL, NULL, "java/lang/Float", 'F', "float" , "[F", NULL, NULL },
163 { NULL, NULL, "java/lang/Double", 'D', "double" , "[D", NULL, NULL },
164 { NULL, NULL, "java/lang/Byte", 'B', "byte" , "[B", NULL, NULL },
165 { NULL, NULL, "java/lang/Character", 'C', "char" , "[C", NULL, NULL },
166 { NULL, NULL, "java/lang/Short", 'S', "short" , "[S", NULL, NULL },
167 { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" , "[Z", NULL, NULL },
168 { NULL, NULL, "java/lang/Void", 'V', "void" , NULL, NULL, NULL }};
170 /* instances of important system classes **************************************/
172 java_objectheader *proto_java_lang_ClassCastException;
173 java_objectheader *proto_java_lang_NullPointerException;
174 java_objectheader *proto_java_lang_ArrayIndexOutOfBoundsException;
175 java_objectheader *proto_java_lang_NegativeArraySizeException;
176 java_objectheader *proto_java_lang_OutOfMemoryError;
177 java_objectheader *proto_java_lang_ArithmeticException;
178 java_objectheader *proto_java_lang_ArrayStoreException;
179 java_objectheader *proto_java_lang_ThreadDeath;
181 /************* functions for reading classdata *********************************
183 getting classdata in blocks of variable size
184 (8,16,32,64-bit integer or float)
186 *******************************************************************************/
188 static char *classpath = ""; /* searchpath for classfiles */
189 static u1 *classbuffer = NULL; /* pointer to buffer with classfile-data */
190 static u1 *classbuf_pos; /* current position in classfile buffer */
191 static int classbuffer_size; /* size of classfile-data */
193 /* transfer block of classfile data into a buffer */
195 #define suck_nbytes(buffer,len) memcpy(buffer,classbuf_pos+1,len); \
198 /* skip block of classfile data */
200 #define skip_nbytes(len) classbuf_pos+=len;
204 return *++classbuf_pos;
208 u1 a=suck_u1(), b=suck_u1();
209 return ((u2)a<<8)+(u2)b;
213 u1 a=suck_u1(), b=suck_u1(), c=suck_u1(), d=suck_u1();
214 return ((u4)a<<24)+((u4)b<<16)+((u4)c<<8)+(u4)d;
216 #define suck_s8() (s8) suck_u8()
217 #define suck_s2() (s2) suck_u2()
218 #define suck_s4() (s4) suck_u4()
219 #define suck_s1() (s1) suck_u1()
222 /* get u8 from classfile data */
229 return (hi << 32) + lo;
239 /* get float from classfile data */
240 static float suck_float()
247 for (i = 0; i < 4; i++) buffer[3 - i] = suck_u1();
248 memcpy((u1*) (&f), buffer, 4);
250 suck_nbytes((u1*) (&f), 4);
253 PANICIF (sizeof(float) != 4, "Incompatible float-format");
259 /* get double from classfile data */
260 static double suck_double()
267 for (i = 0; i < 8; i++) buffer[7 - i] = suck_u1 ();
268 memcpy((u1*) (&d), buffer, 8);
270 suck_nbytes((u1*) (&d), 8);
273 PANICIF (sizeof(double) != 8, "Incompatible double-format" );
279 /************************** function suck_init *********************************
281 called once at startup, sets the searchpath for the classfiles
283 *******************************************************************************/
285 void suck_init(char *cpath)
292 /************************** function suck_start ********************************
294 returns true if classbuffer is already loaded or a file for the
295 specified class has succussfully been read in. All directories of
296 the searchpath are used to find the classfile (<classname>.class).
297 Returns false if no classfile is found and writes an error message.
299 *******************************************************************************/
301 bool suck_start(utf *classname)
304 #define MAXFILENAME 1000 /* maximum length of a filename */
306 char filename[MAXFILENAME+10]; /* room for '.class' */
307 char *pathpos; /* position in searchpath */
308 char c, *utf_ptr; /* pointer to the next utf8-character */
310 int filenamelen, err;
314 if (classbuffer) /* classbuffer is already valid */
321 /* skip path separator */
323 while (*pathpos == ':')
326 /* extract directory from searchpath */
329 while ((*pathpos) && (*pathpos != ':')) {
330 PANICIF (filenamelen >= MAXFILENAME, "Filename too long");
331 filename[filenamelen++] = *(pathpos++);
335 if (filenamelen > 4) {
336 if ( ((filename[filenamelen - 1] == 'p') || (filename[filenamelen - 1] == 'P')) &
337 ((filename[filenamelen - 2] == 'i') || (filename[filenamelen - 2] == 'I')) &
338 ((filename[filenamelen - 3] == 'z') || (filename[filenamelen - 3] == 'Z')) ) {
345 filename[filenamelen++] = '\0';
346 if (uf == 0) uf = unzOpen(filename);
348 utf_ptr = classname->text;
350 while (utf_ptr < utf_end(classname)) {
351 PANICIF (filenamelen >= MAXFILENAME, "Filename too long");
353 if ((c <= ' ' || c > 'z') && (c != '/')) /* invalid character */
355 filename[filenamelen++] = c;
357 strcpy(filename + filenamelen, ".class");
358 if (cacao_locate(uf,classname) == UNZ_OK) {
359 unz_file_info file_info;
360 log_text("Class found in zip file");
361 if (unzGetCurrentFileInfo(uf, &file_info, filename,
362 sizeof(filename), NULL, 0, NULL, 0) == UNZ_OK) {
363 if (unzOpenCurrentFile(uf) == UNZ_OK) {
364 classbuffer_size = file_info.uncompressed_size;
365 classbuffer = MNEW(u1, classbuffer_size);
366 classbuf_pos = classbuffer - 1;
367 /*printf("classfile size: %d\n",file_info.uncompressed_size);*/
368 if (unzReadCurrentFile(uf, classbuffer, classbuffer_size) == classbuffer_size) {
369 unzCloseCurrentFile(uf);
372 MFREE(classbuffer, u1, classbuffer_size);
373 log_text("Error while unzipping");
375 } else log_text("Error while opening file in archive");
376 } else log_text("Error while retrieving fileinfo");
378 unzCloseCurrentFile(uf);
383 filename[filenamelen++] = '/';
385 /* add classname to filename */
387 utf_ptr = classname->text;
388 while (utf_ptr < utf_end(classname)) {
389 PANICIF (filenamelen >= MAXFILENAME, "Filename too long");
391 if ((c <= ' ' || c > 'z') && (c != '/')) /* invalid character */
393 filename[filenamelen++] = c;
398 strcpy(filename + filenamelen, ".class");
400 classfile = fopen(filename, "r");
401 if (classfile) { /* file exists */
403 /* determine size of classfile */
405 err = stat(filename, &buffer);
407 if (!err) { /* read classfile data */
408 classbuffer_size = buffer.st_size;
409 classbuffer = MNEW(u1, classbuffer_size);
410 classbuf_pos = classbuffer - 1;
411 fread(classbuffer, 1, classbuffer_size, classfile);
419 sprintf(logtext, "Warning: Can not open class file '%s'", filename);
427 /************************** function suck_stop *********************************
429 frees memory for buffer with classfile data.
430 Caution: this function may only be called if buffer has been allocated
431 by suck_start with reading a file
433 *******************************************************************************/
437 /* determine amount of classdata not retrieved by suck-operations */
439 int classdata_left = ((classbuffer + classbuffer_size) - classbuf_pos - 1);
441 if (classdata_left > 0) {
443 sprintf(logtext, "There are %d access bytes at end of classfile",
450 MFREE(classbuffer, u1, classbuffer_size);
455 /******************************************************************************/
456 /******************* Some support functions ***********************************/
457 /******************************************************************************/
461 void fprintflags (FILE *fp, u2 f)
463 if ( f & ACC_PUBLIC ) fprintf (fp," PUBLIC");
464 if ( f & ACC_PRIVATE ) fprintf (fp," PRIVATE");
465 if ( f & ACC_PROTECTED ) fprintf (fp," PROTECTED");
466 if ( f & ACC_STATIC ) fprintf (fp," STATIC");
467 if ( f & ACC_FINAL ) fprintf (fp," FINAL");
468 if ( f & ACC_SYNCHRONIZED ) fprintf (fp," SYNCHRONIZED");
469 if ( f & ACC_VOLATILE ) fprintf (fp," VOLATILE");
470 if ( f & ACC_TRANSIENT ) fprintf (fp," TRANSIENT");
471 if ( f & ACC_NATIVE ) fprintf (fp," NATIVE");
472 if ( f & ACC_INTERFACE ) fprintf (fp," INTERFACE");
473 if ( f & ACC_ABSTRACT ) fprintf (fp," ABSTRACT");
476 /********** internal function: printflags (only for debugging) ***************/
477 void printflags (u2 f)
479 if ( f & ACC_PUBLIC ) printf (" PUBLIC");
480 if ( f & ACC_PRIVATE ) printf (" PRIVATE");
481 if ( f & ACC_PROTECTED ) printf (" PROTECTED");
482 if ( f & ACC_STATIC ) printf (" STATIC");
483 if ( f & ACC_FINAL ) printf (" FINAL");
484 if ( f & ACC_SYNCHRONIZED ) printf (" SYNCHRONIZED");
485 if ( f & ACC_VOLATILE ) printf (" VOLATILE");
486 if ( f & ACC_TRANSIENT ) printf (" TRANSIENT");
487 if ( f & ACC_NATIVE ) printf (" NATIVE");
488 if ( f & ACC_INTERFACE ) printf (" INTERFACE");
489 if ( f & ACC_ABSTRACT ) printf (" ABSTRACT");
493 /************************* Function: skipattribute *****************************
495 skips a (1) 'attribute' structure in the class file
497 *******************************************************************************/
499 static void skipattribute ()
502 skip_nbytes(suck_u4());
505 /********************** Function: skipattributebody ****************************
507 skips an attribute after the 16 bit reference to attribute_name has already
510 *******************************************************************************/
512 static void skipattributebody ()
514 skip_nbytes(suck_u4());
517 /************************* Function: skipattributes ****************************
519 skips num attribute structures
521 *******************************************************************************/
523 static void skipattributes (u4 num)
526 for (i = 0; i < num; i++)
530 /******************** function: innerclass_getconstant ************************
532 like class_getconstant, but if cptags is ZERO null is returned
534 *******************************************************************************/
536 voidptr innerclass_getconstant (classinfo *c, u4 pos, u4 ctype)
538 /* invalid position in constantpool */
539 if (pos >= c->cpcount)
540 panic ("Attempt to access constant outside range");
542 /* constantpool entry of type 0 */
546 /* check type of constantpool entry */
547 if (c->cptags[pos] != ctype) {
548 sprintf (logtext, "Type mismatch on constant: %d requested, %d here (innerclass_getconstant)",
549 (int) ctype, (int) c->cptags[pos] );
553 return c->cpinfos[pos];
556 /************************ function: attribute_load ****************************
558 read attributes from classfile
560 *******************************************************************************/
562 static void attribute_load (u4 num, classinfo *c)
566 for (i = 0; i < num; i++) {
567 /* retrieve attribute name */
568 utf *aname = class_getconstant (c, suck_u2(), CONSTANT_Utf8);
570 if ( aname == utf_innerclasses) {
571 /* innerclasses attribute */
573 /* skip attribute length */
575 /* number of records */
576 c->innerclasscount = suck_u2();
577 /* allocate memory for innerclass structure */
578 c->innerclass = MNEW (innerclassinfo, c->innerclasscount);
580 for (j=0;j<c->innerclasscount;j++) {
582 /* The innerclass structure contains a class with an encoded name,
583 its defining scope, its simple name and a bitmask of the access flags.
584 If an inner class is not a member, its outer_class is NULL,
585 if a class is anonymous, its name is NULL. */
587 innerclassinfo *info = c->innerclass + j;
589 info->inner_class = innerclass_getconstant(c, suck_u2(), CONSTANT_Class); /* CONSTANT_Class_info index */
590 info->outer_class = innerclass_getconstant(c, suck_u2(), CONSTANT_Class); /* CONSTANT_Class_info index */
591 info->name = innerclass_getconstant(c, suck_u2(), CONSTANT_Utf8); /* CONSTANT_Utf8_info index */
592 info->flags = suck_u2 (); /* access_flags bitmask */
595 /* unknown attribute */
596 skipattributebody ();
601 /******************* function: checkfielddescriptor ****************************
603 checks whether a field-descriptor is valid and aborts otherwise
604 all referenced classes are inserted into the list of unloaded classes
606 *******************************************************************************/
608 static void checkfielddescriptor (char *utf_ptr, char *end_pos)
610 char *tstart; /* pointer to start of classname */
612 char *start = utf_ptr;
614 switch (*utf_ptr++) {
628 if (!class_from_descriptor(start,end_pos,&utf_ptr,CLASSLOAD_NEW))
629 panic ("Ill formed descriptor");
633 panic ("Ill formed descriptor");
636 /* exceeding characters */
637 if (utf_ptr!=end_pos) panic ("descriptor has exceeding chars");
641 /******************* function checkmethoddescriptor ****************************
643 checks whether a method-descriptor is valid and aborts otherwise.
644 All referenced classes are inserted into the list of unloaded classes.
646 *******************************************************************************/
648 static void checkmethoddescriptor (utf *d)
650 char *utf_ptr = d->text; /* current position in utf text */
651 char *end_pos = utf_end(d); /* points behind utf string */
652 char *tstart; /* pointer to start of classname */
656 /* method descriptor must start with parenthesis */
657 /* XXX check length */
658 if (*utf_ptr++ != '(') panic ("Missing '(' in method descriptor");
660 /* XXX check length */
661 /* check arguments */
662 while ((c = *utf_ptr++) != ')') {
679 if (!class_from_descriptor(start,end_pos,&utf_ptr,CLASSLOAD_NEW))
680 panic ("Ill formed method descriptor");
684 panic ("Ill formed methodtype-descriptor");
688 /* check returntype */
690 /* returntype void */
691 if ((utf_ptr+1) != end_pos) panic ("Method-descriptor has exceeding chars");
694 /* treat as field-descriptor */
695 checkfielddescriptor (utf_ptr,end_pos);
698 /***************** Function: print_arraydescriptor ****************************
700 Debugging helper for displaying an arraydescriptor
702 *******************************************************************************/
705 print_arraydescriptor(FILE *file,arraydescriptor *desc)
708 fprintf(file,"<NULL>");
713 if (desc->componentvftbl) {
714 if (desc->componentvftbl->class)
715 utf_fprint(file,desc->componentvftbl->class->name);
717 fprintf(file,"<no classinfo>");
723 if (desc->elementvftbl) {
724 if (desc->elementvftbl->class)
725 utf_fprint(file,desc->elementvftbl->class->name);
727 fprintf(file,"<no classinfo>");
731 fprintf(file,",%d,%d,%d,%d}",desc->arraytype,desc->dimension,
732 desc->dataoffset,desc->componentsize);
735 /******************************************************************************/
736 /************************** Functions for fields ****************************/
737 /******************************************************************************/
740 /************************ Function: field_load *********************************
742 Load everything about a class field from the class file and fill a
743 'fieldinfo' structure. For static fields, space in the data segment is
746 *******************************************************************************/
748 static void field_load (fieldinfo *f, classinfo *c)
753 f -> flags = suck_u2 (); /* ACC flags */
754 f -> name = class_getconstant (c, suck_u2(), CONSTANT_Utf8); /* name of field */
755 f -> descriptor = class_getconstant (c, suck_u2(), CONSTANT_Utf8); /* JavaVM descriptor */
756 f -> type = jtype = desc_to_type (f->descriptor); /* data type */
757 f -> offset = 0; /* offset from start of object */
761 case TYPE_INT: f->value.i = 0; break;
762 case TYPE_FLOAT: f->value.f = 0.0; break;
763 case TYPE_DOUBLE: f->value.d = 0.0; break;
764 case TYPE_ADDRESS: f->value.a = NULL;
765 heap_addreference (&(f->value.a)); /* make global reference (GC) */
769 f->value.l = 0; break;
771 f->value.l.low = 0; f->value.l.high = 0; break;
775 /* read attributes */
777 for (i=0; i<attrnum; i++) {
781 aname = class_getconstant (c, suck_u2(), CONSTANT_Utf8);
783 if ( aname != utf_constantvalue ) {
784 /* unknown attribute */
785 skipattributebody ();
788 /* constant value attribute */
790 /* skip attribute length */
792 /* index of value in constantpool */
795 /* initialize field with value from constantpool */
798 constant_integer *ci =
799 class_getconstant(c, pindex, CONSTANT_Integer);
800 f->value.i = ci -> value;
806 class_getconstant(c, pindex, CONSTANT_Long);
808 f->value.l = cl -> value;
814 class_getconstant(c, pindex, CONSTANT_Float);
816 f->value.f = cf->value;
821 constant_double *cd =
822 class_getconstant(c, pindex, CONSTANT_Double);
824 f->value.d = cd->value;
829 utf *u = class_getconstant(c, pindex, CONSTANT_String);
830 /* create javastring from compressed utf8-string */
831 f->value.a = literalstring_new(u);
836 log_text ("Invalid Constant - Type");
845 /********************** function: field_free **********************************/
847 static void field_free (fieldinfo *f)
853 /**************** Function: field_display (debugging only) ********************/
855 void field_display (fieldinfo *f)
858 printflags (f -> flags);
860 utf_display (f -> name);
862 utf_display (f -> descriptor);
863 printf (" offset: %ld\n", (long int) (f -> offset) );
867 /******************************************************************************/
868 /************************* Functions for methods ******************************/
869 /******************************************************************************/
872 /*********************** Function: method_load *********************************
874 Loads a method from the class file and fills an existing 'methodinfo'
875 structure. For native methods, the function pointer field is set to the
876 real function pointer, for JavaVM methods a pointer to the compiler is used
879 *******************************************************************************/
881 static void method_load (methodinfo *m, classinfo *c)
891 m -> flags = suck_u2 ();
892 m -> name = class_getconstant (c, suck_u2(), CONSTANT_Utf8);
893 m -> descriptor = class_getconstant (c, suck_u2(), CONSTANT_Utf8);
896 m -> exceptiontable = NULL;
897 m -> entrypoint = NULL;
899 m -> stubroutine = NULL;
900 m -> methodUsed = NOTUSED;
901 m -> monoPoly = MONO;
903 m -> subRedefsUsed = 0;
907 if (! (m->flags & ACC_NATIVE) ) {
908 m -> stubroutine = createcompilerstub (m);
912 functionptr f = native_findfunction
913 (c->name, m->name, m->descriptor, (m->flags & ACC_STATIC) != 0);
915 m -> stubroutine = createnativestub (f, m);
921 for (i=0; i<attrnum; i++) {
924 aname = class_getconstant (c, suck_u2(), CONSTANT_Utf8);
926 if ( aname != utf_code) {
927 skipattributebody ();
930 if (m -> jcode) panic ("Two code-attributes for one method!");
933 m -> maxstack = suck_u2();
934 m -> maxlocals = suck_u2();
935 m -> jcodelength = suck_u4();
936 m -> jcode = MNEW (u1, m->jcodelength);
937 suck_nbytes (m->jcode, m->jcodelength);
938 m -> exceptiontablelength = suck_u2 ();
939 m -> exceptiontable =
940 MNEW (exceptiontable, m->exceptiontablelength);
943 count_vmcode_len += m->jcodelength + 18;
944 count_extable_len += 8 * m->exceptiontablelength;
947 for (e=0; e < m->exceptiontablelength; e++) {
949 m -> exceptiontable[e].startpc = suck_u2();
950 m -> exceptiontable[e].endpc = suck_u2();
951 m -> exceptiontable[e].handlerpc = suck_u2();
954 if (!idx) m -> exceptiontable[e].catchtype = NULL;
956 m -> exceptiontable[e].catchtype =
957 class_getconstant (c, idx, CONSTANT_Class);
961 skipattributes ( suck_u2() );
968 /********************* Function: method_free ***********************************
970 frees all memory that was allocated for this method
972 *******************************************************************************/
974 static void method_free (methodinfo *m)
976 if (m->jcode) MFREE (m->jcode, u1, m->jcodelength);
977 if (m->exceptiontable)
978 MFREE (m->exceptiontable, exceptiontable, m->exceptiontablelength);
979 if (m->mcode) CFREE (m->mcode, m->mcodelength);
980 if (m->stubroutine) {
981 if (m->flags & ACC_NATIVE) removenativestub (m->stubroutine);
982 else removecompilerstub (m->stubroutine);
987 /************** Function: method_display (debugging only) **************/
989 void method_display (methodinfo *m)
992 printflags (m -> flags);
994 utf_display (m -> name);
996 utf_display (m -> descriptor);
1001 /******************** Function: method_canoverwrite ****************************
1003 Check if m and old are identical with respect to type and name. This means
1004 that old can be overwritten with m.
1006 *******************************************************************************/
1008 static bool method_canoverwrite (methodinfo *m, methodinfo *old)
1010 if (m->name != old->name) return false;
1011 if (m->descriptor != old->descriptor) return false;
1012 if (m->flags & ACC_STATIC) return false;
1019 /******************************************************************************/
1020 /************************ Functions for class *********************************/
1021 /******************************************************************************/
1024 /******************** function:: class_getconstant ******************************
1026 retrieves the value at position 'pos' of the constantpool of a class
1027 if the type of the value is other than 'ctype' the system is stopped
1029 *******************************************************************************/
1031 voidptr class_getconstant (classinfo *c, u4 pos, u4 ctype)
1033 /* invalid position in constantpool */
1034 if (pos >= c->cpcount)
1035 panic ("Attempt to access constant outside range");
1037 /* check type of constantpool entry */
1039 if (c->cptags[pos] != ctype) {
1040 class_showconstantpool(c);
1041 sprintf (logtext, "Type mismatch on constant: %d requested, %d here (class_getconstant)",
1042 (int) ctype, (int) c->cptags[pos] );
1046 return c->cpinfos[pos];
1050 /********************* Function: class_constanttype ****************************
1052 Determines the type of a class entry in the ConstantPool
1054 *******************************************************************************/
1056 u4 class_constanttype (classinfo *c, u4 pos)
1058 if (pos >= c->cpcount)
1059 panic ("Attempt to access constant outside range");
1060 return c->cptags[pos];
1064 /******************** function: class_loadcpool ********************************
1066 loads the constantpool of a class,
1067 the entries are transformed into a simpler format
1068 by resolving references
1069 (a detailed overview of the compact structures can be found in global.h)
1071 *******************************************************************************/
1073 static void class_loadcpool (classinfo *c)
1076 /* The following structures are used to save information which cannot be
1077 processed during the first pass. After the complete constantpool has
1078 been traversed the references can be resolved.
1079 (only in specific order) */
1081 /* CONSTANT_Class_info entries */
1082 typedef struct forward_class {
1083 struct forward_class *next;
1088 /* CONSTANT_String */
1089 typedef struct forward_string {
1090 struct forward_string *next;
1095 /* CONSTANT_NameAndType */
1096 typedef struct forward_nameandtype {
1097 struct forward_nameandtype *next;
1101 } forward_nameandtype;
1103 /* CONSTANT_Fieldref, CONSTANT_Methodref or CONSTANT_InterfaceMethodref */
1104 typedef struct forward_fieldmethint {
1105 struct forward_fieldmethint *next;
1109 u2 nameandtype_index;
1110 } forward_fieldmethint;
1114 long int dumpsize = dump_size ();
1116 forward_class *forward_classes = NULL;
1117 forward_string *forward_strings = NULL;
1118 forward_nameandtype *forward_nameandtypes = NULL;
1119 forward_fieldmethint *forward_fieldmethints = NULL;
1121 /* number of entries in the constant_pool table */
1122 u4 cpcount = c -> cpcount = suck_u2();
1123 /* allocate memory */
1124 u1 *cptags = c -> cptags = MNEW (u1, cpcount);
1125 voidptr *cpinfos = c -> cpinfos = MNEW (voidptr, cpcount);
1128 count_const_pool_len += (sizeof(voidptr) + 1) * cpcount;
1131 /* initialize constantpool */
1132 for (idx=0; idx<cpcount; idx++) {
1133 cptags[idx] = CONSTANT_UNUSED;
1134 cpinfos[idx] = NULL;
1138 /******* first pass *******/
1139 /* entries which cannot be resolved now are written into
1140 temporary structures and traversed again later */
1143 while (idx < cpcount) {
1144 /* get constant type */
1148 case CONSTANT_Class: {
1149 forward_class *nfc = DNEW(forward_class);
1151 nfc -> next = forward_classes;
1152 forward_classes = nfc;
1154 nfc -> thisindex = idx;
1155 /* reference to CONSTANT_NameAndType */
1156 nfc -> name_index = suck_u2 ();
1162 case CONSTANT_Fieldref:
1163 case CONSTANT_Methodref:
1164 case CONSTANT_InterfaceMethodref: {
1165 forward_fieldmethint *nff = DNEW (forward_fieldmethint);
1167 nff -> next = forward_fieldmethints;
1168 forward_fieldmethints = nff;
1170 nff -> thisindex = idx;
1173 /* class or interface type that contains the declaration of the field or method */
1174 nff -> class_index = suck_u2 ();
1175 /* name and descriptor of the field or method */
1176 nff -> nameandtype_index = suck_u2 ();
1182 case CONSTANT_String: {
1183 forward_string *nfs = DNEW (forward_string);
1185 nfs -> next = forward_strings;
1186 forward_strings = nfs;
1188 nfs -> thisindex = idx;
1189 /* reference to CONSTANT_Utf8_info with string characters */
1190 nfs -> string_index = suck_u2 ();
1196 case CONSTANT_NameAndType: {
1197 forward_nameandtype *nfn = DNEW (forward_nameandtype);
1199 nfn -> next = forward_nameandtypes;
1200 forward_nameandtypes = nfn;
1202 nfn -> thisindex = idx;
1203 /* reference to CONSTANT_Utf8_info containing simple name */
1204 nfn -> name_index = suck_u2 ();
1205 /* reference to CONSTANT_Utf8_info containing field or method descriptor */
1206 nfn -> sig_index = suck_u2 ();
1212 case CONSTANT_Integer: {
1213 constant_integer *ci = NEW (constant_integer);
1216 count_const_pool_len += sizeof(constant_integer);
1219 ci -> value = suck_s4 ();
1220 cptags [idx] = CONSTANT_Integer;
1227 case CONSTANT_Float: {
1228 constant_float *cf = NEW (constant_float);
1231 count_const_pool_len += sizeof(constant_float);
1234 cf -> value = suck_float ();
1235 cptags [idx] = CONSTANT_Float;
1241 case CONSTANT_Long: {
1242 constant_long *cl = NEW(constant_long);
1245 count_const_pool_len += sizeof(constant_long);
1248 cl -> value = suck_s8 ();
1249 cptags [idx] = CONSTANT_Long;
1255 case CONSTANT_Double: {
1256 constant_double *cd = NEW(constant_double);
1259 count_const_pool_len += sizeof(constant_double);
1262 cd -> value = suck_double ();
1263 cptags [idx] = CONSTANT_Double;
1269 case CONSTANT_Utf8: {
1271 /* number of bytes in the bytes array (not string-length) */
1272 u4 length = suck_u2();
1273 cptags [idx] = CONSTANT_Utf8;
1274 /* insert utf-string into the utf-symboltable */
1275 cpinfos [idx] = utf_new(classbuf_pos+1, length);
1276 /* skip bytes of the string */
1277 skip_nbytes(length);
1283 sprintf (logtext, "Unkown constant type: %d",(int) t);
1292 /* resolve entries in temporary structures */
1294 while (forward_classes) {
1296 class_getconstant (c, forward_classes -> name_index, CONSTANT_Utf8);
1298 cptags [forward_classes -> thisindex] = CONSTANT_Class;
1299 /* retrieve class from class-table */
1300 cpinfos [forward_classes -> thisindex] = class_new (name);
1302 forward_classes = forward_classes -> next;
1306 while (forward_strings) {
1308 class_getconstant (c, forward_strings -> string_index, CONSTANT_Utf8);
1310 log_text("forward_string:");
1311 printf( "classpoolid: %d\n",forward_strings -> thisindex);
1313 log_text("\n------------------"); */
1314 /* resolve utf-string */
1315 cptags [forward_strings -> thisindex] = CONSTANT_String;
1316 cpinfos [forward_strings -> thisindex] = text;
1318 forward_strings = forward_strings -> next;
1321 while (forward_nameandtypes) {
1322 constant_nameandtype *cn = NEW (constant_nameandtype);
1325 count_const_pool_len += sizeof(constant_nameandtype);
1328 /* resolve simple name and descriptor */
1329 cn -> name = class_getconstant
1330 (c, forward_nameandtypes -> name_index, CONSTANT_Utf8);
1331 cn -> descriptor = class_getconstant
1332 (c, forward_nameandtypes -> sig_index, CONSTANT_Utf8);
1334 cptags [forward_nameandtypes -> thisindex] = CONSTANT_NameAndType;
1335 cpinfos [forward_nameandtypes -> thisindex] = cn;
1337 forward_nameandtypes = forward_nameandtypes -> next;
1341 while (forward_fieldmethints) {
1342 constant_nameandtype *nat;
1343 constant_FMIref *fmi = NEW (constant_FMIref);
1346 count_const_pool_len += sizeof(constant_FMIref);
1348 /* resolve simple name and descriptor */
1349 nat = class_getconstant
1350 (c, forward_fieldmethints -> nameandtype_index, CONSTANT_NameAndType);
1353 log_text("trying to resolve:");
1354 log_text(nat->name->text);
1355 switch(forward_fieldmethints ->tag) {
1356 case CONSTANT_Fieldref:
1357 log_text("CONSTANT_Fieldref");
1359 case CONSTANT_InterfaceMethodref:
1360 log_text("CONSTANT_InterfaceMethodref");
1362 case CONSTANT_Methodref:
1363 log_text("CONSTANT_Methodref");
1367 fmi -> class = class_getconstant
1368 (c, forward_fieldmethints -> class_index, CONSTANT_Class);
1369 fmi -> name = nat -> name;
1370 fmi -> descriptor = nat -> descriptor;
1372 cptags [forward_fieldmethints -> thisindex] = forward_fieldmethints -> tag;
1373 cpinfos [forward_fieldmethints -> thisindex] = fmi;
1375 switch (forward_fieldmethints -> tag) {
1376 case CONSTANT_Fieldref: /* check validity of descriptor */
1377 checkfielddescriptor (fmi->descriptor->text,utf_end(fmi->descriptor));
1379 case CONSTANT_InterfaceMethodref:
1380 case CONSTANT_Methodref: /* check validity of descriptor */
1381 checkmethoddescriptor (fmi->descriptor);
1385 forward_fieldmethints = forward_fieldmethints -> next;
1389 /* class_showconstantpool(c); */
1391 dump_release (dumpsize);
1395 /********************** Function: class_load ***********************************
1397 Loads everything interesting about a class from the class file. The
1398 'classinfo' structure must have been allocated previously.
1400 The super class and the interfaces implemented by this class need not be
1401 loaded. The link is set later by the function 'class_link'.
1403 The loaded class is removed from the list 'unloadedclasses' and added to
1404 the list 'unlinkedclasses'.
1406 *******************************************************************************/
1408 static int class_load(classinfo *c)
1414 count_class_loads++;
1417 /* output for debugging purposes */
1419 sprintf(logtext, "Loading class: ");
1420 utf_sprint(logtext+strlen(logtext), c->name);
1424 /* load classdata, throw exception on error */
1426 if (!suck_start(c->name)) {
1427 throw_classnotfoundexception2(c->name);
1431 /* check signature */
1432 if (suck_u4() != MAGIC) panic("Can not find class-file signature");
1436 if (ma != MAJOR_VERSION && (ma != MAJOR_VERSION+1 || mi != 0)) {
1437 sprintf (logtext, "File version %d.%d is not supported",
1438 (int) ma, (int) mi);
1442 class_loadcpool (c);
1444 c->erroneous_state=0;
1445 c->initializing_thread=0;
1447 c -> classUsed = NOTUSED; /* not used initially CO-RT */
1448 c -> impldBy = NULL;
1451 c -> flags = suck_u2 ();
1455 /* retrieve superclass */
1456 if ( (i = suck_u2 () ) ) {
1457 c -> super = class_getconstant (c, i, CONSTANT_Class);
1463 /* retrieve interfaces */
1464 c -> interfacescount = suck_u2 ();
1465 c -> interfaces = MNEW (classinfo*, c -> interfacescount);
1466 for (i=0; i < c -> interfacescount; i++) {
1467 c -> interfaces [i] =
1468 class_getconstant (c, suck_u2(), CONSTANT_Class);
1472 c -> fieldscount = suck_u2 ();
1473 /* utf_display(c->name);
1474 printf(" ,Fieldscount: %d\n",c->fieldscount);*/
1476 c -> fields = GCNEW (fieldinfo, c -> fieldscount);
1477 for (i=0; i < c -> fieldscount; i++) {
1478 field_load (&(c->fields[i]), c);
1482 c -> methodscount = suck_u2 ();
1483 c -> methods = MNEW (methodinfo, c -> methodscount);
1484 for (i=0; i < c -> methodscount; i++) {
1485 method_load (&(c -> methods [i]), c);
1489 count_class_infos += sizeof(classinfo*) * c -> interfacescount;
1490 count_class_infos += sizeof(fieldinfo) * c -> fieldscount;
1491 count_class_infos += sizeof(methodinfo) * c -> methodscount;
1494 /* load variable-length attribute structures */
1495 attribute_load (suck_u2(), c);
1500 /* remove class from list of unloaded classes and
1501 add to list of unlinked classes */
1502 list_remove (&unloadedclasses, c);
1503 list_addlast (&unlinkedclasses, c);
1510 /************** internal Function: class_highestinterface ***********************
1512 Used by the function class_link to determine the amount of memory needed
1513 for the interface table.
1515 *******************************************************************************/
1517 static s4 class_highestinterface (classinfo *c)
1522 if ( ! (c->flags & ACC_INTERFACE) ) {
1523 sprintf (logtext, "Interface-methods count requested for non-interface: ");
1524 utf_sprint (logtext+strlen(logtext), c->name);
1529 for (i=0; i<c->interfacescount; i++) {
1530 s4 h2 = class_highestinterface (c->interfaces[i]);
1537 /* class_addinterface **********************************************************
1539 Is needed by class_link for adding a VTBL to a class. All interfaces
1540 implemented by ic are added as well.
1542 *******************************************************************************/
1544 static void class_addinterface (classinfo *c, classinfo *ic)
1548 vftbl *vftbl = c->vftbl;
1550 if (i >= vftbl->interfacetablelength)
1551 panic ("Inernal error: interfacetable overflow");
1552 if (vftbl->interfacetable[-i])
1555 if (ic->methodscount == 0) { /* fake entry needed for subtype test */
1556 vftbl->interfacevftbllength[i] = 1;
1557 vftbl->interfacetable[-i] = MNEW(methodptr, 1);
1558 vftbl->interfacetable[-i][0] = NULL;
1561 vftbl->interfacevftbllength[i] = ic->methodscount;
1562 vftbl->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
1565 count_vftbl_len += sizeof(methodptr) *
1566 (ic->methodscount + (ic->methodscount == 0));
1569 for (j=0; j<ic->methodscount; j++) {
1572 for (m = 0; m < sc->methodscount; m++) {
1573 methodinfo *mi = &(sc->methods[m]);
1574 if (method_canoverwrite(mi, &(ic->methods[j]))) {
1575 vftbl->interfacetable[-i][j] =
1576 vftbl->table[mi->vftblindex];
1586 for (j = 0; j < ic->interfacescount; j++)
1587 class_addinterface(c, ic->interfaces[j]);
1591 /******************* Function: class_new_array *********************************
1593 This function is called by class_new to setup an array class.
1595 *******************************************************************************/
1598 class_new_array(classinfo *c)
1600 classinfo *comp = NULL;
1604 /* Array classes are not loaded from classfiles. */
1605 list_remove (&unloadedclasses, c);
1607 /* Check array class name */
1608 namelen = c->name->blength;
1609 if (namelen < 2 || c->name->text[0] != '[')
1610 panic("Invalid array class name.");
1612 /* Check the component type */
1613 switch (c->name->text[1]) {
1615 /* c is an array of arrays. We have to create the component class. */
1616 comp = class_new(utf_new(c->name->text + 1,namelen - 1));
1620 /* c is an array of objects. */
1621 if (namelen < 4 || c->name->text[namelen-1] != ';')
1622 panic("Invalid array class name.");
1623 comp = class_new(utf_new(c->name->text + 2,namelen - 3));
1627 /* Setup the array class */
1628 c->super = class_java_lang_Object;
1630 c->interfacescount = 2;
1631 c->interfaces = MNEW(classinfo*,2);
1632 c->interfaces[0] = class_java_lang_Cloneable;
1633 c->interfaces[1] = class_java_io_Serializable;
1635 c->methodscount = 1;
1636 c->methods = MNEW (methodinfo, c->methodscount);
1639 memset(clone,0,sizeof(methodinfo));
1640 clone->flags = ACC_PUBLIC; /* XXX protected? */
1641 clone->name = utf_new_char("clone");
1642 clone->descriptor = utf_new_char("()Ljava/lang/Object;");
1644 clone->stubroutine = createnativestub(&builtin_clone_array,clone);
1645 clone->monoPoly = MONO; /* XXX should be poly? */
1647 /* XXX: field: length? */
1649 /* The array class has to be linked */
1650 list_addlast(&unlinkedclasses,c);
1653 * Array classes which are created after the other classes have been
1654 * loaded and linked are linked explicitely.
1659 loader_load(c->name);
1662 /****************** Function: class_link_array *********************************
1664 This function is called by class_link to create the
1665 arraydescriptor for an array class.
1667 This function returns NULL if the array cannot be linked because
1668 the component type has not been linked yet.
1670 *******************************************************************************/
1674 class_link_array(classinfo *c)
1676 classinfo *comp = NULL;
1677 int namelen = c->name->blength;
1678 arraydescriptor *desc;
1681 /* Check the component type */
1682 switch (c->name->text[1]) {
1684 /* c is an array of arrays. */
1685 comp = class_get(utf_new(c->name->text + 1,namelen - 1));
1686 if (!comp) panic("Could not find component array class.");
1690 /* c is an array of objects. */
1691 comp = class_get(utf_new(c->name->text + 2,namelen - 3));
1692 if (!comp) panic("Could not find component class.");
1696 /* If the component type has not been linked return NULL */
1697 if (comp && !comp->linked)
1700 /* Allocate the arraydescriptor */
1701 desc = NEW(arraydescriptor);
1704 /* c is an array of references */
1705 desc->arraytype = ARRAYTYPE_OBJECT;
1706 desc->componentsize = sizeof(void*);
1707 desc->dataoffset = OFFSET(java_objectarray,data);
1709 compvftbl = comp->vftbl;
1711 panic("Component class has no vftbl.");
1712 desc->componentvftbl = compvftbl;
1714 if (compvftbl->arraydesc) {
1715 desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
1716 desc->dimension = compvftbl->arraydesc->dimension + 1;
1717 desc->elementtype = compvftbl->arraydesc->elementtype;
1720 desc->elementvftbl = compvftbl;
1721 desc->dimension = 1;
1722 desc->elementtype = ARRAYTYPE_OBJECT;
1726 /* c is an array of a primitive type */
1727 switch (c->name->text[1]) {
1728 case 'Z': desc->arraytype = ARRAYTYPE_BOOLEAN;
1729 desc->dataoffset = OFFSET(java_booleanarray,data);
1730 desc->componentsize = sizeof(u1); break;
1731 case 'B': desc->arraytype = ARRAYTYPE_BYTE;
1732 desc->dataoffset = OFFSET(java_bytearray,data);
1733 desc->componentsize = sizeof(u1); break;
1734 case 'C': desc->arraytype = ARRAYTYPE_CHAR;
1735 desc->dataoffset = OFFSET(java_chararray,data);
1736 desc->componentsize = sizeof(u2); break;
1737 case 'D': desc->arraytype = ARRAYTYPE_DOUBLE;
1738 desc->dataoffset = OFFSET(java_doublearray,data);
1739 desc->componentsize = sizeof(double); break;
1740 case 'F': desc->arraytype = ARRAYTYPE_FLOAT;
1741 desc->dataoffset = OFFSET(java_floatarray,data);
1742 desc->componentsize = sizeof(float); break;
1743 case 'I': desc->arraytype = ARRAYTYPE_INT;
1744 desc->dataoffset = OFFSET(java_intarray,data);
1745 desc->componentsize = sizeof(s4); break;
1746 case 'J': desc->arraytype = ARRAYTYPE_LONG;
1747 desc->dataoffset = OFFSET(java_longarray,data);
1748 desc->componentsize = sizeof(s8); break;
1749 case 'S': desc->arraytype = ARRAYTYPE_SHORT;
1750 desc->dataoffset = OFFSET(java_shortarray,data);
1751 desc->componentsize = sizeof(s2); break;
1753 panic("Invalid array class name");
1756 desc->componentvftbl = NULL;
1757 desc->elementvftbl = NULL;
1758 desc->dimension = 1;
1759 desc->elementtype = desc->arraytype;
1765 /********************** Function: class_link ***********************************
1767 Tries to link a class. The super class and every implemented interface must
1768 already have been linked. The function calculates the length in bytes that
1769 an instance of this class requires as well as the VTBL for methods and
1772 If the class can be linked, it is removed from the list 'unlinkedclasses'
1773 and added to 'linkedclasses'. Otherwise, it is moved to the end of
1776 Attention: If cyclical class definitions are encountered, the program gets
1777 into an infinite loop (we'll have to work that out)
1779 *******************************************************************************/
1781 void class_link(classinfo *c)
1783 s4 supervftbllength; /* vftbllegnth of super class */
1784 s4 vftbllength; /* vftbllength of current class */
1785 s4 interfacetablelength; /* interface table length */
1786 classinfo *super = c->super; /* super class */
1787 classinfo *ic, *c2; /* intermediate class variables */
1788 vftbl *v; /* vftbl of current class */
1789 s4 i; /* interface/method/field counter */
1790 arraydescriptor *arraydesc = NULL; /* descriptor for array classes */
1793 /* check if all superclasses are already linked, if not put c at end of
1794 unlinked list and return. Additionally initialize class fields. */
1796 /* check interfaces */
1798 for (i = 0; i < c->interfacescount; i++) {
1799 ic = c->interfaces[i];
1801 list_remove(&unlinkedclasses, c);
1802 list_addlast(&unlinkedclasses, c);
1807 /* check super class */
1809 if (super == NULL) { /* class java.long.Object */
1811 c->classUsed = USED; /* Object class is always used CO-RT*/
1812 c -> impldBy = NULL;
1813 c->instancesize = sizeof(java_objectheader);
1815 vftbllength = supervftbllength = 0;
1817 c->finalizer = NULL;
1820 if (!super->linked) {
1821 list_remove(&unlinkedclasses, c);
1822 list_addlast(&unlinkedclasses, c);
1826 /* handle array classes */
1827 if (c->name->text[0] == '[')
1828 if ((arraydesc = class_link_array(c)) == NULL) {
1829 list_remove(&unlinkedclasses, c);
1830 list_addlast(&unlinkedclasses, c);
1834 if (c->flags & ACC_INTERFACE)
1835 c->index = interfaceindex++;
1837 c->index = super->index + 1;
1839 c->instancesize = super->instancesize;
1841 vftbllength = supervftbllength = super->vftbl->vftbllength;
1843 c->finalizer = super->finalizer;
1848 sprintf (logtext, "Linking Class: ");
1849 utf_sprint (logtext+strlen(logtext), c->name );
1853 /* compute vftbl length */
1855 for (i = 0; i < c->methodscount; i++) {
1856 methodinfo *m = &(c->methods[i]);
1858 if (!(m->flags & ACC_STATIC)) { /* is instance method */
1859 classinfo *sc = super;
1862 for (j = 0; j < sc->methodscount; j++) {
1863 if (method_canoverwrite(m, &(sc->methods[j]))) {
1864 m->vftblindex = sc->methods[j].vftblindex;
1865 goto foundvftblindex;
1870 m->vftblindex = (vftbllength++);
1876 count_vftbl_len += sizeof(vftbl) + sizeof(methodptr)*(vftbllength-1);
1879 /* compute interfacetable length */
1881 interfacetablelength = 0;
1884 for (i = 0; i < c2->interfacescount; i++) {
1885 s4 h = class_highestinterface (c2->interfaces[i]) + 1;
1886 if (h > interfacetablelength)
1887 interfacetablelength = h;
1892 /* allocate virtual function table */
1894 v = (vftbl*) mem_alloc(sizeof(vftbl) + sizeof(methodptr) *
1895 (vftbllength - 1) + sizeof(methodptr*) *
1896 (interfacetablelength - (interfacetablelength > 0)));
1897 v = (vftbl*) (((methodptr*) v) + (interfacetablelength - 1) *
1898 (interfacetablelength > 1));
1899 c->header.vftbl = c->vftbl = v;
1901 v->vftbllength = vftbllength;
1902 v->interfacetablelength = interfacetablelength;
1903 v->arraydesc = arraydesc;
1905 /* store interface index in vftbl */
1906 if (c->flags & ACC_INTERFACE)
1907 v->baseval = -(c->index);
1909 /* copy virtual function table of super class */
1911 for (i = 0; i < supervftbllength; i++)
1912 v->table[i] = super->vftbl->table[i];
1914 /* add method stubs into virtual function table */
1916 for (i = 0; i < c->methodscount; i++) {
1917 methodinfo *m = &(c->methods[i]);
1918 if (!(m->flags & ACC_STATIC)) {
1919 v->table[m->vftblindex] = m->stubroutine;
1923 /* compute instance size and offset of each field */
1925 for (i = 0; i < c->fieldscount; i++) {
1927 fieldinfo *f = &(c->fields[i]);
1929 if (!(f->flags & ACC_STATIC) ) {
1930 dsize = desc_typesize (f->descriptor);
1931 c->instancesize = ALIGN (c->instancesize, dsize);
1932 f->offset = c->instancesize;
1933 c->instancesize += dsize;
1937 /* initialize interfacetable and interfacevftbllength */
1939 v->interfacevftbllength = MNEW (s4, interfacetablelength);
1942 count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
1945 for (i = 0; i < interfacetablelength; i++) {
1946 v->interfacevftbllength[i] = 0;
1947 v->interfacetable[-i] = NULL;
1950 /* add interfaces */
1952 for (c2 = c; c2 != NULL; c2 = c2->super)
1953 for (i = 0; i < c2->interfacescount; i++) {
1954 class_addinterface (c, c2->interfaces[i]);
1957 /* add finalizer method (not for java.lang.Object) */
1959 if (super != NULL) {
1961 static utf *finame = NULL;
1962 static utf *fidesc = NULL;
1965 finame = utf_finalize;
1967 fidesc = utf_fidesc;
1969 fi = class_findmethod (c, finame, fidesc);
1971 if (!(fi->flags & ACC_STATIC)) {
1981 list_remove (&unlinkedclasses, c);
1982 list_addlast (&linkedclasses, c);
1987 /******************* Function: class_freepool **********************************
1989 Frees all resources used by this classes Constant Pool.
1991 *******************************************************************************/
1993 static void class_freecpool (classinfo *c)
1999 for (idx=0; idx < c->cpcount; idx++) {
2000 tag = c->cptags[idx];
2001 info = c->cpinfos[idx];
2005 case CONSTANT_Fieldref:
2006 case CONSTANT_Methodref:
2007 case CONSTANT_InterfaceMethodref:
2008 FREE (info, constant_FMIref);
2010 case CONSTANT_Integer:
2011 FREE (info, constant_integer);
2013 case CONSTANT_Float:
2014 FREE (info, constant_float);
2017 FREE (info, constant_long);
2019 case CONSTANT_Double:
2020 FREE (info, constant_double);
2022 case CONSTANT_NameAndType:
2023 FREE (info, constant_nameandtype);
2029 MFREE (c -> cptags, u1, c -> cpcount);
2030 MFREE (c -> cpinfos, voidptr, c -> cpcount);
2034 /*********************** Function: class_free **********************************
2036 Frees all resources used by the class.
2038 *******************************************************************************/
2040 static void class_free (classinfo *c)
2045 class_freecpool (c);
2047 MFREE (c->interfaces, classinfo*, c->interfacescount);
2049 for (i = 0; i < c->fieldscount; i++)
2050 field_free(&(c->fields[i]));
2052 for (i = 0; i < c->methodscount; i++)
2053 method_free(&(c->methods[i]));
2054 MFREE (c->methods, methodinfo, c->methodscount);
2056 if ((v = c->vftbl) != NULL) {
2058 mem_free(v->arraydesc,sizeof(arraydescriptor));
2060 for (i = 0; i < v->interfacetablelength; i++) {
2061 MFREE (v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
2063 MFREE (v->interfacevftbllength, s4, v->interfacetablelength);
2065 i = sizeof(vftbl) + sizeof(methodptr) * (v->vftbllength - 1) +
2066 sizeof(methodptr*) * (v->interfacetablelength -
2067 (v->interfacetablelength > 0));
2068 v = (vftbl*) (((methodptr*) v) - (v->interfacetablelength - 1) *
2069 (v->interfacetablelength > 1));
2073 if (c->innerclasscount)
2074 MFREE (c->innerclass, innerclassinfo, c->innerclasscount);
2076 /* if (c->classvftbl)
2077 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
2079 FREE (c, classinfo);
2082 /************************* Function: class_findfield ***************************
2084 Searches a 'classinfo' structure for a field having the given name and
2087 *******************************************************************************/
2090 fieldinfo *class_findfield (classinfo *c, utf *name, utf *desc)
2095 for (i = 0; i < c->fieldscount; i++) {
2096 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
2097 return &(c->fields[i]);
2100 panic ("Can not find field given in CONSTANT_Fieldref");
2105 /************************* Function: class_findmethod **************************
2107 Searches a 'classinfo' structure for a method having the given name and
2108 type and returns the index in the class info structure.
2109 If type is NULL, it is ignored.
2111 *******************************************************************************/
2113 s4 class_findmethodIndex (classinfo *c, utf *name, utf *desc)
2116 #if defined(JOWENN_DEBUG1) || defined(JOWENN_DEBUG2)
2118 int buffer_len, pos;
2120 #ifdef JOWENN_DEBUG1
2123 utf_strlen(name) + utf_strlen(desc) +utf_strlen(c->name)+ 64;
2125 buffer = MNEW(char, buffer_len);
2127 strcpy(buffer, "class_findmethod: method:");
2128 utf_sprint(buffer+strlen(buffer), name);
2129 strcpy(buffer+strlen(buffer), ", desc: ");
2130 utf_sprint(buffer+strlen(buffer), desc);
2131 strcpy(buffer+strlen(buffer), ", classname: ");
2132 utf_sprint(buffer+strlen(buffer), c->name);
2136 MFREE(buffer, char, buffer_len);
2138 for (i = 0; i < c->methodscount; i++) {
2139 #ifdef JOWENN_DEBUG2
2141 utf_strlen(c->methods[i].name) + utf_strlen(c->methods[i].descriptor)+ 64;
2143 buffer = MNEW(char, buffer_len);
2145 strcpy(buffer, "class_findmethod: comparing to method:");
2146 utf_sprint(buffer+strlen(buffer), c->methods[i].name);
2147 strcpy(buffer+strlen(buffer), ", desc: ");
2148 utf_sprint(buffer+strlen(buffer), c->methods[i].descriptor);
2152 MFREE(buffer, char, buffer_len);
2156 if ((c->methods[i].name == name) && ((desc == NULL) ||
2157 (c->methods[i].descriptor == desc)))
2160 #ifdef JOWENN_DEBUG2
2161 class_showconstantpool(c);
2162 log_text("class_findmethod: returning NULL");
2169 /************************* Function: class_findmethod **************************
2171 Searches a 'classinfo' structure for a method having the given name and
2173 If type is NULL, it is ignored.
2175 *******************************************************************************/
2177 methodinfo *class_findmethod (classinfo *c, utf *name, utf *desc)
2181 #if defined(JOWENN_DEBUG1) || defined(JOWENN_DEBUG2)
2183 int buffer_len, pos;
2185 #ifdef JOWENN_DEBUG1
2188 utf_strlen(name) + utf_strlen(desc) +utf_strlen(c->name)+ 64;
2190 buffer = MNEW(char, buffer_len);
2192 strcpy(buffer, "class_findmethod: method:");
2193 utf_sprint(buffer+strlen(buffer), name);
2194 strcpy(buffer+strlen(buffer), ", desc: ");
2195 utf_sprint(buffer+strlen(buffer), desc);
2196 strcpy(buffer+strlen(buffer), ", classname: ");
2197 utf_sprint(buffer+strlen(buffer), c->name);
2201 MFREE(buffer, char, buffer_len);
2203 for (i = 0; i < c->methodscount; i++) {
2204 #ifdef JOWENN_DEBUG2
2206 utf_strlen(c->methods[i].name) + utf_strlen(c->methods[i].descriptor)+ 64;
2208 buffer = MNEW(char, buffer_len);
2210 strcpy(buffer, "class_findmethod: comparing to method:");
2211 utf_sprint(buffer+strlen(buffer), c->methods[i].name);
2212 strcpy(buffer+strlen(buffer), ", desc: ");
2213 utf_sprint(buffer+strlen(buffer), c->methods[i].descriptor);
2217 MFREE(buffer, char, buffer_len);
2221 if ((c->methods[i].name == name) && ((desc == NULL) ||
2222 (c->methods[i].descriptor == desc)))
2223 return &(c->methods[i]);
2225 #ifdef JOWENN_DEBUG2
2226 class_showconstantpool(c);
2227 log_text("class_findmethod: returning NULL");
2231 s4 idx=class_findmethodIndex(c,name,desc);
2232 /* if (idx==-1) log_text("class_findmethod: method not found");*/
2233 if (idx==-1) return NULL;
2234 return &(c->methods[idx]);
2242 /************************* Function: class_findmethod_approx ******************
2244 like class_findmethod but ignores the return value when comparing the
2247 *******************************************************************************/
2249 methodinfo *class_findmethod_approx (classinfo *c, utf *name, utf *desc)
2253 for (i = 0; i < c->methodscount; i++)
2254 if (c->methods[i].name == name) {
2255 utf *meth_descr = c->methods[i].descriptor;
2259 return &(c->methods[i]);
2261 if (desc->blength <= meth_descr->blength) {
2262 /* current position in utf text */
2263 char *desc_utf_ptr = desc->text;
2264 char *meth_utf_ptr = meth_descr->text;
2265 /* points behind utf strings */
2266 char *desc_end = utf_end(desc);
2267 char *meth_end = utf_end(meth_descr);
2270 /* compare argument types */
2271 while (desc_utf_ptr<desc_end && meth_utf_ptr<meth_end) {
2273 if ((ch=*desc_utf_ptr++) != (*meth_utf_ptr++))
2274 break; /* no match */
2277 return &(c->methods[i]); /* all parameter types equal */
2285 /***************** Function: class_resolvemethod_approx ***********************
2287 Searches a class and every super class for a method (without paying
2288 attention to the return value)
2290 *******************************************************************************/
2292 methodinfo *class_resolvemethod_approx (classinfo *c, utf *name, utf *desc)
2295 /* search for method (ignore returntype) */
2296 methodinfo *m = class_findmethod_approx (c, name, desc);
2299 /* search superclass */
2306 /************************* Function: class_resolvemethod ***********************
2308 Searches a class and every super class for a method.
2310 *******************************************************************************/
2312 methodinfo *class_resolvemethod (classinfo *c, utf *name, utf *desc)
2315 methodinfo *m = class_findmethod(c, name, desc);
2317 /* search superclass */
2325 /************************* Function: class_issubclass **************************
2327 Checks if sub is a descendant of super.
2329 *******************************************************************************/
2331 bool class_issubclass(classinfo *sub, classinfo *super)
2334 if (!sub) return false;
2335 if (sub==super) return true;
2342 /****************** Initialization function for classes ******************
2344 In Java, every class can have a static initialization function. This
2345 function has to be called BEFORE calling other methods or accessing static
2348 *******************************************************************************/
2351 extern int blockInts;
2354 void class_init(classinfo *c)
2357 java_objectheader *exceptionptr;
2362 if (!makeinitializations)
2366 c -> initialized = true;
2371 count_class_inits++;
2375 class_init (c->super);
2376 for (i=0; i < c->interfacescount; i++)
2377 class_init(c->interfaces[i]); /* real */
2379 m = class_findmethod (c, utf_clinit, utf_fidesc);
2382 sprintf (logtext, "Class ");
2383 utf_sprint (logtext+strlen(logtext), c->name);
2384 sprintf (logtext+strlen(logtext), " has no initializer");
2387 /* goto callinitialize;*/
2391 if (! (m->flags & ACC_STATIC))
2392 panic ("Class initializer is not static!");
2395 sprintf (logtext, "Starting initializer for class: ");
2396 utf_sprint (logtext+strlen(logtext), c->name);
2405 exceptionptr = asm_calljavamethod(m, NULL, NULL, NULL, NULL);
2408 assert(blockInts == 0);
2413 printf ("#### Initializer of ");
2414 utf_display (c->name);
2415 printf (" has thrown: ");
2416 utf_display (exceptionptr->vftbl->class->name);
2422 sprintf (logtext, "Finished initializer for class: ");
2423 utf_sprint (logtext+strlen(logtext), c->name);
2426 if (c->name == utf_systemclass) {
2427 /* class java.lang.System requires explicit initialization */
2430 printf ("#### Initializing class System");
2432 /* find initializing method */
2433 m = class_findmethod (c,
2434 utf_initsystemclass,
2438 /* no method found */
2439 /* printf("initializeSystemClass failed"); */
2447 exceptionptr = asm_calljavamethod (m, NULL,NULL,NULL,NULL);
2450 assert(blockInts == 0);
2455 printf ("#### initializeSystemClass has thrown: ");
2456 utf_display (exceptionptr->vftbl->class->name);
2467 /********* Function: find_class_method_constant *********/
2468 int find_class_method_constant (classinfo *c, utf * c1, utf* m1, utf* d1)
2473 for (i=0; i<c->cpcount; i++) {
2475 e = c -> cpinfos [i];
2478 switch (c -> cptags [i]) {
2479 case CONSTANT_Methodref:
2481 constant_FMIref *fmi = e;
2482 if ( (fmi->class->name == c1)
2483 && (fmi->name == m1)
2484 && (fmi->descriptor == d1)) {
2491 case CONSTANT_InterfaceMethodref:
2493 constant_FMIref *fmi = e;
2494 if ( (fmi->class->name == c1)
2495 && (fmi->name == m1)
2496 && (fmi->descriptor == d1)) {
2510 void class_showconstanti(classinfo *c, int ii)
2516 printf ("#%d: ", (int) i);
2518 switch (c->cptags [i]) {
2519 case CONSTANT_Class:
2520 printf("Classreference -> ");
2521 utf_display(((classinfo*)e)->name);
2524 case CONSTANT_Fieldref:
2525 printf("Fieldref -> "); goto displayFMIi;
2526 case CONSTANT_Methodref:
2527 printf("Methodref -> "); goto displayFMIi;
2528 case CONSTANT_InterfaceMethodref:
2529 printf("InterfaceMethod -> "); goto displayFMIi;
2532 constant_FMIref *fmi = e;
2533 utf_display(fmi->class->name);
2535 utf_display(fmi->name);
2537 utf_display(fmi->descriptor);
2541 case CONSTANT_String:
2542 printf("String -> ");
2545 case CONSTANT_Integer:
2546 printf("Integer -> %d", (int) (((constant_integer*)e)->value));
2548 case CONSTANT_Float:
2549 printf("Float -> %f", ((constant_float*)e)->value);
2551 case CONSTANT_Double:
2552 printf("Double -> %f", ((constant_double*)e)->value);
2556 u8 v = ((constant_long*)e)->value;
2558 printf("Long -> %ld", (long int) v);
2560 printf("Long -> HI: %ld, LO: %ld\n",
2561 (long int) v.high, (long int) v.low);
2565 case CONSTANT_NameAndType:
2567 constant_nameandtype *cnt = e;
2568 printf("NameAndType: ");
2569 utf_display(cnt->name);
2571 utf_display(cnt->descriptor);
2579 panic("Invalid type of ConstantPool-Entry");
2586 void class_showconstantpool (classinfo *c)
2591 printf ("---- dump of constant pool ----\n");
2593 for (i=0; i<c->cpcount; i++) {
2594 printf ("#%d: ", (int) i);
2596 e = c -> cpinfos [i];
2599 switch (c -> cptags [i]) {
2600 case CONSTANT_Class:
2601 printf ("Classreference -> ");
2602 utf_display ( ((classinfo*)e) -> name );
2605 case CONSTANT_Fieldref:
2606 printf ("Fieldref -> "); goto displayFMI;
2607 case CONSTANT_Methodref:
2608 printf ("Methodref -> "); goto displayFMI;
2609 case CONSTANT_InterfaceMethodref:
2610 printf ("InterfaceMethod -> "); goto displayFMI;
2613 constant_FMIref *fmi = e;
2614 utf_display ( fmi->class->name );
2616 utf_display ( fmi->name);
2618 utf_display ( fmi->descriptor );
2622 case CONSTANT_String:
2623 printf ("String -> ");
2626 case CONSTANT_Integer:
2627 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2629 case CONSTANT_Float:
2630 printf ("Float -> %f", ((constant_float*)e) -> value);
2632 case CONSTANT_Double:
2633 printf ("Double -> %f", ((constant_double*)e) -> value);
2637 u8 v = ((constant_long*)e) -> value;
2639 printf ("Long -> %ld", (long int) v);
2641 printf ("Long -> HI: %ld, LO: %ld\n",
2642 (long int) v.high, (long int) v.low);
2646 case CONSTANT_NameAndType:
2648 constant_nameandtype *cnt = e;
2649 printf ("NameAndType: ");
2650 utf_display (cnt->name);
2652 utf_display (cnt->descriptor);
2656 printf ("Utf8 -> ");
2660 panic ("Invalid type of ConstantPool-Entry");
2670 /********** Function: class_showmethods (debugging only) *************/
2672 void class_showmethods (classinfo *c)
2676 printf ("--------- Fields and Methods ----------------\n");
2677 printf ("Flags: "); printflags (c->flags); printf ("\n");
2679 printf ("This: "); utf_display (c->name); printf ("\n");
2681 printf ("Super: "); utf_display (c->super->name); printf ("\n");
2683 printf ("Index: %d\n", c->index);
2685 printf ("interfaces:\n");
2686 for (i=0; i < c-> interfacescount; i++) {
2688 utf_display (c -> interfaces[i] -> name);
2689 printf (" (%d)\n", c->interfaces[i] -> index);
2692 printf ("fields:\n");
2693 for (i=0; i < c -> fieldscount; i++) {
2694 field_display (&(c -> fields[i]));
2697 printf ("methods:\n");
2698 for (i=0; i < c -> methodscount; i++) {
2699 methodinfo *m = &(c->methods[i]);
2700 if ( !(m->flags & ACC_STATIC))
2701 printf ("vftblindex: %d ", m->vftblindex);
2703 method_display ( m );
2707 printf ("Virtual function table:\n");
2708 for (i=0; i<c->vftbl->vftbllength; i++) {
2709 printf ("entry: %d, %ld\n", i, (long int) (c->vftbl->table[i]) );
2716 /******************************************************************************/
2717 /******************* General functions for the class loader *******************/
2718 /******************************************************************************/
2720 /********************* Function: loader_load ***********************************
2722 Loads and links the class desired class and each class and interface
2724 Returns: a pointer to this class
2726 *******************************************************************************/
2728 static int loader_load_running = 0;
2730 classinfo *loader_load (utf *topname)
2734 long int starttime=0,stoptime=0;
2736 /* avoid recursive calls */
2737 if (loader_load_running)
2738 return class_new(topname);
2739 loader_load_running++;
2741 intsDisable(); /* schani */
2744 starttime = getcputime();
2746 top = class_new (topname);
2749 while ( (c = list_first(&unloadedclasses)) ) {
2750 if (!class_load (c)) {
2751 list_remove (&unloadedclasses, c);
2757 while ( (c = list_first(&unlinkedclasses)) ) {
2762 loader_compute_subclasses();
2765 if (getloadingtime) {
2766 stoptime = getcputime();
2767 loadingtime += (stoptime-starttime);
2771 loader_load_running--;
2773 /* check if a former loader_load call tried to load/link the class and failed.
2774 This is needed because the class didn't appear in the undloadclasses or unlinkedclasses list during this class; */
2777 throw_classnotfoundexception2(top->name);
2779 } else if (!top->linked) {
2780 throw_linkageerror2(top->name);
2785 intsRestore(); /* schani */
2791 /**************** function: create_primitive_classes ***************************
2793 create classes representing primitive types
2795 ********************************************************************************/
2798 void create_primitive_classes()
2802 for (i=0;i<PRIMITIVETYPE_COUNT;i++) {
2803 /* create primitive class */
2804 classinfo *c = class_new ( utf_new_char(primitivetype_table[i].name) );
2805 c -> classUsed = NOTUSED; /* not used initially CO-RT */
2806 c -> impldBy = NULL;
2808 /* prevent loader from loading primitive class */
2809 list_remove (&unloadedclasses, c);
2811 /* add to unlinked classes */
2812 list_addlast (&unlinkedclasses, c);
2813 c -> super = class_java_lang_Object;
2816 primitivetype_table[i].class_primitive = c;
2818 /* create class for wrapping the primitive type */
2819 primitivetype_table[i].class_wrap =
2820 class_new( utf_new_char(primitivetype_table[i].wrapname) );
2821 primitivetype_table[i].class_wrap -> classUsed = NOTUSED; /* not used initially CO-RT */
2822 primitivetype_table[i].class_wrap -> impldBy = NULL;
2824 /* create the primitive array class */
2825 if (primitivetype_table[i].arrayname) {
2826 c = class_new( utf_new_char(primitivetype_table[i].arrayname) );
2827 primitivetype_table[i].arrayclass = c;
2829 if (!c->linked) class_link(c);
2830 primitivetype_table[i].arrayvftbl = c->vftbl;
2835 /**************** function: class_primitive_from_sig ***************************
2837 return the primitive class indicated by the given signature character
2839 If the descriptor does not indicate a valid primitive type the
2840 return value is NULL.
2842 ********************************************************************************/
2844 classinfo *class_primitive_from_sig(char sig)
2847 case 'I': return primitivetype_table[PRIMITIVETYPE_INT].class_primitive;
2848 case 'J': return primitivetype_table[PRIMITIVETYPE_LONG].class_primitive;
2849 case 'F': return primitivetype_table[PRIMITIVETYPE_FLOAT].class_primitive;
2850 case 'D': return primitivetype_table[PRIMITIVETYPE_DOUBLE].class_primitive;
2851 case 'B': return primitivetype_table[PRIMITIVETYPE_BYTE].class_primitive;
2852 case 'C': return primitivetype_table[PRIMITIVETYPE_CHAR].class_primitive;
2853 case 'S': return primitivetype_table[PRIMITIVETYPE_SHORT].class_primitive;
2854 case 'Z': return primitivetype_table[PRIMITIVETYPE_BOOLEAN].class_primitive;
2855 case 'V': return primitivetype_table[PRIMITIVETYPE_VOID].class_primitive;
2860 /****************** function: class_from_descriptor ****************************
2862 return the class indicated by the given descriptor
2864 utf_ptr....first character of descriptor
2865 end_ptr....first character after the end of the string
2866 next.......if non-NULL, *next is set to the first character after
2868 mode.......what to do if a class descriptor is parsed successfully:
2869 CLASSLOAD_SKIP...skip it and return something != NULL
2870 CLASSLOAD_NEW....get classinfo * via class_new
2871 CLASSLOAD_LOAD...get classinfo * via loader_load
2873 If the descriptor is invalid the return value is NULL
2875 ********************************************************************************/
2877 classinfo *class_from_descriptor(char *utf_ptr,char *end_ptr,char **next,int mode)
2879 char *start = utf_ptr;
2883 SKIP_FIELDDESCRIPTOR_SAFE(utf_ptr,end_ptr,error);
2884 if (error) return NULL;
2885 if (next) *next = utf_ptr;
2893 if (mode == CLASSLOAD_SKIP) return class_java_lang_Object;
2894 name = utf_new(start,utf_ptr-start);
2895 return (mode == CLASSLOAD_LOAD) ? loader_load(name) : class_new(name);
2897 return class_primitive_from_sig(*start);
2901 /*************** function: create_pseudo_classes *******************************
2903 create pseudo classes used by the typechecker
2905 ********************************************************************************/
2908 create_pseudo_classes()
2910 /* pseudo class for Arraystubs (extends java.lang.Object) */
2912 pseudo_class_Arraystub = class_new( utf_new_char("$ARRAYSTUB$") );
2913 list_remove(&unloadedclasses,pseudo_class_Arraystub);
2915 pseudo_class_Arraystub->super = class_java_lang_Object;
2916 pseudo_class_Arraystub->interfacescount = 2;
2917 pseudo_class_Arraystub->interfaces = MNEW(classinfo*,2);
2918 pseudo_class_Arraystub->interfaces[0] =
2919 class_java_lang_Cloneable;
2920 pseudo_class_Arraystub->interfaces[1] =
2921 class_java_io_Serializable;
2923 list_addlast(&unlinkedclasses,pseudo_class_Arraystub);
2924 class_link(pseudo_class_Arraystub);
2926 pseudo_class_Arraystub_vftbl = pseudo_class_Arraystub->vftbl;
2928 /* pseudo class representing the null type */
2930 pseudo_class_Null = class_new( utf_new_char("$NULL$") );
2931 list_remove(&unloadedclasses,pseudo_class_Null);
2933 pseudo_class_Null->super = class_java_lang_Object;
2935 list_addlast(&unlinkedclasses,pseudo_class_Null);
2936 class_link(pseudo_class_Null);
2939 /********************** Function: loader_init **********************************
2941 Initializes all lists and loads all classes required for the system or the
2944 *******************************************************************************/
2946 void loader_init (u1 * stackbottom)
2951 log_text("Entering loader_init");
2954 list_init (&unloadedclasses, OFFSET(classinfo, listnode) );
2955 list_init (&unlinkedclasses, OFFSET(classinfo, listnode) );
2956 list_init (&linkedclasses, OFFSET(classinfo, listnode) );
2958 /* create utf-symbols for pointer comparison of frequently used strings */
2959 utf_innerclasses = utf_new_char("InnerClasses");
2960 utf_constantvalue = utf_new_char("ConstantValue");
2961 utf_code = utf_new_char("Code");
2962 utf_finalize = utf_new_char("finalize");
2963 utf_fidesc = utf_new_char("()V");
2964 utf_clinit = utf_new_char("<clinit>");
2965 utf_initsystemclass = utf_new_char("initializeSystemClass");
2966 utf_systemclass = utf_new_char("java/lang/System");
2967 utf_vmclassloader =utf_new_char("java/lang/VMClassLoader");
2968 utf_initialize =utf_new_char("initialize");
2969 utf_initializedesc =utf_new_char("(I)V");
2971 utf_vmclass =utf_new_char("java/lang/VMClass");
2973 /* create some important classes */
2974 /* These classes have to be created now because the classinfo
2975 * pointers are used in the loading code.
2977 class_java_lang_Object = class_new( utf_new_char ("java/lang/Object") );
2978 class_java_lang_String = class_new( utf_new_char("java/lang/String") );
2979 class_java_lang_Cloneable = class_new( utf_new_char ("java/lang/Cloneable") );
2980 class_java_io_Serializable = class_new( utf_new_char ("java/io/Serializable") );
2982 log_text("loader_init: java/lang/Object");
2983 /* load the classes which were created above */
2984 loader_load (class_java_lang_Object->name);
2986 loader_inited=1; /*JOWENN*/
2988 class_java_lang_Throwable =
2989 loader_load( utf_new_char("java/lang/Throwable") );
2991 log_text("loader_init: loader_load: java/lang/ClassCastException");
2992 class_java_lang_ClassCastException =
2993 loader_load ( utf_new_char ("java/lang/ClassCastException") );
2994 class_java_lang_NullPointerException =
2995 loader_load ( utf_new_char ("java/lang/NullPointerException") );
2996 class_java_lang_ArrayIndexOutOfBoundsException = loader_load (
2997 utf_new_char ("java/lang/ArrayIndexOutOfBoundsException") );
2998 class_java_lang_NegativeArraySizeException = loader_load (
2999 utf_new_char ("java/lang/NegativeArraySizeException") );
3000 class_java_lang_OutOfMemoryError = loader_load (
3001 utf_new_char ("java/lang/OutOfMemoryError") );
3002 class_java_lang_ArrayStoreException =
3003 loader_load ( utf_new_char ("java/lang/ArrayStoreException") );
3004 class_java_lang_ArithmeticException =
3005 loader_load ( utf_new_char ("java/lang/ArithmeticException") );
3006 class_java_lang_ThreadDeath = /* schani */
3007 loader_load ( utf_new_char ("java/lang/ThreadDeath") );
3008 /* create classes representing primitive types */
3009 create_primitive_classes();
3011 /* create classes used by the typechecker */
3012 create_pseudo_classes();
3014 /* correct vftbl-entries (retarded loading of class java/lang/String) */
3015 stringtable_update();
3023 log_text("loader_init: creating global proto_java_lang_ClassCastException");
3024 proto_java_lang_ClassCastException =
3025 builtin_new(class_java_lang_ClassCastException);
3026 heap_addreference ( (void**) &proto_java_lang_ClassCastException);
3028 log_text("loader_init: proto_java_lang_ClassCastException has been initialized");
3030 proto_java_lang_NullPointerException =
3031 builtin_new(class_java_lang_NullPointerException);
3032 heap_addreference ( (void**) &proto_java_lang_NullPointerException);
3033 log_text("loader_init: proto_java_lang_NullPointerException has been initialized");
3035 proto_java_lang_ArrayIndexOutOfBoundsException =
3036 builtin_new(class_java_lang_ArrayIndexOutOfBoundsException);
3037 heap_addreference ( (void**) &proto_java_lang_ArrayIndexOutOfBoundsException);
3039 proto_java_lang_NegativeArraySizeException =
3040 builtin_new(class_java_lang_NegativeArraySizeException);
3041 heap_addreference ( (void**) &proto_java_lang_NegativeArraySizeException);
3043 proto_java_lang_OutOfMemoryError =
3044 builtin_new(class_java_lang_OutOfMemoryError);
3045 heap_addreference ( (void**) &proto_java_lang_OutOfMemoryError);
3047 proto_java_lang_ArithmeticException =
3048 builtin_new(class_java_lang_ArithmeticException);
3049 heap_addreference ( (void**) &proto_java_lang_ArithmeticException);
3051 proto_java_lang_ArrayStoreException =
3052 builtin_new(class_java_lang_ArrayStoreException);
3053 heap_addreference ( (void**) &proto_java_lang_ArrayStoreException);
3055 proto_java_lang_ThreadDeath = /* schani */
3056 builtin_new(class_java_lang_ThreadDeath);
3057 heap_addreference ( (void**) &proto_java_lang_ThreadDeath);
3066 /********************* Function: loader_initclasses ****************************
3068 Initializes all loaded but uninitialized classes
3070 *******************************************************************************/
3072 void loader_initclasses ()
3076 intsDisable(); /* schani */
3078 if (makeinitializations) {
3079 c = list_first (&linkedclasses);
3082 c = list_next (&linkedclasses, c);
3086 intsRestore(); /* schani */
3089 static s4 classvalue;
3091 static void loader_compute_class_values (classinfo *c)
3095 c->vftbl->baseval = ++classvalue;
3098 while (subs != NULL) {
3099 loader_compute_class_values(subs);
3100 subs = subs->nextsub;
3102 c->vftbl->diffval = classvalue - c->vftbl->baseval;
3107 for (i = 0; i < c->index; i++)
3109 printf("%3d %3d ", (int) c->vftbl->baseval, c->vftbl->diffval);
3110 utf_display(c->name);
3118 void loader_compute_subclasses ()
3122 intsDisable(); /* schani */
3124 c = list_first (&linkedclasses);
3126 if (!(c->flags & ACC_INTERFACE)) {
3130 c = list_next (&linkedclasses, c);
3133 c = list_first (&linkedclasses);
3135 if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
3136 c->nextsub = c->super->sub;
3139 c = list_next (&linkedclasses, c);
3142 loader_compute_class_values(class_java_lang_Object);
3144 intsRestore(); /* schani */
3149 /******************** function classloader_buffer ******************************
3151 sets buffer for reading classdata
3153 *******************************************************************************/
3155 void classload_buffer(u1 *buf, int len)
3158 classbuffer_size = len;
3159 classbuf_pos = buf - 1;
3163 /******************** Function: loader_close ***********************************
3167 *******************************************************************************/
3169 void loader_close ()
3173 while ( (c=list_first(&unloadedclasses)) ) {
3174 list_remove (&unloadedclasses,c);
3177 while ( (c=list_first(&unlinkedclasses)) ) {
3178 list_remove (&unlinkedclasses,c);
3181 while ( (c=list_first(&linkedclasses)) ) {
3182 list_remove (&linkedclasses,c);
3189 * These are local overrides for various environment variables in Emacs.
3190 * Please do not remove this and leave it at the end of the file, where
3191 * Emacs will automagically detect them.
3192 * ---------------------------------------------------------------------
3195 * indent-tabs-mode: t