1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.lir.sparc;
  26 
  27 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  28 import static jdk.vm.ci.code.ValueUtil.asRegister;
  29 import static jdk.vm.ci.code.ValueUtil.isLegal;
  30 
  31 import java.util.EnumSet;
  32 
  33 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  34 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  35 import org.graalvm.compiler.lir.CompositeValue;
  36 import org.graalvm.compiler.lir.InstructionValueConsumer;
  37 import org.graalvm.compiler.lir.InstructionValueProcedure;
  38 import org.graalvm.compiler.lir.LIRInstruction;
  39 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  40 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  41 
  42 import jdk.vm.ci.meta.AllocatableValue;
  43 import jdk.vm.ci.meta.Value;
  44 import jdk.vm.ci.meta.ValueKind;
  45 
  46 public final class SPARCImmediateAddressValue extends SPARCAddressValue {
  47 
  48     @Component({REG}) protected AllocatableValue base;
  49     protected final int displacement;
  50 
  51     private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG);
  52 
  53     public SPARCImmediateAddressValue(ValueKind<?> kind, AllocatableValue base, int displacement) {
  54         super(kind);
  55         assert SPARCAssembler.isSimm13(displacement);
  56         this.base = base;
  57         this.displacement = displacement;
  58     }
  59 
  60     @Override
  61     public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
  62         AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
  63         if (!base.identityEquals(newBase)) {
  64             return new SPARCImmediateAddressValue(getValueKind(), newBase, displacement);
  65         }
  66         return this;
  67     }
  68 
  69     @Override
  70     protected void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
  71         proc.visitValue(inst, base, mode, flags);
  72     }
  73 
  74     @Override
  75     public SPARCAddress toAddress() {
  76         return new SPARCAddress(asRegister(base), displacement);
  77     }
  78 
  79     @Override
  80     public boolean isValidImplicitNullCheckFor(Value value, int implicitNullCheckLimit) {
  81         return value.equals(base) && displacement >= 0 && displacement < implicitNullCheckLimit;
  82     }
  83 
  84     @Override
  85     public String toString() {
  86         StringBuilder s = new StringBuilder("[");
  87         String sep = "";
  88         if (isLegal(base)) {
  89             s.append(base);
  90             sep = " + ";
  91         }
  92         if (displacement < 0) {
  93             s.append(" - ").append(-displacement);
  94         } else if (displacement > 0) {
  95             s.append(sep).append(displacement);
  96         }
  97         s.append("]");
  98         return s.toString();
  99     }
 100 
 101     @Override
 102     public boolean equals(Object obj) {
 103         if (obj instanceof SPARCImmediateAddressValue) {
 104             SPARCImmediateAddressValue addr = (SPARCImmediateAddressValue) obj;
 105             return getValueKind().equals(addr.getValueKind()) && displacement == addr.displacement && base.equals(addr.base);
 106         }
 107         return false;
 108     }
 109 
 110     @Override
 111     public int hashCode() {
 112         return base.hashCode() ^ (displacement << 4) ^ getValueKind().hashCode();
 113     }
 114 }