Commit 8c1ce44e authored by Matthias Braun's avatar Matthias Braun
Browse files

refactor specfiles/scripts

parent 28f0f020
......@@ -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
......
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
......@@ -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:
......
#!/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)
#!/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)
......
# 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):