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