annotate python/tcodegen.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents 5f8c04a8d26b
children ea93e0a7a31e
rev   line source
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
1
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
3 Test individual parts of the code generation for arm using the c3 frontend.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
4 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
5
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
6 import c3
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
7 import ppci
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
8 import codegenarm
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
9 import outstream
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
10
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11 testsrc = """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 package test2;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
14 var int phaa, foo, bar;
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
15
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
16 function int insanemath(int a, int b)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
17 {
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
18 var int c;
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
19 c = a + b + 1;
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
20 return c;
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
21 }
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
22
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23 function void tesssst(int henkie)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 var int a, b, cee;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
26 a = 2 * 33 - 12;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 b = a * 2;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
28 a = b + a;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
29 cee = a;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
30 cee = cee * 2 + cee;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
31 if (cee + a > b and b - a+b== 3*6-b)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
32 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
33 var int x = a;
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
34 x = b - a + insanemath(3, 4);
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35 a = x * (x + a);
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
36 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 else
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
39 a = b + (a + b);
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 var int y;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42 y = a - b * 53;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46 if __name__ == '__main__':
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 diag = ppci.DiagnosticsManager()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48 builder = c3.Builder(diag)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 ir = builder.build(testsrc)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50 ir.dump()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 outs = outstream.TextOutputStream()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 cga = codegenarm.ArmCodeGenerator(outs)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53 cfg_file = open('cfg.gv', 'w')
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
54 ig_file = open('ig.gv', 'w')
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
55 ir2 = cga.generate(ir, cfg_file=cfg_file, ig_file=ig_file)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
56 cfg_file.close()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
57 ig_file.close()
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
58 for i in ir2:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
59 print(i)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
60