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_end;
33 static ir_op *op_push;
35 static type *push_type;
38 enum { imm_Const, imm_SymConst } tp;
46 (sizeof(reg_classes) / sizeof(reg_classes[0]))
50 static void firm_init(void)
52 static struct obstack obst;
53 static int inited = 0;
62 for(k = 0; k < N_CLASSES; ++k) {
63 const arch_register_class_t *cls = ®_classes[k];
66 for(i = 0; i < cls->n_regs; ++i) {
70 arch_register_t *reg = (arch_register_t *) &cls->regs[i];
72 n = snprintf(buf, sizeof(buf), "r%d", i);
73 name = obstack_copy0(&obst, buf, n);
83 * Create some opcodes and types to let firm look a little
84 * bit more like real machines.
87 op_push_end = new_ir_op(get_next_ir_opcode(), "PushEnd",
88 op_pin_state_pinned, 0, oparity_unary, 0, 0);
92 op_push = new_ir_op(get_next_ir_opcode(), "Push",
93 op_pin_state_pinned, 0, oparity_binary, 0, 0);
97 op_imm = new_ir_op(get_next_ir_opcode(), "Imm",
98 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t));
102 push_type = new_type_pointer(new_id_from_str("push_ptr"), get_glob_type());
106 static int firm_get_n_reg_class(void)
111 static const arch_register_class_t *firm_get_reg_class(int i)
113 assert(i >= 0 && i < N_CLASSES);
114 return ®_classes[i];
117 static const arch_register_req_t firm_std_reg_req = {
118 arch_register_req_type_normal,
119 ®_classes[CLS_DATAB],
123 static const rflct_arg_t *get_arg(const ir_node *irn, int pos)
125 int sig = rflct_get_signature(irn);
126 const rflct_arg_t *args =
127 rflct_get_args(get_irn_opcode(irn), sig, arch_pos_is_in(pos));
128 return &args[arch_pos_get_index(pos)];
131 static const arch_register_req_t *
132 firm_get_irn_reg_req(const arch_irn_ops_t *self,
133 arch_register_req_t *req, const ir_node *irn, int pos)
135 if(is_firm_be_mode(get_irn_mode(irn)))
136 memcpy(req, &firm_std_reg_req, sizeof(*req));
143 static int firm_get_n_operands(const arch_irn_ops_t *self, const ir_node *irn, int in_out)
145 int sig = rflct_get_signature(irn);
146 return rflct_get_args_count(get_irn_opcode(irn), sig, in_out >= 0);
149 struct irn_reg_assoc {
152 const arch_register_t *reg;
155 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
157 const struct irn_reg_assoc *x = a;
158 const struct irn_reg_assoc *y = b;
160 return !(x->irn == y->irn && x->pos == y->pos);
163 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, int pos)
165 static set *reg_set = NULL;
166 struct irn_reg_assoc templ;
170 reg_set = new_set(cmp_irn_reg_assoc, 1024);
175 hash = HASH_PTR(irn) + 7 * pos;
177 return set_insert(reg_set, &templ, sizeof(templ), hash);
180 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn,
181 int pos, const arch_register_t *reg)
183 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
187 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self,
188 const ir_node *irn, int pos)
190 struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
194 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
196 arch_irn_class_t res;
198 switch(get_irn_opcode(irn)) {
201 res = arch_irn_class_branch;
204 res = arch_irn_class_normal;
210 static const arch_irn_ops_t irn_ops = {
211 firm_get_irn_reg_req,
218 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
224 const arch_irn_handler_t firm_irn_handler = {
228 static ir_node *new_PushEnd(ir_graph *irg, ir_node *bl, ir_node *arg)
232 return new_ir_node(NULL, irg, bl, op_push_end, mode_P, 1, ins);
235 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
240 return new_ir_node(NULL, irg, bl, op_push, mode_P, 2, ins);
243 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst)
249 res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
250 attr = (imm_attr_t *) &res->attr;
252 if(get_irn_opcode(cnst) == iro_SymConst) {
253 attr->tp = imm_SymConst;
254 attr->data.ent = get_SymConst_entity(cnst);
258 attr->tp = imm_Const;
259 attr->data.tv = get_Const_tarval(cnst);
265 static void prepare_walker(ir_node *irn, void *data)
267 opcode opc = get_irn_opcode(irn);
269 /* A replacement for this node has already been computed. */
270 if(get_irn_link(irn))
273 if(opc == iro_Call) {
274 ir_node *bl = get_nodes_block(irn);
275 ir_graph *irg = get_irn_irg(bl);
278 ir_node *store = get_Call_mem(irn);
279 ir_node *ptr = get_Call_ptr(irn);
280 type *ct = get_Call_type(irn);
281 int np = get_Call_n_params(irn) > 0 ? 1 : 0;
290 push = new_PushEnd(irg, bl, get_Call_param(irn, 0));
292 for(i = 1, n = get_Call_n_params(irn); i < n; ++i) {
293 push = new_Push(irg, bl, push, get_Call_param(irn, i));
296 snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
298 n = get_method_n_ress(ct);
299 nt = new_type_method(new_id_from_str(buf), 1, n);
300 for(i = 0; i < n; ++i)
301 set_method_res_type(nt, i, get_method_res_type(ct, i));
303 set_method_param_type(nt, 0, push_type);
306 nc = new_r_Call(irg, bl, store, ptr, 1, ins, nt);
308 set_irn_link(nc, nc);
313 else if(opc == iro_Const || opc == iro_SymConst) {
314 ir_node *bl = get_nodes_block(irn);
315 ir_graph *irg = get_irn_irg(bl);
317 ir_node *imm = new_Imm(irg, bl, irn);
319 set_irn_link(imm, imm);
325 static void clear_link(ir_node *irn, void *data)
327 set_irn_link(irn, NULL);
330 static void firm_prepare_graph(ir_graph *irg)
332 irg_walk_graph(irg, clear_link, NULL, NULL);
333 irg_walk_graph(irg, NULL, prepare_walker, NULL);
336 const arch_isa_if_t firm_isa = {
338 firm_get_n_reg_class,