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.meta.AllocatableValue; 71 import jdk.vm.ci.meta.JavaKind; 72 import jdk.vm.ci.meta.JavaType; 73 import jdk.vm.ci.meta.PlatformKind; 74 import jdk.vm.ci.meta.Value; 75 import jdk.vm.ci.meta.ValueKind; 76 77 public class AArch64HotSpotRegisterConfig implements RegisterConfig { 78 79 private final TargetDescription target; 80 81 private final Register[] allocatable; 82 83 /** 84 * The caller saved registers always include all parameter registers. 85 */ 86 private final Register[] callerSaved; 87 88 private final boolean allAllocatableAreCallerSaved; 89 90 private final RegisterAttributes[] attributesMap; 91 92 @Override 93 public Register[] getAllocatableRegisters() { 94 return allocatable.clone(); 95 } 96 97 @Override 98 public Register[] filterAllocatableRegisters(PlatformKind kind, Register[] 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 Register[] ret = list.toArray(new Register[list.size()]); 107 return ret; 108 } 109 110 @Override 111 public RegisterAttributes[] getAttributesMap() { 112 return attributesMap.clone(); 113 } 114 115 private final Register[] javaGeneralParameterRegisters = {r1, r2, r3, r4, r5, r6, r7, r0}; 116 private final Register[] nativeGeneralParameterRegisters = {r0, r1, r2, r3, r4, r5, r6, r7}; 117 private final Register[] simdParameterRegisters = {v0, v1, v2, v3, v4, v5, v6, v7}; 118 119 public static final Register inlineCacheRegister = r9; 120 121 /** 122 * Vtable stubs expect the metaspace Method in r12. 123 */ 124 public static final Register metaspaceMethodRegister = r12; 125 126 public static final Register heapBaseRegister = r27; 127 public static final Register threadRegister = r28; 128 public static final Register fp = r29; 129 130 private static final Register[] reservedRegisters = {threadRegister, fp, lr, r31, zr, sp}; 131 132 private static Register[] initAllocatable(Architecture arch, boolean reserveForHeapBase) { 133 Register[] allRegisters = arch.getAvailableValueRegisters(); 134 Register[] registers = new Register[allRegisters.length - reservedRegisters.length - (reserveForHeapBase ? 1 : 0)]; 135 List<Register> reservedRegistersList = Arrays.asList(reservedRegisters); 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 registers; 154 } 155 156 public AArch64HotSpotRegisterConfig(TargetDescription target, boolean useCompressedOops) { 157 this(target, initAllocatable(target.arch, useCompressedOops)); 158 assert callerSaved.length >= allocatable.length; 159 } 160 161 public AArch64HotSpotRegisterConfig(TargetDescription target, Register[] allocatable) { 162 this.target = target; 163 164 this.allocatable = allocatable.clone(); 165 Set<Register> callerSaveSet = new HashSet<>(); 166 Collections.addAll(callerSaveSet, allocatable); 167 Collections.addAll(callerSaveSet, simdParameterRegisters); 168 Collections.addAll(callerSaveSet, javaGeneralParameterRegisters); 169 Collections.addAll(callerSaveSet, nativeGeneralParameterRegisters); 170 callerSaved = callerSaveSet.toArray(new Register[callerSaveSet.size()]); 171 172 allAllocatableAreCallerSaved = true; 173 attributesMap = RegisterAttributes.createMap(this, AArch64.allRegisters); 174 } 175 176 @Override 177 public Register[] getCallerSaveRegisters() { 178 return callerSaved; 179 } 180 181 public Register[] 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 Register[] 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(Register[] 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.length) { 241 Register register = generalParameterRegisters[currentGeneral++]; 242 locations[i] = register.asValue(valueKindFactory.getValueKind(kind)); 243 } 244 break; 245 case Float: 246 case Double: 247 if (currentSIMD < simdParameterRegisters.length) { 248 Register register = simdParameterRegisters[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: " + Arrays.toString(getAllocatableRegisters()) + "%n" + "CallerSave: " + Arrays.toString(getCallerSaveRegisters()) + "%n"); 298 } 299 }