annotate python/tcodegen.py @ 274:ea93e0a7a31e

Move docs
author Windel Bouwman
date Wed, 04 Sep 2013 17:35:06 +0200
parents e64bae57cda8
children 6f2423df0675
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
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
10 import ir
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 testsrc = """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13 package test2;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
14
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
15 var int phaa, foo, bar;
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
16
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
17 function int insanemath(int a, int b)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
18 {
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
19 var int c;
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
20 c = 0;
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
21 var int i;
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
22 i = 9;
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
23 while (i > 1)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
24 {
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
25 c = a + b + 1 + c;
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
26 i = i - 1;
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
27 if (c > 90)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
28 {
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
29 return 42;
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
30 }
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
31 }
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
32 return c;
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
33 }
e64bae57cda8 refactor ir
Windel Bouwman
parents: 269
diff changeset
34
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35 function void tesssst(int henkie)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
36 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 var int a, b, cee;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 a = 2 * 33 - 12;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
39 b = a * 2;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 a = b + a;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 cee = a;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42 cee = cee * 2 + cee;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43 if (cee + a > b and b - a+b== 3*6-b)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45 var int x = a;
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
46 x = b - a + insanemath(3, 4) - insanemath(33,2);
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 a = x * (x + a);
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 else
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 a = b + (a + b);
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53 var int y;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
54 y = a - b * 53;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
55 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
56 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
57
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
58 if __name__ == '__main__':
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
59 diag = ppci.DiagnosticsManager()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
60 builder = c3.Builder(diag)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
61 irc = builder.build(testsrc)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
62 if not irc:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
63 diag.printErrors(testsrc)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
64 irc.check()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
65 irc.dump()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
66 with open('ir.gv', 'w') as f:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
67 ir.dumpgv(irc, f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
68 outs = outstream.TextOutputStream()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
69 cga = codegenarm.ArmCodeGenerator(outs)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
70 ir2 = cga.generate(irc)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
71
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
72 with open('cfg.gv', 'w') as cfg_file:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
73 print('digraph G {', file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
74 #print('edge [constraint=none]', file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
75 print('rankdir=TB', file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
76 for f in cga.frames:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
77 print('subgraph cluster_{} {{'.format(f.name), file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
78 print('label={};'.format(f.name), file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
79 print('color=lightgrey;', file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
80 print('style=filled;', file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
81 f.cfg.to_dot(cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
82 print('}', file=cfg_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
83 print('}', file=cfg_file)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
84
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
85 with open('ig.gv', 'w') as ig_file:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
86 print('digraph G {', file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
87 print('edge [arrowhead=none]', file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
88 for f in cga.frames:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
89 print('subgraph cluster_{} {{'.format(f.name), file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
90 print('label={};'.format(f.name), file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
91 print('color=lightgrey;', file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
92 print('style=filled;', file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
93 f.ig.to_dot(ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
94 print('}', file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
95 print('}', file=ig_file)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
96
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
97 for f in ir2:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
98 print(f)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
99 for i in f.instructions:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
100 print(' {}'.format(i))
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
101