1 /*
   2  * Copyright (c) 2013, 2018, 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.hotspot.aarch64;
  26 
  27 import static jdk.vm.ci.aarch64.AArch64.sp;
  28 import static jdk.vm.ci.code.ValueUtil.asRegister;
  29 import static jdk.vm.ci.hotspot.aarch64.AArch64HotSpotRegisterConfig.fp;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  31 
  32 import org.graalvm.compiler.asm.Label;
  33 import org.graalvm.compiler.asm.aarch64.AArch64Address;
  34 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  35 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler.ScratchRegister;
  36 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  37 import org.graalvm.compiler.lir.LIRInstructionClass;
  38 import org.graalvm.compiler.lir.Opcode;
  39 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  40 import org.graalvm.compiler.serviceprovider.GraalServices;
  41 
  42 import jdk.vm.ci.code.Register;
  43 import jdk.vm.ci.meta.AllocatableValue;
  44 
  45 /**
  46  * Sets up the arguments for an exception handler in the callers frame, removes the current frame
  47  * and jumps to the handler.
  48  */
  49 @Opcode("JUMP_TO_EXCEPTION_HANDLER_IN_CALLER")
  50 public class AArch64HotSpotJumpToExceptionHandlerInCallerOp extends AArch64HotSpotEpilogueOp {
  51 
  52     public static final LIRInstructionClass<AArch64HotSpotJumpToExceptionHandlerInCallerOp> TYPE = LIRInstructionClass.create(AArch64HotSpotJumpToExceptionHandlerInCallerOp.class);
  53 
  54     @Use(REG) private AllocatableValue handlerInCallerPc;
  55     @Use(REG) private AllocatableValue exception;
  56     @Use(REG) private AllocatableValue exceptionPc;
  57     private final Register thread;
  58     private final int isMethodHandleReturnOffset;
  59 
  60     public AArch64HotSpotJumpToExceptionHandlerInCallerOp(AllocatableValue handlerInCallerPc, AllocatableValue exception, AllocatableValue exceptionPc, int isMethodHandleReturnOffset,
  61                     Register thread, GraalHotSpotVMConfig config) {
  62         super(TYPE, config);
  63         this.handlerInCallerPc = handlerInCallerPc;
  64         this.exception = exception;
  65         this.exceptionPc = exceptionPc;
  66         this.isMethodHandleReturnOffset = isMethodHandleReturnOffset;
  67         this.thread = thread;
  68     }
  69 
  70     @Override
  71     public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
  72         leaveFrame(crb, masm, /* emitSafepoint */false, false);
  73 
  74         if (GraalServices.JAVA_SPECIFICATION_VERSION < 8) {
  75             // Restore sp from fp if the exception PC is a method handle call site.
  76             try (ScratchRegister sc = masm.getScratchRegister()) {
  77                 Register scratch = sc.getRegister();
  78                 final boolean allowOverwrite = false;
  79                 AArch64Address address = masm.makeAddress(thread, isMethodHandleReturnOffset, scratch, 4, allowOverwrite);
  80                 masm.ldr(32, scratch, address);
  81                 Label noRestore = new Label();
  82                 masm.cbz(32, scratch, noRestore);
  83                 masm.mov(64, sp, fp);
  84                 masm.bind(noRestore);
  85             }
  86         }
  87 
  88         masm.jmp(asRegister(handlerInCallerPc));
  89     }
  90 }