301
|
1 import unittest
|
|
2 import os
|
252
|
3 import sys
|
309
|
4 import io
|
256
|
5 import ppci
|
301
|
6 from ppci import ir
|
307
|
7 from ppci import irutils
|
300
|
8 from ppci.transform import ConstantFolder
|
204
|
9
|
272
|
10
|
253
|
11 class IrCodeTestCase(unittest.TestCase):
|
307
|
12 def testAdd(self):
|
|
13 v = ir.Add(ir.Const(1), ir.Const(2))
|
|
14
|
|
15
|
|
16 class IrBuilderTestCase(unittest.TestCase):
|
253
|
17 def setUp(self):
|
307
|
18 self.b = irutils.Builder()
|
253
|
19 self.m = ir.Module('test')
|
|
20 self.b.setModule(self.m)
|
|
21
|
|
22 def testBuilder(self):
|
|
23 f = self.b.newFunction('add')
|
|
24 self.b.setFunction(f)
|
268
|
25 bb = self.b.newBlock()
|
274
|
26 self.b.emit(ir.Jump(bb))
|
268
|
27 self.b.setBlock(bb)
|
272
|
28 self.b.emit(ir.Exp(ir.Const(0)))
|
274
|
29 self.b.emit(ir.Jump(f.epiloog))
|
253
|
30 self.m.check()
|
|
31 # Run interpreter:
|
268
|
32 # r = self.m.getFunction('add').call(1, 2)
|
253
|
33 #self.assertEqual(3, r)
|
|
34
|
272
|
35
|
277
|
36 class PatternMatchTestCase(unittest.TestCase):
|
|
37 def testSimpleTree(self):
|
|
38 t = ir.Term('x')
|
|
39 pat = ir.Binop(ir.Const(2), '+', t)
|
|
40 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
|
|
41 self.assertTrue(res)
|
|
42 self.assertIn(t, mp)
|
|
43 self.assertEqual(3, mp[t])
|
|
44
|
|
45 def testSimpleTree2(self):
|
|
46 t = ir.Term('x')
|
|
47 t2 = ir.Term('y')
|
|
48 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
|
|
49 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat)
|
|
50 self.assertTrue(res)
|
|
51 self.assertIn(t, mp)
|
|
52 self.assertEqual(2, mp[t])
|
|
53 self.assertIn(t2, mp)
|
|
54 self.assertEqual(3, mp[t2])
|
|
55 res, mp = ir.match_tree(ir.Const(2), pat)
|
|
56 self.assertFalse(res)
|
|
57
|
|
58
|
204
|
59 class ConstantFolderTestCase(unittest.TestCase):
|
|
60 def setUp(self):
|
307
|
61 self.b = irutils.Builder()
|
300
|
62 self.cf = ConstantFolder()
|
237
|
63 self.m = ir.Module('test')
|
|
64 self.b.setModule(self.m)
|
204
|
65
|
|
66 def testBuilder(self):
|
|
67 f = self.b.newFunction('test')
|
|
68 self.b.setFunction(f)
|
274
|
69 bb = self.b.newBlock()
|
|
70 self.b.emit(ir.Jump(bb))
|
|
71 self.b.setBlock(bb)
|
268
|
72 v1 = ir.Const(5)
|
|
73 v2 = ir.Const(7)
|
|
74 v3 = ir.Add(v1, v2)
|
274
|
75 self.b.emit(ir.Jump(f.epiloog))
|
279
|
76 self.cf.run(self.m)
|
237
|
77
|
|
78 def testAdd0(self):
|
|
79 f = self.b.newFunction('test')
|
|
80 self.b.setFunction(f)
|
268
|
81 self.b.setBlock(self.b.newBlock())
|
|
82 v1 = ir.Const(0)
|
309
|
83 v3 = ir.Add(v1, ir.Const(0))
|
|
84
|
|
85 class TestWriter(unittest.TestCase):
|
|
86 def testWrite(self):
|
|
87 writer = irutils.Writer()
|
|
88 module = ir.Module('mod1')
|
|
89 function = ir.Function('func1', module)
|
|
90 f = io.StringIO()
|
|
91 writer.write(module, f)
|
|
92 #print(f.getvalue())
|
|
93 f2 = io.StringIO(f.getvalue())
|
|
94 reader = irutils.Reader()
|
|
95 module2 = reader.read(f2)
|
|
96 f = io.StringIO()
|
|
97 writer.write(module2, f)
|
|
98 #print(f.getvalue())
|
204
|
99
|
171
|
100
|
309
|
101 class TestReader(unittest.TestCase):
|
|
102 def testAddExample(self):
|
|
103 reader = irutils.Reader()
|
|
104 with open('../examples/pi/add.pi') as f:
|
|
105 m = reader.read(f)
|
|
106 self.assertTrue(m)
|
|
107 #print(m)
|
|
108
|
171
|
109
|
|
110 if __name__ == '__main__':
|
292
|
111 unittest.main()
|
252
|
112 sys.exit()
|