annotate python/testir.py @ 253:74c6a20302d5

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