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