diff python/testc3.py @ 230:88a1e0baef65

Added some tests for IR-code
author Windel Bouwman
date Sat, 13 Jul 2013 19:53:44 +0200
parents 7f18ed9b6b7e
children 521567d17388
line wrap: on
line diff
--- a/python/testc3.py	Sat Jul 13 11:13:01 2013 +0200
+++ b/python/testc3.py	Sat Jul 13 19:53:44 2013 +0200
@@ -14,7 +14,7 @@
 
 const int A = 1337;
 
-function void test1() 
+function void test1()
 {
     var int bdd;
     var int a = 10;
@@ -112,6 +112,16 @@
         self.assertTrue(ircode)
         return ircode
 
+    def expectIR(self, snippet, ir_out):
+        ircode = self.builder.build(snippet)
+        if not ircode:
+            self.diag.printErrors(snippet)
+        self.assertTrue(ircode)
+        actual_ins = [str(i) for i in ircode.Instructions]
+        expected_ins = [i.strip() for i in ir_out.split('\n')]
+        self.assertSequenceEqual(expected_ins, actual_ins)
+        return ircode
+
     def testFunctArgs(self):
       snippet = """
          package testargs;
@@ -139,6 +149,39 @@
       """
       self.expectErrors(snippet, [8, 9, 10])
 
+    def testExpression1(self):
+      snippet = """
+         package testexpr1;
+         function void t()
+         {
+            var int a, b, c;
+            a = 1;
+            b = a * 2 + a * a;
+            c = b * a - 3;
+         }
+      """
+      block_code = """ a0 = alloc
+        b1 = alloc
+        c2 = alloc
+        t3 = 1
+        [a0] = t3
+        t4 = [a0]
+        t5 = 2
+        mul6 = t4 * t5
+        t7 = [a0]
+        t8 = [a0]
+        mul9 = t7 * t8
+        add10 = mul6 + mul9
+        [b1] = add10
+        t11 = [b1]
+        t12 = [a0]
+        mul13 = t11 * t12
+        t14 = 3
+        sub15 = mul13 - t14
+        [c2] = sub15
+      ret """
+      self.expectIR(snippet, block_code)
+
     def testEmpty(self):
       snippet = """
       package A
@@ -220,7 +263,53 @@
         """
         self.expectOK(snippet)
 
-   
+    def testLocalVariable(self):
+        snippet = """
+         package testlocalvar;
+         function void t()
+         {
+            var int a, b;
+            a = 2;
+            b = a + 2;
+         }
+        """
+        block_code = """a0 = alloc
+         b1 = alloc
+         t2 = 2
+         [a0] = t2
+         t3 = [a0]
+         t4 = 2
+         add5 = t3 + t4
+         [b1] = add5
+         ret  """
+        self.expectIR(snippet, block_code)
+
+    def testStruct1(self):
+        snippet = """
+         package teststruct1;
+         function void t()
+         {
+            var struct {int x, y;} a;
+            a.x = 2;
+            a.y = a.x + 2;
+         }
+        """
+        block_code = """a0 = alloc
+         t1 = 2
+         off_x2 = 0
+         adr_x3 = a0 + off_x2
+         [adr_x3] = t1
+         off_x4 = 0
+         adr_x5 = a0 + off_x4
+         t6 = [adr_x5]
+         t7 = 2
+         add8 = t6 + t7
+         off_y9 = 0
+         adr_y10 = a0 + off_y9
+         [adr_y10] = add8
+         ret  """
+        self.expectIR(snippet, block_code)
+
     def testPointerType1(self):
         snippet = """
          package testpointer1;
@@ -262,6 +351,71 @@
         """
         self.expectErrors(snippet, [6, 9, 10])
 
+    def testPointerTypeIr(self):
+        snippet = """
+         package testptr_ir;
+         function void t()
+         {
+            var int* a;
+            a = cast<int*>(40);
+            *a = 2;
+         }
+        """
+        block_code = """a0 = alloc
+         t1 = 40
+         [a0] = t1
+         t2 = 2
+         deref3 = [a0]
+         [deref3] = t2
+         ret  """
+        self.expectIR(snippet, block_code)
+
+    def testPointerTypeIr2(self):
+        snippet = """
+         package testptr_ir;
+         type struct {int x,y;}* gpio;
+         function void t()
+         {
+            var gpio a;
+            a = cast<gpio>(40);
+            a->x = 2;
+            a->y = a->x - 14;
+         }
+        """
+        block_code = """a0 = alloc
+         t1 = 40
+         [a0] = t1
+         t2 = 2
+         deref3 = [a0]
+         off_x4 = 0
+         adr_x5 = deref3 + off_x4
+         [adr_x5] = t2
+         deref6 = [a0]
+         off_x7 = 0
+         adr_x8 = deref6 + off_x7
+         t9 = [adr_x8]
+         t10 = 14
+         sub11 = t9 - t10
+         deref12 = [a0]
+         off_y13 = 0
+         adr_y14 = deref12 + off_y13
+         [adr_y14] = sub11
+         ret  """
+        self.expectIR(snippet, block_code)
+
+    def testWrongCast(self):
+        snippet = """
+         package testptr_ir;
+         type struct {int x,y;}* gpio;
+         function void t()
+         {
+            var gpio a;
+            *cast<gpio>(*a);
+         }
+        """
+        # TODO: remove the duplicate error:
+        self.expectErrors(snippet, [7, 7])
+
     def testComplexType(self):
         snippet = """
          package testpointer;