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/jit/builtin.hpp"
36 #include "vm/exceptions.hpp"
37 #include "vm/global.h"
38 #include "vm/globals.hpp"
39 #include "vm/initialize.h"
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 c->state |= CLASS_INITIALIZED;
151 /* this initalizing run is done */
153 c->state &= ~CLASS_INITIALIZING;
155 LOCK_MONITOR_EXIT(c);
161 /* initialize_class_intern *****************************************************
163 This function MUST NOT be called directly, because of thread
164 <clinit> race conditions.
166 *******************************************************************************/
168 static bool initialize_class_intern(classinfo *c)
171 java_handle_t *cause;
174 /* maybe the class is not already linked */
176 if (!(c->state & CLASS_LINKED))
180 #if defined(ENABLE_STATISTICS)
185 /* Initialize super class. */
187 if (c->super != NULL) {
188 if (!(c->super->state & CLASS_INITIALIZED)) {
191 log_message_class_message_class("Initialize super class ",
197 if (!initialize_class(c->super))
202 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
204 m = class_findmethod(c, utf_clinit, utf_void__void);
209 log_message_class("Class has no static class initializer: ", c);
215 /* Sun's and IBM's JVM don't care about the static flag */
216 /* if (!(m->flags & ACC_STATIC)) { */
217 /* log_text("Class initializer is not static!"); */
221 log_message_class("Starting static class initializer for class: ", c);
224 /* now call the initializer */
226 (void) vm_call_method(m, NULL);
228 /* we have an exception or error */
230 cause = exceptions_get_exception();
233 /* class is NOT initialized and is marked with error */
235 c->state |= CLASS_ERROR;
237 /* Load java/lang/Exception for the instanceof check. */
239 class = load_class_bootstrap(utf_java_lang_Exception);
244 /* Is this an exception? Yes, than wrap it. */
246 if (builtin_instanceof(cause, class)) {
247 /* clear exception, because we are calling jit code again */
249 exceptions_clear_exception();
251 /* wrap the exception */
253 exceptions_throw_exceptionininitializererror(cause);
261 log_message_class("Finished static class initializer for class: ", c);
269 * These are local overrides for various environment variables in Emacs.
270 * Please do not remove this and leave it at the end of the file, where
271 * Emacs will automagically detect them.
272 * ---------------------------------------------------------------------
275 * indent-tabs-mode: t