Moved global string definitions to string.c for 2 reasons: it seems to be
[cacao.git] / src / vm / jit / helper.c
1 /* src/vm/jit/asmhelper.c - code patching helper 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: Christian Thalinger
28
29    Changes:
30
31    $Id: helper.c 2458 2005-05-12 23:02:07Z twisti $
32
33 */
34
35
36 #include "vm/class.h"
37 #include "vm/exceptions.h"
38 #include "vm/initialize.h"
39 #include "vm/linker.h"
40 #include "vm/method.h"
41 #include "vm/references.h"
42 #include "vm/resolve.h"
43 #include "vm/stringlocal.h"
44
45 /* XXX class_resolveclassmethod */
46 #include "vm/loader.h"
47
48
49 /* helper_resolve_classinfo ****************************************************
50
51    This function returns the loaded and resolved class.
52
53 *******************************************************************************/
54
55 classinfo *helper_resolve_classinfo(constant_classref *cr)
56 {
57         classinfo *c;
58
59         /* resolve and load the class */
60
61         if (!resolve_classref(NULL, cr, resolveEager, true, &c)) {
62                 java_objectheader *xptr;
63                 java_objectheader *cause;
64
65                 /* get the cause */
66
67                 cause = *exceptionptr;
68
69                 /* convert ClassNotFoundException's to NoClassDefFoundError's */
70
71                 if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
72                         /* clear exception, because we are calling jit code again */
73
74                         *exceptionptr = NULL;
75
76                         /* create new error */
77
78                         xptr =
79                                 new_exception_javastring(string_java_lang_NoClassDefFoundError,
80                                                                                  ((java_lang_Throwable *) cause)->detailMessage);
81
82                         /* we had an exception while creating the error */
83
84                         if (*exceptionptr)
85                                 return NULL;
86
87                         /* set new exception */
88
89                         *exceptionptr = xptr;
90                 }
91
92                 return NULL;
93         }
94
95         /* return the classinfo pointer */
96
97         return c;
98 }
99
100
101 /* helper_resolve_methodinfo ***************************************************
102
103    This function returns the loaded and resolved methodinfo of the
104    passed method.
105
106 *******************************************************************************/
107
108 methodinfo *helper_resolve_methodinfo(unresolved_method *um)
109 {
110         methodinfo *m;
111
112         /* resolve the method */
113
114         if (!resolve_method(um, resolveEager, &m)) {
115                 java_objectheader *xptr;
116                 java_objectheader *cause;
117
118                 /* get the cause */
119
120                 cause = *exceptionptr;
121
122                 /* convert ClassNotFoundException's to NoClassDefFoundError's */
123
124                 if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
125                         /* clear exception, because we are calling jit code again */
126
127                         *exceptionptr = NULL;
128
129                         /* create new error */
130
131                         xptr =
132                                 new_exception_javastring(string_java_lang_NoClassDefFoundError,
133                                                                                  ((java_lang_Throwable *) cause)->detailMessage);
134
135                         /* we had an exception while creating the error */
136
137                         if (*exceptionptr)
138                                 return NULL;
139
140                         /* set new exception */
141
142                         *exceptionptr = xptr;
143                 }
144
145                 return NULL;
146         }
147
148         /* return the methodinfo pointer */
149
150         return m;
151 }
152
153
154 /* helper_resolve_fieldinfo ****************************************************
155
156    This function returns the fieldinfo pointer of the passed field.
157
158 *******************************************************************************/
159
160 void *helper_resolve_fieldinfo(unresolved_field *uf)
161 {
162         fieldinfo *fi;
163
164         /* resolve the field */
165
166         if (!resolve_field(uf, resolveEager, &fi)) {
167                 java_objectheader *xptr;
168                 java_objectheader *cause;
169
170                 /* get the cause */
171
172                 cause = *exceptionptr;
173
174                 /* convert ClassNotFoundException's to NoClassDefFoundError's */
175
176                 if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
177                         /* clear exception, because we are calling jit code again */
178
179                         *exceptionptr = NULL;
180
181                         /* create new error */
182
183                         xptr =
184                                 new_exception_javastring(string_java_lang_NoClassDefFoundError,
185                                                                                  ((java_lang_Throwable *) cause)->detailMessage);
186
187                         /* we had an exception while creating the error */
188
189                         if (*exceptionptr)
190                                 return NULL;
191
192                         /* set new exception */
193
194                         *exceptionptr = xptr;
195                 }
196
197                 return NULL;
198         }
199
200         /* return the fieldinfo pointer */
201
202         return fi;
203 }
204
205
206 /* helper_fillin_stacktrace  ****************************************************
207
208    This function returns the exception given as parameter with a filled in stacktrace
209
210 *******************************************************************************/
211
212 java_objectheader *helper_fillin_stacktrace(java_objectheader* exc)
213 {
214         classinfo *c;
215         methodinfo *m;
216         /*log_text("helper_fillin_stacktrace has beenentered");*/
217         /* these are panics, since this are sever problems, which must never happen*/
218         if (exc==0) panic("Exception must not be null in helper_fillin_stacktrace");
219         if ( ((java_lang_Throwable *) exc)->vmState!=0) return exc;
220         if (exc->vftbl==0) panic ("Exception vftbl must not be null in helper_fillin_stacktrace");
221         /*get classinfo from object instance*/
222         c=exc->vftbl->class;
223         if (c==0) panic("Exception class must not be null in helper_fillin_stacktrace");
224         /*find the fillInStackTrace method*/
225         m=class_resolvemethod(c,utf_fillInStackTrace,utf_void__java_lang_Throwable);
226         if (m==0) panic ("Exception does not have a fillInStackTrace method");
227
228         /*log_text("helper_fillin_stacktrace doing it's work now");*/
229         asm_calljavafunction(m,exc,0,0,0);
230
231         /*return exception back to asmpart*/
232         return exc;
233 }
234
235 java_objectheader *helper_fillin_stacktrace_always(java_objectheader* exc) {
236         if (exc==0) panic("Exception must not be null in helper_fillin_stacktrace");
237         ((java_lang_Throwable *) exc)->vmState=0;
238         return helper_fillin_stacktrace(exc);
239 }
240
241
242
243 /*
244  * These are local overrides for various environment variables in Emacs.
245  * Please do not remove this and leave it at the end of the file, where
246  * Emacs will automagically detect them.
247  * ---------------------------------------------------------------------
248  * Local variables:
249  * mode: c
250  * indent-tabs-mode: t
251  * c-basic-offset: 4
252  * tab-width: 4
253  * End:
254  * vim:noexpandtab:sw=4:ts=4:
255  */