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