1 /* src/vm/initialize.cpp - static class initializer functions
3 Copyright (C) 1996-2011
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->initializing_thread = thread_get_current();
140 c->state |= CLASS_INITIALIZING;
142 /* call the internal function */
144 r = initialize_class_intern(c);
146 /* if return value is not NULL everything was ok and the class is
150 // Let's make sure that everything is flushed out to memory before
151 // marking the class as initialized.
152 Atomic::write_memory_barrier();
154 c->state |= CLASS_INITIALIZED;
157 /* this initalizing run is done */
159 c->state &= ~CLASS_INITIALIZING;
161 LOCK_MONITOR_EXIT(c);
167 /* initialize_class_intern *****************************************************
169 This function MUST NOT be called directly, because of thread
170 <clinit> race conditions.
172 *******************************************************************************/
174 static bool initialize_class_intern(classinfo *c)
177 java_handle_t *cause;
180 /* maybe the class is not already linked */
182 if (!(c->state & CLASS_LINKED))
186 #if defined(ENABLE_STATISTICS)
191 /* Initialize super class. */
193 if (c->super != NULL) {
194 if (!(c->super->state & CLASS_INITIALIZED)) {
197 log_message_class_message_class("Initialize super class ",
203 if (!initialize_class(c->super))
208 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
210 m = class_findmethod(c, utf_clinit, utf_void__void);
215 log_message_class("Class has no static class initializer: ", c);
221 /* Sun's and IBM's JVM don't care about the static flag */
222 /* if (!(m->flags & ACC_STATIC)) { */
223 /* log_text("Class initializer is not static!"); */
227 log_message_class("Starting static class initializer for class: ", c);
230 /* now call the initializer */
232 (void) vm_call_method(m, NULL);
234 /* we have an exception or error */
236 cause = exceptions_get_exception();
239 /* class is NOT initialized and is marked with error */
241 c->state |= CLASS_ERROR;
243 /* Load java/lang/Exception for the instanceof check. */
245 clazz = load_class_bootstrap(utf_java_lang_Exception);
250 /* Is this an exception? Yes, than wrap it. */
252 if (builtin_instanceof(cause, clazz)) {
253 /* clear exception, because we are calling jit code again */
255 exceptions_clear_exception();
257 /* wrap the exception */
259 exceptions_throw_exceptionininitializererror(cause);
267 log_message_class("Finished static class initializer for class: ", c);
275 * These are local overrides for various environment variables in Emacs.
276 * Please do not remove this and leave it at the end of the file, where
277 * Emacs will automagically detect them.
278 * ---------------------------------------------------------------------
281 * indent-tabs-mode: t
284 * vim:noexpandtab:sw=4:ts=4: