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