1 /*
   2  * Copyright (c) 2012, 2014, 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.amd64;
  24 
  25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  26 import static jdk.vm.ci.amd64.AMD64.rbp;
  27 import static jdk.vm.ci.amd64.AMD64.rsp;
  28 import static jdk.vm.ci.code.ValueUtil.asRegister;
  29 
  30 import org.graalvm.compiler.asm.amd64.AMD64Address;
  31 import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
  32 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
  33 import org.graalvm.compiler.lir.LIRInstructionClass;
  34 import org.graalvm.compiler.lir.Opcode;
  35 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  36 
  37 import jdk.vm.ci.code.Register;
  38 import jdk.vm.ci.meta.AllocatableValue;
  39 
  40 /**
  41  * Sets up the arguments for an exception handler in the callers frame, removes the current frame
  42  * and jumps to the handler.
  43  */
  44 @Opcode("JUMP_TO_EXCEPTION_HANDLER_IN_CALLER")
  45 final class AMD64HotSpotJumpToExceptionHandlerInCallerOp extends AMD64HotSpotEpilogueBlockEndOp {
  46 
  47     public static final LIRInstructionClass<AMD64HotSpotJumpToExceptionHandlerInCallerOp> TYPE = LIRInstructionClass.create(AMD64HotSpotJumpToExceptionHandlerInCallerOp.class);
  48 
  49     @Use(REG) AllocatableValue handlerInCallerPc;
  50     @Use(REG) AllocatableValue exception;
  51     @Use(REG) AllocatableValue exceptionPc;
  52     private final Register thread;
  53     private final int isMethodHandleReturnOffset;
  54 
  55     AMD64HotSpotJumpToExceptionHandlerInCallerOp(AllocatableValue handlerInCallerPc, AllocatableValue exception, AllocatableValue exceptionPc, int isMethodHandleReturnOffset, Register thread) {
  56         super(TYPE);
  57         this.handlerInCallerPc = handlerInCallerPc;
  58         this.exception = exception;
  59         this.exceptionPc = exceptionPc;
  60         this.isMethodHandleReturnOffset = isMethodHandleReturnOffset;
  61         this.thread = thread;
  62     }
  63 
  64     @Override
  65     public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
  66         leaveFrameAndRestoreRbp(crb, masm);
  67 
  68         // Discard the return address, thus completing restoration of caller frame
  69         masm.incrementq(rsp, 8);
  70 
  71         if (System.getProperty("java.specification.version").compareTo("1.8") < 0) {
  72             // Restore rsp from rbp if the exception PC is a method handle call site.
  73             AMD64Address dst = new AMD64Address(thread, isMethodHandleReturnOffset);
  74             masm.cmpl(dst, 0);
  75             masm.cmovq(ConditionFlag.NotEqual, rsp, rbp);
  76         }
  77 
  78         masm.jmp(asRegister(handlerInCallerPc));
  79     }
  80 }