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 */
23package org.graalvm.compiler.hotspot.amd64;
24
25import static org.graalvm.compiler.hotspot.HotSpotBackend.UNWIND_EXCEPTION_TO_CALLER;
26import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
27import static jdk.vm.ci.amd64.AMD64.rsp;
28import static jdk.vm.ci.code.ValueUtil.asRegister;
29
30import org.graalvm.compiler.asm.amd64.AMD64Address;
31import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
32import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
33import org.graalvm.compiler.hotspot.stubs.UnwindExceptionToCallerStub;
34import org.graalvm.compiler.lir.LIRInstructionClass;
35import org.graalvm.compiler.lir.Opcode;
36import org.graalvm.compiler.lir.amd64.AMD64Call;
37import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
38
39import jdk.vm.ci.code.CallingConvention;
40import jdk.vm.ci.code.Register;
41import jdk.vm.ci.code.RegisterValue;
42
43/**
44 * Removes the current frame and jumps to the {@link UnwindExceptionToCallerStub}.
45 */
46@Opcode("UNWIND")
47final class AMD64HotSpotUnwindOp extends AMD64HotSpotEpilogueBlockEndOp {
48    public static final LIRInstructionClass<AMD64HotSpotUnwindOp> TYPE = LIRInstructionClass.create(AMD64HotSpotUnwindOp.class);
49
50    @Use({REG}) protected RegisterValue exception;
51
52    AMD64HotSpotUnwindOp(RegisterValue exception) {
53        super(TYPE);
54        this.exception = exception;
55    }
56
57    @Override
58    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
59        leaveFrameAndRestoreRbp(crb, masm);
60
61        ForeignCallLinkage linkage = crb.foreignCalls.lookupForeignCall(UNWIND_EXCEPTION_TO_CALLER);
62        CallingConvention cc = linkage.getOutgoingCallingConvention();
63        assert cc.getArgumentCount() == 2;
64        assert exception.equals(cc.getArgument(0));
65
66        // Get return address (is on top of stack after leave).
67        Register returnAddress = asRegister(cc.getArgument(1));
68        masm.movq(returnAddress, new AMD64Address(rsp, 0));
69
70        AMD64Call.directJmp(crb, masm, linkage);
71    }
72}
73