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.amd64;
  24 
  25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  26 import static jdk.vm.ci.code.ValueUtil.isLegal;
  27 
  28 import java.util.EnumSet;
  29 
  30 import org.graalvm.compiler.asm.amd64.AMD64Address;
  31 import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
  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.code.Register;
  40 import jdk.vm.ci.code.RegisterValue;
  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 AMD64AddressValue extends CompositeValue {
  46 
  47     @Component({REG, OperandFlag.ILLEGAL}) protected AllocatableValue base;
  48     @Component({REG, OperandFlag.ILLEGAL}) protected AllocatableValue index;
  49     protected final Scale scale;
  50     protected final int displacement;
  51 
  52     private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG, OperandFlag.ILLEGAL);
  53 
  54     public AMD64AddressValue(ValueKind<?> kind, AllocatableValue base, int displacement) {
  55         this(kind, base, Value.ILLEGAL, Scale.Times1, displacement);
  56     }
  57 
  58     public AMD64AddressValue(ValueKind<?> kind, AllocatableValue base, AllocatableValue index, Scale scale, int displacement) {
  59         super(kind);
  60         this.base = base;
  61         this.index = index;
  62         this.scale = scale;
  63         this.displacement = displacement;
  64 
  65         assert scale != null;
  66     }
  67 
  68     @Override
  69     public CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc) {
  70         AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
  71         AllocatableValue newIndex = (AllocatableValue) proc.doValue(inst, index, mode, flags);
  72         if (!base.identityEquals(newBase) || !index.identityEquals(newIndex)) {
  73             return new AMD64AddressValue(getValueKind(), newBase, newIndex, scale, displacement);
  74         }
  75         return this;
  76     }
  77 
  78     @Override
  79     protected void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc) {
  80         proc.visitValue(inst, base, mode, flags);
  81         proc.visitValue(inst, index, mode, flags);
  82     }
  83 
  84     private static Register toRegister(AllocatableValue value) {
  85         if (value.equals(Value.ILLEGAL)) {
  86             return Register.None;
  87         } else {
  88             RegisterValue reg = (RegisterValue) value;
  89             return reg.getRegister();
  90         }
  91     }
  92 
  93     public AMD64Address toAddress() {
  94         return new AMD64Address(toRegister(base), toRegister(index), scale, displacement);
  95     }
  96 
  97     @Override
  98     public String toString() {
  99         StringBuilder s = new StringBuilder("[");
 100         String sep = "";
 101         if (isLegal(base)) {
 102             s.append(base);
 103             sep = " + ";
 104         }
 105         if (isLegal(index)) {
 106             s.append(sep).append(index).append(" * ").append(scale.value);
 107             sep = " + ";
 108         }
 109         if (displacement < 0) {
 110             s.append(" - ").append(-displacement);
 111         } else if (displacement > 0) {
 112             s.append(sep).append(displacement);
 113         }
 114         s.append("]");
 115         return s.toString();
 116     }
 117 
 118     public boolean isValidImplicitNullCheckFor(Value value, int implicitNullCheckLimit) {
 119         return value.equals(base) && index.equals(Value.ILLEGAL) && displacement >= 0 && displacement < implicitNullCheckLimit;
 120     }
 121 
 122     @Override
 123     public boolean equals(Object obj) {
 124         if (obj instanceof AMD64AddressValue) {
 125             AMD64AddressValue addr = (AMD64AddressValue) obj;
 126             return getValueKind().equals(addr.getValueKind()) && displacement == addr.displacement && base.equals(addr.base) && scale == addr.scale && index.equals(addr.index);
 127         }
 128         return false;
 129     }
 130 
 131     @Override
 132     public int hashCode() {
 133         return base.hashCode() ^ index.hashCode() ^ (displacement << 4) ^ (scale.value << 8) ^ getValueKind().hashCode();
 134     }
 135 }