annotate python/testir.py @ 260:b2f94b4951f1

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