1/*
2 * Copyright (c) 2012, 2016, 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.lir.LIRInstruction.OperandFlag.ILLEGAL;
26import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
27
28import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
29import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
30import org.graalvm.compiler.lir.LIRInstructionClass;
31import org.graalvm.compiler.lir.Opcode;
32import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
33import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapStackArgumentSpaceBeforeInstruction;
34
35import jdk.vm.ci.amd64.AMD64.CPUFeature;
36import jdk.vm.ci.code.Register;
37import jdk.vm.ci.meta.Value;
38
39/**
40 * Returns from a function.
41 */
42@Opcode("RETURN")
43final class AMD64HotSpotReturnOp extends AMD64HotSpotEpilogueBlockEndOp implements ZapStackArgumentSpaceBeforeInstruction {
44
45    public static final LIRInstructionClass<AMD64HotSpotReturnOp> TYPE = LIRInstructionClass.create(AMD64HotSpotReturnOp.class);
46    @Use({REG, ILLEGAL}) protected Value value;
47    private final boolean isStub;
48    private final Register scratchForSafepointOnReturn;
49    private final GraalHotSpotVMConfig config;
50
51    AMD64HotSpotReturnOp(Value value, boolean isStub, Register scratchForSafepointOnReturn, GraalHotSpotVMConfig config) {
52        super(TYPE);
53        this.value = value;
54        this.isStub = isStub;
55        this.scratchForSafepointOnReturn = scratchForSafepointOnReturn;
56        this.config = config;
57    }
58
59    @Override
60    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
61        leaveFrameAndRestoreRbp(crb, masm);
62        if (!isStub) {
63            // Every non-stub compile method must have a poll before the return.
64            AMD64HotSpotSafepointOp.emitCode(crb, masm, config, true, null, scratchForSafepointOnReturn);
65
66            /*
67             * We potentially return to the interpreter, and that's an AVX-SSE transition. The only
68             * live value at this point should be the return value in either rax, or in xmm0 with
69             * the upper half of the register unused, so we don't destroy any value here.
70             */
71            if (masm.supports(CPUFeature.AVX)) {
72                masm.vzeroupper();
73            }
74        }
75        masm.ret(0);
76    }
77}
78