Merged subtype branch to new head.
[cacao.git] / src / vm / linker.c
index 3f1d4adb3ba9658fb582e809361ac542faefb5f5..c1b00e40b199f8a605f4a6249c62db6b88851c9f 100644 (file)
@@ -1,9 +1,7 @@
 /* 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, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    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
-
-   Authors: Reinhard Grafl
+*/
 
-   Changes: Andreas Krall
-            Roman Obermaiser
-            Mark Probst
-            Edwin Steiner
-            Christian Thalinger
 
-   $Id: linker.c 2193 2005-04-02 19:33:43Z edwin $
+#include "config.h"
 
-*/
+#include <assert.h>
+#include <stdint.h>
 
+#include "vm/types.h"
 
-#include <assert.h>
 #include "mm/memory.h"
+
 #include "native/native.h"
-#include "vm/builtin.h"
+
+#include "threads/lock-common.h"
+#include "threads/mutex.hpp"
+
+#include "toolbox/logging.h"
+
+#include "vm/access.h"
+#include "vm/array.h"
 #include "vm/class.h"
-#include "vm/exceptions.h"
-#include "vm/loader.h"
+#include "vm/classcache.h"
+#include "vm/exceptions.hpp"
+#include "vm/globals.hpp"
+#include "vm/loader.hpp"
 #include "vm/options.h"
-#include "vm/resolve.h"
-#include "vm/statistics.h"
-#include "vm/jit/codegen.inc.h"
-
+#include "vm/primitive.hpp"
+#include "vm/rt-timing.h"
+#include "vm/string.hpp"
+#include "vm/vm.hpp"
+
+#include "vm/jit/asmpart.h"
+#include "vm/jit/stubs.hpp"
+
+
+/* debugging macros ***********************************************************/
+
+#if !defined(NDEBUG)
+# define TRACELINKCLASS(c) \
+    do { \
+        if (opt_TraceLinkClass) { \
+            log_start(); \
+            log_print("[Linking "); \
+            class_print((c)); \
+            log_print("]"); \
+            log_finish(); \
+        } \
+    } while (0)
+#else
+# define TRACELINKCLASS(c)
+#endif
 
-/* global variables ***********************************************************/
 
-static s4 interfaceindex;       /* sequential numbering of interfaces         */
-static s4 classvalue;
+/* #include "vm/resolve.h" */
+/* copied prototype to avoid bootstrapping problem: */
+classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool checkaccess);
 
+#if defined(ENABLE_STATISTICS)
+# include "vm/statistics.h"
+#endif
 
-/* primitivetype_table *********************************************************
+#if !defined(NDEBUG) && defined(ENABLE_INLINING)
+#define INLINELOG(code)  do { if (opt_TraceInlining) { code } } while (0)
+#else
+#define INLINELOG(code)
+#endif
 
-   Structure for primitive classes: contains the class for wrapping
-   the primitive type, the primitive class, the name of the class for
-   wrapping, the one character type signature and the name of the
-   primitive class.
-   CAUTION: Don't change the order of the types. This table is indexed
-   by the ARRAYTYPE_ constants (expcept ARRAYTYPE_OBJECT).
 
-*******************************************************************************/
+/* global variables ***********************************************************/
 
-primitivetypeinfo primitivetype_table[PRIMITIVETYPE_COUNT] = { 
-       { NULL, NULL, "java/lang/Integer",   'I', "int"     , "[I", NULL, NULL },
-       { 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 }
-};
+static s4 interfaceindex;       /* sequential numbering of interfaces         */
+static s4 classvalue;
 
 
 /* private functions **********************************************************/
 
-static bool link_primitivetype_table(void);
 static classinfo *link_class_intern(classinfo *c);
 static arraydescriptor *link_array(classinfo *c);
 static void linker_compute_class_values(classinfo *c);
 static void linker_compute_subclasses(classinfo *c);
-static void linker_addinterface(classinfo *c, classinfo *ic);
+static bool linker_addinterface(classinfo *c, classinfo *ic);
 static s4 class_highestinterface(classinfo *c);
 
 
 /* linker_init *****************************************************************
 
-   Initializes the linker subsystem.
+   Initializes the linker subsystem and links classes required for the
+   primitive table.
 
 *******************************************************************************/
 
-bool linker_init(void)
+void linker_preinit(void)
 {
-       /* reset interface index */
+       TRACESUBSYSTEMINITIALIZATION("linker_preinit");
+
+       /* Reset interface index. */
 
        interfaceindex = 0;
 
-       /* link important system classes */
+       /* Link the most basic classes. */
 
        if (!link_class(class_java_lang_Object))
-               return false;
-
-       if (!link_class(class_java_lang_String))
-               return false;
+               vm_abort("linker_preinit: linking java/lang/Object failed");
 
+#if defined(ENABLE_JAVASE)
        if (!link_class(class_java_lang_Cloneable))
-               return false;
+               vm_abort("linker_preinit: linking java/lang/Cloneable failed");
 
        if (!link_class(class_java_io_Serializable))
-               return false;
+               vm_abort("linker_preinit: linking java/io/Serializable failed");
+#endif
+}
+
+
+/* linker_init *****************************************************************
+
+   Links all classes required in the VM.
+
+*******************************************************************************/
+
+void linker_init(void)
+{
+       TRACESUBSYSTEMINITIALIZATION("linker_init");
+
+       /* 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))
+               vm_abort("linker_init: linking java/lang/Class failed");
+
+       /* Now set the header.vftbl of all classes which were created
+       before java.lang.Class was linked. */
 
+       class_postset_header_vftbl();
 
-       /* link classes for wrapping primitive types */
+       /* Link primitive-type wrapping classes. */
 
+#if defined(ENABLE_JAVASE)
        if (!link_class(class_java_lang_Void))
-               return false;
+               vm_abort("linker_init: linking failed");
+#endif
 
        if (!link_class(class_java_lang_Boolean))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Byte))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Character))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Short))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Integer))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Long))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Float))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Double))
-               return false;
+               vm_abort("linker_init: linking failed");
 
+       /* Link important system classes. */
 
-       /* create pseudo classes used by the typechecker */
+       if (!link_class(class_java_lang_String))
+               vm_abort("linker_init: linking java/lang/String failed");
 
-    /* pseudo class for Arraystubs (extends java.lang.Object) */
-    
-    pseudo_class_Arraystub = class_new_intern(utf_new_char("$ARRAYSTUB$"));
-       pseudo_class_Arraystub->loaded = true;
-    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 (!link_class(pseudo_class_Arraystub))
-               return false;
+#if defined(ENABLE_JAVASE)
+       if (!link_class(class_java_lang_ClassLoader))
+               vm_abort("linker_init: linking failed");
 
-    /* pseudo class representing the null type */
-    
-       pseudo_class_Null = class_new_intern(utf_new_char("$NULL$"));
-       pseudo_class_Null->loaded = true;
-    pseudo_class_Null->super.cls = class_java_lang_Object;
+       if (!link_class(class_java_lang_SecurityManager))
+               vm_abort("linker_init: linking failed");
+#endif
 
-       if (!link_class(pseudo_class_Null))
-               return false;
+       if (!link_class(class_java_lang_System))
+               vm_abort("linker_init: linking failed");
 
-    /* pseudo class representing new uninitialized objects */
-    
-       pseudo_class_New = class_new_intern(utf_new_char("$NEW$"));
-       pseudo_class_New->loaded = true;
-       pseudo_class_New->linked = true; /* XXX is this allright? */
-       pseudo_class_New->super.cls = class_java_lang_Object;
+       if (!link_class(class_java_lang_Thread))
+               vm_abort("linker_init: linking failed");
 
+#if defined(ENABLE_JAVASE)
+       if (!link_class(class_java_lang_ThreadGroup))
+               vm_abort("linker_init: linking failed");
+#endif
 
-       /* create classes representing primitive types */
+       if (!link_class(class_java_lang_Throwable))
+               vm_abort("linker_init: linking failed");
 
-       if (!link_primitivetype_table())
-               return false;
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+       if (!link_class(class_java_lang_VMSystem))
+               vm_abort("linker_init: linking failed");
 
+       if (!link_class(class_java_lang_VMThread))
+               vm_abort("linker_init: linking failed");
 
-       /* Correct vftbl-entries (retarded loading and linking of class           */
-       /* java/lang/String).                                                     */
+       if (!link_class(class_java_lang_VMThrowable))
+               vm_abort("linker_init: linking failed");
+#endif
 
-       stringtable_update();
+       /* Important system exceptions. */
 
-       return true;
-}
+       if (!link_class(class_java_lang_Exception))
+               vm_abort("linker_init: linking failed");
 
+       if (!link_class(class_java_lang_ClassNotFoundException))
+               vm_abort("linker_init: linking failed");
 
-/* link_primitivetype_table ****************************************************
+       if (!link_class(class_java_lang_RuntimeException))
+               vm_abort("linker_init: linking failed");
 
-   Create classes representing primitive types.
+       /* some classes which may be used more often */
 
-*******************************************************************************/
+#if defined(ENABLE_JAVASE)
+       if (!link_class(class_java_lang_StackTraceElement))
+               vm_abort("linker_init: linking failed");
 
-static bool link_primitivetype_table(void)
-{  
-       classinfo *c;
-       s4 i;
+       if (!link_class(class_java_lang_reflect_Constructor))
+               vm_abort("linker_init: linking failed");
 
-       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;
-               
-               /* prevent loader from loading primitive class */
-               c->loaded = true;
-               if (!link_class(c))
-                       return false;
+       if (!link_class(class_java_lang_reflect_Field))
+               vm_abort("linker_init: linking failed");
 
-               primitivetype_table[i].class_primitive = c;
+       if (!link_class(class_java_lang_reflect_Method))
+               vm_abort("linker_init: linking failed");
 
-               /* create class for wrapping the primitive type */
-               if (!load_class_bootstrap(utf_new_char(primitivetype_table[i].wrapname),&c))
-                       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));
-                       primitivetype_table[i].arrayclass = c;
-                       c->loaded = true;
-                       if (!c->linked)
-                               if (!link_class(c))
-                                       return false;
-                       primitivetype_table[i].arrayvftbl = c->vftbl;
-               }
-       }
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+       if (!link_class(class_java_lang_reflect_VMConstructor))
+               vm_abort("linker_init: linking failed");
 
-       return true;
+       if (!link_class(class_java_lang_reflect_VMField))
+               vm_abort("linker_init: linking failed");
+
+       if (!link_class(class_java_lang_reflect_VMMethod))
+               vm_abort("linker_init: linking failed");
+# endif
+
+       if (!link_class(class_java_security_PrivilegedAction))
+               vm_abort("linker_init: linking failed");
+
+       if (!link_class(class_java_util_Vector))
+               vm_abort("linker_init: linking failed");
+
+       if (!link_class(class_java_util_HashMap))
+               vm_abort("linker_init: linking failed");
+
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+       if (!link_class(class_sun_misc_Signal))
+               vm_abort("linker_init: linking failed");
+
+       if (!link_class(class_sun_reflect_MagicAccessorImpl))
+               vm_abort("linker_init: linking failed");
+# endif
+
+       if (!link_class(arrayclass_java_lang_Object))
+               vm_abort("linker_init: linking failed");
+#endif
+
+
+       /* create pseudo classes used by the typechecker */
+
+    /* pseudo class for Arraystubs (extends java.lang.Object) */
+
+       pseudo_class_Arraystub                   =
+               class_create_classinfo(utf_new_char("$ARRAYSTUB$"));
+       pseudo_class_Arraystub->state           |= CLASS_LOADED;
+       pseudo_class_Arraystub->super            = class_java_lang_Object;
+
+#if defined(ENABLE_JAVASE)
+
+       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;
+
+#elif defined(ENABLE_JAVAME_CLDC1_1)
+
+       pseudo_class_Arraystub->interfacescount    = 0;
+       pseudo_class_Arraystub->interfaces         = NULL;
+
+#else
+# error unknown Java configuration
+#endif
+
+       if (!classcache_store_unique(pseudo_class_Arraystub))
+               vm_abort("linker_init: could not cache pseudo_class_Arraystub");
+
+       if (!link_class(pseudo_class_Arraystub))
+               vm_abort("linker_init: linking pseudo_class_Arraystub failed");
+
+       /* 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  = class_java_lang_Object;
+
+       if (!classcache_store_unique(pseudo_class_Null))
+               vm_abort("linker_init: could not cache pseudo_class_Null");
+
+       if (!link_class(pseudo_class_Null))
+               vm_abort("linker_init: linking failed");
+
+       /* pseudo class representing new uninitialized objects */
+    
+       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  = class_java_lang_Object;
+
+       if (!classcache_store_unique(pseudo_class_New))
+               vm_abort("linker_init: could not cache pseudo_class_New");
+
+       /* Correct vftbl-entries (retarded loading and linking of class
+          java/lang/String). */
+
+       stringtable_update();
 }
 
 
@@ -259,59 +355,159 @@ static bool link_primitivetype_table(void)
 classinfo *link_class(classinfo *c)
 {
        classinfo *r;
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_end;
+#endif
 
-#if defined(USE_THREADS)
-       /* enter a monitor on the class */
+       RT_TIMING_GET_TIME(time_start);
 
-       builtin_monitorenter((java_objectheader *) c);
-#endif
+       if (c == NULL) {
+               exceptions_throw_nullpointerexception();
+               return NULL;
+       }
 
-       /* maybe the class is already linked */
-       if (c->linked) {
-#if defined(USE_THREADS)
-               builtin_monitorexit((java_objectheader *) c);
-#endif
+       LOCK_MONITOR_ENTER(c);
+
+       /* Maybe the class is currently linking or is already linked.*/
+
+       if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
+               LOCK_MONITOR_EXIT(c);
 
                return c;
        }
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        /* measure time */
 
-       if (getcompilingtime)
+       if (opt_getcompilingtime)
                compilingtime_stop();
 
-       if (getloadingtime)
+       if (opt_getloadingtime)
                loadingtime_start();
 #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;
+       /* If return value is NULL, we had a problem and the class is not
+          linked. */
 
-#if defined(STATISTICS)
+       if (r == NULL)
+               c->state &= ~CLASS_LINKING;
+
+#if defined(ENABLE_STATISTICS)
        /* measure time */
 
-       if (getloadingtime)
+       if (opt_getloadingtime)
                loadingtime_stop();
 
-       if (getcompilingtime)
+       if (opt_getcompilingtime)
                compilingtime_start();
 #endif
 
-#if defined(USE_THREADS)
-       /* leave the monitor */
+       LOCK_MONITOR_EXIT(c);
 
-       builtin_monitorexit((java_objectheader *) c);
-#endif
+       RT_TIMING_GET_TIME(time_end);
+
+       RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
 
        return r;
 }
 
 
+/* linker_overwrite_method *****************************************************
+
+   Overwrite a method with another one, update method flags and check
+   assumptions.
+
+   IN:
+      mg................the general method being overwritten
+         ms................the overwriting (more specialized) method
+         wl................worklist where to add invalidated methods
+
+   RETURN VALUE:
+      true..............everything ok
+         false.............an exception has been thrown
+
+*******************************************************************************/
+
+static bool linker_overwrite_method(methodinfo *mg,
+                                                                       methodinfo *ms,
+                                                                       method_worklist **wl)
+{
+       classinfo *cg;
+       classinfo *cs;
+
+       cg = mg->clazz;
+       cs = ms->clazz;
+
+       /* overriding a final method is illegal */
+
+       if (mg->flags & ACC_FINAL) {
+               exceptions_throw_verifyerror(mg, "Overriding final method");
+               return false;
+       }
+
+       /* method ms overwrites method mg */
+
+#if defined(ENABLE_VERIFIER)
+       /* Add loading constraints (for the more general types of method mg). */
+       /* Not for <init>, as it is not invoked virtually.                    */
+
+       if ((ms->name != utf_init)
+                       && !classcache_add_constraints_for_params(
+                               cs->classloader, cg->classloader, mg))
+       {
+               return false;
+       }
+#endif
+
+       /* inherit the vftbl index, and record the overwriting */
+
+       ms->vftblindex = mg->vftblindex;
+       ms->overwrites = mg;
+
+       /* update flags and check assumptions */
+       /* <init> methods are a special case, as they are never dispatched dynamically */
+
+       if ((ms->flags & ACC_METHOD_IMPLEMENTED) && ms->name != utf_init) {
+               do {
+
+#if defined(ENABLE_TLH)
+                       if (mg->flags & ACC_METHOD_MONOMORPHY_USED) {
+                               printf("%s/%s is evil! the siner is %s/%s\n", mg->clazz->name->text, mg->name->text,
+                                       ms->clazz->name->text, ms->name->text);
+                               ms->flags |= ACC_METHOD_PARENT_MONOMORPHY_USED;                                 
+                       }
+#endif
+
+                       if (mg->flags & ACC_METHOD_IMPLEMENTED) {
+                               /* this adds another implementation */
+
+                               mg->flags &= ~ACC_METHOD_MONOMORPHIC;
+
+                               INLINELOG( printf("becomes polymorphic: "); method_println(mg); );
+
+                               method_break_assumption_monomorphic(mg, wl);
+                       }
+                       else {
+                               /* this is the first implementation */
+
+                               mg->flags |= ACC_METHOD_IMPLEMENTED;
+
+                               INLINELOG( printf("becomes implemented: "); method_println(mg); );
+                       }
+
+                       ms = mg;
+                       mg = mg->overwrites;
+               } while (mg != NULL);
+       }
+
+       return true;
+}
+
+
 /* link_class_intern ***********************************************************
 
    Tries to link a class. The function calculates the length in bytes
@@ -320,6 +516,70 @@ classinfo *link_class(classinfo *c)
        
 *******************************************************************************/
 
+static int build_display_inner(classinfo *topc, classinfo *c, int i)
+{
+       int depth;
+       if (!c)
+               return 0;
+       do {
+               if (c->vftbl->arraydesc)
+               {
+                       arraydescriptor *a = c->vftbl->arraydesc;
+                       if (a->elementvftbl && a->elementvftbl->clazz->super)
+                       {
+                               classinfo *cls = a->elementvftbl->clazz->super;
+                               int n;
+                               for (n=0; n<a->dimension; n++)
+                                       cls = class_array_of(cls, true);
+                               depth = build_display_inner(topc, cls, i+1);
+                               break;
+                       }
+                       if (a->componentvftbl && a->elementvftbl)
+                       {
+                               depth = build_display_inner(topc, a->componentvftbl->clazz, i+1);
+                               break;
+                       }
+               }
+               depth = build_display_inner(topc, c->super, i+1);
+       } while (false);
+       if (depth >= DISPLAY_SIZE)
+       {
+               if (depth == DISPLAY_SIZE)
+               {
+                       topc->vftbl->subtype_overflow_length = i+1;
+                       topc->vftbl->subtype_overflow = malloc(sizeof(void*) * (i+1));
+#if defined(ENABLE_STATISTICS)
+                       if (opt_stat)
+                               count_vftbl_len += sizeof(void*) * (i+1);
+#endif
+               }
+               topc->vftbl->subtype_overflow[depth - DISPLAY_SIZE] = c->vftbl;
+               return depth + 1;
+       }
+       topc->vftbl->subtype_display[depth] = c->vftbl;
+       return depth + 1;
+}
+
+static void build_display(classinfo *c)
+{
+       int depth;
+       int i;
+
+       depth = build_display_inner(c, c, 0) - 1;
+       c->vftbl->subtype_depth = depth;
+       if (depth >= DISPLAY_SIZE)
+       {
+               c->vftbl->subtype_offset = OFFSET(vftbl_t, subtype_display[DISPLAY_SIZE]);
+       }
+       else
+       {
+               c->vftbl->subtype_offset = OFFSET(vftbl_t, subtype_display[0]) + sizeof(void*) * depth;
+               for (i=depth+1; i<=DISPLAY_SIZE; i++)
+                       c->vftbl->subtype_display[i] = NULL;
+               c->vftbl->subtype_overflow_length = 0;
+       }
+}
+
 static classinfo *link_class_intern(classinfo *c)
 {
        classinfo *super;             /* super class                              */
@@ -328,53 +588,52 @@ 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,j;                       /* interface/method/field counter           */
+       s4 i;                         /* interface/method/field counter           */
        arraydescriptor *arraydesc;   /* descriptor for array classes             */
+       method_worklist *worklist;    /* worklist for recompilation               */
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_resolving, time_compute_vftbl,
+                                       time_abstract, time_compute_iftbl, time_fill_vftbl,
+                                       time_offsets, time_fill_iftbl, time_finalizer,
+                                       time_subclasses;
+#endif
 
-       /* maybe the class is already linked */
-       if (c->linked)
-               return c;
+       RT_TIMING_GET_TIME(time_start);
 
-       if (linkverbose)
-               log_message_class("Linking class: ", c);
+       TRACELINKCLASS(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;
+       /* XXX should this be a specific exception? */
+       assert(c->state & CLASS_LOADED);
 
-       arraydesc = NULL;
+       /* This is check in link_class. */
 
-       /* check interfaces */
+       assert(!(c->state & CLASS_LINKED));
 
-       for (i = 0; i < c->interfacescount; i++) {
-               /* resolve this super interface */
-               if (!resolve_classref_or_classinfo(NULL,c->interfaces[i],resolveEager,false,&tc))
-                       return NULL;
-               c->interfaces[i].cls = tc;
-               
-               /* detect circularity */
+       /* 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);
 
-               if (tc == c) {
-                       *exceptionptr =
-                               new_exception_utfmessage(string_java_lang_ClassCircularityError,
-                                                                                c->name);
-                       return NULL;
-               }
+       /* this class is currently linking */
 
-               assert(tc->loaded);
+       c->state |= CLASS_LINKING;
 
-               if (!(tc->flags & ACC_INTERFACE)) {
-                       *exceptionptr =
-                               new_exception_message(string_java_lang_IncompatibleClassChangeError,
-                                                                         "Implementing class");
-                       return NULL;
-               }
+       arraydesc = NULL;
+       worklist = NULL;
+
+       /* Link the super interfaces. */
+
+       for (i = 0; i < c->interfacescount; i++) {
+               tc = c->interfaces[i];
 
-               if (!tc->linked)
+               if (!(tc->state & CLASS_LINKED))
                        if (!link_class(tc))
                                return NULL;
        }
@@ -382,50 +641,36 @@ static classinfo *link_class_intern(classinfo *c)
        /* check super class */
 
        super = NULL;
-       if (c->super.any == NULL) {          /* class java.lang.Object */
+
+       /* Check for java/lang/Object. */
+
+       if (c->super == NULL) {
                c->index = 0;
-        c->classUsed = USED;     /* Object class is always used CO-RT*/
-               c->impldBy = NULL;
-               c->instancesize = sizeof(java_objectheader);
+               c->instancesize = sizeof(java_object_t);
                
                vftbllength = supervftbllength = 0;
 
                c->finalizer = NULL;
+       }
+       else {
+               /* Get super class. */
 
-       } else {
-               /* resolve super class */
-               if (!resolve_classref_or_classinfo(NULL,c->super,resolveEager,false,&super))
-                       return NULL;
-               c->super.cls = super;
-               
-               /* detect circularity */
-               if (super == c) {
-                       *exceptionptr =
-                               new_exception_utfmessage(string_java_lang_ClassCircularityError,
-                                                                                c->name);
-                       return NULL;
-               }
-
-               assert(super->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");
-               }
+               super = c->super;
 
-               /* Don't allow extending final classes */
-               if (super->flags & ACC_FINAL) {
-                       *exceptionptr =
-                               new_exception_message(string_java_lang_VerifyError,
-                                                                         "Cannot inherit from final class");
-                       return NULL;
-               }
+               /* Link the super class if necessary. */
                
-               if (!super->linked)
+               if (!(super->state & CLASS_LINKED))
                        if (!link_class(super))
                                return NULL;
 
+               /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
+                  flags. */
+
+               c->flags |= (super->flags &
+                                        (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
+
                /* handle array classes */
+
                if (c->name->text[0] == '[')
                        if (!(arraydesc = link_array(c)))
                                return NULL;
@@ -436,11 +681,12 @@ static classinfo *link_class_intern(classinfo *c)
                        c->index = super->index + 1;
                
                c->instancesize = super->instancesize;
-               
+
                vftbllength = supervftbllength = super->vftbl->vftbllength;
                
                c->finalizer = super->finalizer;
        }
+       RT_TIMING_GET_TIME(time_resolving);
 
 
        /* compute vftbl length */
@@ -459,19 +705,23 @@ static classinfo *link_class_intern(classinfo *c)
                                                if (tc->methods[j].flags & ACC_PRIVATE)
                                                        goto notfoundvftblindex;
 
-                                               if (tc->methods[j].flags & ACC_FINAL) {
-                                                       /* class a overrides final method . */
-                                                       *exceptionptr =
-                                                               new_exception(string_java_lang_VerifyError);
-                                                       return NULL;
+                                               /* 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;
                                                }
 
-                                               m->vftblindex = tc->methods[j].vftblindex;
+                                               if (!linker_overwrite_method(&(tc->methods[j]), m, &worklist))
+                                                       return NULL;
+
                                                goto foundvftblindex;
                                        }
                                }
 
-                               tc = tc->super.cls;
+                               tc = tc->super;
                        }
 
                notfoundvftblindex:
@@ -479,10 +729,15 @@ static classinfo *link_class_intern(classinfo *c)
                foundvftblindex:
                        ;
                }
-       }       
+       }
+       RT_TIMING_GET_TIME(time_compute_vftbl);
 
 
-       /* check interfaces of ABSTRACT class for unimplemented methods */
+       /* Check all interfaces of an abstract class (maybe be an
+          interface too) for unimplemented methods.  Such methods are
+          called miranda-methods and are marked with the ACC_MIRANDA
+          flag.  VMClass.getDeclaredMethods does not return such
+          methods. */
 
        if (c->flags & ACC_ABSTRACT) {
                classinfo  *ic;
@@ -493,26 +748,24 @@ static classinfo *link_class_intern(classinfo *c)
 
                abstractmethodscount = 0;
 
+               /* check all interfaces of the abstract class */
+
                for (i = 0; i < c->interfacescount; i++) {
-                       ic = c->interfaces[i].cls;
+                       ic = c->interfaces[i];
 
                        for (j = 0; j < ic->methodscount; j++) {
                                im = &(ic->methods[j]);
 
                                /* skip `<clinit>' and `<init>' */
 
-                               if (im->name == utf_clinit || im->name == utf_init)
+                               if ((im->name == utf_clinit) || (im->name == utf_init))
                                        continue;
 
-                               tc = c;
-
-                               while (tc) {
+                               for (tc = c; tc != NULL; tc = tc->super) {
                                        for (k = 0; k < tc->methodscount; k++) {
                                                if (method_canoverwrite(im, &(tc->methods[k])))
                                                        goto noabstractmethod;
                                        }
-
-                                       tc = tc->super.cls;
                                }
 
                                abstractmethodscount++;
@@ -531,34 +784,34 @@ static classinfo *link_class_intern(classinfo *c)
                                                                  c->methodscount + abstractmethodscount);
 
                        for (i = 0; i < c->interfacescount; i++) {
-                               ic = c->interfaces[i].cls;
+                               ic = c->interfaces[i];
 
                                for (j = 0; j < ic->methodscount; j++) {
                                        im = &(ic->methods[j]);
 
                                        /* skip `<clinit>' and `<init>' */
 
-                                       if (im->name == utf_clinit || im->name == utf_init)
+                                       if ((im->name == utf_clinit) || (im->name == utf_init))
                                                continue;
 
-                                       tc = c;
-
-                                       while (tc) {
+                                       for (tc = c; tc != NULL; tc = tc->super) {
                                                for (k = 0; k < tc->methodscount; k++) {
                                                        if (method_canoverwrite(im, &(tc->methods[k])))
                                                                goto noabstractmethod2;
                                                }
-
-                                               tc = tc->super.cls;
                                        }
 
+                                       /* Copy the method found into the new c->methods
+                                          array and tag it as miranda-method. */
+
                                        am = &(c->methods[c->methodscount]);
                                        c->methodscount++;
 
                                        MCOPY(am, im, methodinfo, 1);
 
-                                       am->vftblindex = (vftbllength++);
-                                       am->class = c;
+                                       am->vftblindex  = (vftbllength++);
+                                       am->clazz       = c;
+                                       am->flags      |= ACC_MIRANDA;
 
                                noabstractmethod2:
                                        ;
@@ -566,9 +819,10 @@ static classinfo *link_class_intern(classinfo *c)
                        }
                }
        }
+       RT_TIMING_GET_TIME(time_abstract);
 
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                count_vftbl_len +=
                        sizeof(vftbl_t) + (sizeof(methodptr) * (vftbllength - 1));
@@ -577,15 +831,16 @@ static classinfo *link_class_intern(classinfo *c)
        /* compute interfacetable length */
 
        interfacetablelength = 0;
-       tc = c;
-       while (tc) {
+
+       for (tc = c; tc != NULL; tc = tc->super) {
                for (i = 0; i < tc->interfacescount; i++) {
-                       s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
+                       s4 h = class_highestinterface(tc->interfaces[i]) + 1;
+
                        if (h > interfacetablelength)
                                interfacetablelength = h;
                }
-               tc = tc->super.cls;
        }
+       RT_TIMING_GET_TIME(time_compute_iftbl);
 
        /* allocate virtual function table */
 
@@ -594,11 +849,12 @@ 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;
-       v->class = c;
-       v->vftbllength = vftbllength;
+
+       c->vftbl                = v;
+       v->clazz                = c;
+       v->vftbllength          = vftbllength;
        v->interfacetablelength = interfacetablelength;
-       v->arraydesc = arraydesc;
+       v->arraydesc            = arraydesc;
 
        /* store interface index in vftbl */
 
@@ -609,34 +865,59 @@ static classinfo *link_class_intern(classinfo *c)
 
        for (i = 0; i < supervftbllength; i++) 
                v->table[i] = super->vftbl->table[i];
-       
+
+       /* Fill the remaining vftbl slots with the AbstractMethodError
+          stub (all after the super class slots, because they are already
+          initialized). */
+
+       for (; i < vftbllength; i++) {
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_INTRP)
+               if (opt_intrp)
+                       v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
+               else
+# endif
+                       v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
+#else
+               v->table[i] = (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
+#endif
+       }
+
        /* add method stubs into virtual function table */
 
        for (i = 0; i < c->methodscount; i++) {
                methodinfo *m = &(c->methods[i]);
 
-               /* Methods in ABSTRACT classes from interfaces maybe already have a   */
-               /* stubroutine.                                                       */
+               assert(m->stubroutine == NULL);
 
-               if (!m->stubroutine) {
-                       if (!(m->flags & ACC_NATIVE)) {
-                               m->stubroutine = createcompilerstub(m);
+               /* Don't create a compiler stub for abstract methods as they
+                  throw an AbstractMethodError with the default stub in the
+                  vftbl.  This entry is simply copied by sub-classes. */
+
+               if (m->flags & ACC_ABSTRACT)
+                       continue;
 
-                       } else {
-                               functionptr f = native_findfunction(c->name,
-                                                                               m->name,
-                                                                               m->descriptor,
-                                                                               (m->flags & ACC_STATIC));
-#if defined(STATIC_CLASSPATH)
-                               if (f)
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_INTRP)
+               if (opt_intrp)
+                       m->stubroutine = intrp_createcompilerstub(m);
+               else
+#endif
+                       m->stubroutine = CompilerStub_generate(m);
+#else
+               m->stubroutine = intrp_createcompilerstub(m);
 #endif
-                                       m->stubroutine = createnativestub(f, m);
-                       }
-               }
 
-               if (!(m->flags & ACC_STATIC))
-                       v->table[m->vftblindex] = m->stubroutine;
+               /* static methods are not in the vftbl */
+
+               if (m->flags & ACC_STATIC)
+                       continue;
+
+               /* insert the stubroutine into the vftbl */
+
+               v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
        }
+       RT_TIMING_GET_TIME(time_fill_vftbl);
 
        /* compute instance size and offset of each field */
        
@@ -645,18 +926,19 @@ static classinfo *link_class_intern(classinfo *c)
                fieldinfo *f = &(c->fields[i]);
                
                if (!(f->flags & ACC_STATIC)) {
-                       dsize = desc_typesize(f->descriptor);
-                       c->instancesize = ALIGN(c->instancesize, dsize);
+                       dsize = descriptor_typesize(f->parseddesc);
+                       c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
                        f->offset = c->instancesize;
                        c->instancesize += dsize;
                }
        }
+       RT_TIMING_GET_TIME(time_offsets);
 
        /* initialize interfacetable and interfacevftbllength */
-       
+
        v->interfacevftbllength = MNEW(s4, interfacetablelength);
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                count_vftbl_len += (4 + sizeof(s4)) * v->interfacetablelength;
 #endif
@@ -665,12 +947,15 @@ static classinfo *link_class_intern(classinfo *c)
                v->interfacevftbllength[i] = 0;
                v->interfacetable[-i] = NULL;
        }
-       
+
        /* add interfaces */
-       
-       for (tc = c; tc != NULL; tc = tc->super.cls)
+
+       for (tc = c; tc != NULL; tc = tc->super)
                for (i = 0; i < tc->interfacescount; i++)
-                       linker_addinterface(c, tc->interfaces[i].cls);
+                       if (!linker_addinterface(c, tc->interfaces[i]))
+                               return NULL;
+
+       RT_TIMING_GET_TIME(time_fill_iftbl);
 
        /* add finalizer method (not for java.lang.Object) */
 
@@ -683,30 +968,46 @@ static classinfo *link_class_intern(classinfo *c)
                        if (!(fi->flags & ACC_STATIC))
                                c->finalizer = fi;
        }
+       RT_TIMING_GET_TIME(time_finalizer);
 
-       /* 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,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)))
-                               return NULL;
-       }
-       
        /* final tasks */
 
        linker_compute_subclasses(c);
 
-       if (linkverbose)
-               log_message_class("Linking done class: ", c);
+       /* FIXME: this is completely useless now */
+       RT_TIMING_GET_TIME(time_subclasses);
+
+       build_display(c);
+
+       /* revert the linking state and class is linked */
+
+       c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
+
+       /* check worklist */
+
+       /* XXX must this also be done in case of exception? */
+
+       while (worklist != NULL) {
+               method_worklist *wi = worklist;
+
+               worklist = worklist->next;
+
+               INLINELOG( printf("MUST BE RECOMPILED: "); method_println(wi->m); );
+               jit_invalidate_code(wi->m);
+
+               /* XXX put worklist into dump memory? */
+               FREE(wi, method_worklist);
+       }
+
+       RT_TIMING_TIME_DIFF(time_start        ,time_resolving    ,RT_TIMING_LINK_RESOLVE);
+       RT_TIMING_TIME_DIFF(time_resolving    ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
+       RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract     ,RT_TIMING_LINK_ABSTRACT);
+       RT_TIMING_TIME_DIFF(time_abstract     ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
+       RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl   ,RT_TIMING_LINK_F_VFTBL);
+       RT_TIMING_TIME_DIFF(time_fill_vftbl   ,time_offsets      ,RT_TIMING_LINK_OFFSETS);
+       RT_TIMING_TIME_DIFF(time_offsets      ,time_fill_iftbl   ,RT_TIMING_LINK_F_IFTBL);
+       RT_TIMING_TIME_DIFF(time_fill_iftbl   ,time_finalizer    ,RT_TIMING_LINK_FINALIZER);
+       RT_TIMING_TIME_DIFF(time_finalizer    ,time_subclasses   ,RT_TIMING_LINK_SUBCLASS);
 
        /* just return c to show that we didn't had a problem */
 
@@ -726,54 +1027,68 @@ 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) {
-               assert(comp->loaded);
 
+       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) {
                /* c is an array of references */
                desc->arraytype = ARRAYTYPE_OBJECT;
                desc->componentsize = sizeof(void*);
-               desc->dataoffset = OFFSET(java_objectarray, data);
+               desc->dataoffset = OFFSET(java_objectarray_t, 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;
 
@@ -788,54 +1103,55 @@ static arraydescriptor *link_array(classinfo *c)
                switch (c->name->text[1]) {
                case 'Z':
                        desc->arraytype = ARRAYTYPE_BOOLEAN;
-                       desc->dataoffset = OFFSET(java_booleanarray,data);
+                       desc->dataoffset = OFFSET(java_booleanarray_t,data);
                        desc->componentsize = sizeof(u1);
                        break;
 
                case 'B':
                        desc->arraytype = ARRAYTYPE_BYTE;
-                       desc->dataoffset = OFFSET(java_bytearray,data);
+                       desc->dataoffset = OFFSET(java_bytearray_t,data);
                        desc->componentsize = sizeof(u1);
                        break;
 
                case 'C':
                        desc->arraytype = ARRAYTYPE_CHAR;
-                       desc->dataoffset = OFFSET(java_chararray,data);
+                       desc->dataoffset = OFFSET(java_chararray_t,data);
                        desc->componentsize = sizeof(u2);
                        break;
 
                case 'D':
                        desc->arraytype = ARRAYTYPE_DOUBLE;
-                       desc->dataoffset = OFFSET(java_doublearray,data);
+                       desc->dataoffset = OFFSET(java_doublearray_t,data);
                        desc->componentsize = sizeof(double);
                        break;
 
                case 'F':
                        desc->arraytype = ARRAYTYPE_FLOAT;
-                       desc->dataoffset = OFFSET(java_floatarray,data);
+                       desc->dataoffset = OFFSET(java_floatarray_t,data);
                        desc->componentsize = sizeof(float);
                        break;
 
                case 'I':
                        desc->arraytype = ARRAYTYPE_INT;
-                       desc->dataoffset = OFFSET(java_intarray,data);
+                       desc->dataoffset = OFFSET(java_intarray_t,data);
                        desc->componentsize = sizeof(s4);
                        break;
 
                case 'J':
                        desc->arraytype = ARRAYTYPE_LONG;
-                       desc->dataoffset = OFFSET(java_longarray,data);
+                       desc->dataoffset = OFFSET(java_longarray_t,data);
                        desc->componentsize = sizeof(s8);
                        break;
 
                case 'S':
                        desc->arraytype = ARRAYTYPE_SHORT;
-                       desc->dataoffset = OFFSET(java_shortarray,data);
+                       desc->dataoffset = OFFSET(java_shortarray_t,data);
                        desc->componentsize = sizeof(s2);
                        break;
 
                default:
-                       panic("Invalid array class name");
+                       exceptions_throw_noclassdeffounderror(c->name);
+                       return NULL;
                }
                
                desc->componentvftbl = NULL;
@@ -852,46 +1168,30 @@ static arraydescriptor *link_array(classinfo *c)
 
    XXX
 
+   ATTENTION: DO NOT REMOVE ANY OF THE LOCKING MECHANISMS BELOW:
+   This function needs to take the class renumber lock and stop the
+   world during class renumbering. The lock is used in C code which
+   is not that performance critical. Whereas JIT code uses critical
+   sections to atomically access the class values.
+
 *******************************************************************************/
 
 static void linker_compute_subclasses(classinfo *c)
 {
-#if defined(USE_THREADS)
-#if defined(NATIVE_THREADS)
-       compiler_lock();
-#else
-       intsDisable();
-#endif
-#endif
 
        if (!(c->flags & ACC_INTERFACE)) {
-               c->nextsub = 0;
-               c->sub = 0;
+               c->nextsub = NULL;
+               c->sub     = NULL;
+               c->vftbl->baseval = 1; /* so it does not look like an interface */
        }
 
-       if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
-               c->nextsub = c->super.cls->sub;
-               c->super.cls->sub = c;
+       if (!(c->flags & ACC_INTERFACE) && (c->super != NULL)) {
+               c->nextsub    = c->super->sub;
+               c->super->sub = c;
        }
 
        classvalue = 0;
 
-       /* this is the java.lang.Object special case */
-
-       if (!class_java_lang_Object) {
-               linker_compute_class_values(c);
-
-       } else {
-               linker_compute_class_values(class_java_lang_Object);
-       }
-
-#if defined(USE_THREADS)
-#if defined(NATIVE_THREADS)
-       compiler_unlock();
-#else
-       intsRestore();
-#endif
-#endif
 }
 
 
@@ -924,56 +1224,110 @@ static void linker_compute_class_values(classinfo *c)
    Is needed by link_class for adding a VTBL to a class. All
    interfaces implemented by ic are added as well.
 
+   RETURN VALUE:
+      true.........everything ok
+         false........an exception has been thrown
+
 *******************************************************************************/
 
-static void linker_addinterface(classinfo *c, classinfo *ic)
+static bool linker_addinterface(classinfo *c, classinfo *ic)
 {
-       s4     j, m;
-       s4     i   = ic->index;
-       vftbl_t *v = c->vftbl;
+       s4          j, k;
+       vftbl_t    *v;
+       s4          i;
+       classinfo  *sc;
+       methodinfo *m;
+
+       v = c->vftbl;
+       i = ic->index;
 
        if (i >= v->interfacetablelength)
-               panic ("Inernal error: interfacetable overflow");
+               vm_abort("Internal error: interfacetable overflow");
+
+       /* if this interface has already been added, return immediately */
 
-       if (v->interfacetable[-i])
-               return;
+       if (v->interfacetable[-i] != NULL)
+               return true;
 
        if (ic->methodscount == 0) {  /* fake entry needed for subtype test */
                v->interfacevftbllength[i] = 1;
-               v->interfacetable[-i] = MNEW(methodptr, 1);
-               v->interfacetable[-i][0] = NULL;
-
-       else {
+               v->interfacetable[-i]      = MNEW(methodptr, 1);
+               v->interfacetable[-i][0]   = NULL;
+       }
+       else {
                v->interfacevftbllength[i] = ic->methodscount;
-               v->interfacetable[-i] = MNEW(methodptr, 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));
 #endif
 
                for (j = 0; j < ic->methodscount; j++) {
-                       classinfo *sc = c;
+                       for (sc = c; sc != NULL; sc = sc->super) {
+                               for (k = 0; k < sc->methodscount; k++) {
+                                       m = &(sc->methods[k]);
+
+                                       if (method_canoverwrite(m, &(ic->methods[j]))) {
+                                               /* method m overwrites the (abstract) method */
+#if defined(ENABLE_VERIFIER)
+                                               /* Add loading constraints (for the more
+                                                  general types of the method
+                                                  ic->methods[j]).  */
+                                               if (!classcache_add_constraints_for_params(
+                                                                       c->classloader, ic->classloader,
+                                                                       &(ic->methods[j])))
+                                               {
+                                                       return false;
+                                               }
+#endif
+
+                                               /* XXX taken from gcj */
+                                               /* check for ACC_STATIC: IncompatibleClassChangeError */
 
-                       while (sc) {
-                               for (m = 0; m < sc->methodscount; m++) {
-                                       methodinfo *mi = &(sc->methods[m]);
+                                               /* check for !ACC_PUBLIC: IllegalAccessError */
 
-                                       if (method_canoverwrite(mi, &(ic->methods[j]))) {
-                                               v->interfacetable[-i][j] = v->table[mi->vftblindex];
+                                               /* check for ACC_ABSTRACT: AbstracMethodError,
+                                                  not sure about that one */
+
+                                               v->interfacetable[-i][j] = v->table[m->vftblindex];
                                                goto foundmethod;
                                        }
                                }
-                               sc = sc->super.cls;
                        }
+
+                       /* If no method was found, insert the AbstractMethodError
+                          stub. */
+
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_INTRP)
+                       if (opt_intrp)
+                               v->interfacetable[-i][j] =
+                                       (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
+                       else
+# endif
+                               v->interfacetable[-i][j] =
+                                       (methodptr) (ptrint) &asm_abstractmethoderror;
+#else
+                       v->interfacetable[-i][j] =
+                               (methodptr) (ptrint) &intrp_asm_abstractmethoderror;
+#endif
+
                foundmethod:
                        ;
                }
        }
 
-       for (j = 0; j < ic->interfacescount; j++) 
-               linker_addinterface(c, ic->interfaces[j].cls);
+       /* add superinterfaces of this interface */
+
+       for (j = 0; j < ic->interfacescount; j++)
+               if (!linker_addinterface(c, ic->interfaces[j]))
+                       return false;
+
+       /* everything ok */
+
+       return true;
 }
 
 
@@ -995,7 +1349,7 @@ static s4 class_highestinterface(classinfo *c)
     h = c->index;
 
        for (i = 0; i < c->interfacescount; i++) {
-               h2 = class_highestinterface(c->interfaces[i].cls);
+               h2 = class_highestinterface(c->interfaces[i]);
 
                if (h2 > h)
                        h = h2;
@@ -1005,43 +1359,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
@@ -1053,4 +1370,5 @@ void print_arraydescriptor(FILE *file, arraydescriptor *desc)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */