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