1 /*
   2  * Copyright (c) 2011, 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.amd64;
  24 
  25 import static jdk.vm.ci.amd64.AMD64.r12;
  26 import static jdk.vm.ci.amd64.AMD64.r15;
  27 import static jdk.vm.ci.amd64.AMD64.r8;
  28 import static jdk.vm.ci.amd64.AMD64.r9;
  29 import static jdk.vm.ci.amd64.AMD64.rax;
  30 import static jdk.vm.ci.amd64.AMD64.rcx;
  31 import static jdk.vm.ci.amd64.AMD64.rdi;
  32 import static jdk.vm.ci.amd64.AMD64.rdx;
  33 import static jdk.vm.ci.amd64.AMD64.rsi;
  34 import static jdk.vm.ci.amd64.AMD64.rsp;
  35 import static jdk.vm.ci.amd64.AMD64.xmm0;
  36 import static jdk.vm.ci.amd64.AMD64.xmm1;
  37 import static jdk.vm.ci.amd64.AMD64.xmm2;
  38 import static jdk.vm.ci.amd64.AMD64.xmm3;
  39 import static jdk.vm.ci.amd64.AMD64.xmm4;
  40 import static jdk.vm.ci.amd64.AMD64.xmm5;
  41 import static jdk.vm.ci.amd64.AMD64.xmm6;
  42 import static jdk.vm.ci.amd64.AMD64.xmm7;
  43 
  44 import java.util.ArrayList;
  45 import java.util.Arrays;
  46 import java.util.Collections;
  47 import java.util.HashSet;
  48 import java.util.List;
  49 import java.util.Set;
  50 
  51 import jdk.vm.ci.code.Architecture;
  52 import jdk.vm.ci.code.CallingConvention;
  53 import jdk.vm.ci.code.CallingConvention.Type;
  54 import jdk.vm.ci.code.Register;
  55 import jdk.vm.ci.code.RegisterAttributes;
  56 import jdk.vm.ci.code.RegisterConfig;
  57 import jdk.vm.ci.code.StackSlot;
  58 import jdk.vm.ci.code.TargetDescription;
  59 import jdk.vm.ci.code.ValueKindFactory;
  60 import jdk.vm.ci.common.JVMCIError;
  61 import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
  62 import jdk.vm.ci.hotspot.HotSpotVMConfig;
  63 import jdk.vm.ci.meta.AllocatableValue;
  64 import jdk.vm.ci.meta.JavaKind;
  65 import jdk.vm.ci.meta.JavaType;
  66 import jdk.vm.ci.meta.PlatformKind;
  67 import jdk.vm.ci.meta.Value;
  68 import jdk.vm.ci.meta.ValueKind;
  69 
  70 public class AMD64HotSpotRegisterConfig implements RegisterConfig {
  71 
  72     private final TargetDescription target;
  73 
  74     private final Register[] allocatable;
  75 
  76     private final int maxFrameSize;
  77 
  78     /**
  79      * The caller saved registers always include all parameter registers.
  80      */
  81     private final Register[] callerSaved;
  82 
  83     private final boolean allAllocatableAreCallerSaved;
  84 
  85     private final RegisterAttributes[] attributesMap;
  86 
  87     public int getMaximumFrameSize() {
  88         return maxFrameSize;
  89     }
  90 
  91     @Override
  92     public Register[] getAllocatableRegisters() {
  93         return allocatable.clone();
  94     }
  95 
  96     @Override
  97     public Register[] filterAllocatableRegisters(PlatformKind kind, Register[] registers) {
  98         ArrayList<Register> list = new ArrayList<>();
  99         for (Register reg : registers) {
 100             if (target.arch.canStoreValue(reg.getRegisterCategory(), kind)) {
 101                 list.add(reg);
 102             }
 103         }
 104 
 105         Register[] ret = list.toArray(new Register[list.size()]);
 106         return ret;
 107     }
 108 
 109     @Override
 110     public RegisterAttributes[] getAttributesMap() {
 111         return attributesMap.clone();
 112     }
 113 
 114     private final Register[] javaGeneralParameterRegisters;
 115     private final Register[] nativeGeneralParameterRegisters;
 116     private final Register[] xmmParameterRegisters = {xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7};
 117 
 118     /*
 119      * Some ABIs (e.g. Windows) require a so-called "home space", that is a save area on the stack
 120      * to store the argument registers
 121      */
 122     private final boolean needsNativeStackHomeSpace;
 123 
 124     private static final Register[] reservedRegisters = {rsp, r15};
 125 
 126     private static Register[] initAllocatable(Architecture arch, boolean reserveForHeapBase) {
 127         Register[] allRegisters = arch.getAvailableValueRegisters();
 128         Register[] registers = new Register[allRegisters.length - reservedRegisters.length - (reserveForHeapBase ? 1 : 0)];
 129         List<Register> reservedRegistersList = Arrays.asList(reservedRegisters);
 130 
 131         int idx = 0;
 132         for (Register reg : allRegisters) {
 133             if (reservedRegistersList.contains(reg)) {
 134                 // skip reserved registers
 135                 continue;
 136             }
 137             if (reserveForHeapBase && reg.equals(r12)) {
 138                 // skip heap base register
 139                 continue;
 140             }
 141 
 142             registers[idx++] = reg;
 143         }
 144 
 145         assert idx == registers.length;
 146         return registers;
 147     }
 148 
 149     public AMD64HotSpotRegisterConfig(TargetDescription target, HotSpotVMConfig config) {
 150         this(target, config, initAllocatable(target.arch, config.useCompressedOops));
 151         assert callerSaved.length >= allocatable.length;
 152     }
 153 
 154     public AMD64HotSpotRegisterConfig(TargetDescription target, HotSpotVMConfig config, Register[] allocatable) {
 155         this.target = target;
 156         this.maxFrameSize = config.maxFrameSize;
 157 
 158         if (config.windowsOs) {
 159             javaGeneralParameterRegisters = new Register[]{rdx, r8, r9, rdi, rsi, rcx};
 160             nativeGeneralParameterRegisters = new Register[]{rcx, rdx, r8, r9};
 161             this.needsNativeStackHomeSpace = true;
 162         } else {
 163             javaGeneralParameterRegisters = new Register[]{rsi, rdx, rcx, r8, r9, rdi};
 164             nativeGeneralParameterRegisters = new Register[]{rdi, rsi, rdx, rcx, r8, r9};
 165             this.needsNativeStackHomeSpace = false;
 166         }
 167 
 168         this.allocatable = allocatable;
 169         Set<Register> callerSaveSet = new HashSet<>();
 170         Collections.addAll(callerSaveSet, allocatable);
 171         Collections.addAll(callerSaveSet, xmmParameterRegisters);
 172         Collections.addAll(callerSaveSet, javaGeneralParameterRegisters);
 173         Collections.addAll(callerSaveSet, nativeGeneralParameterRegisters);
 174         callerSaved = callerSaveSet.toArray(new Register[callerSaveSet.size()]);
 175 
 176         allAllocatableAreCallerSaved = true;
 177         attributesMap = RegisterAttributes.createMap(this, target.arch.getRegisters());
 178     }
 179 
 180     @Override
 181     public Register[] getCallerSaveRegisters() {
 182         return callerSaved;
 183     }
 184 
 185     @Override
 186     public Register[] getCalleeSaveRegisters() {
 187         return null;
 188     }
 189 
 190     @Override
 191     public boolean areAllAllocatableRegistersCallerSaved() {
 192         return allAllocatableAreCallerSaved;
 193     }
 194 
 195     @Override
 196     public Register getRegisterForRole(int index) {
 197         throw new UnsupportedOperationException();
 198     }
 199 
 200     @Override
 201     public CallingConvention getCallingConvention(Type type, JavaType returnType, JavaType[] parameterTypes, ValueKindFactory<?> valueKindFactory) {
 202         HotSpotCallingConventionType hotspotType = (HotSpotCallingConventionType) type;
 203         if (type == HotSpotCallingConventionType.NativeCall) {
 204             return callingConvention(nativeGeneralParameterRegisters, returnType, parameterTypes, hotspotType, valueKindFactory);
 205         }
 206         // On x64, parameter locations are the same whether viewed
 207         // from the caller or callee perspective
 208         return callingConvention(javaGeneralParameterRegisters, returnType, parameterTypes, hotspotType, valueKindFactory);
 209     }
 210 
 211     @Override
 212     public Register[] getCallingConventionRegisters(Type type, JavaKind kind) {
 213         HotSpotCallingConventionType hotspotType = (HotSpotCallingConventionType) type;
 214         switch (kind) {
 215             case Boolean:
 216             case Byte:
 217             case Short:
 218             case Char:
 219             case Int:
 220             case Long:
 221             case Object:
 222                 return hotspotType == HotSpotCallingConventionType.NativeCall ? nativeGeneralParameterRegisters : javaGeneralParameterRegisters;
 223             case Float:
 224             case Double:
 225                 return xmmParameterRegisters;
 226             default:
 227                 throw JVMCIError.shouldNotReachHere();
 228         }
 229     }
 230 
 231     private CallingConvention callingConvention(Register[] generalParameterRegisters, JavaType returnType, JavaType[] parameterTypes, HotSpotCallingConventionType type,
 232                     ValueKindFactory<?> valueKindFactory) {
 233         AllocatableValue[] locations = new AllocatableValue[parameterTypes.length];
 234 
 235         int currentGeneral = 0;
 236         int currentXMM = 0;
 237         int currentStackOffset = type == HotSpotCallingConventionType.NativeCall && needsNativeStackHomeSpace ? generalParameterRegisters.length * target.wordSize : 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(valueKindFactory.getValueKind(kind));
 253                     }
 254                     break;
 255                 case Float:
 256                 case Double:
 257                     if (currentXMM < xmmParameterRegisters.length) {
 258                         Register register = xmmParameterRegisters[currentXMM++];
 259                         locations[i] = register.asValue(valueKindFactory.getValueKind(kind));
 260                     }
 261                     break;
 262                 default:
 263                     throw JVMCIError.shouldNotReachHere();
 264             }
 265 
 266             if (locations[i] == null) {
 267                 ValueKind<?> valueKind = valueKindFactory.getValueKind(kind);
 268                 locations[i] = StackSlot.get(valueKind, currentStackOffset, !type.out);
 269                 currentStackOffset += Math.max(valueKind.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(valueKindFactory.getValueKind(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 rax;
 289             case Float:
 290             case Double:
 291                 return xmm0;
 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 rsp;
 303     }
 304 
 305     @Override
 306     public String toString() {
 307         return String.format("Allocatable: " + Arrays.toString(getAllocatableRegisters()) + "%n" + "CallerSave:  " + Arrays.toString(getCallerSaveRegisters()) + "%n");
 308     }
 309 }