annotate test/testir.py @ 295:917eab04b8b7

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