annotate test/testir.py @ 399:a7c444404df9

Fix hexwrite
author Windel Bouwman
date Fri, 20 Jun 2014 16:36:49 +0200
parents c0d9837acde8
children
rev   line source
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
1 import unittest
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
2 import sys
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
3 import io
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):
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
12 v = ir.Add(ir.Const(1), ir.Const(2), "add", ir.i32)
307
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):
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
22 f = self.b.new_function('add')
253
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 # Run interpreter:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
30 # r = self.m.getFunction('add').call(1, 2)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
31 #self.assertEqual(3, r)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 252
diff changeset
32
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 268
diff changeset
33
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
34 class PatternMatchTestCase(unittest.TestCase):
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 309
diff changeset
35 @unittest.skip('Not yet implemented')
277
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
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 309
diff changeset
44 @unittest.skip('Not yet implemented')
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
45 def testSimpleTree2(self):
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
46 t = ir.Term('x')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
47 t2 = ir.Term('y')
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
48 pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
49 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
50 self.assertTrue(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
51 self.assertIn(t, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
52 self.assertEqual(2, mp[t])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
53 self.assertIn(t2, mp)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
54 self.assertEqual(3, mp[t2])
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
55 res, mp = ir.match_tree(ir.Const(2), pat)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
56 self.assertFalse(res)
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
57
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
58
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
59 class ConstantFolderTestCase(unittest.TestCase):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
60 def setUp(self):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 301
diff changeset
61 self.b = irutils.Builder()
300
Windel Bouwman
parents: 292
diff changeset
62 self.cf = ConstantFolder()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
63 self.m = ir.Module('test')
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
64 self.b.setModule(self.m)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
65
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
66 def testBuilder(self):
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
67 f = self.b.new_function('test')
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
68 self.b.setFunction(f)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
69 bb = self.b.newBlock()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
70 self.b.emit(ir.Jump(bb))
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
71 self.b.setBlock(bb)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
72 v1 = ir.Const(5)
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 397
diff changeset
73 # self.b.emit(v1)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
74 v2 = ir.Const(7)
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
75 v3 = ir.Add(v1, v2, "add", ir.i32)
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 397
diff changeset
76 # self.b.emit(v3)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
77 self.b.emit(ir.Jump(f.epiloog))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
78 self.cf.run(self.m)
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
79
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
80 def testAdd0(self):
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
81 f = self.b.new_function('test')
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 204
diff changeset
82 self.b.setFunction(f)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
83 self.b.setBlock(self.b.newBlock())
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
84 v1 = ir.Const(12)
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
85 v3 = ir.Add(v1, ir.Const(0), "add", ir.i32)
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
86
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 317
diff changeset
87
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
88 class TestWriter(unittest.TestCase):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
89 def testWrite(self):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
90 writer = irutils.Writer()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
91 module = ir.Module('mod1')
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
92 function = ir.Function('func1', module)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
93 f = io.StringIO()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
94 writer.write(module, f)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
95 #print(f.getvalue())
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
96 f2 = io.StringIO(f.getvalue())
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
97 reader = irutils.Reader()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
98 module2 = reader.read(f2)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
99 f = io.StringIO()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
100 writer.write(module2, f)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
101 #print(f.getvalue())
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 180
diff changeset
102
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
103
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
104 class TestReader(unittest.TestCase):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
105 def testAddExample(self):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
106 reader = irutils.Reader()
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
107 with open('../examples/pi/add.pi') as f:
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
108 m = reader.read(f)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
109 self.assertTrue(m)
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
110 #print(m)
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 317
diff changeset
111
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
112
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
113 if __name__ == '__main__':
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
114 unittest.main()
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
115 sys.exit()