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