annotate test/testir.py @ 300:158068af716c

yafm
author Windel Bouwman
date Tue, 03 Dec 2013 18:00:22 +0100
parents 534b94b40aa8
children 6753763d3bec
rev   line source
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
1 import unittest, os
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
2 import sys
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 253
diff changeset
3 import ppci
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
4 import ir
300
Windel Bouwman
parents: 292
diff changeset
5 from ppci.transform import ConstantFolder
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
6
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
7
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
8 class IrCodeTestCase(unittest.TestCase):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
9 def setUp(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
10 self.b = ir.Builder()
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
11 self.m = ir.Module('test')
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
12 self.b.setModule(self.m)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
13
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
14 def testBuilder(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
15 f = self.b.newFunction('add')
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
16 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
17 bb = self.b.newBlock()
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
18 self.b.emit(ir.Jump(bb))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
19 self.b.setBlock(bb)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
20 self.b.emit(ir.Exp(ir.Const(0)))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
21 self.b.emit(ir.Jump(f.epiloog))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
22 self.m.check()
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
23 # Run interpreter:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
24 # r = self.m.getFunction('add').call(1, 2)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
25 #self.assertEqual(3, r)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
26
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
27
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
28 class PatternMatchTestCase(unittest.TestCase):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
29 def testSimpleTree(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
30 t = ir.Term('x')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
31 pat = ir.Binop(ir.Const(2), '+', t)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
32 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
33 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
34 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
35 self.assertEqual(3, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
36
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
37 def testSimpleTree2(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
38 t = ir.Term('x')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
39 t2 = ir.Term('y')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
40 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
41 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
42 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
43 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
44 self.assertEqual(2, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
45 self.assertIn(t2, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
46 self.assertEqual(3, mp[t2])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
47 res, mp = ir.match_tree(ir.Const(2), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
48 self.assertFalse(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
49
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
50
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
51 class ConstantFolderTestCase(unittest.TestCase):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
52 def setUp(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
53 self.b = ir.Builder()
300
Windel Bouwman
parents: 292
diff changeset
54 self.cf = ConstantFolder()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
55 self.m = ir.Module('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
56 self.b.setModule(self.m)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
57
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
58 def testBuilder(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
59 f = self.b.newFunction('test')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
60 self.b.setFunction(f)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
61 bb = self.b.newBlock()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
62 self.b.emit(ir.Jump(bb))
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
63 self.b.setBlock(bb)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
64 v1 = ir.Const(5)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
65 v2 = ir.Const(7)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
66 v3 = ir.Add(v1, v2)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
67 self.b.emit(ir.Jump(f.epiloog))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
68 self.cf.run(self.m)
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
69
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
70 def testAdd0(self):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
71 f = self.b.newFunction('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
72 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
73 self.b.setBlock(self.b.newBlock())
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
74 v1 = ir.Const(0)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
75 v2 = ir.Const(0)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
76 v3 = ir.Add(v1, v2)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
77
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
78
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
79 testsrc = """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
80 package test2;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
81
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
82 function void tesssst(int henkie)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
83 {
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
84 var int a, b, cee;
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
85 a = 2 * 33 - 12;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
86 b = a * 2 + 13;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
87 a = b + a;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
88 cee = a;
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
89 cee = cee * 2 + a + cee * 2;
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
90 if (cee + a > b and b *3 - a+8*b== 3*6-b)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
91 {
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
92 var int x = a;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
93 x = b * 2 - a;
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
94 a = x * x * (x + 22 - a);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
95 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
96 else
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
97 {
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
98 a = b + a + (a + b);
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
99 }
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
100 var int y;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
101 y = a - b * 53;
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
102 }
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
103 """
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
104
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
105 testsrc2 = """
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
106 function int add2(int x, int y)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
107 {
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
108 var int res;
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
109 res = x + y + 2 - 7 + 2;
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
110 //if (y < 2)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
111 //{
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
112 // return y - 33;
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
113 //}
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
114
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
115 res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
116
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
117 if (x > 13)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
118 {
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
119 while (y > 1337)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
120 {
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
121 res = res + 2;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
122 y = y - 12;
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
123 }
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
124 }
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
125 return res;
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
126 }
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
127
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
128 """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
129
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
130 if __name__ == '__main__':
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
131 unittest.main()
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
132 sys.exit()