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` + ";\n\tir_node **r_in;\n\t" \
61 + "NEW_ARR_A(ir_node *, r_in, r_arity);\n\t"
63 for input in node.ins:
64 res += "r_in[" + `i` + "] = irn_" + input + ";\n\t"
66 res += "memcpy(&r_in[" + `insarity` + "], in, sizeof(ir_node *) * arity);\n\t"
68 res = "ir_node *in[" + `arity` + "];\n\t"
70 for input in node.ins:
71 res += "in[" + `i` + "] = irn_" + input + ";\n\t"
75 def format_arity_and_ins(node):
77 if arity == "dynamic":
79 elif arity == "variable":
80 if len(node.ins) == 0:
83 return "r_arity, r_in"
87 return `arity` + ", in"
89 def format_arity(node):
90 if hasattr(node, "arity_override"):
91 return node.arity_override
93 if arity == "dynamic":
94 return "oparity_dynamic"
95 if arity == "variable":
96 return "oparity_variable"
100 return "oparity_unary"
102 return "oparity_binary"
104 return "oparity_trinary"
107 def format_pinned(node):
110 return "op_pin_state_pinned"
112 return "op_pin_state_floats"
113 if pinned == "exception":
114 return "op_pin_state_exc_pinned"
115 if pinned == "memory":
116 return "op_pin_state_mem_pinned"
117 print "WARNING: Unknown pinned state %s in format pined" % pinned
120 def format_flags(node):
121 flags = map(lambda x : "irop_flag_" + x, node.flags)
123 flags = [ "irop_flag_none" ]
124 return " | ".join(flags)
126 def format_attr_size(node):
127 if not hasattr(node, "attr_struct"):
129 return "sizeof(%s)" % node.attr_struct
131 def format_opindex(node):
132 if hasattr(node, "op_index"):
136 def filter_isnot(list, flag):
137 return filter(lambda x: not hasattr(x, flag), list)
139 def filter_hasnot(list, flag):
140 return filter(lambda x: flag not in x, list)
142 def format_arguments(string, voidwhenempty = False):
143 args = re.split('\s*\n\s*', string)
146 if len(args) > 0 and args[-1] == '':
148 if len(args) == 0 and voidwhenempty:
150 return ", ".join(args)
152 def format_parameters(string):
153 return format_arguments(string, voidwhenempty = True)
156 env.filters['parameterlist'] = format_parameterlist
157 env.filters['nodearguments'] = format_nodearguments
158 env.filters['nodeparameters'] = format_nodeparameters
159 env.filters['blockparameter'] = format_blockparameter
160 env.filters['blockargument'] = format_blockargument
161 env.filters['irgassign'] = format_irgassign
162 env.filters['curblock'] = format_curblock
163 env.filters['insdecl'] = format_insdecl
164 env.filters['arity_and_ins'] = format_arity_and_ins
165 env.filters['arity'] = format_arity
166 env.filters['pinned'] = format_pinned
167 env.filters['flags'] = format_flags
168 env.filters['attr_size'] = format_attr_size
169 env.filters['opindex'] = format_opindex
170 env.filters['isnot'] = filter_isnot
171 env.filters['hasnot'] = filter_hasnot
172 env.filters['arguments'] = format_arguments
173 env.filters['parameters'] = format_parameters
175 def prepare_attr(attr):
177 return dict(type = attr["type"], name = attr["name"], init = attr["init"])
179 return dict(type = attr["type"], name = attr["name"])
181 def preprocess_node(node):
184 setdefault(node, "attrs_name", node.name.lower())
185 setdefault(node, "block", "block")
187 # construct node arguments
191 for input in node.ins:
192 arguments.append(dict(type = "ir_node *", name = "irn_" + input))
194 if node.arity == "variable" or node.arity == "dynamic":
195 arguments.append(dict(type = "int", name = "arity"))
196 arguments.append(dict(type = "ir_node **", name = "in"))
198 if not hasattr(node, "mode"):
199 arguments.append(dict(type = "ir_mode *", name = "mode"))
202 attrs_with_special = 0
203 for attr in node.attrs:
204 attr.setdefault("initname", "." + attr["name"])
206 if "special" in attr:
207 if not "init" in attr:
208 print "Node type %s has an attribute with a \"special\" entry but without \"init\"" % node.name
211 if attrs_with_special != 0:
212 print "Node type %s has more than one attribute with a \"special\" entry" % node.name
215 attrs_with_special += 1
217 if "prefix" in attr["special"]:
218 specialname = attr["special"]["prefix"] + node.name
219 elif "suffix" in attr["special"]:
220 specialname = node.name + attr["special"]["suffix"]
222 print "Unknown special constructor type for node type %s" % node.name
225 specialconstrs.append(
227 constrname = specialname,
231 elif not "init" in attr:
232 arguments.append(prepare_attr(attr))
234 # dynamic pin state means more constructor arguments
235 if is_dynamic_pinned(node):
236 if hasattr(node, "pinned_init"):
237 initattrs.append(dict(
238 initname = ".exc.pin_state",
239 init = node.pinned_init
242 node.constructor_args.append(
245 type = "op_pin_state"
248 initattrs.append(dict(
249 initname = ".exc.pin_state",
253 for arg in node.constructor_args:
254 arguments.append(prepare_attr(arg))
255 if arg["type"] == "ir_cons_flags":
257 initattrs.append(dict(initname = ".exc.pin_state",
258 init = name + " & cons_floats ? op_pin_state_floats : op_pin_state_pinned"))
259 initattrs.append(dict(initname = ".volatility",
260 init = name + " & cons_volatile ? volatility_is_volatile : volatility_non_volatile"))
261 initattrs.append(dict(initname = ".aligned",
262 init = name + " & cons_unaligned ? align_non_aligned : align_is_aligned"))
264 node.arguments = arguments
265 node.initattrs = initattrs
266 node.special_constructors = specialconstrs
268 #############################
270 constructor_template = env.from_string('''
272 ir_node *new_rd_{{node.constrname}}(
273 {%- filter parameters %}
275 {{node|blockparameter}}
276 {{node|nodeparameters}}
280 ir_graph *rem = current_ir_graph;
283 current_ir_graph = irg;
285 {%- filter arguments %}
291 {{node|arity_and_ins}}
293 {% for attr in node.attrs -%}
294 res->attr.{{node.attrs_name}}{{attr["initname"]}} =
295 {%- if "init" in attr %} {{ attr["init"] -}};
296 {%- else %} {{ attr["name"] -}};
299 {%- for attr in node.initattrs -%}
300 res->attr.{{node.attrs_name}}{{attr["initname"]}} = {{ attr["init"] -}};
303 {% if node.optimize != False -%}
304 res = optimize_node(res);
306 IRN_VRFY_IRG(res, irg);
307 current_ir_graph = rem;
311 ir_node *new_r_{{node.constrname}}(
312 {%- filter parameters %}
313 {{node|blockparameter}}
314 {{node|nodeparameters}}
317 return new_rd_{{node.constrname}}(
318 {%- filter arguments %}
320 {{node|blockargument}}
321 {{node|nodearguments}}
325 ir_node *new_d_{{node.constrname}}(
326 {%- filter parameters %}
328 {{node|nodeparameters}}
333 res = new_rd_{{node.constrname}}(
334 {%- filter parameters %}
337 {{node|nodearguments}}
343 ir_node *new_{{node.constrname}}(
344 {%- filter parameters %}
345 {{node|nodeparameters}}
348 return new_d_{{node.constrname}}(
349 {%- filter arguments %}
351 {{node|nodearguments}}
356 irnode_h_template = env.from_string('''
357 /* Warning: automatically generated code */
359 {%- for node in nodes|isnot('custom_is') %}
360 static inline int _is_{{node.name}}(const ir_node *node)
362 assert(node != NULL);
363 return _get_irn_op(node) == op_{{node.name}};
367 {% for node in nodes %}
368 #define is_{{node.name}}(node) _is_{{node.name}}(node)
373 irnode_template = env.from_string('''
374 /* Warning: automatically generated code */
375 {% for node in nodes %}
376 int (is_{{node.name}})(const ir_node *node)
378 return _is_{{node.name}}(node);
382 {%- for node in nodes %}
383 {%- for attr in node.attrs|hasnot("noprop") %}
384 {{attr.type}} (get_{{node.name}}_{{attr.name}})(const ir_node *node)
386 assert(is_{{node.name}}(node));
387 return node->attr.{{node.attrs_name}}.{{attr.name}};
390 void (set_{{node.name}}_{{attr.name}})(ir_node *node, {{attr.type}} {{attr.name}})
392 assert(is_{{node.name}}(node));
393 node->attr.{{node.attrs_name}}.{{attr.name}} = {{attr.name}};
398 {%- for node in nodes %}
399 {%- for in in node.ins %}
400 ir_node *(get_{{node.name}}_{{in}})(const ir_node *node)
402 assert(is_{{node.name}}(node));
403 return get_irn_n(node, {{node.ins.index(in)}});
406 void (set_{{node.name}}_{{in}})(ir_node *node, ir_node *{{in}})
408 assert(is_{{node.name}}(node));
409 set_irn_n(node, {{node.ins.index(in)}}, {{in}});
415 irop_template = env.from_string('''
416 /* Warning: automatically generated code */
417 {% for node in nodes %}
418 ir_op *op_{{node.name}}; ir_op *get_op_{{node.name}}(void) { return op_{{node.name}}; }
423 {% for node in nodes %}
424 op_{{node.name}} = new_ir_op(
425 {%- filter arguments %}
442 {% for node in nodes %}
443 free_ir_op(op_{{node.name}}); op_{{node.name}} = NULL;
449 projnumbers_h_template = env.from_string('''
450 /* Warning: automatically generated code */
452 {% for node in nodes -%}
455 * Projection numbers for result of {{node.name}} node (use for Proj nodes)
458 {% for out in node.outs -%}
459 pn_{{node.name}}_{{out[0]}}
460 {%- if out.__len__() > 2 %} = {{out[2]}}{% endif %}, /**< {{out[1]}} */
469 opcodes_h_template = env.from_string('''
470 /* Warning: automatically generated code */
471 #ifndef FIRM_IR_OPCODES_H
472 #define FIRM_IR_OPCODES_H
474 /** The opcodes of the libFirm predefined operations. */
475 typedef enum ir_opcode {
476 {%- for node in nodes %}
479 iro_First = iro_{{nodes[0].name}},
480 iro_Last = iro_{{nodes[-1].name}},
483 /* backend specific nodes */
484 beo_Spill = beo_First,
499 /* last backend node number */
500 beo_Last = beo_Barrier,
504 {% for node in nodes %}
505 FIRM_API ir_op *op_{{node.name}};
508 {% for node in nodes %}
509 FIRM_API ir_op *get_op_{{node.name}}(void);
515 #############################
522 real_nodes.append(node)
524 for node in real_nodes:
525 preprocess_node(node)
531 print "usage: %s specname(ignored) destdirectory" % argv[0]
535 # hardcoded path to libfirm/include/libfirm
536 gendir2 = argv[2] + "/../../include/libfirm"
539 niymap = [ "ASM", "Const", "Phi", "SymConst", "Sync"]
541 real_nodes = prepare_nodes()
542 file = open(gendir + "/gen_ir_cons.c.inl", "w")
543 for node in real_nodes:
544 if node.name in niymap:
547 if not isAbstract(node) and not hasattr(node, "singleton"):
548 file.write(constructor_template.render(vars()))
550 if hasattr(node, "special_constructors"):
551 for special in node.special_constructors:
552 node.constrname = special["constrname"]
553 special["attr"]["init"] = special["attr"]["special"]["init"]
554 file.write(constructor_template.render(vars()))
558 file = open(gendir + "/gen_irnode.h", "w")
559 file.write(irnode_h_template.render(nodes = real_nodes))
562 file = open(gendir + "/gen_irnode.c.inl", "w")
563 file.write(irnode_template.render(nodes = real_nodes))
566 file = open(gendir + "/gen_irop.c.inl", "w")
567 file.write(irop_template.render(nodes = real_nodes))
570 file = open(gendir2 + "/projnumbers.h", "w")
571 file.write(projnumbers_h_template.render(nodes = real_nodes))
574 file = open(gendir2 + "/opcodes.h", "w")
575 file.write(opcodes_h_template.render(nodes = real_nodes))