annotate python/testir.py @ 271:cf7d5fb7d9c8

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