e7b9df43686b1d1aff0767ed4c9b62835d0b5d54
[cacao.git] / src / vm / initialize.c
1 /* src/vm/initialize.c - static class initializer functions
2
3    Copyright (C) 1996-2005, 2006, 2007 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    $Id: initialize.c 8295 2007-08-11 17:57:24Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <string.h>
33
34 #include "vm/types.h"
35
36 #include "threads/lock-common.h"
37
38 #include "vm/global.h"
39 #include "vm/initialize.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/stringlocal.h"
43 #include "vm/vm.h"
44
45 #include "vm/jit/asmpart.h"
46
47 #include "vmcore/class.h"
48 #include "vmcore/loader.h"
49 #include "vmcore/options.h"
50
51 #if defined(ENABLE_STATISTICS)
52 # include "vmcore/statistics.h"
53 #endif
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         LOCK_MONITOR_ENTER(c);
77
78         /* maybe the class is already initalized or the current thread, which can
79            pass the monitor, is currently initalizing this class */
80
81         if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
82                 LOCK_MONITOR_EXIT(c);
83
84                 return true;
85         }
86
87         /* if <clinit> throw an Error before, the class was marked with an
88        error and we have to throw a NoClassDefFoundError */
89
90         if (c->state & CLASS_ERROR) {
91                 exceptions_throw_noclassdeffounderror(c->name);
92
93                 LOCK_MONITOR_EXIT(c);
94
95                 /* ...but return true, this is ok (mauve test) */
96
97                 return true;
98         }
99
100         /* this initalizing run begins NOW */
101
102         c->state |= CLASS_INITIALIZING;
103
104         /* call the internal function */
105
106         r = initialize_class_intern(c);
107
108         /* if return value is not NULL everything was ok and the class is
109            initialized */
110
111         if (r)
112                 c->state |= CLASS_INITIALIZED;
113
114         /* this initalizing run is done */
115
116         c->state &= ~CLASS_INITIALIZING;
117
118         LOCK_MONITOR_EXIT(c);
119
120         return r;
121 }
122
123
124 /* initialize_class_intern *****************************************************
125
126    This function MUST NOT be called directly, because of thread
127    <clinit> race conditions.
128
129 *******************************************************************************/
130
131 static bool initialize_class_intern(classinfo *c)
132 {
133         methodinfo    *m;
134         java_handle_t *xptr;
135
136         /* maybe the class is not already linked */
137
138         if (!(c->state & CLASS_LINKED))
139                 if (!link_class(c))
140                         return false;
141
142 #if defined(ENABLE_STATISTICS)
143         if (opt_stat)
144                 count_class_inits++;
145 #endif
146
147         /* initialize super class */
148
149         if (c->super.cls) {
150                 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
151 #if !defined(NDEBUG)
152                         if (initverbose)
153                                 log_message_class_message_class("Initialize super class ",
154                                                                                                 c->super.cls,
155                                                                                                 " from ",
156                                                                                                 c);
157 #endif
158
159                         if (!initialize_class(c->super.cls))
160                                 return false;
161                 }
162         }
163
164         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
165
166         m = class_findmethod(c, utf_clinit, utf_void__void);
167
168         if (!m) {
169 #if !defined(NDEBUG)
170                 if (initverbose)
171                         log_message_class("Class has no static class initializer: ", c);
172 #endif
173
174                 return true;
175         }
176
177         /* Sun's and IBM's JVM don't care about the static flag */
178 /*      if (!(m->flags & ACC_STATIC)) { */
179 /*              log_text("Class initializer is not static!"); */
180
181 #if !defined(NDEBUG)
182         if (initverbose)
183                 log_message_class("Starting static class initializer for class: ", c);
184 #endif
185
186         /* now call the initializer */
187
188         (void) vm_call_method(m, NULL);
189
190         /* we have an exception or error */
191
192         xptr = exceptions_get_exception();
193
194         if (xptr != NULL) {
195                 /* class is NOT initialized and is marked with error */
196
197                 c->state |= CLASS_ERROR;
198
199                 /* is this an exception, than wrap it */
200
201                 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
202                         /* clear exception, because we are calling jit code again */
203
204                         exceptions_clear_exception();
205
206                         /* wrap the exception */
207
208                         exceptions_throw_exceptionininitializererror(xptr);
209                 }
210
211                 return false;
212         }
213
214 #if !defined(NDEBUG)
215         if (initverbose)
216                 log_message_class("Finished static class initializer for class: ", c);
217 #endif
218
219         return true;
220 }
221
222
223 /*
224  * These are local overrides for various environment variables in Emacs.
225  * Please do not remove this and leave it at the end of the file, where
226  * Emacs will automagically detect them.
227  * ---------------------------------------------------------------------
228  * Local variables:
229  * mode: c
230  * indent-tabs-mode: t
231  * c-basic-offset: 4
232  * tab-width: 4
233  * End:
234  */