[pypy-svn] pypy default: Add call_initial_function(), similar to call_final_function().

arigo commits-noreply at bitbucket.org
Wed Feb 2 16:33:42 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r41570:e512a5378402
Date: 2011-02-02 14:51 +0100
http://bitbucket.org/pypy/pypy/changeset/e512a5378402/

Log:	Add call_initial_function(), similar to call_final_function().

diff --git a/pypy/translator/unsimplify.py b/pypy/translator/unsimplify.py
--- a/pypy/translator/unsimplify.py
+++ b/pypy/translator/unsimplify.py
@@ -155,6 +155,31 @@
     if links_to_start_block:
         insert_empty_startblock(None, graph)
 
+def call_initial_function(translator, initial_func, annhelper=None):
+    """Before the program starts, call 'initial_func()'."""
+    from pypy.annotation import model as annmodel
+    from pypy.rpython.lltypesystem import lltype
+    from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
+
+    own_annhelper = (annhelper is None)
+    if own_annhelper:
+        annhelper = MixLevelHelperAnnotator(translator.rtyper)
+    c_initial_func = annhelper.constfunc(initial_func, [], annmodel.s_None)
+    if own_annhelper:
+        annhelper.finish()
+
+    entry_point = translator.entry_point_graph
+    args = [copyvar(translator.annotator, v) for v in entry_point.getargs()]
+    extrablock = Block(args)
+    v_none = varoftype(lltype.Void)
+    newop = SpaceOperation('direct_call', [c_initial_func], v_none)
+    extrablock.operations = [newop]
+    extrablock.closeblock(Link(args, entry_point.startblock))
+    entry_point.startblock.isstartblock = False
+    entry_point.startblock = extrablock
+    entry_point.startblock.isstartblock = True
+    checkgraph(entry_point)
+
 def call_final_function(translator, final_func, annhelper=None):
     """When the program finishes normally, call 'final_func()'."""
     from pypy.annotation import model as annmodel

diff --git a/pypy/translator/test/test_unsimplify.py b/pypy/translator/test/test_unsimplify.py
--- a/pypy/translator/test/test_unsimplify.py
+++ b/pypy/translator/test/test_unsimplify.py
@@ -1,6 +1,7 @@
 import os
 from pypy.translator.translator import TranslationContext, graphof
 from pypy.translator.unsimplify import split_block, call_final_function
+from pypy.translator.unsimplify import call_initial_function
 from pypy.rpython.llinterp import LLInterpreter
 from pypy.objspace.flow.model import checkgraph
 from pypy.rlib.objectmodel import we_are_translated
@@ -69,7 +70,26 @@
         assert result == 1
         result = interp.eval_graph(graph, [2])
         assert result == 2
-    
+
+def test_call_initial_function():
+    tmpfile = str(udir.join('test_call_initial_function'))
+    for type_system in ['lltype', 'ootype']:
+        def f(x):
+            return x * 6
+        def hello_world():
+            if we_are_translated():
+                fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0)
+                os.close(fd)
+        graph, t = translate(f, [int], type_system)
+        call_initial_function(t, hello_world)
+        #
+        if os.path.exists(tmpfile):
+            os.unlink(tmpfile)
+        interp = LLInterpreter(t.rtyper)
+        result = interp.eval_graph(graph, [7])
+        assert result == 42
+        assert os.path.isfile(tmpfile)
+
 def test_call_final_function():
     tmpfile = str(udir.join('test_call_final_function'))
     for type_system in ['lltype', 'ootype']:


More information about the Pypy-commit mailing list