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.*;
027import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*;
028import static jdk.internal.jvmci.amd64.AMD64.*;
029import static jdk.internal.jvmci.code.ValueUtil.*;
030
031import com.oracle.graal.asm.amd64.*;
032import com.oracle.graal.hotspot.*;
033import com.oracle.graal.lir.*;
034import com.oracle.graal.lir.StandardOp.SaveRegistersOp;
035import com.oracle.graal.lir.amd64.*;
036import com.oracle.graal.lir.asm.*;
037import com.oracle.graal.lir.framemap.*;
038
039/**
040 * Emits code that enters a stack frame which is tailored to call the C++ method
041 * {@link HotSpotBackend#UNPACK_FRAMES Deoptimization::unpack_frames}.
042 */
043@Opcode("ENTER_UNPACK_FRAMES_STACK_FRAME")
044final class AMD64HotSpotEnterUnpackFramesStackFrameOp extends AMD64LIRInstruction {
045    public static final LIRInstructionClass<AMD64HotSpotEnterUnpackFramesStackFrameOp> TYPE = LIRInstructionClass.create(AMD64HotSpotEnterUnpackFramesStackFrameOp.class);
046
047    private final Register threadRegister;
048    private final int threadLastJavaSpOffset;
049    private final int threadLastJavaPcOffset;
050    private final int threadLastJavaFpOffset;
051    @Alive(REG) AllocatableValue framePc;
052    @Alive(REG) AllocatableValue senderSp;
053    @Alive(REG) AllocatableValue senderFp;
054
055    private final SaveRegistersOp saveRegisterOp;
056
057    AMD64HotSpotEnterUnpackFramesStackFrameOp(Register threadRegister, int threadLastJavaSpOffset, int threadLastJavaPcOffset, int threadLastJavaFpOffset, AllocatableValue framePc,
058                    AllocatableValue senderSp, AllocatableValue senderFp, SaveRegistersOp saveRegisterOp) {
059        super(TYPE);
060        this.threadRegister = threadRegister;
061        this.threadLastJavaSpOffset = threadLastJavaSpOffset;
062        this.threadLastJavaPcOffset = threadLastJavaPcOffset;
063        this.threadLastJavaFpOffset = threadLastJavaFpOffset;
064        this.framePc = framePc;
065        this.senderSp = senderSp;
066        this.senderFp = senderFp;
067        this.saveRegisterOp = saveRegisterOp;
068    }
069
070    @Override
071    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
072        FrameMap frameMap = crb.frameMap;
073        RegisterConfig registerConfig = frameMap.getRegisterConfig();
074        RegisterSaveLayout registerSaveLayout = saveRegisterOp.getMap(frameMap);
075        Register stackPointerRegister = registerConfig.getFrameRegister();
076        final int totalFrameSize = frameMap.totalFrameSize();
077
078        // Push return address.
079        masm.push(asRegister(framePc));
080
081        // Push base pointer.
082        masm.push(asRegister(senderFp));
083        masm.movq(rbp, stackPointerRegister);
084
085        /*
086         * Allocate a full sized frame. Since return address and base pointer are already in place
087         * (see above) we allocate two words less.
088         */
089        masm.decrementq(stackPointerRegister, totalFrameSize - 2 * crb.target.wordSize);
090
091        // Save return registers after moving the frame.
092        final int stackSlotSize = frameMap.getTarget().wordSize;
093        Register integerResultRegister = registerConfig.getReturnRegister(Kind.Long);
094        masm.movptr(new AMD64Address(stackPointerRegister, registerSaveLayout.registerToSlot(integerResultRegister) * stackSlotSize), integerResultRegister);
095
096        Register floatResultRegister = registerConfig.getReturnRegister(Kind.Double);
097        masm.movdbl(new AMD64Address(stackPointerRegister, registerSaveLayout.registerToSlot(floatResultRegister) * stackSlotSize), floatResultRegister);
098
099        // Set up last Java values.
100        masm.movq(new AMD64Address(threadRegister, threadLastJavaSpOffset), stackPointerRegister);
101
102        /*
103         * Save the PC since it cannot easily be retrieved using the last Java SP after we aligned
104         * SP. Don't need the precise return PC here, just precise enough to point into this code
105         * blob.
106         */
107        masm.leaq(rax, new AMD64Address(rip, 0));
108        masm.movq(new AMD64Address(threadRegister, threadLastJavaPcOffset), rax);
109
110        // Use BP because the frames look interpreted now.
111        masm.movq(new AMD64Address(threadRegister, threadLastJavaFpOffset), rbp);
112
113        // Align the stack for the following unpackFrames call.
114        masm.andq(stackPointerRegister, -(crb.target.stackAlignment));
115    }
116}