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