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 calling convention helpers
23 * @author Matthias Braun
29 #include "sparc_cconv.h"
36 #include "gen_sparc_regalloc_if.h"
37 #include "bitfiddle.h"
39 static const unsigned ignore_regs[] = {
41 /* used in case an address offset does not fit into an immediate: */
43 /* reserved for SPARC ABI: */
60 static const arch_register_t* const param_regs[] = {
61 &sparc_registers[REG_I0],
62 &sparc_registers[REG_I1],
63 &sparc_registers[REG_I2],
64 &sparc_registers[REG_I3],
65 &sparc_registers[REG_I4],
66 &sparc_registers[REG_I5],
68 COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
70 static const arch_register_t* const float_result_regs[] = {
71 &sparc_registers[REG_F0],
72 &sparc_registers[REG_F1],
73 &sparc_registers[REG_F2],
74 &sparc_registers[REG_F3],
75 &sparc_registers[REG_F4],
76 &sparc_registers[REG_F5],
77 &sparc_registers[REG_F6],
78 &sparc_registers[REG_F7],
80 static arch_register_req_t float_result_reqs_double[8];
81 static arch_register_req_t float_result_reqs_quad[8];
83 static const unsigned caller_saves[] = {
128 static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
130 static const unsigned returns_twice_saved[] = {
135 static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
138 * Maps an input register representing the i'th register input
139 * to the i'th register output.
141 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
143 unsigned idx = reg->global_index;
144 assert(REG_I0 <= idx && idx <= REG_I7);
145 idx += REG_O0 - REG_I0;
146 assert(REG_O0 <= idx && idx <= REG_O7);
147 return &sparc_registers[idx];
150 static void check_omit_fp(ir_node *node, void *env)
152 /* omit-fp is not possible if:
153 * - we have allocations on the stack
154 * - we have calls (with the exception of tail-calls once we support them)
156 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
157 || (is_Free(node) && get_Free_where(node) == stack_alloc)
159 bool *can_omit_fp = (bool*) env;
160 *can_omit_fp = false;
164 static unsigned determine_n_float_regs(ir_mode *mode)
166 unsigned bits = get_mode_size_bits(mode);
175 panic("Unexpected floatingpoint mode %+F", mode);
179 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
182 bool omit_fp = false;
184 omit_fp = be_options.omit_fp;
185 /* our current vaarg handling needs the standard space to store the
187 if (get_method_variadicity(function_type) == variadicity_variadic)
189 if (omit_fp == true) {
190 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
194 mtp_additional_properties mtp
195 = get_method_additional_properties(function_type);
196 unsigned *caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
197 if (mtp & mtp_property_returns_twice) {
198 rbitset_copy(caller_saves, default_returns_twice_saves,
201 rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
204 /* determine how parameters are passed */
205 int n_params = get_method_n_params(function_type);
207 reg_or_stackslot_t *params = XMALLOCNZ(reg_or_stackslot_t, n_params);
209 int n_param_regs = ARRAY_SIZE(param_regs);
210 unsigned stack_offset = 0;
211 for (int i = 0; i < n_params; ++i) {
212 ir_type *param_type = get_method_param_type(function_type,i);
215 reg_or_stackslot_t *param;
217 if (is_compound_type(param_type))
218 panic("compound arguments not supported yet");
220 mode = get_type_mode(param_type);
221 bits = get_mode_size_bits(mode);
225 (get_method_calling_convention(function_type) & cc_compound_ret)) {
226 assert(mode_is_reference(mode) && bits == 32);
227 /* special case, we have reserved space for this on the between
229 param->type = param_type;
230 param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
234 if (regnum < n_param_regs) {
235 const arch_register_t *reg = param_regs[regnum];
236 if (irg == NULL || omit_fp)
237 reg = map_i_to_o_reg(reg);
239 param->req0 = reg->single_req;
240 param->reg_offset = regnum;
243 param->type = param_type;
244 param->offset = stack_offset;
245 /* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
246 stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
250 /* we might need a 2nd 32bit component (for 64bit or double values) */
253 panic("only 32 and 64bit modes supported");
255 if (regnum < n_param_regs) {
256 const arch_register_t *reg = param_regs[regnum];
257 if (irg == NULL || omit_fp)
258 reg = map_i_to_o_reg(reg);
260 param->req1 = reg->single_req;
263 ir_mode *regmode = param_regs[0]->reg_class->mode;
264 ir_type *type = get_type_for_mode(regmode);
266 param->offset = stack_offset;
267 assert(get_mode_size_bits(regmode) == 32);
268 stack_offset += SPARC_REGISTER_SIZE;
272 unsigned n_param_regs_used = regnum;
274 /* determine how results are passed */
275 int n_results = get_method_n_ress(function_type);
276 unsigned float_regnum = 0;
277 unsigned n_reg_results = 0;
278 unsigned n_float_result_regs = ARRAY_SIZE(float_result_regs);
279 reg_or_stackslot_t *results = XMALLOCNZ(reg_or_stackslot_t, n_results);
281 for (int i = 0; i < n_results; ++i) {
282 ir_type *result_type = get_method_res_type(function_type, i);
283 ir_mode *result_mode = get_type_mode(result_type);
284 reg_or_stackslot_t *result = &results[i];
286 if (mode_is_float(result_mode)) {
287 unsigned n_regs = determine_n_float_regs(result_mode);
288 unsigned next_reg = round_up2(float_regnum, n_regs);
290 if (next_reg >= n_float_result_regs) {
291 panic("Too many float results");
293 const arch_register_t *reg = float_result_regs[next_reg];
294 rbitset_clear(caller_saves, reg->global_index);
295 result->reg_offset = i;
297 result->req0 = reg->single_req;
298 } else if (n_regs == 2) {
299 result->req0 = &float_result_reqs_double[next_reg];
300 rbitset_clear(caller_saves, reg->global_index+1);
301 } else if (n_regs == 4) {
302 result->req0 = &float_result_reqs_quad[next_reg];
303 rbitset_clear(caller_saves, reg->global_index+1);
304 rbitset_clear(caller_saves, reg->global_index+2);
305 rbitset_clear(caller_saves, reg->global_index+3);
307 panic("invalid number of registers in result");
309 float_regnum = next_reg + n_regs;
314 if (get_mode_size_bits(result_mode) > 32) {
315 panic("Results with more than 32bits not supported yet");
318 if (regnum >= n_param_regs) {
319 panic("Too many results");
321 const arch_register_t *reg = param_regs[regnum++];
322 if (irg == NULL || omit_fp)
323 reg = map_i_to_o_reg(reg);
324 result->req0 = reg->single_req;
325 result->reg_offset = i;
326 rbitset_clear(caller_saves, reg->global_index);
332 calling_convention_t *cconv = XMALLOCZ(calling_convention_t);
333 cconv->parameters = params;
334 cconv->param_stack_size = stack_offset;
335 cconv->n_param_regs = n_param_regs_used;
336 cconv->results = results;
337 cconv->omit_fp = omit_fp;
338 cconv->caller_saves = caller_saves;
339 cconv->n_reg_results = n_reg_results;
341 /* setup ignore register array */
343 be_irg_t *birg = be_birg_from_irg(irg);
344 size_t n_ignores = ARRAY_SIZE(ignore_regs);
345 struct obstack *obst = &birg->obst;
348 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
349 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
350 for (r = 0; r < n_ignores; ++r) {
351 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
358 void sparc_free_calling_convention(calling_convention_t *cconv)
360 free(cconv->parameters);
361 free(cconv->results);
362 free(cconv->caller_saves);
366 void sparc_cconv_init(void)
368 for (size_t i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
369 rbitset_set(default_caller_saves, caller_saves[i]);
372 rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
373 for (size_t i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
374 rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
376 for (size_t i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
377 rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
380 for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
381 arch_register_req_t *req = &float_result_reqs_double[i];
382 *req = *float_result_regs[i]->single_req;
383 req->type |= arch_register_req_type_aligned;
386 for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
387 arch_register_req_t *req = &float_result_reqs_quad[i];
388 *req = *float_result_regs[i]->single_req;
389 req->type |= arch_register_req_type_aligned;