[pypy-svn] r25739 - in pypy/dist/pypy/translator/c: . test

arigo at codespeak.net arigo at codespeak.net
Wed Apr 12 21:57:17 CEST 2006


Author: arigo
Date: Wed Apr 12 21:57:16 2006
New Revision: 25739

Modified:
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/node.py
   pypy/dist/pypy/translator/c/primitive.py
   pypy/dist/pypy/translator/c/test/test_symbolic.py
Log:
Generally use cdecl(s,'') more systematically instead of s.replace('@','')
which might leave behind a broken empty pair of parenthesis.

Add a test and a fix for ArrayItemsOffset for FixedSizeArrays.


Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Wed Apr 12 21:57:16 2006
@@ -377,7 +377,8 @@
     OP_INDIRECT_CALL = OP_DIRECT_CALL
 
     def OP_UNSAFE_CALL(self, op):
-        line = '((%s (*)())(%s))();'%(self.lltypename(op.result).replace('@', ''), self.expr(op.args[0]))
+        line = '((%s (*)())(%s))();' % (cdecl(self.lltypename(op.result), ''),
+                                        self.expr(op.args[0]))
         if self.lltypemap(op.result) is not Void:
             r = self.expr(op.result)
             line = '%s = %s' % (r, line)
@@ -586,19 +587,19 @@
 
     #address operations
     def OP_RAW_STORE(self, op):
-       addr = self.expr(op.args[0])
-       TYPE = op.args[1].value
-       offset = self.expr(op.args[2])
-       value = self.expr(op.args[3])
-       typename = self.db.gettype(TYPE).replace("@", "*") #XXX help! is this the way to do it?
-       return "*(((%(typename)s) %(addr)s ) + %(offset)s) = %(value)s;" % locals()
+        addr = self.expr(op.args[0])
+        TYPE = op.args[1].value
+        offset = self.expr(op.args[2])
+        value = self.expr(op.args[3])
+        typename = cdecl(self.db.gettype(TYPE).replace('@', '*@'), '')
+        return "*(((%(typename)s) %(addr)s ) + %(offset)s) = %(value)s;" % locals()
 
     def OP_RAW_LOAD(self, op):
         addr = self.expr(op.args[0])
         TYPE = op.args[1].value
         offset = self.expr(op.args[2])
         result = self.expr(op.result)
-        typename = self.db.gettype(TYPE).replace("@", "*") #XXX see above
+        typename = cdecl(self.db.gettype(TYPE).replace('@', '*@'), '')
         return "%(result)s = *(((%(typename)s) %(addr)s ) + %(offset)s);" % locals()
 
 

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Wed Apr 12 21:57:16 2006
@@ -338,7 +338,8 @@
             self.name = defnode.access_expr(parentnode.name, parentindex)
         self.ptrname = '(&%s)' % self.name
         if self.typename != self.implementationtypename:
-            self.ptrname = '((%s)(void*)%s)' % (self.typename.replace('@', '*'),
+            ptrtypename = db.gettype(Ptr(T))
+            self.ptrname = '((%s)(void*)%s)' % (cdecl(ptrtypename, ''),
                                                 self.ptrname)
 
     def forward_declaration(self):

Modified: pypy/dist/pypy/translator/c/primitive.py
==============================================================================
--- pypy/dist/pypy/translator/c/primitive.py	(original)
+++ pypy/dist/pypy/translator/c/primitive.py	Wed Apr 12 21:57:16 2006
@@ -6,6 +6,7 @@
      CompositeOffset, ArrayLengthOffset
 from pypy.rpython.memory.gc import GCHeaderOffset
 from pypy.rpython.memory.lladdress import NULL
+from pypy.translator.c.support import cdecl
 
 # ____________________________________________________________
 #
@@ -17,19 +18,22 @@
         if isinstance(value, FieldOffset):
             structnode = db.gettypedefnode(value.TYPE)
             return 'offsetof(%s, %s)'%(
-                db.gettype(value.TYPE).replace('@', ''),
+                cdecl(db.gettype(value.TYPE), ''),
                 structnode.c_struct_field_name(value.fldname))
         elif isinstance(value, ItemOffset):
             return '(sizeof(%s) * %s)'%(
-                db.gettype(value.TYPE).replace('@', ''), value.repeat)
+                cdecl(db.gettype(value.TYPE), ''), value.repeat)
         elif isinstance(value, ArrayItemsOffset):
-            return 'offsetof(%s, items)'%(
-                db.gettype(value.TYPE).replace('@', ''))
+            if isinstance(value.TYPE, FixedSizeArray):
+                return '0'
+            else:
+                return 'offsetof(%s, items)'%(
+                    cdecl(db.gettype(value.TYPE), ''))
         elif isinstance(value, ArrayLengthOffset):
             return 'offsetof(%s, length)'%(
-                db.gettype(value.TYPE).replace('@', ''))
+                cdecl(db.gettype(value.TYPE), ''))
         elif isinstance(value, CompositeOffset):
-            return '%s + %s' % (name_signed(value.first, db), name_signed(value.second, db))
+            return '(%s + %s)' % (name_signed(value.first, db), name_signed(value.second, db))
         elif type(value) == AddressOffset:
             return '0'
         elif type(value) == GCHeaderOffset:

Modified: pypy/dist/pypy/translator/c/test/test_symbolic.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_symbolic.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_symbolic.py	Wed Apr 12 21:57:16 2006
@@ -56,6 +56,26 @@
     res = fn()
     assert res == 1234501234
 
+def test_itemoffsetof_fixedsizearray():
+    ARRAY = lltype.FixedSizeArray(lltype.Signed, 5)
+    itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]
+    a = lltype.malloc(ARRAY, immortal=True)
+    def f():
+        adr = llmemory.cast_ptr_to_adr(a)
+        result = 0
+        for i in range(5):
+            a[i] = i + 1
+        for i in range(5):
+            result = result * 10 + (adr + itemoffsets[i]).signed[0]
+        for i in range(5):
+            (adr + itemoffsets[i]).signed[0] = i
+        for i in range(5):
+            result = 10 * result + a[i]
+        return result
+    fn, t = getcompiled(f, [])
+    res = fn()
+    assert res == 1234501234
+
 def test_sizeof_constsize_struct():
     # _not_ a GcStruct, since we want to raw_malloc it
     STRUCT = lltype.Struct("s", ("x", lltype.Signed), ("y", lltype.Signed))



More information about the Pypy-commit mailing list