* src/vm/jit/powerpc/darwin/md-os.c (md_signal_handler_sigusr2): New
[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 4921 2006-05-15 14:24:36Z 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(ENABLE_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(ENABLE_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(ENABLE_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(ENABLE_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
149         /* maybe the class is not already linked */
150
151         if (!(c->state & CLASS_LINKED))
152                 if (!link_class(c))
153                         return false;
154
155 #if defined(ENABLE_STATISTICS)
156         if (opt_stat)
157                 count_class_inits++;
158 #endif
159
160         /* initialize super class */
161
162         if (c->super.cls) {
163                 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
164 #if !defined(NDEBUG)
165                         if (initverbose)
166                                 log_message_class_message_class("Initialize super class ",
167                                                                                                 c->super.cls,
168                                                                                                 " from ",
169                                                                                                 c);
170 #endif
171
172                         if (!initialize_class(c->super.cls))
173                                 return false;
174                 }
175         }
176
177         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
178
179         m = class_findmethod(c, utf_clinit, utf_void__void);
180
181         if (!m) {
182 #if !defined(NDEBUG)
183                 if (initverbose)
184                         log_message_class("Class has no static class initializer: ", c);
185 #endif
186
187                 return true;
188         }
189
190         /* Sun's and IBM's JVM don't care about the static flag */
191 /*      if (!(m->flags & ACC_STATIC)) { */
192 /*              log_text("Class initializer is not static!"); */
193
194 #if !defined(NDEBUG)
195         if (initverbose)
196                 log_message_class("Starting static class initializer for class: ", c);
197 #endif
198
199         /* now call the initializer */
200
201         (void) vm_call_method(m, NULL);
202
203         /* we have an exception or error */
204
205         xptr = *exceptionptr;
206
207         if (xptr) {
208                 /* class is NOT initialized and is marked with error */
209
210                 c->state |= CLASS_ERROR;
211
212                 /* is this an exception, than wrap it */
213
214                 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
215                         /* clear exception, because we are calling jit code again */
216
217                         *exceptionptr = NULL;
218
219                         /* wrap the exception */
220
221                         *exceptionptr =
222                                 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
223                                                                                 (java_lang_Throwable *) xptr);
224                 }
225
226                 return false;
227         }
228
229 #if !defined(NDEBUG)
230         if (initverbose)
231                 log_message_class("Finished static class initializer for class: ", c);
232 #endif
233
234         return true;
235 }
236
237
238 /*
239  * These are local overrides for various environment variables in Emacs.
240  * Please do not remove this and leave it at the end of the file, where
241  * Emacs will automagically detect them.
242  * ---------------------------------------------------------------------
243  * Local variables:
244  * mode: c
245  * indent-tabs-mode: t
246  * c-basic-offset: 4
247  * tab-width: 4
248  * End:
249  */