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
39 #include "raw_bitset.h"
43 static const arch_register_req_t no_requirement = {
44 arch_register_req_type_none,
51 const arch_register_req_t *arch_no_register_req = &no_requirement;
53 static reg_out_info_t dummy_info = {
58 /* Initialize the architecture environment struct. */
59 arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
61 arch_env_t *arch_env = isa_if->init(file_handle);
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 const arch_irn_ops_t *be_ops;
77 irn = get_Proj_pred(irn);
78 assert(!is_Proj(irn));
81 ops = get_irn_op(irn);
82 be_ops = get_op_ops(ops)->be_ops;
87 void arch_set_frame_offset(ir_node *irn, int offset)
89 const arch_irn_ops_t *ops = get_irn_ops(irn);
90 ops->set_frame_offset(irn, offset);
93 ir_entity *arch_get_frame_entity(const ir_node *irn)
95 const arch_irn_ops_t *ops = get_irn_ops(irn);
96 return ops->get_frame_entity(irn);
99 int arch_get_sp_bias(ir_node *irn)
101 const arch_irn_ops_t *ops = get_irn_ops(irn);
102 return ops->get_sp_bias(irn);
105 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
107 const arch_irn_ops_t *ops = get_irn_ops(irn);
109 if (ops->get_inverse) {
110 return ops->get_inverse(irn, i, inverse, obstack);
116 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
118 const arch_irn_ops_t *ops = get_irn_ops(irn);
120 if (ops->possible_memory_operand) {
121 return ops->possible_memory_operand(irn, i);
127 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
129 const arch_irn_ops_t *ops = get_irn_ops(irn);
131 if (ops->perform_memory_operand) {
132 ops->perform_memory_operand(irn, spill, i);
138 int arch_get_op_estimated_cost(const ir_node *irn)
140 const arch_irn_ops_t *ops = get_irn_ops(irn);
142 if (ops->get_op_estimated_cost) {
143 return ops->get_op_estimated_cost(irn);
149 static reg_out_info_t *get_out_info(const ir_node *node)
152 const backend_info_t *info;
153 assert(get_irn_mode(node) != mode_T);
155 pos = get_Proj_proj(node);
156 node = get_Proj_pred(node);
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
164 if (info->out_infos == NULL)
166 assert(pos < ARR_LEN(info->out_infos));
167 return &info->out_infos[pos];
170 static reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
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];
179 const arch_register_t *arch_get_irn_register(const ir_node *node)
181 const reg_out_info_t *out = get_out_info(node);
185 const arch_register_t *arch_get_irn_register_out(const ir_node *node, int pos)
187 const reg_out_info_t *out = get_out_info_n(node, pos);
191 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
193 ir_node *op = get_irn_n(node, pos);
194 return arch_get_irn_register(op);
197 void arch_set_irn_register_out(ir_node *node, int pos,
198 const arch_register_t *reg)
200 reg_out_info_t *out = get_out_info_n(node, pos);
204 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
206 reg_out_info_t *out = get_out_info(node);
210 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
212 reg_out_info_t *out = get_out_info(node);
216 arch_irn_class_t arch_irn_classify(const ir_node *node)
218 const arch_irn_ops_t *ops = get_irn_ops(node);
219 return ops->classify(node);
222 arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
224 backend_info_t *info;
226 return arch_irn_flags_not_scheduled;
228 info = be_get_info(node);
232 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
234 backend_info_t *info;
236 /* setting flags is only supported for instructions currently.
237 * (mainly because we found no use for it yet and saved the space for
238 * be_infos for them */
239 assert(!is_Proj(node));
240 info = be_get_info(node);
244 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
246 backend_info_t *info;
247 assert(!is_Proj(node));
248 info = be_get_info(node);
249 info->flags |= flags;
252 bool arch_reg_is_allocatable(const arch_register_req_t *req,
253 const arch_register_t *reg)
255 if (reg->type & arch_register_type_joker)
257 if (req->type == arch_register_req_type_none)
259 if (req->type & arch_register_req_type_limited) {
260 if (arch_register_get_class(reg) != req->cls)
262 return rbitset_is_set(req->limited, arch_register_get_index(reg));
264 return req->cls == arch_register_get_class(reg);
267 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
270 if (req == NULL || req->type == arch_register_req_type_none) {
275 fprintf(F, "%s", req->cls->name);
277 if (arch_register_req_is(req, limited)) {
278 unsigned n_regs = req->cls->n_regs;
281 fprintf(F, " limited to");
282 for (i = 0; i < n_regs; ++i) {
283 if (rbitset_is_set(req->limited, i)) {
284 const arch_register_t *reg = &req->cls->regs[i];
285 fprintf(F, " %s", reg->name);
290 if (arch_register_req_is(req, should_be_same)) {
291 const unsigned other = req->other_same;
294 fprintf(F, " same as");
295 for (i = 0; 1U << i <= other; ++i) {
296 if (other & (1U << i)) {
297 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
302 if (arch_register_req_is(req, must_be_different)) {
303 const unsigned other = req->other_different;
306 fprintf(F, " different from");
307 for (i = 0; 1U << i <= other; ++i) {
308 if (other & (1U << i)) {
309 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
314 if (req->width != 1) {
315 fprintf(F, " width:%d", req->width);
317 if (arch_register_req_is(req, aligned)) {
318 fprintf(F, " aligned");
320 if (arch_register_req_is(req, ignore)) {
321 fprintf(F, " ignore");
323 if (arch_register_req_is(req, produces_sp)) {
324 fprintf(F, " produces_sp");
328 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
330 int n_ins = get_irn_arity(node);
331 int n_outs = arch_get_irn_n_outs(node);
332 arch_irn_flags_t flags = arch_get_irn_flags(node);
335 for (i = 0; i < n_ins; ++i) {
336 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
337 fprintf(F, "inreq #%d = ", i);
338 arch_dump_register_req(F, req, node);
341 for (i = 0; i < n_outs; ++i) {
342 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
343 fprintf(F, "outreq #%d = ", i);
344 arch_dump_register_req(F, req, node);
347 for (i = 0; i < n_outs; ++i) {
348 const arch_register_t *reg = arch_get_irn_register_out(node, i);
349 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
350 if (req->cls == NULL)
352 fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
355 fprintf(F, "flags =");
356 if (flags == arch_irn_flags_none) {
359 if (flags & arch_irn_flags_dont_spill) {
360 fprintf(F, " unspillable");
362 if (flags & arch_irn_flags_rematerializable) {
363 fprintf(F, " remat");
365 if (flags & arch_irn_flags_modify_flags) {
366 fprintf(F, " modify_flags");
368 if (flags & arch_irn_flags_simple_jump) {
369 fprintf(F, " simple_jump");
371 if (flags & arch_irn_flags_not_scheduled) {
372 fprintf(F, " not_scheduled");
375 fprintf(F, " (%d)\n", (int)flags);