2 * Copyright (C) 1995-2007 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 Register param constraints and some other register handling tools.
23 * @author Christian Wuerdig
34 #include "ia32_map_regs.h"
35 #include "ia32_new_nodes.h"
36 #include "gen_ia32_regalloc_if.h"
37 #include "../benodesets.h"
39 static int maxnum_gpreg_args = 3; /* maximum number of int arguments passed in registers; default 3 */
40 static int maxnum_fpreg_args = 5; /* maximum number of float arguments passed in registers; default 5 */
42 /* this is the order of the assigned registers usesd for parameter passing */
44 const arch_register_t *gpreg_param_reg_std[] = {
45 &ia32_gp_regs[REG_EAX],
46 &ia32_gp_regs[REG_EDX],
47 &ia32_gp_regs[REG_ECX],
48 &ia32_gp_regs[REG_EBX],
49 &ia32_gp_regs[REG_EDI],
50 &ia32_gp_regs[REG_ESI]
53 const arch_register_t *gpreg_param_reg_this[] = {
54 &ia32_gp_regs[REG_ECX],
55 &ia32_gp_regs[REG_EAX],
56 &ia32_gp_regs[REG_EDX],
57 &ia32_gp_regs[REG_EBX],
58 &ia32_gp_regs[REG_EDI],
59 &ia32_gp_regs[REG_ESI]
62 const arch_register_t *fpreg_param_reg_std[] = {
63 &ia32_xmm_regs[REG_XMM0],
64 &ia32_xmm_regs[REG_XMM1],
65 &ia32_xmm_regs[REG_XMM2],
66 &ia32_xmm_regs[REG_XMM3],
67 &ia32_xmm_regs[REG_XMM4],
68 &ia32_xmm_regs[REG_XMM5],
69 &ia32_xmm_regs[REG_XMM6],
70 &ia32_xmm_regs[REG_XMM7]
73 const arch_register_t *fpreg_param_reg_this[] = {
74 NULL, /* in case of a "this" pointer, the first parameter must not be a float */
75 &ia32_xmm_regs[REG_XMM0],
76 &ia32_xmm_regs[REG_XMM1],
77 &ia32_xmm_regs[REG_XMM2],
78 &ia32_xmm_regs[REG_XMM3],
79 &ia32_xmm_regs[REG_XMM4],
80 &ia32_xmm_regs[REG_XMM5],
81 &ia32_xmm_regs[REG_XMM6],
82 &ia32_xmm_regs[REG_XMM7]
87 /* Mapping to store registers in firm nodes */
89 struct ia32_irn_reg_assoc {
91 const arch_register_t *reg;
94 int ia32_cmp_irn_reg_assoc(const void *a, const void *b, size_t len) {
95 const struct ia32_irn_reg_assoc *x = a;
96 const struct ia32_irn_reg_assoc *y = b;
98 return x->irn != y->irn;
101 static struct ia32_irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, set *reg_set) {
102 struct ia32_irn_reg_assoc templ;
107 hash = nodeset_hash(irn);
109 return set_insert(reg_set, &templ, sizeof(templ), hash);
112 void ia32_set_firm_reg(ir_node *irn, const arch_register_t *reg, set *reg_set) {
113 struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
117 const arch_register_t *ia32_get_firm_reg(const ir_node *irn, set *reg_set) {
118 struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
122 void ia32_build_16bit_reg_map(pmap *reg_map) {
123 pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "ax");
124 pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bx");
125 pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "cx");
126 pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dx");
127 pmap_insert(reg_map, &ia32_gp_regs[REG_ESI], "si");
128 pmap_insert(reg_map, &ia32_gp_regs[REG_EDI], "di");
129 pmap_insert(reg_map, &ia32_gp_regs[REG_EBP], "bp");
130 pmap_insert(reg_map, &ia32_gp_regs[REG_ESP], "sp");
133 void ia32_build_8bit_reg_map(pmap *reg_map) {
134 pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "al");
135 pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bl");
136 pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "cl");
137 pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dl");
140 const char *ia32_get_mapped_reg_name(pmap *reg_map, const arch_register_t *reg) {
141 pmap_entry *e = pmap_find(reg_map, (void *)reg);
143 //assert(e && "missing map init?");
145 printf("FIXME: ia32map_regs.c:122: returning fake register name for ia32 with 32 register\n");
153 * Check all parameters and determine the maximum number of parameters
154 * to pass in gp regs resp. in fp regs.
156 * @param n The number of parameters
157 * @param modes The list of the parameter modes
158 * @param n_int Holds the number of int parameters to be passed in regs after the call
159 * @param n_float Holds the number of float parameters to be passed in regs after the call
160 * @return The number of the last parameter to be passed in register
162 int ia32_get_n_regparam_class(int n, ir_mode **modes, int *n_int, int *n_float) {
168 for (i = 0; i < n && !finished; i++) {
169 if (mode_is_int(modes[i]) || mode_is_reference(modes[i])) {
172 else if (mode_is_float(modes[i])) {
173 *n_float = *n_float + 1;
179 /* test for maximum */
180 if (*n_int == maxnum_gpreg_args || *n_float == maxnum_fpreg_args) {
190 * Returns the register for parameter nr.
192 * @param n The number of parameters
193 * @param modes The list of the parameter modes
194 * @param nr The number of the parameter to return the requirements for
195 * @param cc The calling convention
196 * @return The register
198 const arch_register_t *ia32_get_RegParam_reg(int n, ir_mode **modes, long nr, unsigned cc) {
199 const arch_register_t **current_gpreg_param_reg;
200 const arch_register_t **current_fpreg_param_reg;
201 const arch_register_t *param_reg = NULL;
202 int n_gpregparam = 0;
203 int n_fpregparam = 0;
207 int biggest_n = ia32_get_n_regparam_class(n, modes, &n_gpregparam, &n_fpregparam);
209 /* Check if parameter #nr is in range for passing in register */
210 if (nr <= biggest_n) {
211 current_gpreg_param_reg = gpreg_param_reg_std;
212 current_fpreg_param_reg = fpreg_param_reg_std;
214 if (cc & cc_this_call) {
215 current_gpreg_param_reg = gpreg_param_reg_this;
216 current_fpreg_param_reg = fpreg_param_reg_this;
219 /* loop over all parameters and determine whether its a int or float register parameter */
220 for (i = 0; i < nr && !done && (cc & cc_reg_param); i++) {
221 if ((mode_is_int(modes[i]) || mode_is_reference(modes[i])) && cur_gp_idx < maxnum_gpreg_args) {
222 /* param can be passed in general purpose register and we have some registers left */
225 else if (mode_is_float(modes[i]) && cur_fp_idx < maxnum_fpreg_args) {
226 /* param can be passed in floating point register and we have some registers left */
227 assert(current_gpreg_param_reg[cur_fp_idx] && "'this' pointer cannot be passed as float");
232 /* now: i == nr, that's the parameter requirement we want */
233 if ((mode_is_int(modes[i]) || mode_is_reference(modes[i])) && cur_gp_idx < maxnum_gpreg_args) {
234 /* parameter #nr can be passed in general purpose register */
235 param_reg = current_gpreg_param_reg[i];
237 else if (mode_is_float(modes[i]) && cur_fp_idx < maxnum_fpreg_args) {
238 /* parameter #nr can be passed in floating point register */
239 param_reg = current_fpreg_param_reg[i];
242 assert(0 && "This should not happen!");