Author: Spenser Bauman <saba...@gmail.com>
Branch: value-classes
Changeset: r87331:770b5b1dda6b
Date: 2016-09-22 14:40 -0400
http://bitbucket.org/pypy/pypy/changeset/770b5b1dda6b/

Log:    Clean up for virtualstate.py

diff --git a/rpython/jit/metainterp/optimizeopt/test/test_virtualstate.py 
b/rpython/jit/metainterp/optimizeopt/test/test_virtualstate.py
--- a/rpython/jit/metainterp/optimizeopt/test/test_virtualstate.py
+++ b/rpython/jit/metainterp/optimizeopt/test/test_virtualstate.py
@@ -29,6 +29,14 @@
         self.optearlyforce = None
         self.optimizer = Optimizer
 
+
+class FakeDescr(object):
+    def __init__(self, vtable):
+        self.vtable = vtable
+    def get_vtable(self):
+        return self.vtable
+FakeDescr.value = FakeDescr(42)
+
 class BaseTestGenerateGuards(BaseTest):
     def setup_class(self):
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.nodeaddr))
@@ -158,7 +166,8 @@
         info1.fieldstate = info2.fieldstate = []
         postest(info1, info2)
 
-        info1, info2 = VirtualStateInfo(ConstInt(42), []), 
VirtualStateInfo(ConstInt(42), [])
+        fakedescr = FakeDescr.value
+        info1, info2 = VirtualStateInfo(fakedescr, []), 
VirtualStateInfo(fakedescr, [])
         info1.fieldstate = info2.fieldstate = []
         postest(info1, info2)
 
@@ -222,10 +231,10 @@
             def get_index(self):
                 return 0
         fielddescr = FieldDescr()
-        fakedescr = object()
+        fakedescr = FakeDescr.value
         fldtst(VArrayStateInfo(fakedescr), VArrayStateInfo(fakedescr))
         fldtst(VStructStateInfo(fakedescr, [fielddescr]), 
VStructStateInfo(fakedescr, [fielddescr]))
-        fldtst(VirtualStateInfo(ConstInt(42), [fielddescr]), 
VirtualStateInfo(ConstInt(42), [fielddescr]))
+        fldtst(VirtualStateInfo(fakedescr, [fielddescr]), 
VirtualStateInfo(fakedescr, [fielddescr]))
         fldtst(VArrayStructStateInfo(fakedescr, [fielddescr], 1), 
VArrayStructStateInfo(fakedescr, [fielddescr], 1))
 
     def test_known_class_generalization(self):
@@ -249,7 +258,7 @@
 
     def test_circular_generalization(self):
         for info in (VArrayStateInfo(42), VStructStateInfo(42, [7]),
-                     VirtualStateInfo(ConstInt(42), [7])):
+                     VirtualStateInfo(FakeDescr.value, [7])):
             info.position = 0
             info.fieldstate = [info]
             self.check_no_guards(info, info)
@@ -568,10 +577,10 @@
         innerinfo2 = not_virtual(self.cpu, 'r', None)
         innerinfo2.position = 1
 
-        info1 = VirtualStateInfo(ConstInt(42), [self.nextdescr])
+        info1 = VirtualStateInfo(FakeDescr.value, [self.nextdescr])
         info1.fieldstate = [innerinfo1]
 
-        info2 = VirtualStateInfo(ConstInt(42), [self.nextdescr])
+        info2 = VirtualStateInfo(FakeDescr.value, [self.nextdescr])
         info2.fieldstate = [innerinfo2]
 
         value1 = info.InstancePtrInfo(self.nodesize, classbox, is_virtual=True)
@@ -660,12 +669,12 @@
     # the below tests don't really have anything to do with guard generation
 
     def test_virtuals_with_equal_fields(self):
-        info1 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info1 = VirtualStateInfo(FakeDescr.value, [1, 2])
         info1.fieldstate = [self.knownclass_info, self.knownclass_info]
         vstate1 = VirtualState([info1])
         assert vstate1.generalization_of(vstate1, FakeOptimizer(self.cpu))
 
-        info2 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info2 = VirtualStateInfo(FakeDescr.value, [1, 2])
         unknown_info1 = not_virtual(self.cpu, 'r',
                                             info.InstancePtrInfo())
         info2.fieldstate = [unknown_info1, unknown_info1]
@@ -674,7 +683,7 @@
         assert not vstate1.generalization_of(vstate2, FakeOptimizer(self.cpu))
         assert vstate2.generalization_of(vstate1, FakeOptimizer(self.cpu))
 
-        info3 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info3 = VirtualStateInfo(FakeDescr.value, [1, 2])
         unknown_info1 = not_virtual(self.cpu, 'r',
                                             info.InstancePtrInfo())
         unknown_info2 = not_virtual(self.cpu, 'r',
@@ -687,7 +696,7 @@
         assert not vstate1.generalization_of(vstate3, FakeOptimizer(self.cpu))
 
     def test_virtuals_with_nonmatching_fields(self):
-        info1 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info1 = VirtualStateInfo(FakeDescr.value, [1, 2])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.nodeaddr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
@@ -695,7 +704,7 @@
         vstate1 = VirtualState([info1])
         assert vstate1.generalization_of(vstate1, FakeOptimizer(self.cpu))
 
-        info2 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info2 = VirtualStateInfo(FakeDescr.value, [1, 2])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.node2addr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
@@ -707,7 +716,7 @@
         assert not vstate1.generalization_of(vstate2, FakeOptimizer(self.cpu))
 
     def test_virtuals_with_nonmatching_descrs(self):
-        info1 = VirtualStateInfo(ConstInt(42), [10, 20])
+        info1 = VirtualStateInfo(FakeDescr.value, [10, 20])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.nodeaddr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
@@ -715,7 +724,7 @@
         vstate1 = VirtualState([info1])
         assert vstate1.generalization_of(vstate1, FakeOptimizer(self.cpu))
 
-        info2 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info2 = VirtualStateInfo(FakeDescr.value, [1, 2])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.node2addr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
@@ -727,7 +736,7 @@
         assert not vstate1.generalization_of(vstate2, FakeOptimizer(self.cpu))
         
     def test_virtuals_with_nonmatching_classes(self):
-        info1 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info1 = VirtualStateInfo(FakeDescr.value, [1, 2])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.nodeaddr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
@@ -735,7 +744,7 @@
         vstate1 = VirtualState([info1])
         assert vstate1.generalization_of(vstate1, FakeOptimizer(self.cpu))
 
-        info2 = VirtualStateInfo(ConstInt(7), [1, 2])
+        info2 = VirtualStateInfo(FakeDescr(7), [1, 2])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.node2addr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
@@ -747,7 +756,7 @@
         assert not vstate1.generalization_of(vstate2, FakeOptimizer(self.cpu))
 
     def test_nonvirtual_is_not_virtual(self):
-        info1 = VirtualStateInfo(ConstInt(42), [1, 2])
+        info1 = VirtualStateInfo(FakeDescr.value, [1, 2])
         classbox = self.cpu.ts.cls_of_box(InputArgRef(self.nodeaddr))
         value = info.InstancePtrInfo(None, classbox)
         knownclass_info = not_virtual(self.cpu, 'r', value)
diff --git a/rpython/jit/metainterp/optimizeopt/virtualstate.py 
b/rpython/jit/metainterp/optimizeopt/virtualstate.py
--- a/rpython/jit/metainterp/optimizeopt/virtualstate.py
+++ b/rpython/jit/metainterp/optimizeopt/virtualstate.py
@@ -65,6 +65,7 @@
                                                                  descr))
 
 class AbstractVirtualStateInfo(object):
+    _attrs_ = ('position',)
     position = -1
 
     def generate_guards(self, other, op, runtime_op, state):
@@ -133,7 +134,10 @@
 
 
 class AbstractVirtualStructStateInfo(AbstractVirtualStateInfo):
-    def __init__(self, fielddescrs):
+    _attrs_ = ('typedescr', 'fielddescrs')
+
+    def __init__(self, typedescr, fielddescrs):
+        self.typedescr = typedescr
         self.fielddescrs = fielddescrs
 
     def _generate_guards(self, other, box, runtime_box, state):
@@ -173,9 +177,8 @@
                                                    fieldbox,
                                                    fieldbox_runtime, state)
 
-
     def _generalization_of_structpart(self, other):
-        raise NotImplementedError
+        return type(self) is type(other) and self.typedescr is other.typedescr
 
     def enum_forced_boxes(self, boxes, box, optimizer, force_boxes=False):
         box = optimizer.get_box_replacement(box)
@@ -202,30 +205,17 @@
 
 
 class VirtualStateInfo(AbstractVirtualStructStateInfo):
-    def is_virtual(self):
-        return True
+    _attrs_ = ('known_class',)
 
-    def __init__(self, known_class, fielddescrs):
-        AbstractVirtualStructStateInfo.__init__(self, fielddescrs)
-        self.known_class = known_class
-
-    def _generalization_of_structpart(self, other):
-        return (isinstance(other, VirtualStateInfo) and
-                self.known_class.same_constant(other.known_class))
-
+    def __init__(self, typedescr, fielddescrs):
+        AbstractVirtualStructStateInfo.__init__(self, typedescr, fielddescrs)
+        self.known_class = ConstInt(typedescr.get_vtable())
 
     def debug_header(self, indent):
         debug_print(indent + 'VirtualStateInfo(%d):' % self.position)
 
 
 class VStructStateInfo(AbstractVirtualStructStateInfo):
-    def __init__(self, typedescr, fielddescrs):
-        AbstractVirtualStructStateInfo.__init__(self, fielddescrs)
-        self.typedescr = typedescr
-
-    def _generalization_of_structpart(self, other):
-        return (isinstance(other, VStructStateInfo) and
-                self.typedescr is other.typedescr)
 
     def debug_header(self, indent):
         debug_print(indent + 'VStructStateInfo(%d):' % self.position)
@@ -372,12 +362,6 @@
             self.level = LEVEL_CONSTANT
             self.constbox = info.getconst()
 
-    def is_const(self):
-        return self.constbox is not None
-
-    def is_virtual(self):
-        return False
-
     def _generate_guards(self, other, box, runtime_box, state):
         # XXX This will always retrace instead of forcing anything which
         # might be what we want sometimes?
@@ -745,11 +729,12 @@
         return not_virtual(self.optimizer.cpu, box.type,
                            self.optimizer.getinfo(box))
 
-    def visit_virtual(self, descr, fielddescrs):
-        known_class = ConstInt(descr.get_vtable())
-        return VirtualStateInfo(known_class, fielddescrs)
+    def visit_virtual(self, typedescr, fielddescrs):
+        assert typedescr.is_object()
+        return VirtualStateInfo(typedescr, fielddescrs)
 
     def visit_vstruct(self, typedescr, fielddescrs):
+        assert not typedescr.is_object()
         return VStructStateInfo(typedescr, fielddescrs)
 
     def visit_varray(self, arraydescr, clear):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to