36a2e12089f4e004261e1dbbda55d2a01d437f56
[cacao.git] / src / vm / initialize.c
1 /* src/vm/initialize.c - static class initializer functions
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Andreas Krall
31             Christian Thalinger
32
33    $Id: initialize.c 4552 2006-03-04 17:15:44Z 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/vm.h"
54 #include "vm/jit/asmpart.h"
55
56
57 /* private functions **********************************************************/
58
59 static bool initialize_class_intern(classinfo *c);
60
61
62 /* initialize_class ************************************************************
63
64    In Java, every class can have a static initialization
65    function. This function has to be called BEFORE calling other
66    methods or accessing static variables.
67
68 *******************************************************************************/
69
70 bool initialize_class(classinfo *c)
71 {
72         bool r;
73
74         if (!makeinitializations)
75                 return true;
76
77 #if defined(USE_THREADS)
78         /* enter a monitor on the class */
79
80         builtin_monitorenter((java_objectheader *) c);
81 #endif
82
83         /* maybe the class is already initalized or the current thread, which can
84            pass the monitor, is currently initalizing this class */
85
86         if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
87 #if defined(USE_THREADS)
88                 builtin_monitorexit((java_objectheader *) c);
89 #endif
90
91                 return true;
92         }
93
94         /* if <clinit> throw an Error before, the class was marked with an
95        error and we have to throw a NoClassDefFoundError */
96
97         if (c->state & CLASS_ERROR) {
98                 *exceptionptr = new_noclassdeffounderror(c->name);
99
100 #if defined(USE_THREADS)
101                 builtin_monitorexit((java_objectheader *) c);
102 #endif
103
104                 /* ...but return true, this is ok (mauve test) */
105
106                 return true;
107         }
108
109         /* this initalizing run begins NOW */
110
111         c->state |= CLASS_INITIALIZING;
112
113         /* call the internal function */
114
115         r = initialize_class_intern(c);
116
117         /* if return value is not NULL everything was ok and the class is
118            initialized */
119
120         if (r)
121                 c->state |= CLASS_INITIALIZED;
122
123         /* this initalizing run is done */
124
125         c->state &= ~CLASS_INITIALIZING;
126
127 #if defined(USE_THREADS)
128         /* leave the monitor */
129
130         builtin_monitorexit((java_objectheader *) c);
131 #endif
132
133         return r;
134 }
135
136
137 /* initialize_class_intern *****************************************************
138
139    This function MUST NOT be called directly, because of thread
140    <clinit> race conditions.
141
142 *******************************************************************************/
143
144 static bool initialize_class_intern(classinfo *c)
145 {
146         methodinfo        *m;
147         java_objectheader *xptr;
148 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
149         int b;
150 #endif
151
152         /* maybe the class is not already linked */
153
154         if (!(c->state & CLASS_LINKED))
155                 if (!link_class(c))
156                         return false;
157
158 #if defined(ENABLE_STATISTICS)
159         if (opt_stat)
160                 count_class_inits++;
161 #endif
162
163         /* initialize super class */
164
165         if (c->super.cls) {
166                 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
167 #if !defined(NDEBUG)
168                         if (initverbose)
169                                 log_message_class_message_class("Initialize super class ",
170                                                                                                 c->super.cls,
171                                                                                                 " from ",
172                                                                                                 c);
173 #endif
174
175                         if (!initialize_class(c->super.cls))
176                                 return false;
177                 }
178         }
179
180         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
181
182         m = class_findmethod(c, utf_clinit, utf_void__void);
183
184         if (!m) {
185 #if !defined(NDEBUG)
186                 if (initverbose)
187                         log_message_class("Class has no static class initializer: ", c);
188 #endif
189
190                 return true;
191         }
192
193         /* Sun's and IBM's JVM don't care about the static flag */
194 /*      if (!(m->flags & ACC_STATIC)) { */
195 /*              log_text("Class initializer is not static!"); */
196
197 #if !defined(NDEBUG)
198         if (initverbose)
199                 log_message_class("Starting static class initializer for class: ", c);
200 #endif
201
202 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
203         b = blockInts;
204         blockInts = 0;
205 #endif
206
207         /* now call the initializer */
208
209         (void) vm_call_method_intern(m, NULL, NULL, NULL, NULL);
210
211 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
212         assert(blockInts == 0);
213         blockInts = b;
214 #endif
215
216         /* we have an exception or error */
217
218         xptr = *exceptionptr;
219
220         if (xptr) {
221                 /* class is NOT initialized and is marked with error */
222
223                 c->state |= CLASS_ERROR;
224
225                 /* is this an exception, than wrap it */
226
227                 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
228                         /* clear exception, because we are calling jit code again */
229
230                         *exceptionptr = NULL;
231
232                         /* wrap the exception */
233
234                         *exceptionptr =
235                                 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
236                                                                                 (java_lang_Throwable *) xptr);
237                 }
238
239                 return false;
240         }
241
242 #if !defined(NDEBUG)
243         if (initverbose)
244                 log_message_class("Finished static class initializer for class: ", c);
245 #endif
246
247         return true;
248 }
249
250
251 /*
252  * These are local overrides for various environment variables in Emacs.
253  * Please do not remove this and leave it at the end of the file, where
254  * Emacs will automagically detect them.
255  * ---------------------------------------------------------------------
256  * Local variables:
257  * mode: c
258  * indent-tabs-mode: t
259  * c-basic-offset: 4
260  * tab-width: 4
261  * End:
262  */