Mercurial > lcfOS
annotate test/tcodegen.py @ 394:988f3fb861e4
c3 code generator rewrite
author | Windel Bouwman |
---|---|
date | Thu, 22 May 2014 08:14:12 +0200 |
parents | 7b38782ed496 |
children |
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; |
279 | 85 var int *a2; |
86 a2 = cast<int*>(2); | |
87 *a2 = 2; | |
88 *a2 = *a2 + 2; | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
89 c = 0; |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
90 c = c + a + b; |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
91 return c; |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
92 } |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
93 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
94 """ |
275 | 95 def dump_cfg(cga, cfg_file): |
96 print('digraph G {', file=cfg_file) | |
97 #print('edge [constraint=none]', file=cfg_file) | |
98 print('rankdir=TB', file=cfg_file) | |
99 for f in cga.frames: | |
100 print('subgraph cluster_{} {{'.format(f.name), file=cfg_file) | |
101 print('label={};'.format(f.name), file=cfg_file) | |
102 print('color=lightgrey;', file=cfg_file) | |
103 print('style=filled;', file=cfg_file) | |
104 f.cfg.to_dot(cfg_file) | |
105 print('}', file=cfg_file) | |
106 print('}', file=cfg_file) | |
107 | |
108 def dump_ig(cga, ig_file): | |
109 print('digraph G {', file=ig_file) | |
110 print('edge [arrowhead=none]', file=ig_file) | |
111 for f in cga.frames: | |
112 print('subgraph cluster_{} {{'.format(f.name), file=ig_file) | |
113 print('label={};'.format(f.name), file=ig_file) | |
114 print('color=lightgrey;', file=ig_file) | |
115 print('style=filled;', file=ig_file) | |
116 f.ig.to_dot(ig_file) | |
117 print('}', file=ig_file) | |
118 print('}', file=ig_file) | |
119 | |
269 | 120 if __name__ == '__main__': |
121 diag = ppci.DiagnosticsManager() | |
122 builder = c3.Builder(diag) | |
274 | 123 irc = builder.build(testsrc) |
124 if not irc: | |
125 diag.printErrors(testsrc) | |
126 irc.check() | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
275
diff
changeset
|
127 #irc.dump() |
274 | 128 with open('ir.gv', 'w') as f: |
129 ir.dumpgv(irc, f) | |
269 | 130 outs = outstream.TextOutputStream() |
131 cga = codegenarm.ArmCodeGenerator(outs) | |
274 | 132 ir2 = cga.generate(irc) |
133 | |
279 | 134 with open('cfg.gv', 'w') as cfg_file: |
135 dump_cfg(cga, cfg_file) | |
269 | 136 |
279 | 137 with open('ig.gv', 'w') as ig_file: |
138 dump_ig(cga, ig_file) | |
274 | 139 |
275 | 140 outs.dump() |
141 |