1 /* 2 * Copyright (c) 2013, 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.sparc; 24 25 import static jdk.vm.ci.meta.JavaKind.Void; 26 import static jdk.vm.ci.meta.Value.ILLEGAL; 27 import static jdk.vm.ci.sparc.SPARC.REGISTER_SAFE_AREA_SIZE; 28 import static jdk.vm.ci.sparc.SPARC.d0; 29 import static jdk.vm.ci.sparc.SPARC.d2; 30 import static jdk.vm.ci.sparc.SPARC.d4; 31 import static jdk.vm.ci.sparc.SPARC.d6; 32 import static jdk.vm.ci.sparc.SPARC.f0; 33 import static jdk.vm.ci.sparc.SPARC.f1; 34 import static jdk.vm.ci.sparc.SPARC.f2; 35 import static jdk.vm.ci.sparc.SPARC.f3; 36 import static jdk.vm.ci.sparc.SPARC.f4; 37 import static jdk.vm.ci.sparc.SPARC.f5; 38 import static jdk.vm.ci.sparc.SPARC.f6; 39 import static jdk.vm.ci.sparc.SPARC.f7; 40 import static jdk.vm.ci.sparc.SPARC.g0; 41 import static jdk.vm.ci.sparc.SPARC.g2; 42 import static jdk.vm.ci.sparc.SPARC.g6; 43 import static jdk.vm.ci.sparc.SPARC.i0; 44 import static jdk.vm.ci.sparc.SPARC.i1; 45 import static jdk.vm.ci.sparc.SPARC.i2; 46 import static jdk.vm.ci.sparc.SPARC.i3; 47 import static jdk.vm.ci.sparc.SPARC.i4; 48 import static jdk.vm.ci.sparc.SPARC.i5; 49 import static jdk.vm.ci.sparc.SPARC.i6; 50 import static jdk.vm.ci.sparc.SPARC.i7; 51 import static jdk.vm.ci.sparc.SPARC.l0; 52 import static jdk.vm.ci.sparc.SPARC.l1; 53 import static jdk.vm.ci.sparc.SPARC.l2; 54 import static jdk.vm.ci.sparc.SPARC.l3; 55 import static jdk.vm.ci.sparc.SPARC.l4; 56 import static jdk.vm.ci.sparc.SPARC.l5; 57 import static jdk.vm.ci.sparc.SPARC.l6; 58 import static jdk.vm.ci.sparc.SPARC.l7; 59 import static jdk.vm.ci.sparc.SPARC.o0; 60 import static jdk.vm.ci.sparc.SPARC.o1; 61 import static jdk.vm.ci.sparc.SPARC.o2; 62 import static jdk.vm.ci.sparc.SPARC.o3; 63 import static jdk.vm.ci.sparc.SPARC.o4; 64 import static jdk.vm.ci.sparc.SPARC.o5; 65 import static jdk.vm.ci.sparc.SPARC.sp; 66 67 import java.util.ArrayList; 68 import java.util.Arrays; 69 import java.util.Collections; 70 import java.util.HashSet; 71 import java.util.List; 72 73 import jdk.vm.ci.code.Architecture; 74 import jdk.vm.ci.code.CallingConvention; 75 import jdk.vm.ci.code.CallingConvention.Type; 76 import jdk.vm.ci.code.Register; 77 import jdk.vm.ci.code.RegisterAttributes; 78 import jdk.vm.ci.code.RegisterConfig; 79 import jdk.vm.ci.code.StackSlot; 80 import jdk.vm.ci.code.TargetDescription; 81 import jdk.vm.ci.code.ValueKindFactory; 82 import jdk.vm.ci.common.JVMCIError; 83 import jdk.vm.ci.hotspot.HotSpotCallingConventionType; 84 import jdk.vm.ci.hotspot.HotSpotVMConfig; 85 import jdk.vm.ci.meta.AllocatableValue; 86 import jdk.vm.ci.meta.JavaKind; 87 import jdk.vm.ci.meta.JavaType; 88 import jdk.vm.ci.meta.PlatformKind; 89 import jdk.vm.ci.meta.ValueKind; 90 import jdk.vm.ci.sparc.SPARC; 91 92 public class SPARCHotSpotRegisterConfig implements RegisterConfig { 93 94 private final TargetDescription target; 95 96 private final Register[] allocatable; 97 98 private final RegisterAttributes[] attributesMap; 99 100 /** 101 * Does native code (C++ code) spill arguments in registers to the parent frame? 102 */ 103 private final boolean addNativeRegisterArgumentSlots; 104 105 @Override 106 public Register[] getAllocatableRegisters() { 107 return allocatable.clone(); 108 } 109 110 @Override 111 public Register[] filterAllocatableRegisters(PlatformKind kind, Register[] registers) { 112 ArrayList<Register> list = new ArrayList<>(); 113 for (Register reg : registers) { 114 if (target.arch.canStoreValue(reg.getRegisterCategory(), kind)) { 115 list.add(reg); 116 } 117 } 118 Register[] ret = list.toArray(new Register[list.size()]); 119 return ret; 120 } 121 122 @Override 123 public RegisterAttributes[] getAttributesMap() { 124 return attributesMap.clone(); 125 } 126 127 private final Register[] cpuCallerParameterRegisters = {o0, o1, o2, o3, o4, o5}; 128 private final Register[] cpuCalleeParameterRegisters = {i0, i1, i2, i3, i4, i5}; 129 130 private final Register[] fpuFloatParameterRegisters = {f0, f1, f2, f3, f4, f5, f6, f7}; 131 private final Register[] fpuDoubleParameterRegisters = {d0, null, d2, null, d4, null, d6, null}; 132 133 // @formatter:off 134 private final Register[] callerSaveRegisters; 135 136 /** 137 * Registers saved by the callee. This lists all L and I registers which are saved in the 138 * register window. 139 */ 140 private final Register[] calleeSaveRegisters = { 141 l0, l1, l2, l3, l4, l5, l6, l7, 142 i0, i1, i2, i3, i4, i5, i6, i7}; 143 // @formatter:on 144 145 private static final Register[] reservedRegisters = {sp, g0, g2}; 146 147 private static Register[] initAllocatable(Architecture arch, boolean reserveForHeapBase) { 148 Register[] allRegisters = arch.getAvailableValueRegisters(); 149 Register[] registers = new Register[allRegisters.length - reservedRegisters.length - (reserveForHeapBase ? 1 : 0)]; 150 List<Register> reservedRegistersList = Arrays.asList(reservedRegisters); 151 152 int idx = 0; 153 for (Register reg : allRegisters) { 154 if (reservedRegistersList.contains(reg)) { 155 // skip reserved registers 156 continue; 157 } 158 if (reserveForHeapBase && reg.equals(g6)) { 159 // skip heap base register 160 continue; 161 } 162 163 registers[idx++] = reg; 164 } 165 166 assert idx == registers.length; 167 return registers; 168 } 169 170 public SPARCHotSpotRegisterConfig(TargetDescription target, HotSpotVMConfig config) { 171 this(target, initAllocatable(target.arch, config.useCompressedOops), config); 172 } 173 174 public SPARCHotSpotRegisterConfig(TargetDescription target, Register[] allocatable, HotSpotVMConfig config) { 175 this.target = target; 176 this.allocatable = allocatable.clone(); 177 this.addNativeRegisterArgumentSlots = config.linuxOs; 178 HashSet<Register> callerSaveSet = new HashSet<>(); 179 Collections.addAll(callerSaveSet, target.arch.getAvailableValueRegisters()); 180 for (Register cs : calleeSaveRegisters) { 181 callerSaveSet.remove(cs); 182 } 183 this.callerSaveRegisters = callerSaveSet.toArray(new Register[callerSaveSet.size()]); 184 attributesMap = RegisterAttributes.createMap(this, SPARC.allRegisters); 185 } 186 187 @Override 188 public Register[] getCallerSaveRegisters() { 189 return callerSaveRegisters; 190 } 191 192 public Register[] getCalleeSaveRegisters() { 193 return calleeSaveRegisters; 194 } 195 196 @Override 197 public boolean areAllAllocatableRegistersCallerSaved() { 198 return false; 199 } 200 201 @Override 202 public Register getRegisterForRole(int index) { 203 throw new UnsupportedOperationException(); 204 } 205 206 @Override 207 public CallingConvention getCallingConvention(Type type, JavaType returnType, JavaType[] parameterTypes, ValueKindFactory<?> valueKindFactory) { 208 HotSpotCallingConventionType hotspotType = (HotSpotCallingConventionType) type; 209 if (type == HotSpotCallingConventionType.JavaCall || type == HotSpotCallingConventionType.NativeCall) { 210 return callingConvention(cpuCallerParameterRegisters, returnType, parameterTypes, hotspotType, valueKindFactory); 211 } 212 if (type == HotSpotCallingConventionType.JavaCallee) { 213 return callingConvention(cpuCalleeParameterRegisters, returnType, parameterTypes, hotspotType, valueKindFactory); 214 } 215 throw JVMCIError.shouldNotReachHere(); 216 } 217 218 @Override 219 public Register[] getCallingConventionRegisters(Type type, JavaKind kind) { 220 HotSpotCallingConventionType hotspotType = (HotSpotCallingConventionType) type; 221 switch (kind) { 222 case Boolean: 223 case Byte: 224 case Short: 225 case Char: 226 case Int: 227 case Long: 228 case Object: 229 return hotspotType == HotSpotCallingConventionType.JavaCallee ? cpuCalleeParameterRegisters : cpuCallerParameterRegisters; 230 case Double: 231 case Float: 232 return fpuFloatParameterRegisters; 233 default: 234 throw JVMCIError.shouldNotReachHere("Unknown JavaKind " + kind); 235 } 236 } 237 238 private CallingConvention callingConvention(Register[] generalParameterRegisters, JavaType returnType, JavaType[] parameterTypes, HotSpotCallingConventionType type, 239 ValueKindFactory<?> valueKindFactory) { 240 AllocatableValue[] locations = new AllocatableValue[parameterTypes.length]; 241 242 int currentGeneral = 0; 243 int currentFloating = 0; 244 int currentStackOffset = 0; 245 246 for (int i = 0; i < parameterTypes.length; i++) { 247 final JavaKind kind = parameterTypes[i].getJavaKind().getStackKind(); 248 249 switch (kind) { 250 case Byte: 251 case Boolean: 252 case Short: 253 case Char: 254 case Int: 255 case Long: 256 case Object: 257 if (currentGeneral < generalParameterRegisters.length) { 258 Register register = generalParameterRegisters[currentGeneral++]; 259 locations[i] = register.asValue(valueKindFactory.getValueKind(kind)); 260 } 261 break; 262 case Double: 263 if (currentFloating < fpuFloatParameterRegisters.length) { 264 if (currentFloating % 2 != 0) { 265 // Make register number even to be a double reg 266 currentFloating++; 267 } 268 Register register = fpuDoubleParameterRegisters[currentFloating]; 269 currentFloating += 2; // Only every second is a double register 270 locations[i] = register.asValue(valueKindFactory.getValueKind(kind)); 271 } 272 break; 273 case Float: 274 if (currentFloating < fpuFloatParameterRegisters.length) { 275 Register register = fpuFloatParameterRegisters[currentFloating++]; 276 locations[i] = register.asValue(valueKindFactory.getValueKind(kind)); 277 } 278 break; 279 default: 280 throw JVMCIError.shouldNotReachHere(); 281 } 282 283 if (locations[i] == null) { 284 ValueKind<?> valueKind = valueKindFactory.getValueKind(kind); 285 // Stack slot is always aligned to its size in bytes but minimum wordsize 286 int typeSize = valueKind.getPlatformKind().getSizeInBytes(); 287 currentStackOffset = roundUp(currentStackOffset, typeSize); 288 int slotOffset = currentStackOffset + REGISTER_SAFE_AREA_SIZE; 289 locations[i] = StackSlot.get(valueKind, slotOffset, !type.out); 290 currentStackOffset += typeSize; 291 } 292 } 293 294 JavaKind returnKind = returnType == null ? Void : returnType.getJavaKind(); 295 AllocatableValue returnLocation = returnKind == Void ? ILLEGAL : getReturnRegister(returnKind, type).asValue(valueKindFactory.getValueKind(returnKind.getStackKind())); 296 297 int outArgSpillArea; 298 if (type == HotSpotCallingConventionType.NativeCall && addNativeRegisterArgumentSlots) { 299 // Space for native callee which may spill our outgoing arguments 300 outArgSpillArea = Math.min(locations.length, generalParameterRegisters.length) * target.wordSize; 301 } else { 302 outArgSpillArea = 0; 303 } 304 return new CallingConvention(currentStackOffset + outArgSpillArea, returnLocation, locations); 305 } 306 307 private static int roundUp(int number, int mod) { 308 return ((number + mod - 1) / mod) * mod; 309 } 310 311 @Override 312 public Register getReturnRegister(JavaKind kind) { 313 return getReturnRegister(kind, HotSpotCallingConventionType.JavaCallee); 314 } 315 316 private static Register getReturnRegister(JavaKind kind, HotSpotCallingConventionType type) { 317 switch (kind) { 318 case Boolean: 319 case Byte: 320 case Char: 321 case Short: 322 case Int: 323 case Long: 324 case Object: 325 return type == HotSpotCallingConventionType.JavaCallee ? i0 : o0; 326 case Float: 327 return f0; 328 case Double: 329 return d0; 330 case Void: 331 case Illegal: 332 return null; 333 default: 334 throw new UnsupportedOperationException("no return register for type " + kind); 335 } 336 } 337 338 @Override 339 public Register getFrameRegister() { 340 return sp; 341 } 342 343 @Override 344 public String toString() { 345 return String.format("Allocatable: " + Arrays.toString(getAllocatableRegisters()) + "%n" + "CallerSave: " + Arrays.toString(getCallerSaveRegisters()) + "%n"); 346 } 347 }