Mercurial > lcfOS
comparison python/testir.py @ 252:c4370696ccc7
added optimize function
author | Windel Bouwman |
---|---|
date | Tue, 30 Jul 2013 17:57:46 +0200 |
parents | ef683881c64e |
children | 74c6a20302d5 |
comparison
equal
deleted
inserted
replaced
251:6ed3d3a82a63 | 252:c4370696ccc7 |
---|---|
1 import unittest, os | 1 import unittest, os |
2 import sys | |
2 import c3, ppci, ir, x86, transform | 3 import c3, ppci, ir, x86, transform |
3 | 4 |
4 class ConstantFolderTestCase(unittest.TestCase): | 5 class ConstantFolderTestCase(unittest.TestCase): |
5 def setUp(self): | 6 def setUp(self): |
6 self.b = ir.Builder() | 7 self.b = ir.Builder() |
45 var int a, b, cee; | 46 var int a, b, cee; |
46 a = 2 * 33 - 12; | 47 a = 2 * 33 - 12; |
47 b = a * 2 + 13; | 48 b = a * 2 + 13; |
48 a = b + a; | 49 a = b + a; |
49 cee = a; | 50 cee = a; |
50 if (a > b and b *3 - a+8*b== 3*6-b) | 51 cee = cee * 2 + a + cee * 2; |
52 if (cee + a > b and b *3 - a+8*b== 3*6-b) | |
51 { | 53 { |
52 var int x = a; | 54 var int x = a; |
53 x = b * 2 - a; | 55 x = b * 2 - a; |
54 a = x * x * add2(x, 22 - a); | 56 a = x * x * (x + 22 - a); |
55 } | 57 } |
56 else | 58 else |
57 { | 59 { |
58 a = b + a + add2(a, b); | 60 a = b + a + (a + b); |
59 } | 61 } |
60 var int y; | 62 var int y; |
61 y = a - b * 53; | 63 y = a - b * 53; |
62 } | 64 } |
65 """ | |
63 | 66 |
67 testsrc2 = """ | |
64 function int add2(int x, int y) | 68 function int add2(int x, int y) |
65 { | 69 { |
66 var int res; | 70 var int res; |
67 res = x + y + 2 - 7 + 2; | 71 res = x + y + 2 - 7 + 2; |
68 //if (y < 2) | 72 //if (y < 2) |
84 } | 88 } |
85 | 89 |
86 """ | 90 """ |
87 | 91 |
88 if __name__ == '__main__': | 92 if __name__ == '__main__': |
89 #unittest.main() | 93 #unittest.main() |
90 #sys.exit() | 94 #sys.exit() |
91 diag = ppci.DiagnosticsManager() | 95 diag = ppci.DiagnosticsManager() |
92 builder = c3.Builder(diag) | 96 builder = c3.Builder(diag) |
93 cgenx86 = x86.X86CodeGenSimple(diag) | 97 cgenx86 = x86.X86CodeGenSimple(diag) |
94 ir = builder.build(testsrc) | 98 ir = builder.build(testsrc) |
95 diag.printErrors(testsrc) | 99 diag.printErrors(testsrc) |
96 ir.dump() | 100 ir.check() |
97 ir.check() | 101 ir.dump() |
98 cf = transform.ConstantFolder() | 102 transform.optimize(ir) |
99 ir.check() | 103 print('dump IR') |
100 dcd = transform.DeadCodeDeleter() | 104 print('dump IR') |
101 ir.check() | 105 print('dump IR') |
102 m2r = transform.Mem2RegPromotor() | 106 print('dump IR') |
103 ir.check() | 107 ir.dump() |
104 clr = transform.CleanPass() | |
105 ir.check() | |
106 cf.run(ir) | |
107 dcd.run(ir) | |
108 clr.run(ir) | |
109 m2r.run(ir) | |
110 #ir.dump() | |
111 | 108 |
112 # Dump a graphiz file: | 109 # Dump a graphiz file: |
113 with open('graaf.gv', 'w') as f: | 110 with open('graaf.gv', 'w') as f: |
114 ir.dumpgv(f) | 111 ir.dumpgv(f) |
115 os.system('dot -Tpdf -ograaf.pdf graaf.gv') | 112 os.system('dot -Tsvg -ograaf.svg graaf.gv') |
116 | 113 |
117 asm = cgenx86.genBin(ir) | 114 sys.exit() |
118 #for a in asm: | 115 asm = cgenx86.genBin(ir) |
119 # print(a) | 116 #for a in asm: |
120 with open('out.asm', 'w') as f: | 117 # print(a) |
118 with open('out.asm', 'w') as f: | |
121 f.write('BITS 64\n') | 119 f.write('BITS 64\n') |
122 for a in asm: | 120 for a in asm: |
123 f.write(str(a) + '\n') | 121 f.write(str(a) + '\n') |
124 print(a) | 122 print(a) |
125 | 123 |