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