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 keywords = frozenset([ "true", "false" ])
138 def format_escape_keywords(word):
143 def filter_isnot(list, flag):
144 return filter(lambda x: not hasattr(x, flag), list)
146 def filter_hasnot(list, flag):
147 return filter(lambda x: flag not in x, list)
149 def format_arguments(string, voidwhenempty = False):
150 args = re.split('\s*\n\s*', string)
153 if len(args) > 0 and args[-1] == '':
155 if len(args) == 0 and voidwhenempty:
157 return ", ".join(args)
159 def format_parameters(string):
160 return format_arguments(string, voidwhenempty = True)
163 env.filters['parameterlist'] = format_parameterlist
164 env.filters['nodearguments'] = format_nodearguments
165 env.filters['nodeparameters'] = format_nodeparameters
166 env.filters['blockparameter'] = format_blockparameter
167 env.filters['blockargument'] = format_blockargument
168 env.filters['irgassign'] = format_irgassign
169 env.filters['curblock'] = format_curblock
170 env.filters['insdecl'] = format_insdecl
171 env.filters['arity_and_ins'] = format_arity_and_ins
172 env.filters['arity'] = format_arity
173 env.filters['pinned'] = format_pinned
174 env.filters['flags'] = format_flags
175 env.filters['attr_size'] = format_attr_size
176 env.filters['opindex'] = format_opindex
177 env.filters['isnot'] = filter_isnot
178 env.filters['hasnot'] = filter_hasnot
179 env.filters['arguments'] = format_arguments
180 env.filters['parameters'] = format_parameters
181 env.filters['escape_keywords'] = format_escape_keywords
183 def prepare_attr(attr):
185 return dict(type = attr["type"], name = attr["name"], init = attr["init"])
187 return dict(type = attr["type"], name = attr["name"])
189 def preprocess_node(node):
192 setdefault(node, "attrs_name", node.name.lower())
193 setdefault(node, "block", "block")
195 # construct node arguments
199 for input in node.ins:
200 arguments.append(dict(type = "ir_node *", name = "irn_" + input))
202 if node.arity == "variable" or node.arity == "dynamic":
203 arguments.append(dict(type = "int", name = "arity"))
204 arguments.append(dict(type = "ir_node **", name = "in"))
206 if not hasattr(node, "mode"):
207 arguments.append(dict(type = "ir_mode *", name = "mode"))
210 attrs_with_special = 0
211 for attr in node.attrs:
212 attr.setdefault("initname", "." + attr["name"])
214 if "special" in attr:
215 if not "init" in attr:
216 print "Node type %s has an attribute with a \"special\" entry but without \"init\"" % node.name
219 if attrs_with_special != 0:
220 print "Node type %s has more than one attribute with a \"special\" entry" % node.name
223 attrs_with_special += 1
225 if "prefix" in attr["special"]:
226 specialname = attr["special"]["prefix"] + node.name
227 elif "suffix" in attr["special"]:
228 specialname = node.name + attr["special"]["suffix"]
230 print "Unknown special constructor type for node type %s" % node.name
233 specialconstrs.append(
235 constrname = specialname,
239 elif not "init" in attr:
240 arguments.append(prepare_attr(attr))
242 # dynamic pin state means more constructor arguments
243 if is_dynamic_pinned(node):
244 if hasattr(node, "pinned_init"):
245 initattrs.append(dict(
246 initname = ".exc.pin_state",
247 init = node.pinned_init
250 node.constructor_args.append(
253 type = "op_pin_state"
256 initattrs.append(dict(
257 initname = ".exc.pin_state",
261 for arg in node.constructor_args:
262 arguments.append(prepare_attr(arg))
263 if arg["type"] == "ir_cons_flags":
265 initattrs.append(dict(initname = ".exc.pin_state",
266 init = name + " & cons_floats ? op_pin_state_floats : op_pin_state_pinned"))
267 initattrs.append(dict(initname = ".volatility",
268 init = name + " & cons_volatile ? volatility_is_volatile : volatility_non_volatile"))
269 initattrs.append(dict(initname = ".aligned",
270 init = name + " & cons_unaligned ? align_non_aligned : align_is_aligned"))
272 node.arguments = arguments
273 node.initattrs = initattrs
274 node.special_constructors = specialconstrs
276 #############################
278 constructor_template = env.from_string('''
280 ir_node *new_rd_{{node.constrname}}(
281 {%- filter parameters %}
283 {{node|blockparameter}}
284 {{node|nodeparameters}}
288 {%- if node.arity == "dynamic" %}
295 {%- filter arguments %}
301 {{node|arity_and_ins}}
303 {%- if node.arity == "dynamic" %}
304 for (i = 0; i < arity; ++i) {
305 add_irn_n(res, in[i]);
308 {%- for attr in node.attrs %}
309 res->attr.{{node.attrs_name}}{{attr["initname"]}} =
310 {%- if "init" in attr %} {{ attr["init"] -}};
311 {%- else %} {{ attr["name"] -}};
314 {%- for attr in node.initattrs %}
315 res->attr.{{node.attrs_name}}{{attr["initname"]}} = {{ attr["init"] -}};
318 res = optimize_node(res);
319 irn_verify_irg(res, irg);
323 ir_node *new_r_{{node.constrname}}(
324 {%- filter parameters %}
325 {{node|blockparameter}}
326 {{node|nodeparameters}}
329 return new_rd_{{node.constrname}}(
330 {%- filter arguments %}
332 {{node|blockargument}}
333 {{node|nodearguments}}
337 ir_node *new_d_{{node.constrname}}(
338 {%- filter parameters %}
340 {{node|nodeparameters}}
344 assert(get_irg_phase_state(current_ir_graph) == phase_building);
345 res = new_rd_{{node.constrname}}(
346 {%- filter parameters %}
349 {{node|nodearguments}}
354 ir_node *new_{{node.constrname}}(
355 {%- filter parameters %}
356 {{node|nodeparameters}}
359 return new_d_{{node.constrname}}(
360 {%- filter arguments %}
362 {{node|nodearguments}}
367 irnode_h_template = env.from_string('''
368 /* Warning: automatically generated code */
370 {%- for node in nodes|isnot('custom_is') %}
371 static inline int _is_{{node.name}}(const ir_node *node)
373 assert(node != NULL);
374 return _get_irn_op(node) == op_{{node.name}};
378 {% for node in nodes %}
379 #define is_{{node.name}}(node) _is_{{node.name}}(node)
384 irnode_template = env.from_string('''
385 /* Warning: automatically generated code */
386 {% for node in nodes %}
387 int (is_{{node.name}})(const ir_node *node)
389 return _is_{{node.name}}(node);
393 {%- for node in nodes %}
394 {%- for attr in node.attrs|hasnot("noprop") %}
395 {{attr.type}} (get_{{node.name}}_{{attr.name}})(const ir_node *node)
397 assert(is_{{node.name}}(node));
398 return node->attr.{{node.attrs_name}}.{{attr.name}};
401 void (set_{{node.name}}_{{attr.name}})(ir_node *node, {{attr.type}} {{attr.name}})
403 assert(is_{{node.name}}(node));
404 node->attr.{{node.attrs_name}}.{{attr.name}} = {{attr.name}};
409 {%- for node in nodes %}
410 {%- for in in node.ins %}
411 ir_node *(get_{{node.name}}_{{in}})(const ir_node *node)
413 assert(is_{{node.name}}(node));
414 return get_irn_n(node, {{node.ins.index(in)}});
417 void (set_{{node.name}}_{{in}})(ir_node *node, ir_node *{{in|escape_keywords}})
419 assert(is_{{node.name}}(node));
420 set_irn_n(node, {{node.ins.index(in)}}, {{in|escape_keywords}});
426 irop_template = env.from_string('''
427 /* Warning: automatically generated code */
428 {% for node in nodes %}
429 ir_op *op_{{node.name}}; ir_op *get_op_{{node.name}}(void) { return op_{{node.name}}; }
434 {% for node in nodes %}
435 op_{{node.name}} = new_ir_op(
436 {%- filter arguments %}
453 {% for node in nodes %}
454 free_ir_op(op_{{node.name}}); op_{{node.name}} = NULL;
460 nodeops_h_template = env.from_string('''
461 /* Warning: automatically generated code */
462 #ifndef FIRM_IR_NODEOPS_H
463 #define FIRM_IR_NODEOPS_H
465 #include "firm_types.h"
468 * @addtogroup ir_node
472 {% for node in nodes -%}
475 * Projection numbers for result of {{node.name}} node (use for Proj nodes)
478 {% for out in node.outs -%}
479 pn_{{node.name}}_{{out[0]}}
480 {%- if out.__len__() > 2 %} = {{out[2]}}{% endif %}, /**< {{out[1]}} */
487 {% for node in nodes %}
488 /** Return true of the node is a {{node.name}} node. */
489 FIRM_API int is_{{node.name}}(const ir_node *node);
492 {% for node in nodes %}
493 {% for in in node.ins -%}
494 FIRM_API ir_node *get_{{node.name}}_{{in}}(const ir_node *node);
495 void set_{{node.name}}_{{in}}(ir_node *node, ir_node *{{in|escape_keywords}});
497 {% for attr in node.attrs|hasnot("noprop") -%}
498 FIRM_API {{attr.type}} get_{{node.name}}_{{attr.name}}(const ir_node *node);
499 FIRM_API void set_{{node.name}}_{{attr.name}}(ir_node *node, {{attr.type}} {{attr.name}});
508 opcodes_h_template = env.from_string('''
509 /* Warning: automatically generated code */
510 #ifndef FIRM_IR_OPCODES_H
511 #define FIRM_IR_OPCODES_H
513 /** The opcodes of the libFirm predefined operations. */
514 typedef enum ir_opcode {
515 {%- for node in nodes %}
518 iro_First = iro_{{nodes[0].name}},
519 iro_Last = iro_{{nodes[-1].name}},
522 /* backend specific nodes */
523 beo_Spill = beo_First,
538 /* last backend node number */
539 beo_Last = beo_Barrier,
543 {% for node in nodes %}
544 FIRM_API ir_op *op_{{node.name}};
547 {% for node in nodes %}
548 FIRM_API ir_op *get_op_{{node.name}}(void);
554 #############################
561 real_nodes.append(node)
563 for node in real_nodes:
564 preprocess_node(node)
570 print "usage: %s specname(ignored) destdirectory" % argv[0]
574 # hardcoded path to libfirm/include/libfirm
575 gendir2 = argv[2] + "/../../include/libfirm"
578 niymap = [ "ASM", "Const", "Phi", "SymConst" ]
580 real_nodes = prepare_nodes()
581 file = open(gendir + "/gen_ir_cons.c.inl", "w")
582 for node in real_nodes:
583 if node.name in niymap:
586 if not isAbstract(node) and not hasattr(node, "noconstructor"):
587 file.write(constructor_template.render(vars()))
589 if hasattr(node, "special_constructors"):
590 for special in node.special_constructors:
591 node.constrname = special["constrname"]
592 special["attr"]["init"] = special["attr"]["special"]["init"]
593 file.write(constructor_template.render(vars()))
597 file = open(gendir + "/gen_irnode.h", "w")
598 file.write(irnode_h_template.render(nodes = real_nodes))
601 file = open(gendir + "/gen_irnode.c.inl", "w")
602 file.write(irnode_template.render(nodes = real_nodes))
605 file = open(gendir + "/gen_irop.c.inl", "w")
606 file.write(irop_template.render(nodes = real_nodes))
609 file = open(gendir2 + "/opcodes.h", "w")
610 file.write(opcodes_h_template.render(nodes = real_nodes))
613 file = open(gendir2 + "/nodeops.h", "w")
614 file.write(nodeops_h_template.render(nodes = real_nodes))