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