[pypy-svn] r70552 - in pypy/branch/separate-compilation/pypy/translator: . c c/test

afa at codespeak.net afa at codespeak.net
Wed Jan 13 11:48:39 CET 2010


Author: afa
Date: Wed Jan 13 11:48:38 2010
New Revision: 70552

Added:
   pypy/branch/separate-compilation/pypy/translator/c/separate.py
Modified:
   pypy/branch/separate-compilation/pypy/translator/c/genc.py
   pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py
   pypy/branch/separate-compilation/pypy/translator/separate.py
Log:
Move code to a more proper place.


Modified: pypy/branch/separate-compilation/pypy/translator/c/genc.py
==============================================================================
--- pypy/branch/separate-compilation/pypy/translator/c/genc.py	(original)
+++ pypy/branch/separate-compilation/pypy/translator/c/genc.py	Wed Jan 13 11:48:38 2010
@@ -339,30 +339,6 @@
             )
         self._compiled = True
 
-    def make_import_module(self):
-        class Module:
-            pass
-        mod = Module()
-        mod.__file__ = self.so_name
-
-        forwards = []
-        node_names = self.export_node_names.values()
-        for node in self.db.globalcontainers():
-            if node.nodekind == 'func' and node.name in node_names:
-                forwards.append('\n'.join(node.forward_declaration()))
-
-        import_eci = ExternalCompilationInfo(
-            libraries = [self.so_name],
-            post_include_bits = forwards
-            )
-
-        from pypy.translator.separate import make_ll_import_function
-        for funcname, import_name in self.export_node_names.items():
-            functype = lltype.typeOf(self.entrypoint[funcname])
-            func = make_ll_import_function(import_name, functype, import_eci)
-            setattr(mod, funcname, func)
-        return mod
-
     def gen_makefile(self, targetdir):
         pass
 

Added: pypy/branch/separate-compilation/pypy/translator/c/separate.py
==============================================================================
--- (empty file)
+++ pypy/branch/separate-compilation/pypy/translator/c/separate.py	Wed Jan 13 11:48:38 2010
@@ -0,0 +1,129 @@
+from pypy.annotation import model, description
+from pypy.tool.sourcetools import func_with_new_name
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+from pypy.rpython.typesystem import getfunctionptr
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rpython.extregistry import ExtRegistryEntry
+
+import types
+
+def annotate_exported_functions(annotator, exports):
+    bk = annotator.bookkeeper
+
+    # annotate functions with signatures
+    for funcname, func in exports.items():
+        if hasattr(func, 'argtypes'):
+            annotator.build_types(func, func.argtypes,
+                                  complete_now=False)
+    # annotate classes
+    for funcname, cls in exports.items():
+        if not isinstance(cls, (type, types.ClassType)):
+            continue
+        desc = bk.getdesc(cls)
+        classdef = desc.getuniqueclassdef()
+        s_init = desc.s_read_attribute('__init__')
+        if isinstance(s_init, model.SomeImpossibleValue):
+            continue
+
+        argtypes = (model.SomeInstance(classdef),)
+        argtypes += tuple(cls.__init__.argtypes)
+        annotator.build_types(cls.__init__.im_func, argtypes,
+                              complete_now=False)
+
+    annotator.complete()
+
+    # ensure that functions without signature are not constant-folded
+    for funcname, func in exports.items():
+        if not hasattr(func, 'argtypes'):
+            # build a list of arguments where constants are erased
+            newargs = []
+            desc = bk.getdesc(func)
+            if isinstance(desc, description.FunctionDesc):
+                graph = desc.getuniquegraph()
+                for arg in graph.startblock.inputargs:
+                    newarg = model.not_const(annotator.binding(arg))
+                    newargs.append(newarg)
+                # and reflow
+                annotator.build_types(func, newargs)
+
+def get_exported_functions(annotator, exports):
+    bk = annotator.bookkeeper
+
+    exported_funcptr = {}
+    for funcname, func in exports.items():
+        desc = bk.getdesc(func)
+        if not isinstance(desc, description.FunctionDesc):
+            continue
+        graph = desc.getuniquegraph()
+        funcptr = getfunctionptr(graph)
+
+        exported_funcptr[funcname] = funcptr
+    return exported_funcptr
+
+def make_import_module(builder):
+    class Module:
+        pass
+    mod = Module()
+    mod.__file__ = builder.so_name
+
+    forwards = []
+    node_names = builder.export_node_names.values()
+    for node in builder.db.globalcontainers():
+        if node.nodekind == 'func' and node.name in node_names:
+            forwards.append('\n'.join(node.forward_declaration()))
+
+    import_eci = ExternalCompilationInfo(
+        libraries = [builder.so_name],
+        post_include_bits = forwards
+        )
+
+    for funcname, import_name in builder.export_node_names.items():
+        functype = lltype.typeOf(builder.entrypoint[funcname])
+        func = make_ll_import_function(import_name, functype, import_eci)
+        setattr(mod, funcname, func)
+    return mod
+
+def make_ll_import_arg_converter(TARGET):
+    from pypy.annotation import model
+
+    def convert(x):
+        XXX
+
+    class Entry(ExtRegistryEntry):
+        _about_ = convert
+        s_result_annotation = model.lltype_to_annotation(TARGET)
+
+        def specialize_call(self, hop):
+            # TODO: input type check
+            [v_instance] = hop.inputargs(*hop.args_r)
+            return hop.genop('force_cast', [v_instance],
+                             resulttype=TARGET)
+
+    return convert
+make_ll_import_arg_converter._annspecialcase_ = 'specialize:memo'
+
+def make_ll_import_function(name, functype, eci):
+    from pypy.rpython.lltypesystem import lltype, rffi
+
+    imported_func = rffi.llexternal(
+        name, functype.TO.ARGS, functype.TO.RESULT,
+        compilation_info=eci,
+        )
+
+    ARGS = functype.TO.ARGS
+    unrolling_ARGS = unrolling_iterable(enumerate(ARGS))
+    def wrapper(*args):
+        real_args = ()
+        for i, TARGET in unrolling_ARGS:
+            arg = args[i]
+            if isinstance(TARGET, lltype.Ptr): # XXX more precise check?
+                arg = make_ll_import_arg_converter(TARGET)(arg)
+
+            real_args = real_args + (arg,)
+        res = imported_func(*real_args)
+        return res
+    wrapper._annspecialcase_ = 'specialize:ll'
+    wrapper._always_inline_ = True
+    return func_with_new_name(wrapper, name)
+

Modified: pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py
==============================================================================
--- pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py	(original)
+++ pypy/branch/separate-compilation/pypy/translator/c/test/test_separate.py	Wed Jan 13 11:48:38 2010
@@ -1,13 +1,10 @@
 from pypy.translator.separate import export
 from pypy.translator.translator import TranslationContext
-from pypy.translator.c.genc import CExtModuleBuilder, CLibraryBuilder, gen_forwarddecl
+from pypy.translator.c.genc import CExtModuleBuilder, CLibraryBuilder
+from pypy.translator.c import separate
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.rpython.typesystem import getfunctionptr
-from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.annotation import model, description
 import py
 import sys, os
-import types
 
 class TestSeparation:
     def compile_function(self, func, argtypes):
@@ -22,60 +19,18 @@
     def compile_separated(self, name, **exports):
         t = TranslationContext()
         t.buildannotator()
-        bk = t.annotator.bookkeeper
 
-        # annotate functions with signatures
-        for funcname, func in exports.items():
-            if hasattr(func, 'argtypes'):
-                t.annotator.build_types(func, func.argtypes,
-                                        complete_now=False)
-        # annotate classes
-        for funcname, cls in exports.items():
-            if not isinstance(cls, (type, types.ClassType)):
-                continue
-            desc = bk.getdesc(cls)
-            classdef = desc.getuniqueclassdef()
-            s_init = desc.s_read_attribute('__init__')
-            if isinstance(s_init, model.SomeImpossibleValue):
-                continue
-
-            argtypes = (model.SomeInstance(classdef),)
-            argtypes += tuple(cls.__init__.argtypes)
-            t.annotator.build_types(cls.__init__.im_func, argtypes,
-                                    complete_now=False)
-        t.annotator.complete()
-
-        # ensure that functions without signature are not constant-folded
-        for funcname, func in exports.items():
-            if not hasattr(func, 'argtypes'):
-                # build a list of arguments where constants are erased
-                newargs = []
-                desc = bk.getdesc(func)
-                if isinstance(desc, description.FunctionDesc):
-                    graph = desc.getuniquegraph()
-                    for arg in graph.startblock.inputargs:
-                        newarg = model.not_const(t.annotator.binding(arg))
-                        newargs.append(newarg)
-                    # and reflow
-                    t.annotator.build_types(func, newargs)
+        separate.annotate_exported_functions(t.annotator, exports)
 
         t.buildrtyper().specialize()
 
-        exported_funcptr = {}
-        for funcname, func in exports.items():
-            desc = bk.getdesc(func)
-            if not isinstance(desc, description.FunctionDesc):
-                continue
-            graph = desc.getuniquegraph()
-            funcptr = getfunctionptr(graph)
-
-            exported_funcptr[funcname] = funcptr
+        exported_funcptr = separate.get_exported_functions(t.annotator, exports)
 
         builder = CLibraryBuilder(t, exported_funcptr, config=t.config)
         builder.generate_source()
         builder.compile()
 
-        mod = builder.make_import_module()
+        mod = separate.make_import_module(builder)
         return mod
 
     def test_simple_call(self):

Modified: pypy/branch/separate-compilation/pypy/translator/separate.py
==============================================================================
--- pypy/branch/separate-compilation/pypy/translator/separate.py	(original)
+++ pypy/branch/separate-compilation/pypy/translator/separate.py	Wed Jan 13 11:48:38 2010
@@ -1,6 +1,3 @@
-from pypy.tool.sourcetools import func_with_new_name
-from pypy.rlib.unroll import unrolling_iterable
-from pypy.rpython.extregistry import ExtRegistryEntry
 import types
 
 class export(object):
@@ -45,46 +42,3 @@
     return (isinstance(obj, (types.FunctionType, types.UnboundMethodType))
             and getattr(obj, 'exported', False))
 
-def make_ll_import_arg_converter(TARGET):
-    from pypy.annotation import model
-
-    def convert(x):
-        XXX
-
-    class Entry(ExtRegistryEntry):
-        _about_ = convert
-        s_result_annotation = model.lltype_to_annotation(TARGET)
-
-        def specialize_call(self, hop):
-            # TODO: input type check
-            [v_instance] = hop.inputargs(*hop.args_r)
-            return hop.genop('force_cast', [v_instance],
-                             resulttype=TARGET)
-
-    return convert
-make_ll_import_arg_converter._annspecialcase_ = 'specialize:memo'
-
-def make_ll_import_function(name, functype, eci):
-    from pypy.rpython.lltypesystem import lltype, rffi
-
-    imported_func = rffi.llexternal(
-        name, functype.TO.ARGS, functype.TO.RESULT,
-        compilation_info=eci,
-        )
-
-    ARGS = functype.TO.ARGS
-    unrolling_ARGS = unrolling_iterable(enumerate(ARGS))
-    def wrapper(*args):
-        real_args = ()
-        for i, TARGET in unrolling_ARGS:
-            arg = args[i]
-            if isinstance(TARGET, lltype.Ptr): # XXX more precise check?
-                arg = make_ll_import_arg_converter(TARGET)(arg)
-
-            real_args = real_args + (arg,)
-        res = imported_func(*real_args)
-        return res
-    wrapper._annspecialcase_ = 'specialize:ll'
-    wrapper._always_inline_ = True
-    return func_with_new_name(wrapper, name)
-



More information about the Pypy-commit mailing list