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 Processor architecture specification.
23 * @author Sebastian Hack
38 #include "raw_bitset.h"
42 static const arch_register_req_t no_requirement = {
43 arch_register_req_type_none,
50 const arch_register_req_t *arch_no_register_req = &no_requirement;
52 static reg_out_info_t dummy_info = {
57 /* Initialize the architecture environment struct. */
58 arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, be_main_env_t *main_env)
60 arch_env_t *arch_env = isa_if->init(main_env);
61 arch_env->main_env = main_env;
66 * Get the isa responsible for a node.
67 * @param irn The node to get the responsible isa for.
68 * @return The irn operations given by the responsible isa.
70 static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
73 const arch_irn_ops_t *be_ops;
76 irn = get_Proj_pred(irn);
77 assert(!is_Proj(irn));
80 ops = get_irn_op(irn);
81 be_ops = get_op_ops(ops)->be_ops;
86 void arch_set_frame_offset(ir_node *irn, int offset)
88 const arch_irn_ops_t *ops = get_irn_ops(irn);
89 ops->set_frame_offset(irn, offset);
92 ir_entity *arch_get_frame_entity(const ir_node *irn)
94 const arch_irn_ops_t *ops = get_irn_ops(irn);
95 return ops->get_frame_entity(irn);
98 int arch_get_sp_bias(ir_node *irn)
100 const arch_irn_ops_t *ops = get_irn_ops(irn);
101 return ops->get_sp_bias(irn);
104 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
106 const arch_irn_ops_t *ops = get_irn_ops(irn);
108 if (ops->get_inverse) {
109 return ops->get_inverse(irn, i, inverse, obstack);
115 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
117 const arch_irn_ops_t *ops = get_irn_ops(irn);
119 if (ops->possible_memory_operand) {
120 return ops->possible_memory_operand(irn, i);
126 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
128 const arch_irn_ops_t *ops = get_irn_ops(irn);
130 if (ops->perform_memory_operand) {
131 ops->perform_memory_operand(irn, spill, i);
137 int arch_get_op_estimated_cost(const ir_node *irn)
139 const arch_irn_ops_t *ops = get_irn_ops(irn);
141 if (ops->get_op_estimated_cost) {
142 return ops->get_op_estimated_cost(irn);
148 static reg_out_info_t *get_out_info(const ir_node *node)
151 const backend_info_t *info;
152 assert(get_irn_mode(node) != mode_T);
154 pos = get_Proj_proj(node);
155 node = get_Proj_pred(node);
158 info = be_get_info(node);
159 /* We have a problem with the switch-node where there can be arbitrary
160 * Proj-numbers, so we can't easily allocate an array big-enough to hold
161 * all of them. So until we rewrite Switch-nodes we need this special case
163 if (info->out_infos == NULL)
165 assert(pos < ARR_LEN(info->out_infos));
166 return &info->out_infos[pos];
169 static reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
171 const backend_info_t *info = be_get_info(node);
172 assert(!is_Proj(node));
173 assert(pos >= 0 && pos < (int)ARR_LEN(info->out_infos));
174 return &info->out_infos[pos];
178 const arch_register_t *arch_get_irn_register(const ir_node *node)
180 const reg_out_info_t *out = get_out_info(node);
184 const arch_register_t *arch_get_irn_register_out(const ir_node *node, int pos)
186 const reg_out_info_t *out = get_out_info_n(node, pos);
190 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
192 ir_node *op = get_irn_n(node, pos);
193 return arch_get_irn_register(op);
196 void arch_set_irn_register_out(ir_node *node, int pos,
197 const arch_register_t *reg)
199 reg_out_info_t *out = get_out_info_n(node, pos);
203 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
205 reg_out_info_t *out = get_out_info(node);
209 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
211 reg_out_info_t *out = get_out_info(node);
215 arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
217 backend_info_t *info;
219 return arch_irn_flags_not_scheduled;
221 info = be_get_info(node);
225 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
227 backend_info_t *info;
229 /* setting flags is only supported for instructions currently.
230 * (mainly because we found no use for it yet and saved the space for
231 * be_infos for them */
232 assert(!is_Proj(node));
233 info = be_get_info(node);
237 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
239 backend_info_t *info;
240 assert(!is_Proj(node));
241 info = be_get_info(node);
242 info->flags |= flags;
245 bool arch_reg_is_allocatable(const arch_register_req_t *req,
246 const arch_register_t *reg)
248 if (reg->type & arch_register_type_joker)
250 if (req->type == arch_register_req_type_none)
252 if (req->type & arch_register_req_type_limited) {
253 if (arch_register_get_class(reg) != req->cls)
255 return rbitset_is_set(req->limited, arch_register_get_index(reg));
257 return req->cls == arch_register_get_class(reg);
260 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
263 if (req == NULL || req->type == arch_register_req_type_none) {
268 fprintf(F, "%s", req->cls->name);
270 if (arch_register_req_is(req, limited)) {
271 unsigned n_regs = req->cls->n_regs;
274 fprintf(F, " limited to");
275 for (i = 0; i < n_regs; ++i) {
276 if (rbitset_is_set(req->limited, i)) {
277 const arch_register_t *reg = &req->cls->regs[i];
278 fprintf(F, " %s", reg->name);
283 if (arch_register_req_is(req, should_be_same)) {
284 const unsigned other = req->other_same;
287 fprintf(F, " same as");
288 for (i = 0; 1U << i <= other; ++i) {
289 if (other & (1U << i)) {
290 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
295 if (arch_register_req_is(req, must_be_different)) {
296 const unsigned other = req->other_different;
299 fprintf(F, " different from");
300 for (i = 0; 1U << i <= other; ++i) {
301 if (other & (1U << i)) {
302 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
307 if (req->width != 1) {
308 fprintf(F, " width:%d", req->width);
310 if (arch_register_req_is(req, aligned)) {
311 fprintf(F, " aligned");
313 if (arch_register_req_is(req, ignore)) {
314 fprintf(F, " ignore");
316 if (arch_register_req_is(req, produces_sp)) {
317 fprintf(F, " produces_sp");
321 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
323 int n_ins = get_irn_arity(node);
324 int n_outs = arch_get_irn_n_outs(node);
325 arch_irn_flags_t flags = arch_get_irn_flags(node);
328 for (i = 0; i < n_ins; ++i) {
329 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
330 fprintf(F, "inreq #%d = ", i);
331 arch_dump_register_req(F, req, node);
334 for (i = 0; i < n_outs; ++i) {
335 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
336 fprintf(F, "outreq #%d = ", i);
337 arch_dump_register_req(F, req, node);
340 for (i = 0; i < n_outs; ++i) {
341 const arch_register_t *reg = arch_get_irn_register_out(node, i);
342 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
343 if (req->cls == NULL)
345 fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
348 fprintf(F, "flags =");
349 if (flags == arch_irn_flags_none) {
352 if (flags & arch_irn_flags_dont_spill) {
353 fprintf(F, " unspillable");
355 if (flags & arch_irn_flags_rematerializable) {
356 fprintf(F, " remat");
358 if (flags & arch_irn_flags_modify_flags) {
359 fprintf(F, " modify_flags");
361 if (flags & arch_irn_flags_simple_jump) {
362 fprintf(F, " simple_jump");
364 if (flags & arch_irn_flags_not_scheduled) {
365 fprintf(F, " not_scheduled");
368 fprintf(F, " (%d)\n", (int)flags);