[pypy-svn] r48339 - in pypy/dist/pypy/translator/llvm: . module

rxe at codespeak.net rxe at codespeak.net
Tue Nov 6 14:42:34 CET 2007


Author: rxe
Date: Tue Nov  6 14:42:31 2007
New Revision: 48339

Removed:
   pypy/dist/pypy/translator/llvm/module/excsupport.py
Modified:
   pypy/dist/pypy/translator/llvm/codewriter.py
   pypy/dist/pypy/translator/llvm/externs2ll.py
   pypy/dist/pypy/translator/llvm/gc.py
   pypy/dist/pypy/translator/llvm/genllvm.py
   pypy/dist/pypy/translator/llvm/modwrapper.py
Log:
refactor to one place (almost) where we specify default tail call flag, calling convention and linkage options

Modified: pypy/dist/pypy/translator/llvm/codewriter.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/codewriter.py	(original)
+++ pypy/dist/pypy/translator/llvm/codewriter.py	Tue Nov  6 14:42:31 2007
@@ -2,18 +2,20 @@
 
 log = log.codewriter 
 
-DEFAULT_TAIL     = ''       #/tail
-DEFAULT_CCONV    = 'fastcc'    #ccc/fastcc
-DEFAULT_LINKAGE  = 'internal '       #/internal (disabled for now because of the JIT)
-
 class CodeWriter(object): 
-    def __init__(self, file, db, tail=DEFAULT_TAIL, cconv=DEFAULT_CCONV,
-                                 linkage=DEFAULT_LINKAGE): 
+    tail = ''       #/tail
+    cconv = 'fastcc'    #ccc/fastcc
+    linkage = 'internal '       #/internal (disabled for now because of the JIT)
+
+    def __init__(self, file, db, tail=None, cconv=None, linkage=None): 
         self.file = file
         self.word_repr = db.get_machine_word()
-        self.tail = tail
-        self.cconv = cconv
-        self.linkage = linkage
+        if tail is not None:
+            self.tail = tail
+        if cconv is not None:
+            self.cconv = cconv
+        if linkage is not None:
+            self.linkage = linkage
 
     def close(self): 
         self.file.close()

Modified: pypy/dist/pypy/translator/llvm/externs2ll.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/externs2ll.py	(original)
+++ pypy/dist/pypy/translator/llvm/externs2ll.py	Tue Nov  6 14:42:31 2007
@@ -6,7 +6,6 @@
 from pypy.objspace.flow.model import FunctionGraph
 from pypy.rpython.rmodel import inputconst
 from pypy.rpython.lltypesystem import lltype
-from pypy.translator.llvm.codewriter import DEFAULT_CCONV
 from pypy.translator.llvm.buildllvm import llvm_gcc_version
 
 from pypy.tool.udir import udir
@@ -28,7 +27,7 @@
 def get_module_file(name):
     return os.path.join(get_llvm_cpath(), name)
 
-def get_ll(ccode, function_names):
+def get_ll(ccode, function_names, default_cconv):
     function_names += support_functions
     filename = str(udir.join("ccode.c"))
     f = open(filename, "w")
@@ -82,10 +81,10 @@
            if line.find("internal") == -1:
                 if funcname not in ["%main", "%ctypes_RPython_StartupCode"]:
                     internal = 'internal '
-                    line = '%s%s %s' % (internal, DEFAULT_CCONV, line,)
+                    line = '%s%s %s' % (internal, default_cconv, line,)
         ll_lines.append(line)
 
-    # patch calls to function that we just declared fastcc
+    # patch calls to function that we just declared with differnet cconv
     ll_lines2, calltag, declaretag = [], 'call ', 'declare '
     for line in ll_lines:
         i = line.find(calltag)
@@ -93,14 +92,14 @@
             cconv = 'ccc'
             for funcname in funcnames.iterkeys():
                 if line.find(funcname) >= 0:
-                    cconv = DEFAULT_CCONV
+                    cconv = default_cconv
                     break
             line = "%scall %s %s" % (line[:i], cconv, line[i+len(calltag):])
         if line[:len(declaretag)] == declaretag:
             cconv = 'ccc'
             for funcname in funcnames.keys():
                 if line.find(funcname) >= 0:
-                    cconv = DEFAULT_CCONV
+                    cconv = default_cconv
                     break
             line = "declare %s %s" % (cconv, line[len(declaretag):])
         ll_lines2.append(line)
@@ -167,7 +166,7 @@
         includestr += "-I %s " % ii
     return includestr
 
-def generate_llfile(db, extern_decls, entrynode, c_includes, c_sources, standalone):
+def generate_llfile(db, extern_decls, entrynode, c_includes, c_sources, standalone, default_cconv):
     ccode = []
     function_names = []
         
@@ -223,5 +222,4 @@
 
     # append our source file
     ccode.append(open(get_module_file('genexterns.c')).read())
-
-    return get_ll("".join(ccode), function_names)
+    return get_ll("".join(ccode), function_names, default_cconv)

Modified: pypy/dist/pypy/translator/llvm/gc.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/gc.py	(original)
+++ pypy/dist/pypy/translator/llvm/gc.py	Tue Nov  6 14:42:31 2007
@@ -80,6 +80,7 @@
 
         # malloc_size is unsigned right now
         codewriter.malloc(targetvar, "sbyte", size)
+        # XXX uses own cconv
         codewriter.call(None, 'void', '%llvm.memset' + postfix(),
                         ['sbyte*', 'ubyte', uword, uword],
                         [targetvar, 0, size, boundary_size],
@@ -125,6 +126,7 @@
         codewriter.call(targetvar, 'sbyte*', fnname, [word], [size])
 
         if atomic:
+            # XXX uses own cconv
             codewriter.call(None, 'void', '%llvm.memset' + postfix(),
                             ['sbyte*', 'ubyte', uword, uword],
                             [targetvar, 0, sizeu, boundary_size],

Modified: pypy/dist/pypy/translator/llvm/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/genllvm.py	Tue Nov  6 14:42:31 2007
@@ -45,8 +45,10 @@
         # write bottom part of llvm file
         self.write_implementations(codewriter)
 
-        self._checkpoint('done')
+        # write entry point if there is one
+        codewriter.comment("End of file")
         codewriter.close()
+        self._checkpoint('done')
 
         return self.filename
 
@@ -88,13 +90,14 @@
         
         self._print_node_stats()
 
-        # create ll file from c code
-        self.generate_ll_externs()
-        self._checkpoint('setup_externs')
-
         # open file & create codewriter
         codewriter, self.filename = self.create_codewriter()
         self._checkpoint('open file and create codewriter')        
+
+        # create ll file from c code
+        self.generate_ll_externs(codewriter)
+        self._checkpoint('setup_externs')
+
         return codewriter
 
     def _set_wordsize(self, s):
@@ -154,20 +157,12 @@
         
         self._checkpoint('write support implentations')
 
-        # write wrapper code
-        if not self.standalone:
-            from pypy.translator.llvm.modwrapper import llvm_implcode
-            codewriter.write_lines(llvm_implcode(self.entrynode))
-
         # write all node implementations
         for node in self.db.getnodes():
             if hasattr(node, 'writeimpl'):
                 node.writeimpl(codewriter)
 
         self._checkpoint('write node implementations')
-
-        # write entry point if there is one
-        codewriter.comment("End of file")
     
     def get_entry_point(self, func):
         assert func is not None
@@ -191,7 +186,7 @@
         self.entry_name = name[6:]
         return c.value._obj 
 
-    def generate_ll_externs(self):
+    def generate_ll_externs(self, codewriter):
         c_includes = {}
         c_sources = {}
 
@@ -208,13 +203,17 @@
                                                self.entrynode,
                                                c_includes,
                                                c_sources,
-                                               self.standalone)
+                                               self.standalone, 
+                                               codewriter.cconv)
 
     def create_codewriter(self):
         # prevent running the same function twice in a test
         filename = udir.join(self.entry_name).new(ext='.ll')
         f = open(str(filename), 'w')
-        return CodeWriter(f, self.db), filename
+        if self.standalone:
+            return CodeWriter(f, self.db), filename
+        else:
+            return CodeWriter(f, self.db, cconv='ccc', linkage=''), filename
 
     def write_extern_decls(self, codewriter):        
         for c_name, obj in self.extern_decls:

Modified: pypy/dist/pypy/translator/llvm/modwrapper.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/modwrapper.py	(original)
+++ pypy/dist/pypy/translator/llvm/modwrapper.py	Tue Nov  6 14:42:31 2007
@@ -3,33 +3,6 @@
 from pypy.rpython.lltypesystem import lltype 
 from pypy.rpython.lltypesystem.rstr import STR
 
-def _noresult(returntype):
-    r = returntype.strip()
-    if r == 'void':
-        return 'void'
-    elif r == 'bool':
-        return 'bool false'
-    elif r in 'float double'.split():
-        return r + ' 0.0'
-    elif r in 'ubyte sbyte ushort short uint int ulong long'.split():
-        return r + ' 0'
-    return r + ' null'
-
-def llvm_implcode(entrynode):
-    from pypy.translator.llvm.codewriter import DEFAULT_CCONV as cconv
-    from pypy.translator.llvm.module.excsupport import entrycode, voidentrycode, raisedcode 
-    returntype, entrypointname = entrynode.getdecl().split(' %', 1)
-    noresult = _noresult(returntype)
-
-    code = raisedcode % locals()
-    if returntype == "void":
-        code += voidentrycode % locals()
-    else:
-        code += entrycode % locals()
-
-    return code
-
-
 class CtypesModule:
     """ use ctypes to create a temporary module """
 
@@ -40,9 +13,13 @@
 
 _c = ctypes.CDLL(join(dirname(realpath(__file__)), "%s"))
 
-raised = _c.__entrypoint__raised_LLVMException
-raised.argtypes = []
-raised.restype = ctypes.c_int
+rpyexc_occured = _c.pypy__rpyexc_occured
+rpyexc_occured.argtypes = []
+rpyexc_occured.restype = ctypes.c_int
+ 
+rpyexc_fetch_type = _c.pypy_rpyexc_fetch_type
+rpyexc_fetch_type.argtypes = []
+rpyexc_fetch_type.restype = ctypes.c_void_p
 
 GC_get_heap_size_wrapper = _c.GC_get_heap_size
 GC_get_heap_size_wrapper.argtypes = []
@@ -65,7 +42,7 @@
         _setup = True
     args = [f(a) for a, f in zip(args, to_llargs)]
     result = __entrypoint__(*args)
-    if raised():
+    if rpyexc_occured():
         raise LLVMException("Exception raised")
     return ll_to_res(result)
 
@@ -133,11 +110,11 @@
 """
 
     epilog = """
-__entrypoint__ = _c.__entrypoint__pypy_%(name)s
+__entrypoint__ = _c.pypy_%(name)s
 
 # %(RT)r
 to_llargs = %(to_llargs)s
-__entrypoint__.argtypes = %(args)s
+__entrypoint__ = _c.pypy_%(name)s
 
 # %(ARGS)r
 ll_to_res = %(ll_to_res)s



More information about the Pypy-commit mailing list