Author: Maciej Fijalkowski <[email protected]>
Branch: result-in-resops
Changeset: r57403:5f5e61eb89dc
Date: 2012-09-20 14:50 +0200
http://bitbucket.org/pypy/pypy/changeset/5f5e61eb89dc/

Log:    equality

diff --git a/pypy/jit/metainterp/history.py b/pypy/jit/metainterp/history.py
--- a/pypy/jit/metainterp/history.py
+++ b/pypy/jit/metainterp/history.py
@@ -1,17 +1,13 @@
 
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem import lltype, llmemory
-from pypy.rpython.ootypesystem import ootype
-from pypy.rlib.objectmodel import we_are_translated, Symbolic
+from pypy.rlib.objectmodel import we_are_translated
 from pypy.rlib.objectmodel import compute_unique_id
-from pypy.rlib.rarithmetic import r_int64, is_valid_int
+from pypy.rlib.rarithmetic import r_int64
+from pypy.jit.metainterp.resoperation import rop, AbstractValue, ConstPtr
 
 from pypy.conftest import option
 
-from pypy.jit.metainterp.resoperation import rop, AbstractValue, INT, REF,\
-     FLOAT, repr_pointer, repr_object, ConstPtr, ConstFloat
-
-from pypy.jit.codewriter import heaptracker, longlong
 import weakref
 
 # ____________________________________________________________
diff --git a/pypy/jit/metainterp/resoperation.py 
b/pypy/jit/metainterp/resoperation.py
--- a/pypy/jit/metainterp/resoperation.py
+++ b/pypy/jit/metainterp/resoperation.py
@@ -164,7 +164,7 @@
     def _get_str(self):
         raise NotImplementedError
 
-    def same_box(self, other):
+    def eq(self, other):
         return self is other
 
     def is_constant(self):
@@ -376,7 +376,7 @@
     def constbox(self):
         return self
 
-    def same_box(self, other):
+    def eq(self, other):
         return self.same_constant(other)
 
     def same_constant(self, other):
@@ -610,6 +610,22 @@
         self._hash = hash
         return hash
 
+    def eq(self, other):
+        if self is other:
+            return True
+        if self.__class__ != other.__class__:
+            # note that this checks for opnum already
+            return False
+        descr = self.getdescr()
+        if descr is not None:
+            if other.getdescr() is not descr:
+                return False
+        if not self.result_eq(other):
+            return False
+        if not self.args_eq(other):
+            return False
+        return True
+
     # methods implemented by the arity mixins
     # ---------------------------------------
 
@@ -760,6 +776,9 @@
     def get_result_hash(self):
         return 0
 
+    def result_eq(self, other):
+        return True
+
 class ResOpInt(object):
     _mixin_ = True
     type = INT
@@ -782,6 +801,10 @@
     def get_result_hash(self):
         return make_hashable_int(self.intval)
 
+    def result_eq(self, other):
+        assert isinstance(other, self.__class__)
+        return self.intval == other.intval
+
 class ResOpFloat(object):
     _mixin_ = True
     type = FLOAT
@@ -805,6 +828,10 @@
     def get_result_hash(self):
         return longlong.gethash(self.floatval)
 
+    def result_eq(self, other):
+        assert isinstance(other, self.__class__)
+        return self.floatval == other.floatval
+
 class ResOpPointer(object):
     _mixin_ = True
     type = REF
@@ -831,6 +858,10 @@
     def wrap_constant(pval):
         return ConstPtr(pval)
 
+    def result_eq(self, other):
+        assert isinstance(other, self.__class__)
+        return self.pval == other.pval
+
 # ===================
 # Top of the hierachy
 # ===================
@@ -867,7 +898,7 @@
         check_descr(descr)
 
     def get_descr_hash(self):
-        return self._descr._get_hash_()
+        return compute_identity_hash(self._descr)
 
 class GuardResOp(ResOpWithDescr):
 
@@ -926,6 +957,9 @@
     def get_arg_hash(self):
         return 0
 
+    def args_eq(self, other):
+        return True
+
 class UnaryOp(object):
     _mixin_ = True
     _arg0 = None
@@ -968,6 +1002,10 @@
     def get_arg_hash(self):
         return self._arg0._get_hash_()
 
+    def args_eq(self, other):
+        assert isinstance(other, self.__class__)
+        return self._arg0.eq(other._arg0)
+
 class BinaryOp(object):
     _mixin_ = True
     _arg0 = None
@@ -1019,6 +1057,10 @@
         return (intmask(self._arg0._get_hash_() << 16) +
                 self._arg1._get_hash_())
 
+    def args_eq(self, other):
+        assert isinstance(other, self.__class__)
+        return self._arg0.eq(other._arg0) and self._arg1.eq(other._arg1)
+
 class TernaryOp(object):
     _mixin_ = True
     _arg0 = None
@@ -1081,6 +1123,11 @@
                 intmask(self._arg1._get_hash_() << 16) +
                 self._arg2._get_hash_())
 
+    def args_eq(self, other):
+        assert isinstance(other, self.__class__)
+        return (self._arg0.eq(other._arg0) and self._arg1.eq(other._arg1) and
+                self._arg2.eq(other._arg2))
+
 class N_aryOp(object):
     _mixin_ = True
     _args = None
@@ -1137,6 +1184,12 @@
             hash += intmask(arg._get_hash_() << (i & 15))
         return hash
 
+    def args_eq(self, other):
+        for i, arg in enumerate(self._args):
+            if not arg.eq(other._args[i]):
+                return False
+        return True
+
 # ____________________________________________________________
 
 _oplist = [
diff --git a/pypy/jit/metainterp/test/test_resoperation.py 
b/pypy/jit/metainterp/test/test_resoperation.py
--- a/pypy/jit/metainterp/test/test_resoperation.py
+++ b/pypy/jit/metainterp/test/test_resoperation.py
@@ -191,10 +191,12 @@
     op.set_extra("failargs", 2)
     assert op.get_extra("failargs") == 2
 
-def test_hashes():
+def test_hashes_eq():
     arg1 = rop.create_resop_1(rop.rop.FLOAT_NEG, 12.5, rop.BoxFloat(3.5))
     op = rop.create_resop_2(rop.rop.FLOAT_ADD, 13.5, rop.ConstFloat(3.0),
                             arg1)
+    ope = rop.create_resop_2(rop.rop.FLOAT_ADD, 13.5, rop.ConstFloat(3.0),
+                             arg1)
     op1 = rop.create_resop_2(rop.rop.FLOAT_ADD, 13.5, rop.ConstFloat(3.0),
                             rop.ConstFloat(1.0))
     op2 = rop.create_resop_2(rop.rop.FLOAT_ADD, 13.5, rop.ConstFloat(2.0),
@@ -204,10 +206,17 @@
     assert op1._get_hash_() != op._get_hash_()
     assert op2._get_hash_() != op._get_hash_()
     assert op3._get_hash_() != op._get_hash_()
+    assert not op1.eq(op)
+    assert not op.eq(op1)
+    assert not op2.eq(op)
+    assert not op3.eq(op)
+    assert ope._get_hash_() == op._get_hash_()
+    assert ope.eq(op)
 
     op = rop.create_resop_0(rop.rop.FORCE_TOKEN, 13)
     op1 = rop.create_resop_0(rop.rop.FORCE_TOKEN, 15)
     assert op._get_hash_() != op1._get_hash_()
+    assert not op.eq(op1)
     S = lltype.GcStruct('S')
     s = lltype.malloc(S)
     nonnull_ref = lltype.cast_opaque_ptr(llmemory.GCREF, s)
@@ -215,9 +224,11 @@
     op = rop.create_resop_1(rop.rop.NEWSTR, nullref, rop.BoxInt(5))
     op1 = rop.create_resop_1(rop.rop.NEWSTR, nonnull_ref, rop.BoxInt(5))
     assert op._get_hash_() != op1._get_hash_()
+    assert not op.eq(op1)
     op = rop.create_resop_1(rop.rop.NEWSTR, nullref, rop.BoxInt(5))
     op1 = rop.create_resop_1(rop.rop.NEWSTR, nullref, rop.BoxInt(15))
     assert op._get_hash_() != op1._get_hash_()
+    assert not op.eq(op1)
 
     descr = FakeDescr()
     descr2 = FakeDescr()
@@ -239,3 +250,21 @@
     assert op2._get_hash_() != op._get_hash_()
     assert op3._get_hash_() != op._get_hash_()
     assert op4._get_hash_() != op._get_hash_()
+    assert not op.eq(op1)
+    assert not op.eq(op2)
+    assert not op.eq(op3)
+    assert not op.eq(op4)
+
+    # class StrangeDescr(AbstractDescr):
+    #     def _get_hash_(self):
+    #         return 13
+
+    # descr = StrangeDescr()
+    # op1 = rop.create_resop(rop.rop.CALL_i, 12, [rop.BoxInt(0),
+    #                                             rop.BoxFloat(2.0),
+    #                                            rop.BoxPtr(nullref)], descr)
+    # op2 = rop.create_resop(rop.rop.CALL_i, 12, [rop.BoxInt(0),
+    #                                             rop.BoxFloat(2.0),
+    #                                            rop.BoxPtr(nullref)], descr)
+    # assert op1._get_hash_() == op2._get_hash_()
+    # assert not op1.eq(op2)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to