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

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Jan 28 10:14:04 CET 2006


Author: cfbolz
Date: Sat Jan 28 10:14:02 2006
New Revision: 22762

Modified:
   pypy/dist/pypy/translator/c/newfuncgen.py
   pypy/dist/pypy/translator/c/test/test_newgc.py
Log:
added all the gctransform tests to the tests for the new function generator.
moved the operation generation into an method on the function generator because
we will need it somewhere else


Modified: pypy/dist/pypy/translator/c/newfuncgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/newfuncgen.py	(original)
+++ pypy/dist/pypy/translator/c/newfuncgen.py	Sat Jan 28 10:14:02 2006
@@ -204,20 +204,7 @@
             reachable_err = -1   # the number of the first reachable err label
             for op in block.operations:
                 err   = 'err%d_%d' % (myblocknum, len(to_release))
-                macro = 'OP_%s' % op.opname.upper()
-                if op.opname.startswith('gc_'):
-                    meth = getattr(self.gcpolicy, macro, None)
-                    if meth:
-                        line = meth(self, op, err)
-                else:
-                    meth = getattr(self, macro, None)
-                    if meth:
-                        line = meth(op, err)
-                if meth is None:
-                    lst = [self.expr(v) for v in op.args]
-                    lst.append(self.expr(op.result))
-                    lst.append(err)
-                    line = '%s(%s);' % (macro, ', '.join(lst))
+                line = self.gen_op(op, err)
                 if '\n' in line:
                     for subline in line.split('\n'):
                         yield subline
@@ -359,6 +346,23 @@
                 else:
                     yield self.pop_alive(to_release[i-1])
 
+    def gen_op(self, op, err):
+        macro = 'OP_%s' % op.opname.upper()
+        if op.opname.startswith('gc_'):
+            meth = getattr(self.gcpolicy, macro, None)
+            if meth:
+                line = meth(self, op, err)
+        else:
+            meth = getattr(self, macro, None)
+            if meth:
+                line = meth(op, err)
+        if meth is None:
+            lst = [self.expr(v) for v in op.args]
+            lst.append(self.expr(op.result))
+            lst.append(err)
+            line = '%s(%s);' % (macro, ', '.join(lst))
+        return line
+
     # ____________________________________________________________
 
     # the C preprocessor cannot handle operations taking a variable number

Modified: pypy/dist/pypy/translator/c/test/test_newgc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_newgc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_newgc.py	Sat Jan 28 10:14:02 2006
@@ -38,3 +38,75 @@
         return s.x
     fn = compile_func(f, [int])
     assert fn(1) == 1
+
+def test_call_function():
+    class C:
+        pass
+    def f():
+        c = C()
+        c.x = 1
+        return c
+    def g():
+        return f().x
+    fn = compile_func(g, [])
+    assert fn() == 1
+
+def test_multiple_exits():
+    S = lltype.GcStruct("S", ('x', lltype.Signed))
+    T = lltype.GcStruct("T", ('y', lltype.Signed))
+    def f(n):
+        c = lltype.malloc(S)
+        d = lltype.malloc(T)
+        d.y = 1
+        e = lltype.malloc(T)
+        e.y = 2
+        if n:
+            x = d
+        else:
+            x = e
+        return x.y
+    fn = compile_func(f, [int])
+    assert fn(1) == 1
+    assert fn(0) == 2
+
+
+def test_cleanup_vars_on_call():
+    S = lltype.GcStruct("S", ('x', lltype.Signed))
+    def f():
+        return lltype.malloc(S)
+    def g():
+        s1 = f()
+        s1.x = 42
+        s2 = f()
+        s3 = f()
+        return s1.x
+    fn = compile_func(g, [])
+    assert fn() == 42
+
+def test_multiply_passed_var():
+    S = lltype.GcStruct("S", ('x', lltype.Signed))
+    def f(x):
+        if x:
+            a = lltype.malloc(S)
+            a.x = 1
+            b = a
+        else:
+            a = lltype.malloc(S)
+            a.x = 1
+            b = lltype.malloc(S)
+            b.x = 2
+        return a.x + b.x
+    fn = compile_func(f, [int])
+    fn(1) == 2
+    fn(0) == 3
+
+def test_pyobj():
+    def f(x):
+        if x:
+            a = 1
+        else:
+            a = "1"
+        return int(a)
+    fn = compile_func(f, [int])
+    assert fn(1) == 1
+#    assert fn(0) == 0 #XXX this should work but it's not my fault



More information about the Pypy-commit mailing list