changeset 9631:3c9aeef2702c

Value: provide proper `hashCode()' and `equals()' implementations
author Bernhard Urban <bernhard.urban@jku.at>
date Fri, 10 May 2013 12:43:43 +0200
parents cd0c173593a8
children 97db51025787
files graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/Register.java graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/RegisterValue.java graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/StackSlot.java graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Value.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMonitorValue.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValue.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/Variable.java
diffstat 7 files changed, 257 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/Register.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/Register.java	Fri May 10 12:43:43 2013 +0200
@@ -93,6 +93,33 @@
         public String toString() {
             return name;
         }
+
+        @Override
+        public int hashCode() {
+            return 31 + ((name == null) ? 0 : name.hashCode());
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (obj == null) {
+                return false;
+            }
+            if (getClass() != obj.getClass()) {
+                return false;
+            }
+            RegisterCategory other = (RegisterCategory) obj;
+            if (name == null) {
+                if (other.name != null) {
+                    return false;
+                }
+            } else if (!name.equals(other.name)) {
+                return false;
+            }
+            return true;
+        }
     }
 
     /**
@@ -143,16 +170,6 @@
     }
 
     /**
-     * Gets a hash code for this register.
-     * 
-     * @return the value of {@link #number}
-     */
-    @Override
-    public int hashCode() {
-        return number;
-    }
-
-    /**
      * Gets the maximum register {@linkplain #number number} in a given set of registers.
      * 
      * @param registers the set of registers to process
@@ -200,4 +217,50 @@
         return 0;
     }
 
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + encoding;
+        result = prime * result + ((name == null) ? 0 : name.hashCode());
+        result = prime * result + number;
+        result = prime * result + ((registerCategory == null) ? 0 : registerCategory.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        Register other = (Register) obj;
+        if (encoding != other.encoding) {
+            return false;
+        }
+        if (name == null) {
+            if (other.name != null) {
+                return false;
+            }
+        } else if (!name.equals(other.name)) {
+            return false;
+        }
+        if (number != other.number) {
+            return false;
+        }
+        if (registerCategory == null) {
+            if (other.registerCategory != null) {
+                return false;
+            }
+        } else if (!registerCategory.equals(other.registerCategory)) {
+            return false;
+        }
+        return true;
+    }
+
 }
--- a/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/RegisterValue.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/RegisterValue.java	Fri May 10 12:43:43 2013 +0200
@@ -45,11 +45,6 @@
     }
 
     @Override
-    public int hashCode() {
-        return (getRegister().number << 4) ^ getPlatformKind().hashCode();
-    }
-
-    @Override
     public String toString() {
         return getRegister().name + getKindSuffix();
     }
@@ -60,4 +55,31 @@
     public Register getRegister() {
         return reg;
     }
+
+    @Override
+    public int hashCode() {
+        return 31 * super.hashCode() + ((reg == null) ? 0 : reg.hashCode());
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        RegisterValue other = (RegisterValue) obj;
+        if (reg == null) {
+            if (other.reg != null) {
+                return false;
+            }
+        } else if (!reg.equals(other.reg)) {
+            return false;
+        }
+        return true;
+    }
 }
--- a/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/StackSlot.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/StackSlot.java	Fri May 10 12:43:43 2013 +0200
@@ -84,23 +84,6 @@
     }
 
     @Override
-    public int hashCode() {
-        return getPlatformKind().hashCode() ^ (offset << 4) ^ (addFrameSize ? 15 : 0);
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (o == this) {
-            return true;
-        }
-        if (o instanceof StackSlot) {
-            StackSlot l = (StackSlot) o;
-            return l.getPlatformKind().equals(getPlatformKind()) && l.offset == offset && l.addFrameSize == addFrameSize;
-        }
-        return false;
-    }
-
-    @Override
     public String toString() {
         if (!addFrameSize) {
             return "out:" + offset + getKindSuffix();
@@ -132,4 +115,34 @@
         }
         return this;
     }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + (addFrameSize ? 1231 : 1237);
+        result = prime * result + offset;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        StackSlot other = (StackSlot) obj;
+        if (addFrameSize != other.addFrameSize) {
+            return false;
+        }
+        if (offset != other.offset) {
+            return false;
+        }
+        return true;
+    }
 }
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Value.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Value.java	Fri May 10 12:43:43 2013 +0200
@@ -78,4 +78,38 @@
     public final PlatformKind getPlatformKind() {
         return platformKind;
     }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((kind == null) ? 0 : kind.hashCode());
+        result = prime * result + ((platformKind == null) ? 0 : platformKind.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        Value other = (Value) obj;
+        if (kind != other.kind) {
+            return false;
+        }
+        if (platformKind == null) {
+            if (other.platformKind != null) {
+                return false;
+            }
+        } else if (!platformKind.equals(other.platformKind)) {
+            return false;
+        }
+        return true;
+    }
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMonitorValue.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMonitorValue.java	Fri May 10 12:43:43 2013 +0200
@@ -63,4 +63,46 @@
     public String toString() {
         return "monitor[" + owner + (slot != null ? ", " + slot : "") + (eliminated ? ", eliminated" : "") + "]";
     }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + (eliminated ? 1231 : 1237);
+        result = prime * result + ((owner == null) ? 0 : owner.hashCode());
+        result = prime * result + ((slot == null) ? 0 : slot.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        HotSpotMonitorValue other = (HotSpotMonitorValue) obj;
+        if (eliminated != other.eliminated) {
+            return false;
+        }
+        if (owner == null) {
+            if (other.owner != null) {
+                return false;
+            }
+        } else if (!owner.equals(other.owner)) {
+            return false;
+        }
+        if (slot == null) {
+            if (other.slot != null) {
+                return false;
+            }
+        } else if (!slot.equals(other.slot)) {
+            return false;
+        }
+        return true;
+    }
 }
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValue.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/CompositeValue.java	Fri May 10 12:43:43 2013 +0200
@@ -58,4 +58,34 @@
     public String toString() {
         return valueClass.toString(this);
     }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + ((valueClass == null) ? 0 : valueClass.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        CompositeValue other = (CompositeValue) obj;
+        if (valueClass == null) {
+            if (other.valueClass != null) {
+                return false;
+            }
+        } else if (!valueClass.equals(other.valueClass)) {
+            return false;
+        }
+        return true;
+    }
 }
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/Variable.java	Thu May 09 21:08:26 2013 -0700
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/Variable.java	Fri May 10 12:43:43 2013 +0200
@@ -51,12 +51,30 @@
     }
 
     @Override
+    public String toString() {
+        return "v" + index + getKindSuffix();
+    }
+
+    @Override
     public int hashCode() {
-        return (index << 4) | getKind().ordinal();
+        return 31 * super.hashCode() + index;
     }
 
     @Override
-    public String toString() {
-        return "v" + index + getKindSuffix();
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        Variable other = (Variable) obj;
+        if (index != other.index) {
+            return false;
+        }
+        return true;
     }
 }