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 # Run interpreter:
|
268
|
31 # r = self.m.getFunction('add').call(1, 2)
|
253
|
32 #self.assertEqual(3, r)
|
|
33
|
272
|
34
|
277
|
35 class PatternMatchTestCase(unittest.TestCase):
|
317
|
36 @unittest.skip('Not yet implemented')
|
277
|
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
|
317
|
45 @unittest.skip('Not yet implemented')
|
277
|
46 def testSimpleTree2(self):
|
|
47 t = ir.Term('x')
|
|
48 t2 = ir.Term('y')
|
|
49 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
|
|
50 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat)
|
|
51 self.assertTrue(res)
|
|
52 self.assertIn(t, mp)
|
|
53 self.assertEqual(2, mp[t])
|
|
54 self.assertIn(t2, mp)
|
|
55 self.assertEqual(3, mp[t2])
|
|
56 res, mp = ir.match_tree(ir.Const(2), pat)
|
|
57 self.assertFalse(res)
|
|
58
|
|
59
|
204
|
60 class ConstantFolderTestCase(unittest.TestCase):
|
|
61 def setUp(self):
|
307
|
62 self.b = irutils.Builder()
|
300
|
63 self.cf = ConstantFolder()
|
237
|
64 self.m = ir.Module('test')
|
|
65 self.b.setModule(self.m)
|
204
|
66
|
|
67 def testBuilder(self):
|
|
68 f = self.b.newFunction('test')
|
|
69 self.b.setFunction(f)
|
274
|
70 bb = self.b.newBlock()
|
|
71 self.b.emit(ir.Jump(bb))
|
|
72 self.b.setBlock(bb)
|
268
|
73 v1 = ir.Const(5)
|
|
74 v2 = ir.Const(7)
|
|
75 v3 = ir.Add(v1, v2)
|
274
|
76 self.b.emit(ir.Jump(f.epiloog))
|
279
|
77 self.cf.run(self.m)
|
237
|
78
|
|
79 def testAdd0(self):
|
|
80 f = self.b.newFunction('test')
|
|
81 self.b.setFunction(f)
|
268
|
82 self.b.setBlock(self.b.newBlock())
|
|
83 v1 = ir.Const(0)
|
309
|
84 v3 = ir.Add(v1, ir.Const(0))
|
|
85
|
342
|
86
|
309
|
87 class TestWriter(unittest.TestCase):
|
|
88 def testWrite(self):
|
|
89 writer = irutils.Writer()
|
|
90 module = ir.Module('mod1')
|
|
91 function = ir.Function('func1', module)
|
|
92 f = io.StringIO()
|
|
93 writer.write(module, f)
|
|
94 #print(f.getvalue())
|
|
95 f2 = io.StringIO(f.getvalue())
|
|
96 reader = irutils.Reader()
|
|
97 module2 = reader.read(f2)
|
|
98 f = io.StringIO()
|
|
99 writer.write(module2, f)
|
|
100 #print(f.getvalue())
|
204
|
101
|
171
|
102
|
309
|
103 class TestReader(unittest.TestCase):
|
|
104 def testAddExample(self):
|
|
105 reader = irutils.Reader()
|
|
106 with open('../examples/pi/add.pi') as f:
|
|
107 m = reader.read(f)
|
|
108 self.assertTrue(m)
|
|
109 #print(m)
|
342
|
110
|
171
|
111
|
|
112 if __name__ == '__main__':
|
292
|
113 unittest.main()
|
252
|
114 sys.exit()
|