[pypy-svn] r49175 - pypy/dist/pypy/translator/llvm

xoraxax at codespeak.net xoraxax at codespeak.net
Mon Nov 26 21:52:58 CET 2007


Author: xoraxax
Date: Mon Nov 26 21:52:57 2007
New Revision: 49175

Modified:
   pypy/dist/pypy/translator/llvm/buildllvm.py
   pypy/dist/pypy/translator/llvm/externs2ll.py
   pypy/dist/pypy/translator/llvm/extfuncnode.py
   pypy/dist/pypy/translator/llvm/genllvm.py
Log:
Teach eci to the LLVM backend. This has a different approach 49160/49171 because its doing the separate  compilation and linking differently. Those changesets are reverted implicitly therefore.

Modified: pypy/dist/pypy/translator/llvm/buildllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/buildllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/buildllvm.py	Mon Nov 26 21:52:57 2007
@@ -4,8 +4,10 @@
 import py
 
 from pypy.translator.tool import stdoutcapture
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.llvm.log import log
 from pypy.translator.llvm.modwrapper import CtypesModule
+from pypy.translator.llvm.externs2ll import get_incdirs
 
 def llvm_is_on_path():
     if py.path.local.sysfind("llvm-as") is None or \
@@ -87,6 +89,19 @@
             self.cmds.append("llc -relocation-model=pic %s.bc -f -o %s.s" % (base, base))
             self.cmds.append("as %s.s -o %s.o" % (base, base))
 
+        include_opts = get_incdirs(self.genllvm.eci)
+        # compile separate files
+        libraries = set()
+        for filename in self.genllvm.eci.separate_module_files:
+            assert filename.endswith(".c")
+            objname = filename[:-2] + ".o"
+            libraries.add(objname)
+            self.cmds.append("gcc %s -c %s -O3 -o %s" % (filename, include_opts, objname))
+
+        attrs = self.genllvm.eci._copy_attributes()
+        attrs['libraries'] = tuple(libraries) + attrs['libraries'] 
+        self.genllvm.eci = ExternalCompilationInfo(**attrs)
+
 # XXX support profile?
 #             if (self.genllvm.config.translation.profopt is not None and
 #                 not self.genllvm.config.translation.noprofopt):
@@ -112,50 +127,59 @@
         self.dirpath.chdir()
 
         return self.genllvm.entry_name
-        
-    def make_module(self):
+ 
+    def setup_linker_command(self, exename):
         base = self.setup()
         self.cmds_bytecode(base)
         self.cmds_objects(base)
 
-        # link (ok this is a mess!)
+        eci = self.genllvm.eci
         library_files = self.genllvm.db.gcpolicy.gc_libraries()
-        gc_libs = ' '.join(['-l' + lib for lib in library_files])
+        library_files = list(library_files) + list(eci.libraries)
+        library_dirs = list(eci.library_dirs)
+        compiler_opts = []
 
         if sys.platform == 'darwin':
-            libdir = '/sw/lib'
-            gc_libs_path = '-L%s -ldl' % libdir
-            self.cmds.append("gcc -O3 %s.o %s %s -lm -bundle -o %s.so" % (base, gc_libs_path, gc_libs, base))
+            library_dirs.append('/sw/lib')
+            library_files.append("m")
+            library_files.append("dl")
+            if not exename:
+                compiler_opts.append("-bundle")
         else:
+            if not exename:
+                compiler_opts.append("-shared")
+            else:
+                compiler_opts.append("-static")
+            compiler_opts.append("-pipe")
+
+        lib_opts = []
+        for lib in library_files:
+            if lib[0] != "/":
+                lib = "-l" + lib
+            lib_opts.append(lib)
+        lib_dir_opts = ["-L" + libdir for libdir in library_dirs]
+        compiler_opts.extend(lib_opts)
+        compiler_opts.extend(lib_dir_opts)
+
+        out = base + ".so"
+        if exename:
+            out = exename
+        self.cmds.append("gcc -O3 %s.o %s -o %s" % (base, " ".join(compiler_opts), out))
+        return base
 
-            gc_libs_path = '-shared'
-            self.cmds.append("gcc -O3 %s.o %s %s -pipe -o %s.so" % (base, gc_libs_path, gc_libs, base))
 
+    def make_module(self):
+        base = self.setup_linker_command(False)
         try:
             self.execute_cmds()
             modname = CtypesModule(self.genllvm, "%s.so" % base).create()
-
         finally:
             self.lastdir.chdir()
 
         return modname, str(self.dirpath)
 
     def make_standalone(self, exename):
-        base = self.setup()
-        self.cmds_bytecode(base)
-        self.cmds_objects(base)
-
-        object_files = ["-L/sw/lib"]
-        library_files = self.genllvm.db.gcpolicy.gc_libraries()
-        gc_libs = ' '.join(['-l' + lib for lib in library_files])
-
-        if sys.platform == 'darwin':
-            libdir = '/sw/' + "/lib"
-            gc_libs_path = '-L%s -ldl' % libdir
-        else:
-            gc_libs_path = '-static'
-
-        self.cmds.append("gcc -O3 %s.o %s %s -lm -pipe -o %s" % (base, gc_libs_path, gc_libs, exename))
+        base = self.setup_linker_command(exename)
 
         try:
             self.execute_cmds()

Modified: pypy/dist/pypy/translator/llvm/externs2ll.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/externs2ll.py	(original)
+++ pypy/dist/pypy/translator/llvm/externs2ll.py	Mon Nov 26 21:52:57 2007
@@ -3,11 +3,12 @@
 import types
 import urllib
 
+from StringIO import StringIO
+
 from pypy.objspace.flow.model import FunctionGraph
 from pypy.rpython.rmodel import inputconst
 from pypy.rpython.lltypesystem import lltype
 from pypy.tool.udir import udir
-from StringIO import StringIO
 
 def get_c_cpath():
     from pypy.translator.c import genc
@@ -19,10 +20,10 @@
 def get_module_file(name):
     return os.path.join(get_llvm_cpath(), name)
 
-def get_incdirs(c_include_dirs):
+def get_incdirs(eci):
     import distutils.sysconfig
 
-    includes = tuple(c_include_dirs) + ("/sw/include",
+    includes = eci.include_dirs + ("/sw/include",
                 distutils.sysconfig.EXEC_PREFIX + "/include", 
                 distutils.sysconfig.EXEC_PREFIX + "/include/gc",
                 distutils.sysconfig.get_python_inc(),
@@ -52,28 +53,22 @@
     f.write(ccode)
     f.close()
 
-    includes = get_incdirs(eci.include_dirs)
-    eci = eci.convert_sources_to_files()
-    c_files = [filename] + list(eci.separate_module_files)
-    read_lines = []
-    for name in c_files:
-    # XXX this is evil...
-        plain = name[:-2]
-        cmd = "llvm-gcc -emit-llvm -O0 -S %s %s.c -o %s.ll 2>&1" % (
-            includes, plain, plain)
+    plain = filename[:-2]
+    includes = get_incdirs(eci)
+    cmd = "llvm-gcc -emit-llvm -O0 -S %s %s.c -o %s.ll 2>&1" % (
+        includes, plain, plain)
 
-        if os.system(cmd) != 0:
-            raise Exception("Failed to run '%s'" % cmd)
+    if os.system(cmd) != 0:
+        raise Exception("Failed to run '%s'" % cmd)
 
-        llcode = open(plain + '.ll').read()
-        read_lines += llcode.split("\n")
+    llcode = open(plain + '.ll').read()
 
     # strip lines
     lines = []
 
     calltag, declaretag, definetag = 'call ', 'declare ', 'define '
     
-    for line in read_lines:
+    for line in llcode.split('\n'):
 
         # get rid of any of the structs that llvm-gcc introduces to struct types
         line = line.replace("%struct.", "%")
@@ -123,21 +118,16 @@
         ccode.append('#define __ENTRY_POINT__ %s' % entrynode.get_ref()[1:])
         ccode.append('#define ENTRY_POINT_DEFINED 1')
 
+    sio = StringIO()
+    eci.write_c_header(sio)
+    ccode.extend(sio.getvalue().splitlines())
+
     # include python.h early
     ccode.append('#include <Python.h>')
 
     # ask gcpolicy for any code needed
     ccode.append('%s' % db.gcpolicy.genextern_code())
 
-    ccode.append("#define PYPY_NOT_MAIN_FILE")
-    # ask rffi for includes/source
-    s = StringIO()
-    eci.write_c_header(s)
-    ccode.append(s.getvalue())
-    ccode.append("#undef PYPY_NOT_MAIN_FILE")
-    
-    ccode.append('')
-
     # append our source file
     ccode.append(open(get_module_file('genexterns.c')).read())
     return "\n".join(ccode)

Modified: pypy/dist/pypy/translator/llvm/extfuncnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/extfuncnode.py	(original)
+++ pypy/dist/pypy/translator/llvm/extfuncnode.py	Mon Nov 26 21:52:57 2007
@@ -7,7 +7,7 @@
         self.db = db
         self.value = value
         self.name = "@" + name
-        self.compilation_info = getattr(value, 'compilation_info', None)
+        self.eci = self.value.compilation_info
 
     def writeglobalconstants(self, codewriter):
         pass

Modified: pypy/dist/pypy/translator/llvm/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/genllvm.py	Mon Nov 26 21:52:57 2007
@@ -52,7 +52,8 @@
     def __init__(self, translator, standalone):
     
         # reset counters
-        Node.nodename_count = {}    
+        Node.nodename_count = {}
+        self.eci = ExternalCompilationInfo()
 
         self.standalone = standalone
         self.translator = translator
@@ -187,12 +188,14 @@
     def generate_ll_externs(self, codewriter):
         all = []
         for node in self.db.getnodes():
-            eci = getattr(node, 'compilation_info', None)
-            if eci is not None:
+            eci = getattr(node, 'eci', None)
+            if eci:
                 all.append(eci)
-        eci = ExternalCompilationInfo().merge(*all)
-        ccode = generate_c(self.db, self.entrynode, eci, self.standalone)
-        self.llcode = generate_ll(ccode, codewriter.cconv, eci, self.db.extern_to_funcnodes)
+        self.eci = self.eci.merge(*all)
+
+        ccode = generate_c(self.db, self.entrynode, self.eci, self.standalone)
+        self.llcode = generate_ll(ccode, codewriter.cconv, self.eci, self.db.extern_to_funcnodes)
+        self.eci = self.eci.convert_sources_to_files(being_main=True)
         
     def create_codewriter(self):
         # prevent running the same function twice in a test



More information about the Pypy-commit mailing list