* src/vm/jit/reorder.c (reorder): Iterate to m->basicblockcount + 1,
[cacao.git] / src / vm / method.c
1 /* src/vm/method.c - method 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: Andreas Krall
30             Roman Obermaiser
31             Mark Probst
32             Edwin Steiner
33             Christian Thalinger
34
35    $Id: method.c 5038 2006-06-19 22:22:34Z twisti $
36
37 */
38
39
40 #include "config.h"
41
42 #include <assert.h>
43 #include <stdio.h>
44
45 #include "vm/types.h"
46
47 #include "mm/memory.h"
48 #include "vm/class.h"
49 #include "vm/global.h"
50 #include "vm/linker.h"
51 #include "vm/loader.h"
52 #include "vm/method.h"
53 #include "vm/jit/methodheader.h"
54
55
56 /* method_free *****************************************************************
57
58    Frees all memory that was allocated for this method.
59
60 *******************************************************************************/
61
62 void method_free(methodinfo *m)
63 {
64         if (m->jcode)
65                 MFREE(m->jcode, u1, m->jcodelength);
66
67         if (m->exceptiontable)
68                 MFREE(m->exceptiontable, exceptiontable, m->exceptiontablelength);
69
70         code_free_code_of_method(m);
71
72         if (m->stubroutine) {
73                 if (m->flags & ACC_NATIVE) {
74                         removenativestub(m->stubroutine);
75
76                 } else {
77                         removecompilerstub(m->stubroutine);
78                 }
79         }
80 }
81
82
83 /* method_canoverwrite *********************************************************
84
85    Check if m and old are identical with respect to type and
86    name. This means that old can be overwritten with m.
87         
88 *******************************************************************************/
89
90 bool method_canoverwrite(methodinfo *m, methodinfo *old)
91 {
92         if (m->name != old->name)
93                 return false;
94
95         if (m->descriptor != old->descriptor)
96                 return false;
97
98         if (m->flags & ACC_STATIC)
99                 return false;
100
101         return true;
102 }
103
104
105 /* method_vftbl_lookup *********************************************************
106
107    Does a method lookup in the passed virtual function table.  This
108    function does exactly the same thing as JIT, but additionally
109    relies on the fact, that the methodinfo pointer is at the first
110    data segment slot (even for compiler stubs).
111
112 *******************************************************************************/
113
114 methodinfo *method_vftbl_lookup(vftbl_t *vftbl, methodinfo* m)
115 {
116         methodptr   mptr;
117         methodptr  *pmptr;
118         methodinfo *resm;                   /* pointer to new resolved method     */
119         codeinfo   *code;
120
121         /* If the method is not an instance method, just return it. */
122
123         if (m->flags & ACC_STATIC)
124                 return m;
125
126         assert(vftbl);
127
128         /* Get the method from the virtual function table.  Is this an
129            interface method? */
130
131         if (m->class->flags & ACC_INTERFACE) {
132                 pmptr = vftbl->interfacetable[-(m->class->index)];
133                 mptr  = pmptr[(m - m->class->methods)];
134         }
135         else {
136                 mptr = vftbl->table[m->vftblindex];
137         }
138
139         /* and now get the codeinfo pointer from the first data segment slot */
140
141         code = *((codeinfo **) (mptr + CodeinfoPointer));
142
143         resm = code->m;
144
145         return resm;
146 }
147
148
149 /* method_printflags ***********************************************************
150
151    Prints the flags of a method to stdout like.
152
153 *******************************************************************************/
154
155 #if !defined(NDEBUG)
156 void method_printflags(methodinfo *m)
157 {
158         if (m == NULL) {
159                 printf("NULL");
160                 return;
161         }
162
163         if (m->flags & ACC_PUBLIC)       printf(" PUBLIC");
164         if (m->flags & ACC_PRIVATE)      printf(" PRIVATE");
165         if (m->flags & ACC_PROTECTED)    printf(" PROTECTED");
166         if (m->flags & ACC_STATIC)       printf(" STATIC");
167         if (m->flags & ACC_FINAL)        printf(" FINAL");
168         if (m->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
169         if (m->flags & ACC_VOLATILE)     printf(" VOLATILE");
170         if (m->flags & ACC_TRANSIENT)    printf(" TRANSIENT");
171         if (m->flags & ACC_NATIVE)       printf(" NATIVE");
172         if (m->flags & ACC_INTERFACE)    printf(" INTERFACE");
173         if (m->flags & ACC_ABSTRACT)     printf(" ABSTRACT");
174 }
175 #endif /* !defined(NDEBUG) */
176
177
178 /* method_print ****************************************************************
179
180    Prints a method to stdout like:
181
182    java.lang.Object.<init>()V
183
184 *******************************************************************************/
185
186 #if !defined(NDEBUG)
187 void method_print(methodinfo *m)
188 {
189         if (m == NULL) {
190                 printf("NULL");
191                 return;
192         }
193
194         utf_display_printable_ascii_classname(m->class->name);
195         printf(".");
196         utf_display_printable_ascii(m->name);
197         utf_display_printable_ascii(m->descriptor);
198
199         method_printflags(m);
200 }
201 #endif /* !defined(NDEBUG) */
202
203
204 /* method_println **************************************************************
205
206    Prints a method plus new line to stdout like:
207
208    java.lang.Object.<init>()V
209
210 *******************************************************************************/
211
212 #if !defined(NDEBUG)
213 void method_println(methodinfo *m)
214 {
215         method_print(m);
216         printf("\n");
217 }
218 #endif /* !defined(NDEBUG) */
219
220
221 /* method_methodref_print ******************************************************
222
223    Prints a method reference to stdout.
224
225 *******************************************************************************/
226
227 #if !defined(NDEBUG)
228 void method_methodref_print(constant_FMIref *mr)
229 {
230         if (!mr) {
231                 printf("(constant_FMIref *)NULL");
232                 return;
233         }
234
235         if (IS_FMIREF_RESOLVED(mr)) {
236                 printf("<method> ");
237                 method_print(mr->p.method);
238         }
239         else {
240                 printf("<methodref> ");
241                 utf_display_printable_ascii_classname(mr->p.classref->name);
242                 printf(".");
243                 utf_display_printable_ascii(mr->name);
244                 utf_display_printable_ascii(mr->descriptor);
245         }
246 }
247 #endif /* !defined(NDEBUG) */
248
249
250 /* method_methodref_println ****************************************************
251
252    Prints a method reference to stdout, followed by a newline.
253
254 *******************************************************************************/
255
256 #if !defined(NDEBUG)
257 void method_methodref_println(constant_FMIref *mr)
258 {
259         method_methodref_print(mr);
260         printf("\n");
261 }
262 #endif /* !defined(NDEBUG) */
263
264
265 /*
266  * These are local overrides for various environment variables in Emacs.
267  * Please do not remove this and leave it at the end of the file, where
268  * Emacs will automagically detect them.
269  * ---------------------------------------------------------------------
270  * Local variables:
271  * mode: c
272  * indent-tabs-mode: t
273  * c-basic-offset: 4
274  * tab-width: 4
275  * End:
276  * vim:noexpandtab:sw=4:ts=4:
277  */