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