2 * Copyright (C) 1995-2007 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 /* Initialize the architecture environment struct. */
44 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)
46 memset(env, 0, sizeof(*env));
47 env->isa = isa_if->init(file_handle);
48 env->isa->main_env = main_env;
52 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
53 const arch_irn_handler_t *handler)
55 assert(env->handlers_tos < ARCH_MAX_HANDLERS);
56 env->handlers[env->handlers_tos++] = handler;
60 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
62 assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
63 return env->handlers[--env->handlers_tos];
66 static const arch_irn_ops_t *fallback_irn_ops = NULL;
68 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
72 for(i = 0, n = cls->n_regs; i < n; ++i)
80 * Get the isa responsible for a node.
81 * @param env The arch environment with the isa stack.
82 * @param irn The node to get the responsible isa for.
83 * @return The irn operations given by the responsible isa.
85 static INLINE const arch_irn_ops_t *
86 get_irn_ops(const arch_env_t *env, const ir_node *irn)
90 for(i = env->handlers_tos - 1; i >= 0; --i) {
91 const arch_irn_handler_t *handler = env->handlers[i];
92 const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
98 return fallback_irn_ops;
101 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
102 return get_irn_ops(env, irn);
105 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
106 const ir_node *irn, int pos)
108 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
109 return ops->impl->get_irn_reg_req(ops, irn, pos);
112 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
114 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
115 ops->impl->set_frame_offset(ops, irn, offset);
118 ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
120 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
121 return ops->impl->get_frame_entity(ops, irn);
124 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
126 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
127 ops->impl->set_frame_entity(ops, irn, ent);
130 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
132 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
133 return ops->impl->get_sp_bias(ops, irn);
136 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
138 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
140 if(ops->impl->get_inverse) {
141 return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
147 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
148 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
150 if(ops->impl->possible_memory_operand) {
151 return ops->impl->possible_memory_operand(ops, irn, i);
157 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
158 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
160 if(ops->impl->perform_memory_operand) {
161 ops->impl->perform_memory_operand(ops, irn, spill, i);
167 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
169 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
171 if(ops->impl->get_op_estimated_cost) {
172 return ops->impl->get_op_estimated_cost(ops, irn);
178 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
180 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
182 if(ops->impl->possible_memory_operand) {
183 return ops->impl->possible_memory_operand(ops, irn, i);
189 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
191 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
192 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
194 if(req->type == arch_register_req_type_none) {
195 bitset_clear_all(bs);
199 if(arch_register_req_is(req, limited)) {
200 rbitset_copy_to_bitset(req->limited, bs);
201 return bitset_popcnt(bs);
204 arch_register_class_put(req->cls, bs);
205 return req->cls->n_regs;
208 void arch_put_non_ignore_regs(const arch_env_t *env,
209 const arch_register_class_t *cls, bitset_t *bs)
214 for(i = 0; i < cls->n_regs; ++i) {
215 if(!arch_register_type_is(&cls->regs[i], ignore))
220 int arch_count_non_ignore_regs(const arch_env_t *env,
221 const arch_register_class_t *cls)
227 for(i = 0; i < cls->n_regs; ++i) {
228 if(!arch_register_type_is(&cls->regs[i], ignore))
235 int arch_is_register_operand(const arch_env_t *env,
236 const ir_node *irn, int pos)
238 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
239 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
244 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
245 int pos, const arch_register_t *reg)
247 const arch_register_req_t *req;
249 req = arch_get_register_req(env, irn, pos);
251 if(req->type == arch_register_req_type_none)
254 if(arch_register_req_is(req, limited)) {
255 assert(arch_register_get_class(reg) == req->cls);
256 return rbitset_is_set(req->limited, arch_register_get_index(reg));
259 return req->cls == reg->reg_class;
262 const arch_register_class_t *
263 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
265 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
266 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
268 assert(req->type != arch_register_req_type_none || req->cls == NULL);
273 extern const arch_register_t *
274 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
276 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
277 return ops->impl->get_irn_reg(ops, irn);
280 extern void arch_set_irn_register(const arch_env_t *env,
281 ir_node *irn, const arch_register_t *reg)
283 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
284 ops->impl->set_irn_reg(ops, irn, reg);
287 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
289 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
290 return ops->impl->classify(ops, irn);
293 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
295 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
296 return ops->impl->get_flags(ops, irn);
299 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
302 #define XXX(x) case arch_irn_flags_ ## x: return #x;
305 XXX(rematerializable);
314 extern char *arch_register_req_format(char *buf, size_t len,
315 const arch_register_req_t *req,
319 snprintf(buf, len, "class: %s", req->cls->name);
321 if(arch_register_req_is(req, limited)) {
322 unsigned n_regs = req->cls->n_regs;
325 strncat(buf, " limited:", len);
326 for(i = 0; i < n_regs; ++i) {
327 if(rbitset_is_set(req->limited, i)) {
328 const arch_register_t *reg = &req->cls->regs[i];
329 strncat(buf, " ", len);
330 strncat(buf, reg->name, len);
335 if(arch_register_req_is(req, should_be_same)) {
336 const unsigned other = req->other_same;
339 ir_snprintf(tmp, sizeof(tmp), " same to:");
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);
348 if(arch_register_req_is(req, should_be_different)) {
349 const unsigned other = req->other_different;
352 ir_snprintf(tmp, sizeof(tmp), " different from:");
353 for (i = 0; 1U << i <= other; ++i) {
354 if (other & (1U << i)) {
355 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
356 strncat(buf, tmp, len);
364 static const arch_register_req_t no_requirement = {
365 arch_register_req_type_none,
371 const arch_register_req_t *arch_no_register_req = &no_requirement;