1 /* src/vm/initialize.c - static class initializer functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
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-common.h"
34 #include "vm/global.h"
35 #include "vm/initialize.h"
36 #include "vm/builtin.h"
37 #include "vm/exceptions.h"
38 #include "vm/stringlocal.h"
41 #include "vm/jit/asmpart.h"
43 #include "vmcore/class.h"
44 #include "vmcore/globals.hpp"
45 #include "vmcore/loader.h"
46 #include "vmcore/options.h"
48 #if defined(ENABLE_STATISTICS)
49 # include "vmcore/statistics.h"
53 /* private functions **********************************************************/
55 static bool initialize_class_intern(classinfo *c);
58 /* initialize_init *************************************************************
60 Initialize important system classes.
62 *******************************************************************************/
64 void initialize_init(void)
66 TRACESUBSYSTEMINITIALIZATION("initialize_init");
68 #if defined(ENABLE_JAVASE)
69 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
73 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
75 if (!initialize_class(class_java_lang_String))
76 vm_abort("initialize_init: Initialization failed: java.lang.String");
78 if (!initialize_class(class_java_lang_System))
79 vm_abort("initialize_init: Initialization failed: java.lang.System");
81 if (!initialize_class(class_java_lang_ThreadGroup))
82 vm_abort("initialize_init: Initialization failed: java.lang.ThreadGroup");
84 if (!initialize_class(class_java_lang_Thread))
85 vm_abort("initialize_init: Initialization failed: java.lang.Thread");
88 # error unknown classpath configuration
91 #elif defined(ENABLE_JAVAME_CLDC1_1)
96 # error unknown Java configuration
100 /* initialize_class ************************************************************
102 In Java, every class can have a static initialization
103 function. This function has to be called BEFORE calling other
104 methods or accessing static variables.
106 *******************************************************************************/
108 bool initialize_class(classinfo *c)
112 if (!makeinitializations)
115 LOCK_MONITOR_ENTER(c);
117 /* maybe the class is already initalized or the current thread, which can
118 pass the monitor, is currently initalizing this class */
120 if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
121 LOCK_MONITOR_EXIT(c);
126 /* if <clinit> throw an Error before, the class was marked with an
127 error and we have to throw a NoClassDefFoundError */
129 if (c->state & CLASS_ERROR) {
130 exceptions_throw_noclassdeffounderror(c->name);
132 LOCK_MONITOR_EXIT(c);
134 /* ...but return true, this is ok (mauve test) */
139 /* this initalizing run begins NOW */
141 c->state |= CLASS_INITIALIZING;
143 /* call the internal function */
145 r = initialize_class_intern(c);
147 /* if return value is not NULL everything was ok and the class is
151 c->state |= CLASS_INITIALIZED;
153 /* this initalizing run is done */
155 c->state &= ~CLASS_INITIALIZING;
157 LOCK_MONITOR_EXIT(c);
163 /* initialize_class_intern *****************************************************
165 This function MUST NOT be called directly, because of thread
166 <clinit> race conditions.
168 *******************************************************************************/
170 static bool initialize_class_intern(classinfo *c)
173 java_handle_t *cause;
176 /* maybe the class is not already linked */
178 if (!(c->state & CLASS_LINKED))
182 #if defined(ENABLE_STATISTICS)
187 /* Initialize super class. */
189 if (c->super != NULL) {
190 if (!(c->super->state & CLASS_INITIALIZED)) {
193 log_message_class_message_class("Initialize super class ",
199 if (!initialize_class(c->super))
204 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
206 m = class_findmethod(c, utf_clinit, utf_void__void);
211 log_message_class("Class has no static class initializer: ", c);
217 /* Sun's and IBM's JVM don't care about the static flag */
218 /* if (!(m->flags & ACC_STATIC)) { */
219 /* log_text("Class initializer is not static!"); */
223 log_message_class("Starting static class initializer for class: ", c);
226 /* now call the initializer */
228 (void) vm_call_method(m, NULL);
230 /* we have an exception or error */
232 cause = exceptions_get_exception();
235 /* class is NOT initialized and is marked with error */
237 c->state |= CLASS_ERROR;
239 /* Load java/lang/Exception for the instanceof check. */
241 class = load_class_bootstrap(utf_java_lang_Exception);
246 /* Is this an exception? Yes, than wrap it. */
248 if (builtin_instanceof(cause, class)) {
249 /* clear exception, because we are calling jit code again */
251 exceptions_clear_exception();
253 /* wrap the exception */
255 exceptions_throw_exceptionininitializererror(cause);
263 log_message_class("Finished static class initializer for class: ", c);
271 * These are local overrides for various environment variables in Emacs.
272 * Please do not remove this and leave it at the end of the file, where
273 * Emacs will automagically detect them.
274 * ---------------------------------------------------------------------
277 * indent-tabs-mode: t