[pypy-svn] r39802 - in pypy/dist/pypy: rpython rpython/lltypesystem/module rpython/module rpython/module/test rpython/ootypesystem/module translator/c translator/c/test

antocuni at codespeak.net antocuni at codespeak.net
Sat Mar 3 14:46:29 CET 2007


Author: antocuni
Date: Sat Mar  3 14:46:27 2007
New Revision: 39802

Removed:
   pypy/dist/pypy/rpython/module/ll_math.py
   pypy/dist/pypy/rpython/module/test/test_ll_math.py
Modified:
   pypy/dist/pypy/rpython/extfunc.py
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py
   pypy/dist/pypy/rpython/ootypesystem/module/ll_math.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/node.py
   pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
(antocuni, pedronis)

Kill the old rpython/module/ll_math.py module and use the new way for declaring external functions.

Things are slightly less messy now...



Modified: pypy/dist/pypy/rpython/extfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunc.py	(original)
+++ pypy/dist/pypy/rpython/extfunc.py	Sat Mar  3 14:46:27 2007
@@ -1,10 +1,9 @@
-
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem.lltype import typeOf
 from pypy.objspace.flow.model import Constant
 from pypy.annotation.model import unionof
 from pypy.annotation.signature import annotation
-from pypy.annotation import model as annmodel
+
 import py
 
 class _callable(object):
@@ -20,12 +19,15 @@
     # we defer a bit annotation here
 
     def compute_result_annotation(self):
+        from pypy.annotation import model as annmodel
         return annmodel.SomeGenericCallable([annotation(i, self.bookkeeper)
                                              for i in self.instance.args],
                            annotation(self.instance.result, self.bookkeeper))
 
 class ExtFuncEntry(ExtRegistryEntry):
     def compute_result_annotation(self, *args_s):
+        if hasattr(self, 'ann_hook'):
+            self.ann_hook()
         if self.signature_args is not None:
             assert len(args_s) == len(self.signature_args),\
                    "Argument number mismatch"
@@ -46,20 +48,33 @@
         ll_result = r_result.lowleveltype
         name = getattr(self, 'name', None) or self.instance.__name__
         method_name = rtyper.type_system.name[:2] + 'typeimpl'
+        fake_method_name = rtyper.type_system.name[:2] + 'typefakeimpl'
         impl = getattr(self, method_name, None)
+        fakeimpl = getattr(self, fake_method_name, self.instance)
         if impl:
             obj = rtyper.getannmixlevel().delayedfunction(
                 impl.im_func, self.signature_args, self.signature_result)
         else:
             obj = rtyper.type_system.getexternalcallable(args_ll, ll_result,
-                                 name, _entry=self, _callable=self.instance)
+                                 name, _entry=self, _callable=fakeimpl)
         vlist = [hop.inputconst(typeOf(obj), obj)] + hop.inputargs(*args_r)
         hop.exception_is_here()
         return hop.genop('direct_call', vlist, r_result)
 
 def register_external(function, args, result=None, export_name=None,
-                      llimpl=None, ooimpl=None):
-    
+                      llimpl=None, ooimpl=None,
+                      llfakeimpl=None, oofakeimpl=None,
+                      annotation_hook=None):
+    """
+    function: the RPython function that will be rendered as an external function (e.g.: math.floor)
+    args: a list containing the annotation of the arguments
+    result: surprisingly enough, the annotation of the result
+    export_name: the name of the function as it will be seen by the backends
+    llimpl, ooimpl: optional; if provided, these RPython functions are called instead of the target function
+    llfakeimpl, oofakeimpl: optional; if provided, they are called by the llinterpreter
+    annotationhook: optional; a callable that is called during annotation, useful for genc hacks
+    """
+
     class FunEntry(ExtFuncEntry):
         _about_ = function
         if args is None:
@@ -72,6 +87,12 @@
             lltypeimpl = llimpl
         if ooimpl:
             ootypeimpl = ooimpl
+        if llfakeimpl:
+            lltypefakeimpl = llfakeimpl
+        if oofakeimpl:
+            ootypefakeimpl = oofakeimpl
+        if annotation_hook:
+            ann_hook = staticmethod(annotation_hook)
 
     if export_name:
         FunEntry.__name__ = export_name

Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Sat Mar  3 14:46:27 2007
@@ -173,18 +173,6 @@
                 'OS_WAITPID')
     return SomeTuple((SomeInteger(),)*2)
 
-def frexpannotation(*args):
-    from pypy.annotation.model import SomeInteger, SomeTuple, SomeFloat
-    from pypy.rpython.lltypesystem.module.ll_math import ll_frexp_result
-    record_call(ll_frexp_result, (SomeFloat(), SomeInteger()), 'MATH_FREXP')
-    return SomeTuple((SomeFloat(), SomeInteger()))
-
-def modfannotation(*args):
-    from pypy.annotation.model import SomeTuple, SomeFloat
-    from pypy.rpython.lltypesystem.module.ll_math import ll_modf_result
-    record_call(ll_modf_result, (SomeFloat(), SomeFloat()), 'MATH_MODF')
-    return SomeTuple((SomeFloat(), SomeFloat()))
-
 def strnullannotation(*args):
     from pypy.annotation.model import SomeString
     return SomeString(can_be_None=True)
@@ -243,28 +231,6 @@
 declare(time.clock  , float         , 'll_time/clock')
 declare(time.sleep  , noneannotation, 'll_time/sleep')
 
-# ___________________________
-# math functions
-
-declare(math.frexp  , frexpannotation, 'll_math/frexp')
-declare(math.atan2  , float         , 'll_math/atan2')
-declare(math.fmod   , float         ,  'll_math/fmod')
-declare(math.floor  , float         ,  'll_math/floor')
-declare(math.ldexp  , float         ,  'll_math/ldexp')
-declare(math.modf   , modfannotation, 'll_math/modf')
-declare(math.hypot  , float         , 'll_math/hypot')
-declare(math.pow    , float         , 'll_math/pow')
-
-# the following functions all take one float, return one float
-# and are part of math.h
-simple_math_functions = [
-    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
-    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
-    ]
-
-for name in simple_math_functions:
-    declare(getattr(math, name), float, 'll_math/%s' % name)
-
 # ___________________________________________________________
 # win/NT hack: patch ntpath.isabs() to be RPythonic
 
@@ -325,3 +291,54 @@
     AssertionError   : True,
     RuntimeError     : True,
     }
+
+
+
+# ______________________________________________________________
+# this declarations use the new interface for external functions
+# all the above declaration should me moved here at some point.
+
+from extfunc import register_external
+
+# ___________________________
+# math functions
+
+from pypy.rpython.lltypesystem.module import ll_math
+from pypy.rpython.ootypesystem.module import ll_math as oo_math
+
+# the following functions all take one float, return one float
+# and are part of math.h
+simple_math_functions = [
+    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
+    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
+    ]
+for name in simple_math_functions:
+    register_external(getattr(math, name), [float], float, "ll_math.ll_math_%s" % name)
+
+def frexp_hook():
+    from pypy.annotation.model import SomeInteger, SomeTuple, SomeFloat
+    from pypy.rpython.lltypesystem.module.ll_math import ll_frexp_result
+    record_call(ll_frexp_result, (SomeFloat(), SomeInteger()), 'MATH_FREXP')
+
+def modf_hook():
+    from pypy.annotation.model import SomeTuple, SomeFloat
+    from pypy.rpython.lltypesystem.module.ll_math import ll_modf_result
+    record_call(ll_modf_result, (SomeFloat(), SomeFloat()), 'MATH_MODF')
+
+complex_math_functions = [
+    ('frexp', [float],        (float, int),   frexp_hook),
+    ('atan2', [float],        float,          None),
+    ('fmod',  [float, float], float,          None),
+    ('ldexp', [float, int],   float,          None),
+    ('modf',  [float],        (float, float), modf_hook),
+    ('hypot', [float, float], float,          None),
+    ('pow',   [float, float], float,          None),
+    ]
+
+for name, args, res, hook in complex_math_functions:
+    func = getattr(math, name)
+    llfake = getattr(ll_math, 'll_math_%s' % name, None)
+    oofake = getattr(oo_math, 'll_math_%s' % name, None)
+    register_external(func, args, res, 'll_math.ll_math_%s' % name,
+                      llfakeimpl=llfake, oofakeimpl=oofake,
+                      annotation_hook = hook)

Modified: pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py	Sat Mar  3 14:46:27 2007
@@ -1,7 +1,5 @@
 import math
-
 from pypy.rpython.lltypesystem import lltype, rtupletype
-from pypy.tool.staticmethods import ClassMethods
 
 FREXP_RESULT = rtupletype.TUPLE_TYPE([lltype.Float, lltype.Signed]).TO
 MODF_RESULT = rtupletype.TUPLE_TYPE([lltype.Float, lltype.Float]).TO
@@ -18,15 +16,10 @@
     tup.item1 = intpart
     return tup
 
-class Implementation:
-    __metaclass__ = ClassMethods
-
-    def ll_math_frexp(cls, x):
-        mantissa, exponent = math.frexp(x)
-        return ll_frexp_result(mantissa, exponent)
-    ll_math_frexp.suggested_primitive = True
-
-    def ll_math_modf(cls, x):
-        fracpart, intpart = math.modf(x)
-        return ll_modf_result(fracpart, intpart)
-    ll_math_modf.suggested_primitive = True
+def ll_math_frexp(cls, x):
+    mantissa, exponent = math.frexp(x)
+    return ll_frexp_result(mantissa, exponent)
+
+def ll_math_modf(cls, x):
+    fracpart, intpart = math.modf(x)
+    return ll_modf_result(fracpart, intpart)

Modified: pypy/dist/pypy/rpython/ootypesystem/module/ll_math.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/module/ll_math.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/module/ll_math.py	Sat Mar  3 14:46:27 2007
@@ -1,7 +1,5 @@
 import math
-
 from pypy.rpython.ootypesystem import ootype
-from pypy.tool.staticmethods import ClassMethods
 
 FREXP_RESULT = ootype.Record({"item0": ootype.Float, "item1": ootype.Signed})
 MODF_RESULT = ootype.Record({"item0": ootype.Float, "item1": ootype.Float})
@@ -18,15 +16,11 @@
     tup.item1 = intpart
     return tup
 
-class Implementation:
-    __metaclass__ = ClassMethods
+def ll_math_frexp(cls, x):
+    mantissa, exponent = math.frexp(x)
+    return ll_frexp_result(mantissa, exponent)
+
+def ll_math_modf(cls, x):
+    fracpart, intpart = math.modf(x)
+    return ll_modf_result(fracpart, intpart)
 
-    def ll_math_frexp(cls, x):
-        mantissa, exponent = math.frexp(x)
-        return ll_frexp_result(mantissa, exponent)
-    ll_math_frexp.suggested_primitive = True
-
-    def ll_math_modf(cls, x):
-        fracpart, intpart = math.modf(x)
-        return ll_modf_result(fracpart, intpart)
-    ll_math_modf.suggested_primitive = True

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Sat Mar  3 14:46:27 2007
@@ -5,12 +5,11 @@
 from pypy.rpython.lltypesystem.rstr import STR, mallocstr
 from pypy.rpython.lltypesystem import rstr
 from pypy.rpython.lltypesystem import rlist
-from pypy.rpython.module import ll_time, ll_math, ll_os
+from pypy.rpython.module import ll_time, ll_os
 from pypy.rpython.module import ll_stackless, ll_stack
 from pypy.rpython.lltypesystem.module.ll_os import STAT_RESULT, PIPE_RESULT
 from pypy.rpython.lltypesystem.module.ll_os import WAITPID_RESULT
 from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
-from pypy.rpython.lltypesystem.module import ll_math as ll_math2
 from pypy.rpython.lltypesystem.module import ll_strtod
 from pypy.rlib import ros
 
@@ -64,13 +63,6 @@
     ll_time.ll_time_clock: 'LL_time_clock',
     ll_time.ll_time_sleep: 'LL_time_sleep',
     ll_time.ll_time_time:  'LL_time_time',
-    ll_math.ll_math_pow:   'LL_math_pow',
-    ll_math2.Implementation.ll_math_frexp.im_func: 'LL_math_frexp',
-    ll_math.ll_math_atan2: 'LL_math_atan2',
-    ll_math.ll_math_fmod : 'LL_math_fmod',
-    ll_math.ll_math_ldexp: 'LL_math_ldexp',
-    ll_math2.Implementation.ll_math_modf.im_func:  'LL_math_modf',
-    ll_math.ll_math_hypot: 'LL_math_hypot',
     ll_strtod.Implementation.ll_strtod_parts_to_float:
         'LL_strtod_parts_to_float',
     ll_strtod.Implementation.ll_strtod_formatd:
@@ -93,13 +85,20 @@
 #______________________________________________________
 # insert 'simple' math functions into EXTERNALs table:
 
-simple_math_functions = [
+# XXX: messy, messy, messy
+# this interacts in strange ways with node.select_function_code_generators,
+# because it fakes to be an ll_* function.
+
+math_functions = [
     'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
-    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
+    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh',
+    'frexp', 'pow', 'atan2', 'fmod', 'ldexp', 'modf', 'hypot'
     ]
 
-for name in simple_math_functions:
-    EXTERNALS[getattr(ll_math, 'll_math_%s' % name)] = 'LL_math_%s' % name
+import math
+for name in math_functions:
+    EXTERNALS['ll_math.ll_math_%s' % name] = 'LL_math_%s' % name
+
 
 #______________________________________________________
 
@@ -110,13 +109,14 @@
     return None
 
 def predeclare_common_types(db, rtyper):
+    from pypy.rpython.lltypesystem.module import ll_math
     # Common types
     yield ('RPyString', STR)
     LIST_OF_STR = find_list_of_str(rtyper)
     if LIST_OF_STR is not None:
         yield ('RPyListOfString', LIST_OF_STR)
-    yield ('RPyFREXP_RESULT', ll_math2.FREXP_RESULT)
-    yield ('RPyMODF_RESULT', ll_math2.MODF_RESULT)
+    yield ('RPyFREXP_RESULT', ll_math.FREXP_RESULT)
+    yield ('RPyMODF_RESULT', ll_math.MODF_RESULT)
     yield ('RPySTAT_RESULT', STAT_RESULT)
     yield ('RPyPIPE_RESULT', PIPE_RESULT)
     yield ('RPyWAITPID_RESULT', WAITPID_RESULT)

Modified: pypy/dist/pypy/translator/c/node.py
==============================================================================
--- pypy/dist/pypy/translator/c/node.py	(original)
+++ pypy/dist/pypy/translator/c/node.py	Sat Mar  3 14:46:27 2007
@@ -688,7 +688,10 @@
         funcgen.implementation_end()
 
 def select_function_code_generators(fnobj, db, functionname):
-    if fnobj._callable in extfunc.EXTERNALS:
+    if hasattr(fnobj, '_entry'):
+        db.externalfuncs[fnobj._entry.name] = fnobj
+        return []
+    elif fnobj._callable in extfunc.EXTERNALS:
         # 'fnobj' is one of the ll_xyz() functions with the suggested_primitive
         # flag in pypy.rpython.module.*.  The corresponding C wrappers are
         # written by hand in src/ll_*.h, and declared in extfunc.EXTERNALS.

Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py	Sat Mar  3 14:46:27 2007
@@ -7,7 +7,7 @@
 from pypy.rlib import ros
 
 def test_all_suggested_primitives():
-    for modulename in ['ll_math', 'll_os', 'll_os_path', 'll_time']:
+    for modulename in ['ll_os', 'll_os_path', 'll_time']:
         mod = __import__('pypy.rpython.module.%s' % modulename,
                          None, None, ['__doc__'])
         for func in mod.__dict__.values():



More information about the Pypy-commit mailing list