1 /* src/vm/initialize.c - static class initializer functions
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
33 $Id: initialize.c 4921 2006-05-15 14:24:36Z twisti $
44 #include "vm/global.h"
45 #include "vm/initialize.h"
46 #include "vm/builtin.h"
48 #include "vm/loader.h"
49 #include "vm/exceptions.h"
50 #include "vm/options.h"
51 #include "vm/statistics.h"
52 #include "vm/stringlocal.h"
54 #include "vm/jit/asmpart.h"
57 /* private functions **********************************************************/
59 static bool initialize_class_intern(classinfo *c);
62 /* initialize_class ************************************************************
64 In Java, every class can have a static initialization
65 function. This function has to be called BEFORE calling other
66 methods or accessing static variables.
68 *******************************************************************************/
70 bool initialize_class(classinfo *c)
74 if (!makeinitializations)
77 #if defined(ENABLE_THREADS)
78 /* enter a monitor on the class */
80 builtin_monitorenter((java_objectheader *) c);
83 /* maybe the class is already initalized or the current thread, which can
84 pass the monitor, is currently initalizing this class */
86 if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
87 #if defined(ENABLE_THREADS)
88 builtin_monitorexit((java_objectheader *) c);
94 /* if <clinit> throw an Error before, the class was marked with an
95 error and we have to throw a NoClassDefFoundError */
97 if (c->state & CLASS_ERROR) {
98 *exceptionptr = new_noclassdeffounderror(c->name);
100 #if defined(ENABLE_THREADS)
101 builtin_monitorexit((java_objectheader *) c);
104 /* ...but return true, this is ok (mauve test) */
109 /* this initalizing run begins NOW */
111 c->state |= CLASS_INITIALIZING;
113 /* call the internal function */
115 r = initialize_class_intern(c);
117 /* if return value is not NULL everything was ok and the class is
121 c->state |= CLASS_INITIALIZED;
123 /* this initalizing run is done */
125 c->state &= ~CLASS_INITIALIZING;
127 #if defined(ENABLE_THREADS)
128 /* leave the monitor */
130 builtin_monitorexit((java_objectheader *) c);
137 /* initialize_class_intern *****************************************************
139 This function MUST NOT be called directly, because of thread
140 <clinit> race conditions.
142 *******************************************************************************/
144 static bool initialize_class_intern(classinfo *c)
147 java_objectheader *xptr;
149 /* maybe the class is not already linked */
151 if (!(c->state & CLASS_LINKED))
155 #if defined(ENABLE_STATISTICS)
160 /* initialize super class */
163 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
166 log_message_class_message_class("Initialize super class ",
172 if (!initialize_class(c->super.cls))
177 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
179 m = class_findmethod(c, utf_clinit, utf_void__void);
184 log_message_class("Class has no static class initializer: ", c);
190 /* Sun's and IBM's JVM don't care about the static flag */
191 /* if (!(m->flags & ACC_STATIC)) { */
192 /* log_text("Class initializer is not static!"); */
196 log_message_class("Starting static class initializer for class: ", c);
199 /* now call the initializer */
201 (void) vm_call_method(m, NULL);
203 /* we have an exception or error */
205 xptr = *exceptionptr;
208 /* class is NOT initialized and is marked with error */
210 c->state |= CLASS_ERROR;
212 /* is this an exception, than wrap it */
214 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
215 /* clear exception, because we are calling jit code again */
217 *exceptionptr = NULL;
219 /* wrap the exception */
222 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
223 (java_lang_Throwable *) xptr);
231 log_message_class("Finished static class initializer for class: ", c);
239 * These are local overrides for various environment variables in Emacs.
240 * Please do not remove this and leave it at the end of the file, where
241 * Emacs will automagically detect them.
242 * ---------------------------------------------------------------------
245 * indent-tabs-mode: t