1 /* 2 * Copyright (c) 2015, 2018, 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 24 25 26 package org.graalvm.compiler.core.amd64; 27 28 import static org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize.QWORD; 29 import static org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize.WORD; 30 31 import jdk.internal.vm.compiler.collections.EconomicMap; 32 import jdk.internal.vm.compiler.collections.Equivalence; 33 import org.graalvm.compiler.core.common.LIRKind; 34 import org.graalvm.compiler.lir.VirtualStackSlot; 35 import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction; 36 import org.graalvm.compiler.lir.amd64.AMD64Move.AMD64PushPopStackMove; 37 import org.graalvm.compiler.lir.framemap.FrameMapBuilder; 38 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory; 39 40 import jdk.vm.ci.amd64.AMD64Kind; 41 import jdk.vm.ci.code.Architecture; 42 import jdk.vm.ci.code.Register; 43 import jdk.vm.ci.code.RegisterArray; 44 import jdk.vm.ci.code.RegisterConfig; 45 import jdk.vm.ci.meta.AllocatableValue; 46 import jdk.vm.ci.meta.PlatformKind; 47 48 public abstract class AMD64MoveFactoryBase implements MoveFactory { 49 50 private final BackupSlotProvider backupSlotProvider; 51 52 private static class RegisterBackupPair { 53 public final Register register; 54 public final VirtualStackSlot backupSlot; 55 56 RegisterBackupPair(Register register, VirtualStackSlot backupSlot) { 57 this.register = register; 58 this.backupSlot = backupSlot; 59 } 60 } 61 62 public static final class BackupSlotProvider { 63 64 private final FrameMapBuilder frameMapBuilder; 65 private EconomicMap<PlatformKind.Key, RegisterBackupPair> categorized; 66 67 public BackupSlotProvider(FrameMapBuilder frameMapBuilder) { 68 this.frameMapBuilder = frameMapBuilder; 69 } 70 71 protected RegisterBackupPair getScratchRegister(PlatformKind kind) { 72 PlatformKind.Key key = kind.getKey(); 73 if (categorized == null) { 74 categorized = EconomicMap.create(Equivalence.DEFAULT); 75 } else if (categorized.containsKey(key)) { 76 return categorized.get(key); 77 } 78 79 RegisterConfig registerConfig = frameMapBuilder.getRegisterConfig(); 80 81 RegisterArray availableRegister = registerConfig.filterAllocatableRegisters(kind, registerConfig.getAllocatableRegisters()); 82 assert availableRegister != null && availableRegister.size() > 1; 83 Register scratchRegister = availableRegister.get(0); 84 85 Architecture arch = frameMapBuilder.getCodeCache().getTarget().arch; 86 LIRKind largestKind = LIRKind.value(arch.getLargestStorableKind(scratchRegister.getRegisterCategory())); 87 VirtualStackSlot backupSlot = frameMapBuilder.allocateSpillSlot(largestKind); 88 89 RegisterBackupPair value = new RegisterBackupPair(scratchRegister, backupSlot); 90 categorized.put(key, value); 91 92 return value; 93 } 94 } 95 96 public AMD64MoveFactoryBase(BackupSlotProvider backupSlotProvider) { 97 this.backupSlotProvider = backupSlotProvider; 98 } 99 100 @Override 101 public final AMD64LIRInstruction createStackMove(AllocatableValue result, AllocatableValue input) { 102 AMD64Kind kind = (AMD64Kind) result.getPlatformKind(); 103 switch (kind.getSizeInBytes()) { 104 case 2: 105 return new AMD64PushPopStackMove(WORD, result, input); 106 case 8: 107 return new AMD64PushPopStackMove(QWORD, result, input); 108 default: 109 RegisterBackupPair backup = backupSlotProvider.getScratchRegister(input.getPlatformKind()); 110 Register scratchRegister = backup.register; 111 VirtualStackSlot backupSlot = backup.backupSlot; 112 return createStackMove(result, input, scratchRegister, backupSlot); 113 } 114 } 115 116 public abstract AMD64LIRInstruction createStackMove(AllocatableValue result, AllocatableValue input, Register scratchRegister, AllocatableValue backupSlot); 117 }