comparison test/tcodegen.py @ 290:7b38782ed496

File moves
author Windel Bouwman
date Sun, 24 Nov 2013 11:24:15 +0100
parents python/tcodegen.py@2ccd57b1d78c
children
comparison
equal deleted inserted replaced
289:bd2593de3ff8 290:7b38782ed496
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
10 import ir
11
12 testsrc = """
13 package test2;
14
15 var int phaa, foo, bar;
16
17 function int insanemath(int a, int b)
18 {
19 var int c;
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 }
32 return c;
33 }
34
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;
43 insanemath(2, 3);
44 if (cee + a > b and b - a+b== 3*6-insanemath(b, 2))
45 {
46 var int x = a;
47 x = b - a + insanemath(3, 4) - insanemath(33, insanemath(2, 0));
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
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 """
78
79 testsrc = """
80 package test3;
81
82 function int ab(int a, int b)
83 {
84 var int c;
85 var int *a2;
86 a2 = cast<int*>(2);
87 *a2 = 2;
88 *a2 = *a2 + 2;
89 c = 0;
90 c = c + a + b;
91 return c;
92 }
93
94 """
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
120 if __name__ == '__main__':
121 diag = ppci.DiagnosticsManager()
122 builder = c3.Builder(diag)
123 irc = builder.build(testsrc)
124 if not irc:
125 diag.printErrors(testsrc)
126 irc.check()
127 #irc.dump()
128 with open('ir.gv', 'w') as f:
129 ir.dumpgv(irc, f)
130 outs = outstream.TextOutputStream()
131 cga = codegenarm.ArmCodeGenerator(outs)
132 ir2 = cga.generate(irc)
133
134 with open('cfg.gv', 'w') as cfg_file:
135 dump_cfg(cga, cfg_file)
136
137 with open('ig.gv', 'w') as ig_file:
138 dump_ig(cga, ig_file)
139
140 outs.dump()
141