2 * Processor architecture specification.
3 * @author Sebastian Hack
33 /* Initialize the architecture environment struct. */
34 arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle)
36 memset(env, 0, sizeof(*env));
37 env->isa = isa_if->init(file_handle);
41 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
42 const arch_irn_handler_t *handler)
44 assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
45 env->handlers[env->handlers_tos++] = handler;
49 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
51 assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
52 return env->handlers[--env->handlers_tos];
55 static const arch_irn_ops_t *fallback_irn_ops = NULL;
57 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
61 for(i = 0, n = cls->n_regs; i < n; ++i)
69 * Get the isa responsible for a node.
70 * @param env The arch environment with the isa stack.
71 * @param irn The node to get the responsible isa for.
72 * @return The irn operations given by the responsible isa.
74 static INLINE const arch_irn_ops_t *
75 get_irn_ops(const arch_env_t *env, const ir_node *irn)
79 for(i = env->handlers_tos - 1; i >= 0; --i) {
80 const arch_irn_handler_t *handler = env->handlers[i];
81 const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
87 return fallback_irn_ops;
90 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
91 arch_register_req_t *req, const ir_node *irn, int pos)
93 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
94 req->type = arch_register_req_type_none;
95 return ops->impl->get_irn_reg_req(ops, req, irn, pos);
98 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
100 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
101 ops->impl->set_frame_offset(ops, irn, offset);
104 entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
106 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
107 return ops->impl->get_frame_entity(ops, irn);
110 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
112 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
113 if(ops->impl->get_inverse) {
114 return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
120 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
122 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
123 if(ops->impl->get_op_estimated_cost) {
124 return ops->impl->get_op_estimated_cost(ops, irn);
130 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
132 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
133 if(ops->impl->possible_memory_operand) {
134 return ops->impl->possible_memory_operand(ops, irn, i);
140 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
142 arch_register_req_t local_req;
143 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
144 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
146 if(req->type == arch_register_req_type_none) {
147 bitset_clear_all(bs);
151 if(arch_register_req_is(req, limited)) {
152 req->limited(req->limited_env, bs);
153 return bitset_popcnt(bs);
156 arch_register_class_put(req->cls, bs);
157 return req->cls->n_regs;
160 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
164 for(i = 0; i < cls->n_regs; ++i) {
165 if(!arch_register_type_is(&cls->regs[i], ignore))
170 int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls)
175 for(i = 0; i < cls->n_regs; ++i) {
176 if(!arch_register_type_is(&cls->regs[i], ignore))
183 int arch_is_register_operand(const arch_env_t *env,
184 const ir_node *irn, int pos)
186 arch_register_req_t local_req;
187 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
188 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
192 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
193 int pos, const arch_register_t *reg)
195 arch_register_req_t req;
197 arch_get_register_req(env, &req, irn, pos);
199 if(req.type == arch_register_req_type_none)
202 if(arch_register_req_is(&req, limited)) {
203 bitset_t *bs = bitset_alloca(req.cls->n_regs);
204 req.limited(req.limited_env, bs);
205 return bitset_is_set(bs, arch_register_get_index(reg));
208 return req.cls == reg->reg_class;
211 const arch_register_class_t *
212 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
214 arch_register_req_t local_req;
215 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
216 const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
217 return req ? req->cls : NULL;
220 extern const arch_register_t *
221 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
223 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
224 return ops->impl->get_irn_reg(ops, irn);
227 extern void arch_set_irn_register(const arch_env_t *env,
228 ir_node *irn, const arch_register_t *reg)
230 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
231 ops->impl->set_irn_reg(ops, irn, reg);
234 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
236 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
237 return ops->impl->classify(ops, irn);
240 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
242 const arch_irn_ops_t *ops = get_irn_ops(env, irn);
243 return ops->impl->get_flags(ops, irn);
246 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
249 #define XXX(x) case arch_irn_flags_ ## x: return #x;
252 XXX(rematerializable);
260 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
263 snprintf(buf, len, "class: %s", req->cls->name);
265 if(arch_register_req_is(req, limited)) {
267 bitset_t *bs = bitset_alloca(req->cls->n_regs);
268 req->limited(req->limited_env, bs);
269 strncat(buf, " limited:", len);
270 bitset_foreach(bs, elm) {
271 strncat(buf, " ", len);
272 strncat(buf, req->cls->regs[elm].name, len);
276 if(arch_register_req_is(req, should_be_same)) {
277 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
278 strncat(buf, tmp, len);
281 if(arch_register_req_is(req, should_be_different)) {
282 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
283 strncat(buf, tmp, len);