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