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