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 3009 2005-07-12 21:01:36Z 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"
52 /* private functions **********************************************************/
54 static bool initialize_class_intern(classinfo *c);
57 /* initialize_class ************************************************************
59 In Java, every class can have a static initialization
60 function. This function has to be called BEFORE calling other
61 methods or accessing static variables.
63 *******************************************************************************/
65 bool initialize_class(classinfo *c)
69 if (!makeinitializations)
72 #if defined(USE_THREADS)
73 /* enter a monitor on the class */
75 builtin_monitorenter((java_objectheader *) c);
78 /* maybe the class is already initalized or the current thread, which can
79 pass the monitor, is currently initalizing this class */
81 /* JOWENN: In future we need an additinal flag: initializationfailed,
82 since further access to the class should cause a NoClassDefFound,
83 if the static initializer failed once
86 if (c->initialized || c->initializing) {
87 #if defined(USE_THREADS)
88 builtin_monitorexit((java_objectheader *) c);
94 /* this initalizing run begins NOW */
96 c->initializing = true;
98 /* call the internal function */
100 r = initialize_class_intern(c);
102 /* if return value is not NULL everything was ok and the class is */
106 c->initialized = true;
108 /* this initalizing run is done */
110 c->initializing = false;
112 #if defined(USE_THREADS)
113 /* leave the monitor */
115 builtin_monitorexit((java_objectheader *) c);
122 /* initialize_class_intern *****************************************************
124 This function MUST NOT be called directly, because of thread
125 <clinit> race conditions.
127 *******************************************************************************/
129 static bool initialize_class_intern(classinfo *c)
132 java_objectheader *xptr;
134 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
138 /* maybe the class is not already linked */
144 #if defined(STATISTICS)
149 /* initialize super class */
152 if (!c->super.cls->initialized) {
154 log_message_class_message_class("Initialize super class ",
159 if (!initialize_class(c->super.cls))
164 /* initialize interface classes */
166 for (i = 0; i < c->interfacescount; i++) {
167 if (!c->interfaces[i].cls->initialized) {
169 log_message_class_message_class("Initialize interface class ",
170 c->interfaces[i].cls,
174 if (!initialize_class(c->interfaces[i].cls))
179 m = class_findmethod(c, utf_clinit, utf_void__void);
183 log_message_class("Class has no static class initializer: ", c);
188 /* Sun's and IBM's JVM don't care about the static flag */
189 /* if (!(m->flags & ACC_STATIC)) { */
190 /* log_text("Class initializer is not static!"); */
193 log_message_class("Starting static class initializer for class: ", c);
195 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
200 /* now call the initializer */
202 asm_calljavafunction(m, NULL, NULL, NULL, NULL);
204 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
205 assert(blockInts == 0);
209 /* we have an exception or error */
211 xptr = *exceptionptr;
214 /* class is NOT initialized */
216 c->initialized = false;
218 /* is this an exception, than wrap it */
220 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
221 /* clear exception, because we are calling jit code again */
223 *exceptionptr = NULL;
225 /* wrap the exception */
228 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
229 (java_lang_Throwable *) xptr);
236 log_message_class("Finished static class initializer for class: ", c);
243 * These are local overrides for various environment variables in Emacs.
244 * Please do not remove this and leave it at the end of the file, where
245 * Emacs will automagically detect them.
246 * ---------------------------------------------------------------------
249 * indent-tabs-mode: t