001/*
002 * Copyright (c) 2013, 2015, 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 com.oracle.graal.lir.sparc;
024
025import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*;
026import static jdk.internal.jvmci.code.ValueUtil.*;
027
028import java.util.*;
029
030import jdk.internal.jvmci.meta.*;
031
032import com.oracle.graal.asm.sparc.*;
033import com.oracle.graal.lir.*;
034import com.oracle.graal.lir.LIRInstruction.OperandFlag;
035import com.oracle.graal.lir.LIRInstruction.OperandMode;
036
037public final class SPARCImmediateAddressValue extends SPARCAddressValue {
038
039    @Component({REG}) protected AllocatableValue base;
040    protected final int displacement;
041
042    private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG);
043
044    public SPARCImmediateAddressValue(LIRKind kind, AllocatableValue base, int displacement) {
045        super(kind);
046        assert SPARCAssembler.isSimm13(displacement);
047        this.base = base;
048        this.displacement = displacement;
049    }
050
051    @Override
052    public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
053        AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
054        if (!base.identityEquals(newBase)) {
055            return new SPARCImmediateAddressValue(getLIRKind(), newBase, displacement);
056        }
057        return this;
058    }
059
060    @Override
061    protected void forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
062        proc.visitValue(inst, base, mode, flags);
063    }
064
065    @Override
066    public SPARCAddress toAddress() {
067        return new SPARCAddress(asRegister(base), displacement);
068    }
069
070    @Override
071    public boolean isValidImplicitNullCheckFor(Value value, int implicitNullCheckLimit) {
072        return value.equals(base) && displacement >= 0 && displacement < implicitNullCheckLimit;
073    }
074
075    @Override
076    public String toString() {
077        StringBuilder s = new StringBuilder("[");
078        String sep = "";
079        if (isLegal(base)) {
080            s.append(base);
081            sep = " + ";
082        }
083        if (displacement < 0) {
084            s.append(" - ").append(-displacement);
085        } else if (displacement > 0) {
086            s.append(sep).append(displacement);
087        }
088        s.append("]");
089        return s.toString();
090    }
091
092    @Override
093    public boolean equals(Object obj) {
094        if (obj instanceof SPARCImmediateAddressValue) {
095            SPARCImmediateAddressValue addr = (SPARCImmediateAddressValue) obj;
096            return getLIRKind().equals(addr.getLIRKind()) && displacement == addr.displacement && base.equals(addr.base);
097        }
098        return false;
099    }
100
101    @Override
102    public int hashCode() {
103        return base.hashCode() ^ (displacement << 4) ^ getLIRKind().hashCode();
104    }
105}