001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.amd64;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.asm.amd64.*;
029import com.oracle.graal.hotspot.*;
030import com.oracle.graal.lir.*;
031import com.oracle.graal.lir.StandardOp.SaveRegistersOp;
032import com.oracle.graal.lir.amd64.*;
033import com.oracle.graal.lir.asm.*;
034import com.oracle.graal.lir.framemap.*;
035
036/**
037 * Emits code that leaves a stack frame which is tailored to call the C++ method
038 * {@link HotSpotBackend#UNPACK_FRAMES Deoptimization::unpack_frames}.
039 */
040@Opcode("LEAVE_UNPACK_FRAMES_STACK_FRAME")
041final class AMD64HotSpotLeaveUnpackFramesStackFrameOp extends AMD64LIRInstruction {
042    public static final LIRInstructionClass<AMD64HotSpotLeaveUnpackFramesStackFrameOp> TYPE = LIRInstructionClass.create(AMD64HotSpotLeaveUnpackFramesStackFrameOp.class);
043
044    private final Register threadRegister;
045    private final int threadLastJavaSpOffset;
046    private final int threadLastJavaPcOffset;
047    private final int threadLastJavaFpOffset;
048
049    private final SaveRegistersOp saveRegisterOp;
050
051    AMD64HotSpotLeaveUnpackFramesStackFrameOp(Register threadRegister, int threadLastJavaSpOffset, int threadLastJavaPcOffset, int threadLastJavaFpOffset, SaveRegistersOp saveRegisterOp) {
052        super(TYPE);
053        this.threadRegister = threadRegister;
054        this.threadLastJavaSpOffset = threadLastJavaSpOffset;
055        this.threadLastJavaPcOffset = threadLastJavaPcOffset;
056        this.threadLastJavaFpOffset = threadLastJavaFpOffset;
057        this.saveRegisterOp = saveRegisterOp;
058    }
059
060    @Override
061    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
062        FrameMap frameMap = crb.frameMap;
063        RegisterConfig registerConfig = frameMap.getRegisterConfig();
064        RegisterSaveLayout registerSaveLayout = saveRegisterOp.getMap(frameMap);
065        Register stackPointerRegister = registerConfig.getFrameRegister();
066
067        // Restore stack pointer.
068        masm.movq(stackPointerRegister, new AMD64Address(threadRegister, threadLastJavaSpOffset));
069
070        // Clear last Java frame values.
071        masm.movslq(new AMD64Address(threadRegister, threadLastJavaSpOffset), 0);
072        masm.movslq(new AMD64Address(threadRegister, threadLastJavaPcOffset), 0);
073        masm.movslq(new AMD64Address(threadRegister, threadLastJavaFpOffset), 0);
074
075        // Restore return values.
076        final int stackSlotSize = frameMap.getTarget().wordSize;
077        Register integerResultRegister = registerConfig.getReturnRegister(Kind.Long);
078        masm.movptr(integerResultRegister, new AMD64Address(stackPointerRegister, registerSaveLayout.registerToSlot(integerResultRegister) * stackSlotSize));
079
080        Register floatResultRegister = registerConfig.getReturnRegister(Kind.Double);
081        masm.movdbl(floatResultRegister, new AMD64Address(stackPointerRegister, registerSaveLayout.registerToSlot(floatResultRegister) * stackSlotSize));
082    }
083}