Author: Ronan Lamy <[email protected]>
Branch: kill-typesystem
Changeset: r65908:2d755cc6e1da
Date: 2013-08-02 15:26 +0100
http://bitbucket.org/pypy/pypy/changeset/2d755cc6e1da/
Log: merge lltypesystem.rtuple into rtyper.rtuple
diff --git a/rpython/rtyper/callparse.py b/rpython/rtyper/callparse.py
--- a/rpython/rtyper/callparse.py
+++ b/rpython/rtyper/callparse.py
@@ -137,7 +137,7 @@
return self.holders
def _emit(self, repr, hop):
- assert isinstance(repr, rtuple.AbstractTupleRepr)
+ assert isinstance(repr, rtuple.TupleRepr)
tupleitems_v = []
for h in self.holders:
v = h.emit(repr.items_r[len(tupleitems_v)], hop)
diff --git a/rpython/rtyper/lltypesystem/rtuple.py
b/rpython/rtyper/lltypesystem/rtuple.py
deleted file mode 100644
--- a/rpython/rtyper/lltypesystem/rtuple.py
+++ /dev/null
@@ -1,122 +0,0 @@
-from rpython.tool.pairtype import pairtype
-from rpython.rtyper.rmodel import inputconst
-from rpython.rtyper.rtuple import AbstractTupleRepr, AbstractTupleIteratorRepr
-from rpython.rtyper.lltypesystem.lltype import \
- Ptr, GcStruct, Void, Signed, malloc, typeOf, nullptr
-from rpython.rtyper.lltypesystem import rstr
-
-# ____________________________________________________________
-#
-# Concrete implementation of RPython tuples:
-#
-# struct tuple {
-# type0 item0;
-# type1 item1;
-# type2 item2;
-# ...
-# }
-
-def TUPLE_TYPE(field_lltypes):
- if len(field_lltypes) == 0:
- return Void # empty tuple
- else:
- fields = [('item%d' % i, TYPE) for i, TYPE in enumerate(field_lltypes)]
- kwds = {'hints': {'immutable': True,
- 'noidentity': True}}
- return Ptr(GcStruct('tuple%d' % len(field_lltypes), *fields, **kwds))
-
-
-class TupleRepr(AbstractTupleRepr):
- rstr_ll = rstr.LLHelpers
-
- def __init__(self, rtyper, items_r):
- AbstractTupleRepr.__init__(self, rtyper, items_r)
- self.lowleveltype = TUPLE_TYPE(self.lltypes)
-
- def newtuple(cls, llops, r_tuple, items_v):
- # items_v should have the lowleveltype of the internal reprs
- assert len(r_tuple.items_r) == len(items_v)
- for r_item, v_item in zip(r_tuple.items_r, items_v):
- assert r_item.lowleveltype == v_item.concretetype
- #
- if len(r_tuple.items_r) == 0:
- return inputconst(Void, ()) # a Void empty tuple
- c1 = inputconst(Void, r_tuple.lowleveltype.TO)
- cflags = inputconst(Void, {'flavor': 'gc'})
- v_result = llops.genop('malloc', [c1, cflags],
- resulttype = r_tuple.lowleveltype)
- for i in range(len(r_tuple.items_r)):
- cname = inputconst(Void, r_tuple.fieldnames[i])
- llops.genop('setfield', [v_result, cname, items_v[i]])
- return v_result
- newtuple = classmethod(newtuple)
-
- def instantiate(self):
- if len(self.items_r) == 0:
- return dum_empty_tuple # PBC placeholder for an empty tuple
- else:
- return malloc(self.lowleveltype.TO)
-
- def rtype_bltn_list(self, hop):
- from rpython.rtyper.lltypesystem import rlist
- nitems = len(self.items_r)
- vtup = hop.inputarg(self, 0)
- LIST = hop.r_result.lowleveltype.TO
- cno = inputconst(Signed, nitems)
- hop.exception_is_here()
- vlist = hop.gendirectcall(LIST.ll_newlist, cno)
- v_func = hop.inputconst(Void, rlist.dum_nocheck)
- for index in range(nitems):
- name = self.fieldnames[index]
- ritem = self.items_r[index]
- cname = hop.inputconst(Void, name)
- vitem = hop.genop('getfield', [vtup, cname], resulttype = ritem)
- vitem = hop.llops.convertvar(vitem, ritem, hop.r_result.item_repr)
- cindex = inputconst(Signed, index)
- hop.gendirectcall(rlist.ll_setitem_nonneg, v_func, vlist, cindex,
vitem)
- return vlist
-
- def getitem_internal(self, llops, v_tuple, index):
- """Return the index'th item, in internal repr."""
- name = self.fieldnames[index]
- llresult = self.lltypes[index]
- cname = inputconst(Void, name)
- return llops.genop('getfield', [v_tuple, cname], resulttype =
llresult)
-
-
-def rtype_newtuple(hop):
- return TupleRepr._rtype_newtuple(hop)
-
-newtuple = TupleRepr.newtuple
-
-def dum_empty_tuple(): pass
-
-
-# ____________________________________________________________
-#
-# Iteration.
-
-class Length1TupleIteratorRepr(AbstractTupleIteratorRepr):
-
- def __init__(self, r_tuple):
- self.r_tuple = r_tuple
- self.lowleveltype = Ptr(GcStruct('tuple1iter',
- ('tuple', r_tuple.lowleveltype)))
- self.ll_tupleiter = ll_tupleiter
- self.ll_tuplenext = ll_tuplenext
-
-TupleRepr.IteratorRepr = Length1TupleIteratorRepr
-
-def ll_tupleiter(ITERPTR, tuple):
- iter = malloc(ITERPTR.TO)
- iter.tuple = tuple
- return iter
-
-def ll_tuplenext(iter):
- # for iterating over length 1 tuples only!
- t = iter.tuple
- if t:
- iter.tuple = nullptr(typeOf(t).TO)
- return t.item0
- else:
- raise StopIteration
diff --git a/rpython/rtyper/module/ll_os_stat.py
b/rpython/rtyper/module/ll_os_stat.py
--- a/rpython/rtyper/module/ll_os_stat.py
+++ b/rpython/rtyper/module/ll_os_stat.py
@@ -13,7 +13,7 @@
from rpython.rtyper.annlowlevel import hlstr
from rpython.rtyper.extfunc import extdef
from rpython.rtyper.lltypesystem import rffi, lltype
-from rpython.rtyper.lltypesystem.rtuple import TUPLE_TYPE
+from rpython.rtyper.rtuple import TUPLE_TYPE
from rpython.rtyper.tool import rffi_platform as platform
from rpython.tool.pairtype import pairtype
from rpython.tool.sourcetools import func_renamer
diff --git a/rpython/rtyper/rbuiltin.py b/rpython/rtyper/rbuiltin.py
--- a/rpython/rtyper/rbuiltin.py
+++ b/rpython/rtyper/rbuiltin.py
@@ -53,14 +53,14 @@
raise TyperError("**kwds call not implemented")
if arguments.w_stararg is not None:
# expand the *arg in-place -- it must be a tuple
- from rpython.rtyper.rtuple import AbstractTupleRepr
+ from rpython.rtyper.rtuple import TupleRepr
if arguments.w_stararg != hop.nb_args - 3:
raise TyperError("call pattern too complex")
hop.nb_args -= 1
v_tuple = hop.args_v.pop()
s_tuple = hop.args_s.pop()
r_tuple = hop.args_r.pop()
- if not isinstance(r_tuple, AbstractTupleRepr):
+ if not isinstance(r_tuple, TupleRepr):
raise TyperError("*arg must be a tuple")
for i in range(len(r_tuple.items_r)):
v_item = r_tuple.getitem_internal(hop.llops, v_tuple, i)
diff --git a/rpython/rtyper/rstr.py b/rpython/rtyper/rstr.py
--- a/rpython/rtyper/rstr.py
+++ b/rpython/rtyper/rstr.py
@@ -4,7 +4,6 @@
from rpython.rtyper.error import TyperError
from rpython.rtyper.lltypesystem.lltype import Signed, Bool, Void, UniChar
from rpython.rtyper.rmodel import IntegerRepr, IteratorRepr, inputconst, Repr
-from rpython.rtyper.rtuple import AbstractTupleRepr
from rpython.tool.pairtype import pairtype, pair
from rpython.tool.sourcetools import func_with_new_name
from rpython.tool.staticmethods import StaticMethods
@@ -564,18 +563,6 @@
hop.exception_cannot_occur()
return hop.gendirectcall(r_str.ll.ll_contains, v_str, v_chr)
-class __extend__(pairtype(AbstractStringRepr, AbstractTupleRepr)):
- def rtype_mod((r_str, r_tuple), hop):
- r_tuple = hop.args_r[1]
- v_tuple = hop.args_v[1]
-
- sourcevars = []
- for i, r_arg in enumerate(r_tuple.external_items_r):
- v_item = r_tuple.getitem(hop.llops, v_tuple, i)
- sourcevars.append((v_item, r_arg))
-
- return r_str.ll.do_stringformat(hop, sourcevars)
-
class __extend__(AbstractCharRepr):
def ll_str(self, ch):
diff --git a/rpython/rtyper/rtuple.py b/rpython/rtyper/rtuple.py
--- a/rpython/rtyper/rtuple.py
+++ b/rpython/rtyper/rtuple.py
@@ -5,7 +5,10 @@
from rpython.rlib.rarithmetic import intmask
from rpython.rlib.unroll import unrolling_iterable
from rpython.rtyper.error import TyperError
-from rpython.rtyper.lltypesystem.lltype import Void, Signed, Bool
+from rpython.rtyper.lltypesystem.lltype import (
+ Void, Signed, Bool, Ptr, GcStruct, malloc, typeOf, nullptr)
+from rpython.rtyper.lltypesystem.rstr import LLHelpers
+from rpython.rtyper.rstr import AbstractStringRepr
from rpython.rtyper.rmodel import (Repr, IntegerRepr, inputconst, IteratorRepr,
externalvsinternal)
from rpython.tool.pairtype import pairtype
@@ -13,7 +16,6 @@
class __extend__(annmodel.SomeTuple):
def rtyper_makerepr(self, rtyper):
- from rpython.rtyper.lltypesystem.rtuple import TupleRepr
return TupleRepr(rtyper, [rtyper.getrepr(s_item) for s_item in
self.items])
def rtyper_makekey_ex(self, rtyper):
@@ -71,17 +73,16 @@
def gen_str_function(tuplerepr):
items_r = tuplerepr.items_r
- str_funcs = [r_item.ll_str for r_item in items_r]
- key = tuplerepr.rstr_ll, tuple(str_funcs)
+ key = tuple([r_item.ll_str for r_item in items_r])
try:
return _gen_str_function_cache[key]
except KeyError:
- autounrolling_funclist = unrolling_iterable(enumerate(str_funcs))
+ autounrolling_funclist = unrolling_iterable(enumerate(key))
- constant = tuplerepr.rstr_ll.ll_constant
- start = tuplerepr.rstr_ll.ll_build_start
- push = tuplerepr.rstr_ll.ll_build_push
- finish = tuplerepr.rstr_ll.ll_build_finish
+ constant = LLHelpers.ll_constant
+ start = LLHelpers.ll_build_start
+ push = LLHelpers.ll_build_push
+ finish = LLHelpers.ll_build_finish
length = len(items_r)
def ll_str(t):
@@ -105,7 +106,28 @@
return ll_str
-class AbstractTupleRepr(Repr):
+# ____________________________________________________________
+#
+# Concrete implementation of RPython tuples:
+#
+# struct tuple {
+# type0 item0;
+# type1 item1;
+# type2 item2;
+# ...
+# }
+
+def TUPLE_TYPE(field_lltypes):
+ if len(field_lltypes) == 0:
+ return Void # empty tuple
+ else:
+ fields = [('item%d' % i, TYPE) for i, TYPE in enumerate(field_lltypes)]
+ kwds = {'hints': {'immutable': True,
+ 'noidentity': True}}
+ return Ptr(GcStruct('tuple%d' % len(field_lltypes), *fields, **kwds))
+
+
+class TupleRepr(Repr):
def __init__(self, rtyper, items_r):
self.items_r = []
@@ -118,6 +140,7 @@
self.fieldnames = ['item%d' % i for i in range(len(items_r))]
self.lltypes = [r.lowleveltype for r in items_r]
self.tuple_cache = {}
+ self.lowleveltype = TUPLE_TYPE(self.lltypes)
def getitem(self, llops, v_tuple, index):
"""Generate the operations to get the index'th item of v_tuple,
@@ -127,19 +150,37 @@
r_external_item = self.external_items_r[index]
return llops.convertvar(v, r_item, r_external_item)
+ @classmethod
+ def newtuple(cls, llops, r_tuple, items_v):
+ # items_v should have the lowleveltype of the internal reprs
+ assert len(r_tuple.items_r) == len(items_v)
+ for r_item, v_item in zip(r_tuple.items_r, items_v):
+ assert r_item.lowleveltype == v_item.concretetype
+ #
+ if len(r_tuple.items_r) == 0:
+ return inputconst(Void, ()) # a Void empty tuple
+ c1 = inputconst(Void, r_tuple.lowleveltype.TO)
+ cflags = inputconst(Void, {'flavor': 'gc'})
+ v_result = llops.genop('malloc', [c1, cflags],
+ resulttype = r_tuple.lowleveltype)
+ for i in range(len(r_tuple.items_r)):
+ cname = inputconst(Void, r_tuple.fieldnames[i])
+ llops.genop('setfield', [v_result, cname, items_v[i]])
+ return v_result
+
+ @classmethod
def newtuple_cached(cls, hop, items_v):
r_tuple = hop.r_result
if hop.s_result.is_constant():
return inputconst(r_tuple, hop.s_result.const)
else:
return cls.newtuple(hop.llops, r_tuple, items_v)
- newtuple_cached = classmethod(newtuple_cached)
+ @classmethod
def _rtype_newtuple(cls, hop):
r_tuple = hop.r_result
vlist = hop.inputargs(*r_tuple.items_r)
return cls.newtuple_cached(hop, vlist)
- _rtype_newtuple = classmethod(_rtype_newtuple)
def convert_const(self, value):
assert isinstance(value, tuple) and len(value) == len(self.items_r)
@@ -174,8 +215,48 @@
return self.IteratorRepr(self)
raise TyperError("can only iterate over tuples of length 1 for now")
+ def instantiate(self):
+ if len(self.items_r) == 0:
+ return dum_empty_tuple # PBC placeholder for an empty tuple
+ else:
+ return malloc(self.lowleveltype.TO)
-class __extend__(pairtype(AbstractTupleRepr, IntegerRepr)):
+ def rtype_bltn_list(self, hop):
+ from rpython.rtyper.lltypesystem import rlist
+ nitems = len(self.items_r)
+ vtup = hop.inputarg(self, 0)
+ LIST = hop.r_result.lowleveltype.TO
+ cno = inputconst(Signed, nitems)
+ hop.exception_is_here()
+ vlist = hop.gendirectcall(LIST.ll_newlist, cno)
+ v_func = hop.inputconst(Void, rlist.dum_nocheck)
+ for index in range(nitems):
+ name = self.fieldnames[index]
+ ritem = self.items_r[index]
+ cname = hop.inputconst(Void, name)
+ vitem = hop.genop('getfield', [vtup, cname], resulttype = ritem)
+ vitem = hop.llops.convertvar(vitem, ritem, hop.r_result.item_repr)
+ cindex = inputconst(Signed, index)
+ hop.gendirectcall(rlist.ll_setitem_nonneg, v_func, vlist, cindex,
vitem)
+ return vlist
+
+ def getitem_internal(self, llops, v_tuple, index):
+ """Return the index'th item, in internal repr."""
+ name = self.fieldnames[index]
+ llresult = self.lltypes[index]
+ cname = inputconst(Void, name)
+ return llops.genop('getfield', [v_tuple, cname], resulttype =
llresult)
+
+
+def rtype_newtuple(hop):
+ return TupleRepr._rtype_newtuple(hop)
+
+newtuple = TupleRepr.newtuple
+
+def dum_empty_tuple(): pass
+
+
+class __extend__(pairtype(TupleRepr, IntegerRepr)):
def rtype_getitem((r_tup, r_int), hop):
v_tuple, v_index = hop.inputargs(r_tup, Signed)
@@ -186,7 +267,7 @@
index = v_index.value
return r_tup.getitem(hop.llops, v_tuple, index)
-class __extend__(AbstractTupleRepr):
+class __extend__(TupleRepr):
def rtype_getslice(r_tup, hop):
s_start = hop.args_s[1]
@@ -203,7 +284,7 @@
for i in indices]
return hop.r_result.newtuple(hop.llops, hop.r_result, items_v)
-class __extend__(pairtype(AbstractTupleRepr, Repr)):
+class __extend__(pairtype(TupleRepr, Repr)):
def rtype_contains((r_tup, r_item), hop):
s_tup = hop.args_s[0]
if not s_tup.is_constant():
@@ -224,7 +305,7 @@
hop2.v_s_insertfirstarg(v_dict, s_dict)
return hop2.dispatch()
-class __extend__(pairtype(AbstractTupleRepr, AbstractTupleRepr)):
+class __extend__(pairtype(TupleRepr, TupleRepr)):
def rtype_add((r_tup1, r_tup2), hop):
v_tuple1, v_tuple2 = hop.inputargs(r_tup1, r_tup2)
@@ -265,6 +346,21 @@
def rtype_is_((robj1, robj2), hop):
raise TyperError("cannot compare tuples with 'is'")
+class __extend__(pairtype(AbstractStringRepr, TupleRepr)):
+ def rtype_mod((r_str, r_tuple), hop):
+ r_tuple = hop.args_r[1]
+ v_tuple = hop.args_v[1]
+
+ sourcevars = []
+ for i, r_arg in enumerate(r_tuple.external_items_r):
+ v_item = r_tuple.getitem(hop.llops, v_tuple, i)
+ sourcevars.append((v_item, r_arg))
+
+ return r_str.ll.do_stringformat(hop, sourcevars)
+
+# ____________________________________________________________
+#
+# Iteration.
class AbstractTupleIteratorRepr(IteratorRepr):
@@ -279,3 +375,28 @@
hop.exception_is_here()
v = hop.gendirectcall(self.ll_tuplenext, v_iter)
return hop.llops.convertvar(v, self.r_tuple.items_r[0],
self.r_tuple.external_items_r[0])
+
+class Length1TupleIteratorRepr(AbstractTupleIteratorRepr):
+
+ def __init__(self, r_tuple):
+ self.r_tuple = r_tuple
+ self.lowleveltype = Ptr(GcStruct('tuple1iter',
+ ('tuple', r_tuple.lowleveltype)))
+ self.ll_tupleiter = ll_tupleiter
+ self.ll_tuplenext = ll_tuplenext
+
+TupleRepr.IteratorRepr = Length1TupleIteratorRepr
+
+def ll_tupleiter(ITERPTR, tuple):
+ iter = malloc(ITERPTR.TO)
+ iter.tuple = tuple
+ return iter
+
+def ll_tuplenext(iter):
+ # for iterating over length 1 tuples only!
+ t = iter.tuple
+ if t:
+ iter.tuple = nullptr(typeOf(t).TO)
+ return t.item0
+ else:
+ raise StopIteration
diff --git a/rpython/rtyper/rtyper.py b/rpython/rtyper/rtyper.py
--- a/rpython/rtyper/rtyper.py
+++ b/rpython/rtyper/rtyper.py
@@ -578,7 +578,7 @@
return pair(r_arg1, r_arg2).rtype_extend_with_char_count(hop)
def translate_op_newtuple(self, hop):
- from rpython.rtyper.lltypesystem.rtuple import rtype_newtuple
+ from rpython.rtyper.rtuple import rtype_newtuple
return rtype_newtuple(hop)
def translate_op_instantiate1(self, hop):
diff --git a/rpython/rtyper/test/test_rtuple.py
b/rpython/rtyper/test/test_rtuple.py
--- a/rpython/rtyper/test/test_rtuple.py
+++ b/rpython/rtyper/test/test_rtuple.py
@@ -1,4 +1,4 @@
-from rpython.rtyper.lltypesystem.rtuple import TUPLE_TYPE, TupleRepr
+from rpython.rtyper.rtuple import TUPLE_TYPE, TupleRepr
from rpython.rtyper.lltypesystem.lltype import Signed, Bool
from rpython.rtyper.rbool import bool_repr
from rpython.rtyper.rint import signed_repr
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit