* src/vm/jit/powerpc/linux/md-abi.c (md_param_alloc): Cleaned up
[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 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: Edwin Steiner
28
29    Changes: Christian Thalinger
30
31    $Id$
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include <assert.h>
40
41 #include "vm/jit/code.h"
42 #include "mm/memory.h"
43 #include "vm/options.h"
44 #include "arch.h"
45
46
47 /* code_codeinfo_new ***********************************************************
48
49    Create a new codeinfo for the given method.
50    
51    IN:
52        m................method to create a new codeinfo for
53
54    The following fields are set in codeinfo:
55        m
56            isleafmethod
57    all other fields are zeroed
58
59    RETURN VALUE:
60        a new, initialized codeinfo, or
61            NULL if an exception occurred.
62   
63 *******************************************************************************/
64
65 codeinfo *code_codeinfo_new(methodinfo *m)
66 {
67         codeinfo *code;
68
69         code = NEW(codeinfo);
70
71         code->m = m;
72
73 #if defined(ENABLE_STATISTICS)
74         if (opt_stat)
75                 size_codeinfo += sizeof(codeinfo);
76 #endif
77
78         return code;
79 }
80
81
82 /* code_get_sync_slot_count ****************************************************
83
84    Return the number of stack slots used for storing the synchronized object
85    (and the return value around lock_monitor_exit calls) by the given code.
86    
87    IN:
88        code.............the codeinfo of the code in question
89                             (must be != NULL)
90
91    RETURN VALUE:
92        the number of stack slots used for synchronization
93   
94 *******************************************************************************/
95
96 int code_get_sync_slot_count(codeinfo *code)
97 {
98 #ifdef ENABLE_THREADS
99         int count;
100         
101         assert(code);
102
103         if (!checksync)
104                 return 0;
105
106         if (!(code->m->flags & ACC_SYNCHRONIZED))
107                 return 0;
108
109         count = 1;
110
111 #ifdef HAS_4BYTE_STACKSLOT
112         /* long and double need 2 4-byte slots */
113         if (IS_2_WORD_TYPE(code->m->parseddesc->returntype.type))
114                 count++;
115 #endif
116
117 #if defined(__POWERPC__)
118         /* powerpc needs an extra slot */
119         count++;
120 #endif
121
122         return count;
123
124 #else /* !ENABLE_THREADS */
125         
126         return 0;
127
128 #endif /* ENABLE_THREADS */
129 }
130
131
132 /* code_get_stack_frame_size ***************************************************
133
134    Return the number of stack slots that the stack frame of the given code
135    comprises.
136
137    IMPORTANT: The return value does *not* include the saved return address 
138               slot, although it is part of non-leaf stack frames on RISC
139                           architectures. The rationale behind this is that the saved
140                           return address is never moved or changed by replacement, and
141                           this way CISC and RISC architectures can be treated the same.
142                           (See also doc/stack_frames.txt.)
143    
144    IN:
145        code.............the codeinfo of the code in question
146                             (must be != NULL)
147
148    RETURN VALUE:
149        the number of stack slots
150   
151 *******************************************************************************/
152
153 int code_get_stack_frame_size(codeinfo *code)
154 {
155 #if 0
156         int count;
157         
158         assert(code);
159
160         /* slots allocated by register allocator plus saved registers */
161
162 #ifdef HAS_4BYTE_STACKSLOT
163         count = code->memuse + code->savedintcount + 2*code->savedfltcount;
164 #else
165         count = code->memuse + code->savedintcount + code->savedfltcount;
166 #endif
167
168         /* add slots needed in synchronized methods */
169
170         count += code_get_sync_slot_count(code);
171
172         /* keep stack aligned */
173
174 #if defined(__X86_64__)
175         /* the x86_64 codegen only aligns the stack in non-leaf methods */
176         if (!code->isleafmethod || opt_verbosecall)
177                 count |= 1; /* even when return address is added */
178 #endif
179
180         /* XXX align stack on alpha */
181 #if defined(__MIPS__)
182         if (code->isleafmethod)
183                 count = (count + 1) & ~1;
184         else
185                 count |= 1; /* even when return address is added */
186 #endif
187
188 #if defined(__POWERPC__)
189         /* keep stack 16-byte aligned */
190         count = (count + 3) & ~3;
191 #endif
192
193         return count;
194 #endif
195
196         return 0;
197 }
198
199
200 /* code_codeinfo_free **********************************************************
201
202    Free the memory used by a codeinfo.
203    
204    IN:
205        code.............the codeinfo to free
206
207 *******************************************************************************/
208
209 void code_codeinfo_free(codeinfo *code)
210 {
211         if (code == NULL)
212                 return;
213
214         if (code->mcode != NULL)
215                 CFREE((void *) (ptrint) code->mcode, code->mcodelength);
216
217         replace_free_replacement_points(code);
218
219         FREE(code, codeinfo);
220
221 #if defined(ENABLE_STATISTICS)
222         if (opt_stat)
223                 size_codeinfo -= sizeof(codeinfo);
224 #endif
225 }
226
227
228 /* code_free_code_of_method ****************************************************
229
230    Free all codeinfos of the given method
231    
232    IN:
233        m................the method of which the codeinfos are to be freed
234
235 *******************************************************************************/
236
237 void code_free_code_of_method(methodinfo *m)
238 {
239         codeinfo *nextcode;
240         codeinfo *code;
241
242         if (!m)
243                 return;
244         
245         nextcode = m->code;
246         while (nextcode) {
247                 code = nextcode;
248                 nextcode = code->prev;
249                 code_codeinfo_free(code);
250         }
251
252         m->code = NULL;
253 }
254
255 /*
256  * These are local overrides for various environment variables in Emacs.
257  * Please do not remove this and leave it at the end of the file, where
258  * Emacs will automagically detect them.
259  * ---------------------------------------------------------------------
260  * Local variables:
261  * mode: c
262  * indent-tabs-mode: t
263  * c-basic-offset: 4
264  * tab-width: 4
265  * End:
266  * vim:noexpandtab:sw=4:ts=4:
267  */