301
|
1 import unittest
|
252
|
2 import sys
|
309
|
3 import io
|
256
|
4 import ppci
|
301
|
5 from ppci import ir
|
307
|
6 from ppci import irutils
|
300
|
7 from ppci.transform import ConstantFolder
|
204
|
8
|
272
|
9
|
253
|
10 class IrCodeTestCase(unittest.TestCase):
|
307
|
11 def testAdd(self):
|
394
|
12 v = ir.Add(ir.Const(1), ir.Const(2), "add", ir.i32)
|
307
|
13
|
|
14
|
|
15 class IrBuilderTestCase(unittest.TestCase):
|
253
|
16 def setUp(self):
|
307
|
17 self.b = irutils.Builder()
|
253
|
18 self.m = ir.Module('test')
|
|
19 self.b.setModule(self.m)
|
|
20
|
|
21 def testBuilder(self):
|
394
|
22 f = self.b.new_function('add')
|
253
|
23 self.b.setFunction(f)
|
268
|
24 bb = self.b.newBlock()
|
274
|
25 self.b.emit(ir.Jump(bb))
|
268
|
26 self.b.setBlock(bb)
|
272
|
27 self.b.emit(ir.Exp(ir.Const(0)))
|
274
|
28 self.b.emit(ir.Jump(f.epiloog))
|
253
|
29 # Run interpreter:
|
268
|
30 # r = self.m.getFunction('add').call(1, 2)
|
253
|
31 #self.assertEqual(3, r)
|
|
32
|
272
|
33
|
277
|
34 class PatternMatchTestCase(unittest.TestCase):
|
317
|
35 @unittest.skip('Not yet implemented')
|
277
|
36 def testSimpleTree(self):
|
|
37 t = ir.Term('x')
|
|
38 pat = ir.Binop(ir.Const(2), '+', t)
|
|
39 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
|
|
40 self.assertTrue(res)
|
|
41 self.assertIn(t, mp)
|
|
42 self.assertEqual(3, mp[t])
|
|
43
|
317
|
44 @unittest.skip('Not yet implemented')
|
277
|
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):
|
394
|
67 f = self.b.new_function('test')
|
204
|
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)
|
397
|
73 self.b.emit(v1)
|
268
|
74 v2 = ir.Const(7)
|
394
|
75 v3 = ir.Add(v1, v2, "add", ir.i32)
|
397
|
76 self.b.emit(v3)
|
274
|
77 self.b.emit(ir.Jump(f.epiloog))
|
279
|
78 self.cf.run(self.m)
|
237
|
79
|
|
80 def testAdd0(self):
|
394
|
81 f = self.b.new_function('test')
|
237
|
82 self.b.setFunction(f)
|
268
|
83 self.b.setBlock(self.b.newBlock())
|
394
|
84 v1 = ir.Const(12)
|
|
85 v3 = ir.Add(v1, ir.Const(0), "add", ir.i32)
|
309
|
86
|
342
|
87
|
309
|
88 class TestWriter(unittest.TestCase):
|
|
89 def testWrite(self):
|
|
90 writer = irutils.Writer()
|
|
91 module = ir.Module('mod1')
|
|
92 function = ir.Function('func1', module)
|
|
93 f = io.StringIO()
|
|
94 writer.write(module, f)
|
|
95 #print(f.getvalue())
|
|
96 f2 = io.StringIO(f.getvalue())
|
|
97 reader = irutils.Reader()
|
|
98 module2 = reader.read(f2)
|
|
99 f = io.StringIO()
|
|
100 writer.write(module2, f)
|
|
101 #print(f.getvalue())
|
204
|
102
|
171
|
103
|
309
|
104 class TestReader(unittest.TestCase):
|
|
105 def testAddExample(self):
|
|
106 reader = irutils.Reader()
|
|
107 with open('../examples/pi/add.pi') as f:
|
|
108 m = reader.read(f)
|
|
109 self.assertTrue(m)
|
|
110 #print(m)
|
342
|
111
|
171
|
112
|
|
113 if __name__ == '__main__':
|
292
|
114 unittest.main()
|
252
|
115 sys.exit()
|