8fce23ec4a2bca89213a3287124f3024a51ea8bb
[cacao.git] / src / vm / jit / code.c
1 /* src/vm/jit/code.c - codeinfo struct for representing compiled code
2
3    Copyright (C) 1996-2005, 2006, 2007 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    $Id$
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "arch.h"
37
38 #include "mm/memory.h"
39
40 #if defined(ENABLE_THREADS)
41 # include "threads/native/lock.h"
42 #endif
43
44 #include "vm/jit/code.h"
45 #include "vm/jit/codegen-common.h"
46 #include "vm/jit/methodheader.h"
47 #include "vm/jit/patcher-common.h"
48
49 #include "vmcore/options.h"
50
51
52 /* code_init *******************************************************************
53
54    Initialize the code-subsystem.
55
56 *******************************************************************************/
57
58 bool code_init(void)
59 {
60         /* check for offset of code->m == 0 (see comment in code.h) */
61
62         assert(OFFSET(codeinfo, m) == 0);
63
64         /* everything's ok */
65
66         return true;
67 }
68
69
70 /* code_codeinfo_new ***********************************************************
71
72    Create a new codeinfo for the given method.
73    
74    IN:
75        m................method to create a new codeinfo for
76
77    The following fields are set in codeinfo:
78        m
79        patchers
80
81    RETURN VALUE:
82        a new, initialized codeinfo, or
83            NULL if an exception occurred.
84   
85 *******************************************************************************/
86
87 codeinfo *code_codeinfo_new(methodinfo *m)
88 {
89         codeinfo *code;
90
91         code = NEW(codeinfo);
92
93         code->m = m;
94
95         patcher_list_create(code);
96
97 #if defined(ENABLE_STATISTICS)
98         if (opt_stat)
99                 size_codeinfo += sizeof(codeinfo);
100 #endif
101
102         return code;
103 }
104
105
106 /* code_find_codeinfo_for_pc ***************************************************
107
108    Return the codeinfo for the compilation unit that contains the
109    given PC.
110
111    IN:
112        pc...............machine code position
113
114    RETURN VALUE:
115        the codeinfo * for the given PC
116
117 *******************************************************************************/
118
119 codeinfo *code_find_codeinfo_for_pc(u1 *pc)
120 {
121         u1 *pv;
122
123         pv = codegen_get_pv_from_pc(pc);
124         assert(pv);
125
126         return *(codeinfo **)(pv + CodeinfoPointer);
127 }
128
129
130 /* code_get_methodinfo_for_pv **************************************************
131
132    Return the methodinfo for the given PV.
133
134    IN:
135        pv...............PV
136
137    RETURN VALUE:
138        the methodinfo *
139
140 *******************************************************************************/
141
142 methodinfo *code_get_methodinfo_for_pv(u1 *pv)
143 {
144         codeinfo *code;
145
146         code = *((codeinfo **) (pv + CodeinfoPointer));
147
148         return code->m;
149 }
150
151
152 /* code_get_sync_slot_count ****************************************************
153
154    Return the number of stack slots used for storing the synchronized object
155    (and the return value around lock_monitor_exit calls) by the given code.
156    
157    IN:
158        code.............the codeinfo of the code in question
159                             (must be != NULL)
160
161    RETURN VALUE:
162        the number of stack slots used for synchronization
163   
164 *******************************************************************************/
165
166 #if defined(ENABLE_REPLACEMENT)
167 int code_get_sync_slot_count(codeinfo *code)
168 {
169 #ifdef ENABLE_THREADS
170         int count;
171         
172         assert(code);
173
174         if (!checksync)
175                 return 0;
176
177         if (!(code->m->flags & ACC_SYNCHRONIZED))
178                 return 0;
179
180         count = 1;
181
182 #ifdef HAS_4BYTE_STACKSLOT
183         /* long and double need 2 4-byte slots */
184         if (IS_2_WORD_TYPE(code->m->parseddesc->returntype.type))
185                 count++;
186 #endif
187
188 #if defined(__POWERPC__)
189         /* powerpc needs an extra slot */
190         count++;
191 #endif
192
193         return count;
194
195 #else /* !ENABLE_THREADS */
196         
197         return 0;
198
199 #endif /* ENABLE_THREADS */
200 }
201 #endif /* defined(ENABLE_REPLACEMENT) */
202
203
204 /* code_codeinfo_free **********************************************************
205
206    Free the memory used by a codeinfo.
207    
208    IN:
209        code.............the codeinfo to free
210
211 *******************************************************************************/
212
213 void code_codeinfo_free(codeinfo *code)
214 {
215         if (code == NULL)
216                 return;
217
218         if (code->mcode != NULL)
219                 CFREE((void *) (ptrint) code->mcode, code->mcodelength);
220
221         patcher_list_free(code);
222
223 #if defined(ENABLE_REPLACEMENT)
224         replace_free_replacement_points(code);
225 #endif
226
227         FREE(code, codeinfo);
228
229 #if defined(ENABLE_STATISTICS)
230         if (opt_stat)
231                 size_codeinfo -= sizeof(codeinfo);
232 #endif
233 }
234
235
236 /* code_free_code_of_method ****************************************************
237
238    Free all codeinfos of the given method
239    
240    IN:
241        m................the method of which the codeinfos are to be freed
242
243 *******************************************************************************/
244
245 void code_free_code_of_method(methodinfo *m)
246 {
247         codeinfo *nextcode;
248         codeinfo *code;
249
250         if (!m)
251                 return;
252         
253         nextcode = m->code;
254         while (nextcode) {
255                 code = nextcode;
256                 nextcode = code->prev;
257                 code_codeinfo_free(code);
258         }
259
260         m->code = NULL;
261 }
262
263 /*
264  * These are local overrides for various environment variables in Emacs.
265  * Please do not remove this and leave it at the end of the file, where
266  * Emacs will automagically detect them.
267  * ---------------------------------------------------------------------
268  * Local variables:
269  * mode: c
270  * indent-tabs-mode: t
271  * c-basic-offset: 4
272  * tab-width: 4
273  * End:
274  * vim:noexpandtab:sw=4:ts=4:
275  */