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 (scale.equals(Scale.Times1)) {
  77             indexReference = LIRKind.derivedBaseFromValue(indexValue);
  78         } else {
  79             if (LIRKind.isValue(indexValue)) {
  80                 indexReference = null;
  81             } else {
  82                 indexReference = Value.ILLEGAL;
  83             }
  84         }
  85 
  86         LIRKind kind = LIRKind.combineDerived(tool.getLIRKind(stamp()), baseReference, indexReference);
  87         gen.setResult(this, new AMD64AddressValue(kind, baseValue, indexValue, scale, displacement));
  88     }
  89 
  90     public ValueNode getBase() {
  91         return base;
  92     }
  93 
  94     public void setBase(ValueNode base) {
  95         // allow modification before inserting into the graph
  96         if (isAlive()) {
  97             updateUsages(this.base, base);
  98         }
  99         this.base = base;
 100     }
 101 
 102     public ValueNode getIndex() {
 103         return index;
 104     }
 105 
 106     public void setIndex(ValueNode index) {
 107         // allow modification before inserting into the graph
 108         if (isAlive()) {
 109             updateUsages(this.index, index);
 110         }
 111         this.index = index;
 112     }
 113 
 114     public Scale getScale() {
 115         return scale;
 116     }
 117 
 118     public void setScale(Scale scale) {
 119         this.scale = scale;
 120     }
 121 
 122     public int getDisplacement() {
 123         return displacement;
 124     }
 125 
 126     public void setDisplacement(int displacement) {
 127         this.displacement = displacement;
 128     }
 129 }