michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jit_mips_BaselineHelpers_mips_h michael@0: #define jit_mips_BaselineHelpers_mips_h michael@0: michael@0: #ifdef JS_ION michael@0: #include "jit/BaselineFrame.h" michael@0: #include "jit/BaselineIC.h" michael@0: #include "jit/BaselineRegisters.h" michael@0: #include "jit/IonMacroAssembler.h" michael@0: michael@0: namespace js { michael@0: namespace jit { michael@0: michael@0: // Distance from sp to the top Value inside an IC stub (no return address on michael@0: // the stack on MIPS). michael@0: static const size_t ICStackValueOffset = 0; michael@0: michael@0: inline void michael@0: EmitRestoreTailCallReg(MacroAssembler &masm) michael@0: { michael@0: // No-op on MIPS because ra register is always holding the return address. michael@0: } michael@0: michael@0: inline void michael@0: EmitRepushTailCallReg(MacroAssembler &masm) michael@0: { michael@0: // No-op on MIPS because ra register is always holding the return address. michael@0: } michael@0: michael@0: inline void michael@0: EmitCallIC(CodeOffsetLabel *patchOffset, MacroAssembler &masm) michael@0: { michael@0: // Move ICEntry offset into BaselineStubReg. michael@0: CodeOffsetLabel offset = masm.movWithPatch(ImmWord(-1), BaselineStubReg); michael@0: *patchOffset = offset; michael@0: michael@0: // Load stub pointer into BaselineStubReg. michael@0: masm.loadPtr(Address(BaselineStubReg, ICEntry::offsetOfFirstStub()), BaselineStubReg); michael@0: michael@0: // Load stubcode pointer from BaselineStubEntry. michael@0: // R2 won't be active when we call ICs, so we can use it as scratch. michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), R2.scratchReg()); michael@0: michael@0: // Call the stubcode via a direct jump-and-link michael@0: masm.call(R2.scratchReg()); michael@0: } michael@0: michael@0: inline void michael@0: EmitEnterTypeMonitorIC(MacroAssembler &masm, michael@0: size_t monitorStubOffset = ICMonitoredStub::offsetOfFirstMonitorStub()) michael@0: { michael@0: // This is expected to be called from within an IC, when BaselineStubReg michael@0: // is properly initialized to point to the stub. michael@0: masm.loadPtr(Address(BaselineStubReg, (uint32_t) monitorStubOffset), BaselineStubReg); michael@0: michael@0: // Load stubcode pointer from BaselineStubEntry. michael@0: // R2 won't be active when we call ICs, so we can use it. michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), R2.scratchReg()); michael@0: michael@0: // Jump to the stubcode. michael@0: masm.branch(R2.scratchReg()); michael@0: } michael@0: michael@0: inline void michael@0: EmitReturnFromIC(MacroAssembler &masm) michael@0: { michael@0: masm.branch(ra); michael@0: } michael@0: michael@0: inline void michael@0: EmitChangeICReturnAddress(MacroAssembler &masm, Register reg) michael@0: { michael@0: masm.movePtr(reg, ra); michael@0: } michael@0: michael@0: inline void michael@0: EmitTailCallVM(JitCode *target, MacroAssembler &masm, uint32_t argSize) michael@0: { michael@0: // We assume during this that R0 and R1 have been pushed, and that R2 is michael@0: // unused. michael@0: MOZ_ASSERT(R2 == ValueOperand(t7, t6)); michael@0: michael@0: // Compute frame size. michael@0: masm.movePtr(BaselineFrameReg, t6); michael@0: masm.addPtr(Imm32(BaselineFrame::FramePointerOffset), t6); michael@0: masm.subPtr(BaselineStackReg, t6); michael@0: michael@0: // Store frame size without VMFunction arguments for GC marking. michael@0: masm.ma_subu(t7, t6, Imm32(argSize)); michael@0: masm.storePtr(t7, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); michael@0: michael@0: // Push frame descriptor and perform the tail call. michael@0: // BaselineTailCallReg (ra) already contains the return address (as we michael@0: // keep it there through the stub calls), but the VMWrapper code being michael@0: // called expects the return address to also be pushed on the stack. michael@0: MOZ_ASSERT(BaselineTailCallReg == ra); michael@0: masm.makeFrameDescriptor(t6, JitFrame_BaselineJS); michael@0: masm.subPtr(Imm32(sizeof(IonCommonFrameLayout)), StackPointer); michael@0: masm.storePtr(t6, Address(StackPointer, IonCommonFrameLayout::offsetOfDescriptor())); michael@0: masm.storePtr(ra, Address(StackPointer, IonCommonFrameLayout::offsetOfReturnAddress())); michael@0: michael@0: masm.branch(target); michael@0: } michael@0: michael@0: inline void michael@0: EmitCreateStubFrameDescriptor(MacroAssembler &masm, Register reg) michael@0: { michael@0: // Compute stub frame size. We have to add two pointers: the stub reg and michael@0: // previous frame pointer pushed by EmitEnterStubFrame. michael@0: masm.movePtr(BaselineFrameReg, reg); michael@0: masm.addPtr(Imm32(sizeof(intptr_t) * 2), reg); michael@0: masm.subPtr(BaselineStackReg, reg); michael@0: michael@0: masm.makeFrameDescriptor(reg, JitFrame_BaselineStub); michael@0: } michael@0: michael@0: inline void michael@0: EmitCallVM(JitCode *target, MacroAssembler &masm) michael@0: { michael@0: EmitCreateStubFrameDescriptor(masm, t6); michael@0: masm.push(t6); michael@0: masm.call(target); michael@0: } michael@0: michael@0: struct BaselineStubFrame { michael@0: uintptr_t savedFrame; michael@0: uintptr_t savedStub; michael@0: uintptr_t returnAddress; michael@0: uintptr_t descriptor; michael@0: }; michael@0: michael@0: static const uint32_t STUB_FRAME_SIZE = sizeof(BaselineStubFrame); michael@0: static const uint32_t STUB_FRAME_SAVED_STUB_OFFSET = offsetof(BaselineStubFrame, savedStub); michael@0: michael@0: inline void michael@0: EmitEnterStubFrame(MacroAssembler &masm, Register scratch) michael@0: { michael@0: MOZ_ASSERT(scratch != BaselineTailCallReg); michael@0: michael@0: // Compute frame size. michael@0: masm.movePtr(BaselineFrameReg, scratch); michael@0: masm.addPtr(Imm32(BaselineFrame::FramePointerOffset), scratch); michael@0: masm.subPtr(BaselineStackReg, scratch); michael@0: michael@0: masm.storePtr(scratch, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); michael@0: michael@0: // Note: when making changes here, don't forget to update michael@0: // BaselineStubFrame if needed. michael@0: michael@0: // Push frame descriptor and return address. michael@0: masm.makeFrameDescriptor(scratch, JitFrame_BaselineJS); michael@0: masm.subPtr(Imm32(STUB_FRAME_SIZE), StackPointer); michael@0: masm.storePtr(scratch, Address(StackPointer, offsetof(BaselineStubFrame, descriptor))); michael@0: masm.storePtr(BaselineTailCallReg, Address(StackPointer, michael@0: offsetof(BaselineStubFrame, returnAddress))); michael@0: michael@0: // Save old frame pointer, stack pointer and stub reg. michael@0: masm.storePtr(BaselineStubReg, Address(StackPointer, michael@0: offsetof(BaselineStubFrame, savedStub))); michael@0: masm.storePtr(BaselineFrameReg, Address(StackPointer, michael@0: offsetof(BaselineStubFrame, savedFrame))); michael@0: masm.movePtr(BaselineStackReg, BaselineFrameReg); michael@0: michael@0: // We pushed 4 words, so the stack is still aligned to 8 bytes. michael@0: masm.checkStackAlignment(); michael@0: } michael@0: michael@0: inline void michael@0: EmitLeaveStubFrame(MacroAssembler &masm, bool calledIntoIon = false) michael@0: { michael@0: // Ion frames do not save and restore the frame pointer. If we called michael@0: // into Ion, we have to restore the stack pointer from the frame descriptor. michael@0: // If we performed a VM call, the descriptor has been popped already so michael@0: // in that case we use the frame pointer. michael@0: if (calledIntoIon) { michael@0: masm.pop(ScratchRegister); michael@0: masm.rshiftPtr(Imm32(FRAMESIZE_SHIFT), ScratchRegister); michael@0: masm.addPtr(ScratchRegister, BaselineStackReg); michael@0: } else { michael@0: masm.movePtr(BaselineFrameReg, BaselineStackReg); michael@0: } michael@0: michael@0: masm.loadPtr(Address(StackPointer, offsetof(BaselineStubFrame, savedFrame)), michael@0: BaselineFrameReg); michael@0: masm.loadPtr(Address(StackPointer, offsetof(BaselineStubFrame, savedStub)), michael@0: BaselineStubReg); michael@0: michael@0: // Load the return address. michael@0: masm.loadPtr(Address(StackPointer, offsetof(BaselineStubFrame, returnAddress)), michael@0: BaselineTailCallReg); michael@0: michael@0: // Discard the frame descriptor. michael@0: masm.loadPtr(Address(StackPointer, offsetof(BaselineStubFrame, descriptor)), ScratchRegister); michael@0: masm.addPtr(Imm32(STUB_FRAME_SIZE), StackPointer); michael@0: } michael@0: michael@0: inline void michael@0: EmitStowICValues(MacroAssembler &masm, int values) michael@0: { michael@0: MOZ_ASSERT(values >= 0 && values <= 2); michael@0: switch(values) { michael@0: case 1: michael@0: // Stow R0 michael@0: masm.pushValue(R0); michael@0: break; michael@0: case 2: michael@0: // Stow R0 and R1 michael@0: masm.pushValue(R0); michael@0: masm.pushValue(R1); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: EmitUnstowICValues(MacroAssembler &masm, int values, bool discard = false) michael@0: { michael@0: MOZ_ASSERT(values >= 0 && values <= 2); michael@0: switch(values) { michael@0: case 1: michael@0: // Unstow R0. michael@0: if (discard) michael@0: masm.addPtr(Imm32(sizeof(Value)), BaselineStackReg); michael@0: else michael@0: masm.popValue(R0); michael@0: break; michael@0: case 2: michael@0: // Unstow R0 and R1. michael@0: if (discard) { michael@0: masm.addPtr(Imm32(sizeof(Value) * 2), BaselineStackReg); michael@0: } else { michael@0: masm.popValue(R1); michael@0: masm.popValue(R0); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: EmitCallTypeUpdateIC(MacroAssembler &masm, JitCode *code, uint32_t objectOffset) michael@0: { michael@0: // R0 contains the value that needs to be typechecked. michael@0: // The object we're updating is a boxed Value on the stack, at offset michael@0: // objectOffset from $sp, excluding the return address. michael@0: michael@0: // Save the current BaselineStubReg to stack, as well as the TailCallReg, michael@0: // since on mips, the $ra is live. michael@0: masm.subPtr(Imm32(2 * sizeof(intptr_t)), StackPointer); michael@0: masm.storePtr(BaselineStubReg, Address(StackPointer, sizeof(intptr_t))); michael@0: masm.storePtr(BaselineTailCallReg, Address(StackPointer, 0)); michael@0: michael@0: // This is expected to be called from within an IC, when BaselineStubReg michael@0: // is properly initialized to point to the stub. michael@0: masm.loadPtr(Address(BaselineStubReg, ICUpdatedStub::offsetOfFirstUpdateStub()), michael@0: BaselineStubReg); michael@0: michael@0: // Load stubcode pointer from BaselineStubReg into BaselineTailCallReg. michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), R2.scratchReg()); michael@0: michael@0: // Call the stubcode. michael@0: masm.call(R2.scratchReg()); michael@0: michael@0: // Restore the old stub reg and tailcall reg. michael@0: masm.loadPtr(Address(StackPointer, 0), BaselineTailCallReg); michael@0: masm.loadPtr(Address(StackPointer, sizeof(intptr_t)), BaselineStubReg); michael@0: masm.addPtr(Imm32(2 * sizeof(intptr_t)), StackPointer); michael@0: michael@0: // The update IC will store 0 or 1 in R1.scratchReg() reflecting if the michael@0: // value in R0 type-checked properly or not. michael@0: Label success; michael@0: masm.ma_b(R1.scratchReg(), Imm32(1), &success, Assembler::Equal, ShortJump); michael@0: michael@0: // If the IC failed, then call the update fallback function. michael@0: EmitEnterStubFrame(masm, R1.scratchReg()); michael@0: michael@0: masm.loadValue(Address(BaselineStackReg, STUB_FRAME_SIZE + objectOffset), R1); michael@0: michael@0: masm.pushValue(R0); michael@0: masm.pushValue(R1); michael@0: masm.push(BaselineStubReg); michael@0: michael@0: // Load previous frame pointer, push BaselineFrame *. michael@0: masm.loadPtr(Address(BaselineFrameReg, 0), R0.scratchReg()); michael@0: masm.pushBaselineFramePtr(R0.scratchReg(), R0.scratchReg()); michael@0: michael@0: EmitCallVM(code, masm); michael@0: EmitLeaveStubFrame(masm); michael@0: michael@0: // Success at end. michael@0: masm.bind(&success); michael@0: } michael@0: michael@0: template michael@0: inline void michael@0: EmitPreBarrier(MacroAssembler &masm, const AddrType &addr, MIRType type) michael@0: { michael@0: // On MIPS, $ra is clobbered by patchableCallPreBarrier. Save it first. michael@0: masm.push(ra); michael@0: masm.patchableCallPreBarrier(addr, type); michael@0: masm.pop(ra); michael@0: } michael@0: michael@0: inline void michael@0: EmitStubGuardFailure(MacroAssembler &masm) michael@0: { michael@0: // NOTE: This routine assumes that the stub guard code left the stack in michael@0: // the same state it was in when it was entered. michael@0: michael@0: // BaselineStubEntry points to the current stub. michael@0: michael@0: // Load next stub into BaselineStubReg michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfNext()), BaselineStubReg); michael@0: michael@0: // Load stubcode pointer from BaselineStubEntry into scratch register. michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), R2.scratchReg()); michael@0: michael@0: // Return address is already loaded, just jump to the next stubcode. michael@0: MOZ_ASSERT(BaselineTailCallReg == ra); michael@0: masm.branch(R2.scratchReg()); michael@0: } michael@0: michael@0: michael@0: } // namespace jit michael@0: } // namespace js michael@0: michael@0: #endif // JS_ION michael@0: michael@0: #endif /* jit_mips_BaselineHelpers_mips_h */ michael@0: