2 * Copyright (C) 1995-2008 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
33 #include "ia32_map_regs.h"
34 #include "ia32_new_nodes.h"
35 #include "ia32_architecture.h"
36 #include "gen_ia32_regalloc_if.h"
37 #include "bearch_ia32_t.h"
39 #define MAXNUM_GPREG_ARGS 3
40 #define MAXNUM_SSE_ARGS 8
42 /* this is the order of the assigned registers used for parameter passing */
44 static const arch_register_t *gpreg_param_reg_fastcall[] = {
45 &ia32_gp_regs[REG_ECX],
46 &ia32_gp_regs[REG_EDX],
50 static const arch_register_t *gpreg_param_reg_regparam[] = {
51 &ia32_gp_regs[REG_EAX],
52 &ia32_gp_regs[REG_EDX],
53 &ia32_gp_regs[REG_ECX]
56 static const arch_register_t *gpreg_param_reg_this[] = {
57 &ia32_gp_regs[REG_ECX],
62 static const arch_register_t *fpreg_sse_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 static const arch_register_t *fpreg_sse_param_reg_this[] = {
74 NULL, /* in case of a "this" pointer, the first parameter must not be a float */
78 /* Mapping to store registers in firm nodes */
80 struct ia32_irn_reg_assoc {
82 const arch_register_t *reg;
85 int ia32_cmp_irn_reg_assoc(const void *a, const void *b, size_t len) {
86 const struct ia32_irn_reg_assoc *x = a;
87 const struct ia32_irn_reg_assoc *y = b;
90 return x->irn != y->irn;
93 static struct ia32_irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, set *reg_set) {
94 struct ia32_irn_reg_assoc templ;
101 return set_insert(reg_set, &templ, sizeof(templ), hash);
104 void ia32_set_firm_reg(ir_node *irn, const arch_register_t *reg, set *reg_set) {
105 struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
109 const arch_register_t *ia32_get_firm_reg(const ir_node *irn, set *reg_set) {
110 struct ia32_irn_reg_assoc *assoc = get_irn_reg_assoc(irn, reg_set);
114 void ia32_build_16bit_reg_map(pmap *reg_map) {
115 pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "ax");
116 pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bx");
117 pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "cx");
118 pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dx");
119 pmap_insert(reg_map, &ia32_gp_regs[REG_ESI], "si");
120 pmap_insert(reg_map, &ia32_gp_regs[REG_EDI], "di");
121 pmap_insert(reg_map, &ia32_gp_regs[REG_EBP], "bp");
122 pmap_insert(reg_map, &ia32_gp_regs[REG_ESP], "sp");
125 void ia32_build_8bit_reg_map(pmap *reg_map) {
126 pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "al");
127 pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bl");
128 pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "cl");
129 pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dl");
132 void ia32_build_8bit_reg_map_high(pmap *reg_map) {
133 pmap_insert(reg_map, &ia32_gp_regs[REG_EAX], "ah");
134 pmap_insert(reg_map, &ia32_gp_regs[REG_EBX], "bh");
135 pmap_insert(reg_map, &ia32_gp_regs[REG_ECX], "ch");
136 pmap_insert(reg_map, &ia32_gp_regs[REG_EDX], "dh");
139 const char *ia32_get_mapped_reg_name(pmap *reg_map, const arch_register_t *reg) {
140 pmap_entry *e = pmap_find(reg_map, (void *)reg);
142 //assert(e && "missing map init?");
144 printf("FIXME: ia32map_regs.c:122: returning fake register name for ia32 with 32 register\n");
152 * Returns the register for parameter nr.
154 const arch_register_t *ia32_get_RegParam_reg(unsigned cc, size_t nr,
157 if ((cc & cc_this_call) && nr == 0)
158 return gpreg_param_reg_this[0];
160 if (! (cc & cc_reg_param))
163 if (mode_is_float(mode)) {
164 if (!ia32_cg_config.use_sse2 || (cc & cc_fpreg_param) == 0)
166 if (nr >= MAXNUM_SSE_ARGS)
169 if (cc & cc_this_call) {
170 return fpreg_sse_param_reg_this[nr];
172 return fpreg_sse_param_reg_std[nr];
173 } else if (mode_is_int(mode) || mode_is_reference(mode)) {
174 unsigned num_regparam;
176 if (get_mode_size_bits(mode) > 32)
179 if (nr >= MAXNUM_GPREG_ARGS)
182 if (cc & cc_this_call) {
183 return gpreg_param_reg_this[nr];
185 num_regparam = cc & ~cc_bits;
186 if (num_regparam == 0) {
187 /* default fastcall */
188 return gpreg_param_reg_fastcall[nr];
190 if (nr < num_regparam)
191 return gpreg_param_reg_regparam[nr];
195 panic("unknown argument mode");