Added gen_ir.py support for Alloc and Builtin
[libfirm] / scripts / gen_ir.py
index e722c89..8607f71 100755 (executable)
@@ -113,26 +113,15 @@ def prepare_attr(attr):
                return dict(type = attr["type"], name = attr["name"])
 
 def preprocess_node(nodename, node):
-       print "nodename: " + nodename
+       # set default attributes
        if "is_a" in node:
                parent = ir_spec.nodes[node["is_a"]]
                node["ins"] = parent["ins"]
                if "outs" in parent:
                        node["outs"] = parent["outs"]
-       if "ins" not in node:
-               node["ins"] = []
+
        if "outs" in node:
                node["mode"] = "mode_T"
-       if "arity" not in node:
-               node["arity"] = len(node["ins"])
-       if "attrs" not in node:
-               node["attrs"] = []
-       if "constructor_args" not in node:
-               node["constructor_args"] = []
-       if "attrs_name" not in node:
-               node["attrs_name"] = nodename.lower()
-       if "block" not in node:
-               node["block"] = "block"
        if "nodbginfo" in node:
                node["db"] = "NULL"
                node["dbdecl"] = ""
@@ -142,21 +131,26 @@ def preprocess_node(nodename, node):
                node["dbdecl"] = "dbg_info *db, "
                node["dbdeclnocomma"] = "dbg_info *db"
 
+       node.setdefault("ins", [])
+       node.setdefault("arity", len(node["ins"]))
+       node.setdefault("attrs", [])
+       node.setdefault("constrname", nodename);
+       node.setdefault("constructor_args", [])
+       node.setdefault("attrs_name", nodename.lower())
+       node.setdefault("block", "block")
+
        # construct node arguments
        arguments = [ ]
-       initargs = [ ]
        initattrs = [ ]
        specialconstrs = [ ]
-       i = 0
        for input in node["ins"]:
-               print "ins: " + input
                arguments.append(dict(type = "ir_node *", name = "irn_" + input))
-               i += 1
 
        # Special case for Builtin...
        if nodename == "Builtin":
                for attr in node["attrs"]:
                        if attr["name"] == "kind":
+                               attr.setdefault("initname", "." + attr["name"])
                                arguments.append(prepare_attr(attr))
 
        if node["arity"] == "variable":
@@ -169,14 +163,11 @@ def preprocess_node(nodename, node):
 
        attrs_with_special = 0
        for attr in node["attrs"]:
-               print "attr: " + attr["name"]
                if nodename == "Builtin" and attr["name"] == "kind":
                        continue
 
-               if "initname" not in attr:
-                       attr["initname"] = "." + attr["name"]
+               attr.setdefault("initname", "." + attr["name"])
 
-               # "special" stuff does not work at all, yet
                if "special" in attr:
                        if not "init" in attr:
                                print "Node type %s has an attribute with a \"special\" entry but without \"init\"" % nodename
@@ -193,19 +184,16 @@ def preprocess_node(nodename, node):
                        elif "suffix" in attr["special"]:
                                specialname = nodename + attr["special"]["suffix"]
                        else:
-                               print "Unknown special constructor type for node type %s" %nodename
+                               print "Unknown special constructor type for node type %s" % nodename
                                sys.exit(1)
 
                        specialconstrs.append(
                                dict(
                                        constrname = specialname,
-                                       attrname = attr["name"],
-                                       value = attr["special"]["init"]
+                                       attr = attr
                                )
                        )
-               elif "init" in attr:
-                       initargs.append(attr["name"])
-               else:
+               elif not "init" in attr:
                        arguments.append(prepare_attr(attr))
 
        for arg in node["constructor_args"]:
@@ -220,14 +208,13 @@ def preprocess_node(nodename, node):
                                init = name + " & cons_unaligned ? align_non_aligned : align_is_aligned"))
 
        node["args"] = arguments
-       node["initargs"] = initargs
        node["initattrs"] = initattrs
        node["special_constructors"] = specialconstrs
 
 #############################
 
 node_template = env.from_string('''
-ir_node *new_rd_{{nodename}}({{node["dbdecl"]}}ir_graph *irg{{node|blockdecl}}{{node|argdecls}})
+ir_node *new_rd_{{node["constrname"]}}({{node["dbdecl"]}}ir_graph *irg{{node|blockdecl}}{{node|argdecls}})
 {
        ir_node *res;
        ir_graph *rem = current_ir_graph;
@@ -252,34 +239,34 @@ ir_node *new_rd_{{nodename}}({{node["dbdecl"]}}ir_graph *irg{{node|blockdecl}}{{
        return res;
 }
 
-ir_node *new_r_{{nodename}}(ir_graph *irg{{node|blockdecl}}{{node|argdecls}})
+ir_node *new_r_{{node["constrname"]}}(ir_graph *irg{{node|blockdecl}}{{node|argdecls}})
 {
        {% if node["nodbginfo"] -%}
-               return new_rd_{{nodename}}(irg{{node|block}}{{node|args}});
+               return new_rd_{{node["constrname"]}}(irg{{node|block}}{{node|args}});
        {%- else -%}
-               return new_rd_{{nodename}}(NULL, irg{{node|block}}{{node|args}});
+               return new_rd_{{node["constrname"]}}(NULL, irg{{node|block}}{{node|args}});
        {%- endif %}
 }
 
-ir_node *new_d_{{nodename}}({{node["dbdeclnocomma"]}}{{node|argdecls(node["nodbginfo"])}})
+ir_node *new_d_{{node["constrname"]}}({{node["dbdeclnocomma"]}}{{node|argdecls(node["nodbginfo"])}})
 {
        ir_node *res;
        {{ node["d_pre"] }}
        {% if node["nodbginfo"] -%}
-               res = new_rd_{{nodename}}(current_ir_graph{{node|curblock}}{{node|args}});
+               res = new_rd_{{node["constrname"]}}(current_ir_graph{{node|curblock}}{{node|args}});
        {%- else -%}
-               res = new_rd_{{nodename}}(db, current_ir_graph{{node|curblock}}{{node|args}});
+               res = new_rd_{{node["constrname"]}}(db, current_ir_graph{{node|curblock}}{{node|args}});
        {%- endif %}
        {{ node["d_post"] }}
        return res;
 }
 
-ir_node *new_{{nodename}}({{node|argdecls(True, True)}})
+ir_node *new_{{node["constrname"]}}({{node|argdecls(True, True)}})
 {
        {% if node["nodbginfo"] -%}
-               return new_d_{{nodename}}({{node|args(True)}});
+               return new_d_{{node["constrname"]}}({{node|args(True)}});
        {%- else -%}
-               return new_d_{{nodename}}(NULL{{node|args}});
+               return new_d_{{node["constrname"]}}(NULL{{node|args}});
        {%- endif %}
 }
 
@@ -297,19 +284,25 @@ def main(argv):
        gendir = argv[2]
 
        # List of TODOs
-       niymap = ["Alloc", "Anchor", "ASM", "Bad", "Bound", "Break", "Builtin",
-               "Call", "CallBegin", "Cast", "Const", "Const_type", "Const_long", "CopyB",
-               "defaultProj", "Div", "DivRL", "DivMod", "Dummy", "EndReg", "EndExcept",
-               "Filter", "InstOf", "Mod", "NoMem", "Phi", "Quot", "Raise",
-               "simpleSel", "strictConv", "SymConst", "SymConst_type", "Sync"]
+       niymap = ["Anchor", "ASM", "Bad", "Bound", "Break",
+               "CallBegin", "Const", "Const_type", "Const_long", "CopyB",
+               "defaultProj", "Dummy", "EndReg", "EndExcept",
+               "Filter", "InstOf", "NoMem", "Phi", "Raise",
+               "simpleSel", "SymConst", "SymConst_type", "Sync"]
 
-       file = open(gendir + "/gen_ir_cons_py.c.inl", "w")
+       file = open(gendir + "/gen_ir_cons.c.inl", "w")
        for nodename, node in do_dictsort(ir_spec.nodes):
                if nodename in niymap:
                        continue
                preprocess_node(nodename, node)
                if not "abstract" in node:
                        file.write(node_template.render(vars()))
+
+                       if "special_constructors" in node:
+                               for special in node["special_constructors"]:
+                                       node["constrname"] = special["constrname"]
+                                       special["attr"]["init"] = special["attr"]["special"]["init"]
+                                       file.write(node_template.render(vars()))
        file.write("\n")
        file.close()