Python
The purpose of the python module is to embed the python interpreter and wrap the jitdata structure to make it possible to write experimental passes rapidly in python.
A very brief example of a function that orders basic blocks in post order and displays their numbers. Let's suppose it's located in a file foo.py.
1 class post_order:
2 def __init__(self):
3 self.marked = { }
4 def __call__(self, bb):
5 if self.marked.has_key(bb):
6 return [ ]
7 else:
8 self.marked[bb] = True
9 return sum([ self(succ) for succ in bb.successors], []) + [ bb ]
10 def display_post_order(jd):
11 print "----[", jd.method.klass.name, jd.method.name, "]----"
12 po = post_order()
13 po = po(jd.basic_blocks[0])
14 print [b.nr for b in po]
To call it from C:
pythonpass_run(jd, "foo.py", "display_post_order");
An example that renders the control flow graph using dot and displays it:
1 def cfg(jd):
2 f = open("/tmp/x.dot", "w")
3
4 print >> f, "digraph g {"
5
6 for b in jd.basic_blocks:
7
8 attrs = []
9
10 if b.exception_handler:
11 attrs.append(('color', 'red'))
12
13 print >> f, b, '[', ','.join([ '%s="%s"' % attr for attr in attrs ]), '];'
14
15 for b in jd.basic_blocks:
16 for s in b.successors:
17 print >> f, b, "->", s, ";"
18 for p in b.predecessors:
19 print >> f, p, "->", b, ";"
20
21 print >> f, "};"
22
23 f.close()
24
25 import os
26 os.system("dot -Tpng /tmp/x.dot -o /tmp/x.png")
27 os.system("kuickshow /tmp/x.png")