annotate python/testir.py @ 174:3eb06f5fb987

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