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