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