* Removed all Id tags.
[cacao.git] / src / vmcore / linker.c
index 91ea0d774acb0c7f4acdafb4f857dc7ca33dce0f..49b43e13851df02b836eca99e41839a3e2cdd04b 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: linker.c 7246 2007-01-29 18:49:05Z twisti $
-
 */
 
 
 #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.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
 
-#include "vm/jit/asmpart.h"
+#include "vm/jit_interface.h"
 
 #include "vmcore/class.h"
 #include "vmcore/classcache.h"
 #include "vmcore/loader.h"
 #include "vmcore/options.h"
-#include "vmcore/resolve.h"
 #include "vmcore/rt-timing.h"
 
+/* #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 "vmcore/statistics.h"
 #endif
 
 #if !defined(NDEBUG) && defined(ENABLE_INLINING)
-extern bool inline_debug_log;
-#define INLINELOG(code)  do { if (inline_debug_log) { code } } while (0)
+#define INLINELOG(code)  do { if (opt_inline_debug_log) { code } } while (0)
 #else
 #define INLINELOG(code)
 #endif
@@ -75,41 +74,11 @@ extern bool inline_debug_log;
 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);
@@ -118,6 +87,22 @@ 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.
@@ -126,10 +111,39 @@ static s4 class_highestinterface(classinfo *c);
 
 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__)) || (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);
+
+       if (OFFSET(dummy_alignment_double_t, d) != 4)
+               vm_abort("linker_init: double alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_double_t, d), 4);
+#else
+       if (OFFSET(dummy_alignment_long_t, l) != 8)
+               vm_abort("linker_init: long alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_long_t, l), 8);
+
+       if (OFFSET(dummy_alignment_double_t, d) != 8)
+               vm_abort("linker_init: double alignment is different from what assumed: %d != %d",
+                                OFFSET(dummy_alignment_double_t, d), 8);
+#endif
+
        /* 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 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 */
@@ -159,7 +173,6 @@ bool linker_init(void)
                return false;
 #endif
 
-
        /* link classes for wrapping primitive types */
 
 #if defined(ENABLE_JAVASE)
@@ -243,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
@@ -297,12 +315,6 @@ bool linker_init(void)
        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). */
 
@@ -312,75 +324,6 @@ bool linker_init(void)
 }
 
 
-/* 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));
-
-               c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
-               
-               /* 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);
-
-               if (!(c = load_class_bootstrap(u)))
-                       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;
-}
-
-
 /* link_class ******************************************************************
 
    Wrapper function for link_class_intern to ease monitor enter/exit
@@ -404,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;
@@ -426,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)
@@ -562,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);
@@ -577,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 */
@@ -599,8 +542,7 @@ static classinfo *link_class_intern(classinfo *c)
        for (i = 0; i < c->interfacescount; i++) {
                /* resolve this super interface */
 
-               if (!resolve_classref_or_classinfo(NULL, c->interfaces[i], resolveEager,
-                                                                                  true, false, &tc))
+               if ((tc = resolve_classref_or_classinfo_eager(c->interfaces[i], true)) == NULL)
                        return NULL;
 
                c->interfaces[i].cls = tc;
@@ -631,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 (!resolve_classref_or_classinfo(NULL, c->super, resolveEager, true, false,
-                                                                                  &super))
+               if (super == NULL)
                        return NULL;
+
                c->super.cls = super;
                
-               /* detect circularity */
+               /* Detect circularity. */
 
                if (super == c) {
                        exceptions_throw_classcircularityerror(c);
@@ -674,9 +619,11 @@ static classinfo *link_class_intern(classinfo *c)
                        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 */
 
@@ -912,7 +859,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
@@ -937,12 +884,14 @@ static classinfo *link_class_intern(classinfo *c)
                if (!(f->flags & ACC_STATIC)) {
                        dsize = descriptor_typesize(f->parseddesc);
 
-                       /* On i386 we only align to 4 bytes even for double and s8.    */
-                       /* 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__)
+#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).  */
+
                        c->instancesize = MEMORY_ALIGN(c->instancesize, 4);
 #else
                        c->instancesize = MEMORY_ALIGN(c->instancesize, dsize);
@@ -1092,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;
 
@@ -1125,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;
 
@@ -1190,21 +1139,29 @@ 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();
+       LOCK_MONITOR_ENTER(linker_classrenumber_lock);
+
+#if 0 && defined(ENABLE_THREADS) && !defined(DISABLE_GC)
+       threads_cast_stopworld();
 #endif
 
        if (!(c->flags & ACC_INTERFACE)) {
-               c->nextsub = 0;
-               c->sub = 0;
+               c->nextsub = NULL;
+               c->sub     = NULL;
        }
 
        if (!(c->flags & ACC_INTERFACE) && (c->super.any != NULL)) {
-               c->nextsub = c->super.cls->sub;
+               c->nextsub        = c->super.cls->sub;
                c->super.cls->sub = c;
        }
 
@@ -1214,8 +1171,10 @@ static void linker_compute_subclasses(classinfo *c)
 
        linker_compute_class_values(class_java_lang_Object);
 
-#if defined(ENABLE_THREADS)
-       compiler_unlock();
+       LOCK_MONITOR_EXIT(linker_classrenumber_lock);
+
+#if 0 && defined(ENABLE_THREADS) && !defined(DISABLE_GC)
+       threads_cast_startworld();
 #endif
 }