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 3339 2005-10-04 20:19:11Z twisti $
43 #include "vm/global.h"
44 #include "vm/initialize.h"
45 #include "vm/builtin.h"
47 #include "vm/loader.h"
48 #include "vm/exceptions.h"
49 #include "vm/options.h"
50 #include "vm/statistics.h"
51 #include "vm/stringlocal.h"
52 #include "vm/jit/asmpart.h"
55 /* private functions **********************************************************/
57 static bool initialize_class_intern(classinfo *c);
60 /* initialize_class ************************************************************
62 In Java, every class can have a static initialization
63 function. This function has to be called BEFORE calling other
64 methods or accessing static variables.
66 *******************************************************************************/
68 bool initialize_class(classinfo *c)
72 if (!makeinitializations)
75 #if defined(USE_THREADS)
76 /* enter a monitor on the class */
78 builtin_monitorenter((java_objectheader *) c);
81 /* maybe the class is already initalized or the current thread, which can
82 pass the monitor, is currently initalizing this class */
84 /* JOWENN: In future we need an additinal flag: initializationfailed,
85 since further access to the class should cause a NoClassDefFound,
86 if the static initializer failed once
89 if (c->initialized || c->initializing) {
90 #if defined(USE_THREADS)
91 builtin_monitorexit((java_objectheader *) c);
97 /* this initalizing run begins NOW */
99 c->initializing = true;
101 /* call the internal function */
103 r = initialize_class_intern(c);
105 /* if return value is not NULL everything was ok and the class is */
109 c->initialized = true;
111 /* this initalizing run is done */
113 c->initializing = false;
115 #if defined(USE_THREADS)
116 /* leave the monitor */
118 builtin_monitorexit((java_objectheader *) c);
125 /* initialize_class_intern *****************************************************
127 This function MUST NOT be called directly, because of thread
128 <clinit> race conditions.
130 *******************************************************************************/
132 static bool initialize_class_intern(classinfo *c)
135 java_objectheader *xptr;
136 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
140 /* maybe the class is not already linked */
146 #if defined(STATISTICS)
151 /* initialize super class */
154 if (!c->super.cls->initialized) {
156 log_message_class_message_class("Initialize super class ",
161 if (!initialize_class(c->super.cls))
166 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
168 m = class_findmethod(c, utf_clinit, utf_void__void);
172 log_message_class("Class has no static class initializer: ", c);
177 /* Sun's and IBM's JVM don't care about the static flag */
178 /* if (!(m->flags & ACC_STATIC)) { */
179 /* log_text("Class initializer is not static!"); */
182 log_message_class("Starting static class initializer for class: ", c);
184 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
189 /* now call the initializer */
191 asm_calljavafunction(m, NULL, NULL, NULL, NULL);
193 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
194 assert(blockInts == 0);
198 /* we have an exception or error */
200 xptr = *exceptionptr;
203 /* class is NOT initialized */
205 c->initialized = false;
207 /* is this an exception, than wrap it */
209 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
210 /* clear exception, because we are calling jit code again */
212 *exceptionptr = NULL;
214 /* wrap the exception */
217 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
218 (java_lang_Throwable *) xptr);
225 log_message_class("Finished static class initializer for class: ", c);
232 * These are local overrides for various environment variables in Emacs.
233 * Please do not remove this and leave it at the end of the file, where
234 * Emacs will automagically detect them.
235 * ---------------------------------------------------------------------
238 * indent-tabs-mode: t