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