[pypy-commit] pypy expressions-2: Create expression object V_Type and use it during flowing

rlamy noreply at buildbot.pypy.org
Mon Nov 17 19:59:09 CET 2014


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: expressions-2
Changeset: r74551:2bcbfeca1b48
Date: 2014-11-08 01:57 +0000
http://bitbucket.org/pypy/pypy/changeset/2bcbfeca1b48/

Log:	Create expression object V_Type and use it during flowing

diff --git a/rpython/annotator/expression.py b/rpython/annotator/expression.py
new file mode 100644
--- /dev/null
+++ b/rpython/annotator/expression.py
@@ -0,0 +1,17 @@
+from rpython.flowspace.model import Variable
+from rpython.flowspace.operation import op
+from rpython.annotator.model import SomeType
+
+class V_Type(Variable):
+    def __init__(self, v_obj):
+        Variable.__init__(self)
+        self.arg = v_obj
+        s = SomeType()
+        s.is_type_of = [v_obj]
+        self.annotation = s
+
+    def as_operation(self):
+        return op.type(self.arg)
+
+    def __eq__(self, other):
+        return isinstance(other, V_Type) and other.arg == self.arg
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -8,7 +8,7 @@
 from rpython.annotator.model import (SomeObject, SomeInteger, SomeBool,
     SomeString, SomeChar, SomeList, SomeDict, SomeTuple, SomeImpossibleValue,
     SomeUnicodeCodePoint, SomeInstance, SomeBuiltin, SomeBuiltinMethod,
-    SomeFloat, SomeIterator, SomePBC, SomeNone, SomeType, s_ImpossibleValue,
+    SomeFloat, SomeIterator, SomePBC, SomeNone, s_ImpossibleValue,
     s_Bool, s_None, unionof, add_knowntypedata,
     HarmlesslyBlocked, SomeWeakRef, SomeUnicodeString, SomeByteArray)
 from rpython.annotator.bookkeeper import getbookkeeper, immutablevalue
@@ -21,11 +21,9 @@
                         if oper.dispatch == 1])
 UNARY_OPERATIONS.remove('contains')
 
- at op.type.register(SomeObject)
-def type_SomeObject(annotator, arg):
-    r = SomeType()
-    r.is_type_of = [arg]
-    return r
+ at op.assign.register(SomeObject)
+def assign(annotator, v_obj):
+    return annotator.annotation(v_obj)
 
 @op.bool.register(SomeObject)
 def bool_SomeObject(annotator, obj):
diff --git a/rpython/flowspace/model.py b/rpython/flowspace/model.py
--- a/rpython/flowspace/model.py
+++ b/rpython/flowspace/model.py
@@ -248,7 +248,7 @@
 
 
 class Variable(object):
-    __slots__ = ["_name", "_nr", "annotation", "concretetype"]
+    __slots__ = ["_name", "_nr", "annotation", "concretetype", "equals"]
 
     dummyname = 'v'
     namesdict = {dummyname: (dummyname, 0)}
@@ -576,7 +576,8 @@
                 for v in op.args:
                     assert isinstance(v, (Constant, Variable))
                     if isinstance(v, Variable):
-                        usevar(v)
+                        if type(v) is Variable:
+                            usevar(v)
                     else:
                         assert v.value is not last_exception
                         #assert v.value != last_exc_value
@@ -646,14 +647,10 @@
                     assert link.last_exc_value is None
                 for v in link.args:
                     assert isinstance(v, (Constant, Variable))
-                    if isinstance(v, Variable):
+                    if type(v) is Variable:
                         usevar(v, in_link=link)
                         if exc_link:
                             assert v != block.operations[-1].result
-                    #else:
-                    #    if not exc_link:
-                    #        assert v.value is not last_exception
-                    #        #assert v.value != last_exc_value
                 allexitcases[link.exitcase] = True
             assert len(allexitcases) == len(block.exits)
             vars_previous_blocks.update(vars)
diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -356,7 +356,7 @@
 
 add_operator('is_', 2, dispatch=2, pure=True)
 add_operator('id', 1, dispatch=1, pyfunc=id)
-add_operator('type', 1, dispatch=1, pyfunc=new_style_type, pure=True)
+#add_operator('type', 1, dispatch=1, pyfunc=new_style_type, pure=True)
 add_operator('issubtype', 2, dispatch=1, pyfunc=issubclass, pure=True)  # not for old-style classes
 add_operator('repr', 1, dispatch=1, pyfunc=repr, pure=True)
 add_operator('str', 1, dispatch=1, pyfunc=str, pure=True)
@@ -431,6 +431,7 @@
 add_operator('yield_', 1)
 add_operator('newslice', 3)
 add_operator('hint', None, dispatch=1)
+add_operator('assign', 1, dispatch=1)
 
 class Contains(SingleDispatchMixin, PureOperation):
     opname = 'contains'
@@ -442,6 +443,21 @@
     def get_specialization(cls, s_seq, s_elem):
         return cls._dispatch(type(s_seq))
 
+class Type(SingleDispatchMixin, PureOperation):
+    opname = 'type'
+    arity = 1
+    canraise = []
+    pyfunc = staticmethod(new_style_type)
+
+    def eval(self, ctx):
+        result = self.constfold()
+        if result is not None:
+            return result
+        from rpython.annotator.expression import V_Type
+        v_instance, = self.args
+        result = V_Type(v_instance)
+        return ctx.do_op(op.assign(result))
+
 
 class NewDict(HLOperation):
     opname = 'newdict'
diff --git a/rpython/translator/transform.py b/rpython/translator/transform.py
--- a/rpython/translator/transform.py
+++ b/rpython/translator/transform.py
@@ -134,6 +134,16 @@
                     s_dict = self.annotation(op.args[0])
                     s_dict.dictdef.generalize_key(self.binding(op.args[1]))
 
+def remove_assign(ann, block_subset):
+    for block in block_subset:
+        for i in range(len(block.operations)):
+            op = block.operations[i]
+            if op.opname == 'assign':
+                new_op = op.args[0].as_operation()
+                new_op.result = op.result
+                block.operations[i] = new_op
+
+
 
 def transform_dead_op_vars(self, block_subset):
     # we redo the same simplification from simplify.py,
@@ -249,6 +259,7 @@
     transform_extend_with_str_slice,
     transform_extend_with_char_count,
     transform_list_contains,
+    remove_assign,
     ]
 
 def transform_graph(ann, extra_passes=None, block_subset=None):


More information about the pypy-commit mailing list