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