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)
|
|
73 v2 = ir.Const(7)
|
394
|
74 v3 = ir.Add(v1, v2, "add", ir.i32)
|
274
|
75 self.b.emit(ir.Jump(f.epiloog))
|
279
|
76 self.cf.run(self.m)
|
237
|
77
|
|
78 def testAdd0(self):
|
394
|
79 f = self.b.new_function('test')
|
237
|
80 self.b.setFunction(f)
|
268
|
81 self.b.setBlock(self.b.newBlock())
|
394
|
82 v1 = ir.Const(12)
|
|
83 v3 = ir.Add(v1, ir.Const(0), "add", ir.i32)
|
309
|
84
|
342
|
85
|
309
|
86 class TestWriter(unittest.TestCase):
|
|
87 def testWrite(self):
|
|
88 writer = irutils.Writer()
|
|
89 module = ir.Module('mod1')
|
|
90 function = ir.Function('func1', module)
|
|
91 f = io.StringIO()
|
|
92 writer.write(module, f)
|
|
93 #print(f.getvalue())
|
|
94 f2 = io.StringIO(f.getvalue())
|
|
95 reader = irutils.Reader()
|
|
96 module2 = reader.read(f2)
|
|
97 f = io.StringIO()
|
|
98 writer.write(module2, f)
|
|
99 #print(f.getvalue())
|
204
|
100
|
171
|
101
|
309
|
102 class TestReader(unittest.TestCase):
|
|
103 def testAddExample(self):
|
|
104 reader = irutils.Reader()
|
|
105 with open('../examples/pi/add.pi') as f:
|
|
106 m = reader.read(f)
|
|
107 self.assertTrue(m)
|
|
108 #print(m)
|
342
|
109
|
171
|
110
|
|
111 if __name__ == '__main__':
|
292
|
112 unittest.main()
|
252
|
113 sys.exit()
|