further spread size_t (all warnings on linux/gcc fixed)
[libfirm] / ir / be / bearch.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       Processor architecture specification.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "bearch.h"
31 #include "benode.h"
32 #include "beinfo.h"
33 #include "ircons_t.h"
34 #include "irnode_t.h"
35 #include "irop_t.h"
36
37 #include "bitset.h"
38 #include "pset.h"
39 #include "raw_bitset.h"
40
41 #include "irprintf.h"
42
43 /* Initialize the architecture environment struct. */
44 arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
45 {
46         arch_env_t *arch_env = isa_if->init(file_handle);
47         arch_env->main_env   = main_env;
48         return arch_env;
49 }
50
51 /**
52  * Get the isa responsible for a node.
53  * @param irn The node to get the responsible isa for.
54  * @return The irn operations given by the responsible isa.
55  */
56 static inline const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
57 {
58         const ir_op          *ops;
59         const arch_irn_ops_t *be_ops;
60
61         if (is_Proj(irn)) {
62                 irn = get_Proj_pred(irn);
63                 assert(!is_Proj(irn));
64         }
65
66         ops    = get_irn_op(irn);
67         be_ops = get_op_ops(ops)->be_ops;
68
69         return be_ops;
70 }
71
72 const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos)
73 {
74         if (is_Proj(irn)) {
75                 ir_node *pred = get_Proj_pred(irn);
76                 long     pn   = get_Proj_proj(irn);
77                 assert(pos == -1);
78                 return arch_get_out_register_req(pred, pn);
79         }
80
81         if (pos < 0) {
82                 return arch_get_out_register_req(irn, -pos-1);
83         } else {
84                 return arch_get_in_register_req(irn, pos);
85         }
86 }
87
88 void arch_set_frame_offset(ir_node *irn, int offset)
89 {
90         const arch_irn_ops_t *ops = get_irn_ops(irn);
91         ops->set_frame_offset(irn, offset);
92 }
93
94 ir_entity *arch_get_frame_entity(const ir_node *irn)
95 {
96         const arch_irn_ops_t *ops = get_irn_ops(irn);
97         return ops->get_frame_entity(irn);
98 }
99
100 int arch_get_sp_bias(ir_node *irn)
101 {
102         const arch_irn_ops_t *ops = get_irn_ops(irn);
103         return ops->get_sp_bias(irn);
104 }
105
106 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
107 {
108         const arch_irn_ops_t *ops = get_irn_ops(irn);
109
110         if (ops->get_inverse) {
111                 return ops->get_inverse(irn, i, inverse, obstack);
112         } else {
113                 return NULL;
114         }
115 }
116
117 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
118 {
119         const arch_irn_ops_t *ops = get_irn_ops(irn);
120
121         if (ops->possible_memory_operand) {
122                 return ops->possible_memory_operand(irn, i);
123         } else {
124                 return 0;
125         }
126 }
127
128 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
129 {
130         const arch_irn_ops_t *ops = get_irn_ops(irn);
131
132         if (ops->perform_memory_operand) {
133                 ops->perform_memory_operand(irn, spill, i);
134         } else {
135                 return;
136         }
137 }
138
139 int arch_get_op_estimated_cost(const ir_node *irn)
140 {
141         const arch_irn_ops_t *ops = get_irn_ops(irn);
142
143         if (ops->get_op_estimated_cost) {
144                 return ops->get_op_estimated_cost(irn);
145         } else {
146                 return 1;
147         }
148 }
149
150 int arch_reg_is_allocatable(const ir_node *irn, int pos,
151                             const arch_register_t *reg)
152 {
153         const arch_register_req_t *req = arch_get_register_req(irn, pos);
154
155         if (req->type == arch_register_req_type_none)
156                 return 0;
157
158         if (arch_register_req_is(req, limited)) {
159                 if (arch_register_get_class(reg) != req->cls)
160                         return 0;
161                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
162         }
163
164         return req->cls == reg->reg_class;
165 }
166
167 const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn, int pos)
168 {
169         const arch_register_req_t *req = arch_get_register_req(irn, pos);
170
171         assert(req->type != arch_register_req_type_none || req->cls == NULL);
172
173         return req->cls;
174 }
175
176 static inline reg_out_info_t *get_out_info(const ir_node *node)
177 {
178         size_t                pos  = 0;
179         const backend_info_t *info;
180
181         assert(get_irn_mode(node) != mode_T);
182         if (is_Proj(node)) {
183                 pos  = get_Proj_proj(node);
184                 node = get_Proj_pred(node);
185         }
186
187         info = be_get_info(node);
188         assert(pos < ARR_LEN(info->out_infos));
189         return &info->out_infos[pos];
190 }
191
192
193 static inline reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
194 {
195         const backend_info_t *info = be_get_info(node);
196         assert(!is_Proj(node));
197         assert(pos >= 0 && pos < (int)ARR_LEN(info->out_infos));
198         return &info->out_infos[pos];
199 }
200
201
202 const arch_register_t *arch_get_irn_register(const ir_node *node)
203 {
204         const reg_out_info_t *out = get_out_info(node);
205         return out->reg;
206 }
207
208 const arch_register_t *arch_irn_get_register(const ir_node *node, int pos)
209 {
210         const reg_out_info_t *out = get_out_info_n(node, pos);
211         return out->reg;
212 }
213
214 void arch_irn_set_register(ir_node *node, int pos, const arch_register_t *reg)
215 {
216         reg_out_info_t *out = get_out_info_n(node, pos);
217         out->reg            = reg;
218 }
219
220 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
221 {
222         reg_out_info_t *out = get_out_info(node);
223         out->reg = reg;
224 }
225
226 arch_irn_class_t arch_irn_classify(const ir_node *node)
227 {
228         const arch_irn_ops_t *ops = get_irn_ops(node);
229         return ops->classify(node);
230 }
231
232 arch_irn_flags_t arch_irn_get_flags(const ir_node *node)
233 {
234         backend_info_t *info = be_get_info(node);
235         return info->flags;
236 }
237
238 void arch_irn_set_flags(ir_node *node, arch_irn_flags_t flags)
239 {
240         backend_info_t *info = be_get_info(node);
241         info->flags = flags;
242 }
243
244 void arch_irn_add_flags(ir_node *node, arch_irn_flags_t flags)
245 {
246         backend_info_t *info = be_get_info(node);
247         info->flags |= flags;
248 }
249
250 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
251                             const ir_node *node)
252 {
253         if (req == NULL || req->type == arch_register_req_type_none) {
254                 fprintf(F, "n/a");
255                 return;
256         }
257
258         fprintf(F, "%s", req->cls->name);
259
260         if (arch_register_req_is(req, limited)) {
261                 unsigned n_regs = req->cls->n_regs;
262                 unsigned i;
263
264                 fprintf(F, " limited to");
265                 for (i = 0; i < n_regs; ++i) {
266                         if (rbitset_is_set(req->limited, i)) {
267                                 const arch_register_t *reg = &req->cls->regs[i];
268                                 fprintf(F, " %s", reg->name);
269                         }
270                 }
271         }
272
273         if (arch_register_req_is(req, should_be_same)) {
274                 const unsigned other = req->other_same;
275                 int i;
276
277                 fprintf(F, " same as");
278                 for (i = 0; 1U << i <= other; ++i) {
279                         if (other & (1U << i)) {
280                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
281                         }
282                 }
283         }
284
285         if (arch_register_req_is(req, must_be_different)) {
286                 const unsigned other = req->other_different;
287                 int i;
288
289                 fprintf(F, " different from");
290                 for (i = 0; 1U << i <= other; ++i) {
291                         if (other & (1U << i)) {
292                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
293                         }
294                 }
295         }
296
297         if (req->width != 1) {
298                 fprintf(F, " width:%u", req->width);
299         }
300         if (arch_register_req_is(req, aligned)) {
301                 fprintf(F, " aligned");
302         }
303         if (arch_register_req_is(req, ignore)) {
304                 fprintf(F, " ignore");
305         }
306         if (arch_register_req_is(req, produces_sp)) {
307                 fprintf(F, " produces_sp");
308         }
309 }
310
311 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
312 {
313         int              n_ins  = get_irn_arity(node);
314         int              n_outs = arch_irn_get_n_outs(node);
315         arch_irn_flags_t flags  = arch_irn_get_flags(node);
316         int              i;
317
318         for (i = 0; i < n_ins; ++i) {
319                 const arch_register_req_t *req = arch_get_in_register_req(node, i);
320                 fprintf(F, "inreq #%d = ", i);
321                 arch_dump_register_req(F, req, node);
322                 fputs("\n", F);
323         }
324         for (i = 0; i < n_outs; ++i) {
325                 const arch_register_req_t *req = arch_get_out_register_req(node, i);
326                 fprintf(F, "outreq #%d = ", i);
327                 arch_dump_register_req(F, req, node);
328                 fputs("\n", F);
329         }
330         for (i = 0; i < n_outs; ++i) {
331                 const arch_register_t     *reg = arch_irn_get_register(node, i);
332                 const arch_register_req_t *req = arch_get_out_register_req(node, i);
333                 if (req->cls == NULL)
334                         continue;
335                 fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
336         }
337
338         fprintf(F, "flags =");
339         if (flags == arch_irn_flags_none) {
340                 fprintf(F, " none");
341         } else {
342                 if (flags & arch_irn_flags_dont_spill) {
343                         fprintf(F, " unspillable");
344                 }
345                 if (flags & arch_irn_flags_rematerializable) {
346                         fprintf(F, " remat");
347                 }
348                 if (flags & arch_irn_flags_modify_flags) {
349                         fprintf(F, " modify_flags");
350                 }
351         }
352         fprintf(F, " (%d)\n", flags);
353 }
354
355 static const arch_register_req_t no_requirement = {
356         arch_register_req_type_none,
357         NULL,
358         NULL,
359         0,
360         0,
361         0
362 };
363 const arch_register_req_t *arch_no_register_req = &no_requirement;