[pypy-svn] r26980 - in pypy/dist/pypy: module/stackless/test translator translator/backendopt translator/c translator/c/test translator/goal translator/stackless translator/stackless/test

arigo at codespeak.net arigo at codespeak.net
Mon May 8 22:04:32 CEST 2006


Author: arigo
Date: Mon May  8 22:04:28 2006
New Revision: 26980

Removed:
   pypy/dist/pypy/translator/backendopt/stackless.py
Modified:
   pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py
   pypy/dist/pypy/translator/c/database.py
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/genc.py
   pypy/dist/pypy/translator/c/node.py
   pypy/dist/pypy/translator/c/test/test_stackless.py
   pypy/dist/pypy/translator/c/test/test_tasklets.py
   pypy/dist/pypy/translator/goal/targetrecursivestandalone.py
   pypy/dist/pypy/translator/goal/translate.py
   pypy/dist/pypy/translator/stackless/test/test_depth.py
   pypy/dist/pypy/translator/stackless/test/test_transform.py
   pypy/dist/pypy/translator/stackless/test/test_yield_current_frame_to_caller.py
   pypy/dist/pypy/translator/stackless/transform.py
   pypy/dist/pypy/translator/translator.py
Log:
(pedronis, arigo)
More push and pull all over the place.
Acheived here, is some integration between the stackless transform
and GenC.  The --stackless option of translate.py means the
stackless transform now; the --old-stackless option is available
for the old GenC-based stackless.py.



Modified: pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py
==============================================================================
--- pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py	(original)
+++ pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py	Mon May  8 22:04:28 2006
@@ -4,284 +4,265 @@
 
 import os
 from pypy.module.stackless.interp_coroutine import costate, Coroutine, AbstractThunk
-
-costate.__init__()
-
-def wrap_stackless_function(fn):
-    from pypy.translator.translator import TranslationContext
-    from pypy.translator.c.genc import CStandaloneBuilder
-    from pypy.annotation.model import SomeList, SomeString
-    from pypy.annotation.listdef import ListDef
-    from pypy.translator.backendopt.all import backend_optimizations
-
-    def entry_point(argv):
-        os.write(1, str(fn()))
-        return 0
-
-    s_list_of_strings = SomeList(ListDef(None, SomeString()))
-    s_list_of_strings.listdef.resize()
-    t = TranslationContext()
-    t.buildannotator().build_types(entry_point, [s_list_of_strings])
-    #t.view()
-    t.buildrtyper().specialize()
-    backend_optimizations(t)
-    cbuilder = CStandaloneBuilder(t, entry_point)
-    cbuilder.stackless = True
-    cbuilder.generate_source()
-    cbuilder.compile()
-    return cbuilder.cmdexec('')
-
+from pypy.translator.c.test.test_stackless import StacklessTest
+from pypy.translator.c import gc
 
 def output(stuff):
     os.write(2, stuff + '\n')
 
-def test_coroutine():
-    
-    def g(lst, coros):
-        coro_f, coro_g, coro_h = coros
-        lst.append(2)
-        output('g appended 2')
-        coro_h.switch()
-        lst.append(5)
-        output('g appended 5')
-
-    def h(lst, coros):
-        coro_f, coro_g, coro_h = coros
-        lst.append(3)
-        output('h appended 3')
-        coro_f.switch()
-        lst.append(7)
-        output('h appended 7')
-
-    class T(AbstractThunk):
-        def __init__(self, func, arg1, arg2):
-            self.func = func
-            self.arg1 = arg1
-            self.arg2 = arg2
-        def call(self):
-            self.func(self.arg1, self.arg2)
-
-    def f():
-        lst = [1]
-        coro_f = costate.main
-        coro_g = Coroutine()
-        coro_h = Coroutine()
-        coros = [coro_f, coro_g, coro_h]
-        thunk_g = T(g, lst, coros)
-        output('binding g after f set 1')
-        coro_g.bind(thunk_g)
-        thunk_h = T(h, lst, coros)
-        output('binding h after f set 1')
-        coro_h.bind(thunk_h)
-        output('switching to g')
-        coro_g.switch()
-        lst.append(4)
-        output('f appended 4')
-        coro_g.switch()
-        lst.append(6)
-        output('f appended 6')
-        coro_h.switch()
-        lst.append(8)
-        output('f appended 8')
-        n = 0
-        for i in lst:
-            n = n*10 + i
-        return n
-
-    data = wrap_stackless_function(f)
-    assert int(data.strip()) == 12345678
-
-def test_coroutine2():
-
-    class TBase(AbstractThunk):
-        def call(self):
-            pass
-        
-    class T(TBase):
-        def __init__(self, func, arg1, arg2):
-            self.func = func
-            self.arg1 = arg1
-            self.arg2 = arg2
-        def call(self):
-            self.res = self.func(self.arg1, self.arg2)
-
-    class T1(TBase):
-        def __init__(self, func, arg1):
-            self.func = func
-            self.arg1 = arg1
-        def call(self):
-            self.res = self.func(self.arg1)
-
-    def g(lst, coros):
-        coro_f1, coro_g, coro_h = coros
-        lst.append(2)
-        output('g appended 2')
-        coro_h.switch()
-        lst.append(5)
-        output('g appended 5')
-        output('exiting g')
-        
-    def h(lst, coros):
-        coro_f1, coro_g, coro_h = coros
-        lst.append(3)
-        output('h appended 3')
-        coro_f1.switch()
-        lst.append(7)
-        output('h appended 7')
-        output('exiting h')
-
-    def f1(coro_f1):
-        lst = [1]
-        coro_g = Coroutine()
-        coro_h = Coroutine()
-        coros = [coro_f1, coro_g, coro_h]
-        thunk_g = T(g, lst, coros)
-        output('binding g after f1 set 1')
-        coro_g.bind(thunk_g)
-        thunk_h = T(h, lst, coros)
-        output('binding h after f1 set 1')
-        coro_h.bind(thunk_h)
-        output('switching to g')
-        coro_g.switch()
-        lst.append(4)
-        output('f1 appended 4')
-        coro_g.switch()
-        lst.append(6)
-        output('f1 appended 6')
-        coro_h.switch()
-        lst.append(8)
-        output('f1 appended 8')
-        n = 0
-        for i in lst:
-            n = n*10 + i
-        output('exiting f1')
-        return n     
-
-    def f():
-        coro_f = costate.main
-        coro_f1 = Coroutine()
-        thunk_f1 = T1(f1, coro_f1)
-        output('binding f1 after f set 1')
-        coro_f1.bind(thunk_f1)
-        coro_f1.switch()        
-        output('return to main :-(')
-        return thunk_f1.res
-        
-    data = wrap_stackless_function(f)
-    assert int(data.strip()) == 12345678
-
-def test_kill_raise_del_coro():
-    class T(AbstractThunk):
-        def __init__(self, func, arg):
-            self.func = func
-            self.arg = arg
-        def call(self):
-            self.func(self.arg, self)
-
-    def g(nrec, t, count=0):
-        t.count = count
-        if nrec < 0:
-            raise ValueError
-        if nrec:
-            g(nrec-1, t, count+1)
-        costate.main.switch()
-
-    def f():
-        coro_g = Coroutine()
-        thunk_g = T(g, 42)
-        coro_g.bind(thunk_g)
-        coro_g.switch()
-        res = thunk_g.count
-        res *= 10
-        res |= coro_g.frame is not None
-        # testing kill
-        coro_g.kill()
-        res *= 10
-        res |= coro_g.frame is None
-        coro_g = Coroutine()
-        thunk_g = T(g, -42)
-        coro_g.bind(thunk_g)
-        try:
+
+class TestCoroutine(StacklessTest):
+    backendopt = True
+    stacklessmode = True
+    gcpolicy = gc.BoehmGcPolicy
+
+    def setup_meth(self):
+        costate.__init__()
+
+    def test_coroutine(self):
+
+        def g(lst, coros):
+            coro_f, coro_g, coro_h = coros
+            lst.append(2)
+            output('g appended 2')
+            coro_h.switch()
+            lst.append(5)
+            output('g appended 5')
+
+        def h(lst, coros):
+            coro_f, coro_g, coro_h = coros
+            lst.append(3)
+            output('h appended 3')
+            coro_f.switch()
+            lst.append(7)
+            output('h appended 7')
+
+        class T(AbstractThunk):
+            def __init__(self, func, arg1, arg2):
+                self.func = func
+                self.arg1 = arg1
+                self.arg2 = arg2
+            def call(self):
+                self.func(self.arg1, self.arg2)
+
+        def f():
+            lst = [1]
+            coro_f = costate.main
+            coro_g = Coroutine()
+            coro_h = Coroutine()
+            coros = [coro_f, coro_g, coro_h]
+            thunk_g = T(g, lst, coros)
+            output('binding g after f set 1')
+            coro_g.bind(thunk_g)
+            thunk_h = T(h, lst, coros)
+            output('binding h after f set 1')
+            coro_h.bind(thunk_h)
+            output('switching to g')
             coro_g.switch()
-        except ValueError:
-            res += 500
-        return res
-    
-    data = wrap_stackless_function(f)
-    assert int(data.strip()) == 4711
-
-def test_tree_compare():
-    class Node:
-        def __init__(self, value, left=None, right=None):
-            self.value = value
-            self.left = left
-            self.right = right
-        def __repr__(self):
-            return 'Node(%r, %r, %r)'%(self.value, self.left, self.right)
-
-    tree1 = Node(1, Node(2, Node(3)))
-    tree2 = Node(1, Node(3, Node(2)))
-    tree3 = Node(1, Node(2), Node(3))
-    
-    class Producer(AbstractThunk):
-        def __init__(self, tree, objects, consumer):
-            self.tree = tree
-            self.objects = objects
-            self.consumer = consumer
-        def produce(self, t):
-            if t is None:
-                return
-            self.objects.append(t.value)
-            self.consumer.switch()
-            self.produce(t.left)
-            self.produce(t.right)
-        def call(self):
-            self.produce(self.tree)
-            while 1:
-                self.consumer.switch()
-    class Consumer(AbstractThunk):
-        def __init__(self, tree, objects, producer):
-            self.tree = tree
-            self.objects = objects
-            self.producer = producer
-        def consume(self, t):
-            if t is None:
-                return True
-            self.producer.switch()
-            if not self.objects:
-                return False
-            if self.objects.pop(0) != t.value:
-                return False
-            if not self.consume(t.left):
-                return False
-            return self.consume(t.right)
-            
-        def call(self):
-            self.result = self.consume(self.tree)
+            lst.append(4)
+            output('f appended 4')
+            coro_g.switch()
+            lst.append(6)
+            output('f appended 6')
+            coro_h.switch()
+            lst.append(8)
+            output('f appended 8')
+            n = 0
+            for i in lst:
+                n = n*10 + i
+            return n
+
+        data = self.wrap_stackless_function(f)
+        assert data == 12345678
+
+    def test_coroutine2(self):
+
+        class TBase(AbstractThunk):
+            def call(self):
+                pass
+
+        class T(TBase):
+            def __init__(self, func, arg1, arg2):
+                self.func = func
+                self.arg1 = arg1
+                self.arg2 = arg2
+            def call(self):
+                self.res = self.func(self.arg1, self.arg2)
+
+        class T1(TBase):
+            def __init__(self, func, arg1):
+                self.func = func
+                self.arg1 = arg1
+            def call(self):
+                self.res = self.func(self.arg1)
+
+        def g(lst, coros):
+            coro_f1, coro_g, coro_h = coros
+            lst.append(2)
+            output('g appended 2')
+            coro_h.switch()
+            lst.append(5)
+            output('g appended 5')
+            output('exiting g')
+
+        def h(lst, coros):
+            coro_f1, coro_g, coro_h = coros
+            lst.append(3)
+            output('h appended 3')
+            coro_f1.switch()
+            lst.append(7)
+            output('h appended 7')
+            output('exiting h')
+
+        def f1(coro_f1):
+            lst = [1]
+            coro_g = Coroutine()
+            coro_h = Coroutine()
+            coros = [coro_f1, coro_g, coro_h]
+            thunk_g = T(g, lst, coros)
+            output('binding g after f1 set 1')
+            coro_g.bind(thunk_g)
+            thunk_h = T(h, lst, coros)
+            output('binding h after f1 set 1')
+            coro_h.bind(thunk_h)
+            output('switching to g')
+            coro_g.switch()
+            lst.append(4)
+            output('f1 appended 4')
+            coro_g.switch()
+            lst.append(6)
+            output('f1 appended 6')
+            coro_h.switch()
+            lst.append(8)
+            output('f1 appended 8')
+            n = 0
+            for i in lst:
+                n = n*10 + i
+            output('exiting f1')
+            return n     
+
+        def f():
+            coro_f = costate.main
+            coro_f1 = Coroutine()
+            thunk_f1 = T1(f1, coro_f1)
+            output('binding f1 after f set 1')
+            coro_f1.bind(thunk_f1)
+            coro_f1.switch()        
+            output('return to main :-(')
+            return thunk_f1.res
+
+        data = self.wrap_stackless_function(f)
+        assert data == 12345678
+
+    def test_kill_raise_del_coro(self):
+        class T(AbstractThunk):
+            def __init__(self, func, arg):
+                self.func = func
+                self.arg = arg
+            def call(self):
+                self.func(self.arg, self)
+
+        def g(nrec, t, count=0):
+            t.count = count
+            if nrec < 0:
+                raise ValueError
+            if nrec:
+                g(nrec-1, t, count+1)
             costate.main.switch()
 
-    def pre_order_eq(t1, t2):
-        objects = []
-        producer = Coroutine()
-        consumer = Coroutine()
-        
-        producer.bind(Producer(t1, objects, consumer))
-        cons = Consumer(t2, objects, producer)
-        consumer.bind(cons)
-
-        consumer.switch()
-
-        return cons.result
-
-    def ep():
-        return "%d %d %d %d"%(pre_order_eq(tree1, tree2),
-                              pre_order_eq(tree1, tree1),
-                              pre_order_eq(tree1, tree3),
-                              pre_order_eq(tree2, tree1),
-
-                              )
-    
-    output = wrap_stackless_function(ep)
-    assert output.strip() == '0 1 1 0'
-            
-            
+        def f():
+            coro_g = Coroutine()
+            thunk_g = T(g, 42)
+            coro_g.bind(thunk_g)
+            coro_g.switch()
+            res = thunk_g.count
+            res *= 10
+            res |= coro_g.frame is not None
+            # testing kill
+            coro_g.kill()
+            res *= 10
+            res |= coro_g.frame is None
+            coro_g = Coroutine()
+            thunk_g = T(g, -42)
+            coro_g.bind(thunk_g)
+            try:
+                coro_g.switch()
+            except ValueError:
+                res += 500
+            return res
+
+        data = self.wrap_stackless_function(f)
+        assert data == 4711
+
+    def test_tree_compare(self):
+        class Node:
+            def __init__(self, value, left=None, right=None):
+                self.value = value
+                self.left = left
+                self.right = right
+            def __repr__(self):
+                return 'Node(%r, %r, %r)'%(self.value, self.left, self.right)
+
+        tree1 = Node(1, Node(2, Node(3)))
+        tree2 = Node(1, Node(3, Node(2)))
+        tree3 = Node(1, Node(2), Node(3))
+
+        class Producer(AbstractThunk):
+            def __init__(self, tree, objects, consumer):
+                self.tree = tree
+                self.objects = objects
+                self.consumer = consumer
+            def produce(self, t):
+                if t is None:
+                    return
+                self.objects.append(t.value)
+                self.consumer.switch()
+                self.produce(t.left)
+                self.produce(t.right)
+            def call(self):
+                self.produce(self.tree)
+                while 1:
+                    self.consumer.switch()
+        class Consumer(AbstractThunk):
+            def __init__(self, tree, objects, producer):
+                self.tree = tree
+                self.objects = objects
+                self.producer = producer
+            def consume(self, t):
+                if t is None:
+                    return True
+                self.producer.switch()
+                if not self.objects:
+                    return False
+                if self.objects.pop(0) != t.value:
+                    return False
+                if not self.consume(t.left):
+                    return False
+                return self.consume(t.right)
+
+            def call(self):
+                self.result = self.consume(self.tree)
+                costate.main.switch()
+
+        def pre_order_eq(t1, t2):
+            objects = []
+            producer = Coroutine()
+            consumer = Coroutine()
+
+            producer.bind(Producer(t1, objects, consumer))
+            cons = Consumer(t2, objects, producer)
+            consumer.bind(cons)
+
+            consumer.switch()
+
+            return cons.result
+
+        def ep():
+            return int("%d%d%d%d"%(pre_order_eq(tree1, tree2),
+                                   pre_order_eq(tree1, tree1),
+                                   pre_order_eq(tree1, tree3),
+                                   pre_order_eq(tree2, tree1),
+                                   ))
+
+        output = self.wrap_stackless_function(ep)
+        assert output == int('0110')

Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py	(original)
+++ pypy/dist/pypy/translator/c/database.py	Mon May  8 22:04:28 2006
@@ -21,6 +21,7 @@
 # ____________________________________________________________
 
 class LowLevelDatabase(object):
+    stacklesstransformer = None
 
     def __init__(self, translator=None, standalone=False, gcpolicy=None, thread_enabled=False):
         self.translator = translator

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Mon May  8 22:04:28 2006
@@ -33,12 +33,16 @@
                        blocknum
                        oldgraph""".split()
 
-    def __init__(self, graph, db, cpython_exc=False, functionname=None):
+    def __init__(self, graph, db, cpython_exc=False, functionname=None,
+                 do_stackless=True):
         self.graph = graph
         self.db = db
         self.gcpolicy = db.gcpolicy
         self.cpython_exc = cpython_exc
         self.functionname = functionname
+        # apply the stackless transformation
+        if db.stacklesstransformer and do_stackless:
+            db.stacklesstransformer.transform_graph(graph)
         # apply the exception transformation
         if self.db.exctransformer:
             self.db.exctransformer.create_exception_handling(self.graph)

Modified: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/genc.py	(original)
+++ pypy/dist/pypy/translator/c/genc.py	Mon May  8 22:04:28 2006
@@ -20,12 +20,12 @@
     _compiled = False
     symboltable = None
     stackless = False
-    use_stackless_transformation = False
     modulename = None
     
     def __init__(self, translator, entrypoint, gcpolicy=None, libraries=None, thread_enabled=False):
         self.translator = translator
         self.entrypoint = entrypoint
+        self.originalentrypoint = entrypoint
         self.gcpolicy = gcpolicy
         self.thread_enabled = thread_enabled
 
@@ -39,10 +39,19 @@
         db = LowLevelDatabase(translator, standalone=self.standalone, 
                               gcpolicy=self.gcpolicy, thread_enabled=self.thread_enabled)
 
+        assert self.stackless in (False, 'old', True)
         if self.stackless:
-            from pypy.translator.c.stackless import StacklessData
-            db.stacklessdata = StacklessData(db)
-            db.use_stackless_transformation = self.use_stackless_transformation
+            if not self.standalone:
+                raise Exception("stackless: only for stand-alone builds")
+            if self.stackless == 'old':
+                from pypy.translator.c.stackless import StacklessData
+                db.stacklessdata = StacklessData(db)
+            else:
+                from pypy.translator.stackless.transform import \
+                                                       StacklessTransformer
+                db.stacklesstransformer = StacklessTransformer(translator,
+                                                       self.originalentrypoint)
+                self.entrypoint = db.stacklesstransformer.slp_entry_point
 
         # pass extra options into pyobjmaker
         if pyobj_options:
@@ -107,12 +116,9 @@
         else:
             if CBuilder.have___thread:
                 defines['HAVE___THREAD'] = 1
-            if self.stackless:
+            if self.stackless == 'old':
                 defines['USE_STACKLESS'] = '1'
                 defines['USE_OPTIMIZED_STACKLESS_UNWIND'] = '1'
-                if self.use_stackless_transformation: #set in test_stackless.py
-                    from pypy.translator.backendopt.stackless import stackless
-                    stackless(translator, db.stacklessdata)
             cfile, extra = gen_source_standalone(db, modulename, targetdir,
                                                  entrypointname = pfname,
                                                  defines = defines)
@@ -573,7 +579,7 @@
     sg.set_strategy(targetdir)
     sg.gen_readable_parts_of_source(f)
 
-    # 2bis) stackless data
+    # 2bis) old-style stackless data
     if hasattr(database, 'stacklessdata'):
         database.stacklessdata.writefiles(sg)
 

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Mon May  8 22:04:28 2006
@@ -644,7 +644,7 @@
             fnobj._callable,)
     elif hasattr(fnobj, 'graph'):
         cpython_exc = getattr(fnobj, 'exception_policy', None) == "CPython"
-        if hasattr(db, 'stacklessdata') and not db.use_stackless_transformation:
+        if hasattr(db, 'stacklessdata'):
             split_slp_function = False
             if split_slp_function:
                 from pypy.translator.c.stackless import SlpSaveOnlyFunctionCodeGenerator, \
@@ -655,10 +655,8 @@
                 from pypy.translator.c.stackless import SlpFunctionCodeGenerator
                 return [SlpFunctionCodeGenerator(fnobj.graph, db, cpython_exc, functionname)]
         else:
-            if db.translator and db.translator.stacklesstransformer is not None:
-                if not hasattr(fnobj, 'isgchelper'):
-                    db.translator.stacklesstransformer.transform_graph(fnobj.graph)
-            return [FunctionCodeGenerator(fnobj.graph, db, cpython_exc, functionname)]
+            return [FunctionCodeGenerator(fnobj.graph, db, cpython_exc, functionname,
+                       do_stackless = not getattr(fnobj, 'isgchelper', False))]
     elif getattr(fnobj, 'external', None) == 'C':
         # deprecated case
         if hasattr(fnobj, 'includes'):

Modified: pypy/dist/pypy/translator/c/test/test_stackless.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_stackless.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_stackless.py	Mon May  8 22:04:28 2006
@@ -11,6 +11,7 @@
 
 class StacklessTest(object):
     backendopt = False
+    stacklessmode = 'old'
 
     def setup_class(cls):
         import py
@@ -43,8 +44,7 @@
         insert_ll_stackcheck(t)
 
         cbuilder = CStandaloneBuilder(t, entry_point, gcpolicy=self.gcpolicy)
-        cbuilder.stackless = True
-        #cbuilder.use_stackless_transformation = True
+        cbuilder.stackless = self.stacklessmode
         cbuilder.generate_source()
         cbuilder.compile()
         res = cbuilder.cmdexec('')
@@ -306,9 +306,4 @@
 # ____________________________________________________________
 
 class TestStacklessTransformBoehm(TestStacklessBoehm):
-
-    def wrap_stackless_function(self, fn):
-        # temporary way of doing this
-        #import py; py.test.skip("in-progress")
-        from pypy.translator.stackless.test import test_transform
-        return test_transform.run_stackless_function(fn)
+    stacklessmode = True

Modified: pypy/dist/pypy/translator/c/test/test_tasklets.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_tasklets.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_tasklets.py	Mon May  8 22:04:28 2006
@@ -500,9 +500,4 @@
 # ____________________________________________________________
 
 class TestTaskletsStacklessTransform(TestTasklets):
-
-    def wrap_stackless_function(self, fn):
-        # temporary way of doing this
-        #import py; py.test.skip("in-progress")
-        from pypy.translator.stackless.test import test_transform
-        return test_transform.run_stackless_function(fn)
+    stacklessmode = True

Modified: pypy/dist/pypy/translator/goal/targetrecursivestandalone.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetrecursivestandalone.py	(original)
+++ pypy/dist/pypy/translator/goal/targetrecursivestandalone.py	Mon May  8 22:04:28 2006
@@ -22,7 +22,7 @@
     return ackermann(x - 1, ackermann(x, y - 1))
 
 def entry_point(argv):
-    debug(str(ackermann(3, 27)) + "\n")
+    debug(str(ackermann(3, 12)) + "\n")
     return 0
 
 # _____ Define and setup target ___

Modified: pypy/dist/pypy/translator/goal/translate.py
==============================================================================
--- pypy/dist/pypy/translator/goal/translate.py	(original)
+++ pypy/dist/pypy/translator/goal/translate.py	Mon May  8 22:04:28 2006
@@ -50,7 +50,8 @@
 
     '2_gc': [OPT(('--gc',), "Garbage collector", ['boehm', 'ref', 'framework', 'none', 'exact_boehm'])],
     '3_stackless': [OPT(('--stackless',), "Stackless code generation", True)],
-    '4_merge_if_blocks': [OPT(('--no-if-blocks-merge',), "Do not merge if ... elif ... chains and use a switch statement for them.", False)],
+    '4_stackless': [OPT(('--old-stackless',), "Stackless code generation (old GenC way)", 'old')],
+    '5_merge_if_blocks': [OPT(('--no-if-blocks-merge',), "Do not merge if ... elif ... chains and use a switch statement for them.", False)],
     },
 
 
@@ -192,6 +193,9 @@
                     opt_setup['type'] = 'int'
                 elif choice is str:
                     opt_setup['type'] = 'string'
+                else:
+                    opt_setup['action'] = 'store_const'
+                    opt_setup['const'] = choice
 
                 grp.add_option(*names, **opt_setup)
 

Modified: pypy/dist/pypy/translator/stackless/test/test_depth.py
==============================================================================
--- pypy/dist/pypy/translator/stackless/test/test_depth.py	(original)
+++ pypy/dist/pypy/translator/stackless/test/test_depth.py	Mon May  8 22:04:28 2006
@@ -1,100 +1,89 @@
 from pypy.translator.stackless.test.test_transform import \
      llinterp_stackless_function, run_stackless_function
-from pypy.translator.stackless import code
 from pypy.rpython import rstack
 import py
 import os
 
 
-class TestFromCode:
-    stack_frames_depth = staticmethod(code.stack_frames_depth)
+def test_simple():
+    def g1():
+        "just to check Void special cases around the code"
+    def g2(ignored):
+        pass
+        g1()
+    def f(n):
+        g1()
+        if n > 0:
+            res = f(n-1)
+        else:
+            res = rstack.stack_frames_depth()
+        g2(g1)
+        return res
+
+    def fn():
+        count0 = f(0)
+        count10 = f(10)
+        return count10 - count0
+
+    res = llinterp_stackless_function(fn)
+    assert res == 10
+
+    res = run_stackless_function(fn)
+    assert res == 10
+
+def test_with_ptr():
+    def f(n):
+        if n > 0:
+            res = f(n-1)
+        else:
+            res = rstack.stack_frames_depth(), 1
+        return res
+
+    def fn():
+        count0, _ = f(0)
+        count10, _ = f(10)
+        return count10 - count0
+
+    res = llinterp_stackless_function(fn)
+    assert res == 10
+
+    res = run_stackless_function(fn)
+    assert res == 10
+
+def test_manytimes():
+    def f(n):
+        if n > 0:
+            res = f(n-1)
+        else:
+            res = rstack.stack_frames_depth(), 1
+        return res
+
+    def fn():
+        count0, _ = f(0)
+        count10, _ = f(100)
+        return count10 - count0
+
+    res = llinterp_stackless_function(fn)
+    assert res == 100
+
+    res = run_stackless_function(fn)
+    assert res == 100
+
+def test_arguments():
+    def f(n, d, t):
+        if n > 0:
+            res = f(n-1, d, t)
+        else:
+            res = rstack.stack_frames_depth(), d, t
+        return res
+
+    def fn():
+        count0, d, t = f(0, 5.5, (1, 2))
+        count10, d, t = f(10, 5.5, (1, 2))
+        return count10 - count0 + int(d)
 
-    def _freeze_(self):
-        return True
+    res = llinterp_stackless_function(fn)
+    assert res == 15
 
-    def test_simple(self):
-        def g1():
-            "just to check Void special cases around the code"
-        def g2(ignored):
-            pass
-            g1()
-        def f(n):
-            g1()
-            if n > 0:
-                res = f(n-1)
-            else:
-                res = self.stack_frames_depth()
-            g2(g1)
-            return res
-
-        def fn():
-            count0 = f(0)
-            count10 = f(10)
-            return count10 - count0
-
-        res = llinterp_stackless_function(fn)
-        assert res == 10
-
-        res = run_stackless_function(fn)
-        assert res == 10
-
-    def test_with_ptr(self):
-        def f(n):
-            if n > 0:
-                res = f(n-1)
-            else:
-                res = self.stack_frames_depth(), 1
-            return res
-
-        def fn():
-            count0, _ = f(0)
-            count10, _ = f(10)
-            return count10 - count0
-
-        res = llinterp_stackless_function(fn)
-        assert res == 10
-
-        res = run_stackless_function(fn)
-        assert res == 10
-
-    def test_manytimes(self):
-        def f(n):
-            if n > 0:
-                res = f(n-1)
-            else:
-                res = self.stack_frames_depth(), 1
-            return res
-
-        def fn():
-            count0, _ = f(0)
-            count10, _ = f(100)
-            return count10 - count0
-
-        res = llinterp_stackless_function(fn)
-        assert res == 100
-
-        res = run_stackless_function(fn)
-        assert res == 100
-
-    def test_arguments(self):
-        def f(n, d, t):
-            if n > 0:
-                res = f(n-1, d, t)
-            else:
-                res = self.stack_frames_depth(), d, t
-            return res
-
-        def fn():
-            count0, d, t = f(0, 5.5, (1, 2))
-            count10, d, t = f(10, 5.5, (1, 2))
-            return count10 - count0 + int(d)
-
-        res = llinterp_stackless_function(fn)
-        assert res == 15
-
-        res = run_stackless_function(fn)
-        assert res == 15
-
-
-class TestFromRStack(TestFromCode):
-    stack_frames_depth = staticmethod(rstack.stack_frames_depth)
+    res = run_stackless_function(fn)
+    assert res == 15

Modified: pypy/dist/pypy/translator/stackless/test/test_transform.py
==============================================================================
--- pypy/dist/pypy/translator/stackless/test/test_transform.py	(original)
+++ pypy/dist/pypy/translator/stackless/test/test_transform.py	Mon May  8 22:04:28 2006
@@ -165,17 +165,6 @@
     t = TranslationContext()
     annotator = t.buildannotator()
     annotator.policy.allow_someobjects = False
-    bk = annotator.bookkeeper
-    # we want to make sure that the annotator knows what
-    # code.UnwindException looks like very early on, because otherwise
-    # it can get mutated during the annotation of the low level
-    # helpers which can cause slp_main_loop to get re-annotated after
-    # it is rtyped.  which is bad.
-    unwind_def = bk.getuniqueclassdef(code.UnwindException)
-    unwind_def.generalize_attr('frame_bottom',
-                               annmodel.SomePtr(lltype.Ptr(code.STATE_HEADER)))
-    attrdef = unwind_def.attrs['frame_bottom']
-    attrdef.readonly = False
 
     s_returnvar = annotator.build_types(fn, [s_list_of_strings])
     if not isinstance(s_returnvar, annmodel.SomeInteger):
@@ -191,20 +180,14 @@
     
 def run_stackless_function(fn):
     def entry_point(argv):
-        try:
-            r = fn()
-        except code.UnwindException, u:
-            code.slp_main_loop()
-            r = code.global_state.retval_long
+        r = fn()
         os.write(1, str(r)+'\n')
         return 0
-    entry_point.stackless_explicit = True
 
     t = rtype_stackless_function(entry_point)
 
-    t.stacklesstransformer = StacklessTransformer(t)
-
     cbuilder = CStandaloneBuilder(t, entry_point, gcpolicy=gc.BoehmGcPolicy)
+    cbuilder.stackless = True
     cbuilder.generate_source()
     if conftest.option.view:
         t.view()
@@ -213,24 +196,18 @@
     return int(res.strip())
 
 def llinterp_stackless_function(fn):
-    def entry_point(argv):
-        try:
-            r = fn()
-        except code.UnwindException, u:
-            code.slp_main_loop()
-            return code.global_state.retval_long
-        return r
-    entry_point.stackless_explicit = True
-
-    t = rtype_stackless_function(entry_point)
-    st = StacklessTransformer(t)
+    def wrapper(argv):
+        return fn()
+    t = rtype_stackless_function(wrapper)
+    st = StacklessTransformer(t, wrapper)
     st.transform_all()
     if conftest.option.view:
         t.view()
 
+    graph = graphof(t, st.slp_entry_point)
     r_list_of_strings = t.rtyper.getrepr(
-        t.annotator.binding(graphof(t, entry_point).startblock.inputargs[0]))
+        t.annotator.binding(graph.startblock.inputargs[0]))
     ll_list = r_list_of_strings.convert_const([''])
     interp = llinterp.LLInterpreter(t.rtyper)
-    res = interp.eval_graph(graphof(t, entry_point), [ll_list])
+    res = interp.eval_graph(graph, [ll_list])
     return res

Modified: pypy/dist/pypy/translator/stackless/test/test_yield_current_frame_to_caller.py
==============================================================================
--- pypy/dist/pypy/translator/stackless/test/test_yield_current_frame_to_caller.py	(original)
+++ pypy/dist/pypy/translator/stackless/test/test_yield_current_frame_to_caller.py	Mon May  8 22:04:28 2006
@@ -1,84 +1,66 @@
 from pypy.translator.stackless.test.test_transform import \
      llinterp_stackless_function, run_stackless_function
-from pypy.translator.stackless import code
 from pypy.rpython import rstack
 import py
 import os
 
 
-class TestFromCode:
-    yield_current_frame_to_caller = staticmethod(
-        code.yield_current_frame_to_caller)
-    switch = staticmethod(code.ll_frame_switch)
-
-    def _freeze_(self):
-        return True
-
-    def test_simple(self):
-        def f():
-            c = g()
-            assert c
-            return 1
-        def g():
-            d = self.yield_current_frame_to_caller()
-            return d
-
-        data = llinterp_stackless_function(f)
-        assert data == 1
-
-        res = run_stackless_function(f)
-        assert res == 1
-
-    def test_switch(self):
-        def f():
-            c = g()
-            self.switch(c)
-            return 1
-        def g():
-            d = self.yield_current_frame_to_caller()
-            return d
-
-        data = llinterp_stackless_function(f)
-        assert data == 1
-
-        res = run_stackless_function(f)
-        assert res == 1
-
-    def test_yield_frame(self):
-
-        def g(lst):
-            lst.append(2)
-            frametop_before_5 = self.yield_current_frame_to_caller()
-            lst.append(4)
-            frametop_before_7 = self.switch(frametop_before_5)
-            lst.append(6)
-            return frametop_before_7
-
-        def f():
-            lst = [1]
-            frametop_before_4 = g(lst)
-            lst.append(3)
-            frametop_before_6 = self.switch(frametop_before_4)
-            lst.append(5)
-            frametop_after_return = self.switch(frametop_before_6)
-            lst.append(7)
-            assert not frametop_after_return
-            n = 0
-            for i in lst:
-                n = n*10 + i
-            return n
-
-        data = llinterp_stackless_function(f)
-        assert data == 1234567
-
-        res = run_stackless_function(f)
-        assert res == 1234567
-
-
-class TestFromRStack(TestFromCode):
-    yield_current_frame_to_caller = staticmethod(
-        rstack.yield_current_frame_to_caller)
-
-    def switch(state):
-        return state.switch()
-    switch = staticmethod(switch)
+def test_simple():
+    def f():
+        c = g()
+        assert c
+        return 1
+    def g():
+        d = rstack.yield_current_frame_to_caller()
+        return d
+
+    data = llinterp_stackless_function(f)
+    assert data == 1
+
+    res = run_stackless_function(f)
+    assert res == 1
+
+def test_switch():
+    def f():
+        c = g()
+        c.switch()
+        return 1
+    def g():
+        d = rstack.yield_current_frame_to_caller()
+        return d
+
+    data = llinterp_stackless_function(f)
+    assert data == 1
+
+    res = run_stackless_function(f)
+    assert res == 1
+
+def test_yield_frame():
+
+    def g(lst):
+        lst.append(2)
+        frametop_before_5 = rstack.yield_current_frame_to_caller()
+        lst.append(4)
+        frametop_before_7 = frametop_before_5.switch()
+        lst.append(6)
+        return frametop_before_7
+
+    def f():
+        lst = [1]
+        frametop_before_4 = g(lst)
+        lst.append(3)
+        frametop_before_6 = frametop_before_4.switch()
+        lst.append(5)
+        frametop_after_return = frametop_before_6.switch()
+        lst.append(7)
+        assert not frametop_after_return
+        n = 0
+        for i in lst:
+            n = n*10 + i
+        return n
+
+    data = llinterp_stackless_function(f)
+    assert data == 1234567
+
+    res = run_stackless_function(f)
+    assert res == 1234567

Modified: pypy/dist/pypy/translator/stackless/transform.py
==============================================================================
--- pypy/dist/pypy/translator/stackless/transform.py	(original)
+++ pypy/dist/pypy/translator/stackless/transform.py	Mon May  8 22:04:28 2006
@@ -31,7 +31,7 @@
     elif T in [lltype.SignedLongLong, lltype.UnsignedLongLong]:
         return lltype.SignedLongLong
     elif T is llmemory.Address:
-        return lltype.Address
+        return llmemory.Address
     elif isinstance(T, lltype.Primitive):
         return lltype.Signed
     else:
@@ -80,7 +80,7 @@
         self.fieldnames = fieldnames
 
 class StacklessTransformer(object):
-    def __init__(self, translator):
+    def __init__(self, translator, entrypoint):
         self.translator = translator
 
         edata = translator.rtyper.getexceptiondata()
@@ -95,6 +95,21 @@
         mixlevelannotator = MixLevelHelperAnnotator(translator.rtyper)
         l2a = annmodel.lltype_to_annotation
 
+        def slp_entry_point(argv):
+            try:
+                r = entrypoint(argv)
+            except code.UnwindException, u:
+                code.slp_main_loop()
+                return code.global_state.retval_long
+            return r
+        slp_entry_point.stackless_explicit = True
+
+        self.slp_entry_point = slp_entry_point
+        oldgraph = bk.getdesc(entrypoint).cachedgraph(None)
+        s_argv = translator.annotator.binding(oldgraph.getargs()[0])
+        self.slp_entry_point_ptr = mixlevelannotator.constfunc(
+            slp_entry_point, [s_argv], annmodel.SomeInteger())
+
         unwinddef = bk.getuniqueclassdef(code.UnwindException)
         self.add_frame_state_ptr = mixlevelannotator.constfunc(
             code.add_frame_state,

Modified: pypy/dist/pypy/translator/translator.py
==============================================================================
--- pypy/dist/pypy/translator/translator.py	(original)
+++ pypy/dist/pypy/translator/translator.py	Mon May  8 22:04:28 2006
@@ -30,7 +30,6 @@
             raise TypeError("unexpected keyword argument")
         self.annotator = None
         self.rtyper = None
-        self.stacklesstransformer = None
         self.graphs = []      # [graph]
         self.callgraph = {}   # {opaque_tag: (caller-graph, callee-graph)}
         self._prebuilt_graphs = {}   # only used by the pygame viewer



More information about the Pypy-commit mailing list