cleanup: Remove unnecesary #include from bearch.h.
[libfirm] / ir / be / sparc / sparc_stackframe.c
1 /*
2  * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Manage addressing into the stackframe
23  * @author  Matthias Braun
24  */
25 #include "config.h"
26
27 #include "error.h"
28 #include "firm_types.h"
29 #include "irnode_t.h"
30 #include "bearch_sparc_t.h"
31 #include "sparc_new_nodes.h"
32 #include "sparc_cconv.h"
33 #include "bitfiddle.h"
34 #include "bearch.h"
35 #include "benode.h"
36 #include "besched.h"
37
38 static void set_irn_sp_bias(ir_node *node, int new_bias)
39 {
40         if (be_is_IncSP(node)) {
41                 be_set_IncSP_offset(node, new_bias);
42         } else if (is_sparc_Save(node)) {
43                 sparc_attr_t *attr = get_sparc_attr(node);
44                 attr->immediate_value = -new_bias;
45         } else if (is_sparc_Restore(node)) {
46                 sparc_attr_t *attr = get_sparc_attr(node);
47                 attr->immediate_value = new_bias;
48         }
49 }
50
51 static void process_bias(ir_node *block, bool sp_relative, int bias,
52                          int free_bytes)
53 {
54         mark_Block_block_visited(block);
55
56         /* process schedule */
57         sched_foreach(block, irn) {
58                 int irn_bias;
59
60                 /* set bias to nodes with entities */
61                 ir_entity *entity = arch_get_frame_entity(irn);
62                 if (entity != NULL) {
63                         int offset = get_entity_offset(entity);
64                         if (sp_relative)
65                                 offset += bias + SPARC_MIN_STACKSIZE;
66                         arch_set_frame_offset(irn, offset);
67                 }
68
69                 /* The additional alignment bytes cannot be used
70                  * anymore after alloca. */
71                 if (is_sparc_SubSP(irn)) {
72                         free_bytes = 0;
73                 } else if (is_sparc_AddSP(irn)) {
74                         assert(free_bytes == 0);
75                 }
76
77                 irn_bias = arch_get_sp_bias(irn);
78                 if (irn_bias == 0) {
79                         /* do nothing */
80                 } else if (irn_bias == SP_BIAS_RESET) {
81                         bias = 0;
82                 } else {
83                         /* adjust values to respect stack alignment */
84                         int new_bias_unaligned;
85                         int new_bias_aligned;
86                         irn_bias -= free_bytes;
87
88                         new_bias_unaligned = bias + irn_bias;
89                         new_bias_aligned
90                                 = round_up2(new_bias_unaligned, SPARC_STACK_ALIGNMENT);
91                         free_bytes = new_bias_aligned - new_bias_unaligned;
92                         set_irn_sp_bias(irn, new_bias_aligned - bias);
93                         bias = new_bias_aligned;
94                 }
95         }
96
97 #ifndef NDEBUG
98         if (block == get_irg_end_block(get_irn_irg(block))) {
99                 assert(bias == 0);
100         }
101 #endif
102
103         /* continue at the successor blocks */
104         foreach_block_succ(block, edge) {
105                 ir_node *succ = get_edge_src_irn(edge);
106                 if (Block_block_visited(succ))
107                         continue;
108                 process_bias(succ, sp_relative, bias, free_bytes);
109         }
110 }
111
112 static void adjust_entity_offsets(ir_type *type, long offset)
113 {
114         size_t n_members = get_compound_n_members(type);
115         size_t i;
116
117         for (i = 0; i < n_members; ++i) {
118                 ir_entity *member        = get_compound_member(type, i);
119                 int        member_offset = get_entity_offset(member);
120                 set_entity_offset(member, member_offset + offset);
121         }
122 }
123
124 /**
125  * Perform some fixups for variadic functions.
126  * To make the rest of the frontend code easier to understand we add
127  * "dummy" parameters until the number of parameters transmitted in registers.
128  * (because otherwise the backend wouldn't store the value of the register
129  *  parameters into memory for the VLA magic)
130  */
131 bool sparc_variadic_fixups(ir_graph *irg, calling_convention_t *cconv)
132 {
133         ir_entity *entity = get_irg_entity(irg);
134         ir_type   *mtp    = get_entity_type(entity);
135         if (get_method_variadicity(mtp) != variadicity_variadic)
136                 return false;
137
138         if (cconv->n_param_regs >= SPARC_N_PARAM_REGS)
139                 return false;
140
141         {
142         size_t         n_params     = get_method_n_params(mtp);
143         type_dbg_info *dbgi         = get_type_dbg_info(mtp);
144         size_t         n_ress       = get_method_n_ress(mtp);
145         size_t         new_n_params
146                 = n_params + (SPARC_N_PARAM_REGS - cconv->n_param_regs);
147         ir_type       *new_mtp      = new_d_type_method(new_n_params, n_ress, dbgi);
148         ir_mode       *gp_reg_mode  = sparc_reg_classes[CLASS_sparc_gp].mode;
149         ir_type       *gp_reg_type  = get_type_for_mode(gp_reg_mode);
150         ir_type       *frame_type   = get_irg_frame_type(irg);
151         size_t         i;
152
153         for (i = 0; i < n_ress; ++i) {
154                 ir_type *type = get_method_res_type(mtp, i);
155                 set_method_res_type(new_mtp, i, type);
156         }
157         for (i = 0; i < n_params; ++i) {
158                 ir_type *type = get_method_param_type(mtp, i);
159                 set_method_param_type(new_mtp, i, type);
160         }
161         for ( ; i < new_n_params; ++i) {
162                 set_method_param_type(new_mtp, i, gp_reg_type);
163                 new_parameter_entity(frame_type, i, gp_reg_type);
164         }
165
166         set_method_variadicity(new_mtp, get_method_variadicity(mtp));
167         set_method_calling_convention(new_mtp, get_method_calling_convention(mtp));
168         set_method_additional_properties(new_mtp, get_method_additional_properties(mtp));
169         set_higher_type(new_mtp, mtp);
170
171         set_entity_type(entity, new_mtp);
172         }
173         return true;
174 }
175
176 static ir_type *compute_arg_type(ir_graph *irg, calling_convention_t *cconv,
177                                  ir_type *between_type)
178 {
179         ir_entity       *va_start_entity = NULL;
180         const ir_entity *entity          = get_irg_entity(irg);
181         const ir_type   *mtp             = get_entity_type(entity);
182         size_t           n_params        = get_method_n_params(mtp);
183         ir_entity      **param_map       = ALLOCANZ(ir_entity*, n_params);
184
185         ir_type *frame_type      = get_irg_frame_type(irg);
186         size_t   n_frame_members = get_compound_n_members(frame_type);
187         size_t   f;
188         size_t   i;
189
190         ir_type *res = new_type_struct(id_mangle_u(get_entity_ident(entity), new_id_from_chars("arg_type", 8)));
191
192         /* search for existing value_param entities */
193         for (f = n_frame_members; f > 0; ) {
194                 ir_entity *member = get_compound_member(frame_type, --f);
195                 size_t     num;
196
197                 if (!is_parameter_entity(member))
198                         continue;
199                 num = get_entity_parameter_number(member);
200                 if (num == IR_VA_START_PARAMETER_NUMBER) {
201                         if (va_start_entity != NULL)
202                                 panic("multiple va_start entities found (%+F,%+F)",
203                                       va_start_entity, member);
204                         va_start_entity = member;
205                         continue;
206                 }
207                 assert(num < n_params);
208                 if (param_map[num] != NULL)
209                         panic("multiple entities for parameter %u in %+F found", f, irg);
210
211                 param_map[num] = member;
212                 /* move to new arg_type */
213                 set_entity_owner(member, res);
214         }
215
216         /* calculate offsets/create missing entities */
217         for (i = 0; i < n_params; ++i) {
218                 reg_or_stackslot_t *param  = &cconv->parameters[i];
219                 ir_entity          *entity = param_map[i];
220
221                 if (param->reg0 != NULL) {
222                         /* use reserved spill space on between type */
223                         if (entity != NULL) {
224                                 long offset = SPARC_PARAMS_SPILL_OFFSET + i * SPARC_REGISTER_SIZE;
225                                 assert(i < SPARC_N_PARAM_REGS);
226                                 set_entity_owner(entity, between_type);
227                                 set_entity_offset(entity, offset);
228                         }
229                         continue;
230                 }
231
232                 if (entity == NULL)
233                         entity = new_parameter_entity(res, i, param->type);
234                 param->entity = entity;
235                 set_entity_offset(entity, param->offset);
236         }
237
238         if (va_start_entity != NULL) {
239                 /* sparc_variadic_fixups() fiddled with our type, find out the
240                  * original number of parameters */
241                 ir_type *non_lowered   = get_higher_type(mtp);
242                 size_t   orig_n_params = get_method_n_params(non_lowered);
243                 long     offset;
244                 assert(get_method_variadicity(mtp) == variadicity_variadic);
245                 if (orig_n_params < n_params) {
246                         assert(param_map[orig_n_params] != NULL);
247                         offset = get_entity_offset(param_map[orig_n_params]);
248                         set_entity_owner(va_start_entity, between_type);
249                         set_entity_offset(va_start_entity, offset);
250                 } else {
251                         set_entity_owner(va_start_entity, res);
252                         set_entity_offset(va_start_entity, cconv->param_stack_size);
253                 }
254         }
255         set_type_size_bytes(res, cconv->param_stack_size);
256
257         return res;
258 }
259
260 void sparc_create_stacklayout(ir_graph *irg, calling_convention_t *cconv)
261 {
262         be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
263         ir_type           *between_type;
264         memset(layout, 0, sizeof(*layout));
265
266         between_type = new_type_class(new_id_from_str("sparc_between_type"));
267         if (cconv->omit_fp) {
268                 set_type_size_bytes(between_type, 0);
269         } else {
270                 set_type_size_bytes(between_type, SPARC_MIN_STACKSIZE);
271         }
272
273         layout->frame_type     = get_irg_frame_type(irg);
274         layout->between_type   = between_type;
275         layout->arg_type       = compute_arg_type(irg, cconv, between_type);
276         layout->initial_offset = 0;
277         layout->initial_bias   = 0;
278         layout->sp_relative    = cconv->omit_fp;
279
280         assert(N_FRAME_TYPES == 3);
281         layout->order[0] = layout->frame_type;
282         layout->order[1] = layout->between_type;
283         layout->order[2] = layout->arg_type;
284 }
285
286 /* Assign entity offsets, to all stack-related entities.
287  * The offsets are relative to the begin of the stack frame.
288  */
289 void sparc_adjust_stack_entity_offsets(ir_graph *irg)
290 {
291         be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
292
293         /* initially the stackpointer points to the begin of our stackframe.
294          * Situation at the begin of our function:
295          *
296          *      high address |-----------------------------|
297          *                   |            ...              |
298          *          arg-type |         stackarg 1          |
299          *                   |         stackarg 0          |
300          *                   |-----------------------------|
301          *                   | space for storing regarg0-5 |
302          *      between type | pointer to aggregate return |
303          *                   |      16 words save are      |
304          *  stack pointer -> |-----------------------------|
305          *                   |    high end of stackframe   |
306          *                   |            ...              |
307          *                   |    low end of stackframe    |
308          *      low address  |-----------------------------|
309          */
310         ir_type *between_type = layout->between_type;
311         unsigned between_size = get_type_size_bytes(between_type);
312
313         ir_type *frame_type  = get_irg_frame_type(irg);
314         unsigned frame_size  = get_type_size_bytes(frame_type);
315         unsigned frame_align = get_type_alignment_bytes(frame_type);
316
317         /* There's the tricky case of the stackframe size not being a multiple
318          * of the alignment. There are 2 variants:
319          *
320          * - frame-pointer relative addressing:
321          *   Increase frame_size in case it is not a multiple of the alignment as we
322          *   address entities from the "top" with negative offsets
323          * - stack-pointer relative addressing:
324          *   Stackframesize + SPARC_MIN_STACK_SIZE has to be aligned. Increase
325          *   frame_size accordingly.
326          */
327         if (!layout->sp_relative) {
328                 frame_size = (frame_size + frame_align-1) & ~(frame_align-1);
329         } else {
330                 unsigned misalign = (SPARC_MIN_STACKSIZE+frame_size) % frame_align;
331                 frame_size += misalign;
332         }
333         set_type_size_bytes(frame_type, frame_size);
334
335         ir_type *arg_type = layout->arg_type;
336
337         adjust_entity_offsets(frame_type, -(long)frame_size);
338         /* no need to adjust between type, it's already at 0 */
339         adjust_entity_offsets(arg_type, between_size);
340 }
341
342 void sparc_fix_stack_bias(ir_graph *irg)
343 {
344         bool sp_relative = be_get_irg_stack_layout(irg)->sp_relative;
345
346         ir_node *start_block = get_irg_start_block(irg);
347
348         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
349         inc_irg_block_visited(irg);
350         process_bias(start_block, sp_relative, 0, 0);
351         ir_free_resources(irg, IR_RESOURCE_BLOCK_VISITED);
352 }