[pypy-svn] r10410 - pypy/dist/pypy/annotation

arigo at codespeak.net arigo at codespeak.net
Thu Apr 7 19:26:40 CEST 2005


Author: arigo
Date: Thu Apr  7 19:26:40 2005
New Revision: 10410

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/unaryop.py
Log:

Completed the support for the various operations in the annotator.
There are a few obscure operations not supported, but well, they
shouldn't appear in R-Python flow graphs, I suppose.


Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Thu Apr  7 19:26:40 2005
@@ -20,12 +20,17 @@
 
 # XXX unify this with ObjSpace.MethodTable
 BINARY_OPERATIONS = set(['add', 'sub', 'mul', 'div', 'mod',
+                         'truediv', 'floordiv', 'divmod', 'pow',
                          'and_', 'or_', 'xor',
+                         'lshift', 'rshift',
                          'getitem', 'setitem',
-                         'inplace_add', 'inplace_sub',
-                         'lt', 'le', 'eq', 'ne', 'gt', 'ge', 'is_',
-                         'union',
-                         'lshift', 'rshift'
+                         'inplace_add', 'inplace_sub', 'inplace_mul',
+                         'inplace_truediv', 'inplace_floordiv', 'inplace_div',
+                         'inplace_mod', 'inplace_pow',
+                         'inplace_lshift', 'inplace_rshift',
+                         'inplace_and', 'inplace_or', 'inplace_xor',
+                         'lt', 'le', 'eq', 'ne', 'gt', 'ge', 'is_', 'cmp',
+                         'union', 'coerce',
                          ])
 
 for opname in BINARY_OPERATIONS:
@@ -50,11 +55,20 @@
             else:
                 return result
 
-    def inplace_add((obj1, obj2)):
-        return pair(obj1, obj2).add()   # default
-
-    def inplace_sub((obj1, obj2)):
-        return pair(obj1, obj2).sub()   # default
+    # inplace_xxx ---> xxx by default
+    def inplace_add((obj1, obj2)):      return pair(obj1, obj2).add()
+    def inplace_sub((obj1, obj2)):      return pair(obj1, obj2).sub()
+    def inplace_mul((obj1, obj2)):      return pair(obj1, obj2).mul()
+    def inplace_truediv((obj1, obj2)):  return pair(obj1, obj2).truediv()
+    def inplace_floordiv((obj1, obj2)): return pair(obj1, obj2).floordiv()
+    def inplace_div((obj1, obj2)):      return pair(obj1, obj2).div()
+    def inplace_mod((obj1, obj2)):      return pair(obj1, obj2).mod()
+    def inplace_pow((obj1, obj2), obj3):return pair(obj1, obj2).pow(obj3)
+    def inplace_lshift((obj1, obj2)):   return pair(obj1, obj2).lshift()
+    def inplace_rshift((obj1, obj2)):   return pair(obj1, obj2).rshift()
+    def inplace_and((obj1, obj2)):      return pair(obj1, obj2).and_()
+    def inplace_or((obj1, obj2)):       return pair(obj1, obj2).or_()
+    def inplace_xor((obj1, obj2)):      return pair(obj1, obj2).xor()
 
     def lt((obj1, obj2)):
         if obj1.is_constant() and obj2.is_constant():
@@ -92,6 +106,12 @@
         else:
             return SomeBool()
 
+    def cmp((obj1, obj2)):
+        if obj1.is_constant() and obj2.is_constant():
+            return immutablevalue(cmp(obj1.const, obj2.const))
+        else:
+            return SomeInteger()
+
     def is_((obj1, obj2)):
         # XXX assumption: for "X is Y" we for simplification 
         #     assume that X is possibly variable and Y constant 
@@ -122,6 +142,12 @@
             r.knowntypedata = ([op.args[0]], obj2)
         return r
 
+    def divmod((obj1, obj2)):
+        return SomeTuple([pair(obj1, obj2).div(), pair(obj1, obj2).mod()])
+
+    def coerce((obj1, obj2)):
+        return pair(obj1, obj2).union()   # reasonable enough
+
 class __extend__(pairtype(SomeInteger, SomeInteger)):
     # unsignedness is considered a rare and contagious disease
 
@@ -129,7 +155,10 @@
         return SomeInteger(nonneg = int1.nonneg and int2.nonneg,
                            unsigned = int1.unsigned or int2.unsigned)
 
-    add = mul = div = mod = or_ = xor = union
+    add = mul = div = floordiv = mod = or_ = xor = union
+
+    def truediv((int1, int2)):
+        return SomeFloat()
 
     def sub((int1, int2)):
         return SomeInteger(unsigned = int1.unsigned or int2.unsigned)
@@ -145,6 +174,9 @@
 
     rshift = lshift
 
+    def pow((int1, int2), obj3):
+        return SomeInteger()
+
 class __extend__(pairtype(SomeBool, SomeBool)):
 
     def union((boo1, boo2)):
@@ -179,7 +211,10 @@
     def union((flt1, flt2)):
         return SomeFloat()
 
-    add = sub = mul = div = mod = union
+    add = sub = mul = div = truediv = floordiv = mod = union
+
+    def pow((flt1, flt2), obj3):
+        return SomeFloat()
 
 
 class __extend__(pairtype(SomeList, SomeList)):
@@ -198,6 +233,9 @@
         pair(lst1, SomeInteger()).setitem(s_iter.next())
         return lst1
 
+    def inplace_mul((lst1, obj2)):
+        return lst1
+
 
 class __extend__(pairtype(SomeTuple, SomeTuple)):
 

Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Thu Apr  7 19:26:40 2005
@@ -32,7 +32,7 @@
         return SomeObject()
 
 def builtin_int(s_obj):     # we can consider 'int' as a function
-    return SomeInteger()
+    return s_obj.int()
 
 def restricted_uint(s_obj):    # for r_uint
     return SomeInteger(nonneg=True, unsigned=True)
@@ -41,19 +41,13 @@
     return SomeChar()
 
 def builtin_ord(s_chr):
-    return SomeInteger(nonneg=True)
+    return s_chr.ord()
 
 def builtin_id(o):
     return SomeInteger()
 
-def builtin_hex(o):
-    return SomeString()
-
-def builtin_oct(o):
-    return SomeString()
-
 def builtin_abs(o):
-    return o.__class__()
+    return o.abs()
 
 def builtin_divmod(o1, o2):
     return SomeTuple([SomeObject(), SomeObject()])    # XXX
@@ -62,10 +56,10 @@
     return SomeString() 
 
 def builtin_float(s_obj): 
-    return SomeFloat()
+    return s_obj.float()
 
-def builtin_long(s_str): 
-    return SomeObject() 
+def builtin_long(s_obj): 
+    return s_obj.long()
 
 def our_issubclass(cls1, cls2):
     """ we're going to try to be less silly in the face of old-style classes"""
@@ -159,10 +153,16 @@
     return r
 
 def builtin_str(s_obj):
-    return SomeString()
+    return s_obj.str()
 
 def builtin_repr(s_obj):
-    return SomeString()
+    return s_obj.repr()
+
+def builtin_hex(s_obj):
+    return s_obj.hex()
+
+def builtin_oct(s_obj):
+    return s_obj.oct()
 
 def builtin_list(s_iterable):
     factory = getbookkeeper().getfactory(ListFactory)
@@ -206,7 +206,7 @@
     s_self.setattr(immutablevalue('args'), SomeTuple(args))
 
 def builtin_bool(s_obj):
-    return SomeBool()
+    return s_obj.is_true()
 
 def count(s_obj):
     return SomeInteger()

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Thu Apr  7 19:26:40 2005
@@ -8,21 +8,23 @@
 from pypy.annotation.model import SomeObject, SomeInteger, SomeBool
 from pypy.annotation.model import SomeString, SomeChar, SomeList, SomeDict
 from pypy.annotation.model import SomeTuple, SomeImpossibleValue
-from pypy.annotation.model import SomeInstance, SomeBuiltin 
+from pypy.annotation.model import SomeInstance, SomeBuiltin, SomeFloat
 from pypy.annotation.model import SomeIterator, SomePBC, new_or_old_class
 from pypy.annotation.model import unionof, set, setunion, missing_operation
 from pypy.annotation.factory import BlockedInference, generalize, ListFactory
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.annotation.classdef import isclassdef
-from pypy.annotation.builtin import builtin_issubclass
+from pypy.annotation import builtin
 
 # convenience only!
 def immutablevalue(x):
     return getbookkeeper().immutablevalue(x)
 
 UNARY_OPERATIONS = set(['len', 'is_true', 'getattr', 'setattr',
-                        'simple_call', 'call_args',
-                        'iter', 'next', 'invert', 'type', 'issubtype'])
+                        'simple_call', 'call_args', 'str', 'repr',
+                        'iter', 'next', 'invert', 'type', 'issubtype',
+                        'pos', 'neg', 'nonzero', 'abs', 'hex', 'oct',
+                        'ord', 'int', 'float', 'long']) 
 
 for opname in UNARY_OPERATIONS:
     missing_operation(SomeObject, opname)
@@ -30,7 +32,7 @@
 
 class __extend__(SomeObject):
 
-    def type(obj):
+    def type(obj):  # XXX this should be unified with builtin_type
         if obj.is_constant():
             r = immutablevalue(obj.knowntype)
         else:
@@ -39,14 +41,14 @@
         fn, block, i = bk.position_key
         annotator = bk.annotator
         op = block.operations[i]
-        assert op.opname == "type" 
+        assert op.opname == "type"
         assert len(op.args) == 1
         assert annotator.binding(op.args[0]) == obj
         r.is_type_of = [op.args[0]]
         return r
 
     def issubtype(obj, s_cls):
-        return builtin_issubclass(obj, s_cls)
+        return builtin.builtin_issubclass(obj, s_cls)
 
     def len(obj):
         return SomeInteger(nonneg=True)
@@ -61,6 +63,23 @@
             else:
                 return SomeBool()
 
+    def nonzero(obj):
+        return obj.is_true()
+
+    def str(obj):
+        return SomeString()
+
+    repr = hex = oct = str
+
+    def int(obj):
+        return SomeInteger()
+
+    def float(obj):
+        return SomeFloat()
+
+    def long(obj):
+        return SomeObject()   # XXX
+
     def find_method(obj, name):
         "Look for a special-case implementation for the named method."
         analyser = getattr(obj.__class__, 'method_' + name)
@@ -103,11 +122,34 @@
             return SomeInteger(unsigned=True)
         return SomeInteger()
 
+    def pos(self):
+        return self
+
+    int = pos
+
+    def neg(self):
+        return SomeInteger()
+
+    def abs(self):
+        return SomeInteger(nonneg=True)
+
 
 class __extend__(SomeBool):
     def is_true(self):
         return self
 
+
+class __extend__(SomeFloat):
+
+    def pos(flt):
+        return flt
+
+    def neg(flt):
+        return SomeFloat()
+
+    abs = neg
+
+
 class __extend__(SomeTuple):
 
     def len(tup):
@@ -173,6 +215,9 @@
     def iter(str):
         return SomeIterator(SomeChar())
 
+    def ord(str):
+        return SomeInteger(nonneg=True)
+
 
 class __extend__(SomeChar):
 



More information about the Pypy-commit mailing list