annotate test/testir.py @ 307:e609d5296ee9

Massive rewrite of codegenerator
author Windel Bouwman
date Thu, 12 Dec 2013 20:42:56 +0100
parents 6753763d3bec
children 68b01c8abf8a
rev   line source
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
1 import unittest
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
2 import os
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
3 import sys
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 253
diff changeset
4 import ppci
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
5 from ppci import ir
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
6 from ppci import irutils
300
Windel Bouwman
parents: 292
diff changeset
7 from ppci.transform import ConstantFolder
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):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
11 def testAdd(self):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
12 v = ir.Add(ir.Const(1), ir.Const(2))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
13
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
14
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
15 class IrBuilderTestCase(unittest.TestCase):
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
16 def setUp(self):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
17 self.b = irutils.Builder()
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
18 self.m = ir.Module('test')
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
19 self.b.setModule(self.m)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
20
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21 def testBuilder(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
22 f = self.b.newFunction('add')
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
23 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
24 bb = self.b.newBlock()
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
25 self.b.emit(ir.Jump(bb))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
26 self.b.setBlock(bb)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
27 self.b.emit(ir.Exp(ir.Const(0)))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
28 self.b.emit(ir.Jump(f.epiloog))
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
29 self.m.check()
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
30 # Run interpreter:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
31 # r = self.m.getFunction('add').call(1, 2)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
32 #self.assertEqual(3, r)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
33
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
34
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
35 class PatternMatchTestCase(unittest.TestCase):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
36 def testSimpleTree(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
37 t = ir.Term('x')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
38 pat = ir.Binop(ir.Const(2), '+', t)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
39 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
40 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
41 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
42 self.assertEqual(3, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
43
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
44 def testSimpleTree2(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
45 t = ir.Term('x')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
46 t2 = ir.Term('y')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
47 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
48 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
49 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
50 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
51 self.assertEqual(2, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
52 self.assertIn(t2, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
53 self.assertEqual(3, mp[t2])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
54 res, mp = ir.match_tree(ir.Const(2), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
55 self.assertFalse(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
56
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
57
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
58 class ConstantFolderTestCase(unittest.TestCase):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
59 def setUp(self):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
60 self.b = irutils.Builder()
300
Windel Bouwman
parents: 292
diff changeset
61 self.cf = ConstantFolder()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
62 self.m = ir.Module('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
63 self.b.setModule(self.m)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
64
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
65 def testBuilder(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
66 f = self.b.newFunction('test')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
67 self.b.setFunction(f)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
68 bb = self.b.newBlock()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
69 self.b.emit(ir.Jump(bb))
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
70 self.b.setBlock(bb)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
71 v1 = ir.Const(5)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
72 v2 = ir.Const(7)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
73 v3 = ir.Add(v1, v2)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
74 self.b.emit(ir.Jump(f.epiloog))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
75 self.cf.run(self.m)
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
76
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
77 def testAdd0(self):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
78 f = self.b.newFunction('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
79 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
80 self.b.setBlock(self.b.newBlock())
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
81 v1 = ir.Const(0)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
82 v2 = ir.Const(0)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
83 v3 = ir.Add(v1, v2)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
84
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
85
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
86
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
87
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
88 if __name__ == '__main__':
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
89 unittest.main()
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
90 sys.exit()