view test/testir.py @ 398:c0d9837acde8

x86 target refactor
author Windel Bouwman
date Thu, 29 May 2014 12:13:37 +0200
parents 5d03c10fe19d
children
line wrap: on
line source

import unittest
import sys
import io
import ppci
from ppci import ir
from ppci import irutils
from ppci.transform import ConstantFolder


class IrCodeTestCase(unittest.TestCase):
    def testAdd(self):
        v = ir.Add(ir.Const(1), ir.Const(2), "add", ir.i32)


class IrBuilderTestCase(unittest.TestCase):
    def setUp(self):
        self.b = irutils.Builder()
        self.m = ir.Module('test')
        self.b.setModule(self.m)

    def testBuilder(self):
        f = self.b.new_function('add')
        self.b.setFunction(f)
        bb = self.b.newBlock()
        self.b.emit(ir.Jump(bb))
        self.b.setBlock(bb)
        self.b.emit(ir.Exp(ir.Const(0)))
        self.b.emit(ir.Jump(f.epiloog))
        # Run interpreter:
        # r = self.m.getFunction('add').call(1, 2)
        #self.assertEqual(3, r)


class PatternMatchTestCase(unittest.TestCase):
    @unittest.skip('Not yet implemented')
    def testSimpleTree(self):
        t = ir.Term('x')
        pat = ir.Binop(ir.Const(2), '+', t)
        res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat)
        self.assertTrue(res)
        self.assertIn(t, mp)
        self.assertEqual(3, mp[t])

    @unittest.skip('Not yet implemented')
    def testSimpleTree2(self):
        t = ir.Term('x')
        t2 = ir.Term('y')
        pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2))
        res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat)
        self.assertTrue(res)
        self.assertIn(t, mp)
        self.assertEqual(2, mp[t])
        self.assertIn(t2, mp)
        self.assertEqual(3, mp[t2])
        res, mp = ir.match_tree(ir.Const(2), pat)
        self.assertFalse(res)


class ConstantFolderTestCase(unittest.TestCase):
    def setUp(self):
        self.b = irutils.Builder()
        self.cf = ConstantFolder()
        self.m = ir.Module('test')
        self.b.setModule(self.m)

    def testBuilder(self):
        f = self.b.new_function('test')
        self.b.setFunction(f)
        bb = self.b.newBlock()
        self.b.emit(ir.Jump(bb))
        self.b.setBlock(bb)
        v1 = ir.Const(5)
        # self.b.emit(v1)
        v2 = ir.Const(7)
        v3 = ir.Add(v1, v2, "add", ir.i32)
        # self.b.emit(v3)
        self.b.emit(ir.Jump(f.epiloog))
        self.cf.run(self.m)

    def testAdd0(self):
        f = self.b.new_function('test')
        self.b.setFunction(f)
        self.b.setBlock(self.b.newBlock())
        v1 = ir.Const(12)
        v3 = ir.Add(v1, ir.Const(0), "add", ir.i32)


class TestWriter(unittest.TestCase):
    def testWrite(self):
        writer = irutils.Writer()
        module = ir.Module('mod1')
        function = ir.Function('func1', module)
        f = io.StringIO()
        writer.write(module, f)
        #print(f.getvalue())
        f2 = io.StringIO(f.getvalue())
        reader = irutils.Reader()
        module2 = reader.read(f2)
        f = io.StringIO()
        writer.write(module2, f)
        #print(f.getvalue())


class TestReader(unittest.TestCase):
    def testAddExample(self):
        reader = irutils.Reader()
        with open('../examples/pi/add.pi') as f:
            m = reader.read(f)
            self.assertTrue(m)
            #print(m)


if __name__ == '__main__':
    unittest.main()
    sys.exit()