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;
|
|
30 res = x + y;
|
|
31 return res;
|
|
32 }
|
|
33
|
171
|
34 """
|
|
35
|
|
36 if __name__ == '__main__':
|
|
37 diag = ppci.DiagnosticsManager()
|
|
38 builder = c3.Builder(diag)
|
|
39 cgenx86 = x86.X86CodeGen(diag)
|
|
40 ir = builder.build(testsrc)
|
172
|
41 diag.printErrors(testsrc)
|
173
|
42 #ir.dump()
|
|
43 cf = transform.ConstantFolder()
|
|
44 dcd = transform.DeadCodeDeleter()
|
171
|
45 ir.check()
|
173
|
46 cf.run(ir)
|
|
47 cf.run(ir)
|
|
48 #dcd.run(ir)
|
171
|
49 ir.dump()
|
|
50 asm = cgenx86.genBin(ir)
|
|
51 for a in asm:
|
|
52 print(a)
|
|
53 with open('out.asm', 'w') as f:
|
|
54 f.write('BITS 64\n')
|
|
55 for a in asm:
|
|
56 f.write(str(a) + '\n')
|
|
57
|
|
58 # Dump a graphiz file:
|
|
59 with open('graaf.gv', 'w') as f:
|
|
60 ir.dumpgv(f)
|
|
61 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
|
|
62
|
|
63
|