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