2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief calling convention helpers
9 * @author Matthias Braun
15 #include "sparc_cconv.h"
22 #include "gen_sparc_regalloc_if.h"
23 #include "bitfiddle.h"
25 static const unsigned ignore_regs[] = {
27 /* used in case an address offset does not fit into an immediate: */
29 /* reserved for SPARC ABI: */
46 static const arch_register_t* const param_regs[] = {
47 &sparc_registers[REG_I0],
48 &sparc_registers[REG_I1],
49 &sparc_registers[REG_I2],
50 &sparc_registers[REG_I3],
51 &sparc_registers[REG_I4],
52 &sparc_registers[REG_I5],
54 COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
56 static const arch_register_t* const float_result_regs[] = {
57 &sparc_registers[REG_F0],
58 &sparc_registers[REG_F1],
59 &sparc_registers[REG_F2],
60 &sparc_registers[REG_F3],
61 &sparc_registers[REG_F4],
62 &sparc_registers[REG_F5],
63 &sparc_registers[REG_F6],
64 &sparc_registers[REG_F7],
66 static arch_register_req_t float_result_reqs_double[8];
67 static arch_register_req_t float_result_reqs_quad[8];
69 static const unsigned caller_saves[] = {
114 static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
116 static const unsigned returns_twice_saved[] = {
121 static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
124 * Maps an input register representing the i'th register input
125 * to the i'th register output.
127 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
129 unsigned idx = reg->global_index;
130 assert(REG_I0 <= idx && idx <= REG_I7);
131 idx += REG_O0 - REG_I0;
132 assert(REG_O0 <= idx && idx <= REG_O7);
133 return &sparc_registers[idx];
136 static void check_omit_fp(ir_node *node, void *env)
138 /* omit-fp is not possible if:
139 * - we have allocations on the stack
140 * - we have calls (with the exception of tail-calls once we support them)
142 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
143 || (is_Free(node) && get_Free_where(node) == stack_alloc)
145 bool *can_omit_fp = (bool*) env;
146 *can_omit_fp = false;
150 static unsigned determine_n_float_regs(ir_mode *mode)
152 unsigned bits = get_mode_size_bits(mode);
161 panic("Unexpected floatingpoint mode %+F", mode);
165 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
168 bool omit_fp = false;
170 omit_fp = be_options.omit_fp;
171 /* our current vaarg handling needs the standard space to store the
173 if (get_method_variadicity(function_type) == variadicity_variadic)
175 if (omit_fp == true) {
176 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
180 mtp_additional_properties mtp
181 = get_method_additional_properties(function_type);
182 unsigned *caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
183 if (mtp & mtp_property_returns_twice) {
184 rbitset_copy(caller_saves, default_returns_twice_saves,
187 rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
190 /* determine how parameters are passed */
191 int n_params = get_method_n_params(function_type);
193 reg_or_stackslot_t *params = XMALLOCNZ(reg_or_stackslot_t, n_params);
195 int n_param_regs = ARRAY_SIZE(param_regs);
196 unsigned stack_offset = 0;
197 for (int i = 0; i < n_params; ++i) {
198 ir_type *param_type = get_method_param_type(function_type,i);
201 reg_or_stackslot_t *param;
203 if (is_compound_type(param_type))
204 panic("compound arguments not supported yet");
206 mode = get_type_mode(param_type);
207 bits = get_mode_size_bits(mode);
211 (get_method_calling_convention(function_type) & cc_compound_ret)) {
212 assert(mode_is_reference(mode) && bits == 32);
213 /* special case, we have reserved space for this on the between
215 param->type = param_type;
216 param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
220 if (regnum < n_param_regs) {
221 const arch_register_t *reg = param_regs[regnum];
222 if (irg == NULL || omit_fp)
223 reg = map_i_to_o_reg(reg);
225 param->req0 = reg->single_req;
226 param->reg_offset = regnum;
229 param->type = param_type;
230 param->offset = stack_offset;
231 /* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
232 stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
236 /* we might need a 2nd 32bit component (for 64bit or double values) */
239 panic("only 32 and 64bit modes supported");
241 if (regnum < n_param_regs) {
242 const arch_register_t *reg = param_regs[regnum];
243 if (irg == NULL || omit_fp)
244 reg = map_i_to_o_reg(reg);
246 param->req1 = reg->single_req;
249 ir_mode *regmode = param_regs[0]->reg_class->mode;
250 ir_type *type = get_type_for_mode(regmode);
252 param->offset = stack_offset;
253 assert(get_mode_size_bits(regmode) == 32);
254 stack_offset += SPARC_REGISTER_SIZE;
258 unsigned n_param_regs_used = regnum;
260 /* determine how results are passed */
261 int n_results = get_method_n_ress(function_type);
262 unsigned float_regnum = 0;
263 unsigned n_reg_results = 0;
264 unsigned n_float_result_regs = ARRAY_SIZE(float_result_regs);
265 reg_or_stackslot_t *results = XMALLOCNZ(reg_or_stackslot_t, n_results);
267 for (int i = 0; i < n_results; ++i) {
268 ir_type *result_type = get_method_res_type(function_type, i);
269 ir_mode *result_mode = get_type_mode(result_type);
270 reg_or_stackslot_t *result = &results[i];
272 if (mode_is_float(result_mode)) {
273 unsigned n_regs = determine_n_float_regs(result_mode);
274 unsigned next_reg = round_up2(float_regnum, n_regs);
276 if (next_reg >= n_float_result_regs) {
277 panic("Too many float results");
279 const arch_register_t *reg = float_result_regs[next_reg];
280 rbitset_clear(caller_saves, reg->global_index);
281 result->reg_offset = i;
283 result->req0 = reg->single_req;
284 } else if (n_regs == 2) {
285 result->req0 = &float_result_reqs_double[next_reg];
286 rbitset_clear(caller_saves, reg->global_index+1);
287 } else if (n_regs == 4) {
288 result->req0 = &float_result_reqs_quad[next_reg];
289 rbitset_clear(caller_saves, reg->global_index+1);
290 rbitset_clear(caller_saves, reg->global_index+2);
291 rbitset_clear(caller_saves, reg->global_index+3);
293 panic("invalid number of registers in result");
295 float_regnum = next_reg + n_regs;
300 if (get_mode_size_bits(result_mode) > 32) {
301 panic("Results with more than 32bits not supported yet");
304 if (regnum >= n_param_regs) {
305 panic("Too many results");
307 const arch_register_t *reg = param_regs[regnum++];
308 if (irg == NULL || omit_fp)
309 reg = map_i_to_o_reg(reg);
310 result->req0 = reg->single_req;
311 result->reg_offset = i;
312 rbitset_clear(caller_saves, reg->global_index);
318 calling_convention_t *cconv = XMALLOCZ(calling_convention_t);
319 cconv->parameters = params;
320 cconv->param_stack_size = stack_offset;
321 cconv->n_param_regs = n_param_regs_used;
322 cconv->results = results;
323 cconv->omit_fp = omit_fp;
324 cconv->caller_saves = caller_saves;
325 cconv->n_reg_results = n_reg_results;
327 /* setup ignore register array */
329 be_irg_t *birg = be_birg_from_irg(irg);
330 size_t n_ignores = ARRAY_SIZE(ignore_regs);
331 struct obstack *obst = &birg->obst;
334 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
335 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
336 for (r = 0; r < n_ignores; ++r) {
337 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
344 void sparc_free_calling_convention(calling_convention_t *cconv)
346 free(cconv->parameters);
347 free(cconv->results);
348 free(cconv->caller_saves);
352 void sparc_cconv_init(void)
354 for (size_t i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
355 rbitset_set(default_caller_saves, caller_saves[i]);
358 rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
359 for (size_t i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
360 rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
362 for (size_t i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
363 rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
366 for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
367 arch_register_req_t *req = &float_result_reqs_double[i];
368 *req = *float_result_regs[i]->single_req;
369 req->type |= arch_register_req_type_aligned;
372 for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
373 arch_register_req_t *req = &float_result_reqs_quad[i];
374 *req = *float_result_regs[i]->single_req;
375 req->type |= arch_register_req_type_aligned;