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.lir.CompositeValue;
  33 import org.graalvm.compiler.lir.InstructionValueConsumer;
  34 import org.graalvm.compiler.lir.InstructionValueProcedure;
  35 import org.graalvm.compiler.lir.LIRInstruction;
  36 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  37 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  38 
  39 import jdk.vm.ci.meta.AllocatableValue;
  40 import jdk.vm.ci.meta.Value;
  41 import jdk.vm.ci.meta.ValueKind;
  42 
  43 public final class SPARCIndexedAddressValue extends SPARCAddressValue {
  44 
  45     @Component({REG}) protected AllocatableValue base;
  46     @Component({REG}) protected AllocatableValue index;
  47 
  48     private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG);
  49 
  50     public SPARCIndexedAddressValue(ValueKind<?> kind, AllocatableValue base, AllocatableValue index) {
  51         super(kind);
  52         this.base = base;
  53         this.index = index;
  54     }
  55 
  56     @Override
  57     public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
  58         AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
  59         AllocatableValue newIndex = (AllocatableValue) proc.doValue(inst, index, mode, flags);
  60         if (!base.identityEquals(newBase) || !index.identityEquals(newIndex)) {
  61             return new SPARCIndexedAddressValue(getValueKind(), newBase, newIndex);
  62         }
  63         return this;
  64     }
  65 
  66     @Override
  67     protected void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
  68         proc.visitValue(inst, base, mode, flags);
  69         proc.visitValue(inst, index, mode, flags);
  70     }
  71 
  72     @Override
  73     public SPARCAddress toAddress() {
  74         return new SPARCAddress(asRegister(base), asRegister(index));
  75     }
  76 
  77     @Override
  78     public boolean isValidImplicitNullCheckFor(Value value, int implicitNullCheckLimit) {
  79         return false;
  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 (isLegal(index)) {
  91             s.append(sep).append(index);
  92         }
  93         s.append("]");
  94         return s.toString();
  95     }
  96 
  97     @Override
  98     public boolean equals(Object obj) {
  99         if (obj instanceof SPARCIndexedAddressValue) {
 100             SPARCIndexedAddressValue addr = (SPARCIndexedAddressValue) obj;
 101             return getValueKind().equals(addr.getValueKind()) && base.equals(addr.base) && index.equals(addr.index);
 102         }
 103         return false;
 104     }
 105 
 106     @Override
 107     public int hashCode() {
 108         return base.hashCode() ^ index.hashCode() ^ getValueKind().hashCode();
 109     }
 110 }