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