* renamed CACAO_TYPECHECK to ENABLE_VERIFIER
[cacao.git] / src / vm / initialize.c
1 /* src/vm/initialize.c - static class initializer functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Andreas Krall
31             Christian Thalinger
32
33    $Id: initialize.c 3339 2005-10-04 20:19:11Z twisti $
34
35 */
36
37
38 #include <string.h>
39
40 #include "config.h"
41 #include "vm/types.h"
42
43 #include "vm/global.h"
44 #include "vm/initialize.h"
45 #include "vm/builtin.h"
46 #include "vm/class.h"
47 #include "vm/loader.h"
48 #include "vm/exceptions.h"
49 #include "vm/options.h"
50 #include "vm/statistics.h"
51 #include "vm/stringlocal.h"
52 #include "vm/jit/asmpart.h"
53
54
55 /* private functions **********************************************************/
56
57 static bool initialize_class_intern(classinfo *c);
58
59
60 /* initialize_class ************************************************************
61
62    In Java, every class can have a static initialization
63    function. This function has to be called BEFORE calling other
64    methods or accessing static variables.
65
66 *******************************************************************************/
67
68 bool initialize_class(classinfo *c)
69 {
70         bool r;
71
72         if (!makeinitializations)
73                 return true;
74
75 #if defined(USE_THREADS)
76         /* enter a monitor on the class */
77
78         builtin_monitorenter((java_objectheader *) c);
79 #endif
80
81         /* maybe the class is already initalized or the current thread, which can
82            pass the monitor, is currently initalizing this class */
83
84         /* JOWENN: In future we need an additinal flag: initializationfailed,
85                 since further access to the class should cause a NoClassDefFound,
86                 if the static initializer failed once
87         */
88
89         if (c->initialized || c->initializing) {
90 #if defined(USE_THREADS)
91                 builtin_monitorexit((java_objectheader *) c);
92 #endif
93
94                 return true;
95         }
96
97         /* this initalizing run begins NOW */
98
99         c->initializing = true;
100
101         /* call the internal function */
102
103         r = initialize_class_intern(c);
104
105         /* if return value is not NULL everything was ok and the class is */
106         /* initialized */
107
108         if (r)
109                 c->initialized = true;
110
111         /* this initalizing run is done */
112
113         c->initializing = false;
114
115 #if defined(USE_THREADS)
116         /* leave the monitor */
117
118         builtin_monitorexit((java_objectheader *) c);
119 #endif
120
121         return r;
122 }
123
124
125 /* initialize_class_intern *****************************************************
126
127    This function MUST NOT be called directly, because of thread
128    <clinit> race conditions.
129
130 *******************************************************************************/
131
132 static bool initialize_class_intern(classinfo *c)
133 {
134         methodinfo        *m;
135         java_objectheader *xptr;
136 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
137         int b;
138 #endif
139
140         /* maybe the class is not already linked */
141
142         if (!c->linked)
143                 if (!link_class(c))
144                         return false;
145
146 #if defined(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->initialized) {
155                         if (initverbose)
156                                 log_message_class_message_class("Initialize super class ",
157                                                                                                 c->super.cls,
158                                                                                                 " from ",
159                                                                                                 c);
160
161                         if (!initialize_class(c->super.cls))
162                                 return false;
163                 }
164         }
165
166         /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
167
168         m = class_findmethod(c, utf_clinit, utf_void__void);
169
170         if (!m) {
171                 if (initverbose)
172                         log_message_class("Class has no static class initializer: ", c);
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 (initverbose)
182                 log_message_class("Starting static class initializer for class: ", c);
183
184 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
185         b = blockInts;
186         blockInts = 0;
187 #endif
188
189         /* now call the initializer */
190
191         asm_calljavafunction(m, NULL, NULL, NULL, NULL);
192
193 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
194         assert(blockInts == 0);
195         blockInts = b;
196 #endif
197
198         /* we have an exception or error */
199
200         xptr = *exceptionptr;
201
202         if (xptr) {
203                 /* class is NOT initialized */
204
205                 c->initialized = false;
206
207                 /* is this an exception, than wrap it */
208
209                 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
210                         /* clear exception, because we are calling jit code again */
211
212                         *exceptionptr = NULL;
213
214                         /* wrap the exception */
215
216                         *exceptionptr =
217                                 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
218                                                                                 (java_lang_Throwable *) xptr);
219                 }
220
221                 return false;
222         }
223
224         if (initverbose)
225                 log_message_class("Finished static class initializer for class: ", c);
226
227         return true;
228 }
229
230
231 /*
232  * These are local overrides for various environment variables in Emacs.
233  * Please do not remove this and leave it at the end of the file, where
234  * Emacs will automagically detect them.
235  * ---------------------------------------------------------------------
236  * Local variables:
237  * mode: c
238  * indent-tabs-mode: t
239  * c-basic-offset: 4
240  * tab-width: 4
241  * End:
242  */