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