annotate python/tcodegen.py @ 278:9fca39eebe50

First implementation of regalloc with coalsesc
author Windel Bouwman
date Sun, 29 Sep 2013 14:08:15 +0200
parents 6f2423df0675
children 2ccd57b1d78c
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;
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
43 insanemath(2, 3);
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
44 if (cee + a > b and b - a+b== 3*6-insanemath(b, 2))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46 var int x = a;
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
47 x = b - a + insanemath(3, 4) - insanemath(33, insanemath(2, 0));
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48 a = x * (x + a);
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50 else
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 {
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 a = b + (a + b);
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53 }
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
54 var int y;
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
55 y = a - b * 53;
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
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
59 testsrc = """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
60 package test3;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
61
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
62 function int ab(int a, int b)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
63 {
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
64 var int c;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
65 c = 0;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
66 c = c + a + b;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
67 return c;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
68 }
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
69
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
70 function void tesssst()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
71 {
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
72 var int a, b;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
73 a = 2;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
74 b = 3;
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
75 ab(ab(a, b) + ab(9,b), 0);
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
76 }
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
77 """
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
78
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
79 testsrc = """
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
80 package test3;
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
81
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
82 function int ab(int a, int b)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
83 {
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
84 var int c;
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
85 c = 0;
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
86 c = c + a + b;
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
87 return c;
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
88 }
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
89
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
90 """
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
91 def dump_cfg(cga, cfg_file):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
92 print('digraph G {', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
93 #print('edge [constraint=none]', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
94 print('rankdir=TB', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
95 for f in cga.frames:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
96 print('subgraph cluster_{} {{'.format(f.name), file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
97 print('label={};'.format(f.name), file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
98 print('color=lightgrey;', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
99 print('style=filled;', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
100 f.cfg.to_dot(cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
101 print('}', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
102 print('}', file=cfg_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
103
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
104 def dump_ig(cga, ig_file):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
105 print('digraph G {', file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
106 print('edge [arrowhead=none]', file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
107 for f in cga.frames:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
108 print('subgraph cluster_{} {{'.format(f.name), file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
109 print('label={};'.format(f.name), file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
110 print('color=lightgrey;', file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
111 print('style=filled;', file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
112 f.ig.to_dot(ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
113 print('}', file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
114 print('}', file=ig_file)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
115
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
116 if __name__ == '__main__':
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
117 diag = ppci.DiagnosticsManager()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
118 builder = c3.Builder(diag)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
119 irc = builder.build(testsrc)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
120 if not irc:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
121 diag.printErrors(testsrc)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
122 irc.check()
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
123 #irc.dump()
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
124 with open('ir.gv', 'w') as f:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
125 ir.dumpgv(irc, f)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
126 outs = outstream.TextOutputStream()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
127 cga = codegenarm.ArmCodeGenerator(outs)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
128 ir2 = cga.generate(irc)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
129
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
130 #with open('cfg.gv', 'w') as cfg_file:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
131 # dump_cfg(cga, cfg_file)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
132
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
133 #with open('ig.gv', 'w') as ig_file:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
134 # dump_ig(cga, ig_file)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
135
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
136 #for f in ir2:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
137 # print(f)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
138 # for i in f.instructions:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 275
diff changeset
139 # print(' {}'.format(i))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
140
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
141 outs.dump()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
142