3 from jinja2 import Environment, Template
4 from jinja2.filters import do_dictsort
7 def format_argdecls(node, first = False, voidwhenempty = False):
8 if not node.has_key("args") or len(node["args"]) == 0:
19 for arg in node["args"]:
20 res = res + (comma + arg["type"] + " " + arg["name"])
24 def format_args(node, first = False):
25 if not node.has_key("args"):
33 for arg in node["args"]:
34 res = res + (comma + arg["name"])
38 def format_blockdecl(node):
39 if node.get("knownBlock"):
42 return ", ir_node *block"
44 def format_block(node):
45 if node.get("knownBlock"):
50 def format_curblock(node):
51 if node.get("knownBlock"):
54 return ", current_ir_graph->current_block"
56 def format_insdecl(node):
58 if arity == "variable" and len(node["ins"]) == 0 or arity == "dynamic" or arity == 0:
61 if arity == "variable":
62 insarity = len(node["ins"])
63 res = "int r_arity = arity + " + `insarity` + ";\n\tir_node **r_in;\n\t" \
64 + "NEW_ARR_A(ir_node *, r_in, r_arity);\n\t"
66 for input in node["ins"]:
67 res += "r_in[" + `i` + "] = irn_" + input + ";\n\t"
69 res += "memcpy(&r_in[" + `insarity` + "], in, sizeof(ir_node *) * arity);\n\t"
71 res = "ir_node *in[" + `arity` + "];\n\t"
73 for input in node["ins"]:
74 res += "in[" + `i` + "] = irn_" + input + ";\n\t"
78 def format_arity_and_ins(node):
80 if arity == "dynamic":
82 elif arity == "variable":
83 if len(node["ins"]) == 0:
86 return "r_arity, r_in"
90 return `arity` + ", in"
93 env.filters['argdecls'] = format_argdecls
94 env.filters['args'] = format_args
95 env.filters['blockdecl'] = format_blockdecl
96 env.filters['block'] = format_block
97 env.filters['curblock'] = format_curblock
98 env.filters['insdecl'] = format_insdecl
99 env.filters['arity_and_ins'] = format_arity_and_ins
101 def add_attr(list, type, name, init = None, initname = None):
103 initname = "." + name
105 list.append(dict(type = type, name = name, init = init, initname = initname))
107 list.append(dict(type = type, name = name, initname = initname))
109 def prepare_attr(attr):
111 return dict(type = attr["type"], name = attr["name"], init = attr["init"])
113 return dict(type = attr["type"], name = attr["name"])
115 def preprocess_node(nodename, node):
116 # set default attributes
118 parent = ir_spec.nodes[node["is_a"]]
119 node["ins"] = parent["ins"]
121 node["outs"] = parent["outs"]
124 node["mode"] = "mode_T"
125 if "nodbginfo" in node:
128 node["dbdeclnocomma"] = ""
131 node["dbdecl"] = "dbg_info *db, "
132 node["dbdeclnocomma"] = "dbg_info *db"
134 node.setdefault("ins", [])
135 node.setdefault("arity", len(node["ins"]))
136 node.setdefault("attrs", [])
137 node.setdefault("constrname", nodename);
138 node.setdefault("constructor_args", [])
139 node.setdefault("attrs_name", nodename.lower())
140 node.setdefault("block", "block")
142 # construct node arguments
146 for input in node["ins"]:
147 arguments.append(dict(type = "ir_node *", name = "irn_" + input))
149 # Special case for Builtin...
150 if nodename == "Builtin":
151 for attr in node["attrs"]:
152 if attr["name"] == "kind":
153 arguments.append(prepare_attr(attr))
155 if node["arity"] == "variable":
156 arguments.append(dict(type = "int", name = "arity"))
157 arguments.append(dict(type = "ir_node **", name = "in"))
159 if "mode" not in node:
160 arguments.append(dict(type = "ir_mode *", name = "mode"))
161 node["mode"] = "mode"
163 attrs_with_special = 0
164 for attr in node["attrs"]:
165 if nodename == "Builtin" and attr["name"] == "kind":
168 attr.setdefault("initname", "." + attr["name"])
170 if "special" in attr:
171 if not "init" in attr:
172 print "Node type %s has an attribute with a \"special\" entry but without \"init\"" % nodename
175 if attrs_with_special != 0:
176 print "Node type %s has more than one attribute with a \"special\" entry" % nodename
179 attrs_with_special += 1
181 if "prefix" in attr["special"]:
182 specialname = attr["special"]["prefix"] + nodename
183 elif "suffix" in attr["special"]:
184 specialname = nodename + attr["special"]["suffix"]
186 print "Unknown special constructor type for node type %s" % nodename
189 specialconstrs.append(
191 constrname = specialname,
195 elif not "init" in attr:
196 arguments.append(prepare_attr(attr))
198 for arg in node["constructor_args"]:
199 arguments.append(prepare_attr(arg))
200 if arg["type"] == "ir_cons_flags":
202 initattrs.append(dict(initname = ".exc.pin_state",
203 init = name + " & cons_floats ? op_pin_state_floats : op_pin_state_pinned"))
204 initattrs.append(dict(initname = ".volatility",
205 init = name + " & cons_volatile ? volatility_is_volatile : volatility_non_volatile"))
206 initattrs.append(dict(initname = ".aligned",
207 init = name + " & cons_unaligned ? align_non_aligned : align_is_aligned"))
209 node["args"] = arguments
210 node["initattrs"] = initattrs
211 node["special_constructors"] = specialconstrs
213 #############################
215 node_template = env.from_string('''
216 ir_node *new_rd_{{node["constrname"]}}({{node["dbdecl"]}}ir_graph *irg{{node|blockdecl}}{{node|argdecls}})
219 ir_graph *rem = current_ir_graph;
221 current_ir_graph = irg;
222 res = new_ir_node({{node["db"]}}, irg, {{node["block"]}}, op_{{nodename}}, {{node["mode"]}}, {{node|arity_and_ins}});
223 {% for attr in node["attrs"] -%}
224 res->attr.{{node["attrs_name"]}}{{attr["initname"]}} =
225 {%- if "init" in attr %} {{ attr["init"] -}};
226 {%- else %} {{ attr["name"] -}};
229 {%- for attr in node["initattrs"] -%}
230 res->attr.{{node["attrs_name"]}}{{attr["initname"]}} = {{ attr["init"] -}};
233 {% if node["optimize"] != False -%}
234 res = optimize_node(res);
236 IRN_VRFY_IRG(res, irg);
237 current_ir_graph = rem;
241 ir_node *new_r_{{node["constrname"]}}(ir_graph *irg{{node|blockdecl}}{{node|argdecls}})
243 {% if node["nodbginfo"] -%}
244 return new_rd_{{node["constrname"]}}(irg{{node|block}}{{node|args}});
246 return new_rd_{{node["constrname"]}}(NULL, irg{{node|block}}{{node|args}});
250 ir_node *new_d_{{node["constrname"]}}({{node["dbdeclnocomma"]}}{{node|argdecls(node["nodbginfo"])}})
254 {% if node["nodbginfo"] -%}
255 res = new_rd_{{node["constrname"]}}(current_ir_graph{{node|curblock}}{{node|args}});
257 res = new_rd_{{node["constrname"]}}(db, current_ir_graph{{node|curblock}}{{node|args}});
263 ir_node *new_{{node["constrname"]}}({{node|argdecls(True, True)}})
265 {% if node["nodbginfo"] -%}
266 return new_d_{{node["constrname"]}}({{node|args(True)}});
268 return new_d_{{node["constrname"]}}(NULL{{node|args}});
274 #############################
277 """the main function"""
280 print "usage: %s specname(ignored) destdirectory" % argv[0]
286 niymap = ["Alloc", "Anchor", "ASM", "Bad", "Bound", "Break", "Builtin",
287 "Call", "CallBegin", "Const", "Const_type", "Const_long", "CopyB",
288 "defaultProj", "Dummy", "EndReg", "EndExcept",
289 "Filter", "InstOf", "NoMem", "Phi", "Raise",
290 "simpleSel", "SymConst", "SymConst_type", "Sync"]
292 file = open(gendir + "/gen_ir_cons.c.inl", "w")
293 for nodename, node in do_dictsort(ir_spec.nodes):
294 if nodename in niymap:
296 preprocess_node(nodename, node)
297 if not "abstract" in node:
298 file.write(node_template.render(vars()))
300 if "special_constructors" in node:
301 for special in node["special_constructors"]:
302 node["constrname"] = special["constrname"]
303 special["attr"]["init"] = special["attr"]["special"]["init"]
304 file.write(node_template.render(vars()))
308 if __name__ == "__main__":