Mercurial > lcfOS
comparison test/testir.py @ 284:05184b95fa16
Moved tests to seperate folder
author | Windel Bouwman |
---|---|
date | Fri, 15 Nov 2013 13:43:22 +0100 |
parents | python/testir.py@2ccd57b1d78c |
children | 534b94b40aa8 |
comparison
equal
deleted
inserted
replaced
283:c9781c73e7e2 | 284:05184b95fa16 |
---|---|
1 import unittest, os | |
2 import sys | |
3 import c3 | |
4 import ppci | |
5 import ir, x86, transform | |
6 import optimize | |
7 | |
8 | |
9 class IrCodeTestCase(unittest.TestCase): | |
10 def setUp(self): | |
11 self.b = ir.Builder() | |
12 self.m = ir.Module('test') | |
13 self.b.setModule(self.m) | |
14 | |
15 def testBuilder(self): | |
16 f = self.b.newFunction('add') | |
17 self.b.setFunction(f) | |
18 bb = self.b.newBlock() | |
19 self.b.emit(ir.Jump(bb)) | |
20 self.b.setBlock(bb) | |
21 self.b.emit(ir.Exp(ir.Const(0))) | |
22 self.b.emit(ir.Jump(f.epiloog)) | |
23 self.m.check() | |
24 # Run interpreter: | |
25 # r = self.m.getFunction('add').call(1, 2) | |
26 #self.assertEqual(3, r) | |
27 | |
28 | |
29 class PatternMatchTestCase(unittest.TestCase): | |
30 def testSimpleTree(self): | |
31 t = ir.Term('x') | |
32 pat = ir.Binop(ir.Const(2), '+', t) | |
33 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat) | |
34 self.assertTrue(res) | |
35 self.assertIn(t, mp) | |
36 self.assertEqual(3, mp[t]) | |
37 | |
38 def testSimpleTree2(self): | |
39 t = ir.Term('x') | |
40 t2 = ir.Term('y') | |
41 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2)) | |
42 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat) | |
43 self.assertTrue(res) | |
44 self.assertIn(t, mp) | |
45 self.assertEqual(2, mp[t]) | |
46 self.assertIn(t2, mp) | |
47 self.assertEqual(3, mp[t2]) | |
48 res, mp = ir.match_tree(ir.Const(2), pat) | |
49 self.assertFalse(res) | |
50 | |
51 | |
52 class ConstantFolderTestCase(unittest.TestCase): | |
53 def setUp(self): | |
54 self.b = ir.Builder() | |
55 self.cf = transform.ConstantFolder() | |
56 self.m = ir.Module('test') | |
57 self.b.setModule(self.m) | |
58 | |
59 def testBuilder(self): | |
60 f = self.b.newFunction('test') | |
61 self.b.setFunction(f) | |
62 bb = self.b.newBlock() | |
63 self.b.emit(ir.Jump(bb)) | |
64 self.b.setBlock(bb) | |
65 v1 = ir.Const(5) | |
66 v2 = ir.Const(7) | |
67 v3 = ir.Add(v1, v2) | |
68 self.b.emit(ir.Jump(f.epiloog)) | |
69 self.cf.run(self.m) | |
70 | |
71 def testAdd0(self): | |
72 f = self.b.newFunction('test') | |
73 self.b.setFunction(f) | |
74 self.b.setBlock(self.b.newBlock()) | |
75 v1 = ir.Const(0) | |
76 v2 = ir.Const(0) | |
77 v3 = ir.Add(v1, v2) | |
78 | |
79 | |
80 testsrc = """ | |
81 package test2; | |
82 | |
83 function void tesssst(int henkie) | |
84 { | |
85 var int a, b, cee; | |
86 a = 2 * 33 - 12; | |
87 b = a * 2 + 13; | |
88 a = b + a; | |
89 cee = a; | |
90 cee = cee * 2 + a + cee * 2; | |
91 if (cee + a > b and b *3 - a+8*b== 3*6-b) | |
92 { | |
93 var int x = a; | |
94 x = b * 2 - a; | |
95 a = x * x * (x + 22 - a); | |
96 } | |
97 else | |
98 { | |
99 a = b + a + (a + b); | |
100 } | |
101 var int y; | |
102 y = a - b * 53; | |
103 } | |
104 """ | |
105 | |
106 testsrc2 = """ | |
107 function int add2(int x, int y) | |
108 { | |
109 var int res; | |
110 res = x + y + 2 - 7 + 2; | |
111 //if (y < 2) | |
112 //{ | |
113 // return y - 33; | |
114 //} | |
115 | |
116 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8); | |
117 | |
118 if (x > 13) | |
119 { | |
120 while (y > 1337) | |
121 { | |
122 res = res + 2; | |
123 y = y - 12; | |
124 } | |
125 } | |
126 return res; | |
127 } | |
128 | |
129 """ | |
130 | |
131 if __name__ == '__main__': | |
132 #unittest.main() | |
133 #sys.exit() | |
134 diag = ppci.DiagnosticsManager() | |
135 builder = c3.Builder(diag) | |
136 cgenx86 = x86.X86CodeGenSimple(diag) | |
137 ir = builder.build(testsrc) | |
138 diag.printErrors(testsrc) | |
139 ir.check() | |
140 ir.dump() | |
141 optimize.optimize(ir) | |
142 print('dump IR') | |
143 print('dump IR') | |
144 print('dump IR') | |
145 print('dump IR') | |
146 ir.dump() | |
147 | |
148 # Dump a graphiz file: | |
149 with open('graaf.gv', 'w') as f: | |
150 ir.dumpgv(f) | |
151 os.system('dot -Tsvg -ograaf.svg graaf.gv') | |
152 | |
153 sys.exit() | |
154 asm = cgenx86.genBin(ir) | |
155 #for a in asm: | |
156 # print(a) | |
157 with open('out.asm', 'w') as f: | |
158 f.write('BITS 64\n') | |
159 for a in asm: | |
160 f.write(str(a) + '\n') | |
161 print(a) | |
162 |