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