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 3999 2005-12-22 14:04:47Z 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"
53 #include "vm/jit/asmpart.h"
56 /* private functions **********************************************************/
58 static bool initialize_class_intern(classinfo *c);
61 /* initialize_class ************************************************************
63 In Java, every class can have a static initialization
64 function. This function has to be called BEFORE calling other
65 methods or accessing static variables.
67 *******************************************************************************/
69 bool initialize_class(classinfo *c)
73 if (!makeinitializations)
76 #if defined(USE_THREADS)
77 /* enter a monitor on the class */
79 builtin_monitorenter((java_objectheader *) c);
82 /* maybe the class is already initalized or the current thread, which can
83 pass the monitor, is currently initalizing this class */
85 if ((c->state & CLASS_INITIALIZING) || (c->state & CLASS_INITIALIZED)) {
86 #if defined(USE_THREADS)
87 builtin_monitorexit((java_objectheader *) c);
93 /* if <clinit> throw an Error before, the class was marked with an
94 error and we have to throw a NoClassDefFoundError */
96 if (c->state & CLASS_ERROR) {
97 *exceptionptr = new_noclassdeffounderror(c->name);
99 #if defined(USE_THREADS)
100 builtin_monitorexit((java_objectheader *) c);
103 /* ...but return true, this is ok (mauve test) */
108 /* this initalizing run begins NOW */
110 c->state |= CLASS_INITIALIZING;
112 /* call the internal function */
114 r = initialize_class_intern(c);
116 /* if return value is not NULL everything was ok and the class is
120 c->state |= CLASS_INITIALIZED;
122 /* this initalizing run is done */
124 c->state &= ~CLASS_INITIALIZING;
126 #if defined(USE_THREADS)
127 /* leave the monitor */
129 builtin_monitorexit((java_objectheader *) c);
136 /* initialize_class_intern *****************************************************
138 This function MUST NOT be called directly, because of thread
139 <clinit> race conditions.
141 *******************************************************************************/
143 static bool initialize_class_intern(classinfo *c)
146 java_objectheader *xptr;
147 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
151 /* maybe the class is not already linked */
153 if (!(c->state & CLASS_LINKED))
157 #if defined(ENABLE_STATISTICS)
162 /* initialize super class */
165 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
168 log_message_class_message_class("Initialize super class ",
174 if (!initialize_class(c->super.cls))
179 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
181 m = class_findmethod(c, utf_clinit, utf_void__void);
186 log_message_class("Class has no static class initializer: ", c);
192 /* Sun's and IBM's JVM don't care about the static flag */
193 /* if (!(m->flags & ACC_STATIC)) { */
194 /* log_text("Class initializer is not static!"); */
198 log_message_class("Starting static class initializer for class: ", c);
201 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
206 /* now call the initializer */
208 asm_calljavafunction(m, NULL, NULL, NULL, NULL);
210 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
211 assert(blockInts == 0);
215 /* we have an exception or error */
217 xptr = *exceptionptr;
220 /* class is NOT initialized and is marked with error */
222 c->state |= CLASS_ERROR;
224 /* is this an exception, than wrap it */
226 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
227 /* clear exception, because we are calling jit code again */
229 *exceptionptr = NULL;
231 /* wrap the exception */
234 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
235 (java_lang_Throwable *) xptr);
243 log_message_class("Finished static class initializer for class: ", c);
251 * These are local overrides for various environment variables in Emacs.
252 * Please do not remove this and leave it at the end of the file, where
253 * Emacs will automagically detect them.
254 * ---------------------------------------------------------------------
257 * indent-tabs-mode: t