refactor specfiles/scripts
authorMatthias Braun <matthias.braun@kit.edu>
Thu, 16 Aug 2012 11:43:55 +0000 (13:43 +0200)
committerMatthias Braun <matthias.braun@kit.edu>
Tue, 21 Aug 2012 11:48:14 +0000 (13:48 +0200)
Makefile
scripts/filters.py [new file with mode: 0644]
scripts/gen_docu.py
scripts/gen_ir.py
scripts/gen_ir_io.py
scripts/ir_spec.py
scripts/spec_util.py

index 3a55e12..39156e3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -185,7 +185,7 @@ $(docdir)/libfirm.tag: $(IR_SPEC_GENERATED_FILES) Doxyfile $(wildcard include/li
 DOCU_GENERATOR := scripts/gen_docu.py
 $(docdir)/html/nodes.html: $(docdir)/libfirm.tag $(DOCU_GENERATOR) $(IR_SPEC) scripts/spec_util.py scripts/style.css
        @echo gen_docu.py $@
-       $(Q)$(DOCU_GENERATOR) $(docdir)/libfirm.tag "" $@
+       $(Q)$(DOCU_GENERATOR) $(IR_SPEC) $(docdir)/libfirm.tag "" $@
        $(Q)cp scripts/style.css $(docdir)/html
 
 .PHONY: doc
diff --git a/scripts/filters.py b/scripts/filters.py
new file mode 100644 (file)
index 0000000..059b4ea
--- /dev/null
@@ -0,0 +1,20 @@
+import re
+
+def format_arguments(string, voidwhenempty = False):
+       args = re.split('\s*\n\s*', string)
+       if args[0] == '':
+               args = args[1:]
+       if len(args) > 0 and args[-1] == '':
+               args = args[:-1]
+       if len(args) == 0 and voidwhenempty:
+               return "void"
+       return ", ".join(args)
+
+def filter_isnot(list, flag):
+       return filter(lambda x: not hasattr(x, flag), list)
+
+def filter_hasnot(list, flag):
+       return filter(lambda x: flag not in x, list)
+
+def filter_notset(list, flag):
+       return filter(lambda x: not getattr(x,flag), list)
index e6f94c1..07125ef 100755 (executable)
@@ -1,13 +1,10 @@
 #!/usr/bin/env python
 import sys
-import re
 import docutils.core
 import docutils.writers.html4css1
 from datetime import datetime
 from jinja2 import Environment, Template
-from jinja2.filters import do_dictsort
-from spec_util import is_dynamic_pinned, verify_node, isAbstract, setdefault, trim_docstring
-from ir_spec import nodes
+from spec_util import isAbstract, load_spec
 
 tags = None
 linkbase = None
@@ -135,13 +132,9 @@ docu_template = env.from_string(
 
 #############################
 
-def preprocess_node(node):
-       node.doc = trim_docstring(node.__doc__)
-
-def prepare_nodes():
+def prepare_nodes(nodes):
        real_nodes = []
        for node in nodes:
-               preprocess_node(node)
                if isAbstract(node):
                        continue
                real_nodes.append(node)
@@ -151,9 +144,10 @@ def prepare_nodes():
 def main(argv):
        global tags
        output = sys.stdout
-       if len(argv) > 1:
+       specfile = argv[1]
+       if len(argv) > 2:
                output = open(argv[-1], "w")
-       if len(argv) > 3:
+       if len(argv) > 4:
                tagfile = open(argv[-3], "r")
                global linkbase
                linkbase = argv[-2]
@@ -165,7 +159,8 @@ def main(argv):
                except:
                        tags = None
 
-       real_nodes = prepare_nodes()
+       spec = load_spec(specfile)
+       real_nodes = prepare_nodes(spec.nodes)
        time = datetime.now().replace(microsecond=0).isoformat(' ')
        output.write(docu_template.render(nodes=real_nodes, time=time))
        if output != sys.stdout:
index bd1105d..a1e09b7 100755 (executable)
@@ -1,10 +1,8 @@
 #!/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, setdefault, trim_docstring
-from ir_spec import nodes
+from spec_util import is_dynamic_pinned, isAbstract, setdefault, load_spec
+from filters import format_arguments, filter_isnot, filter_hasnot, filter_notset
 
 def format_parameterlist(parameterlist):
        return "\n".join(parameterlist)
@@ -159,22 +157,6 @@ def format_escape_keywords(word):
                return word + "_"
        return word
 
-def filter_isnot(list, flag):
-       return filter(lambda x: not hasattr(x, flag), list)
-
-def filter_hasnot(list, flag):
-       return filter(lambda x: flag not in x, list)
-
-def format_arguments(string, voidwhenempty = False):
-       args = re.split('\s*\n\s*', string)
-       if args[0] == '':
-               args = args[1:]
-       if len(args) > 0 and args[-1] == '':
-               args = args[:-1]
-       if len(args) == 0 and voidwhenempty:
-               return "void"
-       return ", ".join(args)
-
 def format_parameters(string):
        return format_arguments(string, voidwhenempty = True)
 
@@ -216,9 +198,6 @@ def prepare_attr(attr):
                        comment = attr["comment"])
 
 def preprocess_node(node):
-       verify_node(node)
-       node.doc = trim_docstring(node.__doc__)
-
        setdefault(node, "attrs_name", node.name.lower())
        setdefault(node, "block", "block")
 
@@ -456,7 +435,7 @@ static const pns_lookup_t {{node.name}}_lut[] = {
 static const proj_lookup_t proj_lut[] = {
        {%- for node in nodes -%}
        {%- if node.outs %}
-       { iro_{{node.name}}, ARRAY_SIZE({{node.name}}_lut), {{node.name}}_lut },
+       { {{spec.name}}o_{{node.name}}, ARRAY_SIZE({{node.name}}_lut), {{node.name}}_lut },
        {%- endif %}
        {%- endfor %}
 };
@@ -473,7 +452,7 @@ static void generated_init_op(void)
        {%- for node in nodes %}
        op_{{node.name}} = new_ir_op(
                {%- filter arguments %}
-                       iro_{{node.name}}
+                       {{spec.name}}o_{{node.name}}
                        "{{node.name}}"
                        {{node|pinned}}
                        {{node|flags}}
@@ -631,13 +610,14 @@ opcodes_h_template = env.from_string(
 /** The opcodes of the libFirm predefined operations.
  * @ingroup ir_op
  */
-typedef enum ir_opcode {
+typedef enum {{spec.name}}_opcode {
 {%- for node in nodes %}
-       iro_{{node.name}},
+       {{spec.name}}o_{{node.name}},
 {%- endfor %}
-       iro_First = iro_{{nodes[0].name}},
-       iro_Last = iro_{{nodes[-1].name}},
+       {{spec.name}}o_First = {{spec.name}}o_{{nodes[0].name}},
+       {{spec.name}}o_Last  = {{spec.name}}o_{{nodes[-1].name}},
 
+{%- if spec.name == "ir" %}
        beo_First,
        /* backend specific nodes */
        beo_Spill = beo_First,
@@ -656,8 +636,9 @@ typedef enum ir_opcode {
        beo_FrameAddr,
        /* last backend node number */
        beo_Last = beo_FrameAddr,
-       iro_MaxOpcode
-} ir_opcode;
+{%- endif %}
+       {{spec.name}}o_MaxOpcode
+} {{spec.name}}_opcode;
 
 {% for node in nodes %}
 /**
@@ -681,7 +662,7 @@ FIRM_API ir_op *get_op_{{node.name}}(void);
 
 #############################
 
-def prepare_nodes():
+def prepare_nodes(nodes):
        real_nodes = []
        for node in nodes:
                if isAbstract(node):
@@ -698,38 +679,48 @@ def main(argv):
                print "usage: %s specname(ignored) destdirectory" % argv[0]
                sys.exit(1)
 
+       specfile = argv[1]
+       spec = load_spec(specfile)
+       nodes = spec.nodes
+
        gendir = argv[2]
        # hardcoded path to libfirm/include/libfirm
-       gendir2 = argv[2] + "/../../include/libfirm"
+       if len(argv) > 3:
+               gendir2 = argv[3]
+       else:
+               gendir2 = argv[2] + "/../../include/libfirm"
+
+       real_nodes = prepare_nodes(nodes)
 
-       real_nodes = prepare_nodes()
+       env.globals['nodes'] = real_nodes
+       env.globals['spec'] = spec
 
        file = open(gendir + "/gen_ir_cons.c.inl", "w")
-       file.write(gen_ircons_c_inl_template.render(nodes = real_nodes))
+       file.write(gen_ircons_c_inl_template.render())
        file.close()
 
        file = open(gendir + "/gen_irnode.h", "w")
-       file.write(irnode_h_template.render(nodes = real_nodes))
+       file.write(irnode_h_template.render())
        file.close()
 
        file = open(gendir + "/gen_irnode.c.inl", "w")
-       file.write(irnode_template.render(nodes = real_nodes))
+       file.write(irnode_template.render())
        file.close()
 
        file = open(gendir + "/gen_irop.c.inl", "w")
-       file.write(irop_template.render(nodes = real_nodes))
+       file.write(irop_template.render())
        file.close()
 
        file = open(gendir + "/gen_irdump.c.inl", "w")
-       file.write(irdump_template.render(nodes = real_nodes))
+       file.write(irdump_template.render())
        file.close()
 
        file = open(gendir2 + "/opcodes.h", "w")
-       file.write(opcodes_h_template.render(nodes = real_nodes))
+       file.write(opcodes_h_template.render())
        file.close()
 
        file = open(gendir2 + "/nodeops.h", "w")
-       file.write(nodeops_h_template.render(nodes = real_nodes))
+       file.write(nodeops_h_template.render())
        file.close()
 
 main(sys.argv)
index 1d03f29..2692460 100755 (executable)
@@ -1,10 +1,8 @@
 #!/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
-import ir_spec
+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"""
@@ -25,23 +23,6 @@ def format_block(node):
        else:
                return "block"
 
-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)
-
-def filter_isnot(list, flag):
-       return filter(lambda x: not hasattr(x, flag), list)
-
-def filter_notset(list, flag):
-       return filter(lambda x: not getattr(x,flag), list)
-
-def filter_hasnot(list, flag):
-       return filter(lambda x: flag not in x, list)
-
 env = Environment()
 env.filters['args']      = format_args
 env.filters['block']     = format_block
@@ -118,8 +99,6 @@ def prepare_attr(node, attr):
 
 
 def preprocess_node(node):
-       verify_node(node)
-
        if node.customSerializer:
                return
 
@@ -276,10 +255,13 @@ def main(argv):
                print "usage: %s specname(ignored) destdirectory" % argv[0]
                sys.exit(1)
 
+       specfile = argv[1]
        gendir = argv[2]
 
+       spec = load_spec(specfile)
+       nodes = spec.nodes
        real_nodes = []
-       for node in ir_spec.nodes:
+       for node in nodes:
                if isAbstract(node):
                        continue
                preprocess_node(node)
index 4a7aea0..4d35001 100755 (executable)
@@ -1,13 +1,13 @@
 # Firm node specifications
 # The comments are in (standard python) restructured text format and are used
 # to generate documentation.
-from spec_util import abstract, setnodedefaults
+from spec_util import abstract, op
 
-class Op(object):
-       """Base class for firm nodes"""
-abstract(Op)
+name = "ir"
 
-class Unop(Op):
+@abstract
+@op
+class Unop(object):
        """Unary nodes have exactly 1 input"""
        name     = "unop"
        ins      = [
@@ -15,9 +15,10 @@ class Unop(Op):
        ]
        op_index = 0
        pinned   = "no"
-abstract(Unop)
 
-class Binop(Op):
+@abstract
+@op
+class Binop(object):
        """Binary nodes have exactly 2 inputs"""
        name     = "binop"
        ins      = [
@@ -26,13 +27,14 @@ class Binop(Op):
        ]
        op_index = 0
        pinned   = "no"
-abstract(Binop)
 
+@op
 class Add(Binop):
        """returns the sum of its operands"""
        flags = [ "commutative" ]
 
-class Alloc(Op):
+@op
+class Alloc:
        """allocates a block of memory.
        It can be specified whether the memory should be allocated to the stack
        or to the heap.
@@ -66,7 +68,8 @@ class Alloc(Op):
        pinned_init = "op_pin_state_pinned"
        attr_struct = "alloc_attr"
 
-class Anchor(Op):
+@op
+class Anchor:
        """utiliy node used to "hold" nodes in a graph that might possibly not be
        reachable by other means or which should be reachable immediately without
        searching through the graph.
@@ -83,11 +86,13 @@ class Anchor(Op):
        noconstructor    = True
        customSerializer = True
 
+@op
 class And(Binop):
        """returns the result of a bitwise and operation of its operands"""
        flags    = [ "commutative" ]
 
-class ASM(Op):
+@op
+class ASM:
        """executes assembler fragments of the target machine.
 
        The node contains a template for an assembler snippet. The compiler will
@@ -164,7 +169,8 @@ class ASM(Op):
        # constraints arrays needing special handling (2 arguments for 1 attribute)
        noconstructor = True
 
-class Bad(Op):
+@op
+class Bad:
        """Bad nodes indicate invalid input, which is values which should never be
        computed.
 
@@ -191,7 +197,8 @@ class Bad(Op):
        res->attr.bad.irg.irg = irg;
        '''
 
-class Deleted(Op):
+@op
+class Deleted:
        """Internal node which is temporary set to nodes which are already removed
        from the graph."""
        mode             = "mode_Bad"
@@ -200,7 +207,8 @@ class Deleted(Op):
        noconstructor    = True
        customSerializer = True # this has no serializer
 
-class Block(Op):
+@op
+class Block:
        """A basic block"""
        mode             = "mode_BB"
        knownBlock       = True
@@ -231,11 +239,13 @@ class Block(Op):
        }
        '''
 
+@op
 class Borrow(Binop):
        """Returns the borrow bit from and implied subtractions of its 2 operands"""
        flags = []
 
-class Bound(Op):
+@op
+class Bound:
        """Performs a bounds-check: if lower <= index < upper then return index,
        otherwise throw an exception."""
        ins    = [
@@ -256,7 +266,8 @@ class Bound(Op):
        throws_init = "false"
        attr_struct = "bound_attr"
 
-class Builtin(Op):
+@op
+class Builtin:
        """performs a backend-specific builtin."""
        ins      = [
                ("mem", "memory dependency"),
@@ -286,7 +297,8 @@ class Builtin(Op):
        assert((get_unknown_type() == type) || is_Method_type(type));
        '''
 
-class Call(Op):
+@op
+class Call:
        """Calls other code. Control flow is transfered to ptr, additional
        operands are passed to the called code. Called code usually performs a
        return operation. The operands of this return operation are the result
@@ -318,11 +330,13 @@ class Call(Op):
        assert((get_unknown_type() == type) || is_Method_type(type));
        '''
 
+@op
 class Carry(Binop):
        """Computes the value of the carry-bit that would result when adding the 2
        operands"""
        flags = [ "commutative" ]
 
+@op
 class Cast(Unop):
        """perform a high-level type cast"""
        mode     = "get_irn_mode(irn_op)"
@@ -337,6 +351,7 @@ class Cast(Unop):
        attr_struct = "cast_attr"
        init     = "assert(is_atomic_type(type));"
 
+@op
 class Cmp(Binop):
        """Compares its two operands and checks whether a specified
           relation (like less or equal) is fulfilled."""
@@ -351,7 +366,8 @@ class Cmp(Binop):
        ]
        attr_struct = "cmp_attr"
 
-class Cond(Op):
+@op
+class Cond:
        """Conditionally change control flow."""
        ins      = [
                ("selector",  "condition parameter"),
@@ -372,7 +388,8 @@ class Cond(Op):
        ]
        attr_struct = "cond_attr"
 
-class Switch(Op):
+@op
+class Switch:
        """Change control flow. The destination is choosen based on an integer input value which is looked up in a table.
 
        Backends can implement this efficiently using a jump table."""
@@ -399,7 +416,8 @@ class Switch(Op):
        attr_struct = "switch_attr"
        attrs_name  = "switcha"
 
-class Confirm(Op):
+@op
+class Confirm:
        """Specifies constraints for a value. This allows explicit representation
        of path-sensitive properties. (Example: This value is always >= 0 on 1
        if-branch then all users within that branch are rerouted to a confirm-node
@@ -425,7 +443,8 @@ class Confirm(Op):
        ]
        attr_struct = "confirm_attr"
 
-class Const(Op):
+@op
+class Const:
        """Returns a constant value."""
        flags      = [ "constlike", "start_block" ]
        block      = "get_irg_start_block(irg)"
@@ -442,6 +461,7 @@ class Const(Op):
        attr_struct = "const_attr"
        attrs_name  = "con"
 
+@op
 class Conv(Unop):
        """Converts values between modes"""
        flags = []
@@ -455,7 +475,8 @@ class Conv(Unop):
        ]
        attr_struct = "conv_attr"
 
-class CopyB(Op):
+@op
+class CopyB:
        """Copies a block of memory with statically known size/type."""
        ins   = [
                ("mem",  "memory dependency"),
@@ -480,7 +501,8 @@ class CopyB(Op):
        pinned_init = "op_pin_state_pinned"
        throws_init = "false"
 
-class Div(Op):
+@op
+class Div:
        """returns the quotient of its 2 operands"""
        ins   = [
                ("mem",   "memory dependency"),
@@ -512,7 +534,8 @@ class Div(Op):
        op_index    = 1
        arity_override = "oparity_binary"
 
-class Dummy(Op):
+@op
+class Dummy:
        """A placeholder value. This is used when constructing cyclic graphs where
        you have cases where not all predecessors of a phi-node are known. Dummy
        nodes are used for the unknown predecessors and replaced later."""
@@ -522,7 +545,8 @@ class Dummy(Op):
        pinned     = "yes"
        block      = "get_irg_start_block(irg)"
 
-class End(Op):
+@op
+class End:
        """Last node of a graph. It references nodes in endless loops (so called
        keepalive edges)"""
        mode             = "mode_X"
@@ -533,13 +557,15 @@ class End(Op):
        block            = "get_irg_end_block(irg)"
        singleton        = True
 
+@op
 class Eor(Binop):
        """returns the result of a bitwise exclusive or operation of its operands.
 
        This is also known as the Xor operation."""
        flags    = [ "commutative" ]
 
-class Free(Op):
+@op
+class Free:
        """Frees a block of memory previously allocated by an Alloc node"""
        ins    = [
                ("mem",   "memory dependency" ),
@@ -563,7 +589,8 @@ class Free(Op):
        ]
        attr_struct = "free_attr"
 
-class Id(Op):
+@op
+class Id:
        """Returns its operand unchanged.
 
        This is mainly used when exchanging nodes. Usually you shouldn't see Id
@@ -574,7 +601,8 @@ class Id(Op):
        pinned = "no"
        flags  = []
 
-class IJmp(Op):
+@op
+class IJmp:
        """Jumps to the code in its argument. The code has to be in the same
        function and the the destination must be one of the blocks reachable
        by the tuple results"""
@@ -585,7 +613,8 @@ class IJmp(Op):
        ]
        flags    = [ "cfopcode", "forking", "keep", "unknown_jump" ]
 
-class InstOf(Op):
+@op
+class InstOf:
        """Tests whether an object is an instance of a class-type"""
        ins   = [
           ("store", "memory dependency"),
@@ -609,14 +638,16 @@ class InstOf(Op):
        pinned      = "memory"
        pinned_init = "op_pin_state_floats"
 
-class Jmp(Op):
+@op
+class Jmp:
        """Jumps to the block connected through the out-value"""
        mode     = "mode_X"
        pinned   = "yes"
        ins      = []
        flags    = [ "cfopcode" ]
 
-class Load(Op):
+@op
+class Load:
        """Loads a value from memory (heap or stack)."""
        ins   = [
                ("mem", "memory dependency"),
@@ -662,11 +693,13 @@ class Load(Op):
        pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
        throws_init = "(flags & cons_throws_exception) != 0"
 
+@op
 class Minus(Unop):
        """returns the difference between its operands"""
        flags = []
 
-class Mod(Op):
+@op
+class Mod:
        """returns the remainder of its operands from an implied division.
 
        Examples:
@@ -710,7 +743,8 @@ class Mulh(Binop):
        would not fit into the result mode of a normal Mul anymore)"""
        flags = [ "commutative" ]
 
-class Mux(Op):
+@op
+class Mux:
        """returns the false or true operand depending on the value of the sel
        operand"""
        ins    = [
@@ -721,7 +755,8 @@ class Mux(Op):
        flags  = []
        pinned = "no"
 
-class NoMem(Op):
+@op
+class NoMem:
        """Placeholder node for cases where you don't need any memory input"""
        mode          = "mode_M"
        flags         = [ "dump_noblock" ]
@@ -730,15 +765,18 @@ class NoMem(Op):
        block         = "get_irg_start_block(irg)"
        singleton     = True
 
+@op
 class Not(Unop):
        """returns the bitwise complement of a value. Works for boolean values, too."""
        flags = []
 
+@op
 class Or(Binop):
        """returns the result of a bitwise or operation of its operands"""
        flags = [ "commutative" ]
 
-class Phi(Op):
+@op
+class Phi:
        """Choose a value based on control flow. A phi node has 1 input for each
        predecessor of its block. If a block is entered from its nth predecessor
        all phi nodes produce their nth input as result."""
@@ -755,7 +793,8 @@ class Phi(Op):
                add_End_keepalive(get_irg_end(irg), res);'''
        customSerializer = True
 
-class Pin(Op):
+@op
+class Pin:
        """Pin the value of the node node in the current block. No users of the Pin
        node can float above the Block of the Pin. The node cannot float behind
        this block. Often used to Pin the NoMem node."""
@@ -766,7 +805,8 @@ class Pin(Op):
        flags    = [ "highlevel" ]
        pinned   = "yes"
 
-class Proj(Op):
+@op
+class Proj:
        """returns an entry of a tuple value"""
        ins              = [
                ("pred", "the tuple value from which a part is extracted"),
@@ -786,7 +826,8 @@ class Proj(Op):
        ]
        attr_struct = "proj_attr"
 
-class Raise(Op):
+@op
+class Raise:
        """Raises an exception. Unconditional change of control flow. Writes an
        explicit Except variable to memory to pass it to the exception handler.
        Must be lowered to a Call to a runtime check function."""
@@ -801,7 +842,8 @@ class Raise(Op):
        flags  = [ "highlevel", "cfopcode" ]
        pinned = "yes"
 
-class Return(Op):
+@op
+class Return:
        """Returns from the current function. Takes memory and return values as
        operands."""
        ins      = [
@@ -817,7 +859,8 @@ class Rotl(Binop):
        operand"""
        flags    = []
 
-class Sel(Op):
+@op
+class Sel:
        """Computes the address of a entity of a compound type given the base
        address of an instance of the compound type.
 
@@ -840,6 +883,7 @@ class Sel(Op):
        ]
        attr_struct = "sel_attr"
 
+@op
 class Shl(Binop):
        """Returns its first operands bits shifted left by the amount of the 2nd
        operand.
@@ -848,6 +892,7 @@ class Shl(Binop):
        the right input modulo this modulo_shift amount."""
        flags = []
 
+@op
 class Shr(Binop):
        """Returns its first operands bits shifted right by the amount of the 2nd
        operand. No special handling for the sign bit is performed (zero extension).
@@ -856,6 +901,7 @@ class Shr(Binop):
        the right input modulo this modulo_shift amount."""
        flags = []
 
+@op
 class Shrs(Binop):
        """Returns its first operands bits shifted right by the amount of the 2nd
        operand. The leftmost bit (usually the sign bit) stays the same
@@ -865,7 +911,8 @@ class Shrs(Binop):
        the right input modulo this modulo_shift amount."""
        flags = []
 
-class Start(Op):
+@op
+class Start:
        """The first node of a graph. Execution starts with this node."""
        outs       = [
                ("X_initial_exec", "control flow"),
@@ -880,7 +927,8 @@ class Start(Op):
        knownBlock       = True
        block            = "get_irg_start_block(irg)"
 
-class Store(Op):
+@op
+class Store:
        """Stores a value into memory (heap or stack)."""
        ins   = [
           ("mem",   "memory dependency"),
@@ -921,11 +969,13 @@ class Store(Op):
                ),
        ]
 
+@op
 class Sub(Binop):
        """returns the difference of its operands"""
        flags = []
 
-class SymConst(Op):
+@op
+class SymConst:
        """A symbolic constant.
 
         - *symconst_type_size* The symbolic constant represents the size of a type.
@@ -959,7 +1009,8 @@ class SymConst(Op):
        # union argument
        noconstructor = True
 
-class Sync(Op):
+@op
+class Sync:
        """The Sync operation unifies several partial memory blocks. These blocks
        have to be pairwise disjunct or the values in common locations have to
        be identical.  This operation allows to specify all operations that
@@ -970,7 +1021,8 @@ class Sync(Op):
        pinned   = "no"
        arity    = "dynamic"
 
-class Tuple(Op):
+@op
+class Tuple:
        """Builds a Tuple from single values.
 
        This is needed to implement optimizations that remove a node that produced
@@ -984,26 +1036,11 @@ class Tuple(Op):
        pinned = "no"
        flags  = []
 
-class Unknown(Op):
+@op
+class Unknown:
        """Returns an unknown (at compile- and runtime) value. It is a valid
        optimisation to replace an Unknown by any other constant value."""
        knownBlock = True
        pinned     = "yes"
        block      = "get_irg_start_block(irg)"
        flags      = [ "start_block", "constlike", "dump_noblock" ]
-
-# Prepare node list
-
-def getOpList(namespace):
-       nodes = []
-       for t in namespace.values():
-               if type(t) != type:
-                       continue
-
-               if issubclass(t, Op):
-                       setnodedefaults(t)
-                       nodes.append(t)
-       return nodes
-
-nodes = getOpList(globals())
-nodes = sorted(nodes, lambda x,y: cmp(x.name, y.name))
index 9706385..3e537dd 100644 (file)
@@ -1,4 +1,5 @@
 import sys
+import imp
 
 abstracts = set()
 def abstract(cls):
@@ -7,6 +8,12 @@ def abstract(cls):
 def isAbstract(nodetype):
        return nodetype in abstracts
 
+def op(cls):
+       cls.__is_firm_op = True
+       return cls
+def isOp(nodetype):
+       return hasattr(nodetype, "__is_firm_op")
+
 def is_dynamic_pinned(node):
        return node.pinned in ["memory", "exception"]
 
@@ -21,28 +28,30 @@ def inout_contains(l, name):
 
 def verify_node(node):
        if not hasattr(node, "pinned"):
-               print "%s: NO PINNED SET" % node.__name__
+               print "%s: NO PINNED SET" % node.name
        elif node.pinned not in ["yes", "no", "memory", "exception"]:
-               print "%s: UNKNOWN PINNED MODE: %s" % (node.__name__, node.pinned)
+               print "%s: UNKNOWN PINNED MODE: %s" % (node.name, node.pinned)
 
-       if not hasattr(node, "flags") and not isAbstract(node):
-               print "WARNING: no flags specified for %s\n" % node.__name__
+       if not hasattr(node, "flags"):
+               if not isAbstract(node):
+                       print "WARNING: no flags specified for %s\n" % node.name
        elif type(node.flags) != list:
-               print "ERROR: flags of %s not a list" % node.__name__
+               print "ERROR: flags of %s not a list" % node.name
+
        if hasattr(node, "pinned_init") and not is_dynamic_pinned(node):
-               print "ERROR: node %s has pinned_init attribute but is not marked as dynamically pinned" % node.__name__
+               print "ERROR: node %s has pinned_init attribute but is not marked as dynamically pinned" % node.name
        if hasattr(node, "flags") and "uses_memory" in node.flags:
                if not inout_contains(node.ins, "mem"):
-                       print "ERROR: memory op %s needs an input named 'mem'" % node.__name__
+                       print "ERROR: memory op %s needs an input named 'mem'" % node.name
        if is_fragile(node):
                if not is_dynamic_pinned(node):
-                       print "ERROR: fragile node %s must be dynamically pinned" % node.__name__
+                       print "ERROR: fragile node %s must be dynamically pinned" % node.name
                if not hasattr(node, "throws_init"):
-                       print "ERROR: fragile node %s needs a throws_init attribute" % node.__name__
+                       print "ERROR: fragile node %s needs a throws_init attribute" % node.name
                if not inout_contains(node.outs, "X_regular"):
-                       print "ERROR: fragile node %s needs an output named 'X_regular'" % node.__name__
+                       print "ERROR: fragile node %s needs an output named 'X_regular'" % node.name
                if not inout_contains(node.outs, "X_except"):
-                       print "ERROR: fragile node %s needs an output named 'X_except'" % node.__name__
+                       print "ERROR: fragile node %s needs an output named 'X_except'" % node.name
        else:
                if hasattr(node, "throws_init"):
                        print "ERROR: throws_init only makes sense for fragile nodes"
@@ -68,9 +77,30 @@ def setnodedefaults(node):
        setdefault(node, "attrs", [])
        setdefault(node, "constructor_args", [])
        setdefault(node, "customSerializer", False)
+       if hasattr(node, "__doc__"):
+               node.doc = trim_docstring(node.__doc__)
+       else:
+               node.doc = ""
        if hasattr(node, "outs"):
                node.mode = "mode_T"
 
+def load_spec(filename):
+       module = imp.load_source('spec', filename)
+       nodes = []
+       for x in module.__dict__.values():
+               if not isOp(x):
+                       continue
+               setnodedefaults(x)
+               verify_node(x)
+               nodes.append(x)
+       nodes.sort(key=lambda x: x.name)
+       module.nodes = nodes
+       if len(nodes) == 0:
+               print "Warning: No nodes found in spec file '%s'" % filename
+       if not hasattr(module, "name"):
+               print "Warning: No name specified in file '%s'" % filename
+       return module
+
 def trim_docstring(docstring):
     if not docstring:
         return ''