Change "associated type" logic to a single linked list to the "higher type"
[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  * @version $Id$
25  */
26 #include "config.h"
27
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         const ir_edge_t *edge;
55         ir_node         *irn;
56
57         mark_Block_block_visited(block);
58
59         /* process schedule */
60         sched_foreach(block, irn) {
61                 int irn_bias;
62
63                 /* set bias to nodes with entities */
64                 ir_entity *entity = arch_get_frame_entity(irn);
65                 if (entity != NULL) {
66                         int offset = get_entity_offset(entity);
67                         if (sp_relative)
68                                 offset += bias;
69                         arch_set_frame_offset(irn, offset);
70                 }
71
72                 irn_bias = arch_get_sp_bias(irn);
73                 if (irn_bias == 0) {
74                         /* do nothing */
75                 } else if (irn_bias == SP_BIAS_RESET) {
76                         bias = 0;
77                 } else {
78                         /* adjust values to respect stack alignment */
79                         int new_bias_unaligned;
80                         int new_bias_aligned;
81                         irn_bias -= free_bytes;
82
83                         new_bias_unaligned = bias + irn_bias;
84                         new_bias_aligned
85                                 = round_up2(new_bias_unaligned, SPARC_STACK_ALIGNMENT);
86                         free_bytes = new_bias_aligned - new_bias_unaligned;
87                         set_irn_sp_bias(irn, new_bias_aligned - bias);
88                         bias = new_bias_aligned;
89                 }
90         }
91
92 #ifndef NDEBUG
93         if (block == get_irg_end_block(get_irn_irg(block))) {
94                 assert(bias == 0);
95         }
96 #endif
97
98         /* continue at the successor blocks */
99         foreach_block_succ(block, edge) {
100                 ir_node *succ = get_edge_src_irn(edge);
101                 if (Block_block_visited(succ))
102                         continue;
103                 process_bias(succ, sp_relative, bias, free_bytes);
104         }
105 }
106
107 static void adjust_entity_offsets(ir_type *type, long offset)
108 {
109         size_t n_members = get_compound_n_members(type);
110         size_t i;
111
112         for (i = 0; i < n_members; ++i) {
113                 ir_entity *member        = get_compound_member(type, i);
114                 int        member_offset = get_entity_offset(member);
115                 set_entity_offset(member, member_offset + offset);
116         }
117 }
118
119 /**
120  * Perform some fixups for variadic functions.
121  * To make the rest of the frontend code easier to understand we add
122  * "dummy" parameters until the number of parameters transmitted in registers.
123  * (because otherwise the backend wouldn't store the value of the register
124  *  parameters into memory for the VLA magic)
125  */
126 bool sparc_variadic_fixups(ir_graph *irg, calling_convention_t *cconv)
127 {
128         ir_entity *entity = get_irg_entity(irg);
129         ir_type   *mtp    = get_entity_type(entity);
130         if (get_method_variadicity(mtp) != variadicity_variadic)
131                 return false;
132
133         if (cconv->n_param_regs >= SPARC_N_PARAM_REGS)
134                 return false;
135
136         {
137         size_t         n_params     = get_method_n_params(mtp);
138         type_dbg_info *dbgi         = get_type_dbg_info(mtp);
139         size_t         n_ress       = get_method_n_ress(mtp);
140         size_t         new_n_params
141                 = n_params + (SPARC_N_PARAM_REGS - cconv->n_param_regs);
142         ir_type       *new_mtp      = new_d_type_method(new_n_params, n_ress, dbgi);
143         ir_mode       *gp_reg_mode  = sparc_reg_classes[CLASS_sparc_gp].mode;
144         ir_type       *gp_reg_type  = get_type_for_mode(gp_reg_mode);
145         ir_type       *frame_type   = get_irg_frame_type(irg);
146         size_t         i;
147
148         for (i = 0; i < n_ress; ++i) {
149                 ir_type *type = get_method_res_type(mtp, i);
150                 set_method_res_type(new_mtp, i, type);
151         }
152         for (i = 0; i < n_params; ++i) {
153                 ir_type *type = get_method_param_type(mtp, i);
154                 set_method_param_type(new_mtp, i, type);
155         }
156         for ( ; i < new_n_params; ++i) {
157                 set_method_param_type(new_mtp, i, gp_reg_type);
158                 new_parameter_entity(frame_type, i, gp_reg_type);
159         }
160
161         set_method_variadicity(new_mtp, get_method_variadicity(mtp));
162         set_method_calling_convention(new_mtp, get_method_calling_convention(mtp));
163         set_method_additional_properties(new_mtp, get_method_additional_properties(mtp));
164         set_higher_type(new_mtp, mtp);
165
166         set_entity_type(entity, new_mtp);
167         }
168         return true;
169 }
170
171 static ir_type *compute_arg_type(ir_graph *irg, calling_convention_t *cconv,
172                                  ir_type *between_type)
173 {
174         ir_entity       *va_start_entity = NULL;
175         const ir_entity *entity          = get_irg_entity(irg);
176         const ir_type   *mtp             = get_entity_type(entity);
177         size_t           n_params        = get_method_n_params(mtp);
178         ir_entity      **param_map       = ALLOCANZ(ir_entity*, n_params);
179
180         ir_type *frame_type      = get_irg_frame_type(irg);
181         size_t   n_frame_members = get_compound_n_members(frame_type);
182         size_t   f;
183         size_t   i;
184
185         ir_type *res = new_type_struct(id_mangle_u(get_entity_ident(entity), new_id_from_chars("arg_type", 8)));
186
187         /* search for existing value_param entities */
188         for (f = n_frame_members; f > 0; ) {
189                 ir_entity *member = get_compound_member(frame_type, --f);
190                 size_t     num;
191
192                 if (!is_parameter_entity(member))
193                         continue;
194                 num = get_entity_parameter_number(member);
195                 if (num == IR_VA_START_PARAMETER_NUMBER) {
196                         if (va_start_entity != NULL)
197                                 panic("multiple va_start entities found (%+F,%+F)",
198                                       va_start_entity, member);
199                         va_start_entity = member;
200                         continue;
201                 }
202                 assert(num < n_params);
203                 if (param_map[num] != NULL)
204                         panic("multiple entities for parameter %u in %+F found", f, irg);
205
206                 param_map[num] = member;
207                 /* move to new arg_type */
208                 set_entity_owner(member, res);
209         }
210
211         /* calculate offsets/create missing entities */
212         for (i = 0; i < n_params; ++i) {
213                 reg_or_stackslot_t *param  = &cconv->parameters[i];
214                 ir_entity          *entity = param_map[i];
215
216                 if (param->reg0 != NULL) {
217                         /* use reserved spill space on between type */
218                         if (entity != NULL) {
219                                 long offset = SPARC_PARAMS_SPILL_OFFSET + i*4;
220                                 assert(i < SPARC_N_PARAM_REGS);
221                                 set_entity_owner(entity, between_type);
222                                 set_entity_offset(entity, offset);
223                         }
224                         continue;
225                 }
226
227                 if (entity == NULL)
228                         entity = new_parameter_entity(res, i, param->type);
229                 param->entity = entity;
230                 set_entity_offset(entity, param->offset);
231         }
232
233         if (va_start_entity != NULL) {
234                 /* sparc_variadic_fixups() fiddled with our type, find out the
235                  * original number of parameters */
236                 ir_type *non_lowered   = get_higher_type(mtp);
237                 size_t   orig_n_params = get_method_n_params(non_lowered);
238                 long     offset;
239                 assert(get_method_variadicity(mtp) == variadicity_variadic);
240                 if (orig_n_params < n_params) {
241                         assert(param_map[orig_n_params] != NULL);
242                         offset = get_entity_offset(param_map[orig_n_params]);
243                         set_entity_owner(va_start_entity, between_type);
244                         set_entity_offset(va_start_entity, offset);
245                 } else {
246                         set_entity_owner(va_start_entity, res);
247                         set_entity_offset(va_start_entity, cconv->param_stack_size);
248                 }
249         }
250         set_type_size_bytes(res, cconv->param_stack_size);
251
252         return res;
253 }
254
255 void sparc_create_stacklayout(ir_graph *irg, calling_convention_t *cconv)
256 {
257         be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
258         ir_type           *between_type;
259         memset(layout, 0, sizeof(*layout));
260
261         between_type = new_type_class(new_id_from_str("sparc_between_type"));
262         set_type_size_bytes(between_type, SPARC_MIN_STACKSIZE);
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 static void process_frame_types(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
307         ir_type *arg_type = layout->arg_type;
308
309         adjust_entity_offsets(frame_type, -(long)frame_size);
310         /* no need to adjust between type, it's already at 0 */
311         adjust_entity_offsets(arg_type, between_size);
312 }
313
314 void sparc_fix_stack_bias(ir_graph *irg)
315 {
316         ir_node *start_block = get_irg_start_block(irg);
317
318         process_frame_types(irg);
319
320         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
321         inc_irg_block_visited(irg);
322         process_bias(start_block, be_get_irg_stack_layout(irg)->sp_relative, 0, 0);
323         ir_free_resources(irg, IR_RESOURCE_BLOCK_VISITED);
324 }