3 * ISA implementation for Firm IR nodes.
22 #include "irreflect.h"
26 static arch_register_t datab_regs[N_REGS];
28 static arch_register_class_t reg_classes[] = {
29 { "datab", N_REGS, datab_regs },
32 static ir_op *op_push;
36 enum { imm_Const, imm_SymConst } tp;
44 (sizeof(reg_classes) / sizeof(reg_classes[0]))
48 static void firm_init(void)
50 static struct obstack obst;
51 static int inited = 0;
60 for(k = 0; k < N_CLASSES; ++k) {
61 const arch_register_class_t *cls = ®_classes[k];
64 for(i = 0; i < cls->n_regs; ++i) {
68 arch_register_t *reg = (arch_register_t *) &cls->regs[i];
70 n = snprintf(buf, sizeof(buf), "r%d", i);
71 name = obstack_copy0(&obst, buf, n);
81 * Create some opcodes and types to let firm look a little
82 * bit more like real machines.
86 int push_opc = get_next_ir_opcode();
88 op_push = new_ir_op(push_opc, "Push",
89 op_pin_state_pinned, 0, oparity_binary, 0, 0);
91 sig = rflct_signature_allocate(1, 3);
92 rflct_signature_set_arg(sig, 0, 0, "Store", RFLCT_MC(Mem), 0, 0);
93 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
94 rflct_signature_set_arg(sig, 1, 1, "Store", RFLCT_MC(Mem), 0, 0);
95 rflct_signature_set_arg(sig, 1, 2, "Arg", RFLCT_MC(Datab), 0, 0);
97 rflct_new_opcode(push_opc, "Push", false);
98 rflct_opcode_add_signature(push_opc, sig);
103 int imm_opc = get_next_ir_opcode();
105 op_imm = new_ir_op(imm_opc, "Imm",
106 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t));
108 sig = rflct_signature_allocate(1, 1);
109 rflct_signature_set_arg(sig, 0, 0, "Imm", RFLCT_MC(Data), 0, 0);
110 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
111 rflct_new_opcode(imm_opc, "Imm", false);
112 rflct_opcode_add_signature(imm_opc, sig);
116 static int firm_get_n_reg_class(void)
121 static const arch_register_class_t *firm_get_reg_class(int i)
123 assert(i >= 0 && i < N_CLASSES);
124 return ®_classes[i];
127 static const arch_register_req_t firm_std_reg_req = {
128 arch_register_req_type_normal,
129 ®_classes[CLS_DATAB],
133 static const rflct_arg_t *get_arg(const ir_node *irn, int pos)
135 int sig = rflct_get_signature(irn);
136 const rflct_arg_t *args =
137 rflct_get_args(get_irn_opcode(irn), sig, arch_pos_is_in(pos));
138 return &args[arch_pos_get_index(pos)];
141 static const arch_register_req_t *
142 firm_get_irn_reg_req(const arch_irn_ops_t *self,
143 arch_register_req_t *req, const ir_node *irn, int pos)
145 if(is_firm_be_mode(get_irn_mode(irn)))
146 memcpy(req, &firm_std_reg_req, sizeof(*req));
153 static int firm_get_n_operands(const arch_irn_ops_t *self, const ir_node *irn, int in_out)
158 irn = get_Proj_pred(irn);
160 sig = rflct_get_signature(irn);
161 return rflct_get_args_count(get_irn_opcode(irn), sig, in_out >= 0);
164 struct irn_reg_assoc {
167 const arch_register_t *reg;
170 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
172 const struct irn_reg_assoc *x = a;
173 const struct irn_reg_assoc *y = b;
175 return !(x->irn == y->irn && x->pos == y->pos);
178 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, int pos)
180 static set *reg_set = NULL;
181 struct irn_reg_assoc templ;
185 reg_set = new_set(cmp_irn_reg_assoc, 1024);
190 hash = HASH_PTR(irn) + 7 * pos;
192 return set_insert(reg_set, &templ, sizeof(templ), hash);
195 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn,
196 int pos, const arch_register_t *reg)
198 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
202 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self,
203 const ir_node *irn, int pos)
205 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
209 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
211 arch_irn_class_t res;
213 switch(get_irn_opcode(irn)) {
216 res = arch_irn_class_branch;
219 res = arch_irn_class_normal;
225 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
227 arch_irn_flags_t res = arch_irn_flags_spillable;
229 if(get_irn_op(irn) == op_imm)
230 res |= arch_irn_flags_rematerializable;
232 switch(get_irn_opcode(irn)) {
242 res |= arch_irn_flags_rematerializable;
250 static const arch_irn_ops_t irn_ops = {
251 firm_get_irn_reg_req,
259 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
265 const arch_irn_handler_t firm_irn_handler = {
269 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
274 return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
277 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst)
283 res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
284 attr = (imm_attr_t *) &res->attr;
286 if(get_irn_opcode(cnst) == iro_SymConst) {
287 attr->tp = imm_SymConst;
288 attr->data.ent = get_SymConst_entity(cnst);
292 attr->tp = imm_Const;
293 attr->data.tv = get_Const_tarval(cnst);
299 static void prepare_walker(ir_node *irn, void *data)
301 opcode opc = get_irn_opcode(irn);
303 /* A replacement for this node has already been computed. */
304 if(get_irn_link(irn))
307 if(opc == iro_Call) {
308 ir_node *bl = get_nodes_block(irn);
309 ir_graph *irg = get_irn_irg(bl);
311 ir_node *store = get_Call_mem(irn);
312 ir_node *ptr = get_Call_ptr(irn);
313 type *ct = get_Call_type(irn);
314 int np = get_Call_n_params(irn) > 0 ? 1 : 0;
324 store = new_Push(irg, bl, store, get_Call_param(irn, 0));
326 for(i = 1, n = get_Call_n_params(irn); i < n; ++i) {
327 store = new_Push(irg, bl, store, get_Call_param(irn, i));
330 snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
332 n = get_method_n_ress(ct);
333 nt = new_type_method(new_id_from_str(buf), 0, n);
334 for(i = 0; i < n; ++i)
335 set_method_res_type(nt, i, get_method_res_type(ct, i));
337 nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
339 set_irn_link(nc, nc);
344 static void localize_const_walker(ir_node *irn, void *data)
348 ir_node *bl = get_nodes_block(irn);
350 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
351 ir_node *op = get_irn_n(irn, i);
352 opcode opc = get_irn_opcode(op);
355 || (opc == iro_SymConst && get_SymConst_kind(op) == symconst_addr_ent)) {
356 ir_graph *irg = get_irn_irg(bl);
357 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
359 ir_node *imm = new_Imm(irg, imm_bl, op);
360 set_irn_n(irn, i, imm);
366 static void clear_link(ir_node *irn, void *data)
368 set_irn_link(irn, NULL);
371 static void firm_prepare_graph(ir_graph *irg)
373 irg_walk_graph(irg, clear_link, localize_const_walker, NULL);
374 irg_walk_graph(irg, NULL, prepare_walker, NULL);
377 const arch_isa_if_t firm_isa = {
379 firm_get_n_reg_class,