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_x86_BaselineHelpers_x86_h michael@0: #define jit_x86_BaselineHelpers_x86_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 stack top to the top Value inside an IC stub (this is the return address). michael@0: static const size_t ICStackValueOffset = sizeof(void *); michael@0: michael@0: inline void michael@0: EmitRestoreTailCallReg(MacroAssembler &masm) michael@0: { michael@0: masm.pop(BaselineTailCallReg); michael@0: } michael@0: michael@0: inline void michael@0: EmitRepushTailCallReg(MacroAssembler &masm) michael@0: { michael@0: masm.push(BaselineTailCallReg); 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, (int32_t) ICEntry::offsetOfFirstStub()), michael@0: BaselineStubReg); michael@0: michael@0: // Load stubcode pointer from BaselineStubEntry into BaselineTailCallReg michael@0: // BaselineTailCallReg will always be unused in the contexts where ICs are called. michael@0: masm.call(Operand(BaselineStubReg, ICStub::offsetOfStubCode())); 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, (int32_t) monitorStubOffset), BaselineStubReg); michael@0: michael@0: // Jump to the stubcode. michael@0: masm.jmp(Operand(BaselineStubReg, (int32_t) ICStub::offsetOfStubCode())); michael@0: } michael@0: michael@0: inline void michael@0: EmitReturnFromIC(MacroAssembler &masm) michael@0: { michael@0: masm.ret(); michael@0: } michael@0: michael@0: inline void michael@0: EmitChangeICReturnAddress(MacroAssembler &masm, Register reg) michael@0: { michael@0: masm.storePtr(reg, Address(StackPointer, 0)); 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. michael@0: michael@0: // Compute frame size. michael@0: masm.movl(BaselineFrameReg, eax); michael@0: masm.addl(Imm32(BaselineFrame::FramePointerOffset), eax); michael@0: masm.subl(BaselineStackReg, eax); michael@0: michael@0: // Store frame size without VMFunction arguments for GC marking. michael@0: masm.movl(eax, ebx); michael@0: masm.subl(Imm32(argSize), ebx); michael@0: masm.store32(ebx, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); michael@0: michael@0: // Push frame descriptor and perform the tail call. michael@0: masm.makeFrameDescriptor(eax, JitFrame_BaselineJS); michael@0: masm.push(eax); michael@0: masm.push(BaselineTailCallReg); michael@0: masm.jmp(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.movl(BaselineFrameReg, reg); michael@0: masm.addl(Imm32(sizeof(void *) * 2), reg); michael@0: masm.subl(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, eax); michael@0: masm.push(eax); 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: EmitRestoreTailCallReg(masm); michael@0: michael@0: // Compute frame size. michael@0: masm.movl(BaselineFrameReg, scratch); michael@0: masm.addl(Imm32(BaselineFrame::FramePointerOffset), scratch); michael@0: masm.subl(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: 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: Register scratch = BaselineTailCallReg; michael@0: masm.pop(scratch); michael@0: masm.shrl(Imm32(FRAMESIZE_SHIFT), scratch); michael@0: masm.addl(scratch, 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: // Pop return address. michael@0: masm.pop(BaselineTailCallReg); michael@0: michael@0: // Overwrite frame descriptor with return address, so that the stack matches michael@0: // the state before entering the stub frame. michael@0: masm.storePtr(BaselineTailCallReg, Address(BaselineStackReg, 0)); 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.pop(BaselineTailCallReg); michael@0: masm.pushValue(R0); michael@0: masm.push(BaselineTailCallReg); michael@0: break; michael@0: case 2: michael@0: // Stow R0 and R1 michael@0: masm.pop(BaselineTailCallReg); michael@0: masm.pushValue(R0); michael@0: masm.pushValue(R1); michael@0: masm.push(BaselineTailCallReg); 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: masm.pop(BaselineTailCallReg); michael@0: if (discard) michael@0: masm.addPtr(Imm32(sizeof(Value)), BaselineStackReg); michael@0: else michael@0: masm.popValue(R0); michael@0: masm.push(BaselineTailCallReg); michael@0: break; michael@0: case 2: michael@0: // Unstow R0 and R1 michael@0: masm.pop(BaselineTailCallReg); 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: masm.push(BaselineTailCallReg); 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 stack top, excluding the return address. michael@0: michael@0: // Save the current BaselineStubReg to stack michael@0: masm.push(BaselineStubReg); 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, (int32_t) ICUpdatedStub::offsetOfFirstUpdateStub()), michael@0: BaselineStubReg); michael@0: michael@0: // Call the stubcode. michael@0: masm.call(Operand(BaselineStubReg, ICStub::offsetOfStubCode())); michael@0: michael@0: // Restore the old stub reg. 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: masm.patchableCallPreBarrier(addr, type); 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 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, (int32_t) ICStub::offsetOfNext()), BaselineStubReg); michael@0: michael@0: // Return address is already loaded, just jump to the next stubcode. michael@0: masm.jmp(Operand(BaselineStubReg, (int32_t) ICStub::offsetOfStubCode())); 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_x86_BaselineHelpers_x86_h */