2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Manage addressing into the stackframe
23 * @author Matthias Braun
28 #include "firm_types.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"
38 static void set_irn_sp_bias(ir_node *node, int new_bias)
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;
51 static void process_bias(ir_node *block, bool sp_relative, int bias,
54 const ir_edge_t *edge;
57 mark_Block_block_visited(block);
59 /* process schedule */
60 sched_foreach(block, irn) {
63 /* set bias to nodes with entities */
64 ir_entity *entity = arch_get_frame_entity(irn);
66 int offset = get_entity_offset(entity);
69 arch_set_frame_offset(irn, offset);
72 irn_bias = arch_get_sp_bias(irn);
75 } else if (irn_bias == SP_BIAS_RESET) {
78 /* adjust values to respect stack alignment */
79 int new_bias_unaligned;
81 irn_bias -= free_bytes;
83 new_bias_unaligned = bias + irn_bias;
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;
93 if (block == get_irg_end_block(get_irn_irg(block))) {
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))
103 process_bias(succ, sp_relative, bias, free_bytes);
107 static void adjust_entity_offsets(ir_type *type, long offset)
109 size_t n_members = get_compound_n_members(type);
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);
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)
126 bool sparc_variadic_fixups(ir_graph *irg, calling_convention_t *cconv)
128 ir_entity *entity = get_irg_entity(irg);
129 ir_type *mtp = get_entity_type(entity);
130 if (get_method_variadicity(mtp) != variadicity_variadic)
133 if (cconv->n_param_regs >= SPARC_N_PARAM_REGS)
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);
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);
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);
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);
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);
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);
166 set_entity_type(entity, new_mtp);
171 static ir_type *compute_arg_type(ir_graph *irg, calling_convention_t *cconv,
172 ir_type *between_type)
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);
180 ir_type *frame_type = get_irg_frame_type(irg);
181 size_t n_frame_members = get_compound_n_members(frame_type);
185 ir_type *res = new_type_struct(id_mangle_u(get_entity_ident(entity), new_id_from_chars("arg_type", 8)));
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);
192 if (!is_parameter_entity(member))
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;
202 assert(num < n_params);
203 if (param_map[num] != NULL)
204 panic("multiple entities for parameter %u in %+F found", f, irg);
206 param_map[num] = member;
207 /* move to new arg_type */
208 set_entity_owner(member, res);
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];
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 * SPARC_REGISTER_SIZE;
220 assert(i < SPARC_N_PARAM_REGS);
221 set_entity_owner(entity, between_type);
222 set_entity_offset(entity, offset);
228 entity = new_parameter_entity(res, i, param->type);
229 param->entity = entity;
230 set_entity_offset(entity, param->offset);
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);
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);
246 set_entity_owner(va_start_entity, res);
247 set_entity_offset(va_start_entity, cconv->param_stack_size);
250 set_type_size_bytes(res, cconv->param_stack_size);
255 void sparc_create_stacklayout(ir_graph *irg, calling_convention_t *cconv)
257 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
258 ir_type *between_type;
259 memset(layout, 0, sizeof(*layout));
261 between_type = new_type_class(new_id_from_str("sparc_between_type"));
262 set_type_size_bytes(between_type, SPARC_MIN_STACKSIZE);
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;
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;
277 /* Assign entity offsets, to all stack-related entities.
278 * The offsets are relative to the begin of the stack frame.
280 static void process_frame_types(ir_graph *irg)
282 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
284 /* initially the stackpointer points to the begin of our stackframe.
285 * Situation at the begin of our function:
287 * high address |-----------------------------|
289 * arg-type | stackarg 1 |
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 |
298 * | low end of stackframe |
299 * low address |-----------------------------|
301 ir_type *between_type = layout->between_type;
302 unsigned between_size = get_type_size_bytes(between_type);
304 ir_type *frame_type = get_irg_frame_type(irg);
305 unsigned frame_size = get_type_size_bytes(frame_type);
307 ir_type *arg_type = layout->arg_type;
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);
314 void sparc_fix_stack_bias(ir_graph *irg)
316 ir_node *start_block = get_irg_start_block(irg);
318 process_frame_types(irg);
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);