* src/vm/linker.c (link_class_intern): Comment reformatted.
[cacao.git] / src / vm / linker.c
index 28a852752c2474f9ea01e4131080fad0ecbed504..9ea19e707875d3b361b904d61e6cbc11ae53c443 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/linker.c - class linker functions
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    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.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Reinhard Grafl
 
             Edwin Steiner
             Christian Thalinger
 
-   $Id: linker.c 2148 2005-03-30 16:49:40Z twisti $
+   $Id: linker.c 4528 2006-02-20 23:31:39Z twisti $
 
 */
 
 
+#include "config.h"
+
+#include <assert.h>
+
+#include "vm/types.h"
+
 #include "mm/memory.h"
 #include "native/native.h"
 #include "vm/builtin.h"
 #include "vm/class.h"
+#include "vm/classcache.h"
 #include "vm/exceptions.h"
 #include "vm/loader.h"
 #include "vm/options.h"
+#include "vm/resolve.h"
 #include "vm/statistics.h"
-#include "vm/jit/codegen.inc.h"
+#include "vm/stringlocal.h"
+#include "vm/access.h"
 
 
 /* global variables ***********************************************************/
@@ -62,7 +71,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).
 
 *******************************************************************************/
 
@@ -71,10 +80,12 @@ primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = {
        { NULL, NULL, "java/lang/Long",      'J', "long"    , "[J", NULL, NULL },
        { NULL, NULL, "java/lang/Float",     'F', "float"   , "[F", NULL, NULL },
        { NULL, NULL, "java/lang/Double",    'D', "double"  , "[D", NULL, NULL },
+       { NULL, NULL, NULL,                   0 , NULL      , NULL, NULL, NULL },
        { NULL, NULL, "java/lang/Byte",      'B', "byte"    , "[B", NULL, NULL },
        { NULL, NULL, "java/lang/Character", 'C', "char"    , "[C", NULL, NULL },
        { NULL, NULL, "java/lang/Short",     'S', "short"   , "[S", NULL, NULL },
        { NULL, NULL, "java/lang/Boolean",   'Z', "boolean" , "[Z", NULL, NULL },
+       { NULL, NULL, NULL,                   0 , NULL      , NULL, NULL, NULL },
        { NULL, NULL, "java/lang/Void",      'V', "void"    , NULL, NULL, NULL }
 };
 
@@ -102,6 +113,19 @@ bool linker_init(void)
 
        interfaceindex = 0;
 
+       /* link java.lang.Class as first class of the system, because we
+       need it's vftbl for all other classes so we can use a class as
+       object */
+
+       if (!link_class(class_java_lang_Class))
+               return false;
+
+       /* now set the header.vftbl of all classes which were created
+       before java.lang.Class was linked */
+
+       class_postset_header_vftbl();
+
+
        /* link important system classes */
 
        if (!link_class(class_java_lang_Object))
@@ -116,34 +140,128 @@ bool linker_init(void)
        if (!link_class(class_java_io_Serializable))
                return false;
 
+
+       /* link classes for wrapping primitive types */
+
+       if (!link_class(class_java_lang_Void))
+               return false;
+
+       if (!link_class(class_java_lang_Boolean))
+               return false;
+
+       if (!link_class(class_java_lang_Byte))
+               return false;
+
+       if (!link_class(class_java_lang_Character))
+               return false;
+
+       if (!link_class(class_java_lang_Short))
+               return false;
+
+       if (!link_class(class_java_lang_Integer))
+               return false;
+
+       if (!link_class(class_java_lang_Long))
+               return false;
+
+       if (!link_class(class_java_lang_Float))
+               return false;
+
+       if (!link_class(class_java_lang_Double))
+               return false;
+
+
+       /* load some other important classes */
+
+       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_Thread))
+               return false;
+
+       if (!link_class(class_java_lang_ThreadGroup))
+               return false;
+
+       if (!link_class(class_java_lang_VMThread))
+               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) */
     
-       pseudo_class_Arraystub->loaded = true;
-    pseudo_class_Arraystub->super = class_java_lang_Object;
-    pseudo_class_Arraystub->interfacescount = 2;
-    pseudo_class_Arraystub->interfaces = MNEW(classinfo*, 2);
-    pseudo_class_Arraystub->interfaces[0] = class_java_lang_Cloneable;
-    pseudo_class_Arraystub->interfaces[1] = class_java_io_Serializable;
-
-    if (!link_class(pseudo_class_Arraystub))
+       pseudo_class_Arraystub =
+               class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
+       pseudo_class_Arraystub->state |= CLASS_LOADED;
+       pseudo_class_Arraystub->super.cls = class_java_lang_Object;
+       pseudo_class_Arraystub->interfacescount = 2;
+       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_unique(pseudo_class_Arraystub)) {
+               log_text("could not cache pseudo_class_Arraystub");
+               assert(0);
+       }
+
+       if (!link_class(pseudo_class_Arraystub))
                return false;
 
-    /* pseudo class representing the null type */
-    
-       pseudo_class_Null->loaded = true;
-    pseudo_class_Null->super = class_java_lang_Object;
+       /* pseudo class representing the null type */
+
+       pseudo_class_Null = class_create_classinfo(utf_new_char("$NULL$"));
+       pseudo_class_Null->state |= CLASS_LOADED;
+       pseudo_class_Null->super.cls = class_java_lang_Object;
+
+       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;
 
-    /* pseudo class representing new uninitialized objects */
+       /* pseudo class representing new uninitialized objects */
     
-       pseudo_class_New->loaded = true;
-       pseudo_class_New->linked = true;
-       pseudo_class_New->super = class_java_lang_Object;
-
+       pseudo_class_New = class_create_classinfo(utf_new_char("$NEW$"));
+       pseudo_class_New->state |= CLASS_LOADED;
+       pseudo_class_New->state |= CLASS_LINKED; /* XXX is this allright? */
+       pseudo_class_New->super.cls = class_java_lang_Object;
+
+       if (!classcache_store_unique(pseudo_class_New)) {
+               log_text("could not cache pseudo_class_New");
+               assert(0);
+       }
 
        /* create classes representing primitive types */
 
@@ -169,35 +287,58 @@ 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_new_intern(utf_new_char(primitivetype_table[i].name));
-               c->classUsed = NOTUSED; /* not used initially CO-RT */
-               c->impldBy = NULL;
+
+               c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
+
+               c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
                
                /* prevent loader from loading primitive class */
-               c->loaded = true;
+
+               c->state |= CLASS_LOADED;
+
+               /* 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 */
-               c = class_new_intern(utf_new_char(primitivetype_table[i].wrapname));
+
+               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_new_intern(utf_new_char(primitivetype_table[i].arrayname));
+                       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;
-                       c->loaded = true;
-                       if (!c->linked)
+
+                       assert(c->state & CLASS_LOADED);
+
+                       if (!(c->state & CLASS_LINKED))
                                if (!link_class(c))
                                        return false;
+
                        primitivetype_table[i].arrayvftbl = c->vftbl;
                }
        }
@@ -217,6 +358,11 @@ classinfo *link_class(classinfo *c)
 {
        classinfo *r;
 
+       if (!c) {
+               exceptions_throw_nullpointerexception();
+               return NULL;
+       }
+
 #if defined(USE_THREADS)
        /* enter a monitor on the class */
 
@@ -224,7 +370,8 @@ classinfo *link_class(classinfo *c)
 #endif
 
        /* maybe the class is already linked */
-       if (c->linked) {
+
+       if (c->state & CLASS_LINKED) {
 #if defined(USE_THREADS)
                builtin_monitorexit((java_objectheader *) c);
 #endif
@@ -232,7 +379,7 @@ classinfo *link_class(classinfo *c)
                return c;
        }
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        /* measure time */
 
        if (getcompilingtime)
@@ -243,13 +390,15 @@ 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;
+               c->state &= ~CLASS_LINKING;
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        /* measure time */
 
        if (getloadingtime)
@@ -285,31 +434,49 @@ static classinfo *link_class_intern(classinfo *c)
        s4 vftbllength;               /* vftbllength of current class             */
        s4 interfacetablelength;      /* interface table length                   */
        vftbl_t *v;                   /* vftbl of current class                   */
-       s4 i;                         /* interface/method/field counter           */
+       s4 i,j;                       /* interface/method/field counter           */
        arraydescriptor *arraydesc;   /* descriptor for array classes             */
 
-       /* maybe the class is already linked */
-       if (c->linked)
-               return c;
+       /* the class is already linked */
 
-       /* the class must be loaded */
-       if (!c->loaded)
-               throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                  "Trying to link unloaded class");
+       if (c->state & CLASS_LINKED)
+               return c;
 
        if (linkverbose)
                log_message_class("Linking class: ", c);
 
-       /* ok, this class is somewhat linked */
-       c->linked = true;
+       /* the class must be loaded */
+
+       /* XXX should this be a specific exception? */
+       assert(c->state & CLASS_LOADED);
+
+       /* cache the self-reference of this class                          */
+       /* we do this for cases where the defining loader of the class     */
+       /* has not yet been recorded as an initiating loader for the class */
+       /* this is needed so subsequent code can assume that self-refs     */
+       /* will always resolve lazily                                      */
+       /* No need to do it for the bootloader - it is always registered   */
+       /* as initiating loader for the classes it loads.                  */
+       if (c->classloader)
+               classcache_store(c->classloader,c,false);
+
+       /* this class is currently linking */
+
+       c->state |= CLASS_LINKING;
 
        arraydesc = NULL;
 
        /* check interfaces */
 
        for (i = 0; i < c->interfacescount; i++) {
-               tc = c->interfaces[i];
+               /* resolve this super interface */
 
+               if (!resolve_classref_or_classinfo(NULL, c->interfaces[i], resolveEager,
+                                                                                  true, false, &tc))
+                       return NULL;
+
+               c->interfaces[i].cls = tc;
+               
                /* detect circularity */
 
                if (tc == c) {
@@ -319,9 +486,7 @@ static classinfo *link_class_intern(classinfo *c)
                        return NULL;
                }
 
-               if (!tc->loaded)
-                       if (!load_class_from_classloader(tc, c->classloader))
-                               return NULL;
+               assert(tc->state & CLASS_LOADED);
 
                if (!(tc->flags & ACC_INTERFACE)) {
                        *exceptionptr =
@@ -330,19 +495,17 @@ static classinfo *link_class_intern(classinfo *c)
                        return NULL;
                }
 
-               if (!tc->linked)
+               if (!(tc->state & CLASS_LINKED))
                        if (!link_class(tc))
                                return NULL;
        }
        
        /* check super class */
 
-       super = c->super;
+       super = NULL;
 
-       if (super == 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->impldBy = NULL;
                c->instancesize = sizeof(java_objectheader);
                
                vftbllength = supervftbllength = 0;
@@ -350,7 +513,15 @@ static classinfo *link_class_intern(classinfo *c)
                c->finalizer = NULL;
 
        } else {
+               /* resolve super class */
+
+               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,
@@ -358,16 +529,16 @@ static classinfo *link_class_intern(classinfo *c)
                        return NULL;
                }
 
-               if (!super->loaded)
-                       if (!load_class_from_classloader(super, c->classloader))
-                               return NULL;
+               assert(super->state & CLASS_LOADED);
 
                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,
@@ -375,11 +546,12 @@ static classinfo *link_class_intern(classinfo *c)
                        return NULL;
                }
                
-               if (!super->linked)
+               if (!(super->state & CLASS_LINKED))
                        if (!link_class(super))
                                return NULL;
 
                /* handle array classes */
+
                if (c->name->text[0] == '[')
                        if (!(arraydesc = link_array(c)))
                                return NULL;
@@ -413,6 +585,15 @@ static classinfo *link_class_intern(classinfo *c)
                                                if (tc->methods[j].flags & ACC_PRIVATE)
                                                        goto notfoundvftblindex;
 
+                                               /* package-private methods in other packages */
+                                               /* must not be overridden                    */
+                                               /* (see Java Language Specification 8.4.8.1) */
+                                               if ( !(tc->methods[j].flags & (ACC_PUBLIC | ACC_PROTECTED)) 
+                                                        && !SAME_PACKAGE(c,tc) ) 
+                                               {
+                                                   goto notfoundvftblindex;
+                                               }
+
                                                if (tc->methods[j].flags & ACC_FINAL) {
                                                        /* class a overrides final method . */
                                                        *exceptionptr =
@@ -425,7 +606,7 @@ static classinfo *link_class_intern(classinfo *c)
                                        }
                                }
 
-                               tc = tc->super;
+                               tc = tc->super.cls;
                        }
 
                notfoundvftblindex:
@@ -435,8 +616,8 @@ static classinfo *link_class_intern(classinfo *c)
                }
        }       
 
-#if 1
-       /* check interfaces for unimplemented methods in ABSTRACT class */
+
+       /* check interfaces of ABSTRACT class for unimplemented methods */
 
        if (c->flags & ACC_ABSTRACT) {
                classinfo  *ic;
@@ -448,11 +629,16 @@ static classinfo *link_class_intern(classinfo *c)
                abstractmethodscount = 0;
 
                for (i = 0; i < c->interfacescount; i++) {
-                       ic = c->interfaces[i];
+                       ic = c->interfaces[i].cls;
 
                        for (j = 0; j < ic->methodscount; j++) {
                                im = &(ic->methods[j]);
 
+                               /* skip `<clinit>' and `<init>' */
+
+                               if (im->name == utf_clinit || im->name == utf_init)
+                                       continue;
+
                                tc = c;
 
                                while (tc) {
@@ -461,7 +647,7 @@ static classinfo *link_class_intern(classinfo *c)
                                                        goto noabstractmethod;
                                        }
 
-                                       tc = tc->super;
+                                       tc = tc->super.cls;
                                }
 
                                abstractmethodscount++;
@@ -480,11 +666,16 @@ static classinfo *link_class_intern(classinfo *c)
                                                                  c->methodscount + abstractmethodscount);
 
                        for (i = 0; i < c->interfacescount; i++) {
-                               ic = c->interfaces[i];
+                               ic = c->interfaces[i].cls;
 
                                for (j = 0; j < ic->methodscount; j++) {
                                        im = &(ic->methods[j]);
 
+                                       /* skip `<clinit>' and `<init>' */
+
+                                       if (im->name == utf_clinit || im->name == utf_init)
+                                               continue;
+
                                        tc = c;
 
                                        while (tc) {
@@ -493,7 +684,7 @@ static classinfo *link_class_intern(classinfo *c)
                                                                goto noabstractmethod2;
                                                }
 
-                                               tc = tc->super;
+                                               tc = tc->super.cls;
                                        }
 
                                        am = &(c->methods[c->methodscount]);
@@ -510,9 +701,9 @@ static classinfo *link_class_intern(classinfo *c)
                        }
                }
        }
-#endif
 
-#if defined(STATISTICS)
+
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                count_vftbl_len +=
                        sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
@@ -524,11 +715,11 @@ static classinfo *link_class_intern(classinfo *c)
        tc = c;
        while (tc) {
                for (i = 0; i < tc->interfacescount; i++) {
-                       s4 h = class_highestinterface(tc->interfaces[i]) + 1;
+                       s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
                        if (h > interfacetablelength)
                                interfacetablelength = h;
                }
-               tc = tc->super;
+               tc = tc->super.cls;
        }
 
        /* allocate virtual function table */
@@ -538,7 +729,7 @@ static classinfo *link_class_intern(classinfo *c)
                                                          sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
        v = (vftbl_t *) (((methodptr *) v) +
                                         (interfacetablelength - 1) * (interfacetablelength > 1));
-       c->header.vftbl = c->vftbl = v;
+       c->vftbl = v;
        v->class = c;
        v->vftbllength = vftbllength;
        v->interfacetablelength = interfacetablelength;
@@ -559,25 +750,24 @@ static classinfo *link_class_intern(classinfo *c)
        for (i = 0; i < c->methodscount; i++) {
                methodinfo *m = &(c->methods[i]);
 
-#if 1
+               /* Methods in ABSTRACT classes from interfaces maybe already
+                  have a stubroutine. */
+
                if (!m->stubroutine) {
-                       if (!(m->flags & ACC_NATIVE)) {
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_INTRP)
+                       if (opt_intrp)
+                               m->stubroutine = intrp_createcompilerstub(m);
+                       else
+#endif
                                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)
+#else
+                       m->stubroutine = intrp_createcompilerstub(m);
 #endif
-                                       m->stubroutine = createnativestub(f, m);
-                       }
                }
-#endif
 
                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 */
@@ -587,7 +777,7 @@ static classinfo *link_class_intern(classinfo *c)
                fieldinfo *f = &(c->fields[i]);
                
                if (!(f->flags & ACC_STATIC)) {
-                       dsize = desc_typesize(f->descriptor);
+                       dsize = descriptor_typesize(f->parseddesc);
                        c->instancesize = ALIGN(c->instancesize, dsize);
                        f->offset = c->instancesize;
                        c->instancesize += dsize;
@@ -598,7 +788,7 @@ static classinfo *link_class_intern(classinfo *c)
        
        v->interfacevftbllength = MNEW(s4, interfacetablelength);
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
 #endif
@@ -610,9 +800,9 @@ static classinfo *link_class_intern(classinfo *c)
        
        /* add interfaces */
        
-       for (tc = c; tc != NULL; tc = tc->super)
+       for (tc = c; tc != NULL; tc = tc->super.cls)
                for (i = 0; i < tc->interfacescount; i++)
-                       linker_addinterface(c, tc->interfaces[i]);
+                       linker_addinterface(c, tc->interfaces[i].cls);
 
        /* add finalizer method (not for java.lang.Object) */
 
@@ -626,10 +816,30 @@ static classinfo *link_class_intern(classinfo *c)
                                c->finalizer = fi;
        }
 
+       /* resolve exception class references */
+
+       for (i = 0; i < c->methodscount; i++) {
+               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, true, false,
+                                                                                          &(m->exceptiontable[j].catchtype.cls)))
+                               return NULL;
+               }
+       }
+       
        /* final tasks */
 
        linker_compute_subclasses(c);
 
+       /* revert the linking state and class is linked */
+
+       c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
+
        if (linkverbose)
                log_message_class("Linking done class: ", c);
 
@@ -651,39 +861,43 @@ 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. */
-               comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
-               if (!comp)
-                       panic("Could not find component array class.");
+               u = utf_new(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. */
-               comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
-               if (!comp)
-                       panic("Could not find component class.");
+               u = utf_new(c->name->text + 2, namelen - 3);
+               if (!(comp = load_class_from_classloader(u, c->classloader)))
+                       return NULL;
                break;
        }
 
        /* If the component type has not been linked, link it now */
-       if (comp && !comp->linked) {
-               if (!comp->loaded)
-                       if (!load_class_from_classloader(comp, c->classloader))
-                               return NULL;
 
+       assert(!comp || (comp->state & CLASS_LOADED));
+
+       if (comp && !(comp->state & CLASS_LINKED))
                if (!link_class(comp))
                        return NULL;
-       }
 
        /* Allocate the arraydescriptor */
+
        desc = NEW(arraydescriptor);
 
        if (comp) {
@@ -693,14 +907,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;
 
@@ -762,7 +984,8 @@ static arraydescriptor *link_array(classinfo *c)
                        break;
 
                default:
-                       panic("Invalid array class name");
+                       *exceptionptr = new_noclassdeffounderror(c->name);
+                       return NULL;
                }
                
                desc->componentvftbl = NULL;
@@ -796,21 +1019,16 @@ static void linker_compute_subclasses(classinfo *c)
                c->sub = 0;
        }
 
-       if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
-               c->nextsub = c->super->sub;
-               c->super->sub = c;
+       if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
+               c->nextsub = c->super.cls->sub;
+               c->super.cls->sub = c;
        }
 
        classvalue = 0;
 
-       /* this is the java.lang.Object special case */
+       /* compute class values */
 
-       if (!class_java_lang_Object) {
-               linker_compute_class_values(c);
-
-       } 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)
@@ -859,8 +1077,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;
@@ -874,7 +1094,7 @@ static void linker_addinterface(classinfo *c, classinfo *ic)
                v->interfacevftbllength[i] = ic->methodscount;
                v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
                if (opt_stat)
                        count_vftbl_len += sizeof(methodptr) *
                                (ic->methodscount + (ic->methodscount == 0));
@@ -892,7 +1112,7 @@ static void linker_addinterface(classinfo *c, classinfo *ic)
                                                goto foundmethod;
                                        }
                                }
-                               sc = sc->super;
+                               sc = sc->super.cls;
                        }
                foundmethod:
                        ;
@@ -900,7 +1120,7 @@ static void linker_addinterface(classinfo *c, classinfo *ic)
        }
 
        for (j = 0; j < ic->interfacescount; j++) 
-               linker_addinterface(c, ic->interfaces[j]);
+               linker_addinterface(c, ic->interfaces[j].cls);
 }
 
 
@@ -922,7 +1142,7 @@ static s4 class_highestinterface(classinfo *c)
     h = c->index;
 
        for (i = 0; i < c->interfacescount; i++) {
-               h2 = class_highestinterface(c->interfaces[i]);
+               h2 = class_highestinterface(c->interfaces[i].cls);
 
                if (h2 > h)
                        h = h2;
@@ -943,4 +1163,5 @@ static s4 class_highestinterface(classinfo *c)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */