[pypy-svn] r22758 - in pypy/dist/pypy: annotation jit jit/test

pedronis at codespeak.net pedronis at codespeak.net
Fri Jan 27 19:51:31 CET 2006


Author: pedronis
Date: Fri Jan 27 19:51:27 2006
New Revision: 22758

Added:
   pypy/dist/pypy/jit/hintannotator.py   (contents, props changed)
   pypy/dist/pypy/jit/hintbookkeeper.py   (contents, props changed)
   pypy/dist/pypy/jit/hintmodel.py   (contents, props changed)
   pypy/dist/pypy/jit/test/test_hint_annotation.py   (contents, props changed)
Modified:
   pypy/dist/pypy/annotation/annrpython.py
Log:
(arigo, arre, pedronis)

start of off-line partial evalutation, based on the annotator framework.

see PyPy The Movie, coming soon ... 




Modified: pypy/dist/pypy/annotation/annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/annrpython.py	(original)
+++ pypy/dist/pypy/annotation/annrpython.py	Fri Jan 27 19:51:27 2006
@@ -624,23 +624,6 @@
         assert isinstance(op.result, Variable)
         self.setbinding(op.result, resultcell)  # bind resultcell to op.result
 
-    def _registeroperations(loc):
-        # All unary operations
-        for opname in annmodel.UNARY_OPERATIONS:
-            exec """
-def consider_op_%s(self, arg, *args):
-    return arg.%s(*args)
-""" % (opname, opname) in globals(), loc
-        # All binary operations
-        for opname in annmodel.BINARY_OPERATIONS:
-            exec """
-def consider_op_%s(self, arg1, arg2, *args):
-    return pair(arg1,arg2).%s(*args)
-""" % (opname, opname) in globals(), loc
-
-    _registeroperations(locals())
-    del _registeroperations
-
     # XXX "contains" clash with SomeObject method
     def consider_op_contains(self, seq, elem):
         self.bookkeeper.count("contains", seq)
@@ -664,6 +647,24 @@
         return annmodel.SomeSlice(start, stop, step)
 
 
+def _registeroperations(ns, model):
+    # All unary operations
+    for opname in model.UNARY_OPERATIONS:
+        exec """
+def consider_op_%s(self, arg, *args):
+    return arg.%s(*args)
+""" % (opname, opname) in globals(), ns
+    # All binary operations
+    for opname in model.BINARY_OPERATIONS:
+        exec """
+def consider_op_%s(self, arg1, arg2, *args):
+    return pair(arg1,arg2).%s(*args)
+""" % (opname, opname) in globals(), ns
+
+# register simple operations handling
+_registeroperations(RPythonAnnotator.__dict__, annmodel)
+
+
 class CannotSimplify(Exception):
     pass
 

Added: pypy/dist/pypy/jit/hintannotator.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/hintannotator.py	Fri Jan 27 19:51:27 2006
@@ -0,0 +1,12 @@
+from pypy.annotation.annrpython import RPythonAnnotator, _registeroperations
+from pypy.jit import hintmodel
+from pypy.jit.hintbookkeeper import HintBookkeeper
+
+class HintAnnotator(RPythonAnnotator):
+
+    def __init__(self):
+        RPythonAnnotator.__init__(self)
+        self.bookkeeper = HintBookkeeper() # XXX
+
+
+_registeroperations(HintAnnotator.__dict__, hintmodel)

Added: pypy/dist/pypy/jit/hintbookkeeper.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/hintbookkeeper.py	Fri Jan 27 19:51:27 2006
@@ -0,0 +1,42 @@
+from pypy.tool.tls import tlsobject
+
+TLS = tlsobject()
+
+class HintBookkeeper(object):
+
+    def __init__(self):
+        self.pending_specializations = []
+        self.origins = {}
+
+    def enter(self, position_key):
+        """Start of an operation.
+        The operation is uniquely identified by the given key."""
+        self.position_key = position_key
+        TLS.bookkeeper = self
+
+    def leave(self):
+        """End of an operation."""
+        del TLS.bookkeeper
+        del self.position_key
+
+    def myorigin(self):
+        try:
+            origin = self.origins[self.position_key]
+        except KeyError:
+            from pypy.jit import hintmodel
+            origin = hintmodel.OriginTreeNode()
+            self.origins[self.position_key] = origin
+        return origin
+
+    def compute_at_fixpoint(self):
+        pass
+
+# get current bookkeeper
+
+def getbookkeeper():
+    """Get the current Bookkeeper.
+    Only works during the analysis of an operation."""
+    try:
+        return TLS.bookkeeper
+    except AttributeError:
+        return None

Added: pypy/dist/pypy/jit/hintmodel.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/hintmodel.py	Fri Jan 27 19:51:27 2006
@@ -0,0 +1,42 @@
+from pypy.annotation import model as annmodel
+from pypy.annotation.pairtype import pair, pairtype
+from pypy.jit.hintbookkeeper import getbookkeeper
+from pypy.rpython.lltypesystem import lltype
+
+UNARY_OPERATIONS = "".split()
+
+BINARY_OPERATIONS = "int_add".split()
+
+class OriginTreeNode(object):
+
+    def __init__(self, origins=None):
+        if origins is None:
+            origins = {}
+        self.origins = origins
+
+    def merge(self, nodes):
+        self.origins.update(nodes)
+
+class SomeLLAbstractValue(annmodel.SomeObject):
+
+    def __init__(self, T):
+        self.concretetype = T
+
+class SomeLLAbstractVariable(SomeLLAbstractValue):
+    pass
+
+class SomeLLAbstractConstant(SomeLLAbstractValue):
+
+    def __init__(self, T, origins):
+        SomeLLAbstractValue.__init__(self, T)
+        self.origins = origins
+
+
+class __extend__(pairtype(SomeLLAbstractConstant, SomeLLAbstractConstant)):
+
+    def int_add((hs_c1, hs_c2)):
+        origin = getbookkeeper().myorigin()
+        origin.merge(hs_c1.origins)
+        origin.merge(hs_c2.origins)
+        return SomeLLAbstractConstant(lltype.Signed, {origin: True})
+

Added: pypy/dist/pypy/jit/test/test_hint_annotation.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/test/test_hint_annotation.py	Fri Jan 27 19:51:27 2006
@@ -0,0 +1,27 @@
+from pypy.translator.translator import TranslationContext, graphof
+from pypy.jit.hintannotator import HintAnnotator
+from pypy.jit.hintmodel import SomeLLAbstractConstant, OriginTreeNode
+from pypy.rpython.lltypesystem import lltype
+
+def hannotate(func, argtypes):
+    # build the normal ll graphs for ll_function
+    t = TranslationContext()
+    a = t.buildannotator()
+    a.build_types(func, argtypes)
+    rtyper = t.buildrtyper()
+    rtyper.specialize()
+    graph1 = graphof(t, func)
+    # build hint annotator types
+    hannotator = HintAnnotator()
+    hs = hannotator.build_graph_types(graph1, [SomeLLAbstractConstant(v.concretetype,
+                                                                      {OriginTreeNode(): True})
+                                               for v in graph1.getargs()])
+    return hs
+
+def test_simple():
+    def ll_function(x, y):
+        return x + y
+    hs = hannotate(ll_function, [int, int])
+    assert isinstance(hs, SomeLLAbstractConstant)
+    assert len(hs.origins) == 1
+    assert hs.concretetype == lltype.Signed



More information about the Pypy-commit mailing list