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