171
|
1 import c3, ppci, ir, x86
|
|
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
|
|
27 """
|
|
28
|
|
29 if __name__ == '__main__':
|
|
30 diag = ppci.DiagnosticsManager()
|
|
31 builder = c3.Builder(diag)
|
|
32 cgenx86 = x86.X86CodeGen(diag)
|
|
33 ir = builder.build(testsrc)
|
|
34 ir.check()
|
|
35 ir.analyze()
|
|
36 #ir.constantProp()
|
|
37 ir.dump()
|
|
38 asm = cgenx86.genBin(ir)
|
|
39 for a in asm:
|
|
40 print(a)
|
|
41 with open('out.asm', 'w') as f:
|
|
42 f.write('BITS 64\n')
|
|
43 for a in asm:
|
|
44 f.write(str(a) + '\n')
|
|
45
|
|
46 # Dump a graphiz file:
|
|
47 with open('graaf.gv', 'w') as f:
|
|
48 ir.dumpgv(f)
|
|
49 os.system('dot -Tpdf -ograaf.pdf graaf.gv')
|
|
50
|
|
51
|