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
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 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
54 const arch_irn_handler_t *handler)
56 assert(env->handlers_tos < ARCH_MAX_HANDLERS);
57 env->handlers[env->handlers_tos++] = handler;
61 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
63 assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
64 return env->handlers[--env->handlers_tos];
67 static const arch_irn_ops_t *fallback_irn_ops = NULL;
69 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
73 for(i = 0, n = cls->n_regs; i < n; ++i)
81 * Get the isa responsible for a node.
82 * @param env The arch environment with the isa stack.
83 * @param irn The node to get the responsible isa for.
84 * @return The irn operations given by the responsible isa.
86 static INLINE const arch_irn_ops_t *
87 get_irn_ops(const arch_env_t *env, const ir_node *irn)
91 for(i = env->handlers_tos - 1; i >= 0; --i) {
92 const arch_irn_handler_t *handler = env->handlers[i];
93 const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
99 return fallback_irn_ops;
102 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
103 return get_irn_ops(env, irn);
106 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
107 const ir_node *irn, int pos)
109 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
110 return ops->impl->get_irn_reg_req(ops, irn, pos);
113 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
115 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
116 ops->impl->set_frame_offset(ops, irn, offset);
119 ir_entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
121 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
122 return ops->impl->get_frame_entity(ops, irn);
125 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
127 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
128 ops->impl->set_frame_entity(ops, irn, ent);
131 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
133 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
134 return ops->impl->get_sp_bias(ops, irn);
137 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
139 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
141 if(ops->impl->get_inverse) {
142 return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
148 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
149 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
151 if(ops->impl->possible_memory_operand) {
152 return ops->impl->possible_memory_operand(ops, irn, i);
158 void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
159 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
161 if(ops->impl->perform_memory_operand) {
162 ops->impl->perform_memory_operand(ops, irn, spill, i);
168 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
170 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
172 if(ops->impl->get_op_estimated_cost) {
173 return ops->impl->get_op_estimated_cost(ops, irn);
179 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
181 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
183 if(ops->impl->possible_memory_operand) {
184 return ops->impl->possible_memory_operand(ops, irn, i);
190 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
192 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
193 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
195 if(req->type == arch_register_req_type_none) {
196 bitset_clear_all(bs);
200 if(arch_register_req_is(req, limited)) {
201 rbitset_copy_to_bitset(req->limited, bs);
202 return bitset_popcnt(bs);
205 arch_register_class_put(req->cls, bs);
206 return req->cls->n_regs;
209 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
213 for(i = 0; i < cls->n_regs; ++i) {
214 if(!arch_register_type_is(&cls->regs[i], ignore))
219 int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls)
224 for(i = 0; i < cls->n_regs; ++i) {
225 if(!arch_register_type_is(&cls->regs[i], ignore))
232 int arch_is_register_operand(const arch_env_t *env,
233 const ir_node *irn, int pos)
235 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
236 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
241 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
242 int pos, const arch_register_t *reg)
244 const arch_register_req_t *req;
246 req = arch_get_register_req(env, irn, pos);
248 if(req->type == arch_register_req_type_none)
251 if(arch_register_req_is(req, limited)) {
252 assert(arch_register_get_class(reg) == req->cls);
253 return rbitset_is_set(req->limited, arch_register_get_index(reg));
256 return req->cls == reg->reg_class;
259 const arch_register_class_t *
260 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
262 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
263 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);
265 assert(req->type != arch_register_req_type_none || req->cls == NULL);
270 extern const arch_register_t *
271 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
273 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
274 return ops->impl->get_irn_reg(ops, irn);
277 extern void arch_set_irn_register(const arch_env_t *env,
278 ir_node *irn, const arch_register_t *reg)
280 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
281 ops->impl->set_irn_reg(ops, irn, reg);
284 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
286 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
287 return ops->impl->classify(ops, irn);
290 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
292 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
293 return ops->impl->get_flags(ops, irn);
296 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
299 #define XXX(x) case arch_irn_flags_ ## x: return #x;
302 XXX(rematerializable);
310 extern char *arch_register_req_format(char *buf, size_t len,
311 const arch_register_req_t *req,
315 snprintf(buf, len, "class: %s", req->cls->name);
317 if(arch_register_req_is(req, limited)) {
318 unsigned n_regs = req->cls->n_regs;
321 strncat(buf, " limited:", len);
322 for(i = 0; i < n_regs; ++i) {
323 if(rbitset_is_set(req->limited, i)) {
324 const arch_register_t *reg = &req->cls->regs[i];
325 strncat(buf, " ", len);
326 strncat(buf, reg->name, len);
331 if(arch_register_req_is(req, should_be_same)) {
332 const ir_node *same = get_irn_n(node, req->other_same);
333 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", same);
334 strncat(buf, tmp, len);
337 if(arch_register_req_is(req, should_be_different)) {
338 const ir_node *different = get_irn_n(node, req->other_different);
339 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", different);
340 strncat(buf, tmp, len);
346 static const arch_register_req_t no_requirement = {
347 arch_register_req_type_none,
353 const arch_register_req_t *arch_no_register_req = &no_requirement;