1 /*
   2  * Copyright (c) 2013, 2019, 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 package org.graalvm.compiler.lir.amd64;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.asStackSlot;
  28 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  29 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  30 
  31 import java.util.Arrays;
  32 
  33 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
  34 import org.graalvm.compiler.lir.LIRInstructionClass;
  35 import org.graalvm.compiler.lir.LIRValueUtil;
  36 import org.graalvm.compiler.lir.Opcode;
  37 import org.graalvm.compiler.lir.StandardOp;
  38 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  39 
  40 import jdk.vm.ci.amd64.AMD64Kind;
  41 import jdk.vm.ci.code.Register;
  42 import jdk.vm.ci.code.StackSlot;
  43 import jdk.vm.ci.meta.AllocatableValue;
  44 
  45 /**
  46  * Restores registers from stack slots.
  47  */
  48 @Opcode("RESTORE_REGISTER")
  49 public class AMD64RestoreRegistersOp extends AMD64LIRInstruction implements StandardOp.RestoreRegistersOp {
  50     public static final LIRInstructionClass<AMD64RestoreRegistersOp> TYPE = LIRInstructionClass.create(AMD64RestoreRegistersOp.class);
  51 
  52     /**
  53      * The slots from which the registers are restored.
  54      */
  55     @Use(STACK) protected final AllocatableValue[] slots;
  56 
  57     /**
  58      * The operation that saved the registers restored by this operation.
  59      */
  60     private final AMD64SaveRegistersOp save;
  61 
  62     public AMD64RestoreRegistersOp(AllocatableValue[] values, AMD64SaveRegistersOp save) {
  63         this(TYPE, values, save);
  64     }
  65 
  66     protected AMD64RestoreRegistersOp(LIRInstructionClass<? extends AMD64RestoreRegistersOp> c, AllocatableValue[] values, AMD64SaveRegistersOp save) {
  67         super(c);
  68         assert Arrays.asList(values).stream().allMatch(LIRValueUtil::isVirtualStackSlot);
  69         this.slots = values;
  70         this.save = save;
  71     }
  72 
  73     protected Register[] getSavedRegisters() {
  74         return save.getSavedRegisters();
  75     }
  76 
  77     protected void restoreRegister(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register result, StackSlot input) {
  78         AMD64Move.stack2reg((AMD64Kind) input.getPlatformKind(), crb, masm, result, input);
  79     }
  80 
  81     @Override
  82     public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
  83         Register[] savedRegisters = getSavedRegisters();
  84         for (int i = 0; i < savedRegisters.length; i++) {
  85             if (savedRegisters[i] != null) {
  86                 assert isStackSlot(slots[i]) : "not a StackSlot: " + slots[i];
  87                 restoreRegister(crb, masm, savedRegisters[i], asStackSlot(slots[i]));
  88             }
  89         }
  90     }
  91 }