204
|
1 import unittest, os
|
173
|
2 import c3, ppci, ir, x86, transform
|
204
|
3
|
|
4 class ConstantFolderTestCase(unittest.TestCase):
|
|
5 def setUp(self):
|
|
6 self.b = ir.Builder()
|
|
7 self.cf = transform.ConstantFolder()
|
237
|
8 self.m = ir.Module('test')
|
|
9 self.b.setModule(self.m)
|
204
|
10
|
|
11 def testBuilder(self):
|
|
12 f = self.b.newFunction('test')
|
|
13 self.b.setFunction(f)
|
|
14 bb = self.b.newBB()
|
|
15 self.b.setBB(bb)
|
|
16 v1 = self.b.newTmp('t')
|
|
17 v2 = self.b.newTmp('t')
|
|
18 v3 = self.b.newTmp('t')
|
|
19 self.b.addIns(ir.ImmLoad(v1, 5))
|
|
20 self.b.addIns(ir.ImmLoad(v2, 7))
|
|
21 self.b.addIns(ir.BinaryOperator(v3, '+', v1, v2))
|
237
|
22 self.assertEqual(3, len(self.m.Instructions))
|
|
23 self.cf.run(self.m)
|
|
24 self.assertEqual(3, len(self.m.Instructions))
|
|
25 self.assertIsInstance(self.m.Instructions[-1], ir.ImmLoad)
|
|
26 self.assertEqual(12, self.m.Instructions[-1].value)
|
|
27
|
|
28 def testAdd0(self):
|
|
29 f = self.b.newFunction('test')
|
|
30 self.b.setFunction(f)
|
|
31 bb = self.b.newBB()
|
|
32 self.b.setBB(bb)
|
|
33 v1 = self.b.newTmp('t')
|
|
34 v2 = self.b.newTmp('t')
|
|
35 v3 = self.b.newTmp('t')
|
|
36 self.b.addIns(ir.ImmLoad(v2, 0))
|
|
37 self.b.addIns(ir.BinaryOperator(v3, '+', v1, v2))
|
204
|
38
|
171
|
39
|
|
40 testsrc = """
|
|
41 package test2;
|
|
42
|
175
|
43 function void tesssst(int henkie)
|
171
|
44 {
|
175
|
45 var int a, b, cee;
|
171
|
46 a = 2 * 33 - 12;
|
|
47 b = a * 2 + 13;
|
|
48 a = b + a;
|
175
|
49 cee = a;
|
171
|
50 if (a > b and b *3 - a+8*b== 3*6-b)
|
|
51 {
|
|
52 var int x = a;
|
|
53 x = b * 2 - a;
|
175
|
54 a = x * x * add2(x, 22 - a);
|
171
|
55 }
|
|
56 else
|
|
57 {
|
175
|
58 a = b + a + add2(a, b);
|
171
|
59 }
|
|
60 var int y;
|
|
61 y = a - b * 53;
|
|
62 }
|
|
63
|
172
|
64 function int add2(int x, int y)
|
|
65 {
|
|
66 var int res;
|
174
|
67 res = x + y + 2 - 7 + 2;
|
175
|
68 //if (y < 2)
|
|
69 //{
|
|
70 // return y - 33;
|
|
71 //}
|
|
72
|
177
|
73 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
|
|
74
|
174
|
75 if (x > 13)
|
|
76 {
|
176
|
77 while (y > 1337)
|
|
78 {
|
|
79 res = res + 2;
|
|
80 y = y - 12;
|
|
81 }
|
174
|
82 }
|
172
|
83 return res;
|
|
84 }
|
|
85
|
171
|
86 """
|
|
87
|
|
88 if __name__ == '__main__':
|
237
|
89 #unittest.main()
|
|
90 #sys.exit()
|
171
|
91 diag = ppci.DiagnosticsManager()
|
|
92 builder = c3.Builder(diag)
|
180
|
93 cgenx86 = x86.X86CodeGenSimple(diag)
|
171
|
94 ir = builder.build(testsrc)
|
172
|
95 diag.printErrors(testsrc)
|
173
|
96 #ir.dump()
|
|
97 cf = transform.ConstantFolder()
|
|
98 dcd = transform.DeadCodeDeleter()
|
175
|
99 m2r = transform.Mem2RegPromotor()
|
177
|
100 clr = transform.CleanPass()
|
171
|
101 ir.check()
|
173
|
102 cf.run(ir)
|
175
|
103 dcd.run(ir)
|
177
|
104 clr.run(ir)
|
175
|
105 m2r.run(ir)
|
|
106 #ir.dump()
|
176
|
107
|
|
108 # Dump a graphiz file:
|
|
109 with open('graaf.gv', 'w') as f:
|
|
110 ir.dumpgv(f)
|
|
111 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
|
|
112
|
171
|
113 asm = cgenx86.genBin(ir)
|
175
|
114 #for a in asm:
|
|
115 # print(a)
|
171
|
116 with open('out.asm', 'w') as f:
|
|
117 f.write('BITS 64\n')
|
|
118 for a in asm:
|
|
119 f.write(str(a) + '\n')
|
180
|
120 print(a)
|
|
121
|