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(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
47 memset(env, 0, sizeof(*env));
48 env->isa = isa_if->init(file_handle);
49 env->isa->main_env = main_env;
53 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
57 for(i = 0, n = cls->n_regs; i < n; ++i)
65 * Get the isa responsible for a node.
66 * @param env The arch environment with the isa stack.
67 * @param irn The node to get the responsible isa for.
68 * @return The irn operations given by the responsible isa.
70 static INLINE const arch_irn_ops_t *
71 get_irn_ops(const arch_env_t *env, const ir_node *irn)
74 const arch_irn_ops_t *be_ops;
78 irn = get_Proj_pred(irn);
79 assert(!is_Proj(irn));
82 ops = get_irn_op(irn);
83 be_ops = get_op_ops(ops)->be_ops;
89 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
90 return get_irn_ops(env, irn);
93 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
94 const ir_node *irn, int pos)
96 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
97 return ops->get_irn_reg_req(irn, pos);
100 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
102 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
103 ops->set_frame_offset(irn, offset);
106 ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
108 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
109 return ops->get_frame_entity(irn);
112 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
114 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
115 ops->set_frame_entity(irn, ent);
118 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
120 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
121 return ops->get_sp_bias(irn);
124 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
126 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
128 if(ops->get_inverse) {
129 return ops->get_inverse(irn, i, inverse, obstack);
135 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
136 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
138 if(ops->possible_memory_operand) {
139 return ops->possible_memory_operand(irn, i);
145 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
146 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
148 if(ops->perform_memory_operand) {
149 ops->perform_memory_operand(irn, spill, i);
155 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
157 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
159 if(ops->get_op_estimated_cost) {
160 return ops->get_op_estimated_cost(irn);
166 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
168 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
170 if(ops->possible_memory_operand) {
171 return ops->possible_memory_operand(irn, i);
177 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
179 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
180 const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
182 if(req->type == arch_register_req_type_none) {
183 bitset_clear_all(bs);
187 if(arch_register_req_is(req, limited)) {
188 rbitset_copy_to_bitset(req->limited, bs);
189 return bitset_popcnt(bs);
192 arch_register_class_put(req->cls, bs);
193 return req->cls->n_regs;
196 void arch_put_non_ignore_regs(const arch_env_t *env,
197 const arch_register_class_t *cls, bitset_t *bs)
202 for(i = 0; i < cls->n_regs; ++i) {
203 if(!arch_register_type_is(&cls->regs[i], ignore))
208 int arch_count_non_ignore_regs(const arch_env_t *env,
209 const arch_register_class_t *cls)
215 for(i = 0; i < cls->n_regs; ++i) {
216 if(!arch_register_type_is(&cls->regs[i], ignore))
223 int arch_is_register_operand(const arch_env_t *env,
224 const ir_node *irn, int pos)
226 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
227 const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
232 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
233 int pos, const arch_register_t *reg)
235 const arch_register_req_t *req;
237 req = arch_get_register_req(env, irn, pos);
239 if(req->type == arch_register_req_type_none)
242 if(arch_register_req_is(req, limited)) {
243 assert(arch_register_get_class(reg) == req->cls);
244 return rbitset_is_set(req->limited, arch_register_get_index(reg));
247 return req->cls == reg->reg_class;
250 const arch_register_class_t *
251 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
253 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
254 const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
256 assert(req->type != arch_register_req_type_none || req->cls == NULL);
261 extern const arch_register_t *
262 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
264 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
265 return ops->get_irn_reg(irn);
268 extern void arch_set_irn_register(const arch_env_t *env,
269 ir_node *irn, const arch_register_t *reg)
271 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
272 ops->set_irn_reg(irn, reg);
275 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
277 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
278 return ops->classify(irn);
281 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
283 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
284 return ops->get_flags(irn);
287 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
290 #define XXX(x) case arch_irn_flags_ ## x: return #x;
293 XXX(rematerializable);
302 extern char *arch_register_req_format(char *buf, size_t len,
303 const arch_register_req_t *req,
307 snprintf(buf, len, "class: %s", req->cls->name);
309 if(arch_register_req_is(req, limited)) {
310 unsigned n_regs = req->cls->n_regs;
313 strncat(buf, " limited:", len);
314 for(i = 0; i < n_regs; ++i) {
315 if(rbitset_is_set(req->limited, i)) {
316 const arch_register_t *reg = &req->cls->regs[i];
317 strncat(buf, " ", len);
318 strncat(buf, reg->name, len);
323 if(arch_register_req_is(req, should_be_same)) {
324 const unsigned other = req->other_same;
327 ir_snprintf(tmp, sizeof(tmp), " same to:");
328 for (i = 0; 1U << i <= other; ++i) {
329 if (other & (1U << i)) {
330 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
331 strncat(buf, tmp, len);
336 if(arch_register_req_is(req, should_be_different)) {
337 const unsigned other = req->other_different;
340 ir_snprintf(tmp, sizeof(tmp), " different from:");
341 for (i = 0; 1U << i <= other; ++i) {
342 if (other & (1U << i)) {
343 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
344 strncat(buf, tmp, len);
352 static const arch_register_req_t no_requirement = {
353 arch_register_req_type_none,
359 const arch_register_req_t *arch_no_register_req = &no_requirement;