f602073acc171c980f98ae5cea4b98ecf3487560
[cacao.git] / src / vm / initialize.cpp
1 /* src/vm/initialize.cpp - static class initializer functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "vm/types.h"
31
32 #include "threads/lock.hpp"
33
34 #include "vm/jit/builtin.hpp"
35 #include "vm/class.hpp"
36 #include "vm/exceptions.hpp"
37 #include "vm/global.h"
38 #include "vm/globals.hpp"
39 #include "vm/initialize.hpp"
40 #include "vm/loader.hpp"
41 #include "vm/options.h"
42 #include "vm/vm.hpp"
43
44 #if defined(ENABLE_STATISTICS)
45 # include "vm/statistics.h"
46 #endif
47
48 #include "vm/jit/asmpart.h"
49
50
51 /* private functions **********************************************************/
52
53 static bool initialize_class_intern(classinfo *c);
54
55
56 /* initialize_init *************************************************************
57
58    Initialize important system classes.
59
60 *******************************************************************************/
61
62 void initialize_init(void)
63 {
64         TRACESUBSYSTEMINITIALIZATION("initialize_init");
65
66 #if defined(ENABLE_JAVASE)
67 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
68
69         /* Nothing. */
70
71 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
72
73         if (!initialize_class(class_java_lang_String))
74                 vm_abort("initialize_init: Initialization failed: java.lang.String");
75
76         if (!initialize_class(class_java_lang_System))
77                 vm_abort("initialize_init: Initialization failed: java.lang.System");
78
79         if (!initialize_class(class_java_lang_ThreadGroup))
80                 vm_abort("initialize_init: Initialization failed: java.lang.ThreadGroup");
81
82         if (!initialize_class(class_java_lang_Thread))
83                 vm_abort("initialize_init: Initialization failed: java.lang.Thread");
84
85 # else
86 #  error unknown classpath configuration
87 # endif
88
89 #elif defined(ENABLE_JAVAME_CLDC1_1)
90
91         /* Nothing. */
92
93 #else
94 # error unknown Java configuration
95 #endif
96 }
97
98 /* initialize_class ************************************************************
99
100    In Java, every class can have a static initialization
101    function. This function has to be called BEFORE calling other
102    methods or accessing static variables.
103
104 *******************************************************************************/
105
106 bool initialize_class(classinfo *c)
107 {
108         bool r;
109
110         if (!makeinitializations)
111                 return true;
112
113         LOCK_MONITOR_ENTER(c);
114
115         /* maybe the class is already initalized or the current thread, which can
116            pass the monitor, is currently initalizing this class */
117
118         if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
119                 LOCK_MONITOR_EXIT(c);
120
121                 return true;
122         }
123
124         /* if <clinit> throw an Error before, the class was marked with an
125        error and we have to throw a NoClassDefFoundError */
126
127         if (c->state & CLASS_ERROR) {
128                 exceptions_throw_noclassdeffounderror(c->name);
129
130                 LOCK_MONITOR_EXIT(c);
131
132                 /* ...but return true, this is ok (mauve test) */
133
134                 return true;
135         }
136
137         /* this initalizing run begins NOW */
138
139         c->state |= CLASS_INITIALIZING;
140
141         /* call the internal function */
142
143         r = initialize_class_intern(c);
144
145         /* if return value is not NULL everything was ok and the class is
146            initialized */
147
148         if (r) {
149         // Let's make sure that everything is flushed out to memory before
150         // marking the class as initialized.
151         Atomic::write_memory_barrier();
152
153         c->state |= CLASS_INITIALIZED;
154     }
155
156         /* this initalizing run is done */
157
158         c->state &= ~CLASS_INITIALIZING;
159
160         LOCK_MONITOR_EXIT(c);
161
162         return r;
163 }
164
165
166 /* initialize_class_intern *****************************************************
167
168    This function MUST NOT be called directly, because of thread
169    <clinit> race conditions.
170
171 *******************************************************************************/
172
173 static bool initialize_class_intern(classinfo *c)
174 {
175         methodinfo    *m;
176         java_handle_t *cause;
177         classinfo     *clazz;
178
179         /* maybe the class is not already linked */
180
181         if (!(c->state & CLASS_LINKED))
182                 if (!link_class(c))
183                         return false;
184
185 #if defined(ENABLE_STATISTICS)
186         if (opt_stat)
187                 count_class_inits++;
188 #endif
189
190         /* Initialize super class. */
191
192         if (c->super != NULL) {
193                 if (!(c->super->state & CLASS_INITIALIZED)) {
194 #if !defined(NDEBUG)
195                         if (initverbose)
196                                 log_message_class_message_class("Initialize super class ",
197                                                                                                 c->super,
198                                                                                                 " from ",
199                                                                                                 c);
200 #endif
201
202                         if (!initialize_class(c->super))
203                                 return false;
204                 }
205         }
206
207         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
208
209         m = class_findmethod(c, utf_clinit, utf_void__void);
210
211         if (m == NULL) {
212 #if !defined(NDEBUG)
213                 if (initverbose)
214                         log_message_class("Class has no static class initializer: ", c);
215 #endif
216
217                 return true;
218         }
219
220         /* Sun's and IBM's JVM don't care about the static flag */
221 /*      if (!(m->flags & ACC_STATIC)) { */
222 /*              log_text("Class initializer is not static!"); */
223
224 #if !defined(NDEBUG)
225         if (initverbose)
226                 log_message_class("Starting static class initializer for class: ", c);
227 #endif
228
229         /* now call the initializer */
230
231         (void) vm_call_method(m, NULL);
232
233         /* we have an exception or error */
234
235         cause = exceptions_get_exception();
236
237         if (cause != NULL) {
238                 /* class is NOT initialized and is marked with error */
239
240                 c->state |= CLASS_ERROR;
241
242                 /* Load java/lang/Exception for the instanceof check. */
243
244                 clazz = load_class_bootstrap(utf_java_lang_Exception);
245
246                 if (clazz == NULL)
247                         return false;
248
249                 /* Is this an exception?  Yes, than wrap it. */
250
251                 if (builtin_instanceof(cause, clazz)) {
252                         /* clear exception, because we are calling jit code again */
253
254                         exceptions_clear_exception();
255
256                         /* wrap the exception */
257
258                         exceptions_throw_exceptionininitializererror(cause);
259                 }
260
261                 return false;
262         }
263
264 #if !defined(NDEBUG)
265         if (initverbose)
266                 log_message_class("Finished static class initializer for class: ", c);
267 #endif
268
269         return true;
270 }
271
272
273 /*
274  * These are local overrides for various environment variables in Emacs.
275  * Please do not remove this and leave it at the end of the file, where
276  * Emacs will automagically detect them.
277  * ---------------------------------------------------------------------
278  * Local variables:
279  * mode: c++
280  * indent-tabs-mode: t
281  * c-basic-offset: 4
282  * tab-width: 4
283  * End:
284  */