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