001/*
002 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025/**
026 * Abstract base class for values.
027 */
028public abstract class AbstractValue implements Value, KindProvider {
029
030    public static final AllocatableValue ILLEGAL = Value.ILLEGAL;
031
032    private final Kind kind;
033    private final LIRKind lirKind;
034
035    /**
036     * Initializes a new value of the specified kind.
037     *
038     * @param lirKind the kind
039     */
040    protected AbstractValue(LIRKind lirKind) {
041        this.lirKind = lirKind;
042        if (getPlatformKind() instanceof Kind) {
043            this.kind = (Kind) getPlatformKind();
044        } else {
045            this.kind = Kind.Illegal;
046        }
047    }
048
049    /**
050     * Returns a String representation of the kind, which should be the end of all
051     * {@link #toString()} implementation of subclasses.
052     */
053    protected final String getKindSuffix() {
054        return "|" + getKind().getTypeChar();
055    }
056
057    /**
058     * Returns the kind of this value.
059     */
060    public final Kind getKind() {
061        return kind;
062    }
063
064    public final LIRKind getLIRKind() {
065        return lirKind;
066    }
067
068    /**
069     * Returns the platform specific kind used to store this value.
070     */
071    public final PlatformKind getPlatformKind() {
072        return lirKind.getPlatformKind();
073    }
074
075    @Override
076    public int hashCode() {
077        return 41 + lirKind.hashCode();
078    }
079
080    @Override
081    public boolean equals(Object obj) {
082        if (obj instanceof AbstractValue) {
083            AbstractValue that = (AbstractValue) obj;
084            return kind.equals(that.kind) && lirKind.equals(that.lirKind);
085        }
086        return false;
087    }
088
089    /**
090     * Checks if this value is identical to {@code other}.
091     *
092     * Warning: Use with caution! Usually equivalence {@link #equals(Object)} is sufficient and
093     * should be used.
094     */
095    public final boolean identityEquals(AbstractValue other) {
096        return this == other;
097    }
098}