* libffi: Added.
[cacao.git] / src / vm / linker.c
index fa79e8ca43d5725d666c3480e38fbb596d03663f..20351062904246dbfc113119ea21cf4cf06402bc 100644 (file)
             Edwin Steiner
             Christian Thalinger
 
-   $Id: linker.c 2239 2005-04-06 12:16:53Z twisti $
+   $Id: linker.c 3267 2005-09-21 20:21:48Z twisti $
 
 */
 
 
 #include <assert.h>
+
 #include "mm/memory.h"
 #include "native/native.h"
 #include "vm/builtin.h"
@@ -48,6 +49,7 @@
 #include "vm/options.h"
 #include "vm/resolve.h"
 #include "vm/statistics.h"
+#include "vm/stringlocal.h"
 #include "vm/jit/codegen.inc.h"
 
 
@@ -65,7 +67,7 @@ static s4 classvalue;
    primitive class.
  
    CAUTION: Don't change the order of the types. This table is indexed
-   by the ARRAYTYPE_ constants (expcept ARRAYTYPE_OBJECT).
+   by the ARRAYTYPE_ constants (except ARRAYTYPE_OBJECT).
 
 *******************************************************************************/
 
@@ -152,6 +154,48 @@ bool linker_init(void)
                return false;
 
 
+       /* load some other important classes */
+
+       if (!link_class(class_java_lang_Class))
+               return false;
+
+       if (!link_class(class_java_lang_ClassLoader))
+               return false;
+
+       if (!link_class(class_java_lang_SecurityManager))
+               return false;
+
+       if (!link_class(class_java_lang_System))
+               return false;
+
+       if (!link_class(class_java_lang_ThreadGroup))
+               return false;
+
+
+       /* some classes which may be used more often */
+
+       if (!link_class(class_java_lang_StackTraceElement))
+               return false;
+
+       if (!link_class(class_java_lang_reflect_Constructor))
+               return false;
+
+       if (!link_class(class_java_lang_reflect_Field))
+               return false;
+
+       if (!link_class(class_java_lang_reflect_Method))
+               return false;
+
+       if (!link_class(class_java_security_PrivilegedAction))
+               return false;
+
+       if (!link_class(class_java_util_Vector))
+               return false;
+
+       if (!link_class(arrayclass_java_lang_Object))
+               return false;
+
+
        /* create pseudo classes used by the typechecker */
 
     /* pseudo class for Arraystubs (extends java.lang.Object) */
@@ -164,8 +208,11 @@ bool linker_init(void)
     pseudo_class_Arraystub->interfaces = MNEW(classref_or_classinfo, 2);
     pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
     pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
-       if (!classcache_store(NULL, pseudo_class_Arraystub))
-               panic("could not cache pseudo_class_Arraystub");
+
+       if (!classcache_store_unique(pseudo_class_Arraystub)) {
+               log_text("could not cache pseudo_class_Arraystub");
+               assert(0);
+       }
 
     if (!link_class(pseudo_class_Arraystub))
                return false;
@@ -175,8 +222,11 @@ bool linker_init(void)
        pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
        pseudo_class_Null->loaded = true;
     pseudo_class_Null->super.cls = class_java_lang_Object;
-       if (!classcache_store(NULL, pseudo_class_Null))
-               panic("could not cache pseudo_class_Null");
+
+       if (!classcache_store_unique(pseudo_class_Null)) {
+               log_text("could not cache pseudo_class_Null");
+               assert(0);
+       }
 
        if (!link_class(pseudo_class_Null))
                return false;
@@ -187,8 +237,11 @@ bool linker_init(void)
        pseudo_class_New->loaded = true;
        pseudo_class_New->linked = true; /* XXX is this allright? */
        pseudo_class_New->super.cls = class_java_lang_Object;
-       if (!classcache_store(NULL,pseudo_class_New))
-               panic("could not cache pseudo_class_New");
+
+       if (!classcache_store_unique(pseudo_class_New)) {
+               log_text("could not cache pseudo_class_New");
+               assert(0);
+       }
 
        /* create classes representing primitive types */
 
@@ -214,46 +267,61 @@ bool linker_init(void)
 static bool link_primitivetype_table(void)
 {  
        classinfo *c;
-       s4 i;
+       utf       *u;
+       s4         i;
 
        for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
                /* skip dummies */
+
                if (!primitivetype_table[i].name)
                        continue;
                
                /* create primitive class */
+
                c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
+
+               c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
                c->classUsed = NOTUSED; /* not used initially CO-RT */
                c->impldBy = NULL;
                
                /* prevent loader from loading primitive class */
+
                c->loaded = true;
-               if (!classcache_store(NULL,c)) {
-                       log_text("Could not cache primitive class");
-                       return false;
-               }
+
+               /* INFO: don't put primitive classes into the classcache */
+
                if (!link_class(c))
                        return false;
 
                primitivetype_table[i].class_primitive = c;
 
                /* create class for wrapping the primitive type */
-               if (!load_class_bootstrap(utf_new_char(primitivetype_table[i].wrapname),&c))
+
+               u = utf_new_char(primitivetype_table[i].wrapname);
+
+               if (!(c = load_class_bootstrap(u)))
                        return false;
+
                primitivetype_table[i].class_wrap = c;
                primitivetype_table[i].class_wrap->classUsed = NOTUSED; /* not used initially CO-RT */
                primitivetype_table[i].class_wrap->impldBy = NULL;
 
                /* create the primitive array class */
+
                if (primitivetype_table[i].arrayname) {
-                       c = class_create_classinfo(utf_new_char(primitivetype_table[i].arrayname));
-                       if (!load_newly_created_array(c, NULL))
+                       u = utf_new_char(primitivetype_table[i].arrayname);
+                       c = class_create_classinfo(u);
+                       c = load_newly_created_array(c, NULL);
+                       if (c == NULL)
                                return false;
+
                        primitivetype_table[i].arrayclass = c;
+
                        assert(c->loaded);
                        if (!c->linked)
                                if (!link_class(c))
                                        return false;
+
                        primitivetype_table[i].arrayvftbl = c->vftbl;
                }
        }
@@ -304,9 +372,11 @@ classinfo *link_class(classinfo *c)
 #endif
 
        /* call the internal function */
+
        r = link_class_intern(c);
 
        /* if return value is NULL, we had a problem and the class is not linked */
+
        if (!r)
                c->linked = false;
 
@@ -350,6 +420,7 @@ static classinfo *link_class_intern(classinfo *c)
        arraydescriptor *arraydesc;   /* descriptor for array classes             */
 
        /* maybe the class is already linked */
+
        if (c->linked)
                return c;
 
@@ -357,11 +428,13 @@ static classinfo *link_class_intern(classinfo *c)
                log_message_class("Linking class: ", c);
 
        /* the class must be loaded */
+
        if (!c->loaded)
                throw_cacao_exception_exit(string_java_lang_InternalError,
                                                                   "Trying to link unloaded class");
 
        /* ok, this class is somewhat linked */
+
        c->linked = true;
 
        arraydesc = NULL;
@@ -370,8 +443,11 @@ static classinfo *link_class_intern(classinfo *c)
 
        for (i = 0; i < c->interfacescount; i++) {
                /* resolve this super interface */
-               if (!resolve_classref_or_classinfo(NULL,c->interfaces[i],resolveEager,false,&tc))
+
+               if (!resolve_classref_or_classinfo(NULL, c->interfaces[i], resolveEager,
+                                                                                  true, false, &tc))
                        return NULL;
+
                c->interfaces[i].cls = tc;
                
                /* detect circularity */
@@ -400,9 +476,10 @@ static classinfo *link_class_intern(classinfo *c)
        /* check super class */
 
        super = NULL;
-       if (c->super.any == NULL) {          /* class java.lang.Object */
+
+       if (c->super.any == NULL) {                     /* class java.lang.Object */
                c->index = 0;
-        c->classUsed = USED;     /* Object class is always used CO-RT*/
+        c->classUsed = USED;              /* Object class is always used CO-RT*/
                c->impldBy = NULL;
                c->instancesize = sizeof(java_objectheader);
                
@@ -412,11 +489,14 @@ static classinfo *link_class_intern(classinfo *c)
 
        } else {
                /* resolve super class */
-               if (!resolve_classref_or_classinfo(NULL,c->super,resolveEager,false,&super))
+
+               if (!resolve_classref_or_classinfo(NULL, c->super, resolveEager, true, false,
+                                                                                  &super))
                        return NULL;
                c->super.cls = super;
                
                /* detect circularity */
+
                if (super == c) {
                        *exceptionptr =
                                new_exception_utfmessage(string_java_lang_ClassCircularityError,
@@ -428,10 +508,12 @@ static classinfo *link_class_intern(classinfo *c)
 
                if (super->flags & ACC_INTERFACE) {
                        /* java.lang.IncompatibleClassChangeError: class a has interface java.lang.Cloneable as super class */
-                       panic("Interface specified as super class");
+                       log_text("Interface specified as super class");
+                       assert(0);
                }
 
                /* Don't allow extending final classes */
+
                if (super->flags & ACC_FINAL) {
                        *exceptionptr =
                                new_exception_message(string_java_lang_VerifyError,
@@ -444,6 +526,7 @@ static classinfo *link_class_intern(classinfo *c)
                                return NULL;
 
                /* handle array classes */
+
                if (c->name->text[0] == '[')
                        if (!(arraydesc = link_array(c)))
                                return NULL;
@@ -636,24 +719,11 @@ static classinfo *link_class_intern(classinfo *c)
                /* Methods in ABSTRACT classes from interfaces maybe already have a   */
                /* stubroutine.                                                       */
 
-               if (!m->stubroutine) {
-                       if (!(m->flags & ACC_NATIVE)) {
-                               m->stubroutine = createcompilerstub(m);
-
-                       } else {
-                               functionptr f = native_findfunction(c->name,
-                                                                               m->name,
-                                                                               m->descriptor,
-                                                                               (m->flags & ACC_STATIC));
-#if defined(STATIC_CLASSPATH)
-                               if (f)
-#endif
-                                       m->stubroutine = createnativestub(f, m);
-                       }
-               }
+               if (!m->stubroutine)
+                       m->stubroutine = createcompilerstub(m);
 
                if (!(m->flags & ACC_STATIC))
-                       v->table[m->vftblindex] = m->stubroutine;
+                       v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
        }
 
        /* compute instance size and offset of each field */
@@ -705,17 +775,22 @@ static classinfo *link_class_intern(classinfo *c)
        /* resolve exception class references */
 
        for (i = 0; i < c->methodscount; i++) {
-               methodinfo *m = c->methods + i;
-               for (j=0; j<m->exceptiontablelength; ++j) {
+               methodinfo *m = &(c->methods[i]);
+
+               for (j = 0; j < m->exceptiontablelength; j++) {
                        if (!m->exceptiontable[j].catchtype.any)
                                continue;
-                       if (!resolve_classref_or_classinfo(NULL,m->exceptiontable[j].catchtype,
-                                               resolveEager,false,&(m->exceptiontable[j].catchtype.cls)))
+                       if (!resolve_classref_or_classinfo(NULL,
+                                                                                          m->exceptiontable[j].catchtype,
+                                                                                          resolveEager, true, false,
+                                                                                          &(m->exceptiontable[j].catchtype.cls)))
                                return NULL;
                }
-               for (j=0; j<m->thrownexceptionscount; ++j)
-                       if (!resolve_classref_or_classinfo(NULL,m->thrownexceptions[j],
-                                               resolveEager,false,&(m->thrownexceptions[j].cls)))
+
+               for (j = 0; j < m->thrownexceptionscount; j++)
+                       if (!resolve_classref_or_classinfo(NULL, m->thrownexceptions[j],
+                                                                                          resolveEager, true, false,
+                                                                                          &(m->thrownexceptions[j].cls)))
                                return NULL;
        }
        
@@ -744,24 +819,29 @@ static classinfo *link_class_intern(classinfo *c)
 
 static arraydescriptor *link_array(classinfo *c)
 {
-       classinfo *comp = NULL;
-       s4 namelen = c->name->blength;
+       classinfo       *comp;
+       s4               namelen;
        arraydescriptor *desc;
-       vftbl_t *compvftbl;
+       vftbl_t         *compvftbl;
+       utf             *u;
+
+       comp = NULL;
+       namelen = c->name->blength;
 
        /* Check the component type */
+
        switch (c->name->text[1]) {
        case '[':
                /* c is an array of arrays. */
-               if (!load_class_from_classloader(utf_new_intern(c->name->text + 1, namelen - 1),
-                                                                                c->classloader,&comp))
+               u = utf_new_intern(c->name->text + 1, namelen - 1);
+               if (!(comp = load_class_from_classloader(u, c->classloader)))
                        return NULL;
                break;
 
        case 'L':
                /* c is an array of objects. */
-               if (!load_class_from_classloader(utf_new_intern(c->name->text + 2, namelen - 3),
-                                                                                c->classloader,&comp))
+               u = utf_new_intern(c->name->text + 2, namelen - 3);
+               if (!(comp = load_class_from_classloader(u, c->classloader)))
                        return NULL;
                break;
        }
@@ -784,14 +864,22 @@ static arraydescriptor *link_array(classinfo *c)
                desc->dataoffset = OFFSET(java_objectarray, data);
                
                compvftbl = comp->vftbl;
-               if (!compvftbl)
-                       panic("Component class has no vftbl");
+
+               if (!compvftbl) {
+                       log_text("Component class has no vftbl");
+                       assert(0);
+               }
+
                desc->componentvftbl = compvftbl;
                
                if (compvftbl->arraydesc) {
                        desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
-                       if (compvftbl->arraydesc->dimension >= 255)
-                               panic("Creating array of dimension >255");
+
+                       if (compvftbl->arraydesc->dimension >= 255) {
+                               log_text("Creating array of dimension >255");
+                               assert(0);
+                       }
+
                        desc->dimension = compvftbl->arraydesc->dimension + 1;
                        desc->elementtype = compvftbl->arraydesc->elementtype;
 
@@ -853,7 +941,8 @@ static arraydescriptor *link_array(classinfo *c)
                        break;
 
                default:
-                       panic("Invalid array class name");
+                       *exceptionptr = new_classnotfoundexception(c->name);
+                       return NULL;
                }
                
                desc->componentvftbl = NULL;
@@ -894,14 +983,9 @@ static void linker_compute_subclasses(classinfo *c)
 
        classvalue = 0;
 
-       /* this is the java.lang.Object special case */
-
-       if (!class_java_lang_Object) {
-               linker_compute_class_values(c);
+       /* compute class values */
 
-       } else {
-               linker_compute_class_values(class_java_lang_Object);
-       }
+       linker_compute_class_values(class_java_lang_Object);
 
 #if defined(USE_THREADS)
 #if defined(NATIVE_THREADS)
@@ -950,8 +1034,10 @@ static void linker_addinterface(classinfo *c, classinfo *ic)
        s4     i   = ic->index;
        vftbl_t *v = c->vftbl;
 
-       if (i >= v->interfacetablelength)
-               panic ("Inernal error: interfacetable overflow");
+       if (i >= v->interfacetablelength) {
+               log_text("Inernal error: interfacetable overflow");
+               assert(0);
+       }
 
        if (v->interfacetable[-i])
                return;
@@ -1023,43 +1109,6 @@ static s4 class_highestinterface(classinfo *c)
 }
 
 
-/***************** Function: print_arraydescriptor ****************************
-
-       Debug helper for displaying an arraydescriptor
-       
-*******************************************************************************/
-
-void print_arraydescriptor(FILE *file, arraydescriptor *desc)
-{
-       if (!desc) {
-               fprintf(file, "<NULL>");
-               return;
-       }
-
-       fprintf(file, "{");
-       if (desc->componentvftbl) {
-               if (desc->componentvftbl->class)
-                       utf_fprint(file, desc->componentvftbl->class->name);
-               else
-                       fprintf(file, "<no classinfo>");
-       }
-       else
-               fprintf(file, "0");
-               
-       fprintf(file, ",");
-       if (desc->elementvftbl) {
-               if (desc->elementvftbl->class)
-                       utf_fprint(file, desc->elementvftbl->class->name);
-               else
-                       fprintf(file, "<no classinfo>");
-       }
-       else
-               fprintf(file, "0");
-       fprintf(file, ",%d,%d,%d,%d}", desc->arraytype, desc->dimension,
-                       desc->dataoffset, desc->componentsize);
-}
-
-
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where