173
|
1 import c3, ppci, ir, x86, transform
|
171
|
2 import os
|
|
3
|
|
4 testsrc = """
|
|
5 package test2;
|
|
6
|
175
|
7 function void tesssst(int henkie)
|
171
|
8 {
|
175
|
9 var int a, b, cee;
|
171
|
10 a = 2 * 33 - 12;
|
|
11 b = a * 2 + 13;
|
|
12 a = b + a;
|
175
|
13 cee = a;
|
171
|
14 if (a > b and b *3 - a+8*b== 3*6-b)
|
|
15 {
|
|
16 var int x = a;
|
|
17 x = b * 2 - a;
|
175
|
18 a = x * x * add2(x, 22 - a);
|
171
|
19 }
|
|
20 else
|
|
21 {
|
175
|
22 a = b + a + add2(a, b);
|
171
|
23 }
|
|
24 var int y;
|
|
25 y = a - b * 53;
|
|
26 }
|
|
27
|
172
|
28 function int add2(int x, int y)
|
|
29 {
|
|
30 var int res;
|
174
|
31 res = x + y + 2 - 7 + 2;
|
175
|
32 //if (y < 2)
|
|
33 //{
|
|
34 // return y - 33;
|
|
35 //}
|
|
36
|
177
|
37 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
|
|
38
|
174
|
39 if (x > 13)
|
|
40 {
|
176
|
41 while (y > 1337)
|
|
42 {
|
|
43 res = res + 2;
|
|
44 y = y - 12;
|
|
45 }
|
174
|
46 }
|
172
|
47 return res;
|
|
48 }
|
|
49
|
171
|
50 """
|
|
51
|
|
52 if __name__ == '__main__':
|
|
53 diag = ppci.DiagnosticsManager()
|
|
54 builder = c3.Builder(diag)
|
180
|
55 cgenx86 = x86.X86CodeGenSimple(diag)
|
171
|
56 ir = builder.build(testsrc)
|
172
|
57 diag.printErrors(testsrc)
|
173
|
58 #ir.dump()
|
|
59 cf = transform.ConstantFolder()
|
|
60 dcd = transform.DeadCodeDeleter()
|
175
|
61 m2r = transform.Mem2RegPromotor()
|
177
|
62 clr = transform.CleanPass()
|
171
|
63 ir.check()
|
173
|
64 cf.run(ir)
|
175
|
65 dcd.run(ir)
|
177
|
66 clr.run(ir)
|
175
|
67 m2r.run(ir)
|
|
68 #ir.dump()
|
176
|
69
|
|
70 # Dump a graphiz file:
|
|
71 with open('graaf.gv', 'w') as f:
|
|
72 ir.dumpgv(f)
|
|
73 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
|
|
74
|
171
|
75 asm = cgenx86.genBin(ir)
|
175
|
76 #for a in asm:
|
|
77 # print(a)
|
171
|
78 with open('out.asm', 'w') as f:
|
|
79 f.write('BITS 64\n')
|
|
80 for a in asm:
|
|
81 f.write(str(a) + '\n')
|
180
|
82 print(a)
|
|
83
|