use is_Const_0 not is_Const_null in fucom creation
[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 #include "error.h"
34
35 #include "ia32_map_regs.h"
36 #include "ia32_new_nodes.h"
37 #include "ia32_architecture.h"
38 #include "gen_ia32_regalloc_if.h"
39 #include "bearch_ia32_t.h"
40 #include "../benodesets.h"
41
42 #define MAXNUM_GPREG_ARGS     3
43 #define MAXNUM_SSE_ARGS       5
44
45 /* this is the order of the assigned registers usesd for parameter passing */
46
47 const arch_register_t *gpreg_param_reg_std[] = {
48         &ia32_gp_regs[REG_EAX],
49         &ia32_gp_regs[REG_EDX],
50         &ia32_gp_regs[REG_ECX],
51         &ia32_gp_regs[REG_EBX],
52         &ia32_gp_regs[REG_EDI],
53         &ia32_gp_regs[REG_ESI]
54 };
55
56 const arch_register_t *gpreg_param_reg_this[] = {
57         &ia32_gp_regs[REG_ECX],
58         &ia32_gp_regs[REG_EAX],
59         &ia32_gp_regs[REG_EDX],
60         &ia32_gp_regs[REG_EBX],
61         &ia32_gp_regs[REG_EDI],
62         &ia32_gp_regs[REG_ESI]
63 };
64
65 const arch_register_t *fpreg_sse_param_reg_std[] = {
66         &ia32_xmm_regs[REG_XMM0],
67         &ia32_xmm_regs[REG_XMM1],
68         &ia32_xmm_regs[REG_XMM2],
69         &ia32_xmm_regs[REG_XMM3],
70         &ia32_xmm_regs[REG_XMM4],
71         &ia32_xmm_regs[REG_XMM5],
72         &ia32_xmm_regs[REG_XMM6],
73         &ia32_xmm_regs[REG_XMM7]
74 };
75
76 const arch_register_t *fpreg_sse_param_reg_this[] = {
77         NULL,  /* in case of a "this" pointer, the first parameter must not be a float */
78         &ia32_xmm_regs[REG_XMM0],
79         &ia32_xmm_regs[REG_XMM1],
80         &ia32_xmm_regs[REG_XMM2],
81         &ia32_xmm_regs[REG_XMM3],
82         &ia32_xmm_regs[REG_XMM4],
83         &ia32_xmm_regs[REG_XMM5],
84         &ia32_xmm_regs[REG_XMM6],
85         &ia32_xmm_regs[REG_XMM7]
86 };
87
88
89
90 /* Mapping to store registers in firm nodes */
91
92 struct ia32_irn_reg_assoc {
93         const ir_node *irn;
94         const arch_register_t *reg;
95 };
96
97 int ia32_cmp_irn_reg_assoc(const void *a, const void *b, size_t len) {
98         const struct ia32_irn_reg_assoc *x = a;
99         const struct ia32_irn_reg_assoc *y = b;
100         (void) len;
101
102         return x->irn != y->irn;
103 }
104
105 static struct ia32_irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, set *reg_set) {
106         struct ia32_irn_reg_assoc templ;
107         unsigned int hash;
108
109         templ.irn = irn;
110         templ.reg = NULL;
111         hash = nodeset_hash(irn);
112
113         return set_insert(reg_set, &templ, sizeof(templ), hash);
114 }
115
116 void ia32_set_firm_reg(ir_node *irn, const arch_register_t *reg, set *reg_set) {
117         struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
118         assoc->reg = reg;
119 }
120
121 const arch_register_t *ia32_get_firm_reg(const ir_node *irn, set *reg_set) {
122         struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
123         return assoc->reg;
124 }
125
126 void ia32_build_16bit_reg_map(pmap *reg_map) {
127         pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "ax");
128         pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bx");
129         pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "cx");
130         pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dx");
131         pmap_insert(reg_map, &ia32_gp_regs[REG_ESI], "si");
132         pmap_insert(reg_map, &ia32_gp_regs[REG_EDI], "di");
133         pmap_insert(reg_map, &ia32_gp_regs[REG_EBP], "bp");
134         pmap_insert(reg_map, &ia32_gp_regs[REG_ESP], "sp");
135 }
136
137 void ia32_build_8bit_reg_map(pmap *reg_map) {
138         pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "al");
139         pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bl");
140         pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "cl");
141         pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dl");
142 }
143
144 void ia32_build_8bit_reg_map_high(pmap *reg_map) {
145         pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "ah");
146         pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bh");
147         pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "ch");
148         pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dh");
149 }
150
151 const char *ia32_get_mapped_reg_name(pmap *reg_map, const arch_register_t *reg) {
152         pmap_entry *e = pmap_find(reg_map, (void *)reg);
153
154         //assert(e && "missing map init?");
155         if (! e) {
156                 printf("FIXME: ia32map_regs.c:122: returning fake register name for ia32 with 32 register\n");
157                 return reg->name;
158         }
159
160         return e->value;
161 }
162
163 /**
164  * Returns the register for parameter nr.
165  */
166 const arch_register_t *ia32_get_RegParam_reg(unsigned cc, size_t nr,
167                                              const ir_mode *mode)
168 {
169         if(! (cc & cc_reg_param))
170                 return NULL;
171
172         if(mode_is_float(mode)) {
173                 if(!ia32_cg_config.use_sse2)
174                         return NULL;
175                 if(nr >= MAXNUM_SSE_ARGS)
176                         return NULL;
177
178                 if(cc & cc_this_call) {
179                         return fpreg_sse_param_reg_this[nr];
180                 }
181                 return fpreg_sse_param_reg_std[nr];
182         } else if(mode_is_int(mode) || mode_is_reference(mode)) {
183                 if(get_mode_size_bits(mode) > 32)
184                         return NULL;
185
186                 if(nr >= MAXNUM_GPREG_ARGS)
187                         return NULL;
188
189                 if(cc & cc_this_call) {
190                         return gpreg_param_reg_this[nr];
191                 }
192                 return gpreg_param_reg_std[nr];
193         }
194
195         panic("unknown argument mode");
196 }