annotate python/testir.py @ 272:e64bae57cda8

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