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