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