Debian Codegraph @

js/src/jit/mips64/Trampoline-mips64.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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 #include "mozilla/DebugOnly.h"
8
9 #include "jit/Bailouts.h"
10 #include "jit/BaselineFrame.h"
11 #include "jit/CalleeToken.h"
12 #include "jit/JitFrames.h"
13 #include "jit/JitRuntime.h"
14 #include "jit/JitSpewer.h"
15 #include "jit/mips-shared/SharedICHelpers-mips-shared.h"
16 #include "jit/PerfSpewer.h"
17 #include "jit/VMFunctions.h"
18 #include "vm/JitActivation.h" // js::jit::JitActivation
19 #include "vm/JSContext.h"
20 #include "vm/Realm.h"
21
22 #include "jit/MacroAssembler-inl.h"
23
24 using namespace js;
25 using namespace js::jit;
26
27 // All registers to save and restore. This includes the stack pointer, since we
28 // use the ability to reference register values on the stack by index.
29 static const LiveRegisterSet AllRegs =
30 LiveRegisterSet(GeneralRegisterSet(Registers::AllMask),
31 FloatRegisterSet(FloatRegisters::AllMask));
32
33 static_assert(sizeof(uintptr_t) == sizeof(uint64_t), "Not 32-bit clean.");
34
35 struct EnterJITRegs {
36 double f31;
37 double f30;
38 double f29;
39 double f28;
40 double f27;
41 double f26;
42 double f25;
43 double f24;
44
45 uintptr_t align;
46
47 // non-volatile registers.
48 uint64_t ra;
49 uint64_t fp;
50 uint64_t s7;
51 uint64_t s6;
52 uint64_t s5;
53 uint64_t s4;
54 uint64_t s3;
55 uint64_t s2;
56 uint64_t s1;
57 uint64_t s0;
58 // Save reg_vp(a7) on stack, use it after call jit code.
59 uint64_t a7;
60 };
61
62 static void GenerateReturn(MacroAssembler& masm, int returnCode) {
63 MOZ_ASSERT(masm.framePushed() == sizeof(EnterJITRegs));
64
65 if (isLoongson()) {
66 // Restore non-volatile registers
67 masm.as_ld(s0, StackPointer, offsetof(EnterJITRegs, s0));
68 masm.as_gslq(s1, s2, StackPointer, offsetof(EnterJITRegs, s2));
69 masm.as_gslq(s3, s4, StackPointer, offsetof(EnterJITRegs, s4));
70 masm.as_gslq(s5, s6, StackPointer, offsetof(EnterJITRegs, s6));
71 masm.as_gslq(s7, fp, StackPointer, offsetof(EnterJITRegs, fp));
72 masm.as_ld(ra, StackPointer, offsetof(EnterJITRegs, ra));
73
74 // Restore non-volatile floating point registers
75 masm.as_gslq(f24, f25, StackPointer, offsetof(EnterJITRegs, f25));
76 masm.as_gslq(f26, f27, StackPointer, offsetof(EnterJITRegs, f27));
77 masm.as_gslq(f28, f29, StackPointer, offsetof(EnterJITRegs, f29));
78 masm.as_gslq(f30, f31, StackPointer, offsetof(EnterJITRegs, f31));
79 } else {
80 // Restore non-volatile registers
81 masm.as_ld(s0, StackPointer, offsetof(EnterJITRegs, s0));
82 masm.as_ld(s1, StackPointer, offsetof(EnterJITRegs, s1));
83 masm.as_ld(s2, StackPointer, offsetof(EnterJITRegs, s2));
84 masm.as_ld(s3, StackPointer, offsetof(EnterJITRegs, s3));
85 masm.as_ld(s4, StackPointer, offsetof(EnterJITRegs, s4));
86 masm.as_ld(s5, StackPointer, offsetof(EnterJITRegs, s5));
87 masm.as_ld(s6, StackPointer, offsetof(EnterJITRegs, s6));
88 masm.as_ld(s7, StackPointer, offsetof(EnterJITRegs, s7));
89 masm.as_ld(fp, StackPointer, offsetof(EnterJITRegs, fp));
90 masm.as_ld(ra, StackPointer, offsetof(EnterJITRegs, ra));
91
92 // Restore non-volatile floating point registers
93 masm.as_ldc1(f24, StackPointer, offsetof(EnterJITRegs, f24));
94 masm.as_ldc1(f25, StackPointer, offsetof(EnterJITRegs, f25));
95 masm.as_ldc1(f26, StackPointer, offsetof(EnterJITRegs, f26));
96 masm.as_ldc1(f27, StackPointer, offsetof(EnterJITRegs, f27));
97 masm.as_ldc1(f28, StackPointer, offsetof(EnterJITRegs, f28));
98 masm.as_ldc1(f29, StackPointer, offsetof(EnterJITRegs, f29));
99 masm.as_ldc1(f30, StackPointer, offsetof(EnterJITRegs, f30));
100 masm.as_ldc1(f31, StackPointer, offsetof(EnterJITRegs, f31));
101 }
102
103 masm.freeStack(sizeof(EnterJITRegs));
104
105 masm.branch(ra);
106 }
107
108 static void GeneratePrologue(MacroAssembler& masm) {
109 masm.reserveStack(sizeof(EnterJITRegs));
110
111 if (isLoongson()) {
112 masm.as_gssq(a7, s0, StackPointer, offsetof(EnterJITRegs, s0));
113 masm.as_gssq(s1, s2, StackPointer, offsetof(EnterJITRegs, s2));
114 masm.as_gssq(s3, s4, StackPointer, offsetof(EnterJITRegs, s4));
115 masm.as_gssq(s5, s6, StackPointer, offsetof(EnterJITRegs, s6));
116 masm.as_gssq(s7, fp, StackPointer, offsetof(EnterJITRegs, fp));
117 masm.as_sd(ra, StackPointer, offsetof(EnterJITRegs, ra));
118
119 masm.as_gssq(f24, f25, StackPointer, offsetof(EnterJITRegs, f25));
120 masm.as_gssq(f26, f27, StackPointer, offsetof(EnterJITRegs, f27));
121 masm.as_gssq(f28, f29, StackPointer, offsetof(EnterJITRegs, f29));
122 masm.as_gssq(f30, f31, StackPointer, offsetof(EnterJITRegs, f31));
123 return;
124 }
125
126 masm.as_sd(s0, StackPointer, offsetof(EnterJITRegs, s0));
127 masm.as_sd(s1, StackPointer, offsetof(EnterJITRegs, s1));
128 masm.as_sd(s2, StackPointer, offsetof(EnterJITRegs, s2));
129 masm.as_sd(s3, StackPointer, offsetof(EnterJITRegs, s3));
130 masm.as_sd(s4, StackPointer, offsetof(EnterJITRegs, s4));
131 masm.as_sd(s5, StackPointer, offsetof(EnterJITRegs, s5));
132 masm.as_sd(s6, StackPointer, offsetof(EnterJITRegs, s6));
133 masm.as_sd(s7, StackPointer, offsetof(EnterJITRegs, s7));
134 masm.as_sd(fp, StackPointer, offsetof(EnterJITRegs, fp));
135 masm.as_sd(ra, StackPointer, offsetof(EnterJITRegs, ra));
136 masm.as_sd(a7, StackPointer, offsetof(EnterJITRegs, a7));
137
138 masm.as_sdc1(f24, StackPointer, offsetof(EnterJITRegs, f24));
139 masm.as_sdc1(f25, StackPointer, offsetof(EnterJITRegs, f25));
140 masm.as_sdc1(f26, StackPointer, offsetof(EnterJITRegs, f26));
141 masm.as_sdc1(f27, StackPointer, offsetof(EnterJITRegs, f27));
142 masm.as_sdc1(f28, StackPointer, offsetof(EnterJITRegs, f28));
143 masm.as_sdc1(f29, StackPointer, offsetof(EnterJITRegs, f29));
144 masm.as_sdc1(f30, StackPointer, offsetof(EnterJITRegs, f30));
145 masm.as_sdc1(f31, StackPointer, offsetof(EnterJITRegs, f31));
146 }
147
148 // Generates a trampoline for calling Jit compiled code from a C++ function.
149 // The trampoline use the EnterJitCode signature, with the standard x64 fastcall
150 // calling convention.
151 void JitRuntime::generateEnterJIT(JSContext* cx, MacroAssembler& masm) {
152 AutoCreatedBy acb(masm, "JitRuntime::generateEnterJIT");
153
154 enterJITOffset_ = startTrampolineCode(masm);
155
156 const Register reg_code = IntArgReg0;
157 const Register reg_argc = IntArgReg1;
158 const Register reg_argv = IntArgReg2;
159 const mozilla::DebugOnly<Register> reg_frame = IntArgReg3;
160 const Register reg_token = IntArgReg4;
161 const Register reg_chain = IntArgReg5;
162 const Register reg_values = IntArgReg6;
163 const Register reg_vp = IntArgReg7;
164
165 MOZ_ASSERT(OsrFrameReg == reg_frame);
166
167 GeneratePrologue(masm);
168
169 // Save stack pointer as baseline frame.
170 masm.movePtr(StackPointer, FramePointer);
171
172 // Load the number of actual arguments into s3.
173 masm.unboxInt32(Address(reg_vp, 0), s3);
174
175 /***************************************************************
176 Loop over argv vector, push arguments onto stack in reverse order
177 ***************************************************************/
178
179 // if we are constructing, that also needs to include newTarget
180 {
181 Label noNewTarget;
182 masm.branchTest32(Assembler::Zero, reg_token,
183 Imm32(CalleeToken_FunctionConstructing), &noNewTarget);
184
185 masm.add32(Imm32(1), reg_argc);
186
187 masm.bind(&noNewTarget);
188 }
189
190 // Make stack algined
191 masm.ma_and(s0, reg_argc, Imm32(1));
192 masm.ma_dsubu(s1, StackPointer, Imm32(sizeof(Value)));
193 #ifdef MIPSR6
194 masm.as_selnez(s1, s1, s0);
195 masm.as_seleqz(StackPointer, StackPointer, s0);
196 masm.as_or(StackPointer, StackPointer, s1);
197 #else
198 masm.as_movn(StackPointer, s1, s0);
199 #endif
200
201 masm.as_dsll(s0, reg_argc, 3); // Value* argv
202 masm.addPtr(reg_argv, s0); // s0 = &argv[argc]
203
204 // Loop over arguments, copying them from an unknown buffer onto the Ion
205 // stack so they can be accessed from JIT'ed code.
206 Label header, footer;
207 // If there aren't any arguments, don't do anything
208 masm.ma_b(s0, reg_argv, &footer, Assembler::BelowOrEqual, ShortJump);
209 {
210 masm.bind(&header);
211
212 masm.subPtr(Imm32(sizeof(Value)), s0);
213 masm.subPtr(Imm32(sizeof(Value)), StackPointer);
214
215 ValueOperand value = ValueOperand(s6);
216 masm.loadValue(Address(s0, 0), value);
217 masm.storeValue(value, Address(StackPointer, 0));
218
219 masm.ma_b(s0, reg_argv, &header, Assembler::Above, ShortJump);
220 }
221 masm.bind(&footer);
222
223 masm.push(reg_token);
224 masm.pushFrameDescriptorForJitCall(FrameType::CppToJSJit, s3, s3);
225
226 CodeLabel returnLabel;
227 Label oomReturnLabel;
228 {
229 // Handle Interpreter -> Baseline OSR.
230 AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All());
231 MOZ_ASSERT(!regs.has(FramePointer));
232 regs.take(OsrFrameReg);
233 regs.take(reg_code);
234
235 Label notOsr;
236 masm.ma_b(OsrFrameReg, OsrFrameReg, &notOsr, Assembler::Zero, ShortJump);
237
238 Register numStackValues = reg_values;
239 regs.take(numStackValues);
240 Register scratch = regs.takeAny();
241
242 // Push return address.
243 masm.subPtr(Imm32(sizeof(uintptr_t)), StackPointer);
244 masm.ma_li(scratch, &returnLabel);
245 masm.storePtr(scratch, Address(StackPointer, 0));
246
247 // Push previous frame pointer.
248 masm.subPtr(Imm32(sizeof(uintptr_t)), StackPointer);
249 masm.storePtr(FramePointer, Address(StackPointer, 0));
250
251 // Reserve frame.
252 Register framePtr = FramePointer;
253 masm.movePtr(StackPointer, framePtr);
254 masm.subPtr(Imm32(BaselineFrame::Size()), StackPointer);
255
256 Register framePtrScratch = regs.takeAny();
257 masm.movePtr(sp, framePtrScratch);
258
259 // Reserve space for locals and stack values.
260 masm.ma_dsll(scratch, numStackValues, Imm32(3));
261 masm.subPtr(scratch, StackPointer);
262
263 // Enter exit frame.
264 masm.reserveStack(3 * sizeof(uintptr_t));
265 masm.storePtr(
266 ImmWord(MakeFrameDescriptor(FrameType::BaselineJS)),
267 Address(StackPointer, 2 * sizeof(uintptr_t))); // Frame descriptor
268 masm.storePtr(
269 zero, Address(StackPointer, sizeof(uintptr_t))); // fake return address
270 masm.storePtr(FramePointer, Address(StackPointer, 0));
271
272 // No GC things to mark, push a bare token.
273 masm.loadJSContext(scratch);
274 masm.enterFakeExitFrame(scratch, scratch, ExitFrameType::Bare);
275
276 masm.reserveStack(2 * sizeof(uintptr_t));
277 masm.storePtr(framePtr,
278 Address(StackPointer, sizeof(uintptr_t))); // BaselineFrame
279 masm.storePtr(reg_code, Address(StackPointer, 0)); // jitcode
280
281 using Fn = bool (*)(BaselineFrame* frame, InterpreterFrame* interpFrame,
282 uint32_t numStackValues);
283 masm.setupUnalignedABICall(scratch);
284 masm.passABIArg(framePtrScratch); // BaselineFrame
285 masm.passABIArg(OsrFrameReg); // InterpreterFrame
286 masm.passABIArg(numStackValues);
287 masm.callWithABI<Fn, jit::InitBaselineFrameForOsr>(
288 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
289
290 regs.add(OsrFrameReg);
291 Register jitcode = regs.takeAny();
292 masm.loadPtr(Address(StackPointer, 0), jitcode);
293 masm.loadPtr(Address(StackPointer, sizeof(uintptr_t)), framePtr);
294 masm.freeStack(2 * sizeof(uintptr_t));
295
296 Label error;
297 masm.freeStack(ExitFrameLayout::SizeWithFooter());
298 masm.branchIfFalseBool(ReturnReg, &error);
299
300 // If OSR-ing, then emit instrumentation for setting lastProfilerFrame
301 // if profiler instrumentation is enabled.
302 {
303 Label skipProfilingInstrumentation;
304 AbsoluteAddress addressOfEnabled(
305 cx->runtime()->geckoProfiler().addressOfEnabled());
306 masm.branch32(Assembler::Equal, addressOfEnabled, Imm32(0),
307 &skipProfilingInstrumentation);
308 masm.profilerEnterFrame(framePtr, scratch);
309 masm.bind(&skipProfilingInstrumentation);
310 }
311
312 masm.jump(jitcode);
313
314 // OOM: load error value, discard return address and previous frame
315 // pointer and return.
316 masm.bind(&error);
317 masm.movePtr(framePtr, StackPointer);
318 masm.addPtr(Imm32(2 * sizeof(uintptr_t)), StackPointer);
319 masm.moveValue(MagicValue(JS_ION_ERROR), JSReturnOperand);
320 masm.jump(&oomReturnLabel);
321
322 masm.bind(&notOsr);
323 // Load the scope chain in R1.
324 MOZ_ASSERT(R1.scratchReg() != reg_code);
325 masm.ma_move(R1.scratchReg(), reg_chain);
326 }
327
328 // The call will push the return address on the stack, thus we check that
329 // the stack would be aligned once the call is complete.
330 masm.assertStackAlignment(JitStackAlignment, 2 * sizeof(uintptr_t));
331
332 // Call the function with pushing return address to stack.
333 masm.callJitNoProfiler(reg_code);
334
335 {
336 // Interpreter -> Baseline OSR will return here.
337 masm.bind(&returnLabel);
338 masm.addCodeLabel(returnLabel);
339 masm.bind(&oomReturnLabel);
340 }
341
342 // Discard arguments and padding. Set sp to the address of the EnterJITRegs
343 // on the stack.
344 masm.mov(FramePointer, StackPointer);
345
346 // Store the returned value into the vp
347 masm.as_ld(reg_vp, StackPointer, offsetof(EnterJITRegs, a7));
348 masm.storeValue(JSReturnOperand, Address(reg_vp, 0));
349
350 // Restore non-volatile registers and return.
351 GenerateReturn(masm, ShortJump);
352 }
353
354 // static
355 mozilla::Maybe<::JS::ProfilingFrameIterator::RegisterState>
356 JitRuntime::getCppEntryRegisters(JitFrameLayout* frameStackAddress) {
357 // Not supported, or not implemented yet.
358 // TODO: Implement along with the corresponding stack-walker changes, in
359 // coordination with the Gecko Profiler, see bug 1635987 and follow-ups.
360 return mozilla::Nothing{};
361 }
362
363 void JitRuntime::generateInvalidator(MacroAssembler& masm, Label* bailoutTail) {
364 AutoCreatedBy acb(masm, "JitRuntime::generateInvalidator");
365
366 invalidatorOffset_ = startTrampolineCode(masm);
367
368 // Stack has to be alligned here. If not, we will have to fix it.
369 masm.checkStackAlignment();
370
371 // Push registers such that we can access them from [base + code].
372 masm.PushRegsInMask(AllRegs);
373
374 // Pass pointer to InvalidationBailoutStack structure.
375 masm.movePtr(StackPointer, a0);
376
377 // Reserve place for BailoutInfo pointer. Two words to ensure alignment for
378 // setupAlignedABICall.
379 masm.subPtr(Imm32(2 * sizeof(uintptr_t)), StackPointer);
380 // Pass pointer to BailoutInfo
381 masm.movePtr(StackPointer, a1);
382
383 using Fn = bool (*)(InvalidationBailoutStack* sp, BaselineBailoutInfo** info);
384 masm.setupAlignedABICall();
385 masm.passABIArg(a0);
386 masm.passABIArg(a1);
387 masm.callWithABI<Fn, InvalidationBailout>(
388 ABIType::General, CheckUnsafeCallWithABI::DontCheckOther);
389
390 masm.pop(a2);
391
392 // Pop the machine state and the dead frame.
393 masm.moveToStackPtr(FramePointer);
394
395 // Jump to shared bailout tail. The BailoutInfo pointer has to be in r2.
396 masm.jump(bailoutTail);
397 }
398
399 void JitRuntime::generateArgumentsRectifier(MacroAssembler& masm,
400 ArgumentsRectifierKind kind) {
401 // Do not erase the frame pointer in this function.
402
403 AutoCreatedBy acb(masm, "JitRuntime::generateArgumentsRectifier");
404
405 switch (kind) {
406 case ArgumentsRectifierKind::Normal:
407 argumentsRectifierOffset_ = startTrampolineCode(masm);
408 break;
409 case ArgumentsRectifierKind::TrialInlining:
410 trialInliningArgumentsRectifierOffset_ = startTrampolineCode(masm);
411 break;
412 }
413 masm.pushReturnAddress();
414
415 // Caller:
416 // [arg2] [arg1] [this] [[argc] [callee] [descr] [raddr]] <- sp
417
418 // Frame prologue.
419 //
420 // NOTE: if this changes, fix the Baseline bailout code too!
421 // See BaselineStackBuilder::calculatePrevFramePtr and
422 // BaselineStackBuilder::buildRectifierFrame (in BaselineBailouts.cpp).
423 masm.push(FramePointer);
424 masm.mov(StackPointer, FramePointer);
425
426 // Load argc.
427 masm.loadNumActualArgs(FramePointer, s3);
428
429 Register numActArgsReg = a6;
430 Register calleeTokenReg = a7;
431 Register numArgsReg = a5;
432
433 // Load |nformals| into numArgsReg.
434 masm.loadPtr(
435 Address(FramePointer, RectifierFrameLayout::offsetOfCalleeToken()),
436 calleeTokenReg);
437 masm.mov(calleeTokenReg, numArgsReg);
438 masm.andPtr(Imm32(uint32_t(CalleeTokenMask)), numArgsReg);
439 masm.loadFunctionArgCount(numArgsReg, numArgsReg);
440
441 // Stash another copy in t3, since we are going to do destructive operations
442 // on numArgsReg
443 masm.mov(numArgsReg, t3);
444
445 static_assert(
446 CalleeToken_FunctionConstructing == 1,
447 "Ensure that we can use the constructing bit to count the value");
448 masm.mov(calleeTokenReg, t2);
449 masm.ma_and(t2, Imm32(uint32_t(CalleeToken_FunctionConstructing)));
450
451 // Including |this|, and |new.target|, there are (|nformals| + 1 +
452 // isConstructing) arguments to push to the stack. Then we push a
453 // JitFrameLayout. We compute the padding expressed in the number of extra
454 // |undefined| values to push on the stack.
455 static_assert(
456 sizeof(JitFrameLayout) % JitStackAlignment == 0,
457 "No need to consider the JitFrameLayout for aligning the stack");
458 static_assert(
459 JitStackAlignment % sizeof(Value) == 0,
460 "Ensure that we can pad the stack by pushing extra UndefinedValue");
461
462 MOZ_ASSERT(mozilla::IsPowerOfTwo(JitStackValueAlignment));
463 masm.add32(
464 Imm32(JitStackValueAlignment - 1 /* for padding */ + 1 /* for |this| */),
465 numArgsReg);
466 masm.add32(t2, numArgsReg);
467 masm.and32(Imm32(~(JitStackValueAlignment - 1)), numArgsReg);
468
469 // Load the number of |undefined|s to push into t1. Subtract 1 for |this|.
470 masm.as_dsubu(t1, numArgsReg, s3);
471 masm.sub32(Imm32(1), t1);
472
473 // Caller:
474 // [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ] <- sp
475 // '--- s3 ----'
476 //
477 // Rectifier frame:
478 // [fp'][undef] [undef] [undef] [arg2] [arg1] [this] [ [argc] [callee]
479 // [descr] [raddr] ]
480 // '-------- t1 ---------' '--- s3 ----'
481
482 // Copy number of actual arguments into numActArgsReg
483 masm.mov(s3, numActArgsReg); // Save %sp.
484
485 masm.moveValue(UndefinedValue(), ValueOperand(t0));
486
487 // Push undefined. (including the padding)
488 {
489 Label undefLoopTop;
490
491 masm.bind(&undefLoopTop);
492 masm.sub32(Imm32(1), t1);
493 masm.subPtr(Imm32(sizeof(Value)), StackPointer);
494 masm.storeValue(ValueOperand(t0), Address(StackPointer, 0));
495
496 masm.ma_b(t1, t1, &undefLoopTop, Assembler::NonZero, ShortJump);
497 }
498
499 // Get the topmost argument.
500 static_assert(sizeof(Value) == 8, "TimesEight is used to skip arguments");
501
502 // Get the topmost argument.
503 masm.ma_dsll(t0, s3, Imm32(3)); // t0 <- nargs * 8
504 masm.as_daddu(t1, FramePointer, t0); // t1 <- fp(saved sp) + nargs * 8
505 masm.addPtr(Imm32(sizeof(RectifierFrameLayout)), t1);
506
507 // Push arguments, |nargs| + 1 times (to include |this|).
508
509 masm.addPtr(Imm32(1), s3);
510 {
511 Label copyLoopTop;
512
513 masm.bind(&copyLoopTop);
514 masm.sub32(Imm32(1), s3);
515 masm.subPtr(Imm32(sizeof(Value)), StackPointer);
516 masm.loadValue(Address(t1, 0), ValueOperand(t0));
517 masm.storeValue(ValueOperand(t0), Address(StackPointer, 0));
518 masm.subPtr(Imm32(sizeof(Value)), t1);
519
520 masm.ma_b(s3, s3, &copyLoopTop, Assembler::NonZero, ShortJump);
521 }
522
523 // if constructing, copy newTarget
524 {
525 Label notConstructing;
526
527 masm.branchTest32(Assembler::Zero, calleeTokenReg,
528 Imm32(CalleeToken_FunctionConstructing),
529 &notConstructing);
530
531 // thisFrame[numFormals] = prevFrame[argc]
532 ValueOperand newTarget(t0);
533
534 // Load vp[argc]. Add sizeof(Value) for |this|.
535 BaseIndex newTargetSrc(FramePointer, numActArgsReg, TimesEight,
536 sizeof(RectifierFrameLayout) + sizeof(Value));
537 masm.loadValue(newTargetSrc, newTarget);
538
539 // Again, 1 for |this|
540 BaseIndex newTargetDest(StackPointer, t3, TimesEight, sizeof(Value));
541 masm.storeValue(newTarget, newTargetDest);
542
543 masm.bind(&notConstructing);
544 }
545
546 // Caller:
547 // [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ]
548 //
549 //
550 // Rectifier frame:
551 // [fp'] <- fp [undef] [undef] [undef] [arg2] [arg1] [this] <- sp [ [argc]
552 // [callee] [descr] [raddr] ]
553
554 // Construct JitFrameLayout.
555 masm.push(calleeTokenReg);
556 masm.pushFrameDescriptorForJitCall(FrameType::Rectifier, numActArgsReg,
557 numActArgsReg);
558
559 // Call the target function.
560 masm.andPtr(Imm32(uint32_t(CalleeTokenMask)), calleeTokenReg);
561 switch (kind) {
562 case ArgumentsRectifierKind::Normal:
563 masm.loadJitCodeRaw(calleeTokenReg, t1);
564 argumentsRectifierReturnOffset_ = masm.callJitNoProfiler(t1);
565 break;
566 case ArgumentsRectifierKind::TrialInlining:
567 Label noBaselineScript, done;
568 masm.loadBaselineJitCodeRaw(calleeTokenReg, t1, &noBaselineScript);
569 masm.callJitNoProfiler(t1);
570 masm.jump(&done);
571
572 // See BaselineCacheIRCompiler::emitCallInlinedFunction.
573 masm.bind(&noBaselineScript);
574 masm.loadJitCodeRaw(calleeTokenReg, t1);
575 masm.callJitNoProfiler(t1);
576 masm.bind(&done);
577 break;
578 }
579
580 masm.mov(FramePointer, StackPointer);
581 masm.pop(FramePointer);
582 masm.ret();
583 }
584
585 /* - When bailout is done via out of line code (lazy bailout).
586 * Frame size is stored in $ra (look at
587 * CodeGeneratorMIPS64::generateOutOfLineCode()) and thunk code should save it
588 * on stack. Other difference is that members snapshotOffset_ and padding_ are
589 * pushed to the stack by CodeGeneratorMIPS64::visitOutOfLineBailout().
590 */
591 static void PushBailoutFrame(MacroAssembler& masm, Register spArg) {
592 // Push the frameSize_ stored in ra
593 // See: CodeGeneratorMIPS64::generateOutOfLineCode()
594 masm.push(ra);
595
596 // Push registers such that we can access them from [base + code].
597 masm.PushRegsInMask(AllRegs);
598
599 // Put pointer to BailoutStack as first argument to the Bailout()
600 masm.movePtr(StackPointer, spArg);
601 }
602
603 static void GenerateBailoutThunk(MacroAssembler& masm, Label* bailoutTail) {
604 PushBailoutFrame(masm, a0);
605
606 // Put pointer to BailoutInfo
607 static const uint32_t sizeOfBailoutInfo = sizeof(uintptr_t) * 2;
608 masm.subPtr(Imm32(sizeOfBailoutInfo), StackPointer);
609 masm.movePtr(StackPointer, a1);
610
611 using Fn = bool (*)(BailoutStack* sp, BaselineBailoutInfo** info);
612 masm.setupAlignedABICall();
613 masm.passABIArg(a0);
614 masm.passABIArg(a1);
615 masm.callWithABI<Fn, Bailout>(ABIType::General,
616 CheckUnsafeCallWithABI::DontCheckOther);
617
618 // Get BailoutInfo pointer
619 masm.loadPtr(Address(StackPointer, 0), a2);
620
621 // Remove both the bailout frame and the topmost Ion frame's stack.
622 masm.moveToStackPtr(FramePointer);
623
624 // Jump to shared bailout tail. The BailoutInfo pointer has to be in a2.
625 masm.jump(bailoutTail);
626 }
627
628 void JitRuntime::generateBailoutHandler(MacroAssembler& masm,
629 Label* bailoutTail) {
630 AutoCreatedBy acb(masm, "JitRuntime::generateBailoutHandler");
631
632 bailoutHandlerOffset_ = startTrampolineCode(masm);
633
634 GenerateBailoutThunk(masm, bailoutTail);
635 }
636
637 bool JitRuntime::generateVMWrapper(JSContext* cx, MacroAssembler& masm,
638 VMFunctionId id, const VMFunctionData& f,
639 DynFn nativeFun, uint32_t* wrapperOffset) {
640 AutoCreatedBy acb(masm, "JitRuntime::generateVMWrapper");
641
642 *wrapperOffset = startTrampolineCode(masm);
643
644 // Avoid conflicts with argument registers while discarding the result after
645 // the function call.
646 AllocatableGeneralRegisterSet regs(Register::Codes::WrapperMask);
647
648 static_assert(
649 (Register::Codes::VolatileMask & ~Register::Codes::WrapperMask) == 0,
650 "Wrapper register set should be a superset of Volatile register set.");
651
652 // The context is the first argument; a0 is the first argument register.
653 Register cxreg = a0;
654 regs.take(cxreg);
655
656 // On link-register platforms, it is the responsibility of the VM *callee* to
657 // push the return address, while the caller must ensure that the address
658 // is stored in ra on entry. This allows the VM wrapper to work with both
659 // direct calls and tail calls.
660 masm.pushReturnAddress();
661
662 // Push the frame pointer to finish the exit frame, then link it up.
663 masm.Push(FramePointer);
664 masm.moveStackPtrTo(FramePointer);
665 masm.loadJSContext(cxreg);
666 masm.enterExitFrame(cxreg, regs.getAny(), id);
667
668 // Reserve space for the outparameter.
669 masm.reserveVMFunctionOutParamSpace(f);
670 masm.setupUnalignedABICallDontSaveRestoreSP();
671 masm.passABIArg(cxreg);
672
673 size_t argDisp = ExitFrameLayout::Size();
674
675 // Copy any arguments.
676 for (uint32_t explicitArg = 0; explicitArg < f.explicitArgs; explicitArg++) {
677 switch (f.argProperties(explicitArg)) {
678 case VMFunctionData::WordByValue:
679 if (f.argPassedInFloatReg(explicitArg)) {
680 masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::Float64);
681 } else {
682 masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General);
683 }
684 argDisp += sizeof(void*);
685 break;
686 case VMFunctionData::WordByRef:
687 masm.passABIArg(MoveOperand(FramePointer, argDisp,
688 MoveOperand::Kind::EffectiveAddress),
689 ABIType::General);
690 argDisp += sizeof(void*);
691 break;
692 case VMFunctionData::DoubleByValue:
693 case VMFunctionData::DoubleByRef:
694 MOZ_CRASH("NYI: MIPS64 callVM should not be used with 128bits values.");
695 break;
696 }
697 }
698
699 // Copy the implicit outparam, if any.
700 const int32_t outParamOffset =
701 -int32_t(ExitFooterFrame::Size()) - f.sizeOfOutParamStackSlot();
702 if (f.outParam != Type_Void) {
703 masm.passABIArg(MoveOperand(FramePointer, outParamOffset,
704 MoveOperand::Kind::EffectiveAddress),
705 ABIType::General);
706 }
707
708 masm.callWithABI(nativeFun, ABIType::General,
709 CheckUnsafeCallWithABI::DontCheckHasExitFrame);
710
711 // Test for failure.
712 switch (f.failType()) {
713 case Type_Cell:
714 masm.branchTestPtr(Assembler::Zero, v0, v0, masm.failureLabel());
715 break;
716 case Type_Bool:
717 // Called functions return bools, which are 0/false and non-zero/true
718 masm.branchIfFalseBool(v0, masm.failureLabel());
719 break;
720 case Type_Void:
721 break;
722 default:
723 MOZ_CRASH("unknown failure kind");
724 }
725
726 // Load the outparam.
727 masm.loadVMFunctionOutParam(f, Address(FramePointer, outParamOffset));
728
729 // Pop frame and restore frame pointer.
730 masm.moveToStackPtr(FramePointer);
731 masm.pop(FramePointer);
732
733 // Return. Subtract sizeof(void*) for the frame pointer.
734 masm.retn(Imm32(sizeof(ExitFrameLayout) - sizeof(void*) +
735 f.explicitStackSlots() * sizeof(void*) +
736 f.extraValuesToPop * sizeof(Value)));
737
738 return true;
739 }
740
741 uint32_t JitRuntime::generatePreBarrier(JSContext* cx, MacroAssembler& masm,
742 MIRType type) {
743 AutoCreatedBy acb(masm, "JitRuntime::generatePreBarrier");
744
745 uint32_t offset = startTrampolineCode(masm);
746
747 MOZ_ASSERT(PreBarrierReg == a1);
748 Register temp1 = a0;
749 Register temp2 = a2;
750 Register temp3 = a3;
751 masm.push(temp1);
752 masm.push(temp2);
753 masm.push(temp3);
754
755 Label noBarrier;
756 masm.emitPreBarrierFastPath(cx->runtime(), type, temp1, temp2, temp3,
757 &noBarrier);
758
759 // Call into C++ to mark this GC thing.
760 masm.pop(temp3);
761 masm.pop(temp2);
762 masm.pop(temp1);
763
764 LiveRegisterSet save;
765 save.set() = RegisterSet(GeneralRegisterSet(Registers::VolatileMask),
766 FloatRegisterSet(FloatRegisters::VolatileMask));
767 save.add(ra);
768 masm.PushRegsInMask(save);
769
770 masm.movePtr(ImmPtr(cx->runtime()), a0);
771
772 masm.setupUnalignedABICall(a2);
773 masm.passABIArg(a0);
774 masm.passABIArg(a1);
775 masm.callWithABI(JitPreWriteBarrier(type));
776
777 save.take(AnyRegister(ra));
778 masm.PopRegsInMask(save);
779 masm.ret();
780
781 masm.bind(&noBarrier);
782 masm.pop(temp3);
783 masm.pop(temp2);
784 masm.pop(temp1);
785 masm.abiret();
786
787 return offset;
788 }
789
790 void JitRuntime::generateBailoutTailStub(MacroAssembler& masm,
791 Label* bailoutTail) {
792 AutoCreatedBy acb(masm, "JitRuntime::generateBailoutTailStub");
793
794 masm.bind(bailoutTail);
795 masm.generateBailoutTail(a1, a2);
796 }