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: */
54 static const arch_register_t* const param_regs[] = {
55 &sparc_registers[REG_I0],
56 &sparc_registers[REG_I1],
57 &sparc_registers[REG_I2],
58 &sparc_registers[REG_I3],
59 &sparc_registers[REG_I4],
60 &sparc_registers[REG_I5],
63 static const arch_register_t* const float_result_regs[] = {
64 &sparc_registers[REG_F0],
65 &sparc_registers[REG_F1],
66 &sparc_registers[REG_F2],
67 &sparc_registers[REG_F3],
71 * Maps an input register representing the i'th register input
72 * to the i'th register output.
74 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
76 unsigned idx = reg->global_index;
77 assert(REG_I0 <= idx && idx <= REG_I7);
78 idx += REG_O0 - REG_I0;
79 assert(REG_O0 <= idx && idx <= REG_O7);
80 return &sparc_registers[idx];
83 static void check_omit_fp(ir_node *node, void *env)
85 bool *can_omit_fp = (bool*) env;
87 /* omit-fp is not possible if:
88 * - we have allocations on the stack
89 * - we have calls (with the exception of tail-calls once we support them)
91 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
92 || (is_Free(node) && get_Free_where(node) == stack_alloc)
98 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
101 unsigned stack_offset = 0;
102 unsigned n_param_regs_used = 0;
103 int n_param_regs = ARRAY_SIZE(param_regs);
104 int n_float_result_regs = ARRAY_SIZE(float_result_regs);
105 bool omit_fp = false;
106 reg_or_stackslot_t *params;
107 reg_or_stackslot_t *results;
113 calling_convention_t *cconv;
116 const be_options_t *options = be_get_irg_options(irg);
117 omit_fp = options->omit_fp;
118 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
121 /* determine how parameters are passed */
122 n_params = get_method_n_params(function_type);
124 params = XMALLOCNZ(reg_or_stackslot_t, n_params);
126 for (i = 0; i < n_params; ++i) {
127 ir_type *param_type = get_method_param_type(function_type,i);
128 ir_mode *mode = get_type_mode(param_type);
129 int bits = get_mode_size_bits(mode);
130 reg_or_stackslot_t *param = ¶ms[i];
132 if (regnum < n_param_regs) {
133 const arch_register_t *reg = param_regs[regnum];
134 if (irg == NULL || omit_fp)
135 reg = map_i_to_o_reg(reg);
137 param->reg_offset = regnum;
140 param->type = param_type;
141 param->offset = stack_offset;
142 /* increase offset 4 bytes so everything is aligned */
143 stack_offset += bits > 32 ? bits/8 : 4;
147 /* we might need a 2nd 32bit component (for 64bit or double values) */
150 panic("only 32 and 64bit modes supported in sparc backend");
152 if (regnum < n_param_regs) {
153 const arch_register_t *reg = param_regs[regnum];
154 if (irg == NULL || omit_fp)
155 reg = map_i_to_o_reg(reg);
157 param->reg_offset = regnum;
160 ir_mode *regmode = param_regs[0]->reg_class->mode;
161 ir_type *type = get_type_for_mode(regmode);
163 param->offset = stack_offset;
164 assert(get_mode_size_bits(regmode) == 32);
169 n_param_regs_used = regnum;
171 /* determine how results are passed */
172 n_results = get_method_n_ress(function_type);
175 results = XMALLOCNZ(reg_or_stackslot_t, n_results);
176 for (i = 0; i < n_results; ++i) {
177 ir_type *result_type = get_method_res_type(function_type, i);
178 ir_mode *result_mode = get_type_mode(result_type);
179 reg_or_stackslot_t *result = &results[i];
181 if (mode_is_float(result_mode)) {
182 if (float_regnum >= n_float_result_regs) {
183 panic("Too many float results for sparc backend");
185 const arch_register_t *reg = float_result_regs[float_regnum++];
189 if (get_mode_size_bits(result_mode) > 32) {
190 panic("Results with more than 32bits not supported by sparc backend yet");
193 if (regnum >= n_param_regs) {
194 panic("Too many results for sparc backend");
196 const arch_register_t *reg = param_regs[regnum++];
197 if (irg == NULL || omit_fp)
198 reg = map_i_to_o_reg(reg);
204 cconv = XMALLOCZ(calling_convention_t);
205 cconv->parameters = params;
206 cconv->param_stack_size = stack_offset;
207 cconv->n_param_regs = n_param_regs_used;
208 cconv->results = results;
209 cconv->omit_fp = omit_fp;
211 /* setup ignore register array */
213 be_irg_t *birg = be_birg_from_irg(irg);
214 size_t n_ignores = ARRAY_SIZE(ignore_regs);
215 struct obstack *obst = &birg->obst;
218 assert(birg->allocatable_regs == NULL);
219 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
220 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
221 for (r = 0; r < n_ignores; ++r) {
222 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
229 void sparc_free_calling_convention(calling_convention_t *cconv)
231 free(cconv->parameters);
232 free(cconv->results);