* Removed all Id tags.
[cacao.git] / src / vmcore / linker.c
index cc71c70acabb34ddedda9e1744d8a59e8d06a598..49b43e13851df02b836eca99e41839a3e2cdd04b 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: linker.c 8234 2007-07-26 08:21:25Z twisti $
-
 */
 
 
@@ -42,7 +40,9 @@
 #include "toolbox/logging.h"
 
 #include "vm/access.h"
+#include "vm/array.h"
 #include "vm/exceptions.h"
+#include "vm/primitive.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
 
@@ -52,7 +52,6 @@
 #include "vmcore/classcache.h"
 #include "vmcore/loader.h"
 #include "vmcore/options.h"
-#include "vmcore/primitive.h"
 #include "vmcore/rt-timing.h"
 
 /* #include "vm/resolve.h" */
@@ -75,7 +74,7 @@ classinfo *resolve_classref_or_classinfo_eager(classref_or_classinfo cls, bool c
 static s4 interfaceindex;       /* sequential numbering of interfaces         */
 static s4 classvalue;
 
-java_objectheader *linker_classrenumber_lock;
+java_object_t *linker_classrenumber_lock;
 
 
 /* private functions **********************************************************/
@@ -115,7 +114,7 @@ bool linker_init(void)
        /* Check for if alignment for long and double matches what we
           assume for the current architecture. */
 
-#if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__))
+#if defined(__I386__) || (defined(__ARM__) && !defined(__ARM_EABI__)) || (defined(__POWERPC__) && defined(__DARWIN__))
        if (OFFSET(dummy_alignment_long_t, l) != 4)
                vm_abort("linker_init: long alignment is different from what assumed: %d != %d",
                                 OFFSET(dummy_alignment_long_t, l), 4);
@@ -140,7 +139,7 @@ bool linker_init(void)
 #if defined(ENABLE_THREADS)
        /* create the global lock object */
 
-       linker_classrenumber_lock = NEW(java_objectheader);
+       linker_classrenumber_lock = NEW(java_object_t);
 
        LOCK_INIT_OBJECT_LOCK(linker_classrenumber_lock);
 #endif
@@ -174,7 +173,6 @@ bool linker_init(void)
                return false;
 #endif
 
-
        /* link classes for wrapping primitive types */
 
 #if defined(ENABLE_JAVASE)
@@ -258,6 +256,11 @@ bool linker_init(void)
        if (!link_class(class_java_util_Vector))
                return false;
 
+# if defined(WITH_CLASSPATH_SUN)
+       if (!link_class(class_sun_reflect_MagicAccessorImpl))
+               return false;
+# endif
+
        if (!link_class(arrayclass_java_lang_Object))
                return false;
 #endif
@@ -344,9 +347,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;
@@ -366,9 +369,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)
@@ -502,11 +506,6 @@ 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);
@@ -517,6 +516,10 @@ static classinfo *link_class_intern(classinfo *c)
        /* 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 */
@@ -570,21 +573,24 @@ static classinfo *link_class_intern(classinfo *c)
 
        if (c->super.any == NULL) {                     /* class java.lang.Object */
                c->index = 0;
-               c->instancesize = sizeof(java_objectheader);
+               c->instancesize = sizeof(java_object_t);
                
                vftbllength = supervftbllength = 0;
 
                c->finalizer = NULL;
 
-       } else {
-               /* resolve super class */
+       }
+       else {
+               /* Resolve super class. */
+
+               super = resolve_classref_or_classinfo_eager(c->super, true);
 
-               if ((super = resolve_classref_or_classinfo_eager(c->super, true)) == NULL)
+               if (super == NULL)
                        return NULL;
 
                c->super.cls = super;
                
-               /* detect circularity */
+               /* Detect circularity. */
 
                if (super == c) {
                        exceptions_throw_classcircularityerror(c);
@@ -878,7 +884,7 @@ 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__))
+#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
@@ -1035,7 +1041,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;
 
@@ -1068,49 +1074,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;
 
@@ -1133,6 +1139,12 @@ 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)