annotate python/testir.py @ 252:c4370696ccc7

added optimize function
author Windel Bouwman
date Tue, 30 Jul 2013 17:57:46 +0200
parents ef683881c64e
children 74c6a20302d5
rev   line source
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
1 import unittest, os
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
2 import sys
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
3 import c3, ppci, ir, x86, transform
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
4
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
5 class ConstantFolderTestCase(unittest.TestCase):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
6 def setUp(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
7 self.b = ir.Builder()
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
8 self.cf = transform.ConstantFolder()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
9 self.m = ir.Module('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
10 self.b.setModule(self.m)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
11
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
12 def testBuilder(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
13 f = self.b.newFunction('test')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
14 self.b.setFunction(f)
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
15 bb = self.b.newBB()
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
16 self.b.setBB(bb)
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
17 v1 = self.b.newTmp('t')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
18 v2 = self.b.newTmp('t')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
19 v3 = self.b.newTmp('t')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
20 self.b.addIns(ir.ImmLoad(v1, 5))
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
21 self.b.addIns(ir.ImmLoad(v2, 7))
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
22 self.b.addIns(ir.BinaryOperator(v3, '+', v1, v2))
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
23 self.assertEqual(3, len(self.m.Instructions))
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
24 self.cf.run(self.m)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
25 self.assertEqual(3, len(self.m.Instructions))
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
26 self.assertIsInstance(self.m.Instructions[-1], ir.ImmLoad)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
27 self.assertEqual(12, self.m.Instructions[-1].value)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
28
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
29 def testAdd0(self):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
30 f = self.b.newFunction('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
31 self.b.setFunction(f)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
32 bb = self.b.newBB()
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
33 self.b.setBB(bb)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
34 v1 = self.b.newTmp('t')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
35 v2 = self.b.newTmp('t')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
36 v3 = self.b.newTmp('t')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
37 self.b.addIns(ir.ImmLoad(v2, 0))
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
38 self.b.addIns(ir.BinaryOperator(v3, '+', v1, v2))
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
39
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
40
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 testsrc = """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
42 package test2;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
43
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
44 function void tesssst(int henkie)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45 {
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
46 var int a, b, cee;
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
47 a = 2 * 33 - 12;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
48 b = a * 2 + 13;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
49 a = b + a;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
50 cee = a;
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
51 cee = cee * 2 + a + cee * 2;
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
52 if (cee + a > b and b *3 - a+8*b== 3*6-b)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
53 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
54 var int x = a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
55 x = b * 2 - a;
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
56 a = x * x * (x + 22 - a);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
57 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
58 else
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
59 {
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
60 a = b + a + (a + b);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
61 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
62 var int y;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
63 y = a - b * 53;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
64 }
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
65 """
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
66
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
67 testsrc2 = """
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
68 function int add2(int x, int y)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
69 {
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
70 var int res;
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
71 res = x + y + 2 - 7 + 2;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
72 //if (y < 2)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
73 //{
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
74 // return y - 33;
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
75 //}
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
76
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
77 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
78
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
79 if (x > 13)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
80 {
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
81 while (y > 1337)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
82 {
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
83 res = res + 2;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
84 y = y - 12;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
85 }
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
86 }
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
87 return res;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
88 }
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
89
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
90 """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
91
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
92 if __name__ == '__main__':
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
93 #unittest.main()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
94 #sys.exit()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
95 diag = ppci.DiagnosticsManager()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
96 builder = c3.Builder(diag)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
97 cgenx86 = x86.X86CodeGenSimple(diag)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
98 ir = builder.build(testsrc)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
99 diag.printErrors(testsrc)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
100 ir.check()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
101 ir.dump()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
102 transform.optimize(ir)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
103 print('dump IR')
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
104 print('dump IR')
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
105 print('dump IR')
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
106 print('dump IR')
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
107 ir.dump()
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
108
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
109 # Dump a graphiz file:
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
110 with open('graaf.gv', 'w') as f:
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
111 ir.dumpgv(f)
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
112 os.system('dot -Tsvg -ograaf.svg graaf.gv')
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
113
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
114 sys.exit()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
115 asm = cgenx86.genBin(ir)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
116 #for a in asm:
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
117 # print(a)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
118 with open('out.asm', 'w') as f:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
119 f.write('BITS 64\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
120 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
121 f.write(str(a) + '\n')
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
122 print(a)
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
123