+/* loader.c - class loader functions
-/* loader.c ********************************************************************
+ Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+ R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
+ M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
+ P. Tomsich, J. Wenninger
- Copyright (c) 1999 A. Krall, R. Grafl, R. Obermaiser, M. Probst
+ This file is part of CACAO.
- See file COPYRIGHT for information on usage and disclaimer of warranties
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
- Contains the functions of the class loader.
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
- Author: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
- Changes: Andreas Krall EMAIL: cacao@complang.tuwien.ac.at
- Roman Obermaiser EMAIL: cacao@complang.tuwien.ac.at
- Mark Probst EMAIL: cacao@complang.tuwien.ac.at
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ 02111-1307, USA.
- Last Change: 1999/11/08
+ Contact: cacao@complang.tuwien.ac.at
-*******************************************************************************/
+ Authors: Reinhard Grafl
+ Changes: Andreas Krall
+ Roman Obermaiser
+ Mark Probst
+
+ $Id: loader.c 562 2003-11-03 00:34:34Z twisti $
+
+*/
-#include <assert.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/stat.h>
#include "global.h"
#include "loader.h"
#include "native.h"
#include "tables.h"
#include "builtin.h"
#include "jit.h"
-#ifdef OLD_COMPILER
-#include "compiler.h"
-#endif
#include "asmpart.h"
-
+#include "toolbox/memory.h"
+#include "toolbox/loging.h"
#include "threads/thread.h"
-#include <sys/stat.h>
+
/* global variables ***********************************************************/
-extern bool newcompiler; /* true if new compiler is used */
+bool opt_rt = false; /* true if RTA parse should be used RT-CO */
+bool opt_xta = false; /* true if XTA parse should be used XTA-CO */
+bool opt_vta = false; /* true if VTA parse should be used VTA-CO */
int count_class_infos = 0; /* variables for measurements */
int count_const_pool_len = 0;
classinfo *class_java_lang_Object;
classinfo *class_java_lang_String;
-classinfo *class_java_lang_ClassCastException;
-classinfo *class_java_lang_NullPointerException;
-classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
-classinfo *class_java_lang_NegativeArraySizeException;
-classinfo *class_java_lang_OutOfMemoryError;
-classinfo *class_java_lang_ArithmeticException;
-classinfo *class_java_lang_ArrayStoreException;
-classinfo *class_java_lang_ThreadDeath;
classinfo *class_array = NULL;
+/* stefan */
+/* These are made static so they cannot be used for throwing in native */
+/* functions. */
+static classinfo *class_java_lang_ClassCastException;
+static classinfo *class_java_lang_NullPointerException;
+static classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
+static classinfo *class_java_lang_NegativeArraySizeException;
+static classinfo *class_java_lang_OutOfMemoryError;
+static classinfo *class_java_lang_ArithmeticException;
+static classinfo *class_java_lang_ArrayStoreException;
+static classinfo *class_java_lang_ThreadDeath;
+
/******************************************************************************
******************************************************************************/
primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
- { NULL, NULL, "java/lang/Double", 'D', "double" },
- { NULL, NULL, "java/lang/Float", 'F', "float" },
- { NULL, NULL, "java/lang/Character", 'C', "char" },
- { NULL, NULL, "java/lang/Integer", 'I', "int" },
- { NULL, NULL, "java/lang/Long", 'J', "long" },
- { NULL, NULL, "java/lang/Byte", 'B', "byte" },
- { NULL, NULL, "java/lang/Short", 'S', "short" },
- { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" },
- { NULL, NULL, "java/lang/Void", 'V', "void" }};
+ { NULL, NULL, "java/lang/Integer", 'I', "int" },
+ { NULL, NULL, "java/lang/Long", 'J', "long" },
+ { NULL, NULL, "java/lang/Float", 'F', "float" },
+ { NULL, NULL, "java/lang/Double", 'D', "double" },
+ { NULL, NULL, "java/lang/Byte", 'B', "byte" },
+ { NULL, NULL, "java/lang/Character", 'C', "char" },
+ { NULL, NULL, "java/lang/Short", 'S', "short" },
+ { NULL, NULL, "java/lang/Boolean", 'Z', "boolean" },
+ { NULL, NULL, "java/lang/Void", 'V', "void" }
+};
/* instances of important system classes **************************************/
#define skip_nbytes(len) classbuf_pos+=len;
-#define suck_u1() (*++classbuf_pos)
+inline u1 suck_u1()
+{
+ return *++classbuf_pos;
+}
+inline u2 suck_u2()
+{
+ u1 a=suck_u1(), b=suck_u1();
+ return ((u2)a<<8)+(u2)b;
+}
+inline u4 suck_u4()
+{
+ u1 a=suck_u1(), b=suck_u1(), c=suck_u1(), d=suck_u1();
+ return ((u4)a<<24)+((u4)b<<16)+((u4)c<<8)+(u4)d;
+}
#define suck_s8() (s8) suck_u8()
#define suck_s2() (s2) suck_u2()
#define suck_s4() (s4) suck_u4()
#define suck_s1() (s1) suck_u1()
-#define suck_u2() (u2) ((suck_u1()<<8)+suck_u1())
-#define suck_u4() (u4) ((((u4)suck_u1())<<24)+(((u4)suck_u1())<<16)+(((u4)suck_u1())<<8)+((u4)suck_u1()))
/* get u8 from classfile data */
/******************************************************************************/
-/********** internal function: printflags (only for debugging) ***************/
-static void printflags (u2 f)
+void fprintflags (FILE *fp, u2 f)
+{
+ if ( f & ACC_PUBLIC ) fprintf (fp," PUBLIC");
+ if ( f & ACC_PRIVATE ) fprintf (fp," PRIVATE");
+ if ( f & ACC_PROTECTED ) fprintf (fp," PROTECTED");
+ if ( f & ACC_STATIC ) fprintf (fp," STATIC");
+ if ( f & ACC_FINAL ) fprintf (fp," FINAL");
+ if ( f & ACC_SYNCHRONIZED ) fprintf (fp," SYNCHRONIZED");
+ if ( f & ACC_VOLATILE ) fprintf (fp," VOLATILE");
+ if ( f & ACC_TRANSIENT ) fprintf (fp," TRANSIENT");
+ if ( f & ACC_NATIVE ) fprintf (fp," NATIVE");
+ if ( f & ACC_INTERFACE ) fprintf (fp," INTERFACE");
+ if ( f & ACC_ABSTRACT ) fprintf (fp," ABSTRACT");
+}
+
+/********** internal function: printflags (only for debugging) ***************/
+void printflags (u2 f)
{
if ( f & ACC_PUBLIC ) printf (" PUBLIC");
if ( f & ACC_PRIVATE ) printf (" PRIVATE");
case 'L':
d -> arraytype = ARRAYTYPE_OBJECT;
+
d -> objectclass = class_new ( utf_new(utf_ptr+1, namelen-3) );
+ d -> objectclass -> classUsed = NOTUSED; /* not used initially CO-RT */
+ d -> objectclass -> impldBy = NULL;
break;
}
return d;
/******************************************************************************/
-/******************** Functionen fuer Fields **********************************/
+/************************** Functions for fields ****************************/
/******************************************************************************/
/************************ Function: field_load *********************************
- l"adt alle Informationen f"ur eine Feld einer Methode aus dem ClassFile,
- und f"ullt mit diesen Infos eine schon existierende 'fieldinfo'-Struktur.
- Bei 'static'-Fields wird auch noch ein Platz auf dem Datensegment
- reserviert.
+ Load everything about a class field from the class file and fill a
+ 'fieldinfo' structure. For static fields, space in the data segment is
+ allocated.
*******************************************************************************/
f -> descriptor = class_getconstant (c, suck_u2(), CONSTANT_Utf8); /* JavaVM descriptor */
f -> type = jtype = desc_to_type (f->descriptor); /* data type */
f -> offset = 0; /* offset from start of object */
+ f->xta = NULL;
switch (f->type) {
case TYPE_INT: f->value.i = 0; break;
}
-/************** Function: field_display (nur zu Debug-Zwecken) ****************/
+/**************** Function: field_display (debugging only) ********************/
-static void field_display (fieldinfo *f)
+void field_display (fieldinfo *f)
{
printf (" ");
printflags (f -> flags);
m -> entrypoint = NULL;
m -> mcode = NULL;
m -> stubroutine = NULL;
+ m -> methodUsed = NOTUSED;
+ m -> monoPoly = MONO;
+ m -> subRedefs = 0;
+ m -> subRedefsUsed = 0;
+
+ m->xta = NULL;
if (! (m->flags & ACC_NATIVE) ) {
m -> stubroutine = createcompilerstub (m);
functionptr f = native_findfunction
(c->name, m->name, m->descriptor, (m->flags & ACC_STATIC) != 0);
if (f) {
-#ifdef OLD_COMPILER
- if (newcompiler)
-#endif
m -> stubroutine = createnativestub (f, m);
-#ifdef OLD_COMPILER
- else
- m -> stubroutine = oldcreatenativestub (f, m);
-#endif
}
}
/******************************************************************************/
-/******************** function: class_getconstant ******************************
+/******************** function:: class_getconstant ******************************
retrieves the value at position 'pos' of the constantpool of a class
if the type of the value is other than 'ctype' the system is stopped
/* output for debugging purposes */
if (loadverbose) {
+
sprintf (logtext, "Loading class: ");
utf_sprint (logtext+strlen(logtext), c->name );
dolog();
/* check version */
mi = suck_u2();
ma = suck_u2();
- if (ma != MAJOR_VERSION) {
- sprintf (logtext, "Can only support major version %d, but not %d",
- MAJOR_VERSION, (int) ma);
+ if (ma != MAJOR_VERSION && (ma != MAJOR_VERSION+1 || mi != 0)) {
+ sprintf (logtext, "File version %d.%d is not supported",
+ (int) ma, (int) mi);
error();
}
- if (mi > MINOR_VERSION) {
- sprintf (logtext, "Minor version %d is not yet supported.", (int) mi);
- error();
- }
class_loadcpool (c);
+ c -> classUsed = NOTUSED; /* not used initially CO-RT */
+ c -> impldBy = NULL;
+
/* ACC flags */
c -> flags = suck_u2 ();
/* this class */
/* load fields */
c -> fieldscount = suck_u2 ();
- c -> fields = MNEW (fieldinfo, c -> fieldscount);
+ c -> fields = GCNEW (fieldinfo, c -> fieldscount);
for (i=0; i < c -> fieldscount; i++) {
field_load (&(c->fields[i]), c);
}
*******************************************************************************/
-static void class_link (classinfo *c)
+void class_link (classinfo *c)
{
s4 supervftbllength; /* vftbllegnth of super class */
s4 vftbllength; /* vftbllength of current class */
if (super == NULL) { /* class java.long.Object */
c->index = 0;
+ c->classUsed = USED; /* Object class is always used CO-RT*/
+ c -> impldBy = NULL;
c->instancesize = sizeof(java_objectheader);
vftbllength = supervftbllength = 0;
for (i = 0; i < c->fieldscount; i++)
field_free(&(c->fields[i]));
- MFREE (c->fields, fieldinfo, c->fieldscount);
for (i = 0; i < c->methodscount; i++)
method_free(&(c->methods[i]));
if (c->innerclasscount)
MFREE (c->innerclass, innerclassinfo, c->innerclasscount);
+ if (c->classvftbl)
+ mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1));
+
FREE (c, classinfo);
}
s4 i;
int b;
+
if (!makeinitializations)
return;
if (c->initialized)
if (c->super)
class_init (c->super);
for (i=0; i < c->interfacescount; i++)
- class_init(c->interfaces[i]);
+ class_init(c->interfaces[i]); /* real */
m = class_findmethod (c, utf_clinit, utf_fidesc);
if (!m) {
blockInts = 0;
#endif
- exceptionptr = asm_calljavamethod (m, NULL,NULL,NULL,NULL);
+ exceptionptr = asm_calljavamethod(m, NULL, NULL, NULL, NULL);
#ifdef USE_THREADS
assert(blockInts == 0);
blockInts = b;
#endif
- if (exceptionptr) {
- printf ("#### Initializer of ");
- utf_display (c->name);
- printf (" has thrown: ");
- utf_display (exceptionptr->vftbl->class->name);
- printf ("\n");
- fflush (stdout);
- }
+ if (exceptionptr) {
+ printf("#### Initializer of ");
+ utf_display(c->name);
+ printf(" has thrown: ");
+ utf_display(exceptionptr->vftbl->class->name);
+ printf("\n");
+ fflush(stdout);
+ }
if (initverbose) {
- sprintf (logtext, "Finished initializer for class: ");
- utf_sprint (logtext+strlen(logtext), c->name);
- dolog ();
+ sprintf(logtext, "Finished initializer for class: ");
+ utf_sprint(logtext+strlen(logtext), c->name);
+ dolog();
}
if (c->name == utf_systemclass) {
if (!m) {
/* no method found */
- log("initializeSystemClass failed");
+ printf("initializeSystemClass failed");
return;
}
-/********* Function: class_showconstantpool (debugging only) *********/
+/********* Function: find_class_method_constant *********/
+int find_class_method_constant (classinfo *c, utf * c1, utf* m1, utf* d1)
+
+{
+ u4 i;
+ voidptr e;
+
+ for (i=0; i<c->cpcount; i++) {
+
+ e = c -> cpinfos [i];
+ if (e) {
+
+ switch (c -> cptags [i]) {
+ case CONSTANT_Methodref:
+ {
+ constant_FMIref *fmi = e;
+ if ( (fmi->class->name == c1)
+ && (fmi->name == m1)
+ && (fmi->descriptor == d1)) {
+
+ return i;
+ }
+ }
+ break;
+ case CONSTANT_InterfaceMethodref:
+ {
+ constant_FMIref *fmi = e;
+ if ( (fmi->class->name == c1)
+ && (fmi->name == m1)
+ && (fmi->descriptor == d1)) {
+
+ return i;
+ }
+ }
+ break;
+ }
+
+ }
+
+ }
+return -1;
+}
+
+void class_showconstanti(classinfo *c, int ii)
+{
+ u4 i = ii;
+ voidptr e;
+
+
+e = c -> cpinfos [i];
+printf ("#%d: ", (int) i);
+if (e) {
+ switch (c -> cptags [i]) {
+ case CONSTANT_Class:
+ printf ("Classreference -> ");
+ utf_display ( ((classinfo*)e) -> name );
+ break;
+
+ case CONSTANT_Fieldref:
+ printf ("Fieldref -> "); goto displayFMIi;
+ case CONSTANT_Methodref:
+ printf ("Methodref -> "); goto displayFMIi;
+ case CONSTANT_InterfaceMethodref:
+ printf ("InterfaceMethod -> "); goto displayFMIi;
+ displayFMIi:
+ {
+ constant_FMIref *fmi = e;
+ utf_display ( fmi->class->name );
+ printf (".");
+ utf_display ( fmi->name);
+ printf (" ");
+ utf_display ( fmi->descriptor );
+ }
+ break;
+
+ case CONSTANT_String:
+ printf ("String -> ");
+ utf_display (e);
+ break;
+ case CONSTANT_Integer:
+ printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
+ break;
+ case CONSTANT_Float:
+ printf ("Float -> %f", ((constant_float*)e) -> value);
+ break;
+ case CONSTANT_Double:
+ printf ("Double -> %f", ((constant_double*)e) -> value);
+ break;
+ case CONSTANT_Long:
+ {
+ u8 v = ((constant_long*)e) -> value;
+#if U8_AVAILABLE
+ printf ("Long -> %ld", (long int) v);
+#else
+ printf ("Long -> HI: %ld, LO: %ld\n",
+ (long int) v.high, (long int) v.low);
+#endif
+ }
+ break;
+ case CONSTANT_NameAndType:
+ { constant_nameandtype *cnt = e;
+ printf ("NameAndType: ");
+ utf_display (cnt->name);
+ printf (" ");
+ utf_display (cnt->descriptor);
+ }
+ break;
+ case CONSTANT_Utf8:
+ printf ("Utf8 -> ");
+ utf_display (e);
+ break;
+ case CONSTANT_Arraydescriptor: {
+ printf ("Arraydescriptor: ");
+ displayarraydescriptor (e);
+ }
+ break;
+ default:
+ panic ("Invalid type of ConstantPool-Entry");
+ } }
+printf("\n");
+
+}
void class_showconstantpool (classinfo *c)
{
for (i=0;i<PRIMITIVETYPE_COUNT;i++) {
/* create primitive class */
classinfo *c = class_new ( utf_new_char(primitivetype_table[i].name) );
-
+ c -> classUsed = NOTUSED; /* not used initially CO-RT */
+ c -> impldBy = NULL;
+
/* prevent loader from loading primitive class */
list_remove (&unloadedclasses, c);
/* add to unlinked classes */
/* create class for wrapping the primitive type */
primitivetype_table[i].class_wrap =
class_new( utf_new_char(primitivetype_table[i].wrapname) );
+ primitivetype_table[i].class_wrap -> classUsed = NOTUSED; /* not used initially CO-RT */
+ primitivetype_table[i].class_wrap -> impldBy = NULL;
}
}
*******************************************************************************/
-void loader_init ()
+void loader_init()
{
utf *string_class;
interfaceindex = 0;
/* create class for arrays */
class_array = class_new ( utf_new_char ("The_Array_Class") );
+ class_array -> classUsed = NOTUSED; /* not used initially CO-RT */
+ class_array -> impldBy = NULL;
+
list_remove (&unloadedclasses, class_array);
/* create class for strings, load it after class Object was loaded */
string_class = utf_new_char ("java/lang/String");
class_java_lang_String = class_new(string_class);
+ class_java_lang_String -> classUsed = NOTUSED; /* not used initially CO-RT */
+ class_java_lang_String -> impldBy = NULL;
+
list_remove (&unloadedclasses, class_java_lang_String);
class_java_lang_Object =
classinfo *subs;
c->vftbl->baseval = ++classvalue;
+
subs = c->sub;
while (subs != NULL) {
loader_compute_class_values(subs);
subs = subs->nextsub;
}
c->vftbl->diffval = classvalue - c->vftbl->baseval;
-/*
+ /*
{
int i;
for (i = 0; i < c->index; i++)
utf_display(c->name);
printf("\n");
}
-*/
+ */
}
}
c = list_next (&linkedclasses, c);
}
-
classvalue = 0;
loader_compute_class_values(class_java_lang_Object);
* tab-width: 4
* End:
*/
-