1 /* src/vm/initialize.cpp - static class initializer functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "threads/lock.hpp"
34 #include "vm/jit/builtin.hpp"
35 #include "vm/class.hpp"
36 #include "vm/exceptions.hpp"
37 #include "vm/global.h"
38 #include "vm/globals.hpp"
39 #include "vm/initialize.hpp"
40 #include "vm/loader.hpp"
41 #include "vm/options.h"
44 #if defined(ENABLE_STATISTICS)
45 # include "vm/statistics.h"
48 #include "vm/jit/asmpart.h"
51 /* private functions **********************************************************/
53 static bool initialize_class_intern(classinfo *c);
56 /* initialize_init *************************************************************
58 Initialize important system classes.
60 *******************************************************************************/
62 void initialize_init(void)
64 TRACESUBSYSTEMINITIALIZATION("initialize_init");
66 #if defined(ENABLE_JAVASE)
67 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
71 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
73 if (!initialize_class(class_java_lang_String))
74 vm_abort("initialize_init: Initialization failed: java.lang.String");
76 if (!initialize_class(class_java_lang_System))
77 vm_abort("initialize_init: Initialization failed: java.lang.System");
79 if (!initialize_class(class_java_lang_ThreadGroup))
80 vm_abort("initialize_init: Initialization failed: java.lang.ThreadGroup");
82 if (!initialize_class(class_java_lang_Thread))
83 vm_abort("initialize_init: Initialization failed: java.lang.Thread");
86 # error unknown classpath configuration
89 #elif defined(ENABLE_JAVAME_CLDC1_1)
94 # error unknown Java configuration
98 /* initialize_class ************************************************************
100 In Java, every class can have a static initialization
101 function. This function has to be called BEFORE calling other
102 methods or accessing static variables.
104 *******************************************************************************/
106 bool initialize_class(classinfo *c)
110 if (!makeinitializations)
113 LOCK_MONITOR_ENTER(c);
115 /* maybe the class is already initalized or the current thread, which can
116 pass the monitor, is currently initalizing this class */
118 if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
119 LOCK_MONITOR_EXIT(c);
124 /* if <clinit> throw an Error before, the class was marked with an
125 error and we have to throw a NoClassDefFoundError */
127 if (c->state & CLASS_ERROR) {
128 exceptions_throw_noclassdeffounderror(c->name);
130 LOCK_MONITOR_EXIT(c);
132 /* ...but return true, this is ok (mauve test) */
137 /* this initalizing run begins NOW */
139 c->state |= CLASS_INITIALIZING;
141 /* call the internal function */
143 r = initialize_class_intern(c);
145 /* if return value is not NULL everything was ok and the class is
149 // Let's make sure that everything is flushed out to memory before
150 // marking the class as initialized.
151 Atomic::write_memory_barrier();
153 c->state |= CLASS_INITIALIZED;
156 /* this initalizing run is done */
158 c->state &= ~CLASS_INITIALIZING;
160 LOCK_MONITOR_EXIT(c);
166 /* initialize_class_intern *****************************************************
168 This function MUST NOT be called directly, because of thread
169 <clinit> race conditions.
171 *******************************************************************************/
173 static bool initialize_class_intern(classinfo *c)
176 java_handle_t *cause;
179 /* maybe the class is not already linked */
181 if (!(c->state & CLASS_LINKED))
185 #if defined(ENABLE_STATISTICS)
190 /* Initialize super class. */
192 if (c->super != NULL) {
193 if (!(c->super->state & CLASS_INITIALIZED)) {
196 log_message_class_message_class("Initialize super class ",
202 if (!initialize_class(c->super))
207 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
209 m = class_findmethod(c, utf_clinit, utf_void__void);
214 log_message_class("Class has no static class initializer: ", c);
220 /* Sun's and IBM's JVM don't care about the static flag */
221 /* if (!(m->flags & ACC_STATIC)) { */
222 /* log_text("Class initializer is not static!"); */
226 log_message_class("Starting static class initializer for class: ", c);
229 /* now call the initializer */
231 (void) vm_call_method(m, NULL);
233 /* we have an exception or error */
235 cause = exceptions_get_exception();
238 /* class is NOT initialized and is marked with error */
240 c->state |= CLASS_ERROR;
242 /* Load java/lang/Exception for the instanceof check. */
244 clazz = load_class_bootstrap(utf_java_lang_Exception);
249 /* Is this an exception? Yes, than wrap it. */
251 if (builtin_instanceof(cause, clazz)) {
252 /* clear exception, because we are calling jit code again */
254 exceptions_clear_exception();
256 /* wrap the exception */
258 exceptions_throw_exceptionininitializererror(cause);
266 log_message_class("Finished static class initializer for class: ", c);
274 * These are local overrides for various environment variables in Emacs.
275 * Please do not remove this and leave it at the end of the file, where
276 * Emacs will automagically detect them.
277 * ---------------------------------------------------------------------
280 * indent-tabs-mode: t