1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef jit_arm_SharedICHelpers_arm_h
8 #define jit_arm_SharedICHelpers_arm_h
9 
10 #include "jit/BaselineFrame.h"
11 #include "jit/BaselineIC.h"
12 #include "jit/MacroAssembler.h"
13 #include "jit/SharedICRegisters.h"
14 
15 namespace js {
16 namespace jit {
17 
18 // Distance from sp to the top Value inside an IC stub (no return address on the stack on ARM).
19 static const size_t ICStackValueOffset = 0;
20 
21 inline void
EmitRestoreTailCallReg(MacroAssembler & masm)22 EmitRestoreTailCallReg(MacroAssembler& masm)
23 {
24     // No-op on ARM because link register is always holding the return address.
25 }
26 
27 inline void
EmitRepushTailCallReg(MacroAssembler & masm)28 EmitRepushTailCallReg(MacroAssembler& masm)
29 {
30     // No-op on ARM because link register is always holding the return address.
31 }
32 
33 inline void
EmitCallIC(CodeOffset * patchOffset,MacroAssembler & masm)34 EmitCallIC(CodeOffset* patchOffset, MacroAssembler& masm)
35 {
36     // Move ICEntry offset into ICStubReg
37     CodeOffset offset = masm.movWithPatch(ImmWord(-1), ICStubReg);
38     *patchOffset = offset;
39 
40     // Load stub pointer into ICStubReg
41     masm.loadPtr(Address(ICStubReg, ICEntry::offsetOfFirstStub()), ICStubReg);
42 
43     // Load stubcode pointer from BaselineStubEntry.
44     // R2 won't be active when we call ICs, so we can use r0.
45     MOZ_ASSERT(R2 == ValueOperand(r1, r0));
46     masm.loadPtr(Address(ICStubReg, ICStub::offsetOfStubCode()), r0);
47 
48     // Call the stubcode via a direct branch-and-link.
49     masm.ma_blx(r0);
50 }
51 
52 inline void
53 EmitEnterTypeMonitorIC(MacroAssembler& masm,
54                        size_t monitorStubOffset = ICMonitoredStub::offsetOfFirstMonitorStub())
55 {
56     // This is expected to be called from within an IC, when ICStubReg is
57     // properly initialized to point to the stub.
58     masm.loadPtr(Address(ICStubReg, (uint32_t) monitorStubOffset), ICStubReg);
59 
60     // Load stubcode pointer from BaselineStubEntry.
61     // R2 won't be active when we call ICs, so we can use r0.
62     MOZ_ASSERT(R2 == ValueOperand(r1, r0));
63     masm.loadPtr(Address(ICStubReg, ICStub::offsetOfStubCode()), r0);
64 
65     // Jump to the stubcode.
66     masm.branch(r0);
67 }
68 
69 inline void
EmitReturnFromIC(MacroAssembler & masm)70 EmitReturnFromIC(MacroAssembler& masm)
71 {
72     masm.ma_mov(lr, pc);
73 }
74 
75 inline void
EmitChangeICReturnAddress(MacroAssembler & masm,Register reg)76 EmitChangeICReturnAddress(MacroAssembler& masm, Register reg)
77 {
78     masm.ma_mov(reg, lr);
79 }
80 
81 inline void
EmitBaselineTailCallVM(JitCode * target,MacroAssembler & masm,uint32_t argSize)82 EmitBaselineTailCallVM(JitCode* target, MacroAssembler& masm, uint32_t argSize)
83 {
84     // We assume during this that R0 and R1 have been pushed, and that R2 is
85     // unused.
86     MOZ_ASSERT(R2 == ValueOperand(r1, r0));
87 
88     // Compute frame size.
89     masm.movePtr(BaselineFrameReg, r0);
90     masm.as_add(r0, r0, Imm8(BaselineFrame::FramePointerOffset));
91     masm.ma_sub(BaselineStackReg, r0);
92 
93     // Store frame size without VMFunction arguments for GC marking.
94     {
95         ScratchRegisterScope scratch(masm);
96         masm.ma_sub(r0, Imm32(argSize), r1, scratch);
97     }
98     masm.store32(r1, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize()));
99 
100     // Push frame descriptor and perform the tail call.
101     // ICTailCallReg (lr) already contains the return address (as we keep
102     // it there through the stub calls), but the VMWrapper code being called
103     // expects the return address to also be pushed on the stack.
104     MOZ_ASSERT(ICTailCallReg == lr);
105     masm.makeFrameDescriptor(r0, JitFrame_BaselineJS, ExitFrameLayout::Size());
106     masm.push(r0);
107     masm.push(lr);
108     masm.branch(target);
109 }
110 
111 inline void
EmitIonTailCallVM(JitCode * target,MacroAssembler & masm,uint32_t stackSize)112 EmitIonTailCallVM(JitCode* target, MacroAssembler& masm, uint32_t stackSize)
113 {
114     // We assume during this that R0 and R1 have been pushed, and that R2 is
115     // unused.
116     MOZ_ASSERT(R2 == ValueOperand(r1, r0));
117 
118     masm.loadPtr(Address(sp, stackSize), r0);
119     masm.rshiftPtr(Imm32(FRAMESIZE_SHIFT), r0);
120     masm.add32(Imm32(stackSize + JitStubFrameLayout::Size() - sizeof(intptr_t)), r0);
121 
122     // Push frame descriptor and perform the tail call.
123     // ICTailCallReg (lr) already contains the return address (as we keep
124     // it there through the stub calls), but the VMWrapper code being called
125     // expects the return address to also be pushed on the stack.
126     MOZ_ASSERT(ICTailCallReg == lr);
127     masm.makeFrameDescriptor(r0, JitFrame_IonJS, ExitFrameLayout::Size());
128     masm.push(r0);
129     masm.push(lr);
130     masm.branch(target);
131 }
132 
133 inline void
EmitBaselineCreateStubFrameDescriptor(MacroAssembler & masm,Register reg,uint32_t headerSize)134 EmitBaselineCreateStubFrameDescriptor(MacroAssembler& masm, Register reg, uint32_t headerSize)
135 {
136     // Compute stub frame size. We have to add two pointers: the stub reg and
137     // previous frame pointer pushed by EmitEnterStubFrame.
138     masm.mov(BaselineFrameReg, reg);
139     masm.as_add(reg, reg, Imm8(sizeof(void*) * 2));
140     masm.ma_sub(BaselineStackReg, reg);
141 
142     masm.makeFrameDescriptor(reg, JitFrame_BaselineStub, headerSize);
143 }
144 
145 inline void
EmitBaselineCallVM(JitCode * target,MacroAssembler & masm)146 EmitBaselineCallVM(JitCode* target, MacroAssembler& masm)
147 {
148     EmitBaselineCreateStubFrameDescriptor(masm, r0, ExitFrameLayout::Size());
149     masm.push(r0);
150     masm.call(target);
151 }
152 
153 inline void
EmitIonCallVM(JitCode * target,size_t stackSlots,MacroAssembler & masm)154 EmitIonCallVM(JitCode* target, size_t stackSlots, MacroAssembler& masm)
155 {
156     uint32_t descriptor = MakeFrameDescriptor(masm.framePushed(), JitFrame_IonStub,
157                                               ExitFrameLayout::Size());
158     masm.Push(Imm32(descriptor));
159     masm.callJit(target);
160 
161     // Remove rest of the frame left on the stack. We remove the return address
162     // which is implicitly popped when returning.
163     size_t framePop = sizeof(ExitFrameLayout) - sizeof(void*);
164 
165     // Pop arguments from framePushed.
166     masm.implicitPop(stackSlots * sizeof(void*) + framePop);
167 }
168 
169 // Size of vales pushed by EmitEnterStubFrame.
170 static const uint32_t STUB_FRAME_SIZE = 4 * sizeof(void*);
171 static const uint32_t STUB_FRAME_SAVED_STUB_OFFSET = sizeof(void*);
172 
173 inline void
EmitBaselineEnterStubFrame(MacroAssembler & masm,Register scratch)174 EmitBaselineEnterStubFrame(MacroAssembler& masm, Register scratch)
175 {
176     MOZ_ASSERT(scratch != ICTailCallReg);
177 
178     // Compute frame size.
179     masm.mov(BaselineFrameReg, scratch);
180     masm.as_add(scratch, scratch, Imm8(BaselineFrame::FramePointerOffset));
181     masm.ma_sub(BaselineStackReg, scratch);
182 
183     masm.store32(scratch, Address(BaselineFrameReg, BaselineFrame::reverseOffsetOfFrameSize()));
184 
185     // Note: when making changes here, don't forget to update STUB_FRAME_SIZE if
186     // needed.
187 
188     // Push frame descriptor and return address.
189     masm.makeFrameDescriptor(scratch, JitFrame_BaselineJS, BaselineStubFrameLayout::Size());
190     masm.Push(scratch);
191     masm.Push(ICTailCallReg);
192 
193     // Save old frame pointer, stack pointer and stub reg.
194     masm.Push(ICStubReg);
195     masm.Push(BaselineFrameReg);
196     masm.mov(BaselineStackReg, BaselineFrameReg);
197 
198     // We pushed 4 words, so the stack is still aligned to 8 bytes.
199     masm.checkStackAlignment();
200 }
201 
202 inline void
EmitIonEnterStubFrame(MacroAssembler & masm,Register scratch)203 EmitIonEnterStubFrame(MacroAssembler& masm, Register scratch)
204 {
205     MOZ_ASSERT(ICTailCallReg == lr);
206 
207     // In arm the link register contains the return address,
208     // but in jit frames we expect it to be on the stack. As a result
209     // push the link register (which is actually part of the previous frame.
210     // Therefore using push instead of Push).
211     masm.push(ICTailCallReg);
212 
213     masm.Push(ICStubReg);
214 }
215 
216 inline void
217 EmitBaselineLeaveStubFrame(MacroAssembler& masm, bool calledIntoIon = false)
218 {
219     ScratchRegisterScope scratch(masm);
220 
221     // Ion frames do not save and restore the frame pointer. If we called into
222     // Ion, we have to restore the stack pointer from the frame descriptor. If
223     // we performed a VM call, the descriptor has been popped already so in that
224     // case we use the frame pointer.
225     if (calledIntoIon) {
226         masm.Pop(scratch);
227         masm.rshiftPtr(Imm32(FRAMESIZE_SHIFT), scratch);
228         masm.add32(scratch, BaselineStackReg);
229     } else {
230         masm.mov(BaselineFrameReg, BaselineStackReg);
231     }
232 
233     masm.Pop(BaselineFrameReg);
234     masm.Pop(ICStubReg);
235 
236     // Load the return address.
237     masm.Pop(ICTailCallReg);
238 
239     // Discard the frame descriptor.
240     masm.Pop(scratch);
241 }
242 
243 inline void
EmitIonLeaveStubFrame(MacroAssembler & masm)244 EmitIonLeaveStubFrame(MacroAssembler& masm)
245 {
246     masm.Pop(ICStubReg);
247     masm.pop(ICTailCallReg); // See EmitIonEnterStubFrame for explanation on pop/Pop.
248 }
249 
250 inline void
EmitStowICValues(MacroAssembler & masm,int values)251 EmitStowICValues(MacroAssembler& masm, int values)
252 {
253     MOZ_ASSERT(values >= 0 && values <= 2);
254     switch(values) {
255       case 1:
256         // Stow R0.
257         masm.Push(R0);
258         break;
259       case 2:
260         // Stow R0 and R1.
261         masm.Push(R0);
262         masm.Push(R1);
263         break;
264     }
265 }
266 
267 inline void
268 EmitUnstowICValues(MacroAssembler& masm, int values, bool discard = false)
269 {
270     MOZ_ASSERT(values >= 0 && values <= 2);
271     switch(values) {
272       case 1:
273         // Unstow R0.
274         if (discard)
275             masm.addPtr(Imm32(sizeof(Value)), BaselineStackReg);
276         else
277             masm.popValue(R0);
278         break;
279       case 2:
280         // Unstow R0 and R1.
281         if (discard) {
282             masm.addPtr(Imm32(sizeof(Value) * 2), BaselineStackReg);
283         } else {
284             masm.popValue(R1);
285             masm.popValue(R0);
286         }
287         break;
288     }
289     masm.adjustFrame(-values * sizeof(Value));
290 }
291 
292 inline void
EmitCallTypeUpdateIC(MacroAssembler & masm,JitCode * code,uint32_t objectOffset)293 EmitCallTypeUpdateIC(MacroAssembler& masm, JitCode* code, uint32_t objectOffset)
294 {
295     MOZ_ASSERT(R2 == ValueOperand(r1, r0));
296 
297     // R0 contains the value that needs to be typechecked. The object we're
298     // updating is a boxed Value on the stack, at offset objectOffset from esp,
299     // excluding the return address.
300 
301     // Save the current ICStubReg to stack, as well as the TailCallReg,
302     // since on ARM, the LR is live.
303     masm.push(ICStubReg);
304     masm.push(ICTailCallReg);
305 
306     // This is expected to be called from within an IC, when ICStubReg is
307     // properly initialized to point to the stub.
308     masm.loadPtr(Address(ICStubReg, ICUpdatedStub::offsetOfFirstUpdateStub()),
309                  ICStubReg);
310 
311     // TODO: Change r0 uses below to use masm's configurable scratch register instead.
312 
313     // Load stubcode pointer from ICStubReg into ICTailCallReg.
314     masm.loadPtr(Address(ICStubReg, ICStub::offsetOfStubCode()), r0);
315 
316     // Call the stubcode.
317     masm.ma_blx(r0);
318 
319     // Restore the old stub reg and tailcall reg.
320     masm.pop(ICTailCallReg);
321     masm.pop(ICStubReg);
322 
323     // The update IC will store 0 or 1 in R1.scratchReg() reflecting if the
324     // value in R0 type-checked properly or not.
325     Label success;
326     masm.cmp32(R1.scratchReg(), Imm32(1));
327     masm.j(Assembler::Equal, &success);
328 
329     // If the IC failed, then call the update fallback function.
330     EmitBaselineEnterStubFrame(masm, R1.scratchReg());
331 
332     masm.loadValue(Address(BaselineStackReg, STUB_FRAME_SIZE + objectOffset), R1);
333 
334     masm.Push(R0);
335     masm.Push(R1);
336     masm.Push(ICStubReg);
337 
338     // Load previous frame pointer, push BaselineFrame*.
339     masm.loadPtr(Address(BaselineFrameReg, 0), R0.scratchReg());
340     masm.pushBaselineFramePtr(R0.scratchReg(), R0.scratchReg());
341 
342     EmitBaselineCallVM(code, masm);
343     EmitBaselineLeaveStubFrame(masm);
344 
345     // Success at end.
346     masm.bind(&success);
347 }
348 
349 template <typename AddrType>
350 inline void
EmitPreBarrier(MacroAssembler & masm,const AddrType & addr,MIRType type)351 EmitPreBarrier(MacroAssembler& masm, const AddrType& addr, MIRType type)
352 {
353     // On ARM, lr is clobbered by patchableCallPreBarrier. Save it first.
354     masm.push(lr);
355     masm.patchableCallPreBarrier(addr, type);
356     masm.pop(lr);
357 }
358 
359 inline void
EmitStubGuardFailure(MacroAssembler & masm)360 EmitStubGuardFailure(MacroAssembler& masm)
361 {
362     MOZ_ASSERT(R2 == ValueOperand(r1, r0));
363 
364     // NOTE: This routine assumes that the stub guard code left the stack in the
365     // same state it was in when it was entered.
366 
367     // BaselineStubEntry points to the current stub.
368 
369     // Load next stub into ICStubReg.
370     masm.loadPtr(Address(ICStubReg, ICStub::offsetOfNext()), ICStubReg);
371 
372     // Load stubcode pointer from BaselineStubEntry into scratch register.
373     masm.loadPtr(Address(ICStubReg, ICStub::offsetOfStubCode()), r0);
374 
375     // Return address is already loaded, just jump to the next stubcode.
376     MOZ_ASSERT(ICTailCallReg == lr);
377     masm.branch(r0);
378 }
379 
380 
381 } // namespace jit
382 } // namespace js
383 
384 #endif /* jit_arm_SharedICHelpers_arm_h */
385