301
|
1 import unittest
|
|
2 import os
|
252
|
3 import sys
|
256
|
4 import ppci
|
301
|
5 from ppci import ir
|
300
|
6 from ppci.transform import ConstantFolder
|
204
|
7
|
272
|
8
|
253
|
9 class IrCodeTestCase(unittest.TestCase):
|
|
10 def setUp(self):
|
|
11 self.b = ir.Builder()
|
|
12 self.m = ir.Module('test')
|
|
13 self.b.setModule(self.m)
|
|
14
|
|
15 def testBuilder(self):
|
|
16 f = self.b.newFunction('add')
|
|
17 self.b.setFunction(f)
|
268
|
18 bb = self.b.newBlock()
|
274
|
19 self.b.emit(ir.Jump(bb))
|
268
|
20 self.b.setBlock(bb)
|
272
|
21 self.b.emit(ir.Exp(ir.Const(0)))
|
274
|
22 self.b.emit(ir.Jump(f.epiloog))
|
253
|
23 self.m.check()
|
|
24 # Run interpreter:
|
268
|
25 # r = self.m.getFunction('add').call(1, 2)
|
253
|
26 #self.assertEqual(3, r)
|
|
27
|
272
|
28
|
277
|
29 class PatternMatchTestCase(unittest.TestCase):
|
|
30 def testSimpleTree(self):
|
|
31 t = ir.Term('x')
|
|
32 pat = ir.Binop(ir.Const(2), '+', t)
|
|
33 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
|
|
34 self.assertTrue(res)
|
|
35 self.assertIn(t, mp)
|
|
36 self.assertEqual(3, mp[t])
|
|
37
|
|
38 def testSimpleTree2(self):
|
|
39 t = ir.Term('x')
|
|
40 t2 = ir.Term('y')
|
|
41 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
|
|
42 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat)
|
|
43 self.assertTrue(res)
|
|
44 self.assertIn(t, mp)
|
|
45 self.assertEqual(2, mp[t])
|
|
46 self.assertIn(t2, mp)
|
|
47 self.assertEqual(3, mp[t2])
|
|
48 res, mp = ir.match_tree(ir.Const(2), pat)
|
|
49 self.assertFalse(res)
|
|
50
|
|
51
|
204
|
52 class ConstantFolderTestCase(unittest.TestCase):
|
|
53 def setUp(self):
|
|
54 self.b = ir.Builder()
|
300
|
55 self.cf = ConstantFolder()
|
237
|
56 self.m = ir.Module('test')
|
|
57 self.b.setModule(self.m)
|
204
|
58
|
|
59 def testBuilder(self):
|
|
60 f = self.b.newFunction('test')
|
|
61 self.b.setFunction(f)
|
274
|
62 bb = self.b.newBlock()
|
|
63 self.b.emit(ir.Jump(bb))
|
|
64 self.b.setBlock(bb)
|
268
|
65 v1 = ir.Const(5)
|
|
66 v2 = ir.Const(7)
|
|
67 v3 = ir.Add(v1, v2)
|
274
|
68 self.b.emit(ir.Jump(f.epiloog))
|
279
|
69 self.cf.run(self.m)
|
237
|
70
|
|
71 def testAdd0(self):
|
|
72 f = self.b.newFunction('test')
|
|
73 self.b.setFunction(f)
|
268
|
74 self.b.setBlock(self.b.newBlock())
|
|
75 v1 = ir.Const(0)
|
|
76 v2 = ir.Const(0)
|
|
77 v3 = ir.Add(v1, v2)
|
204
|
78
|
171
|
79
|
|
80 testsrc = """
|
|
81 package test2;
|
|
82
|
175
|
83 function void tesssst(int henkie)
|
171
|
84 {
|
175
|
85 var int a, b, cee;
|
171
|
86 a = 2 * 33 - 12;
|
|
87 b = a * 2 + 13;
|
|
88 a = b + a;
|
175
|
89 cee = a;
|
252
|
90 cee = cee * 2 + a + cee * 2;
|
|
91 if (cee + a > b and b *3 - a+8*b== 3*6-b)
|
171
|
92 {
|
|
93 var int x = a;
|
|
94 x = b * 2 - a;
|
252
|
95 a = x * x * (x + 22 - a);
|
171
|
96 }
|
|
97 else
|
|
98 {
|
252
|
99 a = b + a + (a + b);
|
171
|
100 }
|
|
101 var int y;
|
|
102 y = a - b * 53;
|
|
103 }
|
252
|
104 """
|
171
|
105
|
252
|
106 testsrc2 = """
|
172
|
107 function int add2(int x, int y)
|
|
108 {
|
|
109 var int res;
|
174
|
110 res = x + y + 2 - 7 + 2;
|
175
|
111 //if (y < 2)
|
|
112 //{
|
|
113 // return y - 33;
|
|
114 //}
|
|
115
|
177
|
116 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
|
|
117
|
174
|
118 if (x > 13)
|
|
119 {
|
176
|
120 while (y > 1337)
|
|
121 {
|
|
122 res = res + 2;
|
|
123 y = y - 12;
|
|
124 }
|
174
|
125 }
|
172
|
126 return res;
|
|
127 }
|
|
128
|
171
|
129 """
|
|
130
|
|
131 if __name__ == '__main__':
|
292
|
132 unittest.main()
|
252
|
133 sys.exit()
|