view python/testc3.py @ 170:4348da5ca307

Cleanup of ir dir
author Windel Bouwman
date Fri, 29 Mar 2013 17:33:17 +0100
parents ee0d30533dae
children 25a0753da4cf
line wrap: on
line source

import c3
import time, ppci, x86, ir
import unittest

testsrc = """package test;

var u32 c, d;
var double e;
var int f;

const int A = 1337;

function void test1() 
{
    var u32 bdd;
    var int a = 10;
    bd = 20;
    var int buf;
    var int i;
    i = 2;
    var int zero = i - 2;
    if (i > 1)
    {
       buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
    }
    else
    {
      ;;;
    }

    t2(2, 3);
}

function int t2(u32 a, u32 b)
{
   if (a > 0)
   {
      a = 2 + t2(a - 1, 1.0);
   }

   return a + b;
}

function int t3(int aap, int blah)
{
   if (a > blah and blah < 45 + 33 or 33 > aap or 6 and true)
   {
      a = 2 + t2(a - 1);
   }

   return a + b;
}

var int hahaa = 23 * 2;


"""

testsrc2 = """
package test2;

function void tst()
{
   var int a, b;
   a = 2 * 33 - 12;
   b = a * 2 + 13;
   a = b + a;
   if (a > b and b == 3)
   {
      var int x = a;
      x = b * 2 - a;
      a = x*x;
   }
   else
   {
      a = b + a;
   }
}

"""

def c3compile(src, diag):
   # Structures:
   builder = c3.Builder(diag)
   ir = builder.build(src)
   # optional optimize here
   x86gen = x86.X86CodeGen(diag)
   ok = len(diag.diags) == 0
   if not ok:
      return
   print('generating x86 code')
   x86gen.genBin(ir)
   with open('dummydummy.asm', 'w') as f:
      f.write('bits 64\n')
      for a in x86gen.asm:
         print(a)
         f.write(str(a) + '\n')
   
def do():
   diag = ppci.DiagnosticsManager()
   c3compile(testsrc, diag)

class testA(unittest.TestCase):
   def setUp(self):
      self.diag = ppci.DiagnosticsManager()
      self.builder = c3.Builder(self.diag)
   def testSrc(self):
      self.builder.build(testsrc)
   def testFunctArgs(self):
      snippet = """
         package testargs;
         function void t2(int a, double b)
         {
            t2(2, 2);
            t2(2);
            t2(1, 1.2);
         }
      """
      self.diag.clear()
      ir = self.builder.build(snippet)
      assert len(self.diag.diags) == 2
      assert self.diag.diags[0].loc.row == 5
      assert self.diag.diags[1].loc.row == 6

   def testExpressions(self):
      snippet = """
         package test;
         function void t(int a, double b)
         {
            var int a2;
            var bool c;

            a2 = b * a;
            c = a;
            c = b > 1;
         }
      """
      self.diag.clear()
      ir = self.builder.build(snippet)
      assert len(self.diag.diags) == 3
      assert self.diag.diags[0].loc.row == 8
      assert self.diag.diags[1].loc.row == 9
      assert self.diag.diags[2].loc.row == 10
      assert ir == None
   def testEmpty(self):
      snippet = """
      package A
      """
      self.builder.build(snippet)
   def testEmpty2(self):
      snippet = ""
      self.builder.build(snippet)
   def testRedefine(self):
      snippet = """
      package test;
      var int a;
      var int b;
      var int a;
      """
      self.diag.clear()
      self.builder.build(snippet)
      assert len(self.diag.diags) == 1
      assert self.diag.diags[0].loc.row == 5
   def testWhile(self):
      snippet = """
      package tstwhile;
      var int a;
      function void t()
      {
         var int i = 0;
         while (i < 1054)
         {
            i = i + 3;
            a = a + i
         }
      }
      """
      self.builder.build(snippet)
   def testIf(self):
      snippet = """
      package tstIFF;
      var int a;
      function void t(int b)
      {
         a = 2;
         if (a > b)
         {
            if (a > 1337)
            {
               b = 2;
            }
         }
         else
         {
            b = 1;
         }

         return b;
      }
      """
      self.builder.build(snippet)
   def test2(self):
      # testsrc2 is valid code:
      self.diag.clear()
      ir = self.builder.build(testsrc2)
      print(self.diag.diags)
      assert ir
      ir.dump()

if __name__ == '__main__':
   do()
   unittest.main()