annotate python/testir.py @ 175:a51b3c956386

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