1 /*
   2  * Copyright (c) 2016, 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.aarch64;
  25 
  26 import org.graalvm.compiler.asm.aarch64.AArch64Address;
  27 import org.graalvm.compiler.asm.aarch64.AArch64Address.AddressingMode;
  28 import org.graalvm.compiler.core.common.LIRKind;
  29 import org.graalvm.compiler.debug.GraalError;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.lir.aarch64.AArch64AddressValue;
  32 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  33 import org.graalvm.compiler.nodeinfo.NodeInfo;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  36 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  37 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  38 
  39 import jdk.vm.ci.meta.AllocatableValue;
  40 import jdk.vm.ci.meta.Value;
  41 
  42 /**
  43  * Represents an address of the form... TODO.
  44  */
  45 @NodeInfo
  46 public class AArch64AddressNode extends AddressNode implements LIRLowerable {
  47 
  48     public static final NodeClass<AArch64AddressNode> TYPE = NodeClass.create(AArch64AddressNode.class);
  49 
  50     @OptionalInput private ValueNode base;
  51 
  52     @OptionalInput private ValueNode index;
  53     private AArch64Address.AddressingMode addressingMode;
  54 
  55     private int displacement;
  56 
  57     public AArch64AddressNode(ValueNode base) {
  58         this(base, null);
  59     }
  60 
  61     public AArch64AddressNode(ValueNode base, ValueNode index) {
  62         super(TYPE);
  63         this.base = base;
  64         this.index = index;
  65         this.addressingMode = AddressingMode.REGISTER_OFFSET;
  66     }
  67 
  68     @Override
  69     public void generate(NodeLIRBuilderTool gen) {
  70         LIRGeneratorTool tool = gen.getLIRGeneratorTool();
  71 
  72         AllocatableValue baseValue = base == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(base));
  73         AllocatableValue indexValue = index == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(index));
  74 
  75         AllocatableValue baseReference = LIRKind.derivedBaseFromValue(baseValue);
  76         AllocatableValue indexReference;
  77         if (addressingMode.equals(AddressingMode.IMMEDIATE_UNSCALED)) {
  78             indexReference = LIRKind.derivedBaseFromValue(indexValue);
  79             throw GraalError.unimplemented();
  80         } else {
  81             if (LIRKind.isValue(indexValue.getValueKind())) {
  82                 indexReference = null;
  83             } else {
  84                 indexReference = Value.ILLEGAL;
  85             }
  86         }
  87 
  88         LIRKind kind = LIRKind.combineDerived(tool.getLIRKind(stamp()), baseReference, indexReference);
  89         final boolean scaled = false;
  90         gen.setResult(this, new AArch64AddressValue(kind, baseValue, indexValue, displacement, scaled, addressingMode));
  91     }
  92 
  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     public ValueNode getIndex() {
 106         return index;
 107     }
 108 
 109     public void setIndex(ValueNode index) {
 110         // allow modification before inserting into the graph
 111         if (isAlive()) {
 112             updateUsages(this.index, index);
 113         }
 114         this.index = index;
 115     }
 116 
 117     public int getDisplacement() {
 118         return displacement;
 119     }
 120 
 121     public void setDisplacement(int displacement) {
 122         this.displacement = displacement;
 123     }
 124 }