annotate python/testir.py @ 177:460db5669efa

Added clean pass for IR
author Windel Bouwman
date Mon, 22 Apr 2013 23:54:54 +0200
parents 5fd02aa38b42
children 25a0753da4cf
rev   line source
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
1 import c3, ppci, ir, x86, transform
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
2 import os
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
3
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
4 testsrc = """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
5 package test2;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
6
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
7 function void tesssst(int henkie)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
8 {
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
9 var int a, b, cee;
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
10 a = 2 * 33 - 12;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
11 b = a * 2 + 13;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
12 a = b + a;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
13 cee = a;
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
14 if (a > b and b *3 - a+8*b== 3*6-b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
15 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
16 var int x = a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
17 x = b * 2 - a;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
18 a = x * x * add2(x, 22 - a);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
19 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
20 else
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
21 {
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
22 a = b + a + add2(a, b);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
23 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
24 var int y;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
25 y = a - b * 53;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
26 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
27
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
28 function int add2(int x, int y)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
29 {
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
30 var int res;
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
31 res = x + y + 2 - 7 + 2;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
32 //if (y < 2)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
33 //{
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
34 // return y - 33;
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
35 //}
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
36
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
37 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
38
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
39 if (x > 13)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
40 {
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
41 while (y > 1337)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
42 {
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
43 res = res + 2;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
44 y = y - 12;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
45 }
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
46 }
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
47 return res;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
48 }
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
49
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
50 """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
51
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
52 if __name__ == '__main__':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
53 diag = ppci.DiagnosticsManager()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
54 builder = c3.Builder(diag)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
55 cgenx86 = x86.X86CodeGen(diag)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
56 ir = builder.build(testsrc)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
57 diag.printErrors(testsrc)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
58 #ir.dump()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
59 cf = transform.ConstantFolder()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
60 dcd = transform.DeadCodeDeleter()
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
61 m2r = transform.Mem2RegPromotor()
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
62 clr = transform.CleanPass()
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
63 ir.check()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
64 cf.run(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
65 dcd.run(ir)
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
66 clr.run(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
67 m2r.run(ir)
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
68 for bb in ir.BasicBlocks:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
69 bb.dag = x86.Dag(bb)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
70 #ir.dump()
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
71
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
72 # Dump a graphiz file:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
73 with open('graaf.gv', 'w') as f:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
74 ir.dumpgv(f)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
75 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
76
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
77 asm = cgenx86.genBin(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
78 #for a in asm:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
79 # print(a)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
80 with open('out.asm', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
81 f.write('BITS 64\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
82 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
83 f.write(str(a) + '\n')