annotate test/testir.py @ 383:173e20a47fda

Added linker description loader
author Windel Bouwman
date Sun, 27 Apr 2014 17:40:39 +0200
parents 86b02c98a717
children 988f3fb861e4
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
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
4 import io
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 253
diff changeset
5 import ppci
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
6 from ppci import ir
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
7 from ppci import irutils
300
Windel Bouwman
parents: 292
diff changeset
8 from ppci.transform import ConstantFolder
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
9
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
10
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
11 class IrCodeTestCase(unittest.TestCase):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
12 def testAdd(self):
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
13 v = ir.Add(ir.Const(1), ir.Const(2))
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
14
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
15
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
16 class IrBuilderTestCase(unittest.TestCase):
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
17 def setUp(self):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
18 self.b = irutils.Builder()
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
19 self.m = ir.Module('test')
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
20 self.b.setModule(self.m)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
21
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
22 def testBuilder(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
23 f = self.b.newFunction('add')
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
24 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
25 bb = self.b.newBlock()
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
26 self.b.emit(ir.Jump(bb))
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
27 self.b.setBlock(bb)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
28 self.b.emit(ir.Exp(ir.Const(0)))
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
29 self.b.emit(ir.Jump(f.epiloog))
253
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):
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 309
diff changeset
36 @unittest.skip('Not yet implemented')
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
37 def testSimpleTree(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 pat = ir.Binop(ir.Const(2), '+', t)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
40 res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
41 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
42 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
43 self.assertEqual(3, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
44
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 309
diff changeset
45 @unittest.skip('Not yet implemented')
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
46 def testSimpleTree2(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
47 t = ir.Term('x')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
48 t2 = ir.Term('y')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
49 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
50 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
51 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
52 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
53 self.assertEqual(2, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
54 self.assertIn(t2, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
55 self.assertEqual(3, mp[t2])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
56 res, mp = ir.match_tree(ir.Const(2), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
57 self.assertFalse(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
58
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
59
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
60 class ConstantFolderTestCase(unittest.TestCase):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
61 def setUp(self):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
62 self.b = irutils.Builder()
300
Windel Bouwman
parents: 292
diff changeset
63 self.cf = ConstantFolder()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
64 self.m = ir.Module('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
65 self.b.setModule(self.m)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
66
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
67 def testBuilder(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
68 f = self.b.newFunction('test')
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
69 self.b.setFunction(f)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
70 bb = self.b.newBlock()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
71 self.b.emit(ir.Jump(bb))
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
72 self.b.setBlock(bb)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
73 v1 = ir.Const(5)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
74 v2 = ir.Const(7)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
75 v3 = ir.Add(v1, v2)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
76 self.b.emit(ir.Jump(f.epiloog))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
77 self.cf.run(self.m)
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
78
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
79 def testAdd0(self):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
80 f = self.b.newFunction('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
81 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
82 self.b.setBlock(self.b.newBlock())
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
83 v1 = ir.Const(0)
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
84 v3 = ir.Add(v1, ir.Const(0))
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
85
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 317
diff changeset
86
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
87 class TestWriter(unittest.TestCase):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
88 def testWrite(self):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
89 writer = irutils.Writer()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
90 module = ir.Module('mod1')
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
91 function = ir.Function('func1', module)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
92 f = io.StringIO()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
93 writer.write(module, f)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
94 #print(f.getvalue())
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
95 f2 = io.StringIO(f.getvalue())
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
96 reader = irutils.Reader()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
97 module2 = reader.read(f2)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
98 f = io.StringIO()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
99 writer.write(module2, f)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
100 #print(f.getvalue())
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
101
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
102
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
103 class TestReader(unittest.TestCase):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
104 def testAddExample(self):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
105 reader = irutils.Reader()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
106 with open('../examples/pi/add.pi') as f:
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
107 m = reader.read(f)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
108 self.assertTrue(m)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
109 #print(m)
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 317
diff changeset
110
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
111
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
112 if __name__ == '__main__':
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
113 unittest.main()
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
114 sys.exit()