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 """
|
|
78 def dump_cfg(cga, cfg_file):
|
|
79 print('digraph G {', file=cfg_file)
|
|
80 #print('edge [constraint=none]', file=cfg_file)
|
|
81 print('rankdir=TB', file=cfg_file)
|
|
82 for f in cga.frames:
|
|
83 print('subgraph cluster_{} {{'.format(f.name), file=cfg_file)
|
|
84 print('label={};'.format(f.name), file=cfg_file)
|
|
85 print('color=lightgrey;', file=cfg_file)
|
|
86 print('style=filled;', file=cfg_file)
|
|
87 f.cfg.to_dot(cfg_file)
|
|
88 print('}', file=cfg_file)
|
|
89 print('}', file=cfg_file)
|
|
90
|
|
91 def dump_ig(cga, ig_file):
|
|
92 print('digraph G {', file=ig_file)
|
|
93 print('edge [arrowhead=none]', file=ig_file)
|
|
94 for f in cga.frames:
|
|
95 print('subgraph cluster_{} {{'.format(f.name), file=ig_file)
|
|
96 print('label={};'.format(f.name), file=ig_file)
|
|
97 print('color=lightgrey;', file=ig_file)
|
|
98 print('style=filled;', file=ig_file)
|
|
99 f.ig.to_dot(ig_file)
|
|
100 print('}', file=ig_file)
|
|
101 print('}', file=ig_file)
|
|
102
|
269
|
103 if __name__ == '__main__':
|
|
104 diag = ppci.DiagnosticsManager()
|
|
105 builder = c3.Builder(diag)
|
274
|
106 irc = builder.build(testsrc)
|
|
107 if not irc:
|
|
108 diag.printErrors(testsrc)
|
|
109 irc.check()
|
|
110 irc.dump()
|
|
111 with open('ir.gv', 'w') as f:
|
|
112 ir.dumpgv(irc, f)
|
269
|
113 outs = outstream.TextOutputStream()
|
|
114 cga = codegenarm.ArmCodeGenerator(outs)
|
274
|
115 ir2 = cga.generate(irc)
|
|
116
|
|
117 with open('cfg.gv', 'w') as cfg_file:
|
275
|
118 dump_cfg(cga, cfg_file)
|
269
|
119
|
274
|
120 with open('ig.gv', 'w') as ig_file:
|
275
|
121 dump_ig(cga, ig_file)
|
274
|
122
|
|
123 for f in ir2:
|
|
124 print(f)
|
|
125 for i in f.instructions:
|
|
126 print(' {}'.format(i))
|
|
127
|
275
|
128 outs.dump()
|
|
129
|