301
|
1 import unittest
|
|
2 import os
|
252
|
3 import sys
|
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):
|
|
12 v = ir.Add(ir.Const(1), ir.Const(2))
|
|
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):
|
|
22 f = self.b.newFunction('add')
|
|
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 self.m.check()
|
|
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):
|
|
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
|
|
44 def testSimpleTree2(self):
|
|
45 t = ir.Term('x')
|
|
46 t2 = ir.Term('y')
|
|
47 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
|
|
48 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat)
|
|
49 self.assertTrue(res)
|
|
50 self.assertIn(t, mp)
|
|
51 self.assertEqual(2, mp[t])
|
|
52 self.assertIn(t2, mp)
|
|
53 self.assertEqual(3, mp[t2])
|
|
54 res, mp = ir.match_tree(ir.Const(2), pat)
|
|
55 self.assertFalse(res)
|
|
56
|
|
57
|
204
|
58 class ConstantFolderTestCase(unittest.TestCase):
|
|
59 def setUp(self):
|
307
|
60 self.b = irutils.Builder()
|
300
|
61 self.cf = ConstantFolder()
|
237
|
62 self.m = ir.Module('test')
|
|
63 self.b.setModule(self.m)
|
204
|
64
|
|
65 def testBuilder(self):
|
|
66 f = self.b.newFunction('test')
|
|
67 self.b.setFunction(f)
|
274
|
68 bb = self.b.newBlock()
|
|
69 self.b.emit(ir.Jump(bb))
|
|
70 self.b.setBlock(bb)
|
268
|
71 v1 = ir.Const(5)
|
|
72 v2 = ir.Const(7)
|
|
73 v3 = ir.Add(v1, v2)
|
274
|
74 self.b.emit(ir.Jump(f.epiloog))
|
279
|
75 self.cf.run(self.m)
|
237
|
76
|
|
77 def testAdd0(self):
|
|
78 f = self.b.newFunction('test')
|
|
79 self.b.setFunction(f)
|
268
|
80 self.b.setBlock(self.b.newBlock())
|
|
81 v1 = ir.Const(0)
|
|
82 v2 = ir.Const(0)
|
|
83 v3 = ir.Add(v1, v2)
|
204
|
84
|
171
|
85
|
|
86
|
|
87
|
|
88 if __name__ == '__main__':
|
292
|
89 unittest.main()
|
252
|
90 sys.exit()
|