4 from jinja2 import Environment, Template
5 from jinja2.filters import do_dictsort
6 from spec_util import is_dynamic_pinned, verify_node, isAbstract, setdefault
7 from ir_spec import nodes
9 def format_parameterlist(parameterlist):
10 return "\n".join(parameterlist)
12 def format_nodearguments(node):
13 arguments = map(lambda arg: arg["name"], node.arguments)
14 return format_parameterlist(arguments)
16 def format_nodeparameters(node):
17 parameters = map(lambda arg: arg["type"] + " " + arg["name"], node.arguments)
18 return format_parameterlist(parameters)
20 def format_blockparameter(node):
21 if hasattr(node, "knownBlock"):
22 if hasattr(node, "knownGraph"):
24 return "ir_graph *irg"
26 return "ir_node *block"
28 def format_blockargument(node):
29 if hasattr(node, "knownBlock"):
30 if hasattr(node, "knownGraph"):
36 def format_irgassign(node):
37 if hasattr(node, "knownGraph"):
38 return "ir_graph *irg = %s;\n" % node.graph
40 if hasattr(node, "knownBlock"):
43 return "ir_graph *irg = get_Block_irg(block);\n"
45 def format_curblock(node):
46 if hasattr(node, "knownBlock"):
47 if hasattr(node, "knownGraph"):
49 return "current_ir_graph"
51 return "current_ir_graph->current_block"
53 def format_insdecl(node):
55 if arity == "variable" and len(node.ins) == 0 or arity == "dynamic" or arity == 0:
58 if arity == "variable":
59 insarity = len(node.ins)
60 res = "int r_arity = arity + " + `insarity` + ";"
61 res += "\n\tir_node **r_in;"
62 res += "\n\tNEW_ARR_A(ir_node *, r_in, r_arity);"
64 for input in node.ins:
65 res += "\n\tr_in[" + `i` + "] = irn_" + input + ";"
67 res += "\n\tmemcpy(&r_in[" + `insarity` + "], in, sizeof(ir_node *) * arity);\n\t"
69 res = "ir_node *in[" + `arity` + "];"
71 for input in node.ins:
72 res += "\n\tin[" + `i` + "] = irn_" + input + ";"
76 def format_arity_and_ins(node):
78 if arity == "dynamic":
80 elif arity == "variable":
81 if len(node.ins) == 0:
84 return "r_arity, r_in"
88 return `arity` + ", in"
90 def format_arity(node):
91 if hasattr(node, "arity_override"):
92 return node.arity_override
94 if arity == "dynamic":
95 return "oparity_dynamic"
96 if arity == "variable":
97 return "oparity_variable"
101 return "oparity_unary"
103 return "oparity_binary"
105 return "oparity_trinary"
108 def format_pinned(node):
111 return "op_pin_state_pinned"
113 return "op_pin_state_floats"
114 if pinned == "exception":
115 return "op_pin_state_exc_pinned"
116 if pinned == "memory":
117 return "op_pin_state_mem_pinned"
118 print "WARNING: Unknown pinned state %s in format pined" % pinned
121 def format_flags(node):
122 flags = map(lambda x : "irop_flag_" + x, node.flags)
124 flags = [ "irop_flag_none" ]
125 return " | ".join(flags)
127 def format_attr_size(node):
128 if not hasattr(node, "attr_struct"):
130 return "sizeof(%s)" % node.attr_struct
132 def format_opindex(node):
133 if hasattr(node, "op_index"):
137 def filter_isnot(list, flag):
138 return filter(lambda x: not hasattr(x, flag), list)
140 def filter_hasnot(list, flag):
141 return filter(lambda x: flag not in x, list)
143 def format_arguments(string, voidwhenempty = False):
144 args = re.split('\s*\n\s*', string)
147 if len(args) > 0 and args[-1] == '':
149 if len(args) == 0 and voidwhenempty:
151 return ", ".join(args)
153 def format_parameters(string):
154 return format_arguments(string, voidwhenempty = True)
157 env.filters['parameterlist'] = format_parameterlist
158 env.filters['nodearguments'] = format_nodearguments
159 env.filters['nodeparameters'] = format_nodeparameters
160 env.filters['blockparameter'] = format_blockparameter
161 env.filters['blockargument'] = format_blockargument
162 env.filters['irgassign'] = format_irgassign
163 env.filters['curblock'] = format_curblock
164 env.filters['insdecl'] = format_insdecl
165 env.filters['arity_and_ins'] = format_arity_and_ins
166 env.filters['arity'] = format_arity
167 env.filters['pinned'] = format_pinned
168 env.filters['flags'] = format_flags
169 env.filters['attr_size'] = format_attr_size
170 env.filters['opindex'] = format_opindex
171 env.filters['isnot'] = filter_isnot
172 env.filters['hasnot'] = filter_hasnot
173 env.filters['arguments'] = format_arguments
174 env.filters['parameters'] = format_parameters
176 def prepare_attr(attr):
178 return dict(type = attr["type"], name = attr["name"], init = attr["init"])
180 return dict(type = attr["type"], name = attr["name"])
182 def preprocess_node(node):
185 setdefault(node, "attrs_name", node.name.lower())
186 setdefault(node, "block", "block")
188 # construct node arguments
192 for input in node.ins:
193 arguments.append(dict(type = "ir_node *", name = "irn_" + input))
195 if node.arity == "variable" or node.arity == "dynamic":
196 arguments.append(dict(type = "int", name = "arity"))
197 arguments.append(dict(type = "ir_node **", name = "in"))
199 if not hasattr(node, "mode"):
200 arguments.append(dict(type = "ir_mode *", name = "mode"))
203 attrs_with_special = 0
204 for attr in node.attrs:
205 attr.setdefault("initname", "." + attr["name"])
207 if "special" in attr:
208 if not "init" in attr:
209 print "Node type %s has an attribute with a \"special\" entry but without \"init\"" % node.name
212 if attrs_with_special != 0:
213 print "Node type %s has more than one attribute with a \"special\" entry" % node.name
216 attrs_with_special += 1
218 if "prefix" in attr["special"]:
219 specialname = attr["special"]["prefix"] + node.name
220 elif "suffix" in attr["special"]:
221 specialname = node.name + attr["special"]["suffix"]
223 print "Unknown special constructor type for node type %s" % node.name
226 specialconstrs.append(
228 constrname = specialname,
232 elif not "init" in attr:
233 arguments.append(prepare_attr(attr))
235 # dynamic pin state means more constructor arguments
236 if is_dynamic_pinned(node):
237 if hasattr(node, "pinned_init"):
238 initattrs.append(dict(
239 initname = ".exc.pin_state",
240 init = node.pinned_init
243 node.constructor_args.append(
246 type = "op_pin_state"
249 initattrs.append(dict(
250 initname = ".exc.pin_state",
254 for arg in node.constructor_args:
255 arguments.append(prepare_attr(arg))
256 if arg["type"] == "ir_cons_flags":
258 initattrs.append(dict(initname = ".exc.pin_state",
259 init = name + " & cons_floats ? op_pin_state_floats : op_pin_state_pinned"))
260 initattrs.append(dict(initname = ".volatility",
261 init = name + " & cons_volatile ? volatility_is_volatile : volatility_non_volatile"))
262 initattrs.append(dict(initname = ".aligned",
263 init = name + " & cons_unaligned ? align_non_aligned : align_is_aligned"))
265 node.arguments = arguments
266 node.initattrs = initattrs
267 node.special_constructors = specialconstrs
269 #############################
271 constructor_template = env.from_string('''
273 ir_node *new_rd_{{node.constrname}}(
274 {%- filter parameters %}
276 {{node|blockparameter}}
277 {{node|nodeparameters}}
281 ir_graph *rem = current_ir_graph;
284 current_ir_graph = irg;
286 {%- filter arguments %}
292 {{node|arity_and_ins}}
294 {%- for attr in node.attrs %}
295 res->attr.{{node.attrs_name}}{{attr["initname"]}} =
296 {%- if "init" in attr %} {{ attr["init"] -}};
297 {%- else %} {{ attr["name"] -}};
300 {%- for attr in node.initattrs %}
301 res->attr.{{node.attrs_name}}{{attr["initname"]}} = {{ attr["init"] -}};
304 {%- if node.optimize != False %}
305 res = optimize_node(res);
307 IRN_VRFY_IRG(res, irg);
308 current_ir_graph = rem;
312 ir_node *new_r_{{node.constrname}}(
313 {%- filter parameters %}
314 {{node|blockparameter}}
315 {{node|nodeparameters}}
318 return new_rd_{{node.constrname}}(
319 {%- filter arguments %}
321 {{node|blockargument}}
322 {{node|nodearguments}}
326 ir_node *new_d_{{node.constrname}}(
327 {%- filter parameters %}
329 {{node|nodeparameters}}
333 res = new_rd_{{node.constrname}}(
334 {%- filter parameters %}
337 {{node|nodearguments}}
339 {%- if "fragile" in node.flags %}
340 firm_alloc_frag_arr(res, op_{{node.name}}, &res->attr.except.frag_arr);
345 ir_node *new_{{node.constrname}}(
346 {%- filter parameters %}
347 {{node|nodeparameters}}
350 return new_d_{{node.constrname}}(
351 {%- filter arguments %}
353 {{node|nodearguments}}
358 irnode_h_template = env.from_string('''
359 /* Warning: automatically generated code */
361 {%- for node in nodes|isnot('custom_is') %}
362 static inline int _is_{{node.name}}(const ir_node *node)
364 assert(node != NULL);
365 return _get_irn_op(node) == op_{{node.name}};
369 {% for node in nodes %}
370 #define is_{{node.name}}(node) _is_{{node.name}}(node)
375 irnode_template = env.from_string('''
376 /* Warning: automatically generated code */
377 {% for node in nodes %}
378 int (is_{{node.name}})(const ir_node *node)
380 return _is_{{node.name}}(node);
384 {%- for node in nodes %}
385 {%- for attr in node.attrs|hasnot("noprop") %}
386 {{attr.type}} (get_{{node.name}}_{{attr.name}})(const ir_node *node)
388 assert(is_{{node.name}}(node));
389 return node->attr.{{node.attrs_name}}.{{attr.name}};
392 void (set_{{node.name}}_{{attr.name}})(ir_node *node, {{attr.type}} {{attr.name}})
394 assert(is_{{node.name}}(node));
395 node->attr.{{node.attrs_name}}.{{attr.name}} = {{attr.name}};
400 {%- for node in nodes %}
401 {%- for in in node.ins %}
402 ir_node *(get_{{node.name}}_{{in}})(const ir_node *node)
404 assert(is_{{node.name}}(node));
405 return get_irn_n(node, {{node.ins.index(in)}});
408 void (set_{{node.name}}_{{in}})(ir_node *node, ir_node *{{in}})
410 assert(is_{{node.name}}(node));
411 set_irn_n(node, {{node.ins.index(in)}}, {{in}});
417 irop_template = env.from_string('''
418 /* Warning: automatically generated code */
419 {% for node in nodes %}
420 ir_op *op_{{node.name}}; ir_op *get_op_{{node.name}}(void) { return op_{{node.name}}; }
425 {% for node in nodes %}
426 op_{{node.name}} = new_ir_op(
427 {%- filter arguments %}
444 {% for node in nodes %}
445 free_ir_op(op_{{node.name}}); op_{{node.name}} = NULL;
451 projnumbers_h_template = env.from_string('''
452 /* Warning: automatically generated code */
454 {% for node in nodes -%}
457 * Projection numbers for result of {{node.name}} node (use for Proj nodes)
460 {% for out in node.outs -%}
461 pn_{{node.name}}_{{out[0]}}
462 {%- if out.__len__() > 2 %} = {{out[2]}}{% endif %}, /**< {{out[1]}} */
471 opcodes_h_template = env.from_string('''
472 /* Warning: automatically generated code */
473 #ifndef FIRM_IR_OPCODES_H
474 #define FIRM_IR_OPCODES_H
476 /** The opcodes of the libFirm predefined operations. */
477 typedef enum ir_opcode {
478 {%- for node in nodes %}
481 iro_First = iro_{{nodes[0].name}},
482 iro_Last = iro_{{nodes[-1].name}},
485 /* backend specific nodes */
486 beo_Spill = beo_First,
501 /* last backend node number */
502 beo_Last = beo_Barrier,
506 {% for node in nodes %}
507 FIRM_API ir_op *op_{{node.name}};
510 {% for node in nodes %}
511 FIRM_API ir_op *get_op_{{node.name}}(void);
517 #############################
524 real_nodes.append(node)
526 for node in real_nodes:
527 preprocess_node(node)
533 print "usage: %s specname(ignored) destdirectory" % argv[0]
537 # hardcoded path to libfirm/include/libfirm
538 gendir2 = argv[2] + "/../../include/libfirm"
541 niymap = [ "ASM", "Const", "Phi", "SymConst", "Sync"]
543 real_nodes = prepare_nodes()
544 file = open(gendir + "/gen_ir_cons.c.inl", "w")
545 for node in real_nodes:
546 if node.name in niymap:
549 if not isAbstract(node) and not hasattr(node, "singleton"):
550 file.write(constructor_template.render(vars()))
552 if hasattr(node, "special_constructors"):
553 for special in node.special_constructors:
554 node.constrname = special["constrname"]
555 special["attr"]["init"] = special["attr"]["special"]["init"]
556 file.write(constructor_template.render(vars()))
560 file = open(gendir + "/gen_irnode.h", "w")
561 file.write(irnode_h_template.render(nodes = real_nodes))
564 file = open(gendir + "/gen_irnode.c.inl", "w")
565 file.write(irnode_template.render(nodes = real_nodes))
568 file = open(gendir + "/gen_irop.c.inl", "w")
569 file.write(irop_template.render(nodes = real_nodes))
572 file = open(gendir2 + "/projnumbers.h", "w")
573 file.write(projnumbers_h_template.render(nodes = real_nodes))
576 file = open(gendir2 + "/opcodes.h", "w")
577 file.write(opcodes_h_template.render(nodes = real_nodes))