unified main comments
[libfirm] / ir / be / ia32 / ia32_map_regs.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Register param constraints and some other register handling tools.
23  * @author      Christian Wuerdig
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdlib.h>
31
32 #include "pmap.h"
33
34 #include "ia32_map_regs.h"
35 #include "ia32_new_nodes.h"
36 #include "gen_ia32_regalloc_if.h"
37 #include "../benodesets.h"
38
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 */
41
42 /* this is the order of the assigned registers usesd for parameter passing */
43
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]
51 };
52
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]
60 };
61
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]
71 };
72
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]
83 };
84
85
86
87 /* Mapping to store registers in firm nodes */
88
89 struct ia32_irn_reg_assoc {
90         const ir_node *irn;
91         const arch_register_t *reg;
92 };
93
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;
97
98         return x->irn != y->irn;
99 }
100
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;
103         unsigned int hash;
104
105         templ.irn = irn;
106         templ.reg = NULL;
107         hash = nodeset_hash(irn);
108
109         return set_insert(reg_set, &templ, sizeof(templ), hash);
110 }
111
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);
114         assoc->reg = reg;
115 }
116
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);
119         return assoc->reg;
120 }
121
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");
131 }
132
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");
138 }
139
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);
142
143         //assert(e && "missing map init?");
144         if (! e) {
145                 printf("FIXME: ia32map_regs.c:122: returning fake register name for ia32 with 32 register\n");
146                 return reg->name;
147         }
148
149         return e->value;
150 }
151
152 /**
153  * Check all parameters and determine the maximum number of parameters
154  * to pass in gp regs resp. in fp regs.
155  *
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
161  */
162 int ia32_get_n_regparam_class(int n, ir_mode **modes, int *n_int, int *n_float) {
163         int i, finished = 0;
164
165         *n_int   = 0;
166         *n_float = 0;
167
168         for (i = 0; i < n && !finished; i++) {
169                 if (mode_is_int(modes[i]) || mode_is_reference(modes[i])) {
170                         *n_int = *n_int + 1;
171                 }
172                 else if (mode_is_float(modes[i])) {
173                         *n_float = *n_float + 1;
174                 }
175                 else {
176                         finished = 1;
177                 }
178
179                 /* test for maximum */
180                 if (*n_int == maxnum_gpreg_args || *n_float == maxnum_fpreg_args) {
181                         finished = 1;
182                 }
183         }
184
185         return i - 1;
186 }
187
188
189 /**
190  * Returns the register for parameter nr.
191  *
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
197  */
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;
204         int i, done      = 0;
205         int cur_gp_idx   = 0;
206         int cur_fp_idx   = 0;
207         int biggest_n    = ia32_get_n_regparam_class(n, modes, &n_gpregparam, &n_fpregparam);
208
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;
213
214                 if (cc & cc_this_call) {
215                         current_gpreg_param_reg = gpreg_param_reg_this;
216                         current_fpreg_param_reg = fpreg_param_reg_this;
217                 }
218
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 */
223                                 cur_gp_idx++;
224                         }
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");
228                                 cur_fp_idx++;
229                         }
230                 }
231
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];
236                 }
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];
240                 }
241                 else {
242                         assert(0 && "This should not happen!");
243                 }
244         }
245
246         return param_reg;
247 }