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

arigo at codespeak.net arigo at codespeak.net
Tue May 31 16:10:50 CEST 2005


Author: arigo
Date: Tue May 31 16:10:50 2005
New Revision: 12930

Modified:
   pypy/dist/pypy/translator/c/database.py
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/test/test_genc.py
Log:
Support for low-level nonzero, == and != operations in translator/c.
All pointers can be NULL for now without crashing the incref/decref code.


Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py	(original)
+++ pypy/dist/pypy/translator/c/database.py	Tue May 31 16:10:50 2005
@@ -105,23 +105,24 @@
     def cincrefstmt(self, expr, T):
         if isinstance(T, _PtrType) and 'gc' in T.flags:
             if T.TO == PyObject:
-                return 'Py_INCREF(%s);' % expr
+                return 'Py_XINCREF(%s);' % expr
             else:
                 defnode = self.gettypedefnode(T.TO)
                 if defnode.refcount is not None:
-                    return '%s->%s++;' % (expr, defnode.refcount)
+                    return 'if (%s) %s->%s++;' % (expr, expr, defnode.refcount)
         return ''
 
     def cdecrefstmt(self, expr, T):
         if isinstance(T, _PtrType) and 'gc' in T.flags:
             if T.TO == PyObject:
-                return 'Py_DECREF(%s);' % expr
+                return 'Py_XDECREF(%s);' % expr
             else:
                 defnode = self.gettypedefnode(T.TO)
                 if defnode.refcount is not None:
-                    return 'if (!--%s->%s) %s(%s);' % (expr, defnode.refcount,
+                    return 'if (%s && !--%s->%s) %s(%s);' % (expr, expr,
+                                                             defnode.refcount,
                                                defnode.deallocator or 'OP_FREE',
-                                                       expr)
+                                                             expr)
         return ''
 
     def complete(self):

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Tue May 31 16:10:50 2005
@@ -329,7 +329,7 @@
             result.insert(0, '{ %s = %s;' % (
                 cdecl(self.typemap[op.args[2]], 'prev'),
                 oldvalue))
-            result.append('if (prev) ' + decrefstmt)
+            result.append(decrefstmt)
             result.append('}')
         return '\t'.join(result)
 
@@ -345,6 +345,20 @@
         return '%s = %s->length;' % (self.expr(op.result),
                                      self.expr(op.args[0]))
 
+    def OP_PTR_NONZERO(self, op, err):
+        return '%s = (%s != NULL);' % (self.expr(op.result),
+                                       self.expr(op.args[0]))
+
+    def OP_PTR_EQ(self, op, err):
+        return '%s = (%s == %s);' % (self.expr(op.result),
+                                     self.expr(op.args[0]),
+                                     self.expr(op.args[1]))
+
+    def OP_PTR_NE(self, op, err):
+        return '%s = (%s != %s);' % (self.expr(op.result),
+                                     self.expr(op.args[0]),
+                                     self.expr(op.args[1]))
+
     def OP_MALLOC(self, op, err):
         TYPE = self.lltypemap[op.result].TO
         typename = self.db.gettype(TYPE)

Modified: pypy/dist/pypy/translator/c/test/test_genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_genc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_genc.py	Tue May 31 16:10:50 2005
@@ -62,6 +62,7 @@
     py.test.raises(TypeError, f1)
     py.test.raises(TypeError, f1, 2, 3)
     py.test.raises(TypeError, f1, 2, x=2)
+    #py.test.raises(TypeError, f1, 2, y=2)   XXX missing a check at the moment
     assert module.malloc_counters() == (0, 0)
 
 
@@ -86,3 +87,37 @@
     assert f1(x=5) == 30
     mallocs, frees = module.malloc_counters()
     assert mallocs == frees
+
+
+def test_rptr():
+    S = GcStruct('testing', ('x', Signed), ('y', Signed))
+    def f(i):
+        if i < 0:
+            p = nullgcptr(S)
+        else:
+            p = malloc(S)
+            p.x = i*2
+        if i > 0:
+            return p.x
+        else:
+            return -42
+    t = Translator(f)
+    a = t.annotate([int])
+    rtyper = RPythonTyper(t.annotator)
+    rtyper.specialize()
+
+    db = LowLevelDatabase(rtyper)
+    entrypoint = db.get(pyobjectptr(f))
+    db.complete()
+    #t.view()
+    module = compile_db(db)
+
+    f1 = getattr(module, entrypoint)
+    assert f1(5) == 10
+    assert f1(i=5) == 10
+    assert f1(1) == 2
+    assert f1(0) == -42
+    assert f1(-1) == -42
+    assert f1(-5) == -42
+    mallocs, frees = module.malloc_counters()
+    assert mallocs == frees



More information about the Pypy-commit mailing list