changeset 72:7d0c4cd0926a

Check if addition does not overflow.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Wed, 27 May 2009 14:59:22 +0200
parents a34b7936cf7f
children 1ed5f9156fe3
files pysmcl/range_analysis.py pysmcl/test/unit/test_rangeanalysis.py
diffstat 2 files changed, 39 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/range_analysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/range_analysis.py	Wed May 27 14:59:22 2009 +0200
@@ -152,12 +152,17 @@
         right = self.visit(node.right)
 
         if isinstance(node.op, ast.Add):
-            r0 = left[0] + right[0]
-            r1 = left[1] + right[1]
-            if r0 >= self.prime:
-                r0 = Bottom()
-            if r1 >= self.prime:
-                r1 = Bottom()
+            def liftAdd(a, b):
+                if (isinstance(a, Bottom) or
+                    isinstance(b, Bottom)):
+                    return Bottom()
+                c = a + b
+                if c > self.prime:
+                    return Bottom()
+                return c
+            
+            r0 = liftAdd(left[0], right[0])
+            r1 = liftAdd(left[1], right[1])
             return (r0, r1)
 
         if isinstance(node.op, ast.Sub):
--- a/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
@@ -55,6 +55,34 @@
         r = prog.body[0].body[2].out_values["range"]
         self.assertEquals(r, {'y': (2, 2), 'x': (6, 6), 'z': (Bottom(), Bottom())})
 
+    def test_range_add_bottom(self):
+        p = 7
+        prog = parse("def f(x):\n\tx=6\n\ty=8\n\tz=x+y\n")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[2].out_values["range"]
+        self.assertEquals(r, {'y': (Bottom(), Bottom()), 'x': (6, 6), 'z': (Bottom(), Bottom())})
+
+    def test_range_add_top_bottom(self):
+        p = 7
+        prog = parse(
+"""
+def f(x):
+    x=1
+    y=4
+    if x:
+         y=y+3
+    y=y+1
+    z=x+y
+""")
+        init_statements(prog)
+        range_analysis = RangeAnalysis(p)
+        range_analysis.apply(prog.body[0])
+        r = prog.body[0].body[-1].out_values["range"]
+        self.assertEquals(r, {'x': (1, 1), 'y': (5, Bottom()), 'z': (6, Bottom())})
+
+
     def test_range_mul(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x*y\n")