1 /*
   2  * Copyright (c) 2013, 2015, 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 package org.graalvm.compiler.lir.sparc;
  24 
  25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  26 import static jdk.vm.ci.code.ValueUtil.asRegister;
  27 import static jdk.vm.ci.code.ValueUtil.isLegal;
  28 
  29 import java.util.EnumSet;
  30 
  31 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  32 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  33 import org.graalvm.compiler.lir.CompositeValue;
  34 import org.graalvm.compiler.lir.InstructionValueConsumer;
  35 import org.graalvm.compiler.lir.InstructionValueProcedure;
  36 import org.graalvm.compiler.lir.LIRInstruction;
  37 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  38 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  39 
  40 import jdk.vm.ci.meta.AllocatableValue;
  41 import jdk.vm.ci.meta.Value;
  42 import jdk.vm.ci.meta.ValueKind;
  43 
  44 public final class SPARCImmediateAddressValue extends SPARCAddressValue {
  45 
  46     @Component({REG}) protected AllocatableValue base;
  47     protected final int displacement;
  48 
  49     private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG);
  50 
  51     public SPARCImmediateAddressValue(ValueKind<?> kind, AllocatableValue base, int displacement) {
  52         super(kind);
  53         assert SPARCAssembler.isSimm13(displacement);
  54         this.base = base;
  55         this.displacement = displacement;
  56     }
  57 
  58     @Override
  59     public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
  60         AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
  61         if (!base.identityEquals(newBase)) {
  62             return new SPARCImmediateAddressValue(getValueKind(), newBase, displacement);
  63         }
  64         return this;
  65     }
  66 
  67     @Override
  68     protected void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
  69         proc.visitValue(inst, base, mode, flags);
  70     }
  71 
  72     @Override
  73     public SPARCAddress toAddress() {
  74         return new SPARCAddress(asRegister(base), displacement);
  75     }
  76 
  77     @Override
  78     public boolean isValidImplicitNullCheckFor(Value value, int implicitNullCheckLimit) {
  79         return value.equals(base) && displacement >= 0 && displacement < implicitNullCheckLimit;
  80     }
  81 
  82     @Override
  83     public String toString() {
  84         StringBuilder s = new StringBuilder("[");
  85         String sep = "";
  86         if (isLegal(base)) {
  87             s.append(base);
  88             sep = " + ";
  89         }
  90         if (displacement < 0) {
  91             s.append(" - ").append(-displacement);
  92         } else if (displacement > 0) {
  93             s.append(sep).append(displacement);
  94         }
  95         s.append("]");
  96         return s.toString();
  97     }
  98 
  99     @Override
 100     public boolean equals(Object obj) {
 101         if (obj instanceof SPARCImmediateAddressValue) {
 102             SPARCImmediateAddressValue addr = (SPARCImmediateAddressValue) obj;
 103             return getValueKind().equals(addr.getValueKind()) && displacement == addr.displacement && base.equals(addr.base);
 104         }
 105         return false;
 106     }
 107 
 108     @Override
 109     public int hashCode() {
 110         return base.hashCode() ^ (displacement << 4) ^ getValueKind().hashCode();
 111     }
 112 }