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