2 * Copyright (C) 1995-2008 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
27 #include "arm_cconv.h"
35 static const unsigned ignore_regs[] = {
42 static const arch_register_t* const param_regs[] = {
43 &arm_registers[REG_R0],
44 &arm_registers[REG_R1],
45 &arm_registers[REG_R2],
46 &arm_registers[REG_R3]
49 static const arch_register_t* const result_regs[] = {
50 &arm_registers[REG_R0],
51 &arm_registers[REG_R1],
52 &arm_registers[REG_R2],
53 &arm_registers[REG_R3]
56 static const arch_register_t* const float_result_regs[] = {
57 &arm_registers[REG_F0],
58 &arm_registers[REG_F1]
61 calling_convention_t *arm_decide_calling_convention(const ir_graph *irg,
62 ir_type *function_type)
64 unsigned stack_offset = 0;
65 unsigned n_param_regs_used = 0;
66 reg_or_stackslot_t *params;
67 reg_or_stackslot_t *results;
68 size_t const n_param_regs = ARRAY_SIZE(param_regs);
69 size_t const n_result_regs = ARRAY_SIZE(result_regs);
70 size_t const n_float_result_regs = ARRAY_SIZE(float_result_regs);
76 calling_convention_t *cconv;
78 /* determine how parameters are passed */
79 n_params = get_method_n_params(function_type);
81 params = XMALLOCNZ(reg_or_stackslot_t, n_params);
83 for (i = 0; i < n_params; ++i) {
84 ir_type *param_type = get_method_param_type(function_type,i);
85 ir_mode *mode = get_type_mode(param_type);
86 int bits = get_mode_size_bits(mode);
87 reg_or_stackslot_t *param = ¶ms[i];
88 param->type = param_type;
90 if (regnum < n_param_regs) {
91 const arch_register_t *reg = param_regs[regnum++];
94 param->offset = stack_offset;
95 /* increase offset 4 bytes so everything is aligned */
96 stack_offset += bits > 32 ? bits/8 : 4;
100 /* we might need a 2nd 32bit component (for 64bit or double values) */
103 panic("only 32 and 64bit modes supported");
105 if (regnum < n_param_regs) {
106 const arch_register_t *reg = param_regs[regnum++];
109 ir_mode *pmode = param_regs[0]->reg_class->mode;
110 ir_type *type = get_type_for_mode(pmode);
112 param->offset = stack_offset;
113 assert(get_mode_size_bits(pmode) == 32);
118 n_param_regs_used = regnum;
120 n_results = get_method_n_ress(function_type);
123 results = XMALLOCNZ(reg_or_stackslot_t, n_results);
124 for (i = 0; i < n_results; ++i) {
125 ir_type *result_type = get_method_res_type(function_type, i);
126 ir_mode *result_mode = get_type_mode(result_type);
127 reg_or_stackslot_t *result = &results[i];
129 if (mode_is_float(result_mode)) {
130 if (float_regnum >= n_float_result_regs) {
131 panic("Too many float results");
133 const arch_register_t *reg = float_result_regs[float_regnum++];
137 if (get_mode_size_bits(result_mode) > 32) {
138 panic("Results with more than 32bits not supported yet");
141 if (regnum >= n_result_regs) {
142 panic("Too many results");
144 const arch_register_t *reg = result_regs[regnum++];
150 cconv = XMALLOCZ(calling_convention_t);
151 cconv->parameters = params;
152 cconv->param_stack_size = stack_offset;
153 cconv->n_reg_params = n_param_regs_used;
154 cconv->results = results;
156 /* setup allocatable registers */
158 be_irg_t *birg = be_birg_from_irg(irg);
159 size_t n_ignores = ARRAY_SIZE(ignore_regs);
160 struct obstack *obst = &birg->obst;
163 assert(birg->allocatable_regs == NULL);
164 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_ARM_REGISTERS);
165 rbitset_set_all(birg->allocatable_regs, N_ARM_REGISTERS);
166 for (r = 0; r < n_ignores; ++r) {
167 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
174 void arm_free_calling_convention(calling_convention_t *cconv)
176 free(cconv->parameters);
177 free(cconv->results);