1 /*
   2  * Copyright (c) 2013, 2019, 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 jdk.vm.ci.code.ValueUtil.asStackSlot;
  29 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  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.StandardOp;
  39 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  40 
  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 AArch64RestoreRegistersOp extends AArch64LIRInstruction implements StandardOp.RestoreRegistersOp {
  50     public static final LIRInstructionClass<AArch64RestoreRegistersOp> TYPE = LIRInstructionClass.create(AArch64RestoreRegistersOp.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 AArch64SaveRegistersOp save;
  61 
  62     public AArch64RestoreRegistersOp(AllocatableValue[] values, AArch64SaveRegistersOp save) {
  63         this(TYPE, values, save);
  64     }
  65 
  66     protected AArch64RestoreRegistersOp(LIRInstructionClass<? extends AArch64RestoreRegistersOp> c, AllocatableValue[] values, AArch64SaveRegistersOp 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, AArch64MacroAssembler masm, Register result, StackSlot input) {
  78         AArch64Move.stack2reg(crb, masm, result.asValue(), input);
  79     }
  80 
  81     @Override
  82     public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler 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 }