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_begin_codegeneration(const arch_isa_if_t *isa_if,
59 be_main_env_t *main_env)
61 arch_env_t *arch_env = isa_if->begin_codegeneration(main_env);
62 arch_env->main_env = main_env;
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.
71 static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
74 irn = get_Proj_pred(irn);
75 assert(!is_Proj(irn));
78 ir_op *ops = get_irn_op(irn);
79 const arch_irn_ops_t *be_ops = get_op_ops(ops)->be_ops;
84 void arch_set_frame_offset(ir_node *irn, int offset)
86 const arch_irn_ops_t *ops = get_irn_ops(irn);
87 ops->set_frame_offset(irn, offset);
90 ir_entity *arch_get_frame_entity(const ir_node *irn)
92 const arch_irn_ops_t *ops = get_irn_ops(irn);
93 return ops->get_frame_entity(irn);
96 int arch_get_sp_bias(ir_node *irn)
98 const arch_irn_ops_t *ops = get_irn_ops(irn);
99 return ops->get_sp_bias(irn);
102 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
104 const arch_irn_ops_t *ops = get_irn_ops(irn);
106 if (ops->get_inverse) {
107 return ops->get_inverse(irn, i, inverse, obstack);
113 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
115 const arch_irn_ops_t *ops = get_irn_ops(irn);
117 if (ops->possible_memory_operand) {
118 return ops->possible_memory_operand(irn, i);
124 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
126 const arch_irn_ops_t *ops = get_irn_ops(irn);
128 if (ops->perform_memory_operand) {
129 ops->perform_memory_operand(irn, spill, i);
135 int arch_get_op_estimated_cost(const ir_node *irn)
137 const arch_irn_ops_t *ops = get_irn_ops(irn);
139 if (ops->get_op_estimated_cost) {
140 return ops->get_op_estimated_cost(irn);
146 static reg_out_info_t *get_out_info(const ir_node *node)
149 const backend_info_t *info;
150 assert(get_irn_mode(node) != mode_T);
152 pos = get_Proj_proj(node);
153 node = get_Proj_pred(node);
156 info = be_get_info(node);
157 /* We have a problem with the switch-node where there can be arbitrary
158 * Proj-numbers, so we can't easily allocate an array big-enough to hold
159 * all of them. So until we rewrite Switch-nodes we need this special case
161 if (info->out_infos == NULL)
163 assert(pos < ARR_LEN(info->out_infos));
164 return &info->out_infos[pos];
167 static reg_out_info_t *get_out_info_n(const ir_node *node, unsigned pos)
169 const backend_info_t *info = be_get_info(node);
170 assert(!is_Proj(node));
171 assert(pos < (unsigned)ARR_LEN(info->out_infos));
172 return &info->out_infos[pos];
176 const arch_register_t *arch_get_irn_register(const ir_node *node)
178 const reg_out_info_t *out = get_out_info(node);
182 const arch_register_t *arch_get_irn_register_out(const ir_node *node,
185 const reg_out_info_t *out = get_out_info_n(node, pos);
189 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
191 ir_node *op = get_irn_n(node, pos);
192 return arch_get_irn_register(op);
195 void arch_set_irn_register_out(ir_node *node, unsigned pos,
196 const arch_register_t *reg)
198 reg_out_info_t *out = get_out_info_n(node, pos);
202 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
204 reg_out_info_t *out = get_out_info(node);
208 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
210 reg_out_info_t *out = get_out_info(node);
214 arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
216 backend_info_t *info;
218 return arch_irn_flags_not_scheduled;
220 info = be_get_info(node);
224 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
226 backend_info_t *info;
228 /* setting flags is only supported for instructions currently.
229 * (mainly because we found no use for it yet and saved the space for
230 * be_infos for them */
231 assert(!is_Proj(node));
232 info = be_get_info(node);
236 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
238 backend_info_t *info;
239 assert(!is_Proj(node));
240 info = be_get_info(node);
241 info->flags |= flags;
244 bool arch_reg_is_allocatable(const arch_register_req_t *req,
245 const arch_register_t *reg)
247 if (reg->type & arch_register_type_joker)
249 if (req->type == arch_register_req_type_none)
251 if (req->type & arch_register_req_type_limited) {
252 if (arch_register_get_class(reg) != req->cls)
254 return rbitset_is_set(req->limited, arch_register_get_index(reg));
256 return req->cls == arch_register_get_class(reg);
259 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
262 if (req == NULL || req->type == arch_register_req_type_none) {
267 fprintf(F, "%s", req->cls->name);
269 if (arch_register_req_is(req, limited)) {
270 unsigned n_regs = req->cls->n_regs;
273 fprintf(F, " limited to");
274 for (i = 0; i < n_regs; ++i) {
275 if (rbitset_is_set(req->limited, i)) {
276 const arch_register_t *reg = &req->cls->regs[i];
277 fprintf(F, " %s", reg->name);
282 if (arch_register_req_is(req, should_be_same)) {
283 const unsigned other = req->other_same;
286 fprintf(F, " same as");
287 for (i = 0; 1U << i <= other; ++i) {
288 if (other & (1U << i)) {
289 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
294 if (arch_register_req_is(req, must_be_different)) {
295 const unsigned other = req->other_different;
298 fprintf(F, " different from");
299 for (i = 0; 1U << i <= other; ++i) {
300 if (other & (1U << i)) {
301 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
306 if (req->width != 1) {
307 fprintf(F, " width:%d", req->width);
309 if (arch_register_req_is(req, aligned)) {
310 fprintf(F, " aligned");
312 if (arch_register_req_is(req, ignore)) {
313 fprintf(F, " ignore");
315 if (arch_register_req_is(req, produces_sp)) {
316 fprintf(F, " produces_sp");
320 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
322 int n_ins = get_irn_arity(node);
323 for (int i = 0; i < n_ins; ++i) {
324 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
325 fprintf(F, "inreq #%d = ", i);
326 arch_dump_register_req(F, req, node);
329 unsigned n_outs = arch_get_irn_n_outs(node);
330 for (unsigned o = 0; o < n_outs; ++o) {
331 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
332 fprintf(F, "outreq #%u = ", o);
333 arch_dump_register_req(F, req, node);
336 for (unsigned o = 0; o < n_outs; ++o) {
337 const arch_register_t *reg = arch_get_irn_register_out(node, o);
338 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
339 if (req->cls == NULL)
341 fprintf(F, "reg #%u = %s\n", o, reg != NULL ? reg->name : "n/a");
344 fprintf(F, "flags =");
345 arch_irn_flags_t flags = arch_get_irn_flags(node);
346 if (flags == arch_irn_flags_none) {
349 if (flags & arch_irn_flags_dont_spill) {
350 fprintf(F, " unspillable");
352 if (flags & arch_irn_flags_rematerializable) {
353 fprintf(F, " remat");
355 if (flags & arch_irn_flags_modify_flags) {
356 fprintf(F, " modify_flags");
358 if (flags & arch_irn_flags_simple_jump) {
359 fprintf(F, " simple_jump");
361 if (flags & arch_irn_flags_not_scheduled) {
362 fprintf(F, " not_scheduled");
365 fprintf(F, " (0x%x)\n", (unsigned)flags);