Backed out changeset ecf451e74e7e
[cacao.git] / src / vmcore / linker.c
index 12a7debfa9869f83b42dd923d6a28b9269324a2a..e8b985aad4891065d335bea85195f6081964781d 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.
 
 #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);
@@ -64,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
@@ -113,10 +130,17 @@ struct dummy_alignment_double_t {
 
 void linker_preinit(void)
 {
+       TRACESUBSYSTEMINITIALIZATION("linker_preinit");
+
        /* Check for if alignment for long and double matches what we
           assume for the current architecture. */
 
-#if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__))
+#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. */
+
+# define LINKER_ALIGNMENT_LONG_DOUBLE 4
+
        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);
@@ -125,6 +149,9 @@ void linker_preinit(void)
                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
+
        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);
@@ -169,6 +196,8 @@ void linker_preinit(void)
 
 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. */
@@ -236,14 +265,30 @@ void linker_init(void)
                vm_abort("linker_init: linking failed");
 #endif
 
+       if (!link_class(class_java_lang_Throwable))
+               vm_abort("linker_init: linking failed");
+
 #if defined(WITH_CLASSPATH_GNU)
        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");
+
+       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 */
 
@@ -260,12 +305,26 @@ void linker_init(void)
        if (!link_class(class_java_lang_reflect_Method))
                vm_abort("linker_init: linking failed");
 
+# if defined(WITH_CLASSPATH_GNU)
+       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))
                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_CLASSPATH_SUN)
        if (!link_class(class_sun_reflect_MagicAccessorImpl))
                vm_abort("linker_init: linking failed");
@@ -280,22 +339,22 @@ void 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
@@ -309,9 +368,9 @@ void linker_init(void)
 
        /* 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");
@@ -321,10 +380,10 @@ void linker_init(void)
 
        /* 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");
@@ -430,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,10 +577,7 @@ static classinfo *link_class_intern(classinfo *c)
 
        RT_TIMING_GET_TIME(time_start);
 
-#if !defined(NDEBUG)
-       if (linkverbose)
-               log_message_class("Linking class: ", c);
-#endif
+       TRACELINKCLASS(c);
 
        /* the class must be loaded */
 
@@ -549,30 +605,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))
@@ -583,49 +619,22 @@ 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_object_t);
                
                vftbllength = supervftbllength = 0;
 
                c->finalizer = NULL;
-
        }
        else {
-               /* Resolve super class. */
+               /* Get super class. */
 
-               super = resolve_classref_or_classinfo_eager(c->super, true);
-
-               if (super == NULL)
-                       return NULL;
+               super = c->super;
 
-               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;
-               }
-
-               /* link the superclass if necessary */
+               /* Link the super class if necessary. */
                
                if (!(super->state & CLASS_LINKED))
                        if (!link_class(super))
@@ -689,7 +698,7 @@ static classinfo *link_class_intern(classinfo *c)
                                        }
                                }
 
-                               tc = tc->super.cls;
+                               tc = tc->super;
                        }
 
                notfoundvftblindex:
@@ -719,7 +728,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]);
@@ -729,7 +738,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;
@@ -752,7 +761,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]);
@@ -762,7 +771,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;
@@ -778,7 +787,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:
@@ -800,9 +809,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;
@@ -819,7 +828,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;
@@ -896,16 +905,25 @@ static classinfo *link_class_intern(classinfo *c)
                if (!(f->flags & ACC_STATIC)) {
                        dsize = descriptor_typesize(f->parseddesc);
 
-#if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__))
-                       /* 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
 
@@ -931,9 +949,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);
@@ -977,11 +995,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);
@@ -1164,7 +1177,7 @@ static void linker_compute_subclasses(classinfo *c)
        LOCK_MONITOR_ENTER(linker_classrenumber_lock);
 
 #if 0 && defined(ENABLE_THREADS) && !defined(DISABLE_GC)
-       threads_cast_stopworld();
+       threads_stopworld();
 #endif
 
        if (!(c->flags & ACC_INTERFACE)) {
@@ -1172,9 +1185,9 @@ static void linker_compute_subclasses(classinfo *c)
                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;
@@ -1186,7 +1199,7 @@ static void linker_compute_subclasses(classinfo *c)
        LOCK_MONITOR_EXIT(linker_classrenumber_lock);
 
 #if 0 && defined(ENABLE_THREADS) && !defined(DISABLE_GC)
-       threads_cast_startworld();
+       threads_startworld();
 #endif
 }
 
@@ -1261,7 +1274,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]);
 
@@ -1318,7 +1331,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 */
@@ -1345,7 +1358,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;