[pypy-svn] r29509 - in pypy/dist/pypy/jit/hintannotator: . test

arigo at codespeak.net arigo at codespeak.net
Thu Jun 29 20:16:14 CEST 2006


Author: arigo
Date: Thu Jun 29 20:16:12 2006
New Revision: 29509

Modified:
   pypy/dist/pypy/jit/hintannotator/model.py
   pypy/dist/pypy/jit/hintannotator/test/test_annotator.py
Log:
(arre, pedronis, arigo)

Clean-ups and more operations support in hintannotator.model.



Modified: pypy/dist/pypy/jit/hintannotator/model.py
==============================================================================
--- pypy/dist/pypy/jit/hintannotator/model.py	(original)
+++ pypy/dist/pypy/jit/hintannotator/model.py	Thu Jun 29 20:16:12 2006
@@ -1,7 +1,7 @@
 from pypy.annotation import model as annmodel
 from pypy.annotation.pairtype import pair, pairtype
 from pypy.jit.hintannotator.bookkeeper import getbookkeeper
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, lloperation
 
 UNARY_OPERATIONS = """same_as hint getfield setfield getsubstruct getarraysize setarrayitem
                       cast_pointer
@@ -50,12 +50,14 @@
                 for p in self.read_positions:
                     annotator.reflowfromposition(p)
 
+
 class SomeLLAbstractValue(annmodel.SomeObject):
 
     def __init__(self, T):
         self.concretetype = T
         assert self.__class__ != SomeLLAbstractValue
 
+
 class SomeLLAbstractConstant(SomeLLAbstractValue):
 
     def __init__(self, T, origins, eager_concrete=False):
@@ -97,9 +99,11 @@
             return None
     annotationcolor = property(annotationcolor)
 
+
 class SomeLLAbstractVariable(SomeLLAbstractValue):
     pass
 
+
 class SomeLLAbstractContainer(SomeLLAbstractValue):
 
     def __init__(self, contentdef):
@@ -149,24 +153,18 @@
 
 class __extend__(SomeLLAbstractValue):
 
-    def define_unary(TYPE):
-        def var_unary(hs_v):
-            return SomeLLAbstractVariable(TYPE)
-        return var_unary
-
-    int_neg = define_unary(lltype.Signed)
-    uint_is_true = int_is_true = define_unary(lltype.Bool)
-
     def same_as(hs_v1):
         return hs_v1
 
     def hint(hs_v1, hs_flags):
         if hs_flags.const.get('variable', False): # only for testing purposes!!!
-            return SomeLLAbstractVariable(hs_c1.concretetype)
+            return SomeLLAbstractVariable(hs_v1.concretetype)
         if hs_flags.const.get('concrete', False):
             raise HintError("cannot make a concrete from %r" % (hs_v1,))
         if hs_flags.const.get('forget', False):
-            XXX    # not implemented
+            # turn a variable to a constant
+            origin = getbookkeeper().myorigin()
+            return SomeLLAbstractConstant(hs_v1.concretetype, {origin: True})
 
     def getfield(hs_v1, hs_fieldname):
         S = hs_v1.concretetype.TO
@@ -181,8 +179,25 @@
         FIELD_TYPE = getattr(S, hs_fieldname.const)
         return SomeLLAbstractVariable(lltype.Ptr(FIELD_TYPE))
 
+    def getarrayitem(hs_v1, hs_index):
+        ARRAY = hs_v1.concretetype.TO
+        return SomeLLAbstractVariable(ARRAY.OF)
+
+    def setarrayitem(hs_v1, hs_index, hs_value):
+        pass
+
+    def getarraysubstruct(hs_v1, hs_index):
+        ARRAY = hs_v1.concretetype.TO
+        return SomeLLAbstractVariable(lltype.Ptr(ARRAY.OF))
+
+
 class __extend__(SomeLLAbstractConstant):
 
+    def same_as(hs_c1):
+        # this is here to prevent setup() below from adding a different
+        # version of same_as()
+        return hs_c1
+
     def hint(hs_c1, hs_flags):
         if hs_flags.const.get('variable', False): # only for testing purposes!!!
             return SomeLLAbstractVariable(hs_c1.concretetype)
@@ -282,24 +297,7 @@
         d = setadd(hs_c1.origins, getbookkeeper().myorigin())
         return SomeLLAbstractConstant(lltype.Ptr(SUB_TYPE), d)
 
-    def getarraysize(hs_c1):
-        d = setadd(hs_c1.origins, getbookkeeper().myorigin())
-        return SomeLLAbstractConstant(lltype.Signed, d)
 
-    def define_unary(TYPE):
-        def const_unary(hs_c1):
-            d = setadd(hs_c1.origins, getbookkeeper().myorigin())
-            return SomeLLAbstractConstant(TYPE, d, eager_concrete=hs_c1.eager_concrete)
-        return const_unary
-
-    cast_int_to_char = define_unary(lltype.Char)
-    
-    cast_uint_to_int = cast_bool_to_int = cast_char_to_int = int_neg = define_unary(lltype.Signed)
-
-    cast_int_to_uint = define_unary(lltype.Unsigned)
-
-    uint_is_true = int_is_true = define_unary(lltype.Bool)
-    
 class __extend__(SomeLLAbstractContainer):
 
     def setfield(hs_s1, hs_fieldname, hs_value):
@@ -327,28 +325,13 @@
 
 class __extend__(pairtype(SomeLLAbstractValue, SomeLLAbstractValue)):
 
-    def define_binary(TYPE):
-        def var_binary((hs_v1, hs_v2)):
-            return SomeLLAbstractVariable(TYPE)
-        return var_binary
-
-    int_mul = int_mod = int_sub = int_add = define_binary(lltype.Signed)
-    int_floordiv = int_rshift = int_and = int_add 
-
-    uint_mul = uint_mod = uint_sub = uint_add = define_binary(lltype.Unsigned)
-    uint_floordiv = uint_rshift = uint_and = uint_add
-
-    int_lt = int_le = int_ge = int_ne = int_gt = int_eq = define_binary(lltype.Bool)
-    uint_lt = uint_le = uint_ge = uint_ne = uint_gt = uint_eq = int_eq
-
-    char_gt = char_lt = char_le = char_ge = char_eq = char_ne = int_eq
-
     def getarrayitem((hs_v1, hs_v2)):
         return SomeLLAbstractVariable(hs_v1.concretetype.TO.OF)
 
     def union((hs_v1, hs_v2)):
         raise annmodel.UnionError("%s %s don't mix" % (hs_v1, hs_v2))
 
+
 class __extend__(pairtype(SomeLLAbstractVariable, SomeLLAbstractConstant),
                  pairtype(SomeLLAbstractConstant, SomeLLAbstractVariable)):
 
@@ -358,25 +341,8 @@
             raise annmodel.UnionError("%s %s don't mix" % (hs_v1, hs_v2))
         return SomeLLAbstractVariable(hs_v1.concretetype)
 
-class __extend__(pairtype(SomeLLAbstractConstant, SomeLLAbstractConstant)):
-
-    def define_binary(TYPE):
-        def const_binary((hs_c1, hs_c2)):
-            d = newset(hs_c1.origins, hs_c2.origins,
-                       {getbookkeeper().myorigin(): True})
-            return SomeLLAbstractConstant(TYPE, d, eager_concrete= hs_c1.eager_concrete or hs_c2.eager_concrete)
-        return const_binary
-            
-    int_mul = int_mod = int_sub = int_add = define_binary(lltype.Signed)
-    int_floordiv = int_rshift = int_and = int_add 
-
-    uint_mul = uint_mod = uint_sub = uint_add = define_binary(lltype.Unsigned)
-    uint_floordiv = uint_rshift = uint_and = uint_add
 
-    int_lt = int_le = int_ge = int_ne = int_gt = int_eq = define_binary(lltype.Bool)
-    uint_lt = uint_le = uint_ge = uint_ne = uint_gt = uint_eq = int_eq
-
-    char_gt = char_lt = char_le = char_ge = char_eq = char_ne = int_eq
+class __extend__(pairtype(SomeLLAbstractConstant, SomeLLAbstractConstant)):
 
     def union((hs_c1, hs_c2)):
         assert hs_c1.concretetype == hs_c2.concretetype
@@ -393,22 +359,26 @@
         else:
             return SomeLLAbstractVariable(READ_TYPE)
 
+
 class __extend__(pairtype(SomeLLAbstractContainer, SomeLLAbstractContainer)):
 
     def union((hs_cont1, hs_cont2)):
         contentdef = hs_cont1.contentdef.union(hs_cont2.contentdef)
         return SomeLLAbstractContainer(contentdef)
 
+
 class __extend__(pairtype(SomeLLAbstractContainer, SomeLLAbstractValue)):
     def union((hs_cont1, hs_val2)):
         hs_cont1.contentdef.mark_degenerated()
         assert hs_cont1.concretetype == hs_val2.concretetype
         return SomeLLAbstractVariable(hs_cont1.concretetype)
 
+
 class __extend__(pairtype(SomeLLAbstractValue, SomeLLAbstractContainer)):
     def union((hs_val1, hs_cont2)):
         return pair(hs_cont2, hs_val1).union()
 
+
 class __extend__(pairtype(SomeLLAbstractContainer, SomeLLAbstractConstant)):
 
     def getarrayitem((hs_a1, hs_index)):
@@ -461,3 +431,47 @@
 
     hs_result = handler(*args_hs)   # which may raise NotImplementedError
     return hs_result
+
+# ____________________________________________________________
+#
+# Register automatically simple operations
+
+def var_unary(hs_v, *rest_hs):
+    RESTYPE = getbookkeeper().current_op_concretetype()
+    return SomeLLAbstractVariable(RESTYPE)
+
+def var_binary((hs_v1, hs_v2), *rest_hs):
+    RESTYPE = getbookkeeper().current_op_concretetype()
+    return SomeLLAbstractVariable(RESTYPE)
+
+def const_unary(hs_c1):
+    bk = getbookkeeper()
+    d = setadd(hs_c1.origins, bk.myorigin())
+    RESTYPE = bk.current_op_concretetype()
+    return SomeLLAbstractConstant(RESTYPE, d,
+                                  eager_concrete = hs_c1.eager_concrete)
+
+def const_binary((hs_c1, hs_c2)):
+    bk = getbookkeeper()
+    d = newset(hs_c1.origins, hs_c2.origins, {bk.myorigin(): True})
+    RESTYPE = bk.current_op_concretetype()
+    return SomeLLAbstractConstant(RESTYPE, d,
+                                  eager_concrete = hs_c1.eager_concrete or
+                                                   hs_c2.eager_concrete)
+
+def setup(oplist, ValueCls, var_fn, ConstantCls, const_fn):
+    for name in oplist:
+        llop = getattr(lloperation.llop, name)
+        if not llop.sideeffects:
+            if name not in ValueCls.__dict__:
+                setattr(ValueCls, name, var_fn)
+            if llop.canfold:
+                if name not in ConstantCls.__dict__:
+                    setattr(ConstantCls, name, const_fn)
+setup(UNARY_OPERATIONS,
+      SomeLLAbstractValue, var_unary,
+      SomeLLAbstractConstant, const_unary)
+setup(BINARY_OPERATIONS,
+      pairtype(SomeLLAbstractValue, SomeLLAbstractValue), var_binary,
+      pairtype(SomeLLAbstractConstant, SomeLLAbstractConstant), const_binary)
+del setup

Modified: pypy/dist/pypy/jit/hintannotator/test/test_annotator.py
==============================================================================
--- pypy/dist/pypy/jit/hintannotator/test/test_annotator.py	(original)
+++ pypy/dist/pypy/jit/hintannotator/test/test_annotator.py	Thu Jun 29 20:16:12 2006
@@ -13,6 +13,10 @@
 P_OOPSPEC = AnnotatorPolicy()
 P_OOPSPEC.oopspec = True
 
+P_OOPSPEC_NOVIRTUAL = AnnotatorPolicy()
+P_OOPSPEC_NOVIRTUAL.oopspec = True
+P_OOPSPEC_NOVIRTUAL.novirtualcontainer = True
+
 def hannotate(func, argtypes, policy=None, annotator=False, inline=None):
     # build the normal ll graphs for ll_function
     t = TranslationContext()
@@ -140,7 +144,9 @@
 
 def test_op_meet():
     def meet(hs1, hs2):
-        HintBookkeeper(None).enter(None)
+        bk = HintBookkeeper(None)
+        bk.enter(None)
+        bk.current_op_concretetype = lambda: lltype.Signed     # hack
         return pair(hs1, hs2).int_add()
     av1, av2 = SomeLLAbstractVariable(lltype.Signed), SomeLLAbstractVariable(lltype.Signed)
     cv1, cv2 = SomeLLAbstractConstant(lltype.Signed, {}, True), SomeLLAbstractConstant(lltype.Signed, {}, True)
@@ -423,6 +429,10 @@
     from pypy.jit.tl import tl
     hannotate(tl.interp, [str, int, int], policy=P_OOPSPEC)
 
+def test_hannotate_tl_novirtual():
+    from pypy.jit.tl import tl
+    hannotate(tl.interp, [str, int, int], policy=P_OOPSPEC_NOVIRTUAL)
+
 def test_hannotate_plus_minus():
     def ll_plus_minus(s, x, y):
         acc = x



More information about the Pypy-commit mailing list