- remove block parameter from new_r_Proj and new_rd_Proj
[libfirm] / scripts / gen_ir_io.py
index d685b96..acf29fb 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,14 +24,25 @@ 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*":
@@ -79,6 +88,9 @@ def get_io_type(type, attrname, node):
        elif type == "int":
                importcmd = "int %s = (int) read_long(env);" % attrname
                exportcmd = """fprintf(env->file, "%%d ", %(val)s);"""
+       elif type == "unsigned":
+               importcmd = "unsigned %s = (unsigned) read_long(env);" % attrname
+               exportcmd = """fprintf(env->file, "%%u ", %(val)s);"""
        elif type == "long":
                importcmd = "long %s = read_long(env);" % attrname
                exportcmd = """fprintf(env->file, "%%ld ", %(val)s);"""
@@ -179,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;
        }
 ''')
@@ -237,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)