[pypy-svn] r27049 - in pypy/dist/pypy: annotation rpython rpython/rctypes/test translator/stackless

ac at codespeak.net ac at codespeak.net
Wed May 10 14:22:23 CEST 2006


Author: ac
Date: Wed May 10 14:22:20 2006
New Revision: 27049

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/bookkeeper.py
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/rpython/rarithmetic.py
   pypy/dist/pypy/rpython/rctypes/test/test_rcarithmetic.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/translator/stackless/transform.py
Log:
(pedronis, arre) Remove the size attribute of SomeInteger in preparation to support more integer types.

Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Wed May 10 14:22:20 2006
@@ -20,7 +20,7 @@
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.objspace.flow.model import Variable
 from pypy.annotation.listdef import ListDef
-from pypy.rpython import extregistry
+from pypy.rpython import extregistry, rarithmetic
 
 # convenience only!
 def immutablevalue(x):
@@ -225,10 +225,9 @@
     # unsignedness is considered a rare and contagious disease
 
     def union((int1, int2)):
-        unsigned = int1.unsigned or int2.unsigned
-        return SomeInteger(nonneg = unsigned or (int1.nonneg and int2.nonneg),
-                           unsigned=unsigned,
-                           size = max(int1.size, int2.size))
+        knowntype = rarithmetic.compute_restype(int1.knowntype, int2.knowntype)
+        return SomeInteger(nonneg=int1.nonneg and int2.nonneg,
+                           knowntype=knowntype)
 
     or_ = xor = add = mul = _clone(union, [])
     add_ovf = mul_ovf = _clone(union, [OverflowError])
@@ -241,33 +240,28 @@
     truediv_ovf = _clone(truediv, [ZeroDivisionError, OverflowError])
 
     def sub((int1, int2)):
-        return SomeInteger(unsigned = int1.unsigned or int2.unsigned,
-                           size = max(int1.size, int2.size))
+        knowntype = rarithmetic.compute_restype(int1.knowntype, int2.knowntype)
+        return SomeInteger(knowntype=knowntype)
     sub.can_only_throw = []
     sub_ovf = _clone(sub, [OverflowError])
 
     def and_((int1, int2)):
-        unsigned = int1.unsigned or int2.unsigned
-        return SomeInteger(nonneg = unsigned or int1.nonneg or int2.nonneg,
-                           unsigned = unsigned,
-                           size = max(int1.size, int2.size))
+        knowntype = rarithmetic.compute_restype(int1.knowntype, int2.knowntype)
+        return SomeInteger(nonneg=int1.nonneg and int2.nonneg,
+                           knowntype=knowntype)
     and_.can_only_throw = []
 
     def lshift((int1, int2)):
-        if int1.unsigned:
-            return SomeInteger(unsigned=True)
-        return SomeInteger(nonneg = int1.nonneg,
-                           size = max(int1.size, int2.size))
+        return SomeInteger(knowntype=int1.knowntype)
+
     lshift.can_only_throw = [ValueError]
     rshift = lshift
     lshift_ovf = _clone(lshift, [ValueError, OverflowError])
 
     def pow((int1, int2), obj3):
-        if int1.unsigned or int2.unsigned or getattr(obj3, 'unsigned', False):
-            return SomeInteger(unsigned=True,
-                               size = max(int1.size, int2.size))
+        knowntype = rarithmetic.compute_restype(int1.knowntype, int2.knowntype)
         return SomeInteger(nonneg = int1.nonneg,
-                           size = max(int1.size, int2.size))
+                           knowntype=knowntype)
     pow.can_only_throw = [ZeroDivisionError]
     pow_ovf = _clone(pow, [ZeroDivisionError, OverflowError])
 

Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py	(original)
+++ pypy/dist/pypy/annotation/bookkeeper.py	Wed May 10 14:22:20 2006
@@ -315,9 +315,9 @@
         elif tp is r_uint:
             result = SomeInteger(nonneg = True, unsigned = True)
         elif tp is r_ulonglong:
-            result = SomeInteger(nonneg = True, unsigned = True, size = 2)
+            result = SomeInteger(nonneg = True, knowntype=r_ulonglong)
         elif tp is r_longlong:
-            result = SomeInteger(nonneg = x>=0, size = 2)
+            result = SomeInteger(nonneg = x>=0, knowntype=r_longlong)
         elif issubclass(tp, str): # py.lib uses annotated str subclasses
             if len(x) == 1:
                 result = SomeChar()
@@ -503,11 +503,11 @@
         elif t is int or t is r_int:
             return SomeInteger()
         elif t is r_uint:
-            return SomeInteger(nonneg = True, unsigned = True)
+            return SomeInteger(unsigned = True)
         elif t is r_ulonglong or t is base_int:
-            return SomeInteger(nonneg = True, unsigned = True, size = 2)
+            return SomeInteger(knowntype=r_ulonglong)
         elif t is r_longlong:
-            return SomeInteger(size = 2)
+            return SomeInteger(knowntype=r_longlong)
         elif issubclass(t, str): # py.lib uses annotated str subclasses
             return SomeString()
         elif t is float:

Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Wed May 10 14:22:20 2006
@@ -87,11 +87,11 @@
 
 def restricted_longlong(s_obj):    # for r_uint
     return constpropagate(pypy.rpython.rarithmetic.r_longlong, [s_obj],
-                          SomeInteger(size=2))
+                          SomeInteger(knowntype=pypy.rpython.rarithmetic.r_longlong))
 
 def restricted_ulonglong(s_obj):    # for r_uint
     return constpropagate(pypy.rpython.rarithmetic.r_ulonglong, [s_obj],
-                          SomeInteger(size=2, nonneg=True, unsigned=True))
+                          SomeInteger(knowntype=pypy.rpython.rarithmetic.r_ulonglong))
 
 def restricted_base_int(s_obj):
     # insane hack: only for isinstance(., base_int), not for base_int()

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Wed May 10 14:22:20 2006
@@ -147,34 +147,27 @@
     "Stands for an object which is known to be an integer."
     knowntype = int
     # size is in multiples of C's sizeof(long)!
-    def __init__(self, nonneg=False, unsigned=False, size=1):
-        self.nonneg = unsigned or nonneg
-        self.unsigned = unsigned  # pypy.rpython.rarithmetic.r_uint
+    def __init__(self, nonneg=False, unsigned=None, knowntype=None):
+        
         if maxint != 2**31-1:
             size = 1    #XXX don't support longlong on 64 bits systems
-        self.size = size
-        if self.unsigned:
-            if self.size == 2:
-                self.knowntype = r_ulonglong
-            else:
-                self.knowntype = r_uint
-        else:
-            if self.size == 2:
-                self.knowntype = r_longlong
+        if knowntype is None:
+            if unsigned:
+                knowntype = r_uint
             else:
-                self.knowntype = int
-
-    def fmt_size(self, s):
-        if s != 1:
-            return str(s)
-
+                knowntype = int
+        elif unsigned is not None:
+            raise TypeError('Conflicting specification for SomeInteger')
+        self.knowntype = knowntype
+        unsigned = self.knowntype(-1) > 0
+        self.nonneg = unsigned or nonneg
+        self.unsigned = unsigned  # pypy.rpython.rarithmetic.r_uint
 
 class SomeBool(SomeInteger):
     "Stands for true or false."
     knowntype = bool
     nonneg = True
     unsigned = False
-    size = 1
     def __init__(self):
         pass
 
@@ -527,9 +520,9 @@
     (s_None, lltype.Void),   # also matches SomeImpossibleValue()
     (SomeBool(), lltype.Bool),
     (SomeInteger(), lltype.Signed),
-    (SomeInteger(size=2), lltype.SignedLongLong),    
-    (SomeInteger(nonneg=True, unsigned=True), lltype.Unsigned),    
-    (SomeInteger(nonneg=True, unsigned=True, size=2), lltype.UnsignedLongLong),    
+    (SomeInteger(knowntype=r_longlong), lltype.SignedLongLong),    
+    (SomeInteger(unsigned=True), lltype.Unsigned),    
+    (SomeInteger(knowntype=r_ulonglong), lltype.UnsignedLongLong),    
     (SomeFloat(), lltype.Float),
     (SomeChar(), lltype.Char),
     (SomeUnicodeCodePoint(), lltype.UniChar),

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Wed May 10 14:22:20 2006
@@ -198,9 +198,7 @@
 class __extend__(SomeInteger):
 
     def invert(self):
-        if self.unsigned:
-            return SomeInteger(unsigned=True, size=self.size)
-        return SomeInteger(size=self.size)
+        return SomeInteger(knowntype=self.knowntype)
 
     invert.can_only_throw = []
 
@@ -213,17 +211,13 @@
     # these are the only ones which can overflow:
 
     def neg(self):
-        if self.unsigned:
-            return SomeInteger(unsigned=True, size=self.size)
-        return SomeInteger(size=self.size)
+        return SomeInteger(knowntype=self.knowntype)
 
     neg.can_only_throw = []
     neg_ovf = _clone(neg, [OverflowError])
 
     def abs(self):
-        if self.unsigned:
-            return self
-        return SomeInteger(nonneg=True, size=self.size)
+        return SomeInteger(nonneg=True, knowntype=self.knowntype)
 
     abs.can_only_throw = []
     abs_ovf = _clone(abs, [OverflowError])

Modified: pypy/dist/pypy/rpython/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rarithmetic.py	Wed May 10 14:22:20 2006
@@ -84,15 +84,11 @@
     raise OverflowError
 
 def compute_restype(self_type, other_type):
-    if other_type in (int, long):
+    if other_type in (bool, int, long):
         return self_type
-    if self_type.SIGNED != other_type.SIGNED:
-        raise TypeError('Can not mix %r and %r'%(self_type, other_type))
-    if self_type.BITS > other_type.BITS:
-        return self_type
-    if self_type.BITS < other_type.BITS:
+    if self_type in (bool, int, long):
         return other_type
-    return self_type
+    return build_int(None, self_type.SIGNED and other_type.SIGNED, max(self_type.BITS, other_type.BITS))
 
 
 class base_int(long):
@@ -107,7 +103,7 @@
         self_type = type(self)
         other_type = type(other)
         try:
-            return self.typemap[ self_type, other_type ](value)
+            return self.typemap[self_type, other_type](value)
         except KeyError:
             pass
         restype = compute_restype(self_type, other_type)
@@ -274,6 +270,8 @@
     else:
         int_type = unsigned_int
     mask = (1 << bits) - 1
+    if name is None:
+        raise TypeError('No predefined %sint%d'%(['u', ''][sign], bits))
     ret = _inttypes[sign, bits] = type(name, (int_type,), {'MASK': mask,
                                                            'BITS': bits})
     return ret

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rcarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rcarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rcarithmetic.py	Wed May 10 14:22:20 2006
@@ -14,8 +14,8 @@
     assert type(rcbyte(1) + rcshort(1)) is rcshort
     assert type(rcshort(1) + rcbyte(1)) is rcshort
 
-    py.test.raises(TypeError, 'rcubyte(1) + rcshort(1)')
-    py.test.raises(TypeError, 'rcshort(1) + rcubyte(1)')
+    assert type(rcubyte(1) + rcshort(1)) is rcushort
+    assert type(rcshort(1) + rcubyte(1)) is rcushort
 
     
 def test_typeof():

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Wed May 10 14:22:20 2006
@@ -12,23 +12,22 @@
 from pypy.rpython.rmodel import log
 from pypy.rpython import objectmodel
 
-
 class __extend__(annmodel.SomeInteger):
     def rtyper_makerepr(self, rtyper):
-        if self.unsigned:
-            if self.size == 2:
-                return unsignedlonglong_repr
-            else:
-                assert self.size == 1
-                return unsigned_repr
-        else:
-            if self.size == 2:
-                return signedlonglong_repr
-            else:
-                assert self.size == 1
-                return signed_repr
+        if self.knowntype is int:
+            return signed_repr
+        if self.knowntype is bool:
+            return signed_repr
+        if self.knowntype is r_uint:
+            return unsigned_repr
+        if self.knowntype is r_longlong:
+            return signedlonglong_repr
+        if self.knowntype is r_ulonglong:
+            return unsignedlonglong_repr
+        raise TypeError('Can not build a repr for %r'%(self.knowntype,))
+
     def rtyper_makekey(self):
-        return self.__class__, self.unsigned, self.size
+        return self.__class__, self.knowntype
 
 signed_repr = IntegerRepr(Signed, 'int_')
 signedlonglong_repr = IntegerRepr(SignedLongLong, 'llong_')

Modified: pypy/dist/pypy/translator/stackless/transform.py
==============================================================================
--- pypy/dist/pypy/translator/stackless/transform.py	(original)
+++ pypy/dist/pypy/translator/stackless/transform.py	Wed May 10 14:22:20 2006
@@ -126,7 +126,7 @@
             lltype.Signed: mixlevelannotator.constfunc(
                 code.fetch_retval_long, [], annmodel.SomeInteger()),
             lltype.SignedLongLong: mixlevelannotator.constfunc(
-                code.fetch_retval_longlong, [], annmodel.SomeInteger(size=2)),
+                code.fetch_retval_longlong, [], annmodel.SomeInteger(knowntype=rarithmetic.r_longlong)),
             lltype.Float: mixlevelannotator.constfunc(
                 code.fetch_retval_float, [], annmodel.SomeFloat()),
             llmemory.Address: mixlevelannotator.constfunc(



More information about the Pypy-commit mailing list