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
40 #include "raw_bitset.h"
44 /* Initialize the architecture environment struct. */
45 arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
47 arch_env_t *arch_env = isa_if->init(file_handle);
48 arch_env->main_env = main_env;
52 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
56 for(i = 0, n = cls->n_regs; i < n; ++i)
64 * Get the isa responsible for a node.
65 * @param env The arch environment with the isa stack.
66 * @param irn The node to get the responsible isa for.
67 * @return The irn operations given by the responsible isa.
69 static INLINE const arch_irn_ops_t *
70 get_irn_ops(const arch_env_t *env, const ir_node *irn)
73 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;
88 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
89 return get_irn_ops(env, irn);
92 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
93 const ir_node *irn, int pos)
95 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
96 return ops->get_irn_reg_req(irn, pos);
99 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
101 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
102 ops->set_frame_offset(irn, offset);
105 ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
107 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
108 return ops->get_frame_entity(irn);
111 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
113 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
114 ops->set_frame_entity(irn, ent);
117 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
119 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
120 return ops->get_sp_bias(irn);
123 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
125 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
127 if(ops->get_inverse) {
128 return ops->get_inverse(irn, i, inverse, obstack);
134 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
135 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
137 if(ops->possible_memory_operand) {
138 return ops->possible_memory_operand(irn, i);
144 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
145 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
147 if(ops->perform_memory_operand) {
148 ops->perform_memory_operand(irn, spill, i);
154 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
156 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
158 if(ops->get_op_estimated_cost) {
159 return ops->get_op_estimated_cost(irn);
165 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
167 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
169 if(ops->possible_memory_operand) {
170 return ops->possible_memory_operand(irn, i);
176 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
178 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
179 const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
181 if(req->type == arch_register_req_type_none) {
182 bitset_clear_all(bs);
186 if(arch_register_req_is(req, limited)) {
187 rbitset_copy_to_bitset(req->limited, bs);
188 return bitset_popcnt(bs);
191 arch_register_class_put(req->cls, bs);
192 return req->cls->n_regs;
195 void arch_put_non_ignore_regs(const arch_env_t *env,
196 const arch_register_class_t *cls, bitset_t *bs)
201 for(i = 0; i < cls->n_regs; ++i) {
202 if(!arch_register_type_is(&cls->regs[i], ignore))
207 int arch_count_non_ignore_regs(const arch_env_t *env,
208 const arch_register_class_t *cls)
214 for(i = 0; i < cls->n_regs; ++i) {
215 if(!arch_register_type_is(&cls->regs[i], ignore))
222 int arch_is_register_operand(const arch_env_t *env,
223 const ir_node *irn, int pos)
225 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
226 const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
231 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
232 int pos, const arch_register_t *reg)
234 const arch_register_req_t *req;
236 req = arch_get_register_req(env, irn, pos);
238 if(req->type == arch_register_req_type_none)
241 if(arch_register_req_is(req, limited)) {
242 assert(arch_register_get_class(reg) == req->cls);
243 return rbitset_is_set(req->limited, arch_register_get_index(reg));
246 return req->cls == reg->reg_class;
249 const arch_register_class_t *
250 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
252 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
253 const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
255 assert(req->type != arch_register_req_type_none || req->cls == NULL);
260 extern const arch_register_t *
261 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
263 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
264 return ops->get_irn_reg(irn);
267 extern void arch_set_irn_register(const arch_env_t *env,
268 ir_node *irn, const arch_register_t *reg)
270 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
271 ops->set_irn_reg(irn, reg);
274 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
276 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
277 return ops->classify(irn);
280 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
282 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
283 return ops->get_flags(irn);
286 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
289 #define XXX(x) case arch_irn_flags_ ## x: return #x;
292 XXX(rematerializable);
301 extern char *arch_register_req_format(char *buf, size_t len,
302 const arch_register_req_t *req,
306 snprintf(buf, len, "class: %s", req->cls->name);
308 if(arch_register_req_is(req, limited)) {
309 unsigned n_regs = req->cls->n_regs;
312 strncat(buf, " limited:", len);
313 for(i = 0; i < n_regs; ++i) {
314 if(rbitset_is_set(req->limited, i)) {
315 const arch_register_t *reg = &req->cls->regs[i];
316 strncat(buf, " ", len);
317 strncat(buf, reg->name, len);
322 if(arch_register_req_is(req, should_be_same)) {
323 const unsigned other = req->other_same;
326 ir_snprintf(tmp, sizeof(tmp), " same to:");
327 for (i = 0; 1U << i <= other; ++i) {
328 if (other & (1U << i)) {
329 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
330 strncat(buf, tmp, len);
335 if (arch_register_req_is(req, must_be_different)) {
336 const unsigned other = req->other_different;
339 ir_snprintf(tmp, sizeof(tmp), " different from:");
340 for (i = 0; 1U << i <= other; ++i) {
341 if (other & (1U << i)) {
342 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
343 strncat(buf, tmp, len);
351 static const arch_register_req_t no_requirement = {
352 arch_register_req_type_none,
358 const arch_register_req_t *arch_no_register_req = &no_requirement;