Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r69011:083be7f23e9b
Date: 2014-01-30 00:33 +0000
http://bitbucket.org/pypy/pypy/changeset/083be7f23e9b/

Log:    move some low-level stuff out of the annotator

diff --git a/rpython/annotator/binaryop.py b/rpython/annotator/binaryop.py
--- a/rpython/annotator/binaryop.py
+++ b/rpython/annotator/binaryop.py
@@ -14,7 +14,6 @@
     SomeLongFloat, SomeType, SomeConstantType, unionof, UnionError,
     read_can_only_throw, add_knowntypedata,
     merge_knowntypedata,)
-from rpython.rtyper.llannotation import SomeAddress, SomeTypedAddressAccess
 from rpython.annotator.bookkeeper import getbookkeeper
 from rpython.flowspace.model import Variable, Constant
 from rpython.flowspace.operation import op
@@ -827,50 +826,6 @@
             raise AnnotatorError('add on %r' % pbc)
         return s_ImpossibleValue
 
-# ____________________________________________________________
-# annotation of low-level types
-from rpython.rtyper.llannotation import (
-    SomePtr, ll_to_annotation, annotation_to_lltype, lltype_to_annotation)
-
-class __extend__(pairtype(SomePtr, SomePtr)):
-    def union((p1, p2)):
-        assert p1.ll_ptrtype == p2.ll_ptrtype,("mixing of incompatible pointer 
types: %r, %r" %
-                                               (p1.ll_ptrtype, p2.ll_ptrtype))
-        return SomePtr(p1.ll_ptrtype)
-
-class __extend__(pairtype(SomePtr, SomeInteger)):
-
-    def getitem((p, int1)):
-        example = p.ll_ptrtype._example()
-        try:
-            v = example[0]
-        except IndexError:
-            return None       # impossible value, e.g. FixedSizeArray(0)
-        return ll_to_annotation(v)
-    getitem.can_only_throw = []
-
-    def setitem((p, int1), s_value):   # just doing checking
-        example = p.ll_ptrtype._example()
-        if example[0] is not None:  # ignore Void s_value
-            v_lltype = annotation_to_lltype(s_value)
-            example[0] = v_lltype._defl()
-    setitem.can_only_throw = []
-
-class __extend__(pairtype(SomePtr, SomeObject)):
-    def union((p, obj)):
-        assert False, ("mixing pointer type %r with something else %r" % 
(p.ll_ptrtype, obj))
-
-    def getitem((p, obj)):
-        assert False,"ptr %r getitem index not an int: %r" % (p.ll_ptrtype, 
obj)
-
-    def setitem((p, obj), s_value):
-        assert False,"ptr %r setitem index not an int: %r" % (p.ll_ptrtype, 
obj)
-
-class __extend__(pairtype(SomeObject, SomePtr)):
-    def union((obj, p2)):
-        return pair(p2, obj).union()
-
-
 #_________________________________________
 # weakrefs
 
@@ -885,60 +840,3 @@
             if basedef is None:    # no common base class! complain...
                 return SomeObject()
         return SomeWeakRef(basedef)
-
-#_________________________________________
-# memory addresses
-
-class __extend__(pairtype(SomeAddress, SomeAddress)):
-    def union((s_addr1, s_addr2)):
-        return SomeAddress()
-
-    def sub((s_addr1, s_addr2)):
-        if s_addr1.is_null_address() and s_addr2.is_null_address():
-            return getbookkeeper().immutablevalue(0)
-        return SomeInteger()
-
-    def is_((s_addr1, s_addr2)):
-        assert False, "comparisons with is not supported by addresses"
-
-class __extend__(pairtype(SomeTypedAddressAccess, SomeTypedAddressAccess)):
-    def union((s_taa1, s_taa2)):
-        assert s_taa1.type == s_taa2.type
-        return s_taa1
-
-class __extend__(pairtype(SomeTypedAddressAccess, SomeInteger)):
-    def getitem((s_taa, s_int)):
-        return lltype_to_annotation(s_taa.type)
-    getitem.can_only_throw = []
-
-    def setitem((s_taa, s_int), s_value):
-        assert annotation_to_lltype(s_value) is s_taa.type
-    setitem.can_only_throw = []
-
-
-class __extend__(pairtype(SomeAddress, SomeInteger)):
-    def add((s_addr, s_int)):
-        return SomeAddress()
-
-    def sub((s_addr, s_int)):
-        return SomeAddress()
-
-class __extend__(pairtype(SomeAddress, SomeImpossibleValue)):
-    # need to override this specifically to hide the 'raise UnionError'
-    # of pairtype(SomeAddress, SomeObject).
-    def union((s_addr, s_imp)):
-        return s_addr
-
-class __extend__(pairtype(SomeImpossibleValue, SomeAddress)):
-    # need to override this specifically to hide the 'raise UnionError'
-    # of pairtype(SomeObject, SomeAddress).
-    def union((s_imp, s_addr)):
-        return s_addr
-
-class __extend__(pairtype(SomeAddress, SomeObject)):
-    def union((s_addr, s_obj)):
-        raise UnionError(s_addr, s_obj)
-
-class __extend__(pairtype(SomeObject, SomeAddress)):
-    def union((s_obj, s_addr)):
-        raise UnionError(s_obj, s_addr)
diff --git a/rpython/annotator/test/test_model.py 
b/rpython/annotator/test/test_model.py
--- a/rpython/annotator/test/test_model.py
+++ b/rpython/annotator/test/test_model.py
@@ -1,11 +1,8 @@
 import py
 
 from rpython.annotator.model import *
-from rpython.rtyper.llannotation import (
-    SomePtr, annotation_to_lltype, ll_to_annotation)
 from rpython.annotator.listdef import ListDef
 from rpython.translator.translator import TranslationContext
-from rpython.rtyper.typesystem import lltype
 
 
 listdef1 = ListDef(None, SomeTuple([SomeInteger(nonneg=True), SomeString()]))
@@ -102,73 +99,6 @@
     assert not s1.contains(s2)
     assert s1 != s2
 
-def test_ll_to_annotation():
-    s_z = ll_to_annotation(lltype.Signed._defl())
-    s_s = SomeInteger()
-    s_u = SomeInteger(nonneg=True, unsigned=True)
-    assert s_z.contains(s_s)
-    assert not s_z.contains(s_u)
-    s_uz = ll_to_annotation(lltype.Unsigned._defl())
-    assert s_uz.contains(s_u)
-    assert ll_to_annotation(lltype.Bool._defl()).contains(SomeBool())
-    assert ll_to_annotation(lltype.Char._defl()).contains(SomeChar())
-    S = lltype.GcStruct('s')
-    A = lltype.GcArray()
-    s_p = ll_to_annotation(lltype.malloc(S))
-    assert isinstance(s_p, SomePtr) and s_p.ll_ptrtype == lltype.Ptr(S)
-    s_p = ll_to_annotation(lltype.malloc(A, 0))
-    assert isinstance(s_p, SomePtr) and s_p.ll_ptrtype == lltype.Ptr(A)
-
-def test_annotation_to_lltype():
-    from rpython.rlib.rarithmetic import r_uint, r_singlefloat
-    s_i = SomeInteger()
-    s_pos = SomeInteger(nonneg=True)
-    s_1 = SomeInteger(nonneg=True); s_1.const = 1
-    s_m1 = SomeInteger(nonneg=False); s_m1.const = -1
-    s_u = SomeInteger(nonneg=True, unsigned=True);
-    s_u1 = SomeInteger(nonneg=True, unsigned=True);
-    s_u1.const = r_uint(1)
-    assert annotation_to_lltype(s_i) == lltype.Signed
-    assert annotation_to_lltype(s_pos) == lltype.Signed
-    assert annotation_to_lltype(s_1) == lltype.Signed
-    assert annotation_to_lltype(s_m1) == lltype.Signed
-    assert annotation_to_lltype(s_u) == lltype.Unsigned
-    assert annotation_to_lltype(s_u1) == lltype.Unsigned
-    assert annotation_to_lltype(SomeBool()) == lltype.Bool
-    assert annotation_to_lltype(SomeChar()) == lltype.Char
-    PS = lltype.Ptr(lltype.GcStruct('s'))
-    s_p = SomePtr(ll_ptrtype=PS)
-    assert annotation_to_lltype(s_p) == PS
-    py.test.raises(ValueError, "annotation_to_lltype(si0)")
-    s_singlefloat = SomeSingleFloat()
-    s_singlefloat.const = r_singlefloat(0.0)
-    assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
-
-def test_ll_union():
-    PS1 = lltype.Ptr(lltype.GcStruct('s'))
-    PS2 = lltype.Ptr(lltype.GcStruct('s'))
-    PS3 = lltype.Ptr(lltype.GcStruct('s3'))
-    PA1 = lltype.Ptr(lltype.GcArray())
-    PA2 = lltype.Ptr(lltype.GcArray())
-
-    assert unionof(SomePtr(PS1),SomePtr(PS1)) == SomePtr(PS1)
-    assert unionof(SomePtr(PS1),SomePtr(PS2)) == SomePtr(PS2)
-    assert unionof(SomePtr(PS1),SomePtr(PS2)) == SomePtr(PS1)
-
-    assert unionof(SomePtr(PA1),SomePtr(PA1)) == SomePtr(PA1)
-    assert unionof(SomePtr(PA1),SomePtr(PA2)) == SomePtr(PA2)
-    assert unionof(SomePtr(PA1),SomePtr(PA2)) == SomePtr(PA1)
-
-    assert unionof(SomePtr(PS1),SomeImpossibleValue()) == SomePtr(PS1)
-    assert unionof(SomeImpossibleValue(), SomePtr(PS1)) == SomePtr(PS1)
-
-    py.test.raises(AssertionError, "unionof(SomePtr(PA1), SomePtr(PS1))")
-    py.test.raises(AssertionError, "unionof(SomePtr(PS1), SomePtr(PS3))")
-    py.test.raises(AssertionError, "unionof(SomePtr(PS1), SomeInteger())")
-    py.test.raises(AssertionError, "unionof(SomePtr(PS1), SomeObject())")
-    py.test.raises(AssertionError, "unionof(SomeInteger(), SomePtr(PS1))")
-    py.test.raises(AssertionError, "unionof(SomeObject(), SomePtr(PS1))")
-
 def test_nan():
     f1 = SomeFloat()
     f1.const = float("nan")
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -12,7 +12,6 @@
     SomePBC, SomeType, s_ImpossibleValue,
     s_Bool, s_None, unionof, add_knowntypedata,
     HarmlesslyBlocked, SomeWeakRef, SomeUnicodeString, SomeByteArray)
-from rpython.rtyper.llannotation import SomeAddress, SomeTypedAddressAccess
 from rpython.annotator.bookkeeper import getbookkeeper
 from rpython.annotator import builtin
 from rpython.annotator.binaryop import _clone ## XXX where to put this?
@@ -824,20 +823,3 @@
             return s_None   # known to be a dead weakref
         else:
             return SomeInstance(self.classdef, can_be_None=True)
-
-#_________________________________________
-# memory addresses
-
-from rpython.rtyper.lltypesystem import llmemory
-
-class __extend__(SomeAddress):
-    def getattr(self, s_attr):
-        assert s_attr.is_constant()
-        assert isinstance(s_attr, SomeString)
-        assert s_attr.const in llmemory.supported_access_types
-        return SomeTypedAddressAccess(
-            llmemory.supported_access_types[s_attr.const])
-    getattr.can_only_throw = []
-
-    def bool(self):
-        return s_Bool
diff --git a/rpython/rtyper/llannotation.py b/rpython/rtyper/llannotation.py
--- a/rpython/rtyper/llannotation.py
+++ b/rpython/rtyper/llannotation.py
@@ -1,9 +1,11 @@
 """
 Code for annotating low-level thingies.
 """
+from rpython.tool.pairtype import pair, pairtype
 from rpython.annotator.model import (
     SomeObject, SomeSingleFloat, SomeFloat, SomeLongFloat, SomeChar,
-    SomeUnicodeCodePoint, SomeInteger, s_None, s_Bool)
+    SomeUnicodeCodePoint, SomeInteger, SomeString, SomeImpossibleValue,
+    s_None, s_Bool, UnionError, AnnotatorError)
 from rpython.rtyper.lltypesystem import lltype, llmemory
 
 class SomeAddress(SomeObject):
@@ -15,6 +17,17 @@
     def is_null_address(self):
         return self.is_immutable_constant() and not self.const
 
+    def getattr(self, s_attr):
+        assert s_attr.is_constant()
+        assert isinstance(s_attr, SomeString)
+        assert s_attr.const in llmemory.supported_access_types
+        return SomeTypedAddressAccess(
+            llmemory.supported_access_types[s_attr.const])
+    getattr.can_only_throw = []
+
+    def bool(self):
+        return s_Bool
+
 class SomeTypedAddressAccess(SomeObject):
     """This class is used to annotate the intermediate value that
     appears in expressions of the form:
@@ -27,6 +40,63 @@
     def can_be_none(self):
         return False
 
+
+class __extend__(pairtype(SomeAddress, SomeAddress)):
+    def union((s_addr1, s_addr2)):
+        return SomeAddress()
+
+    def sub((s_addr1, s_addr2)):
+        from rpython.annotator.bookkeeper import getbookkeeper
+        if s_addr1.is_null_address() and s_addr2.is_null_address():
+            return getbookkeeper().immutablevalue(0)
+        return SomeInteger()
+
+    def is_((s_addr1, s_addr2)):
+        assert False, "comparisons with is not supported by addresses"
+
+class __extend__(pairtype(SomeTypedAddressAccess, SomeTypedAddressAccess)):
+    def union((s_taa1, s_taa2)):
+        assert s_taa1.type == s_taa2.type
+        return s_taa1
+
+class __extend__(pairtype(SomeTypedAddressAccess, SomeInteger)):
+    def getitem((s_taa, s_int)):
+        return lltype_to_annotation(s_taa.type)
+    getitem.can_only_throw = []
+
+    def setitem((s_taa, s_int), s_value):
+        assert annotation_to_lltype(s_value) is s_taa.type
+    setitem.can_only_throw = []
+
+
+class __extend__(pairtype(SomeAddress, SomeInteger)):
+    def add((s_addr, s_int)):
+        return SomeAddress()
+
+    def sub((s_addr, s_int)):
+        return SomeAddress()
+
+class __extend__(pairtype(SomeAddress, SomeImpossibleValue)):
+    # need to override this specifically to hide the 'raise UnionError'
+    # of pairtype(SomeAddress, SomeObject).
+    def union((s_addr, s_imp)):
+        return s_addr
+
+class __extend__(pairtype(SomeImpossibleValue, SomeAddress)):
+    # need to override this specifically to hide the 'raise UnionError'
+    # of pairtype(SomeObject, SomeAddress).
+    def union((s_imp, s_addr)):
+        return s_addr
+
+class __extend__(pairtype(SomeAddress, SomeObject)):
+    def union((s_addr, s_obj)):
+        raise UnionError(s_addr, s_obj)
+
+class __extend__(pairtype(SomeObject, SomeAddress)):
+    def union((s_obj, s_addr)):
+        raise UnionError(s_obj, s_addr)
+
+
 class SomePtr(SomeObject):
     knowntype = lltype._ptr
     immutable = True
@@ -55,6 +125,46 @@
     def can_be_none(self):
         return False
 
+class __extend__(pairtype(SomePtr, SomePtr)):
+    def union((p1, p2)):
+        if p1.ll_ptrtype != p2.ll_ptrtype:
+            raise UnionError(p1, p2)
+        return SomePtr(p1.ll_ptrtype)
+
+class __extend__(pairtype(SomePtr, SomeInteger)):
+
+    def getitem((p, int1)):
+        example = p.ll_ptrtype._example()
+        try:
+            v = example[0]
+        except IndexError:
+            return None       # impossible value, e.g. FixedSizeArray(0)
+        return ll_to_annotation(v)
+    getitem.can_only_throw = []
+
+    def setitem((p, int1), s_value):   # just doing checking
+        example = p.ll_ptrtype._example()
+        if example[0] is not None:  # ignore Void s_value
+            v_lltype = annotation_to_lltype(s_value)
+            example[0] = v_lltype._defl()
+    setitem.can_only_throw = []
+
+class __extend__(pairtype(SomePtr, SomeObject)):
+    def union((p, obj)):
+        raise UnionError(p, obj)
+
+    def getitem((p, obj)):
+        raise AnnotatorError("ptr %r getitem index not an int: %r" %
+                             (p.ll_ptrtype, obj))
+
+    def setitem((p, obj), s_value):
+        raise AnnotatorError("ptr %r setitem index not an int: %r" %
+                             (p.ll_ptrtype, obj))
+
+class __extend__(pairtype(SomeObject, SomePtr)):
+    def union((obj, p2)):
+        return pair(p2, obj).union()
+
 
 annotation_to_ll_map = [
     (SomeSingleFloat(), lltype.SingleFloat),
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to