annotate python/testgraph.py @ 279:2ccd57b1d78c

Fix register allocator to do burn2 OK
author Windel Bouwman
date Sat, 12 Oct 2013 09:56:23 +0200
parents 9fca39eebe50
children 02385f62f250
rev   line source
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
3 import unittest
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
4 import graph
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
5 import interferencegraph
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
6 import flowgraph
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
7 import ir
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
8 import irmach
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
9
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
10
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
11 class GraphTestCase(unittest.TestCase):
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
12
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
13 def testEdge(self):
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
14 g = graph.Graph()
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
15 n1 = graph.Node(g)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
16 g.addNode(n1)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
17 n2 = graph.Node(g)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
18 g.addNode(n2)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
19 g.addEdge(n1, n2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
20 self.assertTrue(g.hasEdge(n2, n1))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
21 self.assertTrue(g.hasEdge(n1, n2))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
22 g.delNode(n1)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
23 g.delNode(n2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
24
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
25 def testDegree(self):
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
26 g = graph.Graph()
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
27 n1 = graph.Node(g)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
28 g.addNode(n1)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
29 n2 = graph.Node(g)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
30 g.addNode(n2)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
31 n3 = graph.Node(g)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
32 g.addNode(n3)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
33 g.addEdge(n1, n2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
34 g.addEdge(n1, n3)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
35 self.assertEqual(2, n1.Degree)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
36 self.assertEqual(1, n2.Degree)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
37 g.delNode(n2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
38 self.assertEqual(1, n1.Degree)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
39
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
40
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
41 class DigraphTestCase(unittest.TestCase):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
42 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
43
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
44
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
45 class InterferenceGraphTestCase(unittest.TestCase):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
46 def testNormalUse(self):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
47 t1 = ir.Temp('t1')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
48 t2 = ir.Temp('t2')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
49 t3 = ir.Temp('t3')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
50 t4 = ir.Temp('t4')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
51 t5 = ir.Temp('t5')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
52 t6 = ir.Temp('t6')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
53 instrs = []
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
54 instrs.append(irmach.makeIns('ld %d', dst=[t1]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
55 instrs.append(irmach.makeIns('ld %d', dst=[t2]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
56 instrs.append(irmach.makeIns('ld %d', dst=[t3]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
57 cfg = flowgraph.FlowGraph(instrs)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
58 ig = interferencegraph.InterferenceGraph(cfg)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
59
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
60 def testCombine(self):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
61 t1 = ir.Temp('t1')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
62 t2 = ir.Temp('t2')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
63 t3 = ir.Temp('t3')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
64 t4 = ir.Temp('t4')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
65 instrs = []
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
66 instrs.append(irmach.makeIns('ld %d0', dst=[t1]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
67 instrs.append(irmach.makeIns('ld %d0', dst=[t2]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
68 instrs.append(irmach.makeIns('ld %d0', dst=[t3]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
69 instrs.append(irmach.makeIns('mov %d0, %s0', dst=[t4], src=[t3]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
70 instrs.append(irmach.makeIns('st %s0', src=[t4]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
71 instrs.append(irmach.makeIns('st %s0', src=[t1]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
72 instrs.append(irmach.makeIns('st %s0', src=[t2]))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
73 cfg = flowgraph.FlowGraph(instrs)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
74 ig = interferencegraph.InterferenceGraph(cfg)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
75 ig.to_txt()
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
76 ig.Combine(ig.getNode(t4), ig.getNode(t3))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
77 self.assertIs(ig.getNode(t4), ig.getNode(t3))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
78 print('after')
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
79 ig.to_txt()
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
80
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
81
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
82 if __name__ == '__main__':
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
83 unittest.main()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
84