8afc07435950185e94f02c50a48acadeb22d946e
[cacao.git] / src / vm / initialize.c
1 /* src/vm/initialize.c - static class initializer functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Andreas Krall
31             Christian Thalinger
32
33    $Id: initialize.c 4188 2006-01-12 23:31:00Z twisti $
34
35 */
36
37
38 #include "config.h"
39
40 #include <string.h>
41
42 #include "vm/types.h"
43
44 #include "vm/global.h"
45 #include "vm/initialize.h"
46 #include "vm/builtin.h"
47 #include "vm/class.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"
54
55
56 /* private functions **********************************************************/
57
58 static bool initialize_class_intern(classinfo *c);
59
60
61 /* initialize_class ************************************************************
62
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.
66
67 *******************************************************************************/
68
69 bool initialize_class(classinfo *c)
70 {
71         bool r;
72
73         if (!makeinitializations)
74                 return true;
75
76 #if defined(USE_THREADS)
77         /* enter a monitor on the class */
78
79         builtin_monitorenter((java_objectheader *) c);
80 #endif
81
82         /* maybe the class is already initalized or the current thread, which can
83            pass the monitor, is currently initalizing this class */
84
85         if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
86 #if defined(USE_THREADS)
87                 builtin_monitorexit((java_objectheader *) c);
88 #endif
89
90                 return true;
91         }
92
93         /* if <clinit> throw an Error before, the class was marked with an
94        error and we have to throw a NoClassDefFoundError */
95
96         if (c->state & CLASS_ERROR) {
97                 *exceptionptr = new_noclassdeffounderror(c->name);
98
99 #if defined(USE_THREADS)
100                 builtin_monitorexit((java_objectheader *) c);
101 #endif
102
103                 /* ...but return true, this is ok (mauve test) */
104
105                 return true;
106         }
107
108         /* this initalizing run begins NOW */
109
110         c->state |= CLASS_INITIALIZING;
111
112         /* call the internal function */
113
114         r = initialize_class_intern(c);
115
116         /* if return value is not NULL everything was ok and the class is
117            initialized */
118
119         if (r)
120                 c->state |= CLASS_INITIALIZED;
121
122         /* this initalizing run is done */
123
124         c->state &= ~CLASS_INITIALIZING;
125
126 #if defined(USE_THREADS)
127         /* leave the monitor */
128
129         builtin_monitorexit((java_objectheader *) c);
130 #endif
131
132         return r;
133 }
134
135
136 /* initialize_class_intern *****************************************************
137
138    This function MUST NOT be called directly, because of thread
139    <clinit> race conditions.
140
141 *******************************************************************************/
142
143 static bool initialize_class_intern(classinfo *c)
144 {
145         methodinfo        *m;
146         java_objectheader *xptr;
147 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
148         int b;
149 #endif
150
151         /* maybe the class is not already linked */
152
153         if (!(c->state & CLASS_LINKED))
154                 if (!link_class(c))
155                         return false;
156
157 #if defined(ENABLE_STATISTICS)
158         if (opt_stat)
159                 count_class_inits++;
160 #endif
161
162         /* initialize super class */
163
164         if (c->super.cls) {
165                 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
166 #if !defined(NDEBUG)
167                         if (initverbose)
168                                 log_message_class_message_class("Initialize super class ",
169                                                                                                 c->super.cls,
170                                                                                                 " from ",
171                                                                                                 c);
172 #endif
173
174                         if (!initialize_class(c->super.cls))
175                                 return false;
176                 }
177         }
178
179         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
180
181         m = class_findmethod(c, utf_clinit, utf_void__void);
182
183         if (!m) {
184 #if !defined(NDEBUG)
185                 if (initverbose)
186                         log_message_class("Class has no static class initializer: ", c);
187 #endif
188
189                 return true;
190         }
191
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!"); */
195
196 #if !defined(NDEBUG)
197         if (initverbose)
198                 log_message_class("Starting static class initializer for class: ", c);
199 #endif
200
201 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
202         b = blockInts;
203         blockInts = 0;
204 #endif
205
206         /* now call the initializer */
207
208         ASM_CALLJAVAFUNCTION(m, NULL, NULL, NULL, NULL);
209
210 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
211         assert(blockInts == 0);
212         blockInts = b;
213 #endif
214
215         /* we have an exception or error */
216
217         xptr = *exceptionptr;
218
219         if (xptr) {
220                 /* class is NOT initialized and is marked with error */
221
222                 c->state |= CLASS_ERROR;
223
224                 /* is this an exception, than wrap it */
225
226                 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
227                         /* clear exception, because we are calling jit code again */
228
229                         *exceptionptr = NULL;
230
231                         /* wrap the exception */
232
233                         *exceptionptr =
234                                 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
235                                                                                 (java_lang_Throwable *) xptr);
236                 }
237
238                 return false;
239         }
240
241 #if !defined(NDEBUG)
242         if (initverbose)
243                 log_message_class("Finished static class initializer for class: ", c);
244 #endif
245
246         return true;
247 }
248
249
250 /*
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  * ---------------------------------------------------------------------
255  * Local variables:
256  * mode: c
257  * indent-tabs-mode: t
258  * c-basic-offset: 4
259  * tab-width: 4
260  * End:
261  */