view test/testir.py @ 301:6753763d3bec

merge codegen into ppci package
author Windel Bouwman
date Thu, 05 Dec 2013 17:02:38 +0100
parents 158068af716c
children e609d5296ee9
line wrap: on
line source

import unittest
import os
import sys
import ppci
from ppci import ir
from ppci.transform import ConstantFolder


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

    def testBuilder(self):
        f = self.b.newFunction('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))
        self.m.check()
        # Run interpreter:
        # r = self.m.getFunction('add').call(1, 2)
        #self.assertEqual(3, r)


class PatternMatchTestCase(unittest.TestCase):
    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])

    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 = ir.Builder()
        self.cf = ConstantFolder()
        self.m = ir.Module('test')
        self.b.setModule(self.m)

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

    def testAdd0(self):
        f = self.b.newFunction('test')
        self.b.setFunction(f)
        self.b.setBlock(self.b.newBlock())
        v1 = ir.Const(0)
        v2 = ir.Const(0)
        v3 = ir.Add(v1, v2)


testsrc = """
package test2;

function void tesssst(int henkie)
{
   var int a, b, cee;
   a = 2 * 33 - 12;
   b = a * 2 + 13;
   a = b + a;
   cee = a;
   cee = cee * 2 + a + cee * 2;
   if (cee + a > b and b *3 - a+8*b== 3*6-b)
   {
      var int x = a;
      x = b * 2 - a;
      a = x * x * (x + 22 - a);
   }
   else
   {
      a = b + a + (a + b);
   }
   var int y;
   y = a - b * 53;
}
"""

testsrc2 = """
function int add2(int x, int y)
{
   var int res;
   res = x + y + 2 -  7 + 2;
   //if (y < 2)
   //{
   //   return y - 33;
   //}

   res = res + (x + 2 * y) + (x + 2 * y) + (2*8) + (2*8);

   if (x > 13)
   {
      while (y > 1337)
      {
         res = res + 2;
         y = y - 12;
      }
   }
   return res;
}

"""

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