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