annotate python/testir.py @ 249:e41e4109addd

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