annotate python/testir.py @ 211:99164160fb0b

Added another missing file
author Windel Bouwman
date Sat, 29 Jun 2013 10:10:45 +0200
parents de3a68f677a5
children 81752b0f85a5
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()
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
8
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
9 def testBuilder(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
10 m = ir.Module('test')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
11 self.b.setModule(m)
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))
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
22 self.assertEqual(3, len(m.Instructions))
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
23 self.cf.run(m)
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
24 self.assertEqual(3, len(m.Instructions))
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
25 self.assertIsInstance(m.Instructions[-1], ir.ImmLoad)
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
26 self.assertEqual(12, m.Instructions[-1].value)
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
27
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
28
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
29 testsrc = """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
30 package test2;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
31
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
32 function void tesssst(int henkie)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
33 {
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
34 var int a, b, cee;
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
35 a = 2 * 33 - 12;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36 b = a * 2 + 13;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 a = b + a;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
38 cee = a;
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 if (a > b and b *3 - a+8*b== 3*6-b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
40 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 var int x = a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
42 x = b * 2 - a;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
43 a = x * x * add2(x, 22 - a);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
44 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45 else
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
46 {
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
47 a = b + a + add2(a, b);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
48 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
49 var int y;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
50 y = a - b * 53;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
51 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
52
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
53 function int add2(int x, int y)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
54 {
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
55 var int res;
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
56 res = x + y + 2 - 7 + 2;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
57 //if (y < 2)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
58 //{
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
59 // return y - 33;
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
60 //}
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
61
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
62 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
63
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
64 if (x > 13)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
65 {
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
66 while (y > 1337)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
67 {
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
68 res = res + 2;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
69 y = y - 12;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
70 }
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
71 }
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
72 return res;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
73 }
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
74
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
75 """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
76
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
77 if __name__ == '__main__':
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
78 unittest.main()
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
79 sys.exit()
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
80 diag = ppci.DiagnosticsManager()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
81 builder = c3.Builder(diag)
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
82 cgenx86 = x86.X86CodeGenSimple(diag)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
83 ir = builder.build(testsrc)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
84 diag.printErrors(testsrc)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
85 #ir.dump()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
86 cf = transform.ConstantFolder()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
87 dcd = transform.DeadCodeDeleter()
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
88 m2r = transform.Mem2RegPromotor()
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
89 clr = transform.CleanPass()
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
90 ir.check()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
91 cf.run(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
92 dcd.run(ir)
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
93 clr.run(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
94 m2r.run(ir)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
95 #ir.dump()
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
96
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
97 # Dump a graphiz file:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
98 with open('graaf.gv', 'w') as f:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
99 ir.dumpgv(f)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
100 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
101
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
102 asm = cgenx86.genBin(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
103 #for a in asm:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
104 # print(a)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
105 with open('out.asm', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
106 f.write('BITS 64\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
107 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
108 f.write(str(a) + '\n')
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
109 print(a)
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
110