Commit 35cc1819 authored by Matthias Braun's avatar Matthias Braun
Browse files

merge gen_ir.py and gen_ir_io.py scripts

There was a lot of duplicated code, some changes in the templates allows
to share the two generators.
parent 8dbad942
......@@ -130,17 +130,10 @@ $(gendir)/include/libfirm/% : scripts/templates/% $(IR_SPEC_GENERATOR_DEPS) $(IR
@echo GEN $@
$(Q)$(IR_SPEC_GENERATOR) $(IR_SPEC) "$<" > "$@"
IR_IO_GENERATOR := $(srcdir)/scripts/gen_ir_io.py
IR_IO_GENERATOR_DEPS := $(IR_IO_GENERATOR) $(srcdir)/scripts/spec_util.py $(srcdir)/scripts/filters.py
libfirm_GEN_DIRS += ir/ir include/libfirm
ir/ir/irio.c : $(gendir)/ir/ir/gen_irio.c.inl
$(gendir)/ir/ir/% : scripts/templates_io/% $(IR_IO_GENERATOR_DEPS) $(IR_SPEC)
@echo GEN $@
$(Q)$(IR_IO_GENERATOR) $(IR_SPEC) "$<" > "$@"
$(libfirm_a): $(libfirm_OBJECTS)
@echo AR $@
$(Q)$(AR) -crsu $@ $^
......
......@@ -18,5 +18,8 @@ def filter_isnot(list, flag):
def filter_hasnot(list, flag):
return filter(lambda x: flag not in x, list)
def filter_has(list, flag):
return filter(lambda x: flag in x, list)
def filter_notset(list, flag):
return filter(lambda x: not getattr(x,flag), list)
......@@ -5,7 +5,7 @@
import sys
from jinja2 import Environment, Template, FileSystemLoader
from spec_util import is_dynamic_pinned, isAbstract, setdefault, load_spec
from filters import format_arguments, filter_isnot, filter_hasnot, filter_notset
from filters import format_arguments, filter_isnot, filter_has, filter_hasnot, filter_notset
def format_parameterlist(parameterlist):
return "\n".join(parameterlist)
......@@ -133,6 +133,9 @@ def format_flags(node):
flags = [ "irop_flag_none" ]
return " | ".join(flags)
def format_stringformat(string, *args):
return string % args
def format_attr_size(node):
if not hasattr(node, "attr_struct"):
return "0"
......@@ -152,29 +155,56 @@ def format_escape_keywords(word):
def format_parameters(string):
return format_arguments(string, voidwhenempty = True)
def format_args(arglist):
argument_names = [ a['name'] for a in arglist ]
return "\n".join(argument_names)
def format_block(node):
if hasattr(node, "knownBlock"):
if hasattr(node, "knownGraph"):
return ""
return "env->irg"
else:
return "block"
def format_simplify_type(string):
"""Returns a simplified version of a C type for use in a function name.
Stars are replaced with _ref, spaces removed and the ir_ firm namespace
prefix stripped."""
res = string.replace("*", "_ref").replace(" ", "")
if res.startswith("ir_"):
res = res[3:]
return res
env = Environment(loader=FileSystemLoader([".", "/"]), keep_trailing_newline=True)
env.filters['a_an'] = format_a_an
env.filters['parameterlist'] = format_parameterlist
env.filters['nodearguments'] = format_nodearguments
env.filters['nodeparameters'] = format_nodeparameters
env.filters['nodeparametershelp'] = format_nodeparametershelp
env.filters['args'] = format_args
env.filters['arguments'] = format_arguments
env.filters['arity_and_ins'] = format_arity_and_ins
env.filters['arity'] = format_arity
env.filters['attr_size'] = format_attr_size
env.filters['blockargument'] = format_blockargument
env.filters['block'] = format_block
env.filters['blockparameter'] = format_blockparameter
env.filters['blockparameterhelp'] = format_blockparameterhelp
env.filters['blockargument'] = format_blockargument
env.filters['irgassign'] = format_irgassign
env.filters['curblock'] = format_curblock
env.filters['insdecl'] = format_insdecl
env.filters['arity_and_ins'] = format_arity_and_ins
env.filters['arity'] = format_arity
env.filters['pinned'] = format_pinned
env.filters['escape_keywords'] = format_escape_keywords
env.filters['flags'] = format_flags
env.filters['attr_size'] = format_attr_size
env.filters['opindex'] = format_opindex
env.filters['isnot'] = filter_isnot
env.filters['has'] = filter_has
env.filters['hasnot'] = filter_hasnot
env.filters['arguments'] = format_arguments
env.filters['insdecl'] = format_insdecl
env.filters['irgassign'] = format_irgassign
env.filters['isnot'] = filter_isnot
env.filters['nodearguments'] = format_nodearguments
env.filters['nodeparameters'] = format_nodeparameters
env.filters['nodeparametershelp'] = format_nodeparametershelp
env.filters['notset'] = filter_notset
env.filters['opindex'] = format_opindex
env.filters['parameterlist'] = format_parameterlist
env.filters['parameters'] = format_parameters
env.filters['escape_keywords'] = format_escape_keywords
env.filters['pinned'] = format_pinned
env.filters['simplify_type'] = format_simplify_type
env.filters['stringformat'] = format_stringformat
def prepare_attr(attr):
if "init" in attr:
......@@ -217,7 +247,6 @@ def preprocess_node(node):
type = "ir_mode *",
name = "mode",
comment = "mode of the operations result"))
node.mode = "mode"
for attr in node.attrs:
attr["fqname"] = attr["name"]
......@@ -283,6 +312,8 @@ def main(argv):
env.globals['nodes'] = real_nodes
env.globals['spec'] = spec
env.globals['len'] = len
env.globals['hasattr'] = hasattr
env.globals['is_dynamic_pinned'] = is_dynamic_pinned
env.globals['warning'] = "/* Warning: automatically generated file */"
template = env.get_template(templatefile)
......
#!/usr/bin/env python
#
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology.
import sys
from jinja2 import Environment, Template, FileSystemLoader
from spec_util import is_dynamic_pinned, isAbstract, load_spec
from filters import format_arguments, filter_isnot, filter_hasnot, filter_notset
def error(msg):
"""writes an error message to stderr"""
sys.stderr.write("Error: " + msg + "\n");
def warning(msg):
"""writes a warning message to stderr"""
sys.stderr.write("Warning: " + msg + "\n");
def format_args(arglist):
return "\n".join(arglist)
def format_block(node):
if hasattr(node, "knownBlock"):
if hasattr(node, "knownGraph"):
return ""
return "env->irg"
else:
return "block"
def format_simplify_type(string):
"""Returns a simplified version of a C type for use in a function name.
Stars are replaced with _ref, spaces removed and the ir_ firm namespace
prefix stripped."""
res = string.replace("*", "_ref").replace(" ", "")
if res.startswith("ir_"):
res = res[3:]
return res
env = Environment(loader=FileSystemLoader([".", "/"]), keep_trailing_newline=True)
env.filters['args'] = format_args
env.filters['block'] = format_block
env.filters['arguments'] = format_arguments
env.filters['simplify_type'] = format_simplify_type
env.filters['isnot'] = filter_isnot
env.filters['notset'] = filter_notset
env.filters['hasnot'] = filter_hasnot
def preprocess_node(node):
if node.customSerializer:
return
# construct node arguments
arguments = [ ]
extraattrs = [ ]
for input in node.ins:
arguments.append("in_%s" % input[0])
if node.arity == "variable" or node.arity == "dynamic":
arguments.append("n_preds")
arguments.append("preds")
if not hasattr(node, "mode"):
arguments.append("mode")
for attr in node.attrs:
if "to_flags" in attr:
node.constructorFlags = True
attr['to_flags'] = attr['to_flags'] % (attr["name"])
elif "init" in attr:
extraattrs.append(attr)
else:
arguments.append(attr["name"])
for arg in node.constructor_args:
if arg['type'] != "ir_cons_flags" and arg['name'] != "flags":
error("only ir_cons_flags constructor arg supported in irio")
continue
node.constructorFlags = True
arguments.append("flags")
node.arguments = arguments
node.extraattrs = extraattrs
node.dynamic_pinned = is_dynamic_pinned(node)
def main(argv):
if len(argv) < 3:
print("usage: %s specname(ignored) destdirectory" % argv[0])
sys.exit(1)
specfile = argv[1]
templatefile = argv[2]
spec = load_spec(specfile)
nodes = spec.nodes
real_nodes = []
for node in nodes:
if isAbstract(node):
continue
preprocess_node(node)
real_nodes.append(node)
env.globals['nodes'] = real_nodes
env.globals['spec'] = spec
env.globals['warning'] = "/* Warning: automatically generated file */"
env.globals['hasattr'] = hasattr
template = env.get_template(templatefile)
result = template.render()
sys.stdout.write(result)
if __name__ == "__main__":
main(sys.argv)
......@@ -6,7 +6,7 @@ static ir_node *read_{{node.name}}(read_env_t *env)
ir_node *block = read_node_ref(env);
{%- endif %}
{%- for input in node.ins %}
ir_node *in_{{input[0]}} = read_node_ref(env);
ir_node *irn_{{input[0]}} = read_node_ref(env);
{%- endfor %}
{%- if not hasattr(node, "mode") %}
ir_mode *mode = read_mode_ref(env);
......@@ -14,50 +14,45 @@ static ir_node *read_{{node.name}}(read_env_t *env)
{%- for attr in node.attrs %}
{{attr.type}} {{attr.name}} = read_{{attr.type|simplify_type}}(env);
{%- endfor %}
{%- if node.dynamic_pinned %}
{%- if is_dynamic_pinned(node) %}
op_pin_state pin_state = read_pin_state(env);
{%- endif %}
{%- if "fragile" in node.flags %}
bool throws = read_throws(env);
{%- endif %}
{%- if node.arity == "dynamic" or node.arity == "variable" %}
int n_preds = read_preds(env);
ir_node **preds = (ir_node**)obstack_finish(&env->preds_obst);
int arity = read_preds(env);
ir_node **in = (ir_node**)obstack_finish(&env->preds_obst);
{%- endif %}
{%- if node.constructorFlags %}
ir_cons_flags flags = cons_none;
{%- endif %}
ir_node *res;
{%- if node.constructorFlags %}
{%- if node.attrs|has('to_flags') %}
ir_cons_flags flags = cons_none
{%- for attr in node.attrs %}
{%- if "to_flags" in attr %}
flags |= {{attr.to_flags}};
| ({{attr.to_flags|stringformat(attr.name)}})
{%- endif %}
{%- endfor %}
{%- if node.dynamic_pinned %}
flags |= pin_state == op_pin_state_floats ? cons_floats : cons_none;
{%- if is_dynamic_pinned(node) %}
| (pin_state == op_pin_state_floats ? cons_floats : cons_none)
{%- endif %}
{%- if "fragile" in node.flags %}
flags |= throws ? cons_throws_exception : cons_none;
{%- endif %}
| (throws ? cons_throws_exception : cons_none)
{%- endif -%}
;
{%- endif %}
res = new_r_{{node.name}}(
ir_node *res = new_r_{{node.name}}(
{%- filter arguments %}
{{node|block}}
{{node.arguments|args}}
{%- if node.dynamic_pinned and not hasattr(node, "pinned_init") %}
pin_state
{%- endif %}
{% endfilter %});
{{node|block}}
{{node.arguments|args}}
{%- endfilter %});
{%- if node.arity == "dynamic" or node.arity == "variable" %}
obstack_free(&env->preds_obst, preds);
obstack_free(&env->preds_obst, in);
{%- endif %}
{%- for attr in node.extraattrs %}
{%- for attr in node.attrs|has('init') %}
set_{{node.name}}_{{attr.name}}(res, {{attr.name}});
{%- endfor %}
{%- if not node.constructorFlags %}
{%- if node.dynamic_pinned and hasattr(node, "pinned_init") %}
{%- if is_dynamic_pinned(node) and hasattr(node, "pinned_init") %}
set_irn_pinned(res, pin_state);
{%- endif %}
{%- if "fragile" in node.flags and hasattr(node, "throws_init") %}
......@@ -85,7 +80,7 @@ static void write_{{node.name}}(write_env_t *env, const ir_node *node)
{%- for attr in node.attrs %}
write_{{attr.type|simplify_type}}(env, get_{{node.name}}_{{attr.name}}(node));
{%- endfor %}
{%- if node.dynamic_pinned %}
{%- if is_dynamic_pinned(node) %}
write_pin_state(env, get_irn_pinned(node));
{%- endif %}
{%- if "fragile" in node.flags %}
......
......@@ -57,7 +57,11 @@ ir_node *new_rd_{{node.name}}(
irg
{{node.block}}
op_{{node.name}}
{% if hasattr(node, 'mode') -%}
{{node.mode}}
{%- else -%}
mode
{%- endif %}
{{node|arity_and_ins}}
{% endfilter %});
{%- if node.arity == "dynamic" %}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment