view python/testir.py @ 266:649884d9dc61 Working chain 1

Test
author Windel Bouwman
date Fri, 09 Aug 2013 16:49:29 +0200
parents 225f444019b1
children 5ec7580976d9
line wrap: on
line source

import unittest, os
import sys
import c3
import ppci
import ir, x86, transform
import optimize

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.newBB()
        self.b.setBB(bb)
        v1 = self.b.newTmp('t')
        self.b.addIns(ir.Return(v1))
        self.m.check()
        # Run interpreter:
        r = self.m.getFunction('add').call(1, 2)
        #self.assertEqual(3, r)

class ConstantFolderTestCase(unittest.TestCase):
    def setUp(self):
        self.b = ir.Builder()
        self.cf = transform.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.newBB()
        self.b.setBB(bb)
        v1 = self.b.newTmp('t')
        v2 = self.b.newTmp('t')
        v3 = self.b.newTmp('t')
        self.b.addIns(ir.ImmLoad(v1, 5))
        self.b.addIns(ir.ImmLoad(v2, 7))
        self.b.addIns(ir.BinaryOperator(v3, '+', v1, v2))
        self.assertEqual(3, len(self.m.Instructions))
        self.cf.run(self.m)
        self.assertEqual(3, len(self.m.Instructions))
        self.assertIsInstance(self.m.Instructions[-1], ir.ImmLoad)
        self.assertEqual(12, self.m.Instructions[-1].value)

    def testAdd0(self):
        f = self.b.newFunction('test')
        self.b.setFunction(f)
        bb = self.b.newBB()
        self.b.setBB(bb)
        v1 = self.b.newTmp('t')
        v2 = self.b.newTmp('t')
        v3 = self.b.newTmp('t')
        self.b.addIns(ir.ImmLoad(v2, 0))
        self.b.addIns(ir.BinaryOperator(v3, '+', 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()
    diag = ppci.DiagnosticsManager()
    builder = c3.Builder(diag)
    cgenx86 = x86.X86CodeGenSimple(diag)
    ir = builder.build(testsrc)
    diag.printErrors(testsrc)
    ir.check()
    ir.dump()
    optimize.optimize(ir)
    print('dump IR')
    print('dump IR')
    print('dump IR')
    print('dump IR')
    ir.dump()

    # Dump a graphiz file:
    with open('graaf.gv', 'w') as f:
      ir.dumpgv(f)
    os.system('dot -Tsvg -ograaf.svg graaf.gv')

    sys.exit()
    asm = cgenx86.genBin(ir)
    #for a in asm:
    #   print(a)
    with open('out.asm', 'w') as f:
      f.write('BITS 64\n')
      for a in asm:
         f.write(str(a) + '\n')
         print(a)