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
--- /dev/null
+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)
#!/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
#############################
-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)
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]
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:
#!/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)
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)
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")
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 %}
};
{%- 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}}
/** 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,
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 %}
/**
#############################
-def prepare_nodes():
+def prepare_nodes(nodes):
real_nodes = []
for node in nodes:
if isAbstract(node):
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)
#!/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"""
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
def preprocess_node(node):
- verify_node(node)
-
if node.customSerializer:
return
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)
# 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 = [
]
op_index = 0
pinned = "no"
-abstract(Unop)
-class Binop(Op):
+@abstract
+@op
+class Binop(object):
"""Binary nodes have exactly 2 inputs"""
name = "binop"
ins = [
]
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.
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.
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
# 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.
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"
noconstructor = True
customSerializer = True # this has no serializer
-class Block(Op):
+@op
+class Block:
"""A basic block"""
mode = "mode_BB"
knownBlock = True
}
'''
+@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 = [
throws_init = "false"
attr_struct = "bound_attr"
-class Builtin(Op):
+@op
+class Builtin:
"""performs a backend-specific builtin."""
ins = [
("mem", "memory dependency"),
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
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)"
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."""
]
attr_struct = "cmp_attr"
-class Cond(Op):
+@op
+class Cond:
"""Conditionally change control flow."""
ins = [
("selector", "condition parameter"),
]
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."""
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
]
attr_struct = "confirm_attr"
-class Const(Op):
+@op
+class Const:
"""Returns a constant value."""
flags = [ "constlike", "start_block" ]
block = "get_irg_start_block(irg)"
attr_struct = "const_attr"
attrs_name = "con"
+@op
class Conv(Unop):
"""Converts values between modes"""
flags = []
]
attr_struct = "conv_attr"
-class CopyB(Op):
+@op
+class CopyB:
"""Copies a block of memory with statically known size/type."""
ins = [
("mem", "memory dependency"),
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"),
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."""
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"
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" ),
]
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
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"""
]
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"),
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"),
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:
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 = [
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" ]
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."""
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."""
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"),
]
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."""
flags = [ "highlevel", "cfopcode" ]
pinned = "yes"
-class Return(Op):
+@op
+class Return:
"""Returns from the current function. Takes memory and return values as
operands."""
ins = [
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.
]
attr_struct = "sel_attr"
+@op
class Shl(Binop):
"""Returns its first operands bits shifted left by the amount of the 2nd
operand.
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).
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
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"),
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"),
),
]
+@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.
# 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
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
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))
import sys
+import imp
abstracts = set()
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"]
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"
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 ''