[pypy-svn] r58936 - in pypy/branch/getslice/pypy: annotation objspace/flow/test rpython rpython/lltypesystem rpython/ootypesystem translator
arigo at codespeak.net
arigo at codespeak.net
Fri Oct 10 20:14:03 CEST 2008
Author: arigo
Date: Fri Oct 10 20:14:02 2008
New Revision: 58936
Modified:
pypy/branch/getslice/pypy/annotation/unaryop.py
pypy/branch/getslice/pypy/objspace/flow/test/test_objspace.py
pypy/branch/getslice/pypy/rpython/lltypesystem/rstr.py
pypy/branch/getslice/pypy/rpython/ootypesystem/rstr.py
pypy/branch/getslice/pypy/rpython/rlist.py
pypy/branch/getslice/pypy/rpython/rstr.py
pypy/branch/getslice/pypy/rpython/rtyper.py
pypy/branch/getslice/pypy/translator/transform.py
Log:
(antocuni, arigo)
More in-progress stuff.
Modified: pypy/branch/getslice/pypy/annotation/unaryop.py
==============================================================================
--- pypy/branch/getslice/pypy/annotation/unaryop.py (original)
+++ pypy/branch/getslice/pypy/annotation/unaryop.py Fri Oct 10 20:14:02 2008
@@ -290,9 +290,11 @@
def method_extend(lst, s_iterable):
lst.listdef.resize()
- if not isinstance(s_iterable, SomeList):
- raise Exception("list.extend(x): x must be a list")
- lst.listdef.agree(s_iterable.listdef)
+ if isinstance(s_iterable, SomeList): # unify the two lists
+ lst.listdef.agree(s_iterable.listdef)
+ else:
+ s_iter = s_iterable.iter()
+ lst.method_append(s_iter.next())
def method_reverse(lst):
lst.listdef.mutate()
Modified: pypy/branch/getslice/pypy/objspace/flow/test/test_objspace.py
==============================================================================
--- pypy/branch/getslice/pypy/objspace/flow/test/test_objspace.py (original)
+++ pypy/branch/getslice/pypy/objspace/flow/test/test_objspace.py Fri Oct 10 20:14:02 2008
@@ -851,6 +851,13 @@
return foolist[0]
py.test.raises(RuntimeError, "self.codetest(f)")
+ def test_getslice_constfold(self):
+ def f():
+ s = 'hello'
+ return s[:3]
+ graph = self.codetest(f)
+ assert not self.all_operations(graph)
+
class TestFlowObjSpaceDelay(Base):
def setup_class(cls):
cls.space = FlowObjSpace()
Modified: pypy/branch/getslice/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/branch/getslice/pypy/rpython/lltypesystem/rstr.py (original)
+++ pypy/branch/getslice/pypy/rpython/lltypesystem/rstr.py Fri Oct 10 20:14:02 2008
@@ -640,9 +640,7 @@
s1.copy_contents(s1, newstr, start, 0, lgt)
return newstr
- def ll_stringslice(s1, slice):
- start = slice.start
- stop = slice.stop
+ def ll_stringslice_startstop(s1, start, stop):
if stop >= len(s1.chars):
if start == 0:
return s1
Modified: pypy/branch/getslice/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/branch/getslice/pypy/rpython/ootypesystem/rstr.py (original)
+++ pypy/branch/getslice/pypy/rpython/ootypesystem/rstr.py Fri Oct 10 20:14:02 2008
@@ -198,9 +198,7 @@
def ll_stringslice_startonly(s, start):
return s.ll_substring(start, s.ll_strlen() - start)
- def ll_stringslice(s, slice):
- start = slice.start
- stop = slice.stop
+ def ll_stringslice_startstop(s, start, stop):
length = s.ll_strlen()
if stop > length:
stop = length
Modified: pypy/branch/getslice/pypy/rpython/rlist.py
==============================================================================
--- pypy/branch/getslice/pypy/rpython/rlist.py (original)
+++ pypy/branch/getslice/pypy/rpython/rlist.py Fri Oct 10 20:14:02 2008
@@ -370,24 +370,14 @@
if r_lst1.item_repr.lowleveltype not in (Char, UniChar):
raise TyperError('"lst += string" only supported with a list '
'of chars or unichars')
- rs = r_lst1.rtyper.type_system.rslice
string_repr = r_lst1.rtyper.type_system.rstr.string_repr
+ v_lst1 = hop.inputarg(r_lst1, arg=0)
+ v_str2 = hop.inputarg(string_repr, arg=3)
+ kind, vlist = hop.decompose_slice_args()
c_strlen = hop.inputconst(Void, string_repr.ll.ll_strlen)
c_stritem = hop.inputconst(Void, string_repr.ll.ll_stritem_nonneg)
- r_slic = hop.args_r[2]
- v_lst1, v_str2, v_slice = hop.inputargs(r_lst1, string_repr, r_slic)
- if r_slic == rs.startonly_slice_repr:
- hop.gendirectcall(ll_extend_with_str_slice_startonly,
- v_lst1, v_str2, c_strlen, c_stritem, v_slice)
- elif r_slic == rs.startstop_slice_repr:
- hop.gendirectcall(ll_extend_with_str_slice,
- v_lst1, v_str2, c_strlen, c_stritem, v_slice)
- elif r_slic == rs.minusone_slice_repr:
- hop.gendirectcall(ll_extend_with_str_slice_minusone,
- v_lst1, v_str2, c_strlen, c_stritem)
- else:
- raise TyperError('lst += str[:] does not support slices with %r' %
- (r_slic,))
+ ll_fn = globals()['ll_extend_with_str_slice_%s' % kind]
+ hop.gendirectcall(ll_fn, v_lst1, v_str2, c_strlen, c_stritem, *vlist)
return v_lst1
class __extend__(pairtype(AbstractListRepr, AbstractCharRepr)):
@@ -775,9 +765,8 @@
i += 1
j += 1
-def ll_extend_with_str_slice(lst, s, getstrlen, getstritem, slice):
- start = slice.start
- stop = slice.stop
+def ll_extend_with_str_slice_startstop(lst, s, getstrlen, getstritem,
+ start, stop):
len1 = lst.ll_length()
len2 = getstrlen(s)
ll_assert(start >= 0, "unexpectedly negative str slice start")
Modified: pypy/branch/getslice/pypy/rpython/rstr.py
==============================================================================
--- pypy/branch/getslice/pypy/rpython/rstr.py (original)
+++ pypy/branch/getslice/pypy/rpython/rstr.py Fri Oct 10 20:14:02 2008
@@ -330,22 +330,14 @@
rtype_getitem_idx_key = rtype_getitem_idx
-##class __extend__(pairtype(AbstractStringRepr, AbstractSliceRepr)):
+class __extend__(AbstractStringRepr):
-## def rtype_getitem((r_str, r_slic), hop):
-## string_repr = r_str.repr
-## rslice = hop.rtyper.type_system.rslice
-
-## if r_slic == rslice.startonly_slice_repr:
-## v_str, v_start = hop.inputargs(string_repr, rslice.startonly_slice_repr)
-## return hop.gendirectcall(r_str.ll.ll_stringslice_startonly, v_str, v_start)
-## if r_slic == rslice.startstop_slice_repr:
-## v_str, v_slice = hop.inputargs(string_repr, rslice.startstop_slice_repr)
-## return hop.gendirectcall(r_str.ll.ll_stringslice, v_str, v_slice)
-## if r_slic == rslice.minusone_slice_repr:
-## v_str, v_ignored = hop.inputargs(string_repr, rslice.minusone_slice_repr)
-## return hop.gendirectcall(r_str.ll.ll_stringslice_minusone, v_str)
-## raise TyperError(r_slic)
+ def rtype_getslice(r_str, hop):
+ string_repr = r_str.repr
+ v_str = hop.inputarg(string_repr, arg=0)
+ kind, vlist = hop.decompose_slice_args()
+ ll_fn = getattr(r_str.ll, 'll_stringslice_%s' % (kind,))
+ return hop.gendirectcall(ll_fn, v_str, *vlist)
class __extend__(pairtype(AbstractStringRepr, AbstractStringRepr)):
def rtype_add((r_str1, r_str2), hop):
Modified: pypy/branch/getslice/pypy/rpython/rtyper.py
==============================================================================
--- pypy/branch/getslice/pypy/rpython/rtyper.py (original)
+++ pypy/branch/getslice/pypy/rpython/rtyper.py Fri Oct 10 20:14:02 2008
@@ -578,7 +578,7 @@
def translate_op_extend_with_str_slice(self, hop):
r_arg1 = hop.args_r[0]
- r_arg2 = hop.args_r[1]
+ r_arg2 = hop.args_r[3]
return pair(r_arg1, r_arg2).rtype_extend_with_str_slice(hop)
def translate_op_extend_with_char_count(self, hop):
@@ -589,9 +589,6 @@
def translate_op_newtuple(self, hop):
return self.type_system.rtuple.rtype_newtuple(hop)
- def translate_op_newslice(self, hop):
- return rslice.rtype_newslice(hop)
-
def translate_op_instantiate1(self, hop):
from pypy.rpython.lltypesystem import rclass
if not isinstance(hop.s_result, annmodel.SomeInstance):
Modified: pypy/branch/getslice/pypy/translator/transform.py
==============================================================================
--- pypy/branch/getslice/pypy/translator/transform.py (original)
+++ pypy/branch/getslice/pypy/translator/transform.py Fri Oct 10 20:14:02 2008
@@ -57,10 +57,10 @@
# lst += string[x:y]
# -->
-# b = getitem(string, slice)
+# b = getslice(string, x, y)
# c = inplace_add(lst, b)
# -->
-# c = extend_with_str_slice(lst, string, slice)
+# c = extend_with_str_slice(lst, x, y, string)
def transform_extend_with_str_slice(self, block_subset):
"""Transforms lst += string[x:y] to extend_with_str_slice"""
@@ -68,16 +68,15 @@
slice_sources = {} # maps b to [string, slice] in the above notation
for i in range(len(block.operations)):
op = block.operations[i]
- if (op.opname == 'getitem' and
- self.gettype(op.args[0]) is str and
- self.gettype(op.args[1]) is slice):
+ if (op.opname == 'getslice' and
+ self.gettype(op.args[0]) is str):
slice_sources[op.result] = op.args
elif (op.opname == 'inplace_add' and
op.args[1] in slice_sources and
self.gettype(op.args[0]) is list):
- v_string, v_slice = slice_sources[op.args[1]]
+ v_string, v_x, v_y = slice_sources[op.args[1]]
new_op = SpaceOperation('extend_with_str_slice',
- [op.args[0], v_string, v_slice],
+ [op.args[0], v_x, v_y, v_string],
op.result)
block.operations[i] = new_op
More information about the Pypy-commit
mailing list