1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit/x64/BaselineHelpers-x64.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,304 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef jit_x64_BaselineHelpers_x64_h 1.11 +#define jit_x64_BaselineHelpers_x64_h 1.12 + 1.13 +#ifdef JS_ION 1.14 +#include "jit/BaselineFrame.h" 1.15 +#include "jit/BaselineIC.h" 1.16 +#include "jit/BaselineRegisters.h" 1.17 +#include "jit/IonMacroAssembler.h" 1.18 + 1.19 +namespace js { 1.20 +namespace jit { 1.21 + 1.22 +// Distance from Stack top to the top Value inside an IC stub (this is the return address). 1.23 +static const size_t ICStackValueOffset = sizeof(void *); 1.24 + 1.25 +inline void 1.26 +EmitRestoreTailCallReg(MacroAssembler &masm) 1.27 +{ 1.28 + masm.pop(BaselineTailCallReg); 1.29 +} 1.30 + 1.31 +inline void 1.32 +EmitRepushTailCallReg(MacroAssembler &masm) 1.33 +{ 1.34 + masm.push(BaselineTailCallReg); 1.35 +} 1.36 + 1.37 +inline void 1.38 +EmitCallIC(CodeOffsetLabel *patchOffset, MacroAssembler &masm) 1.39 +{ 1.40 + // Move ICEntry offset into BaselineStubReg 1.41 + CodeOffsetLabel offset = masm.movWithPatch(ImmWord(-1), BaselineStubReg); 1.42 + *patchOffset = offset; 1.43 + 1.44 + // Load stub pointer into BaselineStubReg 1.45 + masm.loadPtr(Address(BaselineStubReg, (int32_t) ICEntry::offsetOfFirstStub()), 1.46 + BaselineStubReg); 1.47 + 1.48 + // Call the stubcode. 1.49 + masm.call(Operand(BaselineStubReg, ICStub::offsetOfStubCode())); 1.50 +} 1.51 + 1.52 +inline void 1.53 +EmitEnterTypeMonitorIC(MacroAssembler &masm, 1.54 + size_t monitorStubOffset = ICMonitoredStub::offsetOfFirstMonitorStub()) 1.55 +{ 1.56 + // This is expected to be called from within an IC, when BaselineStubReg 1.57 + // is properly initialized to point to the stub. 1.58 + masm.loadPtr(Address(BaselineStubReg, (int32_t) monitorStubOffset), BaselineStubReg); 1.59 + 1.60 + // Jump to the stubcode. 1.61 + masm.jmp(Operand(BaselineStubReg, (int32_t) ICStub::offsetOfStubCode())); 1.62 +} 1.63 + 1.64 +inline void 1.65 +EmitReturnFromIC(MacroAssembler &masm) 1.66 +{ 1.67 + masm.ret(); 1.68 +} 1.69 + 1.70 +inline void 1.71 +EmitChangeICReturnAddress(MacroAssembler &masm, Register reg) 1.72 +{ 1.73 + masm.storePtr(reg, Address(StackPointer, 0)); 1.74 +} 1.75 + 1.76 +inline void 1.77 +EmitTailCallVM(JitCode *target, MacroAssembler &masm, uint32_t argSize) 1.78 +{ 1.79 + // We an assume during this that R0 and R1 have been pushed. 1.80 + masm.movq(BaselineFrameReg, ScratchReg); 1.81 + masm.addq(Imm32(BaselineFrame::FramePointerOffset), ScratchReg); 1.82 + masm.subq(BaselineStackReg, ScratchReg); 1.83 + 1.84 + // Store frame size without VMFunction arguments for GC marking. 1.85 + masm.movq(ScratchReg, rdx); 1.86 + masm.subq(Imm32(argSize), rdx); 1.87 + masm.store32(rdx, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); 1.88 + 1.89 + // Push frame descriptor and perform the tail call. 1.90 + masm.makeFrameDescriptor(ScratchReg, JitFrame_BaselineJS); 1.91 + masm.push(ScratchReg); 1.92 + masm.push(BaselineTailCallReg); 1.93 + masm.jmp(target); 1.94 +} 1.95 + 1.96 +inline void 1.97 +EmitCreateStubFrameDescriptor(MacroAssembler &masm, Register reg) 1.98 +{ 1.99 + // Compute stub frame size. We have to add two pointers: the stub reg and previous 1.100 + // frame pointer pushed by EmitEnterStubFrame. 1.101 + masm.movq(BaselineFrameReg, reg); 1.102 + masm.addq(Imm32(sizeof(void *) * 2), reg); 1.103 + masm.subq(BaselineStackReg, reg); 1.104 + 1.105 + masm.makeFrameDescriptor(reg, JitFrame_BaselineStub); 1.106 +} 1.107 + 1.108 +inline void 1.109 +EmitCallVM(JitCode *target, MacroAssembler &masm) 1.110 +{ 1.111 + EmitCreateStubFrameDescriptor(masm, ScratchReg); 1.112 + masm.push(ScratchReg); 1.113 + masm.call(target); 1.114 +} 1.115 + 1.116 +// Size of vales pushed by EmitEnterStubFrame. 1.117 +static const uint32_t STUB_FRAME_SIZE = 4 * sizeof(void *); 1.118 +static const uint32_t STUB_FRAME_SAVED_STUB_OFFSET = sizeof(void *); 1.119 + 1.120 +inline void 1.121 +EmitEnterStubFrame(MacroAssembler &masm, Register) 1.122 +{ 1.123 + EmitRestoreTailCallReg(masm); 1.124 + 1.125 + // Compute frame size. 1.126 + masm.movq(BaselineFrameReg, ScratchReg); 1.127 + masm.addq(Imm32(BaselineFrame::FramePointerOffset), ScratchReg); 1.128 + masm.subq(BaselineStackReg, ScratchReg); 1.129 + 1.130 + masm.store32(ScratchReg, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize())); 1.131 + 1.132 + // Note: when making changes here, don't forget to update STUB_FRAME_SIZE 1.133 + // if needed. 1.134 + 1.135 + // Push frame descriptor and return address. 1.136 + masm.makeFrameDescriptor(ScratchReg, JitFrame_BaselineJS); 1.137 + masm.push(ScratchReg); 1.138 + masm.push(BaselineTailCallReg); 1.139 + 1.140 + // Save old frame pointer, stack pointer and stub reg. 1.141 + masm.push(BaselineStubReg); 1.142 + masm.push(BaselineFrameReg); 1.143 + masm.mov(BaselineStackReg, BaselineFrameReg); 1.144 +} 1.145 + 1.146 +inline void 1.147 +EmitLeaveStubFrameHead(MacroAssembler &masm, bool calledIntoIon = false) 1.148 +{ 1.149 + // Ion frames do not save and restore the frame pointer. If we called 1.150 + // into Ion, we have to restore the stack pointer from the frame descriptor. 1.151 + // If we performed a VM call, the descriptor has been popped already so 1.152 + // in that case we use the frame pointer. 1.153 + if (calledIntoIon) { 1.154 + masm.pop(ScratchReg); 1.155 + masm.shrq(Imm32(FRAMESIZE_SHIFT), ScratchReg); 1.156 + masm.addq(ScratchReg, BaselineStackReg); 1.157 + } else { 1.158 + masm.mov(BaselineFrameReg, BaselineStackReg); 1.159 + } 1.160 +} 1.161 + 1.162 +inline void 1.163 +EmitLeaveStubFrameCommonTail(MacroAssembler &masm) 1.164 +{ 1.165 + masm.pop(BaselineFrameReg); 1.166 + masm.pop(BaselineStubReg); 1.167 + 1.168 + // Pop return address. 1.169 + masm.pop(BaselineTailCallReg); 1.170 + 1.171 + // Overwrite frame descriptor with return address, so that the stack matches 1.172 + // the state before entering the stub frame. 1.173 + masm.storePtr(BaselineTailCallReg, Address(BaselineStackReg, 0)); 1.174 +} 1.175 + 1.176 +inline void 1.177 +EmitLeaveStubFrame(MacroAssembler &masm, bool calledIntoIon = false) 1.178 +{ 1.179 + EmitLeaveStubFrameHead(masm, calledIntoIon); 1.180 + EmitLeaveStubFrameCommonTail(masm); 1.181 +} 1.182 + 1.183 +inline void 1.184 +EmitStowICValues(MacroAssembler &masm, int values) 1.185 +{ 1.186 + JS_ASSERT(values >= 0 && values <= 2); 1.187 + switch(values) { 1.188 + case 1: 1.189 + // Stow R0 1.190 + masm.pop(BaselineTailCallReg); 1.191 + masm.pushValue(R0); 1.192 + masm.push(BaselineTailCallReg); 1.193 + break; 1.194 + case 2: 1.195 + // Stow R0 and R1 1.196 + masm.pop(BaselineTailCallReg); 1.197 + masm.pushValue(R0); 1.198 + masm.pushValue(R1); 1.199 + masm.push(BaselineTailCallReg); 1.200 + break; 1.201 + } 1.202 +} 1.203 + 1.204 +inline void 1.205 +EmitUnstowICValues(MacroAssembler &masm, int values, bool discard = false) 1.206 +{ 1.207 + JS_ASSERT(values >= 0 && values <= 2); 1.208 + switch(values) { 1.209 + case 1: 1.210 + // Unstow R0 1.211 + masm.pop(BaselineTailCallReg); 1.212 + if (discard) 1.213 + masm.addPtr(Imm32(sizeof(Value)), BaselineStackReg); 1.214 + else 1.215 + masm.popValue(R0); 1.216 + masm.push(BaselineTailCallReg); 1.217 + break; 1.218 + case 2: 1.219 + // Unstow R0 and R1 1.220 + masm.pop(BaselineTailCallReg); 1.221 + if (discard) { 1.222 + masm.addPtr(Imm32(sizeof(Value) * 2), BaselineStackReg); 1.223 + } else { 1.224 + masm.popValue(R1); 1.225 + masm.popValue(R0); 1.226 + } 1.227 + masm.push(BaselineTailCallReg); 1.228 + break; 1.229 + } 1.230 +} 1.231 + 1.232 +inline void 1.233 +EmitCallTypeUpdateIC(MacroAssembler &masm, JitCode *code, uint32_t objectOffset) 1.234 +{ 1.235 + // R0 contains the value that needs to be typechecked. 1.236 + // The object we're updating is a boxed Value on the stack, at offset 1.237 + // objectOffset from stack top, excluding the return address. 1.238 + 1.239 + // Save the current BaselineStubReg to stack 1.240 + masm.push(BaselineStubReg); 1.241 + 1.242 + // This is expected to be called from within an IC, when BaselineStubReg 1.243 + // is properly initialized to point to the stub. 1.244 + masm.loadPtr(Address(BaselineStubReg, (int32_t) ICUpdatedStub::offsetOfFirstUpdateStub()), 1.245 + BaselineStubReg); 1.246 + 1.247 + // Call the stubcode. 1.248 + masm.call(Operand(BaselineStubReg, ICStub::offsetOfStubCode())); 1.249 + 1.250 + // Restore the old stub reg. 1.251 + masm.pop(BaselineStubReg); 1.252 + 1.253 + // The update IC will store 0 or 1 in R1.scratchReg() reflecting if the 1.254 + // value in R0 type-checked properly or not. 1.255 + Label success; 1.256 + masm.cmp32(R1.scratchReg(), Imm32(1)); 1.257 + masm.j(Assembler::Equal, &success); 1.258 + 1.259 + // If the IC failed, then call the update fallback function. 1.260 + EmitEnterStubFrame(masm, R1.scratchReg()); 1.261 + 1.262 + masm.loadValue(Address(BaselineStackReg, STUB_FRAME_SIZE + objectOffset), R1); 1.263 + 1.264 + masm.pushValue(R0); 1.265 + masm.pushValue(R1); 1.266 + masm.push(BaselineStubReg); 1.267 + 1.268 + // Load previous frame pointer, push BaselineFrame *. 1.269 + masm.loadPtr(Address(BaselineFrameReg, 0), R0.scratchReg()); 1.270 + masm.pushBaselineFramePtr(R0.scratchReg(), R0.scratchReg()); 1.271 + 1.272 + EmitCallVM(code, masm); 1.273 + EmitLeaveStubFrame(masm); 1.274 + 1.275 + // Success at end. 1.276 + masm.bind(&success); 1.277 +} 1.278 + 1.279 +template <typename AddrType> 1.280 +inline void 1.281 +EmitPreBarrier(MacroAssembler &masm, const AddrType &addr, MIRType type) 1.282 +{ 1.283 + masm.patchableCallPreBarrier(addr, type); 1.284 +} 1.285 + 1.286 +inline void 1.287 +EmitStubGuardFailure(MacroAssembler &masm) 1.288 +{ 1.289 + // NOTE: This routine assumes that the stub guard code left the stack in the 1.290 + // same state it was in when it was entered. 1.291 + 1.292 + // BaselineStubEntry points to the current stub. 1.293 + 1.294 + // Load next stub into BaselineStubReg 1.295 + masm.loadPtr(Address(BaselineStubReg, ICStub::offsetOfNext()), BaselineStubReg); 1.296 + 1.297 + // Return address is already loaded, just jump to the next stubcode. 1.298 + masm.jmp(Operand(BaselineStubReg, ICStub::offsetOfStubCode())); 1.299 +} 1.300 + 1.301 + 1.302 +} // namespace jit 1.303 +} // namespace js 1.304 + 1.305 +#endif // JS_ION 1.306 + 1.307 +#endif /* jit_x64_BaselineHelpers_x64_h */