[pypy-svn] r14284 - in pypy/dist/pypy: annotation rpython rpython/test

pedronis at codespeak.net pedronis at codespeak.net
Tue Jul 5 14:45:17 CEST 2005


Author: pedronis
Date: Tue Jul  5 14:45:15 2005
New Revision: 14284

Added:
   pypy/dist/pypy/rpython/extfunctable.py   (contents, props changed)
Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/rmodel.py
   pypy/dist/pypy/rpython/rtyper.py
   pypy/dist/pypy/rpython/test/test_rbuiltin.py
Log:
started implementing the plan in documentation/ext-function-draft about supporting external function calls
trhough the translation tool chain.



Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Tue Jul  5 14:45:15 2005
@@ -335,3 +335,9 @@
 BUILTIN_ANALYZERS[lltype.getRuntimeTypeInfo] = getRuntimeTypeInfo
 BUILTIN_ANALYZERS[lltype.runtime_type_info] = runtime_type_info
 
+from pypy.rpython import extfunctable
+
+# import annotation information for external functions 
+# from the extfunctable.table  into our own annotation specific table 
+for func, extfuncinfo in extfunctable.table.iteritems():
+    BUILTIN_ANALYZERS[func] = extfuncinfo.annotation 

Added: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/extfunctable.py	Tue Jul  5 14:45:15 2005
@@ -0,0 +1,35 @@
+"""
+information table about external functions for annotation/ rtyping and backends
+"""
+import os
+import types
+
+class ExtFuncInfo:
+    def __init__(self, func, annotation, ll_function, ll_annotable):
+        self.func = func
+        self.annotation = annotation
+        self.ll_function = ll_function
+        self.ll_annotable = ll_annotable
+
+table = {}
+def declare(func, annotation, ll_function, ll_annotable=True):
+    # annotation can be a function computing the annotation
+    # or a simple python type from which an annotation will be construcuted
+    global table
+    if not isinstance(annotation, types.FunctionType):
+        typ = annotation
+        def annotation(*args_s):
+            from pypy.annotation import bookkeeper
+            return bookkeeper.getbookkeeper().valueoftype(typ)
+    table[func] = ExtFuncInfo(func, annotation, ll_function, ll_annotable)
+
+# low-level helpers representing the external functions
+def ll_os_getuid():
+    return 1 #return os.getuid()
+
+def ll_os_dup(fd):
+    return 999
+
+# external function declarations
+declare(os.getuid, int, ll_os_getuid, ll_annotable=True)
+declare(os.dup, int, ll_os_dup, ll_annotable=True)

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Tue Jul  5 14:45:15 2005
@@ -9,7 +9,7 @@
 from pypy.rpython.robject import pyobj_repr
 from pypy.rpython.rfloat import float_repr, FloatRepr
 from pypy.rpython import rclass
-
+from pypy.tool import sourcetools
 
 class __extend__(annmodel.SomeBuiltin):
     def rtyper_makerepr(self, rtyper):
@@ -241,3 +241,21 @@
                                  includes=("math.h",))   # XXX clean up needed
 
 BUILTIN_TYPER[math.exp] = rtype_math_exp
+
+from pypy.rpython import extfunctable
+
+def make_rtype_extfunc(extfuncinfo):
+    ll_function = extfuncinfo.ll_function
+    if extfuncinfo.ll_annotable:
+        def rtype_extfunc(hop):
+            return hop.gendirectcall(ll_function, *hop.args_v)
+    else:
+        assert False, "xxx not implemented"
+    return sourcetools.func_with_new_name(rtype_extfunc,
+                                          "rtype_extfunc_%s" % extfuncinfo.func.__name__)
+
+# import rtyping information for external functions 
+# from the extfunctable.table  into our own specific table 
+for func, extfuncinfo in extfunctable.table.iteritems():
+    BUILTIN_TYPER[func] = make_rtype_extfunc(extfuncinfo)
+

Modified: pypy/dist/pypy/rpython/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rmodel.py	(original)
+++ pypy/dist/pypy/rpython/rmodel.py	Tue Jul  5 14:45:15 2005
@@ -226,11 +226,13 @@
 def getconcretetype(v):
     return getattr(v, 'concretetype', PyObjPtr)
 
-def getfunctionptr(translator, func, getconcretetype=getconcretetype):
+def getfunctionptr(translator, graphfunc, getconcretetype=getconcretetype, _callable=None):
     """Make a functionptr from the given Python function."""
-    graph = translator.getflowgraph(func)
+    graph = translator.getflowgraph(graphfunc)
     llinputs = [getconcretetype(v) for v in graph.getargs()]
     lloutput = getconcretetype(graph.getreturnvar())
     FT = FuncType(llinputs, lloutput)
-    return functionptr(FT, func.func_name, graph = graph, _callable = func)
+    if _callable is None:
+        _callable = graphfunc
+    return functionptr(FT, graphfunc.func_name, graph = graph, _callable = _callable)
 

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Tue Jul  5 14:45:15 2005
@@ -355,10 +355,10 @@
 
     # __________ utilities __________
 
-    def getfunctionptr(self, func):
+    def getfunctionptr(self, graphfunc, _callable=None):
         def getconcretetype(v):
             return self.bindingrepr(v).lowleveltype
-        return getfunctionptr(self.annotator.translator, func, getconcretetype)
+        return getfunctionptr(self.annotator.translator, graphfunc, getconcretetype, _callable=_callable)
 
     def attachRuntimeTypeInfoFunc(self, GCSTRUCT, func, ARG_GCSTRUCT=None):
         self.call_all_setups()  # compute ForwardReferences now
@@ -516,7 +516,7 @@
         dontcare, spec_function = annotate_lowlevel_helper(rtyper.annotator, ll_function, args_s)
 
         # build the 'direct_call' operation
-        f = self.rtyper.getfunctionptr(spec_function)
+        f = self.rtyper.getfunctionptr(spec_function, _callable=ll_function)
         c = inputconst(typeOf(f), f)
         return self.genop('direct_call', [c]+list(args_v),
                           resulttype = typeOf(f).TO.RESULT)

Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py	Tue Jul  5 14:45:15 2005
@@ -1,4 +1,6 @@
 from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.test import test_llinterp
+from pypy.objspace.flow import model as flowmodel
 
 from pypy.annotation.builtin import *
 import py
@@ -61,6 +63,35 @@
             ry = 100 * float(i-10) +0.1
             assert fn(rv,ry) == interpret(fn, (rv, ry))
 
+def DONOT_test_os_getuid():
+    import os
+    def fn():
+        return os.getuid()
+    assert interpret(fn, []) == fn()
+
+def test_os_dup():
+    import os
+    def fn(fd):
+        return os.dup(fd)
+    res = interpret(fn, [0])
+    #try:
+    #    os.close(res)
+    #except OSError:
+    #    pass
+    t = test_llinterp.typer.annotator.translator
+    graph = t.getflowgraph(fn)
+    count = [0]
+    def visit(block):
+        from pypy.rpython import extfunctable
+        if isinstance(block, flowmodel.Block):
+            for op in block.operations:
+                if op.opname == 'direct_call':
+                    cfptr = op.args[0]
+                    assert cfptr.value._obj._callable == extfunctable.ll_os_dup
+                    count[0] += 1
+    flowmodel.traverse(visit, graph)
+    assert count[0] == 1
+
 def test_pbc_isTrue():
     class C:
         def f(self):



More information about the Pypy-commit mailing list