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, int pos)
169 const backend_info_t *info = be_get_info(node);
170 assert(!is_Proj(node));
171 assert(pos >= 0 && pos < (int)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, int pos)
184 const reg_out_info_t *out = get_out_info_n(node, pos);
188 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
190 ir_node *op = get_irn_n(node, pos);
191 return arch_get_irn_register(op);
194 void arch_set_irn_register_out(ir_node *node, int pos,
195 const arch_register_t *reg)
197 reg_out_info_t *out = get_out_info_n(node, pos);
201 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
203 reg_out_info_t *out = get_out_info(node);
207 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
209 reg_out_info_t *out = get_out_info(node);
213 arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
215 backend_info_t *info;
217 return arch_irn_flags_not_scheduled;
219 info = be_get_info(node);
223 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
225 backend_info_t *info;
227 /* setting flags is only supported for instructions currently.
228 * (mainly because we found no use for it yet and saved the space for
229 * be_infos for them */
230 assert(!is_Proj(node));
231 info = be_get_info(node);
235 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
237 backend_info_t *info;
238 assert(!is_Proj(node));
239 info = be_get_info(node);
240 info->flags |= flags;
243 bool arch_reg_is_allocatable(const arch_register_req_t *req,
244 const arch_register_t *reg)
246 if (reg->type & arch_register_type_joker)
248 if (req->type == arch_register_req_type_none)
250 if (req->type & arch_register_req_type_limited) {
251 if (arch_register_get_class(reg) != req->cls)
253 return rbitset_is_set(req->limited, arch_register_get_index(reg));
255 return req->cls == arch_register_get_class(reg);
258 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
261 if (req == NULL || req->type == arch_register_req_type_none) {
266 fprintf(F, "%s", req->cls->name);
268 if (arch_register_req_is(req, limited)) {
269 unsigned n_regs = req->cls->n_regs;
272 fprintf(F, " limited to");
273 for (i = 0; i < n_regs; ++i) {
274 if (rbitset_is_set(req->limited, i)) {
275 const arch_register_t *reg = &req->cls->regs[i];
276 fprintf(F, " %s", reg->name);
281 if (arch_register_req_is(req, should_be_same)) {
282 const unsigned other = req->other_same;
285 fprintf(F, " same as");
286 for (i = 0; 1U << i <= other; ++i) {
287 if (other & (1U << i)) {
288 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
293 if (arch_register_req_is(req, must_be_different)) {
294 const unsigned other = req->other_different;
297 fprintf(F, " different from");
298 for (i = 0; 1U << i <= other; ++i) {
299 if (other & (1U << i)) {
300 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
305 if (req->width != 1) {
306 fprintf(F, " width:%d", req->width);
308 if (arch_register_req_is(req, aligned)) {
309 fprintf(F, " aligned");
311 if (arch_register_req_is(req, ignore)) {
312 fprintf(F, " ignore");
314 if (arch_register_req_is(req, produces_sp)) {
315 fprintf(F, " produces_sp");
319 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
321 int n_ins = get_irn_arity(node);
322 int n_outs = arch_get_irn_n_outs(node);
323 arch_irn_flags_t flags = arch_get_irn_flags(node);
326 for (i = 0; i < n_ins; ++i) {
327 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
328 fprintf(F, "inreq #%d = ", i);
329 arch_dump_register_req(F, req, node);
332 for (i = 0; i < n_outs; ++i) {
333 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
334 fprintf(F, "outreq #%d = ", i);
335 arch_dump_register_req(F, req, node);
338 for (i = 0; i < n_outs; ++i) {
339 const arch_register_t *reg = arch_get_irn_register_out(node, i);
340 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
341 if (req->cls == NULL)
343 fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
346 fprintf(F, "flags =");
347 if (flags == arch_irn_flags_none) {
350 if (flags & arch_irn_flags_dont_spill) {
351 fprintf(F, " unspillable");
353 if (flags & arch_irn_flags_rematerializable) {
354 fprintf(F, " remat");
356 if (flags & arch_irn_flags_modify_flags) {
357 fprintf(F, " modify_flags");
359 if (flags & arch_irn_flags_simple_jump) {
360 fprintf(F, " simple_jump");
362 if (flags & arch_irn_flags_not_scheduled) {
363 fprintf(F, " not_scheduled");
366 fprintf(F, " (%d)\n", (int)flags);