[pypy-svn] r25705 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Tue Apr 11 18:52:11 CEST 2006


Author: arigo
Date: Tue Apr 11 18:52:10 2006
New Revision: 25705

Modified:
   pypy/dist/pypy/rpython/rctypes/rchar_p.py
   pypy/dist/pypy/rpython/rctypes/rmodel.py
   pypy/dist/pypy/rpython/rctypes/rpointer.py
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
Log:
Change the c_data layout for primitives: instead of a struct with a
single field called 'value', it is now a FixedSizeArray of a single
item.  The goal is then to consider pointers to a FixedSizeArray of a
single primitive item as pretty much the same as the (non-lltype) notion
of pointer to primitive.



Modified: pypy/dist/pypy/rpython/rctypes/rchar_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rchar_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rchar_p.py	Tue Apr 11 18:52:10 2006
@@ -92,7 +92,7 @@
     return llmemory.cast_ptr_to_adr(s.chars) + FIRSTITEMOFS
 
 def ll_getstring(box):
-    p = box.c_data.value
+    p = box.c_data[0]
     if p:
         if box.keepalive_str and ll_str2charp(box.keepalive_str) == p:
             maxlen = len(box.keepalive_str.chars)
@@ -111,9 +111,9 @@
 
 def ll_setstring(box, string):
     if string:
-        box.c_data.value = ll_str2charp(string)
+        box.c_data[0] = ll_str2charp(string)
     else:
-        box.c_data.value = llmemory.NULL
+        box.c_data[0] = llmemory.NULL
     box.keepalive_str = string
 
 

Modified: pypy/dist/pypy/rpython/rctypes/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rmodel.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rmodel.py	Tue Apr 11 18:52:10 2006
@@ -18,7 +18,7 @@
     #  * 'c_data_type'  is a low-level container type that also represents
     #                   the raw C data; the difference is that we can take
     #                   an lltype pointer to it.  For primitives or pointers
-    #                   this is a Struct with a single field 'value' of
+    #                   this is a FixedSizeArray with a single item of
     #                   type 'll_type'.  Otherwise, c_data_type == ll_type.
     #
     #  * 'lowleveltype' is the Repr's choosen low-level type for the RPython
@@ -177,17 +177,14 @@
     semantics, like primitives and pointers."""
 
     def get_c_data_type(self, ll_type):
-        return lltype.Struct('C_Data_%s' % (self.ctype.__name__,),
-                             ('value', ll_type) )
+        return lltype.FixedSizeArray(ll_type, 1)
 
     def getvalue_from_c_data(self, llops, v_c_data):
-        cname = inputconst(lltype.Void, 'value')
-        return llops.genop('getfield', [v_c_data, cname],
+        return llops.genop('getarrayitem', [v_c_data, C_ZERO],
                 resulttype=self.ll_type)
 
     def setvalue_inside_c_data(self, llops, v_c_data, v_value):
-        cname = inputconst(lltype.Void, 'value')
-        llops.genop('setfield', [v_c_data, cname, v_value])
+        llops.genop('setarrayitem', [v_c_data, C_ZERO, v_value])
 
     def getvalue(self, llops, v_box):
         """Reads from the 'value' field of the raw data."""
@@ -202,24 +199,39 @@
     def initialize_const(self, p, value):
         if isinstance(value, self.ctype):
             value = value.value
-        p.c_data.value = value
+        p.c_data[0] = value
 
 # ____________________________________________________________
 
+C_ZERO = inputconst(lltype.Signed, 0)
+
 def reccopy(source, dest):
     # copy recursively a structure or array onto another.
     T = lltype.typeOf(source).TO
     assert T == lltype.typeOf(dest).TO
-    assert isinstance(T, lltype.Struct)   # XXX implement arrays
-    for name in T._names:
-        FIELDTYPE = getattr(T, name)
-        if isinstance(FIELDTYPE, lltype.ContainerType):
-            subsrc = getattr(source, name)
-            subdst = getattr(dest,   name)
-            reccopy(subsrc, subdst)
-        else:
-            llvalue = getattr(source, name)
-            setattr(dest, name, llvalue)
+    if isinstance(T, (lltype.Array, lltype.FixedSizeArray)):
+        assert len(source) == len(dest)
+        ITEMTYPE = T.OF
+        for i in range(len(source)):
+            if isinstance(ITEMTYPE, lltype.ContainerType):
+                subsrc = source[i]
+                subdst = dest[i]
+                reccopy(subsrc, subdst)
+            else:
+                llvalue = source[i]
+                dest[i] = llvalue
+    elif isinstance(T, lltype.Struct):
+        for name in T._names:
+            FIELDTYPE = getattr(T, name)
+            if isinstance(FIELDTYPE, lltype.ContainerType):
+                subsrc = getattr(source, name)
+                subdst = getattr(dest,   name)
+                reccopy(subsrc, subdst)
+            else:
+                llvalue = getattr(source, name)
+                setattr(dest, name, llvalue)
+    else:
+        raise TypeError(T)
 
 def reccopy_arrayitem(source, destarray, destindex):
     ITEMTYPE = lltype.typeOf(destarray).TO.OF
@@ -234,18 +246,45 @@
     # (v, i) to mean the ith item of the array that v points to.
     T = v_source.concretetype.TO
     assert T == v_dest.concretetype.TO
-    assert isinstance(T, lltype.Struct)   # XXX implement arrays
-    for name in T._names:
-        FIELDTYPE = getattr(T, name)
-        cname = inputconst(lltype.Void, name)
-        if isinstance(FIELDTYPE, lltype.ContainerType):
-            RESTYPE = lltype.Ptr(FIELDTYPE)
-            v_subsrc = llops.genop('getsubstruct', [v_source, cname], RESTYPE)
-            v_subdst = llops.genop('getsubstruct', [v_dest,   cname], RESTYPE)
-            genreccopy(llops, v_subsrc, v_subdst)
-        else:
-            v_value = llops.genop('getfield', [v_source, cname], FIELDTYPE)
-            llops.genop('setfield', [v_dest, cname, v_value])
+
+    if isinstance(T, lltype.FixedSizeArray):
+        # XXX don't do that if the length is large
+        ITEMTYPE = T.OF
+        for i in range(T.length):
+            c_i = inputconst(lltype.Signed, i)
+            if isinstance(ITEMTYPE, lltype.ContainerType):
+                RESTYPE = lltype.Ptr(ITEMTYPE)
+                v_subsrc = llops.genop('getarraysubstruct', [v_source, c_i],
+                                       resulttype = RESTYPE)
+                v_subdst = llops.genop('getarraysubstruct', [v_dest,   c_i],
+                                       resulttype = RESTYPE)
+                genreccopy(llops, v_subsrc, v_subdst)
+            else:
+                v_value = llops.genop('getarrayitem', [v_source, c_i],
+                                      resulttype = ITEMTYPE)
+                llops.genop('setarrayitem', [v_dest, c_i, v_value])
+
+    elif isinstance(T, lltype.Array):
+        raise NotImplementedError("XXX genreccopy() for arrays")
+
+    elif isinstance(T, lltype.Struct):
+        for name in T._names:
+            FIELDTYPE = getattr(T, name)
+            cname = inputconst(lltype.Void, name)
+            if isinstance(FIELDTYPE, lltype.ContainerType):
+                RESTYPE = lltype.Ptr(FIELDTYPE)
+                v_subsrc = llops.genop('getsubstruct', [v_source, cname],
+                                       resulttype = RESTYPE)
+                v_subdst = llops.genop('getsubstruct', [v_dest,   cname],
+                                       resulttype = RESTYPE)
+                genreccopy(llops, v_subsrc, v_subdst)
+            else:
+                v_value = llops.genop('getfield', [v_source, cname],
+                                      resulttype = FIELDTYPE)
+                llops.genop('setfield', [v_dest, cname, v_value])
+
+    else:
+        raise TypeError(T)
 
 def genreccopy_arrayitem(llops, v_source, v_destarray, v_destindex):
     ITEMTYPE = v_destarray.concretetype.TO.OF

Modified: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py	Tue Apr 11 18:52:10 2006
@@ -33,7 +33,7 @@
 
     def initialize_const(self, p, ptr):
         llcontents = self.r_contents.convert_const(ptr.contents)
-        p.c_data.value = llcontents.c_data
+        p.c_data[0] = llcontents.c_data
         # the following line is probably pointless, as 'llcontents' will be
         # an immortal global constant just like 'p', but better safe than sorry
         p.keepalive_contents = llcontents.c_data_owner_keepalive

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Tue Apr 11 18:52:10 2006
@@ -124,8 +124,8 @@
 
         res = interpret(create_array, [])
         c_data = res.c_data
-        assert c_data[0].value == 0
-        assert c_data[9].value == 0
+        assert c_data[0][0] == 0
+        assert c_data[9][0] == 0
         py.test.raises(IndexError, "c_data[10]")
         assert len(c_data) == 10
 

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py	Tue Apr 11 18:52:10 2006
@@ -124,14 +124,14 @@
             return c_int(42)
         res = interpret(create_c_int, [])
         c_data = res.c_data
-        assert c_data.value == 42
+        assert c_data[0] == 42
 
     def test_specialize_c_int_default_value(self):
         def create_c_int():
             return c_int()
         res = interpret(create_c_int, [])
         c_data = res.c_data
-        assert c_data.value == 0
+        assert c_data[0] == 0
     
     def test_specialize_c_int_access_value(self):
         def create_c_int():
@@ -161,14 +161,14 @@
             return c_float(4.2)
         res = interpret(create_c_float, [])
         c_data = res.c_data
-        assert c_data.value == 4.2
+        assert c_data[0] == 4.2
 
     def test_specialize_c_float_default_value(self):
         def create_c_float():
             return c_float()
         res = interpret(create_c_float, [])
         c_data = res.c_data
-        assert c_data.value == 0.0
+        assert c_data[0] == 0.0
 
     def test_specialize_c_float_access_value(self):
         def create_c_float():



More information about the Pypy-commit mailing list