7fd35a4a82d316ccc7f0f1cf746063a6a71a711e
[libfirm] / spec_util.py
1 abstracts = set()
2 def abstract(cls):
3         abstracts.add(cls)
4         return cls
5 def isAbstract(nodetype):
6         return nodetype in abstracts
7
8 def is_dynamic_pinned(node):
9         return node.pinned in ["memory", "exception"]
10
11 def inout_contains(l, name):
12         for entry in l:
13                 if entry[0] == name:
14                         return True
15         return False
16
17 def verify_node(node):
18         if not hasattr(node, "pinned"):
19                 print "%s: NO PINNED SET" % node.__name__
20         elif node.pinned not in ["yes", "no", "memory", "exception"]:
21                 print "%s: UNKNOWN PINNED MODE: %s" % (node.__name__, node.pinned)
22
23         if not hasattr(node, "flags") and not isAbstract(node):
24                 print "WARNING: no flags specified for %s\n" % node.__name__
25         elif type(node.flags) != list:
26                 print "ERROR: flags of %s not a list" % node.__name__
27         if hasattr(node, "flags"):
28                 flags = node.flags
29                 if "fragile" in flags:
30                         if not inout_contains(node.ins, "mem"):
31                                 print "ERROR: fragile node %s needs an input named 'mem'" % node.__name__
32                         if not inout_contains(node.outs, "X_regular"):
33                                 print "ERROR: fragile node %s needs an output named 'X_regular'" % node.__name__
34                         if not inout_contains(node.outs, "X_except"):
35                                 print "ERROR: fragile node %s needs an output named 'X_except'" % node.__name__
36
37 def setldefault(node, attr, val):
38         # Don't use hasattr, as these things should not be inherited
39         if attr not in node.__dict__:
40                 setattr(node, attr, val)
41
42 def setdefault(node, attr, val):
43         # Don't use hasattr, as these things should not be inherited
44         if not hasattr(node, attr):
45                 setattr(node, attr, val)
46
47 def setnodedefaults(node):
48         setldefault(node, "name", node.__name__)
49         if isAbstract(node):
50                 return
51
52         setdefault(node, "ins", [])
53         setdefault(node, "arity", len(node.ins))
54         setdefault(node, "attrs", [])
55         setdefault(node, "constructor_args", [])
56         setdefault(node, "customSerializer", False)
57         if hasattr(node, "outs"):
58                 node.mode = "mode_T"