* src/vm/jit/verify/typecheck-common.c: Made debugging and statistics
[cacao.git] / src / vm / initialize.c
index 5402de8adac4c24c9e692a21b14b7babe2195f64..c3a86b719c61bc284c48330000e3761b9b5d82bf 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/initialize.c - static class initializer 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 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
 
    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
+   Contact: cacao@cacaojvm.org
 
    Authors: Reinhard Grafl
 
             Andreas Krall
             Christian Thalinger
 
-   $Id: initialize.c 3321 2005-10-04 10:00:21Z twisti $
+   $Id: initialize.c 5123 2006-07-12 21:45:34Z twisti $
 
 */
 
 
+#include "config.h"
+
 #include <string.h>
 
-#include "config.h"
 #include "vm/types.h"
 
+#if defined(ENABLE_THREADS)
+# include "threads/native/lock.h"
+#else
+# include "threads/none/lock.h"
+#endif
+
 #include "vm/global.h"
 #include "vm/initialize.h"
 #include "vm/builtin.h"
@@ -49,6 +56,7 @@
 #include "vm/options.h"
 #include "vm/statistics.h"
 #include "vm/stringlocal.h"
+#include "vm/vm.h"
 #include "vm/jit/asmpart.h"
 
 
@@ -72,51 +80,49 @@ bool initialize_class(classinfo *c)
        if (!makeinitializations)
                return true;
 
-#if defined(USE_THREADS)
-       /* enter a monitor on the class */
-
-       builtin_monitorenter((java_objectheader *) c);
-#endif
+       LOCK_MONITOR_ENTER(c);
 
        /* maybe the class is already initalized or the current thread, which can
           pass the monitor, is currently initalizing this class */
 
-       /* JOWENN: In future we need an additinal flag: initializationfailed,
-               since further access to the class should cause a NoClassDefFound,
-               if the static initializer failed once
-        */
+       if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
+               LOCK_MONITOR_EXIT(c);
 
-       if (c->initialized || c->initializing) {
-#if defined(USE_THREADS)
-               builtin_monitorexit((java_objectheader *) c);
-#endif
+               return true;
+       }
+
+       /* if <clinit> throw an Error before, the class was marked with an
+       error and we have to throw a NoClassDefFoundError */
+
+       if (c->state & CLASS_ERROR) {
+               *exceptionptr = new_noclassdeffounderror(c->name);
+
+               LOCK_MONITOR_EXIT(c);
+
+               /* ...but return true, this is ok (mauve test) */
 
                return true;
        }
 
        /* this initalizing run begins NOW */
 
-       c->initializing = true;
+       c->state |= CLASS_INITIALIZING;
 
        /* call the internal function */
 
        r = initialize_class_intern(c);
 
-       /* if return value is not NULL everything was ok and the class is */
-       /* initialized */
+       /* if return value is not NULL everything was ok and the class is
+          initialized */
 
        if (r)
-               c->initialized = true;
+               c->state |= CLASS_INITIALIZED;
 
        /* this initalizing run is done */
 
-       c->initializing = false;
-
-#if defined(USE_THREADS)
-       /* leave the monitor */
+       c->state &= ~CLASS_INITIALIZING;
 
-       builtin_monitorexit((java_objectheader *) c);
-#endif
+       LOCK_MONITOR_EXIT(c);
 
        return r;
 }
@@ -133,18 +139,14 @@ static bool initialize_class_intern(classinfo *c)
 {
        methodinfo        *m;
        java_objectheader *xptr;
-       s4                 i;
-#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
-       int b;
-#endif
 
        /* maybe the class is not already linked */
 
-       if (!c->linked)
+       if (!(c->state & CLASS_LINKED))
                if (!link_class(c))
                        return false;
 
-#if defined(STATISTICS)
+#if defined(ENABLE_STATISTICS)
        if (opt_stat)
                count_class_inits++;
 #endif
@@ -152,12 +154,14 @@ static bool initialize_class_intern(classinfo *c)
        /* initialize super class */
 
        if (c->super.cls) {
-               if (!c->super.cls->initialized) {
+               if (!(c->super.cls->state & CLASS_INITIALIZED)) {
+#if !defined(NDEBUG)
                        if (initverbose)
                                log_message_class_message_class("Initialize super class ",
                                                                                                c->super.cls,
                                                                                                " from ",
                                                                                                c);
+#endif
 
                        if (!initialize_class(c->super.cls))
                                return false;
@@ -169,8 +173,10 @@ static bool initialize_class_intern(classinfo *c)
        m = class_findmethod(c, utf_clinit, utf_void__void);
 
        if (!m) {
+#if !defined(NDEBUG)
                if (initverbose)
                        log_message_class("Class has no static class initializer: ", c);
+#endif
 
                return true;
        }
@@ -179,31 +185,23 @@ static bool initialize_class_intern(classinfo *c)
 /*     if (!(m->flags & ACC_STATIC)) { */
 /*             log_text("Class initializer is not static!"); */
 
+#if !defined(NDEBUG)
        if (initverbose)
                log_message_class("Starting static class initializer for class: ", c);
-
-#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
-       b = blockInts;
-       blockInts = 0;
 #endif
 
        /* now call the initializer */
 
-       asm_calljavafunction(m, NULL, NULL, NULL, NULL);
-
-#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
-       assert(blockInts == 0);
-       blockInts = b;
-#endif
+       (void) vm_call_method(m, NULL);
 
        /* we have an exception or error */
 
        xptr = *exceptionptr;
 
        if (xptr) {
-               /* class is NOT initialized */
+               /* class is NOT initialized and is marked with error */
 
-               c->initialized = false;
+               c->state |= CLASS_ERROR;
 
                /* is this an exception, than wrap it */
 
@@ -222,8 +220,10 @@ static bool initialize_class_intern(classinfo *c)
                return false;
        }
 
+#if !defined(NDEBUG)
        if (initverbose)
                log_message_class("Finished static class initializer for class: ", c);
+#endif
 
        return true;
 }