[pypy-svn] r13039 - in pypy/branch/rpython-refactoring: . test

arigo at codespeak.net arigo at codespeak.net
Fri Jun 3 21:09:43 CEST 2005


Author: arigo
Date: Fri Jun  3 21:09:43 2005
New Revision: 13039

Added:
   pypy/branch/rpython-refactoring/rrange.py   (contents, props changed)
   pypy/branch/rpython-refactoring/test/test_rrange.py   (contents, props changed)
Removed:
   pypy/branch/rpython-refactoring/riter.py
Modified:
   pypy/branch/rpython-refactoring/rbuiltin.py
   pypy/branch/rpython-refactoring/rclass.py
   pypy/branch/rpython-refactoring/rlist.py
   pypy/branch/rpython-refactoring/rmodel.py
   pypy/branch/rpython-refactoring/rpbc.py
   pypy/branch/rpython-refactoring/rptr.py
   pypy/branch/rpython-refactoring/rstr.py
   pypy/branch/rpython-refactoring/rtyper.py
   pypy/branch/rpython-refactoring/test/test_rlist.py
Log:
Intermediate check-in.  New rrange.py.


Modified: pypy/branch/rpython-refactoring/rbuiltin.py
==============================================================================
--- pypy/branch/rpython-refactoring/rbuiltin.py	(original)
+++ pypy/branch/rpython-refactoring/rbuiltin.py	Fri Jun  3 21:09:43 2005
@@ -1,5 +1,5 @@
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomeBuiltin, SomeObject, SomeString
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 from pypy.rpython.lltype import malloc, typeOf, nullptr, nullgcptr
 from pypy.rpython.lltype import Void, Signed
 from pypy.rpython.rtyper import TyperError

Modified: pypy/branch/rpython-refactoring/rclass.py
==============================================================================
--- pypy/branch/rpython-refactoring/rclass.py	(original)
+++ pypy/branch/rpython-refactoring/rclass.py	Fri Jun  3 21:09:43 2005
@@ -1,5 +1,5 @@
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomePBC, SomeInstance
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 from pypy.rpython.lltype import *
 from pypy.rpython.rtyper import inputconst
 

Deleted: /pypy/branch/rpython-refactoring/riter.py
==============================================================================
--- /pypy/branch/rpython-refactoring/riter.py	Fri Jun  3 21:09:43 2005
+++ (empty file)
@@ -1,57 +0,0 @@
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomeIterator, SomeList
-from pypy.rpython.lltype import *
-
-
-class __extend__(SomeIterator):
-
-    def getiteratorkind(s_itr):
-        s_cont = s_itr.s_container
-        if isinstance(s_cont, SomeList):
-            if not s_cont.ll_range_step():
-                return PlainListIterator
-            else:
-                return RangeIterator
-        else:
-            raise TyperError("not implemented yet")
-
-    def lowleveltype(s_itr):
-        kind = s_itr.getiteratorkind()
-        return kind.lowlevelitertype(s_itr.s_container)
-
-    def rtype_next(s_itr, hop):
-        v_itr, = hop.inputargs(s_itr)
-        kind = s_itr.getiteratorkind()
-        return kind.next(v_itr, hop)
-
-# ____________________________________________________________
-
-class Namespace(object):
-    def __init__(self, name, bases, dict):
-        assert not bases
-        self.__dict__.update(dict)
-
-
-class PlainListIterator:
-    """__________ regular list iterator __________"""
-    __metaclass__ = Namespace
-
-    def lowlevelitertype(s_lst):
-        return GcPtr(GcStruct('listiter', ('list', s_lst.lowleveltype()),
-                                          ('index', Signed)))
-
-    def ll_listiter(ITERPTR, lst):
-        iter = malloc(ITERPTR.TO)
-        iter.list = lst
-        iter.index = 0
-        return iter
-
-    def rtype_new_iter(hop):
-        s_lst, = hop.args_s
-        v_lst, = hop.inputargs(s_lst)
-        ITERPTR = PlainListIterator.lowlevelitertype(s_lst)
-        citerptr = hop.inputconst(Void, ITERPTR)
-        return hop.gendirectcall(PlainListIterator.ll_listiter, citerptr, v_lst)
-
-    def next(v_itr, hop):
-        XXX - NotImplementedYet

Modified: pypy/branch/rpython-refactoring/rlist.py
==============================================================================
--- pypy/branch/rpython-refactoring/rlist.py	(original)
+++ pypy/branch/rpython-refactoring/rlist.py	Fri Jun  3 21:09:43 2005
@@ -1,7 +1,9 @@
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomeList, SomeInteger
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 from pypy.objspace.flow.model import Constant
 from pypy.rpython.lltype import *
+from pypy.rpython.rmodel import Repr, TyperError, IntegerRepr
+from pypy.rpython import rrange
 
 # ____________________________________________________________
 #
@@ -14,68 +16,54 @@
 #    'items' points to a C-like array in memory preceded by a 'length' header,
 #    where each item contains a primitive value or pointer to the actual list
 #    item.
-#
-#    Lists returned by range() and never mutated use a simpler implementation:
-#
-#    struct range {
-#        Signed start, stop;    // step is always constant
-#    }
-
-RANGE = GcStruct("range", ("start", Signed), ("stop", Signed))
-
 
-class __extend__(SomeList):
-
-    def ll_range_step(s_list):
-        return (not s_list.listdef.listitem.mutated
-                and s_list.listdef.listitem.range_step)
-
-    def lowleveltype(s_list):
-        if s_list.ll_range_step():
-            assert isinstance(s_list.get_s_items(), SomeInteger)
-            return GcPtr(RANGE)
-        else:
-            ITEM = s_list.get_s_items().lowleveltype()
-            LIST = GcStruct("list", ("items", GcPtr(GcArray(("item", ITEM)))))
-            return GcPtr(LIST)
-
-    def get_s_items(s_list):
-        return s_list.listdef.listitem.s_value
-
-    def rtype_len(s_lst, hop):
-        v_lst, = hop.inputargs(s_lst)
-        step = s_lst.ll_range_step()
-        if step:
-            cstep = hop.inputconst(Signed, step)
-            return hop.gendirectcall(ll_rangelen, v_lst, cstep)
+class __extend__(annmodel.SomeList):
+    def rtyper_makerepr(self, rtyper):
+        listitem = self.listdef.listitem
+        if listitem.range_step and not listitem.mutated:
+            return rrange.RangeRepr(listitem.range_step)
         else:
-            return hop.gendirectcall(ll_len, v_lst)
+            # cannot do the rtyper.getrepr() call immediately, for the case
+            # of recursive structures -- i.e. if the listdef contains itself
+            return ListRepr(lambda: rtyper.getrepr(listitem.s_value))
+
+
+class ListRepr(Repr):
+
+    def __init__(self, item_repr):
+        self.LIST = GcForwardReference()
+        self.lowleveltype = GcPtr(self.LIST)
+        self.item_repr = item_repr   # possibly uncomputed at this point!
+
+    def setup(self):
+        if callable(self.item_repr):
+            self.item_repr = self.item_repr()
+        if isinstance(self.LIST, GcForwardReference):
+            ITEM = self.item_repr.lowleveltype
+            ITEMARRAY = GcArray(("item", ITEM))
+            self.LIST.become(GcStruct("list", ("items", GcPtr(ITEMARRAY))))
+
+    def rtype_len(self, hop):
+        v_lst, = hop.inputargs(self)
+        return hop.gendirectcall(ll_len, v_lst)
 
-    def rtype_method_append(s_lst, hop):
-        assert not s_lst.ll_range_step()
-        v_lst, v_value = hop.inputargs(s_lst, s_lst.get_s_items())
+    def rtype_method_append(self, hop):
+        v_lst, v_value = hop.inputargs(self, self.item_repr)
         hop.gendirectcall(ll_append, v_lst, v_value)
 
-    def rtype_iter(s_lst):
-        s_itr = hop.s_result
-        return s_itr.getiteratorkind().rtype_new_iter(hop)
+    def make_iterator_repr(self):
+        return ListIteratorRepr(self)
 
 
-class __extend__(pairtype(SomeList, SomeInteger)):
-
-    def rtype_getitem((s_lst1, s_int2), hop):
-        v_lst, v_index = hop.inputargs(s_lst1, Signed)
-        step = s_lst1.ll_range_step()
-        if step:
-            cstep = hop.inputconst(Signed, step)
-            return hop.gendirectcall(ll_rangeitem, v_lst, v_index, cstep)
-        else:
-            if s_int2.nonneg:
-                llfn = ll_getitem_nonneg
-            else:
-                llfn = ll_getitem
-            return hop.gendirectcall(llfn, v_lst, v_index)
+class __extend__(pairtype(ListRepr, IntegerRepr)):
 
+    def rtype_getitem((r_lst, r_int), hop):
+        v_lst, v_index = hop.inputargs(r_lst, Signed)
+        if hop.args_s[1].nonneg:
+            llfn = ll_getitem_nonneg
+        else:
+            llfn = ll_getitem
+        return hop.gendirectcall(llfn, v_lst, v_index)
 
 # ____________________________________________________________
 #
@@ -112,28 +100,6 @@
 def ll_setitem_nonneg(l, i, newitem):
     l.items[i].item = newitem
 
-# __________ range __________
-
-def ll_rangelen(l, step):
-    if step > 0:
-        result = (l.stop - l.start + (step-1)) // step
-    else:
-        result = (l.start - l.stop - (step+1)) // (-step)
-    if result < 0:
-        result = 0
-    return result
-
-def ll_rangeitem(l, i, step):
-    if i<0:
-        # XXX ack. cannot call ll_rangelen() here for now :-(
-        if step > 0:
-            length = (l.stop - l.start + (step-1)) // step
-        else:
-            length = (l.start - l.stop - (step+1)) // (-step)
-        #assert length >= 0
-        i += length
-    return l.start + i*step
-
 # ____________________________________________________________
 #
 #  Irregular operations.
@@ -156,23 +122,37 @@
         hop.gendirectcall(ll_setitem_nonneg, v_result, ci, v_item)
     return v_result
 
-def ll_newrange(start, stop):
-    l = malloc(RANGE)
-    l.start = start
-    l.stop = stop
-    return l
+# ____________________________________________________________
+#
+#  Iteration.
+
+class ListIteratorRepr(Repr):
 
-def rtype_builtin_range(hop):
-    s_range = hop.s_result
-    step = s_range.listdef.listitem.range_step
-    if step is None:   # cannot build a RANGE object, needs a real list
-        raise TyperError("range() list used too dynamically")
-    if hop.nb_args == 1:
-        vstart = hop.inputconst(Signed, 0)
-        vstop, = hop.inputargs(Signed)
-    elif hop.nb_args == 2:
-        vstart, vstop = hop.inputargs(Signed, Signed)
-    else:
-        vstart, vstop, vstep = hop.inputargs(Signed, Signed, Signed)
-        assert isinstance(vstep, Constant) and vstep.value == step
-    return hop.gendirectcall(ll_newrange, vstart, vstop)
+    def __init__(self, r_list):
+        self.r_list = r_list
+        self.lowleveltype = GcPtr(GcStruct('listiter',
+                                           ('list', r_list.lowleveltype),
+                                           ('index', Signed)))
+
+    def newiter(self, hop):
+        v_lst, = hop.inputargs(self.r_list)
+        citerptr = hop.inputconst(Void, self.lowleveltype)
+        return hop.gendirectcall(ll_listiter, citerptr, v_lst)
+
+    def next(self, hop):
+        v_iter = hop.inputargs(self)
+        return hop.gendirectcall(ll_listnext, v_iter)
+
+def ll_listiter(ITERPTR, lst):
+    iter = malloc(ITERPTR.TO)
+    iter.list = lst
+    iter.index = 0
+    return iter
+
+def ll_listnext(iter):
+    l = iter.list
+    index = iter.index
+    if index >= len(l.items):
+        raise StopIteration
+    iter.index = index + 1
+    return l.items[index]

Modified: pypy/branch/rpython-refactoring/rmodel.py
==============================================================================
--- pypy/branch/rpython-refactoring/rmodel.py	(original)
+++ pypy/branch/rpython-refactoring/rmodel.py	Fri Jun  3 21:09:43 2005
@@ -17,6 +17,9 @@
     def __repr__(self):
         return '<%s %s>' % (self.__class__.__name__, self.lowleveltype)
 
+    def setup(self):
+        "For recursive data structure, which must be initialized in two steps."
+
     # default implementation of some operations
 
     def rtype_getattr(self, hop):
@@ -45,6 +48,19 @@
         else:
             return hop.genop('int_is_true', [vlen], resulttype=Bool)
 
+    def rtype_iter(self, hop):
+        r_iter = self.make_iterator_repr()
+        return r_iter.newiter(hop)
+
+    def make_iterator_repr(self):
+        raise TyperError("%s is not iterable" % (self,))
+
+class __extend__(annmodel.SomeIterator):
+    # NOTE: SomeIterator is for iterators over any container, not just list
+    def rtyper_makerepr(self, rtyper):
+        r_container = rtyper.getrepr(self.s_container)
+        return r_container.make_iterator_repr()
+
 # ____________________________________________________________
 
 class TyperError(Exception):

Modified: pypy/branch/rpython-refactoring/rpbc.py
==============================================================================
--- pypy/branch/rpython-refactoring/rpbc.py	(original)
+++ pypy/branch/rpython-refactoring/rpbc.py	Fri Jun  3 21:09:43 2005
@@ -1,6 +1,6 @@
 import types
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomePBC
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 from pypy.rpython.lltype import typeOf
 from pypy.rpython import rclass
 

Modified: pypy/branch/rpython-refactoring/rptr.py
==============================================================================
--- pypy/branch/rpython-refactoring/rptr.py	(original)
+++ pypy/branch/rpython-refactoring/rptr.py	Fri Jun  3 21:09:43 2005
@@ -1,5 +1,5 @@
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomePtr, SomeInteger
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 from pypy.rpython.lltype import ContainerType, Void, Signed, Bool
 
 

Added: pypy/branch/rpython-refactoring/rrange.py
==============================================================================
--- (empty file)
+++ pypy/branch/rpython-refactoring/rrange.py	Fri Jun  3 21:09:43 2005
@@ -0,0 +1,96 @@
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
+from pypy.rpython.rmodel import Repr, TyperError, IntegerRepr
+from pypy.rpython.lltype import *
+
+# ____________________________________________________________
+#
+#  Concrete implementation of RPython lists that are returned by range()
+#  and never mutated afterwards:
+#
+#    struct range {
+#        Signed start, stop;    // step is always constant
+#    }
+
+RANGE = GcPtr(GcStruct("range", ("start", Signed), ("stop", Signed)))
+
+class RangeRepr(Repr):
+    lowlevelrepr = RANGE
+
+    def __init__(self, step):
+        self.step = step
+
+    def rtype_len(self, hop):
+        v_rng, = hop.inputargs(self)
+        cstep = hop.inputconst(Signed, self.step)
+        return hop.gendirectcall(ll_rangelen, v_rng, cstep)
+
+    def make_iterator_repr(self):
+        return riter.RangeIteratorRepr(self)
+
+class __extend__(pairtype(RangeRepr, IntegerRepr)):
+
+    def rtype_getitem((r_rng, r_int), hop):
+        v_lst, v_index = hop.inputargs(r_lst, Signed)
+        cstep = hop.inputconst(Signed, self.step)
+        if hop.args_s[1].nonneg:
+            llfn = ll_rangeitem_nonneg
+        else:
+            llfn = ll_rangeitem
+        return hop.gendirectcall(llfn, v_lst, v_index, cstep)
+
+# ____________________________________________________________
+#
+#  Low-level methods.
+
+def ll_rangelen(l, step):
+    if step > 0:
+        result = (l.stop - l.start + (step-1)) // step
+    else:
+        result = (l.start - l.stop - (step+1)) // (-step)
+    if result < 0:
+        result = 0
+    return result
+
+def ll_rangeitem_nonneg(l, i, step):
+    return l.start + i*step
+
+def ll_rangeitem(l, i, step):
+    if i<0:
+        # XXX ack. cannot call ll_rangelen() here for now :-(
+        if step > 0:
+            length = (l.stop - l.start + (step-1)) // step
+        else:
+            length = (l.start - l.stop - (step+1)) // (-step)
+        #assert length >= 0
+        i += length
+    return l.start + i*step
+
+# ____________________________________________________________
+#
+#  Irregular operations.
+
+def ll_newrange(start, stop):
+    l = malloc(RANGE.TO)
+    l.start = start
+    l.stop = stop
+    return l
+
+def rtype_builtin_range(hop):
+    vstep = hop.inputconst(Signed, 1)
+    if hop.nb_args == 1:
+        vstart = hop.inputconst(Signed, 0)
+        vstop, = hop.inputargs(Signed)
+    elif hop.nb_args == 2:
+        vstart, vstop = hop.inputargs(Signed, Signed)
+    else:
+        vstart, vstop, vstep = hop.inputargs(Signed, Signed, Signed)
+        assert isinstance(vstep, Constant)
+
+    if isinstance(hop.r_result, RangeRepr):
+        return hop.gendirectcall(ll_newrange, vstart, vstop)
+    else:
+        # cannot build a RANGE object, needs a real list
+        raise TyperError("range() result used as a normal list: "
+                         "XXX not implemented")
+        #return hop.gendirectcall(ll_range2list, vstart, vstop, vstep)

Modified: pypy/branch/rpython-refactoring/rstr.py
==============================================================================
--- pypy/branch/rpython-refactoring/rstr.py	(original)
+++ pypy/branch/rpython-refactoring/rstr.py	Fri Jun  3 21:09:43 2005
@@ -1,5 +1,5 @@
-from pypy.annotation.pairtype import pair, pairtype
-from pypy.annotation.model import SomeString, SomeChar, SomeInteger, SomeObject
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation import model as annmodel
 from pypy.rpython.lltype import *
 
 # ____________________________________________________________

Modified: pypy/branch/rpython-refactoring/rtyper.py
==============================================================================
--- pypy/branch/rpython-refactoring/rtyper.py	(original)
+++ pypy/branch/rpython-refactoring/rtyper.py	Fri Jun  3 21:09:43 2005
@@ -54,6 +54,7 @@
                     "missing a GcPtr or NonGcPtr in the type specification "
                     "of %s:\n%r" % (s_obj, result.lowleveltype))
                 self.reprs_by_content[key] = result
+                result.setup()
             self.reprs_by_id[id(s_obj)] = result
             return result
 
@@ -393,7 +394,8 @@
 # _______________________________________________________________________
 # this has the side-effect of registering the unary and binary operations
 # and the rtyper_chooserepr() methods
-#from pypy.rpython import robject, rlist, rptr, rbuiltin, rint, rbool, rfloat
-#from pypy.rpython import rpbc, rstr, riter
-
-from pypy.rpython import robject, rint, rbool, rfloat
+from pypy.rpython import robject
+from pypy.rpython import rint, rbool, rfloat
+from pypy.rpython import rlist#, rstr
+#from pypy.rpython import rbuiltin, rpbc
+#from pypy.rpython import rptr

Modified: pypy/branch/rpython-refactoring/test/test_rlist.py
==============================================================================
--- pypy/branch/rpython-refactoring/test/test_rlist.py	(original)
+++ pypy/branch/rpython-refactoring/test/test_rlist.py	Fri Jun  3 21:09:43 2005
@@ -1,13 +1,14 @@
 from pypy.translator.translator import Translator
-from pypy.annotation.listdef import ListDef
 from pypy.rpython.lltype import *
 from pypy.rpython.rtyper import RPythonTyper
 from pypy.rpython.rlist import *
+from pypy.rpython.rint import signed_repr
 
 
 def test_rlist():
-    s = SomeList(ListDef(None, SomeInteger()))
-    l = ll_newlist(s.lowleveltype(), 3)
+    rlist = ListRepr(signed_repr)
+    rlist.setup()
+    l = ll_newlist(rlist.lowleveltype, 3)
     ll_setitem(l, 0, 42)
     ll_setitem(l, -2, 43)
     ll_setitem_nonneg(l, 2, 44)
@@ -18,23 +19,6 @@
     assert ll_getitem(l, 3) == 45
     assert ll_len(l) == 4
 
-def test_rlist_range():
-    def test1(start, stop, step):
-        expected = range(start, stop, step)
-        length = len(expected)
-        l = ll_newrange(start, stop)
-        assert ll_rangelen(l, step) == length
-        lst = [ll_rangeitem(l, i, step) for i in range(length)]
-        assert lst == expected
-        lst = [ll_rangeitem(l, i-length, step) for i in range(length)]
-        assert lst == expected
-
-    for start in (-10, 0, 1, 10):
-        for stop in (-8, 0, 4, 8, 25):
-            for step in (1, 2, 3, -1, -2):
-                test1(start, stop, step)
-
-
 # ____________________________________________________________
 
 def test_simple():

Added: pypy/branch/rpython-refactoring/test/test_rrange.py
==============================================================================
--- (empty file)
+++ pypy/branch/rpython-refactoring/test/test_rrange.py	Fri Jun  3 21:09:43 2005
@@ -0,0 +1,19 @@
+from pypy.rpython.rrange import *
+
+def test_rlist_range():
+    def test1(start, stop, step):
+        expected = range(start, stop, step)
+        length = len(expected)
+        l = ll_newrange(start, stop)
+        assert ll_rangelen(l, step) == length
+        lst = [ll_rangeitem(l, i, step) for i in range(length)]
+        assert lst == expected
+        lst = [ll_rangeitem_nonneg(l, i, step) for i in range(length)]
+        assert lst == expected
+        lst = [ll_rangeitem(l, i-length, step) for i in range(length)]
+        assert lst == expected
+
+    for start in (-10, 0, 1, 10):
+        for stop in (-8, 0, 4, 8, 25):
+            for step in (1, 2, 3, -1, -2):
+                test1(start, stop, step)



More information about the Pypy-commit mailing list