[pypy-svn] r14308 - in pypy/dist/pypy/translator/llvm2: . test

ericvrp at codespeak.net ericvrp at codespeak.net
Tue Jul 5 20:13:51 CEST 2005


Author: ericvrp
Date: Tue Jul  5 20:13:50 2005
New Revision: 14308

Added:
   pypy/dist/pypy/translator/llvm2/extfunctions.py
Modified:
   pypy/dist/pypy/translator/llvm2/database.py
   pypy/dist/pypy/translator/llvm2/funcnode.py
   pypy/dist/pypy/translator/llvm2/genllvm.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
Log:
Initial version of external function calls working.


Modified: pypy/dist/pypy/translator/llvm2/database.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/database.py	(original)
+++ pypy/dist/pypy/translator/llvm2/database.py	Tue Jul  5 20:13:50 2005
@@ -76,7 +76,10 @@
                 ct = ct.TO
             
             if isinstance(ct, lltype.FuncType):
-                self.addpending(const_or_var, FuncNode(self, const_or_var))
+                if const_or_var.value._obj._callable and not hasattr(const_or_var.value._obj, 'graph'):
+                    log('EXTERNAL FUNCTION' + str(dir(const_or_var.value._obj)))
+                else:
+                    self.addpending(const_or_var, FuncNode(self, const_or_var))
             else:
                 value = const_or_var.value
                 while hasattr(value, "_obj"):

Added: pypy/dist/pypy/translator/llvm2/extfunctions.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/extfunctions.py	Tue Jul  5 20:13:50 2005
@@ -0,0 +1,17 @@
+extdeclarations = """; External declarations
+
+declare int %dup(int)
+
+; End of external declarations
+"""
+
+extfunctions = """; External functions (will be inlined by LLVM)
+
+int %ll_os_dup__Signed(int %i) {
+block0:
+    %i.0 = call int %dup(int %i)
+    ret int %i.0
+}
+
+; End of external functions
+"""

Modified: pypy/dist/pypy/translator/llvm2/funcnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/funcnode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/funcnode.py	Tue Jul  5 20:13:50 2005
@@ -7,6 +7,7 @@
 from pypy.translator.llvm2.node import LLVMNode
 from pypy.translator.llvm2.atomic import is_atomic
 from pypy.translator.llvm2.log import log 
+from pypy.rpython.extfunctable import table as extfunctable
 nextnum = py.std.itertools.count().next
 log = log.funcnode
 
@@ -38,6 +39,7 @@
 
     def __init__(self, db, const_ptr_func):
         self.db = db
+        self.const_ptr_func = const_ptr_func
         self.ref = "%" + const_ptr_func.value._obj._name
         self.graph = const_ptr_func.value._obj.graph 
         remove_same_as(self.graph) 
@@ -65,6 +67,12 @@
         codewriter.declare(self.getdecl())
 
     def writeimpl(self, codewriter):
+        _callable = self.const_ptr_func.value._obj._callable
+        for func, extfuncinfo in extfunctable.iteritems():  # precompute a dict?
+            if _callable is extfuncinfo.ll_function:
+                log('skipped output of external function %s' % self.const_ptr_func.value._obj._name)
+                return
+
         assert self._issetup 
         graph = self.graph
         log.writeimpl(graph.name)

Modified: pypy/dist/pypy/translator/llvm2/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/genllvm.py	Tue Jul  5 20:13:50 2005
@@ -12,6 +12,7 @@
 from pypy.tool.udir import udir
 from pypy.translator.llvm2.codewriter import CodeWriter
 from pypy.translator.backendoptimization import remove_void
+from pypy.translator.llvm2.extfunctions import extdeclarations, extfunctions
 
 function_count = {}
 
@@ -39,6 +40,8 @@
         typ_decl.writeglobalconstants(codewriter)
 
     nl(); comment("Function Prototypes") ; nl()
+    for extdecl in extdeclarations.split('\n'):
+        codewriter.append(extdecl)
     if use_boehm_gc: 
         codewriter.declare('sbyte* %GC_malloc(uint)')
         codewriter.declare('sbyte* %GC_malloc_atomic(uint)')
@@ -49,6 +52,8 @@
     #import pdb ; pdb.set_trace()
     nl(); comment("Function Implementation") 
     codewriter.startimpl()
+    for extfunc in extfunctions.split('\n'):
+        codewriter.append(extfunc)
     for typ_decl in db.getobjects():
         typ_decl.writeimpl(codewriter)
 

Modified: pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	Tue Jul  5 20:13:50 2005
@@ -37,6 +37,13 @@
     f = getattr(mod, function.func_name + "_wrapper")
     return mod, f
 
+def test_external_function():
+    import os
+    def fn():
+        return os.dup(0)
+    f = compile_function(fn, [], view=False)
+    assert os.path.sameopenfile(f(), fn())
+
 def test_GC_malloc(): 
     if not use_boehm_gc:
         py.test.skip("test_GC_malloc skipped because Boehm collector library was not found")
@@ -324,7 +331,7 @@
     l = "Hello, World"
     def string_getitem1(i): 
         return l[i]
-    f = compile_function(string_getitem1, [int], view=True)
+    f = compile_function(string_getitem1, [int], view=False)
     assert f(0) == ord("H")
 
 def DONOT_test_string_getitem2():



More information about the Pypy-commit mailing list