annotate python/testir.py @ 180:25a0753da4cf

Re-organized files
author Windel Bouwman
date Thu, 09 May 2013 17:35:19 +0200
parents 460db5669efa
children de3a68f677a5
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)
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
55 cgenx86 = x86.X86CodeGenSimple(diag)
171
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)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
68 #ir.dump()
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
69
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
70 # Dump a graphiz file:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
71 with open('graaf.gv', 'w') as f:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
72 ir.dumpgv(f)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
73 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
74
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
75 asm = cgenx86.genBin(ir)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
76 #for a in asm:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
77 # print(a)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
78 with open('out.asm', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
79 f.write('BITS 64\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
80 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
81 f.write(str(a) + '\n')
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
82 print(a)
25a0753da4cf Re-organized files
Windel Bouwman
parents: 177
diff changeset
83