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