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