annotate python/testir.py @ 173:c1d2b6b9f9a7

Rework into passes
author Windel Bouwman
date Fri, 19 Apr 2013 12:42:21 +0200
parents 5a7d37d615ee
children 3eb06f5fb987
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
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
7 function void tst()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
8 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
9 var int a, b;
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;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
13 if (a > b and b *3 - a+8*b== 3*6-b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
14 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
15 var int x = a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
16 x = b * 2 - a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
17 a = x * x;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
18 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
19 else
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
20 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
21 a = b + a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
22 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
23 var int y;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
24 y = a - b * 53;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
25 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
26
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
27 function int add2(int x, int y)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
28 {
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
29 var int res;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
30 res = x + y;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
31 return res;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
32 }
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
33
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
34 """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
35
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36 if __name__ == '__main__':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 diag = ppci.DiagnosticsManager()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
38 builder = c3.Builder(diag)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 cgenx86 = x86.X86CodeGen(diag)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
40 ir = builder.build(testsrc)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
41 diag.printErrors(testsrc)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
42 #ir.dump()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
43 cf = transform.ConstantFolder()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
44 dcd = transform.DeadCodeDeleter()
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45 ir.check()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
46 cf.run(ir)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
47 cf.run(ir)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
48 #dcd.run(ir)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
49 ir.dump()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
50 asm = cgenx86.genBin(ir)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
51 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
52 print(a)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
53 with open('out.asm', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
54 f.write('BITS 64\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
55 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
56 f.write(str(a) + '\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
57
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
58 # Dump a graphiz file:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
59 with open('graaf.gv', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
60 ir.dumpgv(f)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
61 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
62
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
63