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