1 /*
   2  * Copyright (c) 2015, 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 
  24 package org.graalvm.compiler.core.amd64;
  25 
  26 import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
  27 import org.graalvm.compiler.core.common.LIRKind;
  28 import org.graalvm.compiler.graph.NodeClass;
  29 import org.graalvm.compiler.lir.amd64.AMD64AddressValue;
  30 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  34 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  35 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  36 
  37 import jdk.vm.ci.meta.AllocatableValue;
  38 import jdk.vm.ci.meta.Value;
  39 
  40 /**
  41  * Represents an address of the form [base + index*scale + displacement]. Both base and index are
  42  * optional.
  43  */
  44 @NodeInfo
  45 public class AMD64AddressNode extends AddressNode implements LIRLowerable {
  46 
  47     public static final NodeClass<AMD64AddressNode> TYPE = NodeClass.create(AMD64AddressNode.class);
  48 
  49     @OptionalInput private ValueNode base;
  50 
  51     @OptionalInput private ValueNode index;
  52     private Scale scale;
  53 
  54     private int displacement;
  55 
  56     public AMD64AddressNode(ValueNode base) {
  57         this(base, null);
  58     }
  59 
  60     public AMD64AddressNode(ValueNode base, ValueNode index) {
  61         super(TYPE);
  62         this.base = base;
  63         this.index = index;
  64         this.scale = Scale.Times1;
  65     }
  66 
  67     @Override
  68     public void generate(NodeLIRBuilderTool gen) {
  69         LIRGeneratorTool tool = gen.getLIRGeneratorTool();
  70 
  71         AllocatableValue baseValue = base == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(base));
  72         AllocatableValue indexValue = index == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(index));
  73 
  74         AllocatableValue baseReference = LIRKind.derivedBaseFromValue(baseValue);
  75         AllocatableValue indexReference;
  76         if (index == null) {
  77             indexReference = null;
  78         } else if (scale.equals(Scale.Times1)) {
  79             indexReference = LIRKind.derivedBaseFromValue(indexValue);
  80         } else {
  81             if (LIRKind.isValue(indexValue)) {
  82                 indexReference = null;
  83             } else {
  84                 indexReference = Value.ILLEGAL;
  85             }
  86         }
  87 
  88         LIRKind kind = LIRKind.combineDerived(tool.getLIRKind(stamp()), baseReference, indexReference);
  89         gen.setResult(this, new AMD64AddressValue(kind, baseValue, indexValue, scale, displacement));
  90     }
  91 
  92     @Override
  93     public ValueNode getBase() {
  94         return base;
  95     }
  96 
  97     public void setBase(ValueNode base) {
  98         // allow modification before inserting into the graph
  99         if (isAlive()) {
 100             updateUsages(this.base, base);
 101         }
 102         this.base = base;
 103     }
 104 
 105     @Override
 106     public ValueNode getIndex() {
 107         return index;
 108     }
 109 
 110     public void setIndex(ValueNode index) {
 111         // allow modification before inserting into the graph
 112         if (isAlive()) {
 113             updateUsages(this.index, index);
 114         }
 115         this.index = index;
 116     }
 117 
 118     public Scale getScale() {
 119         return scale;
 120     }
 121 
 122     public void setScale(Scale scale) {
 123         this.scale = scale;
 124     }
 125 
 126     public int getDisplacement() {
 127         return displacement;
 128     }
 129 
 130     public void setDisplacement(int displacement) {
 131         this.displacement = displacement;
 132     }
 133 
 134     @Override
 135     public long getMaxConstantDisplacement() {
 136         return displacement;
 137     }
 138 }