[pypy-svn] r76351 - pypy/trunk/pypy/rlib
fijal at codespeak.net
fijal at codespeak.net
Mon Jul 26 20:22:22 CEST 2010
Author: fijal
Date: Mon Jul 26 20:22:19 2010
New Revision: 76351
Modified:
pypy/trunk/pypy/rlib/objectmodel.py
Log:
Simplify @specialize. There is more code now, but most of it is simple and
contains docstrings (unlike magic __getattr__ before). magic--
Modified: pypy/trunk/pypy/rlib/objectmodel.py
==============================================================================
--- pypy/trunk/pypy/rlib/objectmodel.py (original)
+++ pypy/trunk/pypy/rlib/objectmodel.py Mon Jul 26 20:22:19 2010
@@ -19,29 +19,66 @@
# def f(...
#
-class _AttachSpecialization(object):
+class _Specialize(object):
+ def memo(self):
+ """ Specialize functions based on argument values. All arguments has
+ to be constant at the compile time. The whole function call is replaced
+ by a call result then.
+ """
+ def decorated_func(func):
+ func._annspecialcase_ = 'memo()'
+ return func
+ return decorated_func
- def __init__(self, tag):
- self.tag = tag
+ def arg(self, *args):
+ """ Specialize function based on values of given positions of arguments.
+ They must be compile-time constants in order to work.
+
+ There will be a copy of provided function for each combination
+ of given arguments on positions in args (that can lead to
+ exponential behavior!).
+ """
+ def decorated_func(func):
+ func._annspecialcase_ = 'args' + self._wrap(args)
+ return func
- def __call__(self, *args):
- if not args:
- args = ""
- else:
- args = "("+','.join([repr(arg) for arg in args]) +")"
- specialcase = "specialize:%s%s" % (self.tag, args)
-
- def specialize_decorator(func):
- "NOT_RPYTHON"
- func._annspecialcase_ = specialcase
+ return decorated_func
+
+ def argtype(self, *args):
+ """ Specialize function based on types of arguments on given positions.
+
+ There will be a copy of provided function for each combination
+ of given arguments on positions in args (that can lead to
+ exponential behavior!).
+ """
+ def decorated_func(func):
+ func._annspecialcase_ = 'argtypes' + self._wrap(args)
return func
- return specialize_decorator
-
-class _Specialize(object):
+ return decorated_func
+
+ def ll(self):
+ """ This is version of argtypes that cares about low-level types
+ (so it'll get additional copies for two different types of pointers
+ for example). Same warnings about exponential behavior apply.
+ """
+ def decorated_func(func):
+ func._annspecialcase_ = 'll()'
+ return func
+
+ return decorated_func
+
+ def ll_and_arg(self, arg):
+ """ XXX what does that do?
+ """
+ def decorated_func(func):
+ func._annspecialcase_ = 'll_and_arg(%d)' % arg
+ return func
+
+ return decorated_func
- def __getattr__(self, name):
- return _AttachSpecialization(name)
+ def _wrap(self, args):
+ return "("+','.join([repr(arg) for arg in args]) +")"
specialize = _Specialize()
More information about the Pypy-commit
mailing list