Debian Codegraph @

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