rename tarval to ir_tarval
[libfirm] / scripts / gen_ir_io.py
index 781ee46..2deead7 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 import sys
+import re
 from jinja2 import Environment, Template
 from jinja2.filters import do_dictsort
 from spec_util import is_dynamic_pinned, verify_node, isAbstract
@@ -14,10 +15,7 @@ def warning(msg):
        sys.stderr.write("Warning: " + msg + "\n");
 
 def format_args(arglist):
-       s = ", ".join(arglist)
-       if len(s) == 0:
-         return ""
-       return ", " + s
+       return "\n".join(arglist)
 
 def format_ifnset(string, node, key):
        if hasattr(node, key):
@@ -26,18 +24,29 @@ def format_ifnset(string, node, key):
 
 def format_block(node):
        if hasattr(node, "knownBlock"):
+               if hasattr(node, "knownGraph"):
+                       return ""
                return "current_ir_graph"
        else:
                return "get_node(env, preds[0])"
 
+def format_arguments(string):
+       args = re.split('\s*\n\s*', string)
+       if args[0] == '':
+               args = args[1:]
+       if len(args) > 0 and args[-1] == '':
+               args = args[:-1]
+       return ", ".join(args)
+
 env = Environment()
-env.filters['args']   = format_args
-env.filters['ifnset'] = format_ifnset
-env.filters['block']  = format_block
+env.filters['args']      = format_args
+env.filters['ifnset']    = format_ifnset
+env.filters['block']     = format_block
+env.filters['arguments'] = format_arguments
 
 def get_io_type(type, attrname, node):
-       if type == "tarval*":
-               importcmd = "tarval *%s = read_tv(env);" % attrname
+       if type == "ir_tarval*":
+               importcmd = "ir_tarval *%s = read_tv(env);" % attrname
                exportcmd = "write_tarval(env, %(val)s);";
        elif type == "ir_mode*":
                importcmd = "ir_mode *%s = read_mode(env);" % attrname
@@ -86,7 +95,7 @@ def get_io_type(type, attrname, node):
                importcmd = "long %s = read_long(env);" % attrname
                exportcmd = """fprintf(env->file, "%%ld ", %(val)s);"""
        else:
-               error("cannot generate import/export for node %s: unsupported attribute type: %s" % (node.name, type))
+               warning("cannot generate import/export for node %s: unsupported attribute type: %s" % (node.name, type))
                importcmd = """// BAD: %s %s
                        %s %s = (%s) 0;""" % (type, attrname, type, attrname, type)
                exportcmd = "// BAD: %s" % type
@@ -182,19 +191,31 @@ export_attrs_template = env.from_string('''
                {% endfor %}break;''')
 
 import_attrs_template = env.from_string('''
-       case iro_{{node.name}}:
-       {
+       case iro_{{node.name}}: {
                {{"ir_mode *mode = read_mode(env);"|ifnset(node,"mode")}}
-               {% for attr in node.attrs %}{{attr.importcmd}}
-               {% endfor %}
-               {% for attr in node.constructor_args %}{{attr.importcmd}}
-               {% endfor %}
-               {% for special in node.special_constructors %}if({{special.attrname}} == {{special.value}})
-                       newnode = new_r_{{special.constrname}}({{node|block}}{{node.arguments|args}});
-               else{% endfor %}
-               newnode = new_r_{{node.name}}({{node|block}}{{node.arguments|args}});
-               {% for (initarg, initfunc) in node.initargs %}{{initfunc}}(newnode, {{initarg}});
-               {% endfor %}
+               {% for attr in node.attrs %}
+               {{attr.importcmd}}
+               {% endfor -%}
+               {% for attr in node.constructor_args %}
+               {{attr.importcmd}}
+               {% endfor -%}
+               {% for special in node.special_constructors %}
+               if ({{special.attrname}} == {{special.value}})
+                       newnode = new_r_{{special.constrname}}(
+{%- filter arguments %}
+{{node|block}}
+{{node.arguments|args}}
+{% endfilter %});
+               else
+               {% endfor -%}
+               newnode = new_r_{{node.name}}(
+{%- filter arguments %}
+{{node|block}}
+{{node.arguments|args}}
+{% endfilter %});
+               {% for (initarg, initfunc) in node.initargs %}
+               {{initfunc}}(newnode, {{initarg}});
+               {% endfor -%}
                break;
        }
 ''')
@@ -208,9 +229,9 @@ def main(argv):
 
        gendir = argv[2]
        # these nodes don't work correctly yet for some reasons...
-       niynodes = [ "EndExcept", "EndReg", "ASM" ]
+       niynodes = [ "ASM" ]
        # these have custom im-/export code
-       customcode = [ "Start", "End", "Anchor", "SymConst", "Block" ]
+       customcode = [ "Start", "End", "Anchor", "SymConst", "Block", "Deleted" ]
 
        real_nodes = []
        for node in ir_spec.nodes:
@@ -240,7 +261,7 @@ def main(argv):
        for node in real_nodes:
                if node.name in niynodes:
                        continue
-               file.write("\tINSERT(\"" + node.name + "\", tt_iro, iro_" + node.name + ");\n");
+               file.write("\tINSERT(tt_iro, \"%s\", iro_%s);\n" % (node.name, node.name));
        file.close()
 
 main(sys.argv)