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