AArch64HotSpotRegisterConfig.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2015, 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 */
23package jdk.vm.ci.hotspot.aarch64;
24
25import static jdk.vm.ci.aarch64.AArch64.lr;
26import static jdk.vm.ci.aarch64.AArch64.r0;
27import static jdk.vm.ci.aarch64.AArch64.r1;
28import static jdk.vm.ci.aarch64.AArch64.r2;
29import static jdk.vm.ci.aarch64.AArch64.r3;
30import static jdk.vm.ci.aarch64.AArch64.r4;
31import static jdk.vm.ci.aarch64.AArch64.r5;
32import static jdk.vm.ci.aarch64.AArch64.r6;
33import static jdk.vm.ci.aarch64.AArch64.r7;
34import static jdk.vm.ci.aarch64.AArch64.rscratch1;
35import static jdk.vm.ci.aarch64.AArch64.rscratch2;
36import static jdk.vm.ci.aarch64.AArch64.r12;
37import static jdk.vm.ci.aarch64.AArch64.r27;
38import static jdk.vm.ci.aarch64.AArch64.r28;
39import static jdk.vm.ci.aarch64.AArch64.r29;
40import static jdk.vm.ci.aarch64.AArch64.r31;
41import static jdk.vm.ci.aarch64.AArch64.sp;
42import static jdk.vm.ci.aarch64.AArch64.v0;
43import static jdk.vm.ci.aarch64.AArch64.v1;
44import static jdk.vm.ci.aarch64.AArch64.v2;
45import static jdk.vm.ci.aarch64.AArch64.v3;
46import static jdk.vm.ci.aarch64.AArch64.v4;
47import static jdk.vm.ci.aarch64.AArch64.v5;
48import static jdk.vm.ci.aarch64.AArch64.v6;
49import static jdk.vm.ci.aarch64.AArch64.v7;
50import static jdk.vm.ci.aarch64.AArch64.zr;
51
52import java.util.ArrayList;
53import java.util.HashSet;
54import java.util.List;
55import java.util.Set;
56
57import jdk.vm.ci.aarch64.AArch64;
58import jdk.vm.ci.code.Architecture;
59import jdk.vm.ci.code.CallingConvention;
60import jdk.vm.ci.code.CallingConvention.Type;
61import jdk.vm.ci.code.Register;
62import jdk.vm.ci.code.RegisterArray;
63import jdk.vm.ci.code.RegisterAttributes;
64import jdk.vm.ci.code.RegisterConfig;
65import jdk.vm.ci.code.StackSlot;
66import jdk.vm.ci.code.TargetDescription;
67import jdk.vm.ci.code.ValueKindFactory;
68import jdk.vm.ci.common.JVMCIError;
69import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
70import jdk.vm.ci.meta.AllocatableValue;
71import jdk.vm.ci.meta.JavaKind;
72import jdk.vm.ci.meta.JavaType;
73import jdk.vm.ci.meta.PlatformKind;
74import jdk.vm.ci.meta.Value;
75import jdk.vm.ci.meta.ValueKind;
76
77public class AArch64HotSpotRegisterConfig implements RegisterConfig {
78
79    private final TargetDescription target;
80
81    private final RegisterArray allocatable;
82
83    /**
84     * The caller saved registers always include all parameter registers.
85     */
86    private final RegisterArray callerSaved;
87
88    private final boolean allAllocatableAreCallerSaved;
89
90    private final RegisterAttributes[] attributesMap;
91
92    @Override
93    public RegisterArray getAllocatableRegisters() {
94        return allocatable;
95    }
96
97    @Override
98    public RegisterArray filterAllocatableRegisters(PlatformKind kind, RegisterArray registers) {
99        ArrayList<Register> list = new ArrayList<>();
100        for (Register reg : registers) {
101            if (target.arch.canStoreValue(reg.getRegisterCategory(), kind)) {
102                list.add(reg);
103            }
104        }
105
106        return new RegisterArray(list);
107    }
108
109    @Override
110    public RegisterAttributes[] getAttributesMap() {
111        return attributesMap.clone();
112    }
113
114    private final RegisterArray javaGeneralParameterRegisters = new RegisterArray(r1, r2, r3, r4, r5, r6, r7, r0);
115    private final RegisterArray nativeGeneralParameterRegisters = new RegisterArray(r0, r1, r2, r3, r4, r5, r6, r7);
116    private final RegisterArray simdParameterRegisters = new RegisterArray(v0, v1, v2, v3, v4, v5, v6, v7);
117
118    public static final Register inlineCacheRegister = rscratch2;
119
120    /**
121     * Vtable stubs expect the metaspace Method in r12.
122     */
123    public static final Register metaspaceMethodRegister = r12;
124
125    public static final Register heapBaseRegister = r27;
126    public static final Register threadRegister = r28;
127    public static final Register fp = r29;
128
129    private static final RegisterArray reservedRegisters
130        = new RegisterArray(rscratch1, rscratch2, threadRegister, fp, lr, r31, zr, sp);
131
132    private static RegisterArray initAllocatable(Architecture arch, boolean reserveForHeapBase) {
133        RegisterArray allRegisters = arch.getAvailableValueRegisters();
134        Register[] registers = new Register[allRegisters.size() - reservedRegisters.size() - (reserveForHeapBase ? 1 : 0)];
135        List<Register> reservedRegistersList = reservedRegisters.asList();
136
137        int idx = 0;
138        for (Register reg : allRegisters) {
139            if (reservedRegistersList.contains(reg)) {
140                // skip reserved registers
141                continue;
142            }
143            assert !(reg.equals(threadRegister) || reg.equals(fp) || reg.equals(lr) || reg.equals(r31) || reg.equals(zr) || reg.equals(sp));
144            if (reserveForHeapBase && reg.equals(heapBaseRegister)) {
145                // skip heap base register
146                continue;
147            }
148
149            registers[idx++] = reg;
150        }
151
152        assert idx == registers.length;
153        return new RegisterArray(registers);
154    }
155
156    public AArch64HotSpotRegisterConfig(TargetDescription target, boolean useCompressedOops) {
157        this(target, initAllocatable(target.arch, useCompressedOops));
158        assert callerSaved.size() >= allocatable.size();
159    }
160
161    public AArch64HotSpotRegisterConfig(TargetDescription target, RegisterArray allocatable) {
162        this.target = target;
163
164        this.allocatable = allocatable;
165        Set<Register> callerSaveSet = new HashSet<>();
166        allocatable.addTo(callerSaveSet);
167        simdParameterRegisters.addTo(callerSaveSet);
168        javaGeneralParameterRegisters.addTo(callerSaveSet);
169        nativeGeneralParameterRegisters.addTo(callerSaveSet);
170        callerSaved = new RegisterArray(callerSaveSet);
171
172        allAllocatableAreCallerSaved = true;
173        attributesMap = RegisterAttributes.createMap(this, AArch64.allRegisters);
174    }
175
176    @Override
177    public RegisterArray getCallerSaveRegisters() {
178        return callerSaved;
179    }
180
181    public RegisterArray getCalleeSaveRegisters() {
182        return null;
183    }
184
185    @Override
186    public boolean areAllAllocatableRegistersCallerSaved() {
187        return allAllocatableAreCallerSaved;
188    }
189
190    @Override
191    public CallingConvention getCallingConvention(Type type, JavaType returnType, JavaType[] parameterTypes, ValueKindFactory<?> valueKindFactory) {
192        HotSpotCallingConventionType hotspotType = (HotSpotCallingConventionType) type;
193        if (type == HotSpotCallingConventionType.NativeCall) {
194            return callingConvention(nativeGeneralParameterRegisters, returnType, parameterTypes, hotspotType, valueKindFactory);
195        }
196        // On x64, parameter locations are the same whether viewed
197        // from the caller or callee perspective
198        return callingConvention(javaGeneralParameterRegisters, returnType, parameterTypes, hotspotType, valueKindFactory);
199    }
200
201    @Override
202    public RegisterArray getCallingConventionRegisters(Type type, JavaKind kind) {
203        HotSpotCallingConventionType hotspotType = (HotSpotCallingConventionType) type;
204        switch (kind) {
205            case Boolean:
206            case Byte:
207            case Short:
208            case Char:
209            case Int:
210            case Long:
211            case Object:
212                return hotspotType == HotSpotCallingConventionType.NativeCall ? nativeGeneralParameterRegisters : javaGeneralParameterRegisters;
213            case Float:
214            case Double:
215                return simdParameterRegisters;
216            default:
217                throw JVMCIError.shouldNotReachHere();
218        }
219    }
220
221    private CallingConvention callingConvention(RegisterArray generalParameterRegisters, JavaType returnType, JavaType[] parameterTypes, HotSpotCallingConventionType type,
222                    ValueKindFactory<?> valueKindFactory) {
223        AllocatableValue[] locations = new AllocatableValue[parameterTypes.length];
224
225        int currentGeneral = 0;
226        int currentSIMD = 0;
227        int currentStackOffset = 0;
228
229        for (int i = 0; i < parameterTypes.length; i++) {
230            final JavaKind kind = parameterTypes[i].getJavaKind().getStackKind();
231
232            switch (kind) {
233                case Byte:
234                case Boolean:
235                case Short:
236                case Char:
237                case Int:
238                case Long:
239                case Object:
240                    if (currentGeneral < generalParameterRegisters.size()) {
241                        Register register = generalParameterRegisters.get(currentGeneral++);
242                        locations[i] = register.asValue(valueKindFactory.getValueKind(kind));
243                    }
244                    break;
245                case Float:
246                case Double:
247                    if (currentSIMD < simdParameterRegisters.size()) {
248                        Register register = simdParameterRegisters.get(currentSIMD++);
249                        locations[i] = register.asValue(valueKindFactory.getValueKind(kind));
250                    }
251                    break;
252                default:
253                    throw JVMCIError.shouldNotReachHere();
254            }
255
256            if (locations[i] == null) {
257                ValueKind<?> valueKind = valueKindFactory.getValueKind(kind);
258                locations[i] = StackSlot.get(valueKind, currentStackOffset, !type.out);
259                currentStackOffset += Math.max(valueKind.getPlatformKind().getSizeInBytes(), target.wordSize);
260            }
261        }
262
263        JavaKind returnKind = returnType == null ? JavaKind.Void : returnType.getJavaKind();
264        AllocatableValue returnLocation = returnKind == JavaKind.Void ? Value.ILLEGAL : getReturnRegister(returnKind).asValue(valueKindFactory.getValueKind(returnKind.getStackKind()));
265        return new CallingConvention(currentStackOffset, returnLocation, locations);
266    }
267
268    @Override
269    public Register getReturnRegister(JavaKind kind) {
270        switch (kind) {
271            case Boolean:
272            case Byte:
273            case Char:
274            case Short:
275            case Int:
276            case Long:
277            case Object:
278                return r0;
279            case Float:
280            case Double:
281                return v0;
282            case Void:
283            case Illegal:
284                return null;
285            default:
286                throw new UnsupportedOperationException("no return register for type " + kind);
287        }
288    }
289
290    @Override
291    public Register getFrameRegister() {
292        return sp;
293    }
294
295    @Override
296    public String toString() {
297        return String.format("Allocatable: " + getAllocatableRegisters() + "%n" + "CallerSave:  " + getCallerSaveRegisters() + "%n");
298    }
299}
300