1 /* src/vm/initialize.c - static class initializer functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 $Id: initialize.c 2592 2005-06-08 11:03:52Z twisti $
40 #include "vm/global.h"
41 #include "vm/initialize.h"
42 #include "vm/builtin.h"
44 #include "vm/loader.h"
45 #include "vm/exceptions.h"
46 #include "vm/options.h"
47 #include "vm/statistics.h"
48 #include "vm/stringlocal.h"
49 #include "vm/jit/asmpart.h"
53 /* private functions **********************************************************/
55 static bool initialize_class_intern(classinfo *c);
58 /* initialize_class ************************************************************
60 In Java, every class can have a static initialization
61 function. This function has to be called BEFORE calling other
62 methods or accessing static variables.
64 *******************************************************************************/
66 bool initialize_class(classinfo *c)
70 if (!makeinitializations)
73 #if defined(USE_THREADS)
74 /* enter a monitor on the class */
76 builtin_monitorenter((java_objectheader *) c);
79 /* maybe the class is already initalized or the current thread, which can
80 pass the monitor, is currently initalizing this class */
82 /* JOWENN: In future we need an additinal flag: initializationfailed,
83 since further access to the class should cause a NoClassDefFound,
84 if the static initializer failed once
87 if (c->initialized || c->initializing) {
88 #if defined(USE_THREADS)
89 builtin_monitorexit((java_objectheader *) c);
96 printf("preparing to call initialize_class_intern for %s\n",c->name->text);
98 /* this initalizing run begins NOW */
99 c->initializing = true;
101 /* call the internal function */
102 r = initialize_class_intern(c);
104 /* if return value is not NULL everything was ok and the class is
107 c->initialized = true;
110 printf("finished to call initialize_class_intern for %s\n",c->name->text);
113 /* this initalizing run is done */
114 c->initializing = false;
116 #if defined(USE_THREADS)
117 /* leave the monitor */
119 builtin_monitorexit((java_objectheader *) c);
126 /* initialize_class_intern *****************************************************
128 This function MUST NOT be called directly, because of thread
129 <clinit> race conditions.
131 *******************************************************************************/
133 static bool initialize_class_intern(classinfo *c)
136 java_objectheader *xptr;
138 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
142 /* maybe the class is not already linked */
148 #if defined(STATISTICS)
153 /* initialize super class */
156 if (!c->super.cls->initialized) {
158 log_message_class_message_class("Initialize super class ",
164 printf("preparing to call initialize_class for super %s\n",c->super.cls->name->text);
166 if (!initialize_class(c->super.cls))
171 /* initialize interface classes */
173 for (i = 0; i < c->interfacescount; i++) {
174 if (!c->interfaces[i].cls->initialized) {
176 printf("preparing to call initialize_class for interface %s\n",c->interfaces[i].cls->name->text);
179 log_message_class_message_class("Initialize interface class ",
180 c->interfaces[i].cls,
184 if (!initialize_class(c->interfaces[i].cls))
189 m = class_findmethod(c, utf_clinit, utf_void__void);
193 log_message_class("Class has no static class initializer: ", c);
198 /* Sun's and IBM's JVM don't care about the static flag */
199 /* if (!(m->flags & ACC_STATIC)) { */
200 /* log_text("Class initializer is not static!"); */
203 log_message_class("Starting static class initializer for class: ", c);
205 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
210 /* now call the initializer */
212 asm_calljavafunction(m, NULL, NULL, NULL, NULL);
214 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
215 assert(blockInts == 0);
219 /* we have an exception or error */
221 xptr = *exceptionptr;
224 /* class is NOT initialized */
226 c->initialized = false;
228 /* is this an exception, than wrap it */
230 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
231 /* clear exception, because we are calling jit code again */
233 *exceptionptr = NULL;
235 /* wrap the exception */
238 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
239 (java_lang_Throwable *) xptr);
246 log_message_class("Finished static class initializer for class: ", c);
253 * These are local overrides for various environment variables in Emacs.
254 * Please do not remove this and leave it at the end of the file, where
255 * Emacs will automagically detect them.
256 * ---------------------------------------------------------------------
259 * indent-tabs-mode: t