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