Mercurial > lcfOS
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 | 1 |
2 """ | |
3 Test individual parts of the code generation for arm using the c3 frontend. | |
4 """ | |
5 | |
6 import c3 | |
7 import ppci | |
8 import codegenarm | |
9 import outstream | |
274 | 10 import ir |
269 | 11 |
12 testsrc = """ | |
13 package test2; | |
14 | |
272 | 15 var int phaa, foo, bar; |
16 | |
17 function int insanemath(int a, int b) | |
18 { | |
19 var int c; | |
274 | 20 c = 0; |
21 var int i; | |
22 i = 9; | |
23 while (i > 1) | |
24 { | |
25 c = a + b + 1 + c; | |
26 i = i - 1; | |
27 if (c > 90) | |
28 { | |
29 return 42; | |
30 } | |
31 } | |
272 | 32 return c; |
33 } | |
34 | |
269 | 35 function void tesssst(int henkie) |
36 { | |
37 var int a, b, cee; | |
38 a = 2 * 33 - 12; | |
39 b = a * 2; | |
40 a = b + a; | |
41 cee = a; | |
42 cee = cee * 2 + cee; | |
275 | 43 insanemath(2, 3); |
44 if (cee + a > b and b - a+b== 3*6-insanemath(b, 2)) | |
269 | 45 { |
46 var int x = a; | |
275 | 47 x = b - a + insanemath(3, 4) - insanemath(33, insanemath(2, 0)); |
269 | 48 a = x * (x + a); |
49 } | |
50 else | |
51 { | |
52 a = b + (a + b); | |
53 } | |
54 var int y; | |
55 y = a - b * 53; | |
56 } | |
57 """ | |
58 | |
275 | 59 testsrc = """ |
60 package test3; | |
61 | |
62 function int ab(int a, int b) | |
63 { | |
64 var int c; | |
65 c = 0; | |
66 c = c + a + b; | |
67 return c; | |
68 } | |
69 | |
70 function void tesssst() | |
71 { | |
72 var int a, b; | |
73 a = 2; | |
74 b = 3; | |
75 ab(ab(a, b) + ab(9,b), 0); | |
76 } | |
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 | 91 def dump_cfg(cga, cfg_file): |
92 print('digraph G {', file=cfg_file) | |
93 #print('edge [constraint=none]', file=cfg_file) | |
94 print('rankdir=TB', file=cfg_file) | |
95 for f in cga.frames: | |
96 print('subgraph cluster_{} {{'.format(f.name), file=cfg_file) | |
97 print('label={};'.format(f.name), file=cfg_file) | |
98 print('color=lightgrey;', file=cfg_file) | |
99 print('style=filled;', file=cfg_file) | |
100 f.cfg.to_dot(cfg_file) | |
101 print('}', file=cfg_file) | |
102 print('}', file=cfg_file) | |
103 | |
104 def dump_ig(cga, ig_file): | |
105 print('digraph G {', file=ig_file) | |
106 print('edge [arrowhead=none]', file=ig_file) | |
107 for f in cga.frames: | |
108 print('subgraph cluster_{} {{'.format(f.name), file=ig_file) | |
109 print('label={};'.format(f.name), file=ig_file) | |
110 print('color=lightgrey;', file=ig_file) | |
111 print('style=filled;', file=ig_file) | |
112 f.ig.to_dot(ig_file) | |
113 print('}', file=ig_file) | |
114 print('}', file=ig_file) | |
115 | |
269 | 116 if __name__ == '__main__': |
117 diag = ppci.DiagnosticsManager() | |
118 builder = c3.Builder(diag) | |
274 | 119 irc = builder.build(testsrc) |
120 if not irc: | |
121 diag.printErrors(testsrc) | |
122 irc.check() | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
123 #irc.dump() |
274 | 124 with open('ir.gv', 'w') as f: |
125 ir.dumpgv(irc, f) | |
269 | 126 outs = outstream.TextOutputStream() |
127 cga = codegenarm.ArmCodeGenerator(outs) | |
274 | 128 ir2 = cga.generate(irc) |
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 | 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 | 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 | 140 |
275 | 141 outs.dump() |
142 |