[pypy-svn] r13202 - in pypy/dist/pypy: rpython rpython/test translator/c

arigo at codespeak.net arigo at codespeak.net
Wed Jun 8 22:58:32 CEST 2005


Author: arigo
Date: Wed Jun  8 22:58:28 2005
New Revision: 13202

Added:
   pypy/dist/pypy/rpython/rtuple.py   (contents, props changed)
   pypy/dist/pypy/rpython/test/test_rtuple.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rtyper.py
   pypy/dist/pypy/translator/c/funcgen.py
Log:
Start of the rtuple implementation.


Added: pypy/dist/pypy/rpython/rtuple.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rtuple.py	Wed Jun  8 22:58:28 2005
@@ -0,0 +1,61 @@
+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
+
+# ____________________________________________________________
+#
+#  Concrete implementation of RPython tuples:
+#
+#    struct tuple {
+#        type0 item0;
+#        type1 item1;
+#        type2 item2;
+#        ...
+#    }
+
+class __extend__(annmodel.SomeTuple):
+    def rtyper_makerepr(self, rtyper):
+        return TupleRepr([rtyper.getrepr(s_item) for s_item in self.items])
+
+
+class TupleRepr(Repr):
+
+    def __init__(self, items_r):
+        self.items_r = items_r
+        self.fieldnames = ['item%d' % i for i in range(len(items_r))]
+        self.lltypes = [r.lowleveltype for r in items_r]
+        fields = zip(self.fieldnames, self.lltypes)
+        self.lowleveltype = Ptr(GcStruct('tuple%d' % len(items_r), *fields))
+
+    def rtype_len(self, hop):
+        return hop.inputconst(Signed, len(self.items_r))
+
+
+class __extend__(pairtype(TupleRepr, IntegerRepr)):
+
+    def rtype_getitem((r_tup, r_int), hop):
+        v_tuple, v_index = hop.inputargs(r_tup, Signed)
+        if not isinstance(v_index, Constant):
+            raise TyperError("non-constant tuple index")
+        index = v_index.value
+        name = r_tup.fieldnames[index]
+        llresult = r_tup.lltypes[index]
+        cname = hop.inputconst(Void, name)
+        return hop.genop('getfield', [v_tuple, cname], resulttype = llresult)
+
+# ____________________________________________________________
+#
+#  Irregular operations.
+
+def rtype_newtuple(hop):
+    nb_args = hop.nb_args
+    r_tuple = hop.r_result
+    c1 = hop.inputconst(Void, r_tuple.lowleveltype)
+    v_result = hop.genop('malloc', [c1], resulttype = r_tuple.lowleveltype)
+    for i in range(nb_args):
+        cname = hop.inputconst(Void, r_tuple.fieldnames[i])
+        v_item = hop.inputarg(r_tuple.items_r[i], arg=i)
+        hop.genop('setfield', [v_result, cname, v_item])
+    return v_result

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Wed Jun  8 22:58:28 2005
@@ -215,6 +215,8 @@
                 raise TyperError("constant mismatch: %r vs %r" % (
                     resultvar.value, hop.s_result.const))
             op.result.concretetype = hop.r_result.lowleveltype
+            hop.llops.append(SpaceOperation('same_as', [resultvar],
+                                            op.result))
 
     def gottypererror(self, e, block, position, llops):
         """Record a TyperError without crashing immediately.
@@ -255,6 +257,9 @@
     def translate_op_newlist(self, hop):
         return rlist.rtype_newlist(hop)
 
+    def translate_op_newtuple(self, hop):
+        return rtuple.rtype_newtuple(hop)
+
     def missing_operation(self, hop):
         raise TyperError("unimplemented operation: '%s'" % hop.spaceop.opname)
 
@@ -414,6 +419,6 @@
 # and the rtyper_chooserepr() methods
 from pypy.rpython import robject
 from pypy.rpython import rint, rbool, rfloat
-from pypy.rpython import rlist, rstr
+from pypy.rpython import rlist, rstr, rtuple
 from pypy.rpython import rbuiltin, rpbc
 from pypy.rpython import rptr

Added: pypy/dist/pypy/rpython/test/test_rtuple.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/test/test_rtuple.py	Wed Jun  8 22:58:28 2005
@@ -0,0 +1,46 @@
+from pypy.translator.translator import Translator
+from pypy.rpython.lltype import *
+from pypy.rpython.rtyper import RPythonTyper
+from pypy.rpython.rtuple import *
+from pypy.rpython.rint import signed_repr
+from pypy.rpython.rbool import bool_repr
+
+
+def test_rtuple():
+    rtuple = TupleRepr([signed_repr, bool_repr])
+    assert rtuple.lowleveltype == Ptr(GcStruct('tuple2',
+                                               ('item0', Signed),
+                                               ('item1', Bool),
+                                               ))
+
+# ____________________________________________________________
+
+def rtype(fn, argtypes=[]):
+    t = Translator(fn)
+    t.annotate(argtypes)
+    typer = RPythonTyper(t.annotator)
+    typer.specialize()
+    #t.view()
+    t.checkgraphs()
+    return t
+
+
+def test_simple():
+    def dummyfn(x):
+        l = (10,x,30)
+        return l[2]
+    rtype(dummyfn, [int])
+
+def test_len():
+    def dummyfn(x):
+        l = (5,x)
+        return len(l)
+    rtype(dummyfn, [int])
+
+##def test_iterate():
+##    def dummyfn():
+##        total = 0
+##        for x in (1,3,5,7,9):
+##            total += x
+##        return total
+##    rtype(dummyfn)

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Wed Jun  8 22:58:28 2005
@@ -390,11 +390,14 @@
         return '\t'.join(result)
 
     def OP_SAME_AS(self, op, err):
-        result = ['%s = %s;' % (self.expr(op.result),
-                                self.expr(op.args[0]))]
-        line = self.cincref(op.result)
-        if line:
-            result.append(line)
+        result = []
+        assert self.lltypemap[op.args[0]] == self.lltypemap[op.result]
+        if self.lltypemap[op.result] != Void:
+            result.append('%s = %s;' % (self.expr(op.result),
+                                        self.expr(op.args[0])))
+            line = self.cincref(op.result)
+            if line:
+                result.append(line)
         return '\t'.join(result)
 
     def cincref(self, v):



More information about the Pypy-commit mailing list