annotate python/testir.py @ 171:3eb9b9e2958d

Improved IR code
author Windel Bouwman
date Wed, 03 Apr 2013 22:20:20 +0200
parents
children 5a7d37d615ee
rev   line source
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
1 import c3, ppci, ir, x86
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
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
27 """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
28
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
29 if __name__ == '__main__':
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
30 diag = ppci.DiagnosticsManager()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
31 builder = c3.Builder(diag)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
32 cgenx86 = x86.X86CodeGen(diag)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
33 ir = builder.build(testsrc)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
34 ir.check()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
35 ir.analyze()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36 #ir.constantProp()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 ir.dump()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
38 asm = cgenx86.genBin(ir)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
40 print(a)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 with open('out.asm', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
42 f.write('BITS 64\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
43 for a in asm:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
44 f.write(str(a) + '\n')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
45
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
46 # Dump a graphiz file:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
47 with open('graaf.gv', 'w') as f:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
48 ir.dumpgv(f)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
49 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
50
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
51