comparison python/tcodegen.py @ 274:ea93e0a7a31e

Move docs
author Windel Bouwman
date Wed, 04 Sep 2013 17:35:06 +0200
parents e64bae57cda8
children 6f2423df0675
comparison
equal deleted inserted replaced
273:6b3a874edd6e 274:ea93e0a7a31e
5 5
6 import c3 6 import c3
7 import ppci 7 import ppci
8 import codegenarm 8 import codegenarm
9 import outstream 9 import outstream
10 import ir
10 11
11 testsrc = """ 12 testsrc = """
12 package test2; 13 package test2;
13 14
14 var int phaa, foo, bar; 15 var int phaa, foo, bar;
15 16
16 function int insanemath(int a, int b) 17 function int insanemath(int a, int b)
17 { 18 {
18 var int c; 19 var int c;
19 c = a + b + 1; 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 }
20 return c; 32 return c;
21 } 33 }
22 34
23 function void tesssst(int henkie) 35 function void tesssst(int henkie)
24 { 36 {
29 cee = a; 41 cee = a;
30 cee = cee * 2 + cee; 42 cee = cee * 2 + cee;
31 if (cee + a > b and b - a+b== 3*6-b) 43 if (cee + a > b and b - a+b== 3*6-b)
32 { 44 {
33 var int x = a; 45 var int x = a;
34 x = b - a + insanemath(3, 4); 46 x = b - a + insanemath(3, 4) - insanemath(33,2);
35 a = x * (x + a); 47 a = x * (x + a);
36 } 48 }
37 else 49 else
38 { 50 {
39 a = b + (a + b); 51 a = b + (a + b);
44 """ 56 """
45 57
46 if __name__ == '__main__': 58 if __name__ == '__main__':
47 diag = ppci.DiagnosticsManager() 59 diag = ppci.DiagnosticsManager()
48 builder = c3.Builder(diag) 60 builder = c3.Builder(diag)
49 ir = builder.build(testsrc) 61 irc = builder.build(testsrc)
50 ir.dump() 62 if not irc:
63 diag.printErrors(testsrc)
64 irc.check()
65 irc.dump()
66 with open('ir.gv', 'w') as f:
67 ir.dumpgv(irc, f)
51 outs = outstream.TextOutputStream() 68 outs = outstream.TextOutputStream()
52 cga = codegenarm.ArmCodeGenerator(outs) 69 cga = codegenarm.ArmCodeGenerator(outs)
53 cfg_file = open('cfg.gv', 'w') 70 ir2 = cga.generate(irc)
54 ig_file = open('ig.gv', 'w')
55 ir2 = cga.generate(ir, cfg_file=cfg_file, ig_file=ig_file)
56 cfg_file.close()
57 ig_file.close()
58 for i in ir2:
59 print(i)
60 71
72 with open('cfg.gv', 'w') as cfg_file:
73 print('digraph G {', file=cfg_file)
74 #print('edge [constraint=none]', file=cfg_file)
75 print('rankdir=TB', file=cfg_file)
76 for f in cga.frames:
77 print('subgraph cluster_{} {{'.format(f.name), file=cfg_file)
78 print('label={};'.format(f.name), file=cfg_file)
79 print('color=lightgrey;', file=cfg_file)
80 print('style=filled;', file=cfg_file)
81 f.cfg.to_dot(cfg_file)
82 print('}', file=cfg_file)
83 print('}', file=cfg_file)
84
85 with open('ig.gv', 'w') as ig_file:
86 print('digraph G {', file=ig_file)
87 print('edge [arrowhead=none]', file=ig_file)
88 for f in cga.frames:
89 print('subgraph cluster_{} {{'.format(f.name), file=ig_file)
90 print('label={};'.format(f.name), file=ig_file)
91 print('color=lightgrey;', file=ig_file)
92 print('style=filled;', file=ig_file)
93 f.ig.to_dot(ig_file)
94 print('}', file=ig_file)
95 print('}', file=ig_file)
96
97 for f in ir2:
98 print(f)
99 for i in f.instructions:
100 print(' {}'.format(i))
101