fixed some minor bugs
[libfirm] / ir / be / ia32 / ia32_map_regs.c
1 /**
2  * Register mapping for firm nodes. Stolen from bearch_firm :)
3  * $Id$
4  */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include <stdlib.h>
10
11 #include "ia32_map_regs.h"
12 #include "ia32_new_nodes.h"
13 #include "gen_ia32_regalloc_if.h"
14
15 static int maxnum_gpreg_args = 3;   /* maximum number of int arguments passed in registers; default 3 */
16 static int maxnum_fpreg_args = 5;   /* maximum number of float arguments passed in registers; default 5 */
17
18 /* this is the order of the assigned registers usesd for parameter passing */
19
20 const arch_register_t *gpreg_param_reg_std[] = {
21         &ia32_gp_regs[REG_EAX],
22         &ia32_gp_regs[REG_EDX],
23         &ia32_gp_regs[REG_ECX],
24         &ia32_gp_regs[REG_EBX],
25         &ia32_gp_regs[REG_EDI],
26         &ia32_gp_regs[REG_ESI]
27 };
28
29 const arch_register_t *gpreg_param_reg_this[] = {
30         &ia32_gp_regs[REG_ECX],
31         &ia32_gp_regs[REG_EAX],
32         &ia32_gp_regs[REG_EDX],
33         &ia32_gp_regs[REG_EBX],
34         &ia32_gp_regs[REG_EDI],
35         &ia32_gp_regs[REG_ESI]
36 };
37
38 const arch_register_t *fpreg_param_reg_std[] = {
39         &ia32_fp_regs[REG_XMM0],
40         &ia32_fp_regs[REG_XMM1],
41         &ia32_fp_regs[REG_XMM2],
42         &ia32_fp_regs[REG_XMM3],
43         &ia32_fp_regs[REG_XMM4],
44         &ia32_fp_regs[REG_XMM5],
45         &ia32_fp_regs[REG_XMM6],
46         &ia32_fp_regs[REG_XMM7]
47 };
48
49 const arch_register_t *fpreg_param_reg_this[] = {
50         NULL,  /* in case of a "this" pointer, the first parameter must not be a float */
51         &ia32_fp_regs[REG_XMM0],
52         &ia32_fp_regs[REG_XMM1],
53         &ia32_fp_regs[REG_XMM2],
54         &ia32_fp_regs[REG_XMM3],
55         &ia32_fp_regs[REG_XMM4],
56         &ia32_fp_regs[REG_XMM5],
57         &ia32_fp_regs[REG_XMM6],
58         &ia32_fp_regs[REG_XMM7]
59 };
60
61
62
63 /* Mapping to store registers in firm nodes */
64
65 struct ia32_irn_reg_assoc {
66         const ir_node *irn;
67         const arch_register_t *reg;
68 };
69
70 int ia32_cmp_irn_reg_assoc(const void *a, const void *b, size_t len) {
71         const struct ia32_irn_reg_assoc *x = a;
72         const struct ia32_irn_reg_assoc *y = b;
73
74         return x->irn != y->irn;
75 }
76
77 static struct ia32_irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, set *reg_set) {
78         struct ia32_irn_reg_assoc templ;
79         unsigned int hash;
80
81         templ.irn = irn;
82         templ.reg = NULL;
83         hash = HASH_PTR(irn);
84
85         return set_insert(reg_set, &templ, sizeof(templ), hash);
86 }
87
88 void ia32_set_firm_reg(ir_node *irn, const arch_register_t *reg, set *reg_set) {
89         struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
90         assoc->reg = reg;
91 }
92
93 const arch_register_t *ia32_get_firm_reg(const ir_node *irn, set *reg_set) {
94         struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
95         return assoc->reg;
96 }
97
98
99
100 /**
101  * Check all parameters and determine the maximum number of parameters
102  * to pass in gp regs resp. in fp regs.
103  *
104  * @param n       The number of parameters
105  * @param modes   The list of the parameter modes
106  * @param n_int   Holds the number of int parameters to be passed in regs after the call
107  * @param n_float Holds the number of float parameters to be passed in regs after the call
108  * @return        The number of the last parameter to be passed in register
109  */
110 int ia32_get_n_regparam_class(int n, ir_mode **modes, int *n_int, int *n_float) {
111         int i, finished = 0;
112
113         for (i = 0; i < n && !finished; i++) {
114                 if (mode_is_int(modes[i])) {
115                         *n_int = *n_int + 1;
116                 }
117                 else if (mode_is_float(modes[i])) {
118                         *n_float = *n_float + 1;
119                 }
120                 else {
121                         finished = 1;
122                 }
123
124                 /* test for maximum */
125                 if (*n_int == maxnum_gpreg_args || *n_float == maxnum_fpreg_args) {
126                         finished = 1;
127                 }
128         }
129
130         return i - 1;
131 }
132
133
134 /**
135  * Returns the register for parameter nr.
136  *
137  * @param n     The number of parameters
138  * @param modes The list of the parameter modes
139  * @param nr    The number of the parameter to return the requirements for
140  * @param cc    The calling convention
141  * @return      The register
142  */
143 const arch_register_t *ia32_get_RegParam_reg(int n, ir_mode **modes, long nr, unsigned cc) {
144         const arch_register_t **current_gpreg_param_reg;
145         const arch_register_t **current_fpreg_param_reg;
146         const arch_register_t  *param_reg = NULL;
147         int n_gpregparam = 0;
148         int n_fpregparam = 0;
149         int i, done      = 0;
150         int cur_gp_idx   = 0;
151         int cur_fp_idx   = 0;
152         int biggest_n    = ia32_get_n_regparam_class(n, modes, &n_gpregparam, &n_fpregparam);
153
154         /* Check if parameter #nr is in range for passing in register */
155         if (nr <= biggest_n) {
156                 current_gpreg_param_reg = gpreg_param_reg_std;
157                 current_fpreg_param_reg = fpreg_param_reg_std;
158
159                 if (cc & cc_this_call) {
160                         current_gpreg_param_reg = gpreg_param_reg_this;
161                         current_fpreg_param_reg = fpreg_param_reg_this;
162                 }
163
164                 /* loop over all parameters and determine whether its a int or float register parameter */
165                 for (i = 0; i < nr && !done && (cc & cc_reg_param); i++) {
166                         if (mode_is_int(modes[i]) && cur_gp_idx < maxnum_gpreg_args) {
167                                 /* param can be passed in general purpose register and we have some registers left */
168                                 cur_gp_idx++;
169                         }
170                         else if (mode_is_float(modes[i]) && cur_fp_idx < maxnum_fpreg_args) {
171                                 /* param can be passed in floating point register and we have some registers left */
172                                 assert(current_gpreg_param_reg[cur_fp_idx] && "'this' pointer cannot be passed as float");
173                                 cur_fp_idx++;
174                         }
175                 }
176
177                 /* now: i == nr, that's the parameter requirement we want */
178                 if (mode_is_int(modes[i]) && cur_gp_idx < maxnum_gpreg_args) {
179                         /* parameter #nr can be passed in general purpose register */
180                         param_reg = current_gpreg_param_reg[i];
181                 }
182                 else if (mode_is_float(modes[i]) && cur_fp_idx < maxnum_fpreg_args) {
183                         /* parameter #nr can be passed in floating point register */
184                         param_reg = current_fpreg_param_reg[i];
185                 }
186                 else {
187                         assert(0 && "This should not happen!");
188                 }
189         }
190
191         return param_reg;
192 }
193
194
195
196 /**
197  * Translates the projnum into a "real" argument position for register
198  * requirements dependend on the predecessor.
199  */
200 long ia32_translate_proj_pos(const ir_node *proj) {
201         ir_node *pred = get_Proj_pred(proj);
202         long nr       = get_Proj_proj(proj);
203
204         if (is_ia32_Load(pred)) {
205                 if (nr == pn_Load_res)
206                         return 0;
207                 assert(0 && "unsupported Proj(Load) number");
208         }
209         else if (is_ia32_Store(pred)) {
210                 return 0;
211         }
212         else if (is_ia32_CondJmp(pred)) {
213                 return 0;
214         }
215         else if (is_ia32_SwitchJmp(pred)) {
216                 return 0;
217         }
218         else if (is_ia32_Cdq(pred) || is_ia32_Mulh(pred)) {
219                 if (nr == pn_EAX)
220                         return 0;
221                 if (nr == pn_EDX)
222                         return 1;
223         }
224         else if (is_ia32_DivMod(pred)) {
225                 if (nr == pn_DivMod_res_div || pn_Div_res)
226                         return 0;
227                 if (nr == pn_DivMod_res_mod || pn_Mod_res)
228                         return 1;
229         }
230         else if (is_ia32_fDiv(pred)) {
231                 if (nr == pn_Quot_res)
232                         return 0;
233                 else
234                         assert(0 && "there should be no more Projs for a fDiv");
235         }
236         else if (get_irn_mode(proj) == mode_X && nr == pn_Start_X_initial_exec) {
237                 return 0;
238         }
239         else if (is_Proj(pred)) {
240                 return nr;
241         }
242         else if (get_irn_opcode(pred) == iro_Start) {
243                 return nr;
244         }
245
246 //      assert(0 && "unsupported Proj(X)");
247         return nr;
248 }