Merged with tip.
[cacao.git] / src / vmcore / linker.c
index 4bd8aacf82faa09ea6bc8d30e7d9df61ad933862..37e8daeb7d362077aa0b5ea88a392d0b2dea373e 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vmcore/linker.c - class linker functions
 
-   Copyright (C) 1996-2005, 2006, 2007 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.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: linker.c 7561 2007-03-23 19:10:35Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
 
 #include "vm/types.h"
 
 #include "mm/memory.h"
+
 #include "native/native.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/lock.h"
-#else
-# include "threads/none/lock.h"
-#endif
+#include "threads/lock-common.h"
 
 #include "toolbox/logging.h"
 
 #include "vm/access.h"
+#include "vm/array.h"
 #include "vm/exceptions.h"
+#include "vm/primitive.hpp"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
 
 #include "vmcore/options.h"
 #include "vmcore/rt-timing.h"
 
+
+/* 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
+
+
 /* #include "vm/resolve.h" */
 /* copied prototype to avoid bootstrapping problem: */
 classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool checkaccess);
@@ -66,7 +81,7 @@ classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool c
 #endif
 
 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
-#define INLINELOG(code)  do { if (opt_inline_debug_log) { code } } while (0)
+#define INLINELOG(code)  do { if (opt_TraceInlining) { code } } while (0)
 #else
 #define INLINELOG(code)
 #endif
@@ -77,41 +92,11 @@ classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool c
 static s4 interfaceindex;       /* sequential numbering of interfaces         */
 static s4 classvalue;
 
-
-/* primitivetype_table *********************************************************
-
-   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 (except ARRAYTYPE_OBJECT).
-
-*******************************************************************************/
-
-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 },
-#if defined(ENABLE_JAVASE)
-       { NULL, NULL, "java/lang/Void",      'V', "void"    , NULL, NULL, NULL }
-#else
-       { NULL, NULL, NULL,                   0 , NULL      , NULL, NULL, NULL },
-#endif
-};
+java_object_t *linker_classrenumber_lock;
 
 
 /* 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);
@@ -120,133 +105,233 @@ static bool linker_addinterface(classinfo *c, classinfo *ic);
 static s4 class_highestinterface(classinfo *c);
 
 
+/* dummy structures for alinment checks ***************************************/
+
+typedef struct dummy_alignment_long_t   dummy_alignment_long_t;
+typedef struct dummy_alignment_double_t dummy_alignment_double_t;
+
+struct dummy_alignment_long_t {
+       int32_t i;
+       int64_t l;
+};
+
+struct dummy_alignment_double_t {
+       int32_t i;
+       double  d;
+};
+
+
 /* 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");
 
-       interfaceindex = 0;
+       /* Check for if alignment for long and double matches what we
+          assume for the current architecture. */
 
-       /* 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 defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__)) || defined(__M68K__)
+       /* Define a define here which is later checked when we use this
+          offset. */
 
-       if (!link_class(class_java_lang_Class))
-               return false;
+# define LINKER_ALIGNMENT_LONG_DOUBLE 4
 
-       /* now set the header.vftbl of all classes which were created
-       before java.lang.Class was linked */
+       if (OFFSET(dummy_alignment_long_t, l) != 4)
+               vm_abort("linker_preinit: long alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_long_t, l), 4);
 
-       class_postset_header_vftbl();
+       if (OFFSET(dummy_alignment_double_t, d) != 4)
+               vm_abort("linker_preinit: double alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_double_t, d), 4);
+#else
 
+# define LINKER_ALIGNMENT_LONG_DOUBLE 8
 
-       /* link important system classes */
+       if (OFFSET(dummy_alignment_long_t, l) != 8)
+               vm_abort("linker_preinit: long alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_long_t, l), 8);
 
-       if (!link_class(class_java_lang_Object))
-               return false;
+       if (OFFSET(dummy_alignment_double_t, d) != 8)
+               vm_abort("linker_preinit: double alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_double_t, d), 8);
+#endif
 
-       if (!link_class(class_java_lang_String))
-               return false;
+       /* Reset interface index. */
+
+       interfaceindex = 0;
+
+#if defined(ENABLE_THREADS)
+       /* create the global lock object */
+
+       linker_classrenumber_lock = NEW(java_object_t);
+
+       LOCK_INIT_OBJECT_LOCK(linker_classrenumber_lock);
+#endif
+
+       /* Link the most basic classes. */
+
+       if (!link_class(class_java_lang_Object))
+               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
+}
 
 
-       /* link classes for wrapping primitive types */
+/* 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 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. */
 
-       /* load some other important classes */
+       if (!link_class(class_java_lang_String))
+               vm_abort("linker_init: linking java/lang/String failed");
 
 #if defined(ENABLE_JAVASE)
        if (!link_class(class_java_lang_ClassLoader))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_SecurityManager))
-               return false;
+               vm_abort("linker_init: linking failed");
 #endif
 
        if (!link_class(class_java_lang_System))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_Thread))
-               return false;
+               vm_abort("linker_init: linking failed");
 
 #if defined(ENABLE_JAVASE)
        if (!link_class(class_java_lang_ThreadGroup))
-               return false;
+               vm_abort("linker_init: linking failed");
 #endif
 
-#if defined(WITH_CLASSPATH_GNU)
+       if (!link_class(class_java_lang_Throwable))
+               vm_abort("linker_init: linking failed");
+
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
        if (!link_class(class_java_lang_VMSystem))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_VMThread))
-               return false;
+               vm_abort("linker_init: linking failed");
+
+       if (!link_class(class_java_lang_VMThrowable))
+               vm_abort("linker_init: linking failed");
 #endif
 
+       /* Important system exceptions. */
+
+       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");
+
+       if (!link_class(class_java_lang_RuntimeException))
+               vm_abort("linker_init: linking failed");
 
        /* some classes which may be used more often */
 
 #if defined(ENABLE_JAVASE)
        if (!link_class(class_java_lang_StackTraceElement))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_reflect_Constructor))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_reflect_Field))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_lang_reflect_Method))
-               return false;
+               vm_abort("linker_init: linking failed");
+
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+       if (!link_class(class_java_lang_reflect_VMConstructor))
+               vm_abort("linker_init: linking failed");
+
+       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))
-               return false;
+               vm_abort("linker_init: linking failed");
 
        if (!link_class(class_java_util_Vector))
-               return false;
+               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_reflect_MagicAccessorImpl))
+               vm_abort("linker_init: linking failed");
+# endif
 
        if (!link_class(arrayclass_java_lang_Object))
-               return false;
+               vm_abort("linker_init: linking failed");
 #endif
 
 
@@ -254,140 +339,59 @@ bool linker_init(void)
 
     /* pseudo class for Arraystubs (extends java.lang.Object) */
 
-       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->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(classref_or_classinfo, 2);
-       pseudo_class_Arraystub->interfaces[0].cls = class_java_lang_Cloneable;
-       pseudo_class_Arraystub->interfaces[1].cls = class_java_io_Serializable;
+
+       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;
+
+       pseudo_class_Arraystub->interfacescount    = 0;
+       pseudo_class_Arraystub->interfaces         = NULL;
+
+#else
+# error unknown Java configuration
 #endif
 
-       if (!classcache_store_unique(pseudo_class_Arraystub)) {
-               log_text("could not cache pseudo_class_Arraystub");
-               assert(0);
-       }
+       if (!classcache_store_unique(pseudo_class_Arraystub))
+               vm_abort("linker_init: could not cache pseudo_class_Arraystub");
 
        if (!link_class(pseudo_class_Arraystub))
-               return false;
+               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         = class_create_classinfo(utf_new_char("$NULL$"));
        pseudo_class_Null->state |= CLASS_LOADED;
-       pseudo_class_Null->super.cls = class_java_lang_Object;
+       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))
-               return false;
+               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         = 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;
+       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");
 
-       /* create classes representing primitive types */
-
-       if (!link_primitivetype_table())
-               return false;
-
-
        /* Correct vftbl-entries (retarded loading and linking of class
           java/lang/String). */
 
        stringtable_update();
-
-       return true;
-}
-
-
-/* link_primitivetype_table ****************************************************
-
-   Create classes representing primitive types.
-
-*******************************************************************************/
-
-static bool link_primitivetype_table(void)
-{  
-       classinfo *c;
-       utf       *u;
-       s4         i;
-
-       for (i = 0; i < PRIMITIVETYPE_COUNT; i++) {
-               /* skip dummies */
-
-               if (!primitivetype_table[i].name)
-                       continue;
-               
-               /* create primitive class */
-
-               c = class_create_classinfo(utf_new_char(primitivetype_table[i].name));
-
-               /* primitive classes don't have a super class */
-
-               c->super.any = NULL;
-
-               /* set flags and mark it as primitive class */
-
-               c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT | ACC_CLASS_PRIMITIVE;
-               
-               /* prevent loader from loading primitive class */
-
-               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 */
-
-               u = utf_new_char(primitivetype_table[i].wrapname);
-               c = load_class_bootstrap(u);
-
-               if (c == NULL)
-                       return false;
-
-               primitivetype_table[i].class_wrap = c;
-
-               /* create the primitive array class */
-
-               if (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;
-
-                       assert(c->state & CLASS_LOADED);
-
-                       if (!(c->state & CLASS_LINKED))
-                               if (!link_class(c))
-                                       return false;
-
-                       primitivetype_table[i].arrayvftbl = c->vftbl;
-               }
-       }
-
-       return true;
 }
 
 
@@ -414,9 +418,9 @@ classinfo *link_class(classinfo *c)
 
        LOCK_MONITOR_ENTER(c);
 
-       /* maybe the class is already linked */
+       /* Maybe the class is currently linking or is already linked.*/
 
-       if (c->state & CLASS_LINKED) {
+       if ((c->state & CLASS_LINKING) || (c->state & CLASS_LINKED)) {
                LOCK_MONITOR_EXIT(c);
 
                return c;
@@ -436,9 +440,10 @@ classinfo *link_class(classinfo *c)
 
        r = link_class_intern(c);
 
-       /* if return value is NULL, we had a problem and the class is not linked */
+       /* If return value is NULL, we had a problem and the class is not
+          linked. */
 
-       if (!r)
+       if (r == NULL)
                c->state &= ~CLASS_LINKING;
 
 #if defined(ENABLE_STATISTICS)
@@ -484,8 +489,8 @@ static bool linker_overwrite_method(methodinfo *mg,
        classinfo *cg;
        classinfo *cs;
 
-       cg = mg->class;
-       cs = ms->class;
+       cg = mg->clazz;
+       cs = ms->clazz;
 
        /* overriding a final method is illegal */
 
@@ -518,6 +523,15 @@ static bool linker_overwrite_method(methodinfo *mg,
 
        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 */
 
@@ -572,21 +586,17 @@ static classinfo *link_class_intern(classinfo *c)
 
        RT_TIMING_GET_TIME(time_start);
 
-       /* the class is already linked */
-
-       if (c->state & CLASS_LINKED)
-               return c;
-
-#if !defined(NDEBUG)
-       if (linkverbose)
-               log_message_class("Linking class: ", c);
-#endif
+       TRACELINKCLASS(c);
 
        /* the class must be loaded */
 
        /* XXX should this be a specific exception? */
        assert(c->state & CLASS_LOADED);
 
+       /* This is check in link_class. */
+
+       assert(!(c->state & CLASS_LINKED));
+
        /* 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 */
@@ -604,30 +614,10 @@ static classinfo *link_class_intern(classinfo *c)
        arraydesc = NULL;
        worklist = NULL;
 
-       /* check interfaces */
+       /* Link the super interfaces. */
 
        for (i = 0; i < c->interfacescount; i++) {
-               /* resolve this super interface */
-
-               if ((tc = resolve_classref_or_classinfo_eager(c->interfaces[i], true)) == NULL)
-                       return NULL;
-
-               c->interfaces[i].cls = tc;
-               
-               /* detect circularity */
-
-               if (tc == c) {
-                       exceptions_throw_classcircularityerror(c);
-                       return NULL;
-               }
-
-               assert(tc->state & CLASS_LOADED);
-
-               if (!(tc->flags & ACC_INTERFACE)) {
-                       exceptions_throw_incompatibleclasschangeerror(tc,
-                                                                                                                 "Implementing class");
-                       return NULL;
-               }
+               tc = c->interfaces[i];
 
                if (!(tc->state & CLASS_LINKED))
                        if (!link_class(tc))
@@ -638,54 +628,32 @@ static classinfo *link_class_intern(classinfo *c)
 
        super = NULL;
 
-       if (c->super.any == NULL) {                     /* class java.lang.Object */
+       /* Check for java/lang/Object. */
+
+       if (c->super == NULL) {
                c->index = 0;
-               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 ((super = resolve_classref_or_classinfo_eager(c->super, true)) == NULL)
-                       return NULL;
-
-               c->super.cls = super;
-               
-               /* detect circularity */
-
-               if (super == c) {
-                       exceptions_throw_classcircularityerror(c);
-                       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 */
-                       log_text("Interface specified as super class");
-                       assert(0);
-               }
-
-               /* Don't allow extending final classes */
-
-               if (super->flags & ACC_FINAL) {
-                       exceptions_throw_verifyerror(NULL,
-                                                                                "Cannot inherit from final class");
-                       return NULL;
-               }
+               super = c->super;
 
-               /* link the superclass if necessary */
+               /* Link the super class if necessary. */
                
                if (!(super->state & CLASS_LINKED))
                        if (!link_class(super))
                                return NULL;
 
-               /* OR the ACC_CLASS_HAS_POINTERS flag */
+               /* OR the ACC_CLASS_HAS_POINTERS and the ACC_CLASS_REFERENCE_*
+                  flags. */
 
-               c->flags |= (super->flags & ACC_CLASS_HAS_POINTERS);
+               c->flags |= (super->flags &
+                                        (ACC_CLASS_HAS_POINTERS | ACC_CLASS_REFERENCE_MASK));
 
                /* handle array classes */
 
@@ -739,7 +707,7 @@ static classinfo *link_class_intern(classinfo *c)
                                        }
                                }
 
-                               tc = tc->super.cls;
+                               tc = tc->super;
                        }
 
                notfoundvftblindex:
@@ -769,7 +737,7 @@ static classinfo *link_class_intern(classinfo *c)
                /* 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]);
@@ -779,7 +747,7 @@ static classinfo *link_class_intern(classinfo *c)
                                if ((im->name == utf_clinit) || (im->name == utf_init))
                                        continue;
 
-                               for (tc = c; tc != NULL; tc = tc->super.cls) {
+                               for (tc = c; tc != NULL; tc = tc->super) {
                                        for (k = 0; k < tc->methodscount; k++) {
                                                if (method_canoverwrite(im, &(tc->methods[k])))
                                                        goto noabstractmethod;
@@ -802,7 +770,7 @@ 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]);
@@ -812,7 +780,7 @@ static classinfo *link_class_intern(classinfo *c)
                                        if ((im->name == utf_clinit) || (im->name == utf_init))
                                                continue;
 
-                                       for (tc = c; tc != NULL; tc = tc->super.cls) {
+                                       for (tc = c; tc != NULL; tc = tc->super) {
                                                for (k = 0; k < tc->methodscount; k++) {
                                                        if (method_canoverwrite(im, &(tc->methods[k])))
                                                                goto noabstractmethod2;
@@ -828,7 +796,7 @@ static classinfo *link_class_intern(classinfo *c)
                                        MCOPY(am, im, methodinfo, 1);
 
                                        am->vftblindex  = (vftbllength++);
-                                       am->class       = c;
+                                       am->clazz       = c;
                                        am->flags      |= ACC_MIRANDA;
 
                                noabstractmethod2:
@@ -850,9 +818,9 @@ static classinfo *link_class_intern(classinfo *c)
 
        interfacetablelength = 0;
 
-       for (tc = c; tc != NULL; tc = tc->super.cls) {
+       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;
@@ -869,7 +837,7 @@ static classinfo *link_class_intern(classinfo *c)
                                         (interfacetablelength - 1) * (interfacetablelength > 1));
 
        c->vftbl                = v;
-       v->class                = c;
+       v->clazz                = c;
        v->vftbllength          = vftbllength;
        v->interfacetablelength = interfacetablelength;
        v->arraydesc            = arraydesc;
@@ -921,7 +889,7 @@ static classinfo *link_class_intern(classinfo *c)
                        m->stubroutine = intrp_createcompilerstub(m);
                else
 #endif
-                       m->stubroutine = createcompilerstub(m);
+                       m->stubroutine = codegen_generate_stub_compiler(m);
 #else
                m->stubroutine = intrp_createcompilerstub(m);
 #endif
@@ -946,16 +914,25 @@ static classinfo *link_class_intern(classinfo *c)
                if (!(f->flags & ACC_STATIC)) {
                        dsize = descriptor_typesize(f->parseddesc);
 
-#if defined(__I386__) || defined(__ARM__)
-                       /* On i386 and ARM we align double and s8 fields to
-                          4-bytes.  This matches what GCC does for struct
-                          members. We must do the same as gcc here because the
-                          offsets in native header structs like java_lang_Double
-                          must match the offsets of the Java fields
-                          (eg. java.lang.Double.value).  */
+#if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__)) || defined(__M68K__)
+                       /* On some architectures and configurations we need to
+                          align long (int64_t) and double fields to 4-bytes to
+                          match what GCC does for struct members.  We must do the
+                          same as GCC here because the offsets in native header
+                          structs like java_lang_Double must match the offsets of
+                          the Java fields (eg. java.lang.Double.value). */
+
+# if LINKER_ALIGNMENT_LONG_DOUBLE != 4
+#  error alignment of long and double is not 4
+# endif
 
                        c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
 #else
+
+# if LINKER_ALIGNMENT_LONG_DOUBLE != 8
+#  error alignment of long and double is not 8
+# endif
+
                        c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
 #endif
 
@@ -981,9 +958,9 @@ static classinfo *link_class_intern(classinfo *c)
 
        /* 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++)
-                       if (!linker_addinterface(c, tc->interfaces[i].cls))
+                       if (!linker_addinterface(c, tc->interfaces[i]))
                                return NULL;
 
        RT_TIMING_GET_TIME(time_fill_iftbl);
@@ -1027,11 +1004,6 @@ static classinfo *link_class_intern(classinfo *c)
                FREE(wi, method_worklist);
        }
 
-#if !defined(NDEBUG)
-       if (linkverbose)
-               log_message_class("Linking done class: ", c);
-#endif
-
        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);
@@ -1103,7 +1075,7 @@ static arraydescriptor *link_array(classinfo *c)
                /* 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;
 
@@ -1136,49 +1108,49 @@ 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;
 
@@ -1201,22 +1173,26 @@ 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(ENABLE_THREADS)
-       compiler_lock();
-#endif
+       LOCK_MONITOR_ENTER(linker_classrenumber_lock);
 
        if (!(c->flags & ACC_INTERFACE)) {
                c->nextsub = NULL;
                c->sub     = NULL;
        }
 
-       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;
@@ -1225,9 +1201,7 @@ static void linker_compute_subclasses(classinfo *c)
 
        linker_compute_class_values(class_java_lang_Object);
 
-#if defined(ENABLE_THREADS)
-       compiler_unlock();
-#endif
+       LOCK_MONITOR_EXIT(linker_classrenumber_lock);
 }
 
 
@@ -1301,7 +1275,7 @@ static bool linker_addinterface(classinfo *c, classinfo *ic)
 #endif
 
                for (j = 0; j < ic->methodscount; j++) {
-                       for (sc = c; sc != NULL; sc = sc->super.cls) {
+                       for (sc = c; sc != NULL; sc = sc->super) {
                                for (k = 0; k < sc->methodscount; k++) {
                                        m = &(sc->methods[k]);
 
@@ -1358,7 +1332,7 @@ static bool linker_addinterface(classinfo *c, classinfo *ic)
        /* add superinterfaces of this interface */
 
        for (j = 0; j < ic->interfacescount; j++)
-               if (!linker_addinterface(c, ic->interfaces[j].cls))
+               if (!linker_addinterface(c, ic->interfaces[j]))
                        return false;
 
        /* everything ok */
@@ -1385,7 +1359,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;