* main: Removed log_cputime call.
[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 3009 2005-07-12 21:01:36Z twisti $
34
35 */
36
37
38 #include <string.h>
39
40 #include "vm/global.h"
41 #include "vm/initialize.h"
42 #include "vm/builtin.h"
43 #include "vm/class.h"
44 #include "vm/loader.h"
45 #include "vm/exceptions.h"
46 #include "vm/options.h"
47 #include "vm/statistics.h"
48 #include "vm/stringlocal.h"
49 #include "vm/jit/asmpart.h"
50
51
52 /* private functions **********************************************************/
53
54 static bool initialize_class_intern(classinfo *c);
55
56
57 /* initialize_class ************************************************************
58
59    In Java, every class can have a static initialization
60    function. This function has to be called BEFORE calling other
61    methods or accessing static variables.
62
63 *******************************************************************************/
64
65 bool initialize_class(classinfo *c)
66 {
67         bool r;
68
69         if (!makeinitializations)
70                 return true;
71
72 #if defined(USE_THREADS)
73         /* enter a monitor on the class */
74
75         builtin_monitorenter((java_objectheader *) c);
76 #endif
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         /* JOWENN: In future we need an additinal flag: initializationfailed,
82                 since further access to the class should cause a NoClassDefFound,
83                 if the static initializer failed once
84         */
85
86         if (c->initialized || c->initializing) {
87 #if defined(USE_THREADS)
88                 builtin_monitorexit((java_objectheader *) c);
89 #endif
90
91                 return true;
92         }
93
94         /* this initalizing run begins NOW */
95
96         c->initializing = true;
97
98         /* call the internal function */
99
100         r = initialize_class_intern(c);
101
102         /* if return value is not NULL everything was ok and the class is */
103         /* initialized */
104
105         if (r)
106                 c->initialized = true;
107
108         /* this initalizing run is done */
109
110         c->initializing = false;
111
112 #if defined(USE_THREADS)
113         /* leave the monitor */
114
115         builtin_monitorexit((java_objectheader *) c);
116 #endif
117
118         return r;
119 }
120
121
122 /* initialize_class_intern *****************************************************
123
124    This function MUST NOT be called directly, because of thread
125    <clinit> race conditions.
126
127 *******************************************************************************/
128
129 static bool initialize_class_intern(classinfo *c)
130 {
131         methodinfo        *m;
132         java_objectheader *xptr;
133         s4                 i;
134 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
135         int b;
136 #endif
137
138         /* maybe the class is not already linked */
139
140         if (!c->linked)
141                 if (!link_class(c))
142                         return false;
143
144 #if defined(STATISTICS)
145         if (opt_stat)
146                 count_class_inits++;
147 #endif
148
149         /* initialize super class */
150
151         if (c->super.cls) {
152                 if (!c->super.cls->initialized) {
153                         if (initverbose)
154                                 log_message_class_message_class("Initialize super class ",
155                                                                                                 c->super.cls,
156                                                                                                 " from ",
157                                                                                                 c);
158
159                         if (!initialize_class(c->super.cls))
160                                 return false;
161                 }
162         }
163
164         /* initialize interface classes */
165
166         for (i = 0; i < c->interfacescount; i++) {
167                 if (!c->interfaces[i].cls->initialized) {
168                         if (initverbose)
169                                 log_message_class_message_class("Initialize interface class ",
170                                                                                                 c->interfaces[i].cls,
171                                                                                                 " from ",
172                                                                                                 c);
173                         
174                         if (!initialize_class(c->interfaces[i].cls))
175                                 return false;
176                 }
177         }
178
179         m = class_findmethod(c, utf_clinit, utf_void__void);
180
181         if (!m) {
182                 if (initverbose)
183                         log_message_class("Class has no static class initializer: ", c);
184
185                 return true;
186         }
187
188         /* Sun's and IBM's JVM don't care about the static flag */
189 /*      if (!(m->flags & ACC_STATIC)) { */
190 /*              log_text("Class initializer is not static!"); */
191
192         if (initverbose)
193                 log_message_class("Starting static class initializer for class: ", c);
194
195 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
196         b = blockInts;
197         blockInts = 0;
198 #endif
199
200         /* now call the initializer */
201
202         asm_calljavafunction(m, NULL, NULL, NULL, NULL);
203
204 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
205         assert(blockInts == 0);
206         blockInts = b;
207 #endif
208
209         /* we have an exception or error */
210
211         xptr = *exceptionptr;
212
213         if (xptr) {
214                 /* class is NOT initialized */
215
216                 c->initialized = false;
217
218                 /* is this an exception, than wrap it */
219
220                 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
221                         /* clear exception, because we are calling jit code again */
222
223                         *exceptionptr = NULL;
224
225                         /* wrap the exception */
226
227                         *exceptionptr =
228                                 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
229                                                                                 (java_lang_Throwable *) xptr);
230                 }
231
232                 return false;
233         }
234
235         if (initverbose)
236                 log_message_class("Finished static class initializer for class: ", c);
237
238         return true;
239 }
240
241
242 /*
243  * These are local overrides for various environment variables in Emacs.
244  * Please do not remove this and leave it at the end of the file, where
245  * Emacs will automagically detect them.
246  * ---------------------------------------------------------------------
247  * Local variables:
248  * mode: c
249  * indent-tabs-mode: t
250  * c-basic-offset: 4
251  * tab-width: 4
252  * End:
253  */