1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 package org.graalvm.compiler.lir.aarch64;
  27 
  28 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  29 import static jdk.vm.ci.code.ValueUtil.asStackSlot;
  30 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  31 
  32 import java.util.Arrays;
  33 
  34 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  35 import org.graalvm.compiler.lir.LIRInstructionClass;
  36 import org.graalvm.compiler.lir.LIRValueUtil;
  37 import org.graalvm.compiler.lir.Opcode;
  38 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  39 
  40 import jdk.vm.ci.code.Register;
  41 import jdk.vm.ci.code.StackSlot;
  42 import jdk.vm.ci.meta.AllocatableValue;
  43 
  44 /**
  45  * Restores registers from stack slots.
  46  */
  47 @Opcode("RESTORE_REGISTER")
  48 public class AArch64RestoreRegistersOp extends AArch64LIRInstruction {
  49     public static final LIRInstructionClass<AArch64RestoreRegistersOp> TYPE = LIRInstructionClass.create(AArch64RestoreRegistersOp.class);
  50 
  51     /**
  52      * The slots from which the registers are restored.
  53      */
  54     @Use(STACK) protected final AllocatableValue[] slots;
  55 
  56     /**
  57      * The operation that saved the registers restored by this operation.
  58      */
  59     private final AArch64SaveRegistersOp save;
  60 
  61     public AArch64RestoreRegistersOp(AllocatableValue[] values, AArch64SaveRegistersOp save) {
  62         this(TYPE, values, save);
  63     }
  64 
  65     protected AArch64RestoreRegistersOp(LIRInstructionClass<? extends AArch64RestoreRegistersOp> c, AllocatableValue[] values, AArch64SaveRegistersOp save) {
  66         super(c);
  67         assert Arrays.asList(values).stream().allMatch(LIRValueUtil::isVirtualStackSlot);
  68         this.slots = values;
  69         this.save = save;
  70     }
  71 
  72     protected Register[] getSavedRegisters() {
  73         return save.savedRegisters;
  74     }
  75 
  76     protected void restoreRegister(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register result, StackSlot input) {
  77         AArch64Move.stack2reg(crb, masm, result.asValue(), input);
  78     }
  79 
  80     @Override
  81     public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
  82         Register[] savedRegisters = getSavedRegisters();
  83         for (int i = 0; i < savedRegisters.length; i++) {
  84             if (savedRegisters[i] != null) {
  85                 assert isStackSlot(slots[i]) : "not a StackSlot: " + slots[i];
  86                 restoreRegister(crb, masm, savedRegisters[i], asStackSlot(slots[i]));
  87             }
  88         }
  89     }
  90 }