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_arm_BaselineHelpers_arm_h michael@0: #define jit_arm_BaselineHelpers_arm_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 the stack on ARM). 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 ARM because link 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 ARM because link 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 r0. michael@0: JS_ASSERT(R2 == ValueOperand(r1, r0)); michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), r0); michael@0: michael@0: // Call the stubcode via a direct branch-and-link michael@0: masm.ma_blx(r0); 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 r0. michael@0: JS_ASSERT(R2 == ValueOperand(r1, r0)); michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), r0); michael@0: michael@0: // Jump to the stubcode. michael@0: masm.branch(r0); michael@0: } michael@0: michael@0: inline void michael@0: EmitReturnFromIC(MacroAssembler &masm) michael@0: { michael@0: masm.ma_mov(lr, pc); michael@0: } michael@0: michael@0: inline void michael@0: EmitChangeICReturnAddress(MacroAssembler &masm, Register reg) michael@0: { michael@0: masm.ma_mov(reg, lr); 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: JS_ASSERT(R2 == ValueOperand(r1, r0)); michael@0: michael@0: // Compute frame size. michael@0: masm.movePtr(BaselineFrameReg, r0); michael@0: masm.ma_add(Imm32(BaselineFrame::FramePointerOffset), r0); michael@0: masm.ma_sub(BaselineStackReg, r0); michael@0: michael@0: // Store frame size without VMFunction arguments for GC marking. michael@0: masm.ma_sub(r0, Imm32(argSize), r1); michael@0: masm.store32(r1, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); michael@0: michael@0: // Push frame descriptor and perform the tail call. michael@0: // BaselineTailCallReg (lr) already contains the return address (as we keep it there through michael@0: // the stub calls), but the VMWrapper code being called expects the return address to also michael@0: // be pushed on the stack. michael@0: JS_ASSERT(BaselineTailCallReg == lr); michael@0: masm.makeFrameDescriptor(r0, JitFrame_BaselineJS); michael@0: masm.push(r0); michael@0: masm.push(lr); 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 previous michael@0: // frame pointer pushed by EmitEnterStubFrame. michael@0: masm.mov(BaselineFrameReg, reg); michael@0: masm.ma_add(Imm32(sizeof(void *) * 2), reg); michael@0: masm.ma_sub(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, r0); michael@0: masm.push(r0); michael@0: masm.call(target); michael@0: } michael@0: michael@0: // Size of vales pushed by EmitEnterStubFrame. michael@0: static const uint32_t STUB_FRAME_SIZE = 4 * sizeof(void *); michael@0: static const uint32_t STUB_FRAME_SAVED_STUB_OFFSET = sizeof(void *); michael@0: michael@0: inline void michael@0: EmitEnterStubFrame(MacroAssembler &masm, Register scratch) michael@0: { michael@0: JS_ASSERT(scratch != BaselineTailCallReg); michael@0: michael@0: // Compute frame size. michael@0: masm.mov(BaselineFrameReg, scratch); michael@0: masm.ma_add(Imm32(BaselineFrame::FramePointerOffset), scratch); michael@0: masm.ma_sub(BaselineStackReg, scratch); michael@0: michael@0: masm.store32(scratch, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); michael@0: michael@0: // Note: when making changes here, don't forget to update STUB_FRAME_SIZE michael@0: // if needed. michael@0: michael@0: // Push frame descriptor and return address. michael@0: masm.makeFrameDescriptor(scratch, JitFrame_BaselineJS); michael@0: masm.push(scratch); michael@0: masm.push(BaselineTailCallReg); michael@0: michael@0: // Save old frame pointer, stack pointer and stub reg. michael@0: masm.push(BaselineStubReg); michael@0: masm.push(BaselineFrameReg); michael@0: masm.mov(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: EmitLeaveStubFrameHead(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.ma_lsr(Imm32(FRAMESIZE_SHIFT), ScratchRegister, ScratchRegister); michael@0: masm.ma_add(ScratchRegister, BaselineStackReg); michael@0: } else { michael@0: masm.mov(BaselineFrameReg, BaselineStackReg); michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: EmitLeaveStubFrameCommonTail(MacroAssembler &masm) michael@0: { michael@0: masm.pop(BaselineFrameReg); michael@0: masm.pop(BaselineStubReg); michael@0: michael@0: // Load the return address. michael@0: masm.pop(BaselineTailCallReg); michael@0: michael@0: // Discard the frame descriptor. michael@0: masm.pop(ScratchRegister); michael@0: } michael@0: michael@0: inline void michael@0: EmitLeaveStubFrame(MacroAssembler &masm, bool calledIntoIon = false) michael@0: { michael@0: EmitLeaveStubFrameHead(masm, calledIntoIon); michael@0: EmitLeaveStubFrameCommonTail(masm); michael@0: } michael@0: michael@0: inline void michael@0: EmitStowICValues(MacroAssembler &masm, int values) michael@0: { michael@0: JS_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: JS_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: JS_ASSERT(R2 == ValueOperand(r1, r0)); 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 esp, excluding the return address. michael@0: michael@0: // Save the current BaselineStubReg to stack, as well as the TailCallReg, michael@0: // since on ARM, the LR is live. michael@0: masm.push(BaselineStubReg); michael@0: masm.push(BaselineTailCallReg); 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: // TODO: Change r0 uses below to use masm's configurable scratch register instead. michael@0: michael@0: // Load stubcode pointer from BaselineStubReg into BaselineTailCallReg. michael@0: masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfStubCode()), r0); michael@0: michael@0: // Call the stubcode. michael@0: masm.ma_blx(r0); michael@0: michael@0: // Restore the old stub reg and tailcall reg. michael@0: masm.pop(BaselineTailCallReg); michael@0: masm.pop(BaselineStubReg); 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.cmp32(R1.scratchReg(), Imm32(1)); michael@0: masm.j(Assembler::Equal, &success); 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 ARM, lr is clobbered by patchableCallPreBarrier. Save it first. michael@0: masm.push(lr); michael@0: masm.patchableCallPreBarrier(addr, type); michael@0: masm.pop(lr); michael@0: } michael@0: michael@0: inline void michael@0: EmitStubGuardFailure(MacroAssembler &masm) michael@0: { michael@0: JS_ASSERT(R2 == ValueOperand(r1, r0)); michael@0: michael@0: // NOTE: This routine assumes that the stub guard code left the stack in the michael@0: // 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()), r0); michael@0: michael@0: // Return address is already loaded, just jump to the next stubcode. michael@0: JS_ASSERT(BaselineTailCallReg == lr); michael@0: masm.branch(r0); 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_arm_BaselineHelpers_arm_h */ michael@0: