[pypy-svn] r27144 - in pypy/dist/pypy/annotation: . test

pedronis at codespeak.net pedronis at codespeak.net
Fri May 12 18:35:42 CEST 2006


Author: pedronis
Date: Fri May 12 18:35:40 2006
New Revision: 27144

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/test/test_annrpython.py
Log:
generalize integer comparison logic to cope with general bit sizes



Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Fri May 12 18:35:40 2006
@@ -193,7 +193,7 @@
 
             bind(obj2, obj1, 0)
             bind(obj1, obj2, 1)
-                
+
         return r
 
     def divmod((obj1, obj2)):
@@ -212,7 +212,7 @@
         else:
             return obj
 
-    
+
 # cloning a function with identical code, for the can_only_throw attribute
 def _clone(f, can_only_throw = None):
     newfunc = type(f)(f.func_code, f.func_globals, f.func_name,
@@ -279,14 +279,19 @@
         op = block.operations[i]
         assert op.opname == opname
         assert len(op.args) == 2
+        def tointtype(int0):
+            if int1.knowntype is bool:
+                return int
+            return int0.knowntype
         if int1.nonneg and isinstance(op.args[1], Variable):
             case = opname in ('lt', 'le', 'eq')
+                
             add_knowntypedata(knowntypedata, case, [op.args[1]],
-                              SomeInteger(nonneg=True, unsigned=int2.unsigned))
+                              SomeInteger(nonneg=True, knowntype=tointtype(int2)))
         if int2.nonneg and isinstance(op.args[0], Variable):
             case = opname in ('gt', 'ge', 'eq')
             add_knowntypedata(knowntypedata, case, [op.args[0]],
-                              SomeInteger(nonneg=True, unsigned=int1.unsigned))
+                              SomeInteger(nonneg=True, knowntype=tointtype(int1)))
         if knowntypedata:
             r.knowntypedata = knowntypedata
         return r

Modified: pypy/dist/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/annotation/test/test_annrpython.py	Fri May 12 18:35:40 2006
@@ -1303,6 +1303,25 @@
         s = a.build_types(f, [int]*8)
         assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True)] * 8)
 
+    def test_general_nonneg_cleverness(self):
+        def f(a, b, c, d, e, f, g, h):
+            if a < 0: a = 0
+            if b <= 0: b = 0
+            if c >= 0:
+                pass
+            else:
+                c = 0
+            if d < a: d = a
+            if e <= b: e = 1
+            if c > f: f = 2
+            if d >= g: g = 3
+            if h != a: h = 0
+            return a, b, c, d, e, f, g, h
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [r_longlong]*8)
+        assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True, knowntype=r_longlong)] * 8)
+
+
     def test_more_nonneg_cleverness(self):
         def f(start, stop):
             assert 0 <= start <= stop
@@ -1311,6 +1330,14 @@
         s = a.build_types(f, [int, int])
         assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True)] * 2)
 
+    def test_more_general_nonneg_cleverness(self):
+        def f(start, stop):
+            assert 0 <= start <= stop
+            return start, stop
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [r_longlong, r_longlong])
+        assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True, knowntype=r_longlong)] * 2)
+
     def test_nonneg_cleverness_is_gentle_with_unsigned(self):
         def witness1(x):
             pass
@@ -1328,7 +1355,23 @@
         assert a.binding(wg1.getargs()[0]).unsigned is True
         assert a.binding(wg2.getargs()[0]).unsigned is True        
         
-
+    def test_general_nonneg_cleverness_is_gentle_with_unsigned(self):
+        def witness1(x):
+            pass
+        def witness2(x):
+            pass        
+        def f(x):
+            if 0 < x:
+                witness1(x)
+            if x > 0:
+                witness2(x)
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [annmodel.SomeInteger(knowntype=r_ulonglong)])
+        wg1 = graphof(a, witness1)
+        wg2 = graphof(a, witness2)        
+        assert a.binding(wg1.getargs()[0]).knowntype is r_ulonglong
+        assert a.binding(wg2.getargs()[0]).knowntype is r_ulonglong
+    
     def test_attr_moving_into_parent(self):
         class A: pass
         class B(A): pass



More information about the Pypy-commit mailing list