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
28 #include "sparc_cconv.h"
35 #include "gen_sparc_regalloc_if.h"
37 static const unsigned ignore_regs[] = {
39 /* reserved for sparc ABI: */
55 * Maps an input register representing the i'th register input
56 * to the i'th register output.
58 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
60 unsigned idx = reg->global_index;
61 assert(REG_I0 <= idx && idx <= REG_I7);
62 idx += REG_O0 - REG_I0;
63 assert(REG_O0 <= idx && idx <= REG_O7);
64 return &sparc_registers[idx];
67 static void check_omit_fp(ir_node *node, void *env)
69 bool *can_omit_fp = (bool*) env;
71 /* omit-fp is not possible if:
72 * - we have allocations on the stack
73 * - we have calls (with the exception of tail-calls once we support them)
75 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
76 || (is_Free(node) && get_Free_where(node) == stack_alloc)
82 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
86 int n_param_regs = ARRAY_SIZE(param_regs);
87 int n_float_result_regs = ARRAY_SIZE(float_result_regs);
89 reg_or_stackslot_t *params;
90 reg_or_stackslot_t *results;
96 calling_convention_t *cconv;
99 const be_options_t *options = be_get_irg_options(irg);
100 omit_fp = options->omit_fp;
101 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
104 /* determine how parameters are passed */
105 n_params = get_method_n_params(function_type);
107 params = XMALLOCNZ(reg_or_stackslot_t, n_params);
109 for (i = 0; i < n_params; ++i) {
110 ir_type *param_type = get_method_param_type(function_type,i);
111 ir_mode *mode = get_type_mode(param_type);
112 int bits = get_mode_size_bits(mode);
113 reg_or_stackslot_t *param = ¶ms[i];
115 if (regnum < n_param_regs) {
116 const arch_register_t *reg = param_regs[regnum++];
117 if (irg == NULL || omit_fp)
118 reg = map_i_to_o_reg(reg);
121 param->type = param_type;
122 param->offset = stack_offset;
123 /* increase offset 4 bytes so everything is aligned */
124 stack_offset += bits > 32 ? bits/8 : 4;
128 /* we might need a 2nd 32bit component (for 64bit or double values) */
131 panic("only 32 and 64bit modes supported in sparc backend");
133 if (regnum < n_param_regs) {
134 const arch_register_t *reg = param_regs[regnum++];
135 if (irg == NULL || omit_fp)
136 reg = map_i_to_o_reg(reg);
139 ir_mode *mode = param_regs[0]->reg_class->mode;
140 ir_type *type = get_type_for_mode(mode);
142 param->offset = stack_offset;
143 assert(get_mode_size_bits(mode) == 32);
149 /* determine how results are passed */
150 n_results = get_method_n_ress(function_type);
153 results = XMALLOCNZ(reg_or_stackslot_t, n_results);
154 for (i = 0; i < n_results; ++i) {
155 ir_type *result_type = get_method_res_type(function_type, i);
156 ir_mode *result_mode = get_type_mode(result_type);
157 reg_or_stackslot_t *result = &results[i];
159 if (mode_is_float(result_mode)) {
160 if (float_regnum >= n_float_result_regs) {
161 panic("Too many float results for sparc backend");
163 const arch_register_t *reg = float_result_regs[float_regnum++];
167 if (get_mode_size_bits(result_mode) > 32) {
168 panic("Results with more than 32bits not supported by sparc backend yet");
171 if (regnum >= n_param_regs) {
172 panic("Too many results for sparc backend");
174 const arch_register_t *reg = param_regs[regnum++];
175 if (irg == NULL || omit_fp)
176 reg = map_i_to_o_reg(reg);
182 cconv = XMALLOCZ(calling_convention_t);
183 cconv->parameters = params;
184 cconv->param_stack_size = stack_offset;
185 cconv->results = results;
186 cconv->omit_fp = omit_fp;
188 /* setup ignore register array */
190 be_irg_t *birg = be_birg_from_irg(irg);
191 size_t n_ignores = ARRAY_SIZE(ignore_regs);
192 struct obstack *obst = &birg->obst;
195 assert(birg->allocatable_regs == NULL);
196 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
197 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
198 for (r = 0; r < n_ignores; ++r) {
199 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
206 void sparc_free_calling_convention(calling_convention_t *cconv)
208 free(cconv->parameters);
209 free(cconv->results);