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)
|
243
|
96 ir.dump()
|
|
97 ir.check()
|
173
|
98 cf = transform.ConstantFolder()
|
243
|
99 ir.check()
|
173
|
100 dcd = transform.DeadCodeDeleter()
|
243
|
101 ir.check()
|
175
|
102 m2r = transform.Mem2RegPromotor()
|
243
|
103 ir.check()
|
177
|
104 clr = transform.CleanPass()
|
171
|
105 ir.check()
|
173
|
106 cf.run(ir)
|
175
|
107 dcd.run(ir)
|
177
|
108 clr.run(ir)
|
175
|
109 m2r.run(ir)
|
|
110 #ir.dump()
|
176
|
111
|
|
112 # Dump a graphiz file:
|
|
113 with open('graaf.gv', 'w') as f:
|
|
114 ir.dumpgv(f)
|
|
115 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
|
|
116
|
171
|
117 asm = cgenx86.genBin(ir)
|
175
|
118 #for a in asm:
|
|
119 # print(a)
|
171
|
120 with open('out.asm', 'w') as f:
|
|
121 f.write('BITS 64\n')
|
|
122 for a in asm:
|
|
123 f.write(str(a) + '\n')
|
180
|
124 print(a)
|
|
125
|