AArch64AddressValue.java revision 12651:6ef01bd40ce2
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 */
23package org.graalvm.compiler.lir.aarch64;
24
25import java.util.EnumSet;
26
27import org.graalvm.compiler.asm.aarch64.AArch64Address;
28import org.graalvm.compiler.asm.aarch64.AArch64Address.AddressingMode;
29import org.graalvm.compiler.asm.aarch64.AArch64Assembler;
30import org.graalvm.compiler.asm.aarch64.AArch64Assembler.ExtendType;
31import org.graalvm.compiler.lir.CompositeValue;
32import org.graalvm.compiler.lir.InstructionValueConsumer;
33import org.graalvm.compiler.lir.InstructionValueProcedure;
34import org.graalvm.compiler.lir.LIRInstruction;
35import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
36
37import jdk.vm.ci.aarch64.AArch64;
38import jdk.vm.ci.code.Register;
39import jdk.vm.ci.code.RegisterValue;
40import jdk.vm.ci.meta.AllocatableValue;
41import jdk.vm.ci.meta.Value;
42import jdk.vm.ci.meta.ValueKind;
43
44public final class AArch64AddressValue extends CompositeValue {
45    private static final EnumSet<OperandFlag> flags = EnumSet.of(OperandFlag.REG, OperandFlag.ILLEGAL);
46
47    @Component({OperandFlag.REG, OperandFlag.ILLEGAL}) protected AllocatableValue base;
48    @Component({OperandFlag.REG, OperandFlag.ILLEGAL}) protected AllocatableValue offset;
49    private final int immediate;
50
51    /**
52     * Whether register offset should be scaled or not.
53     */
54    private final boolean scaled;
55    private final AddressingMode addressingMode;
56
57    public AArch64AddressValue(ValueKind<?> kind, AllocatableValue base, AllocatableValue offset, int immediate, boolean scaled, AddressingMode addressingMode) {
58        super(kind);
59        this.base = base;
60        this.offset = offset;
61        this.immediate = immediate;
62        this.scaled = scaled;
63        this.addressingMode = addressingMode;
64    }
65
66    private static Register toRegister(AllocatableValue value) {
67        if (value.equals(Value.ILLEGAL)) {
68            return AArch64.zr;
69        } else {
70            return ((RegisterValue) value).getRegister();
71        }
72    }
73
74    public AllocatableValue getBase() {
75        return base;
76    }
77
78    public AllocatableValue getOffset() {
79        return offset;
80    }
81
82    public int getImmediate() {
83        return immediate;
84    }
85
86    public boolean isScaled() {
87        return scaled;
88    }
89
90    public AddressingMode getAddressingMode() {
91        return addressingMode;
92    }
93
94    public AArch64Address toAddress() {
95        Register baseReg = toRegister(base);
96        Register offsetReg = toRegister(offset);
97        AArch64Assembler.ExtendType extendType = addressingMode == AddressingMode.EXTENDED_REGISTER_OFFSET ? ExtendType.SXTW : null;
98        return AArch64Address.createAddress(addressingMode, baseReg, offsetReg, immediate, scaled, extendType);
99    }
100
101    @Override
102    public CompositeValue forEachComponent(LIRInstruction inst, LIRInstruction.OperandMode mode, InstructionValueProcedure proc) {
103        AllocatableValue newBase = (AllocatableValue) proc.doValue(inst, base, mode, flags);
104        AllocatableValue newOffset = (AllocatableValue) proc.doValue(inst, offset, mode, flags);
105        if (!base.identityEquals(newBase) || !offset.identityEquals(newOffset)) {
106            return new AArch64AddressValue(getValueKind(), newBase, newOffset, immediate, scaled, addressingMode);
107        }
108        return this;
109    }
110
111    @Override
112    protected void visitEachComponent(LIRInstruction inst, LIRInstruction.OperandMode mode, InstructionValueConsumer proc) {
113        proc.visitValue(inst, base, mode, flags);
114        proc.visitValue(inst, offset, mode, flags);
115    }
116}
117