Debian Codegraph @

js/src/jit/CacheIRCompiler.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/CacheIRCompiler.h"
8
9 #include "mozilla/ArrayUtils.h"
10 #include "mozilla/FunctionTypeTraits.h"
11 #include "mozilla/MaybeOneOf.h"
12 #include "mozilla/ScopeExit.h"
13
14 #include <type_traits>
15 #include <utility>
16
17 #include "jslibmath.h"
18 #include "jsmath.h"
19
20 #include "builtin/DataViewObject.h"
21 #include "builtin/Object.h"
22 #include "gc/GCEnum.h"
23 #include "jit/BaselineCacheIRCompiler.h"
24 #include "jit/CacheIRGenerator.h"
25 #include "jit/IonCacheIRCompiler.h"
26 #include "jit/JitFrames.h"
27 #include "jit/JitRuntime.h"
28 #include "jit/JitZone.h"
29 #include "jit/SharedICHelpers.h"
30 #include "jit/SharedICRegisters.h"
31 #include "jit/VMFunctions.h"
32 #include "js/friend/DOMProxy.h" // JS::ExpandoAndGeneration
33 #include "js/friend/XrayJitInfo.h" // js::jit::GetXrayJitInfo
34 #include "js/ScalarType.h" // js::Scalar::Type
35 #include "js/SweepingAPI.h"
36 #include "proxy/DOMProxy.h"
37 #include "proxy/Proxy.h"
38 #include "proxy/ScriptedProxyHandler.h"
39 #include "util/DifferentialTesting.h"
40 #include "vm/ArgumentsObject.h"
41 #include "vm/ArrayBufferObject.h"
42 #include "vm/ArrayBufferViewObject.h"
43 #include "vm/BigIntType.h"
44 #include "vm/FunctionFlags.h" // js::FunctionFlags
45 #include "vm/GeneratorObject.h"
46 #include "vm/GetterSetter.h"
47 #include "vm/Interpreter.h"
48 #include "vm/TypeofEqOperand.h" // TypeofEqOperand
49 #include "vm/Uint8Clamped.h"
50
51 #include "builtin/Boolean-inl.h"
52 #include "jit/MacroAssembler-inl.h"
53 #include "jit/SharedICHelpers-inl.h"
54 #include "jit/VMFunctionList-inl.h"
55
56 using namespace js;
57 using namespace js::jit;
58
59 using mozilla::Maybe;
60
61 using JS::ExpandoAndGeneration;
62
63 ValueOperand CacheRegisterAllocator::useValueRegister(MacroAssembler& masm,
64 ValOperandId op) {
65 OperandLocation& loc = operandLocations_[op.id()];
66
67 switch (loc.kind()) {
68 case OperandLocation::ValueReg:
69 currentOpRegs_.add(loc.valueReg());
70 return loc.valueReg();
71
72 case OperandLocation::ValueStack: {
73 ValueOperand reg = allocateValueRegister(masm);
74 popValue(masm, &loc, reg);
75 return reg;
76 }
77
78 case OperandLocation::BaselineFrame: {
79 ValueOperand reg = allocateValueRegister(masm);
80 Address addr = addressOf(masm, loc.baselineFrameSlot());
81 masm.loadValue(addr, reg);
82 loc.setValueReg(reg);
83 return reg;
84 }
85
86 case OperandLocation::Constant: {
87 ValueOperand reg = allocateValueRegister(masm);
88 masm.moveValue(loc.constant(), reg);
89 loc.setValueReg(reg);
90 return reg;
91 }
92
93 case OperandLocation::PayloadReg: {
94 // Temporarily add the payload register to currentOpRegs_ so
95 // allocateValueRegister will stay away from it.
96 currentOpRegs_.add(loc.payloadReg());
97 ValueOperand reg = allocateValueRegister(masm);
98 masm.tagValue(loc.payloadType(), loc.payloadReg(), reg);
99 currentOpRegs_.take(loc.payloadReg());
100 availableRegs_.add(loc.payloadReg());
101 loc.setValueReg(reg);
102 return reg;
103 }
104
105 case OperandLocation::PayloadStack: {
106 ValueOperand reg = allocateValueRegister(masm);
107 popPayload(masm, &loc, reg.scratchReg());
108 masm.tagValue(loc.payloadType(), reg.scratchReg(), reg);
109 loc.setValueReg(reg);
110 return reg;
111 }
112
113 case OperandLocation::DoubleReg: {
114 ValueOperand reg = allocateValueRegister(masm);
115 {
116 ScratchDoubleScope fpscratch(masm);
117 masm.boxDouble(loc.doubleReg(), reg, fpscratch);
118 }
119 loc.setValueReg(reg);
120 return reg;
121 }
122
123 case OperandLocation::Uninitialized:
124 break;
125 }
126
127 MOZ_CRASH();
128 }
129
130 // Load a value operand directly into a float register. Caller must have
131 // guarded isNumber on the provided val.
132 void CacheRegisterAllocator::ensureDoubleRegister(MacroAssembler& masm,
133 NumberOperandId op,
134 FloatRegister dest) const {
135 // If AutoScratchFloatRegister is active, we have to add sizeof(double) to
136 // any stack slot offsets below.
137 int32_t stackOffset = hasAutoScratchFloatRegisterSpill() ? sizeof(double) : 0;
138
139 const OperandLocation& loc = operandLocations_[op.id()];
140
141 Label failure, done;
142 switch (loc.kind()) {
143 case OperandLocation::ValueReg: {
144 masm.ensureDouble(loc.valueReg(), dest, &failure);
145 break;
146 }
147
148 case OperandLocation::ValueStack: {
149 Address addr = valueAddress(masm, &loc);
150 addr.offset += stackOffset;
151 masm.ensureDouble(addr, dest, &failure);
152 break;
153 }
154
155 case OperandLocation::BaselineFrame: {
156 Address addr = addressOf(masm, loc.baselineFrameSlot());
157 addr.offset += stackOffset;
158 masm.ensureDouble(addr, dest, &failure);
159 break;
160 }
161
162 case OperandLocation::DoubleReg: {
163 masm.moveDouble(loc.doubleReg(), dest);
164 return;
165 }
166
167 case OperandLocation::Constant: {
168 MOZ_ASSERT(loc.constant().isNumber(),
169 "Caller must ensure the operand is a number value");
170 masm.loadConstantDouble(loc.constant().toNumber(), dest);
171 return;
172 }
173
174 case OperandLocation::PayloadReg: {
175 // Doubles can't be stored in payload registers, so this must be an int32.
176 MOZ_ASSERT(loc.payloadType() == JSVAL_TYPE_INT32,
177 "Caller must ensure the operand is a number value");
178 masm.convertInt32ToDouble(loc.payloadReg(), dest);
179 return;
180 }
181
182 case OperandLocation::PayloadStack: {
183 // Doubles can't be stored in payload registers, so this must be an int32.
184 MOZ_ASSERT(loc.payloadType() == JSVAL_TYPE_INT32,
185 "Caller must ensure the operand is a number value");
186 MOZ_ASSERT(loc.payloadStack() <= stackPushed_);
187 Address addr = payloadAddress(masm, &loc);
188 addr.offset += stackOffset;
189 masm.convertInt32ToDouble(addr, dest);
190 return;
191 }
192
193 case OperandLocation::Uninitialized:
194 MOZ_CRASH("Unhandled operand type in ensureDoubleRegister");
195 return;
196 }
197 masm.jump(&done);
198 masm.bind(&failure);
199 masm.assumeUnreachable(
200 "Missing guard allowed non-number to hit ensureDoubleRegister");
201 masm.bind(&done);
202 }
203
204 void CacheRegisterAllocator::copyToScratchRegister(MacroAssembler& masm,
205 TypedOperandId typedId,
206 Register dest) const {
207 // If AutoScratchFloatRegister is active, we have to add sizeof(double) to
208 // any stack slot offsets below.
209 int32_t stackOffset = hasAutoScratchFloatRegisterSpill() ? sizeof(double) : 0;
210
211 const OperandLocation& loc = operandLocations_[typedId.id()];
212
213 switch (loc.kind()) {
214 case OperandLocation::ValueReg: {
215 masm.unboxNonDouble(loc.valueReg(), dest, typedId.type());
216 break;
217 }
218 case OperandLocation::ValueStack: {
219 Address addr = valueAddress(masm, &loc);
220 addr.offset += stackOffset;
221 masm.unboxNonDouble(addr, dest, typedId.type());
222 break;
223 }
224 case OperandLocation::BaselineFrame: {
225 Address addr = addressOf(masm, loc.baselineFrameSlot());
226 addr.offset += stackOffset;
227 masm.unboxNonDouble(addr, dest, typedId.type());
228 break;
229 }
230 case OperandLocation::PayloadReg: {
231 MOZ_ASSERT(loc.payloadType() == typedId.type());
232 masm.mov(loc.payloadReg(), dest);
233 return;
234 }
235 case OperandLocation::PayloadStack: {
236 MOZ_ASSERT(loc.payloadType() == typedId.type());
237 MOZ_ASSERT(loc.payloadStack() <= stackPushed_);
238 Address addr = payloadAddress(masm, &loc);
239 addr.offset += stackOffset;
240 masm.loadPtr(addr, dest);
241 return;
242 }
243 case OperandLocation::DoubleReg:
244 case OperandLocation::Constant:
245 case OperandLocation::Uninitialized:
246 MOZ_CRASH("Unhandled operand location");
247 }
248 }
249
250 void CacheRegisterAllocator::copyToScratchValueRegister(
251 MacroAssembler& masm, ValOperandId valId, ValueOperand dest) const {
252 MOZ_ASSERT(!addedFailurePath_);
253 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
254
255 const OperandLocation& loc = operandLocations_[valId.id()];
256 switch (loc.kind()) {
257 case OperandLocation::ValueReg:
258 masm.moveValue(loc.valueReg(), dest);
259 break;
260 case OperandLocation::ValueStack: {
261 Address addr = valueAddress(masm, &loc);
262 masm.loadValue(addr, dest);
263 break;
264 }
265 case OperandLocation::BaselineFrame: {
266 Address addr = addressOf(masm, loc.baselineFrameSlot());
267 masm.loadValue(addr, dest);
268 break;
269 }
270 case OperandLocation::Constant:
271 masm.moveValue(loc.constant(), dest);
272 break;
273 case OperandLocation::PayloadReg:
274 masm.tagValue(loc.payloadType(), loc.payloadReg(), dest);
275 break;
276 case OperandLocation::PayloadStack: {
277 Address addr = payloadAddress(masm, &loc);
278 masm.loadPtr(addr, dest.scratchReg());
279 masm.tagValue(loc.payloadType(), dest.scratchReg(), dest);
280 break;
281 }
282 case OperandLocation::DoubleReg: {
283 ScratchDoubleScope fpscratch(masm);
284 masm.boxDouble(loc.doubleReg(), dest, fpscratch);
285 break;
286 }
287 case OperandLocation::Uninitialized:
288 MOZ_CRASH();
289 }
290 }
291
292 Register CacheRegisterAllocator::useRegister(MacroAssembler& masm,
293 TypedOperandId typedId) {
294 MOZ_ASSERT(!addedFailurePath_);
295 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
296
297 OperandLocation& loc = operandLocations_[typedId.id()];
298 switch (loc.kind()) {
299 case OperandLocation::PayloadReg:
300 currentOpRegs_.add(loc.payloadReg());
301 return loc.payloadReg();
302
303 case OperandLocation::ValueReg: {
304 // It's possible the value is still boxed: as an optimization, we unbox
305 // the first time we use a value as object.
306 ValueOperand val = loc.valueReg();
307 availableRegs_.add(val);
308 Register reg = val.scratchReg();
309 availableRegs_.take(reg);
310 masm.unboxNonDouble(val, reg, typedId.type());
311 loc.setPayloadReg(reg, typedId.type());
312 currentOpRegs_.add(reg);
313 return reg;
314 }
315
316 case OperandLocation::PayloadStack: {
317 Register reg = allocateRegister(masm);
318 popPayload(masm, &loc, reg);
319 return reg;
320 }
321
322 case OperandLocation::ValueStack: {
323 // The value is on the stack, but boxed. If it's on top of the stack we
324 // unbox it and then remove it from the stack, else we just unbox.
325 Register reg = allocateRegister(masm);
326 if (loc.valueStack() == stackPushed_) {
327 masm.unboxNonDouble(Address(masm.getStackPointer(), 0), reg,
328 typedId.type());
329 masm.addToStackPtr(Imm32(sizeof(js::Value)));
330 MOZ_ASSERT(stackPushed_ >= sizeof(js::Value));
331 stackPushed_ -= sizeof(js::Value);
332 } else {
333 MOZ_ASSERT(loc.valueStack() < stackPushed_);
334 masm.unboxNonDouble(
335 Address(masm.getStackPointer(), stackPushed_ - loc.valueStack()),
336 reg, typedId.type());
337 }
338 loc.setPayloadReg(reg, typedId.type());
339 return reg;
340 }
341
342 case OperandLocation::BaselineFrame: {
343 Register reg = allocateRegister(masm);
344 Address addr = addressOf(masm, loc.baselineFrameSlot());
345 masm.unboxNonDouble(addr, reg, typedId.type());
346 loc.setPayloadReg(reg, typedId.type());
347 return reg;
348 };
349
350 case OperandLocation::Constant: {
351 Value v = loc.constant();
352 Register reg = allocateRegister(masm);
353 if (v.isString()) {
354 masm.movePtr(ImmGCPtr(v.toString()), reg);
355 } else if (v.isSymbol()) {
356 masm.movePtr(ImmGCPtr(v.toSymbol()), reg);
357 } else if (v.isBigInt()) {
358 masm.movePtr(ImmGCPtr(v.toBigInt()), reg);
359 } else if (v.isBoolean()) {
360 masm.movePtr(ImmWord(v.toBoolean() ? 1 : 0), reg);
361 } else {
362 MOZ_CRASH("Unexpected Value");
363 }
364 loc.setPayloadReg(reg, v.extractNonDoubleType());
365 return reg;
366 }
367
368 case OperandLocation::DoubleReg:
369 case OperandLocation::Uninitialized:
370 break;
371 }
372
373 MOZ_CRASH();
374 }
375
376 ConstantOrRegister CacheRegisterAllocator::useConstantOrRegister(
377 MacroAssembler& masm, ValOperandId val) {
378 MOZ_ASSERT(!addedFailurePath_);
379 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
380
381 OperandLocation& loc = operandLocations_[val.id()];
382 switch (loc.kind()) {
383 case OperandLocation::Constant:
384 return loc.constant();
385
386 case OperandLocation::PayloadReg:
387 case OperandLocation::PayloadStack: {
388 JSValueType payloadType = loc.payloadType();
389 Register reg = useRegister(masm, TypedOperandId(val, payloadType));
390 return TypedOrValueRegister(MIRTypeFromValueType(payloadType),
391 AnyRegister(reg));
392 }
393
394 case OperandLocation::ValueReg:
395 case OperandLocation::ValueStack:
396 case OperandLocation::BaselineFrame:
397 return TypedOrValueRegister(useValueRegister(masm, val));
398
399 case OperandLocation::DoubleReg:
400 return TypedOrValueRegister(MIRType::Double,
401 AnyRegister(loc.doubleReg()));
402
403 case OperandLocation::Uninitialized:
404 break;
405 }
406
407 MOZ_CRASH();
408 }
409
410 Register CacheRegisterAllocator::defineRegister(MacroAssembler& masm,
411 TypedOperandId typedId) {
412 MOZ_ASSERT(!addedFailurePath_);
413 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
414
415 OperandLocation& loc = operandLocations_[typedId.id()];
416 MOZ_ASSERT(loc.kind() == OperandLocation::Uninitialized);
417
418 Register reg = allocateRegister(masm);
419 loc.setPayloadReg(reg, typedId.type());
420 return reg;
421 }
422
423 ValueOperand CacheRegisterAllocator::defineValueRegister(MacroAssembler& masm,
424 ValOperandId val) {
425 MOZ_ASSERT(!addedFailurePath_);
426 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
427
428 OperandLocation& loc = operandLocations_[val.id()];
429 MOZ_ASSERT(loc.kind() == OperandLocation::Uninitialized);
430
431 ValueOperand reg = allocateValueRegister(masm);
432 loc.setValueReg(reg);
433 return reg;
434 }
435
436 void CacheRegisterAllocator::freeDeadOperandLocations(MacroAssembler& masm) {
437 // See if any operands are dead so we can reuse their registers. Note that
438 // we skip the input operands, as those are also used by failure paths, and
439 // we currently don't track those uses.
440 for (size_t i = writer_.numInputOperands(); i < operandLocations_.length();
441 i++) {
442 if (!writer_.operandIsDead(i, currentInstruction_)) {
443 continue;
444 }
445
446 OperandLocation& loc = operandLocations_[i];
447 switch (loc.kind()) {
448 case OperandLocation::PayloadReg:
449 availableRegs_.add(loc.payloadReg());
450 break;
451 case OperandLocation::ValueReg:
452 availableRegs_.add(loc.valueReg());
453 break;
454 case OperandLocation::PayloadStack:
455 masm.propagateOOM(freePayloadSlots_.append(loc.payloadStack()));
456 break;
457 case OperandLocation::ValueStack:
458 masm.propagateOOM(freeValueSlots_.append(loc.valueStack()));
459 break;
460 case OperandLocation::Uninitialized:
461 case OperandLocation::BaselineFrame:
462 case OperandLocation::Constant:
463 case OperandLocation::DoubleReg:
464 break;
465 }
466 loc.setUninitialized();
467 }
468 }
469
470 void CacheRegisterAllocator::discardStack(MacroAssembler& masm) {
471 // This should only be called when we are no longer using the operands,
472 // as we're discarding everything from the native stack. Set all operand
473 // locations to Uninitialized to catch bugs.
474 for (size_t i = 0; i < operandLocations_.length(); i++) {
475 operandLocations_[i].setUninitialized();
476 }
477
478 if (stackPushed_ > 0) {
479 masm.addToStackPtr(Imm32(stackPushed_));
480 stackPushed_ = 0;
481 }
482 freePayloadSlots_.clear();
483 freeValueSlots_.clear();
484 }
485
486 Register CacheRegisterAllocator::allocateRegister(MacroAssembler& masm) {
487 MOZ_ASSERT(!addedFailurePath_);
488 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
489
490 if (availableRegs_.empty()) {
491 freeDeadOperandLocations(masm);
492 }
493
494 if (availableRegs_.empty()) {
495 // Still no registers available, try to spill unused operands to
496 // the stack.
497 for (size_t i = 0; i < operandLocations_.length(); i++) {
498 OperandLocation& loc = operandLocations_[i];
499 if (loc.kind() == OperandLocation::PayloadReg) {
500 Register reg = loc.payloadReg();
501 if (currentOpRegs_.has(reg)) {
502 continue;
503 }
504
505 spillOperandToStack(masm, &loc);
506 availableRegs_.add(reg);
507 break; // We got a register, so break out of the loop.
508 }
509 if (loc.kind() == OperandLocation::ValueReg) {
510 ValueOperand reg = loc.valueReg();
511 if (currentOpRegs_.aliases(reg)) {
512 continue;
513 }
514
515 spillOperandToStack(masm, &loc);
516 availableRegs_.add(reg);
517 break; // Break out of the loop.
518 }
519 }
520 }
521
522 if (availableRegs_.empty() && !availableRegsAfterSpill_.empty()) {
523 Register reg = availableRegsAfterSpill_.takeAny();
524 masm.push(reg);
525 stackPushed_ += sizeof(uintptr_t);
526
527 masm.propagateOOM(spilledRegs_.append(SpilledRegister(reg, stackPushed_)));
528
529 availableRegs_.add(reg);
530 }
531
532 // At this point, there must be a free register.
533 MOZ_RELEASE_ASSERT(!availableRegs_.empty());
534
535 Register reg = availableRegs_.takeAny();
536 currentOpRegs_.add(reg);
537 return reg;
538 }
539
540 void CacheRegisterAllocator::allocateFixedRegister(MacroAssembler& masm,
541 Register reg) {
542 MOZ_ASSERT(!addedFailurePath_);
543 MOZ_ASSERT(!hasAutoScratchFloatRegisterSpill());
544
545 // Fixed registers should be allocated first, to ensure they're
546 // still available.
547 MOZ_ASSERT(!currentOpRegs_.has(reg), "Register is in use");
548
549 freeDeadOperandLocations(masm);
550
551 if (availableRegs_.has(reg)) {
552 availableRegs_.take(reg);
553 currentOpRegs_.add(reg);
554 return;
555 }
556
557 // Register may be available only after spilling contents.
558 if (availableRegsAfterSpill_.has(reg)) {
559 availableRegsAfterSpill_.take(reg);
560 masm.push(reg);
561 stackPushed_ += sizeof(uintptr_t);
562
563 masm.propagateOOM(spilledRegs_.append(SpilledRegister(reg, stackPushed_)));
564 currentOpRegs_.add(reg);
565 return;
566 }
567
568 // The register must be used by some operand. Spill it to the stack.
569 for (size_t i = 0; i < operandLocations_.length(); i++) {
570 OperandLocation& loc = operandLocations_[i];
571 if (loc.kind() == OperandLocation::PayloadReg) {
572 if (loc.payloadReg() != reg) {
573 continue;
574 }
575
576 spillOperandToStackOrRegister(masm, &loc);
577 currentOpRegs_.add(reg);
578 return;
579 }
580 if (loc.kind() == OperandLocation::ValueReg) {
581 if (!loc.valueReg().aliases(reg)) {
582 continue;
583 }
584
585 ValueOperand valueReg = loc.valueReg();
586 spillOperandToStackOrRegister(masm, &loc);
587
588 availableRegs_.add(valueReg);
589 availableRegs_.take(reg);
590 currentOpRegs_.add(reg);
591 return;
592 }
593 }
594
595 MOZ_CRASH("Invalid register");
596 }
597
598 void CacheRegisterAllocator::allocateFixedValueRegister(MacroAssembler& masm,
599 ValueOperand reg) {
600 #ifdef JS_NUNBOX32
601 allocateFixedRegister(masm, reg.payloadReg());
602 allocateFixedRegister(masm, reg.typeReg());
603 #else
604 allocateFixedRegister(masm, reg.valueReg());
605 #endif
606 }
607
608 #ifdef JS_NUNBOX32
609 // Possible miscompilation in clang-12 (bug 1689641)
610 MOZ_NEVER_INLINE
611 #endif
612 ValueOperand CacheRegisterAllocator::allocateValueRegister(
613 MacroAssembler& masm) {
614 #ifdef JS_NUNBOX32
615 Register reg1 = allocateRegister(masm);
616 Register reg2 = allocateRegister(masm);
617 return ValueOperand(reg1, reg2);
618 #else
619 Register reg = allocateRegister(masm);
620 return ValueOperand(reg);
621 #endif
622 }
623
624 bool CacheRegisterAllocator::init() {
625 if (!origInputLocations_.resize(writer_.numInputOperands())) {
626 return false;
627 }
628 if (!operandLocations_.resize(writer_.numOperandIds())) {
629 return false;
630 }
631 return true;
632 }
633
634 void CacheRegisterAllocator::initAvailableRegsAfterSpill() {
635 // Registers not in availableRegs_ and not used by input operands are
636 // available after being spilled.
637 availableRegsAfterSpill_.set() = GeneralRegisterSet::Intersect(
638 GeneralRegisterSet::Not(availableRegs_.set()),
639 GeneralRegisterSet::Not(inputRegisterSet()));
640 }
641
642 void CacheRegisterAllocator::fixupAliasedInputs(MacroAssembler& masm) {
643 // If IC inputs alias each other, make sure they are stored in different
644 // locations so we don't have to deal with this complexity in the rest of
645 // the allocator.
646 //
647 // Note that this can happen in IonMonkey with something like |o.foo = o|
648 // or |o[i] = i|.
649
650 size_t numInputs = writer_.numInputOperands();
651 MOZ_ASSERT(origInputLocations_.length() == numInputs);
652
653 for (size_t i = 1; i < numInputs; i++) {
654 OperandLocation& loc1 = operandLocations_[i];
655 if (!loc1.isInRegister()) {
656 continue;
657 }
658
659 for (size_t j = 0; j < i; j++) {
660 OperandLocation& loc2 = operandLocations_[j];
661 if (!loc1.aliasesReg(loc2)) {
662 continue;
663 }
664
665 // loc1 and loc2 alias so we spill one of them. If one is a
666 // ValueReg and the other is a PayloadReg, we have to spill the
667 // PayloadReg: spilling the ValueReg instead would leave its type
668 // register unallocated on 32-bit platforms.
669 if (loc1.kind() == OperandLocation::ValueReg) {
670 spillOperandToStack(masm, &loc2);
671 } else {
672 MOZ_ASSERT(loc1.kind() == OperandLocation::PayloadReg);
673 spillOperandToStack(masm, &loc1);
674 break; // Spilled loc1, so nothing else will alias it.
675 }
676 }
677 }
678
679 #ifdef DEBUG
680 assertValidState();
681 #endif
682 }
683
684 GeneralRegisterSet CacheRegisterAllocator::inputRegisterSet() const {
685 MOZ_ASSERT(origInputLocations_.length() == writer_.numInputOperands());
686
687 AllocatableGeneralRegisterSet result;
688 for (size_t i = 0; i < writer_.numInputOperands(); i++) {
689 const OperandLocation& loc = operandLocations_[i];
690 MOZ_ASSERT(loc == origInputLocations_[i]);
691
692 switch (loc.kind()) {
693 case OperandLocation::PayloadReg:
694 result.addUnchecked(loc.payloadReg());
695 continue;
696 case OperandLocation::ValueReg:
697 result.addUnchecked(loc.valueReg());
698 continue;
699 case OperandLocation::PayloadStack:
700 case OperandLocation::ValueStack:
701 case OperandLocation::BaselineFrame:
702 case OperandLocation::Constant:
703 case OperandLocation::DoubleReg:
704 continue;
705 case OperandLocation::Uninitialized:
706 break;
707 }
708 MOZ_CRASH("Invalid kind");
709 }
710
711 return result.set();
712 }
713
714 JSValueType CacheRegisterAllocator::knownType(ValOperandId val) const {
715 const OperandLocation& loc = operandLocations_[val.id()];
716
717 switch (loc.kind()) {
718 case OperandLocation::ValueReg:
719 case OperandLocation::ValueStack:
720 case OperandLocation::BaselineFrame:
721 return JSVAL_TYPE_UNKNOWN;
722
723 case OperandLocation::PayloadStack:
724 case OperandLocation::PayloadReg:
725 return loc.payloadType();
726
727 case OperandLocation::Constant:
728 return loc.constant().isDouble() ? JSVAL_TYPE_DOUBLE
729 : loc.constant().extractNonDoubleType();
730
731 case OperandLocation::DoubleReg:
732 return JSVAL_TYPE_DOUBLE;
733
734 case OperandLocation::Uninitialized:
735 break;
736 }
737
738 MOZ_CRASH("Invalid kind");
739 }
740
741 void CacheRegisterAllocator::initInputLocation(
742 size_t i, const TypedOrValueRegister& reg) {
743 if (reg.hasValue()) {
744 initInputLocation(i, reg.valueReg());
745 } else if (reg.typedReg().isFloat()) {
746 MOZ_ASSERT(reg.type() == MIRType::Double);
747 initInputLocation(i, reg.typedReg().fpu());
748 } else {
749 initInputLocation(i, reg.typedReg().gpr(),
750 ValueTypeFromMIRType(reg.type()));
751 }
752 }
753
754 void CacheRegisterAllocator::initInputLocation(
755 size_t i, const ConstantOrRegister& value) {
756 if (value.constant()) {
757 initInputLocation(i, value.value());
758 } else {
759 initInputLocation(i, value.reg());
760 }
761 }
762
763 void CacheRegisterAllocator::spillOperandToStack(MacroAssembler& masm,
764 OperandLocation* loc) {
765 MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
766
767 if (loc->kind() == OperandLocation::ValueReg) {
768 if (!freeValueSlots_.empty()) {
769 uint32_t stackPos = freeValueSlots_.popCopy();
770 MOZ_ASSERT(stackPos <= stackPushed_);
771 masm.storeValue(loc->valueReg(),
772 Address(masm.getStackPointer(), stackPushed_ - stackPos));
773 loc->setValueStack(stackPos);
774 return;
775 }
776 stackPushed_ += sizeof(js::Value);
777 masm.pushValue(loc->valueReg());
778 loc->setValueStack(stackPushed_);
779 return;
780 }
781
782 MOZ_ASSERT(loc->kind() == OperandLocation::PayloadReg);
783
784 if (!freePayloadSlots_.empty()) {
785 uint32_t stackPos = freePayloadSlots_.popCopy();
786 MOZ_ASSERT(stackPos <= stackPushed_);
787 masm.storePtr(loc->payloadReg(),
788 Address(masm.getStackPointer(), stackPushed_ - stackPos));
789 loc->setPayloadStack(stackPos, loc->payloadType());
790 return;
791 }
792 stackPushed_ += sizeof(uintptr_t);
793 masm.push(loc->payloadReg());
794 loc->setPayloadStack(stackPushed_, loc->payloadType());
795 }
796
797 void CacheRegisterAllocator::spillOperandToStackOrRegister(
798 MacroAssembler& masm, OperandLocation* loc) {
799 MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
800
801 // If enough registers are available, use them.
802 if (loc->kind() == OperandLocation::ValueReg) {
803 static const size_t BoxPieces = sizeof(Value) / sizeof(uintptr_t);
804 if (availableRegs_.set().size() >= BoxPieces) {
805 ValueOperand reg = availableRegs_.takeAnyValue();
806 masm.moveValue(loc->valueReg(), reg);
807 loc->setValueReg(reg);
808 return;
809 }
810 } else {
811 MOZ_ASSERT(loc->kind() == OperandLocation::PayloadReg);
812 if (!availableRegs_.empty()) {
813 Register reg = availableRegs_.takeAny();
814 masm.movePtr(loc->payloadReg(), reg);
815 loc->setPayloadReg(reg, loc->payloadType());
816 return;
817 }
818 }
819
820 // Not enough registers available, spill to the stack.
821 spillOperandToStack(masm, loc);
822 }
823
824 void CacheRegisterAllocator::popPayload(MacroAssembler& masm,
825 OperandLocation* loc, Register dest) {
826 MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
827 MOZ_ASSERT(stackPushed_ >= sizeof(uintptr_t));
828
829 // The payload is on the stack. If it's on top of the stack we can just
830 // pop it, else we emit a load.
831 if (loc->payloadStack() == stackPushed_) {
832 masm.pop(dest);
833 stackPushed_ -= sizeof(uintptr_t);
834 } else {
835 MOZ_ASSERT(loc->payloadStack() < stackPushed_);
836 masm.loadPtr(payloadAddress(masm, loc), dest);
837 masm.propagateOOM(freePayloadSlots_.append(loc->payloadStack()));
838 }
839
840 loc->setPayloadReg(dest, loc->payloadType());
841 }
842
843 Address CacheRegisterAllocator::valueAddress(MacroAssembler& masm,
844 const OperandLocation* loc) const {
845 MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
846 return Address(masm.getStackPointer(), stackPushed_ - loc->valueStack());
847 }
848
849 Address CacheRegisterAllocator::payloadAddress(
850 MacroAssembler& masm, const OperandLocation* loc) const {
851 MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
852 return Address(masm.getStackPointer(), stackPushed_ - loc->payloadStack());
853 }
854
855 void CacheRegisterAllocator::popValue(MacroAssembler& masm,
856 OperandLocation* loc, ValueOperand dest) {
857 MOZ_ASSERT(loc >= operandLocations_.begin() && loc < operandLocations_.end());
858 MOZ_ASSERT(stackPushed_ >= sizeof(js::Value));
859
860 // The Value is on the stack. If it's on top of the stack we can just
861 // pop it, else we emit a load.
862 if (loc->valueStack() == stackPushed_) {
863 masm.popValue(dest);
864 stackPushed_ -= sizeof(js::Value);
865 } else {
866 MOZ_ASSERT(loc->valueStack() < stackPushed_);
867 masm.loadValue(
868 Address(masm.getStackPointer(), stackPushed_ - loc->valueStack()),
869 dest);
870 masm.propagateOOM(freeValueSlots_.append(loc->valueStack()));
871 }
872
873 loc->setValueReg(dest);
874 }
875
876 #ifdef DEBUG
877 void CacheRegisterAllocator::assertValidState() const {
878 // Assert different operands don't have aliasing storage. We depend on this
879 // when spilling registers, for instance.
880
881 if (!JitOptions.fullDebugChecks) {
882 return;
883 }
884
885 for (size_t i = 0; i < operandLocations_.length(); i++) {
886 const auto& loc1 = operandLocations_[i];
887 if (loc1.isUninitialized()) {
888 continue;
889 }
890
891 for (size_t j = 0; j < i; j++) {
892 const auto& loc2 = operandLocations_[j];
893 if (loc2.isUninitialized()) {
894 continue;
895 }
896 MOZ_ASSERT(!loc1.aliasesReg(loc2));
897 }
898 }
899 }
900 #endif
901
902 bool OperandLocation::aliasesReg(const OperandLocation& other) const {
903 MOZ_ASSERT(&other != this);
904
905 switch (other.kind_) {
906 case PayloadReg:
907 return aliasesReg(other.payloadReg());
908 case ValueReg:
909 return aliasesReg(other.valueReg());
910 case PayloadStack:
911 case ValueStack:
912 case BaselineFrame:
913 case Constant:
914 case DoubleReg:
915 return false;
916 case Uninitialized:
917 break;
918 }
919
920 MOZ_CRASH("Invalid kind");
921 }
922
923 void CacheRegisterAllocator::restoreInputState(MacroAssembler& masm,
924 bool shouldDiscardStack) {
925 size_t numInputOperands = origInputLocations_.length();
926 MOZ_ASSERT(writer_.numInputOperands() == numInputOperands);
927
928 for (size_t j = 0; j < numInputOperands; j++) {
929 const OperandLocation& dest = origInputLocations_[j];
930 OperandLocation& cur = operandLocations_[j];
931 if (dest == cur) {
932 continue;
933 }
934
935 auto autoAssign = mozilla::MakeScopeExit([&] { cur = dest; });
936
937 // We have a cycle if a destination register will be used later
938 // as source register. If that happens, just push the current value
939 // on the stack and later get it from there.
940 for (size_t k = j + 1; k < numInputOperands; k++) {
941 OperandLocation& laterSource = operandLocations_[k];
942 if (dest.aliasesReg(laterSource)) {
943 spillOperandToStack(masm, &laterSource);
944 }
945 }
946
947 if (dest.kind() == OperandLocation::ValueReg) {
948 // We have to restore a Value register.
949 switch (cur.kind()) {
950 case OperandLocation::ValueReg:
951 masm.moveValue(cur.valueReg(), dest.valueReg());
952 continue;
953 case OperandLocation::PayloadReg:
954 masm.tagValue(cur.payloadType(), cur.payloadReg(), dest.valueReg());
955 continue;
956 case OperandLocation::PayloadStack: {
957 Register scratch = dest.valueReg().scratchReg();
958 popPayload(masm, &cur, scratch);
959 masm.tagValue(cur.payloadType(), scratch, dest.valueReg());
960 continue;
961 }
962 case OperandLocation::ValueStack:
963 popValue(masm, &cur, dest.valueReg());
964 continue;
965 case OperandLocation::DoubleReg:
966 masm.boxDouble(cur.doubleReg(), dest.valueReg(), cur.doubleReg());
967 continue;
968 case OperandLocation::Constant:
969 case OperandLocation::BaselineFrame:
970 case OperandLocation::Uninitialized:
971 break;
972 }
973 } else if (dest.kind() == OperandLocation::PayloadReg) {
974 // We have to restore a payload register.
975 switch (cur.kind()) {
976 case OperandLocation::ValueReg:
977 MOZ_ASSERT(dest.payloadType() != JSVAL_TYPE_DOUBLE);
978 masm.unboxNonDouble(cur.valueReg(), dest.payloadReg(),
979 dest.payloadType());
980 continue;
981 case OperandLocation::PayloadReg:
982 MOZ_ASSERT(cur.payloadType() == dest.payloadType());
983 masm.mov(cur.payloadReg(), dest.payloadReg());
984 continue;
985 case OperandLocation::PayloadStack: {
986 MOZ_ASSERT(cur.payloadType() == dest.payloadType());
987 popPayload(masm, &cur, dest.payloadReg());
988 continue;
989 }
990 case OperandLocation::ValueStack:
991 MOZ_ASSERT(stackPushed_ >= sizeof(js::Value));
992 MOZ_ASSERT(cur.valueStack() <= stackPushed_);
993 MOZ_ASSERT(dest.payloadType() != JSVAL_TYPE_DOUBLE);
994 masm.unboxNonDouble(
995 Address(masm.getStackPointer(), stackPushed_ - cur.valueStack()),
996 dest.payloadReg(), dest.payloadType());
997 continue;
998 case OperandLocation::Constant:
999 case OperandLocation::BaselineFrame:
1000 case OperandLocation::DoubleReg:
1001 case OperandLocation::Uninitialized:
1002 break;
1003 }
1004 } else if (dest.kind() == OperandLocation::Constant ||
1005 dest.kind() == OperandLocation::BaselineFrame ||
1006 dest.kind() == OperandLocation::DoubleReg) {
1007 // Nothing to do.
1008 continue;
1009 }
1010
1011 MOZ_CRASH("Invalid kind");
1012 }
1013
1014 for (const SpilledRegister& spill : spilledRegs_) {
1015 MOZ_ASSERT(stackPushed_ >= sizeof(uintptr_t));
1016
1017 if (spill.stackPushed == stackPushed_) {
1018 masm.pop(spill.reg);
1019 stackPushed_ -= sizeof(uintptr_t);
1020 } else {
1021 MOZ_ASSERT(spill.stackPushed < stackPushed_);
1022 masm.loadPtr(
1023 Address(masm.getStackPointer(), stackPushed_ - spill.stackPushed),
1024 spill.reg);
1025 }
1026 }
1027
1028 if (shouldDiscardStack) {
1029 discardStack(masm);
1030 }
1031 }
1032
1033 size_t CacheIRStubInfo::stubDataSize() const {
1034 size_t field = 0;
1035 size_t size = 0;
1036 while (true) {
1037 StubField::Type type = fieldType(field++);
1038 if (type == StubField::Type::Limit) {
1039 return size;
1040 }
1041 size += StubField::sizeInBytes(type);
1042 }
1043 }
1044
1045 template <typename T>
1046 static GCPtr<T>* AsGCPtr(void* ptr) {
1047 return static_cast<GCPtr<T>*>(ptr);
1048 }
1049
1050 void CacheIRStubInfo::replaceStubRawWord(uint8_t* stubData, uint32_t offset,
1051 uintptr_t oldWord,
1052 uintptr_t newWord) const {
1053 MOZ_ASSERT(uintptr_t(stubData + offset) % sizeof(uintptr_t) == 0);
1054 uintptr_t* addr = reinterpret_cast<uintptr_t*>(stubData + offset);
1055 MOZ_ASSERT(*addr == oldWord);
1056 *addr = newWord;
1057 }
1058
1059 void CacheIRStubInfo::replaceStubRawValueBits(uint8_t* stubData,
1060 uint32_t offset, uint64_t oldBits,
1061 uint64_t newBits) const {
1062 MOZ_ASSERT(uint64_t(stubData + offset) % sizeof(uint64_t) == 0);
1063 uint64_t* addr = reinterpret_cast<uint64_t*>(stubData + offset);
1064 MOZ_ASSERT(*addr == oldBits);
1065 *addr = newBits;
1066 }
1067
1068 template <class Stub, StubField::Type type>
1069 typename MapStubFieldToType<type>::WrappedType& CacheIRStubInfo::getStubField(
1070 Stub* stub, uint32_t offset) const {
1071 uint8_t* stubData = (uint8_t*)stub + stubDataOffset_;
1072 MOZ_ASSERT(uintptr_t(stubData + offset) % sizeof(uintptr_t) == 0);
1073
1074 using WrappedType = typename MapStubFieldToType<type>::WrappedType;
1075 return *reinterpret_cast<WrappedType*>(stubData + offset);
1076 }
1077
1078 #define INSTANTIATE_GET_STUB_FIELD(Type) \
1079 template typename MapStubFieldToType<Type>::WrappedType& \
1080 CacheIRStubInfo::getStubField<ICCacheIRStub, Type>(ICCacheIRStub * stub, \
1081 uint32_t offset) const;
1082 INSTANTIATE_GET_STUB_FIELD(StubField::Type::Shape)
1083 INSTANTIATE_GET_STUB_FIELD(StubField::Type::WeakShape)
1084 INSTANTIATE_GET_STUB_FIELD(StubField::Type::WeakGetterSetter)
1085 INSTANTIATE_GET_STUB_FIELD(StubField::Type::JSObject)
1086 INSTANTIATE_GET_STUB_FIELD(StubField::Type::WeakObject)
1087 INSTANTIATE_GET_STUB_FIELD(StubField::Type::Symbol)
1088 INSTANTIATE_GET_STUB_FIELD(StubField::Type::String)
1089 INSTANTIATE_GET_STUB_FIELD(StubField::Type::WeakBaseScript)
1090 INSTANTIATE_GET_STUB_FIELD(StubField::Type::Value)
1091 INSTANTIATE_GET_STUB_FIELD(StubField::Type::Id)
1092 #undef INSTANTIATE_GET_STUB_FIELD
1093
1094 template <class Stub, class T>
1095 T* CacheIRStubInfo::getPtrStubField(Stub* stub, uint32_t offset) const {
1096 uint8_t* stubData = (uint8_t*)stub + stubDataOffset_;
1097 MOZ_ASSERT(uintptr_t(stubData + offset) % sizeof(uintptr_t) == 0);
1098
1099 return *reinterpret_cast<T**>(stubData + offset);
1100 }
1101
1102 template gc::AllocSite* CacheIRStubInfo::getPtrStubField(ICCacheIRStub* stub,
1103 uint32_t offset) const;
1104
1105 template <StubField::Type type, typename V>
1106 static void InitWrappedPtr(void* ptr, V val) {
1107 using RawType = typename MapStubFieldToType<type>::RawType;
1108 using WrappedType = typename MapStubFieldToType<type>::WrappedType;
1109 auto* wrapped = static_cast<WrappedType*>(ptr);
1110 new (wrapped) WrappedType(mozilla::BitwiseCast<RawType>(val));
1111 }
1112
1113 static void InitWordStubField(StubField::Type type, void* dest,
1114 uintptr_t value) {
1115 MOZ_ASSERT(StubField::sizeIsWord(type));
1116 MOZ_ASSERT((uintptr_t(dest) % sizeof(uintptr_t)) == 0,
1117 "Unaligned stub field");
1118
1119 switch (type) {
1120 case StubField::Type::RawInt32:
1121 case StubField::Type::RawPointer:
1122 case StubField::Type::ICScript:
1123 case StubField::Type::AllocSite:
1124 *static_cast<uintptr_t*>(dest) = value;
1125 break;
1126 case StubField::Type::Shape:
1127 InitWrappedPtr<StubField::Type::Shape>(dest, value);
1128 break;
1129 case StubField::Type::WeakShape:
1130 // No read barrier required to copy weak pointer.
1131 InitWrappedPtr<StubField::Type::WeakShape>(dest, value);
1132 break;
1133 case StubField::Type::WeakGetterSetter:
1134 // No read barrier required to copy weak pointer.
1135 InitWrappedPtr<StubField::Type::WeakGetterSetter>(dest, value);
1136 break;
1137 case StubField::Type::JSObject:
1138 InitWrappedPtr<StubField::Type::JSObject>(dest, value);
1139 break;
1140 case StubField::Type::WeakObject:
1141 // No read barrier required to copy weak pointer.
1142 InitWrappedPtr<StubField::Type::WeakObject>(dest, value);
1143 break;
1144 case StubField::Type::Symbol:
1145 InitWrappedPtr<StubField::Type::Symbol>(dest, value);
1146 break;
1147 case StubField::Type::String:
1148 InitWrappedPtr<StubField::Type::String>(dest, value);
1149 break;
1150 case StubField::Type::WeakBaseScript:
1151 // No read barrier required to copy weak pointer.
1152 InitWrappedPtr<StubField::Type::WeakBaseScript>(dest, value);
1153 break;
1154 case StubField::Type::JitCode:
1155 InitWrappedPtr<StubField::Type::JitCode>(dest, value);
1156 break;
1157 case StubField::Type::Id:
1158 AsGCPtr<jsid>(dest)->init(jsid::fromRawBits(value));
1159 break;
1160 case StubField::Type::RawInt64:
1161 case StubField::Type::Double:
1162 case StubField::Type::Value:
1163 case StubField::Type::Limit:
1164 MOZ_CRASH("Invalid type");
1165 }
1166 }
1167
1168 static void InitInt64StubField(StubField::Type type, void* dest,
1169 uint64_t value) {
1170 MOZ_ASSERT(StubField::sizeIsInt64(type));
1171 MOZ_ASSERT((uintptr_t(dest) % sizeof(uint64_t)) == 0, "Unaligned stub field");
1172
1173 switch (type) {
1174 case StubField::Type::RawInt64:
1175 case StubField::Type::Double:
1176 *static_cast<uint64_t*>(dest) = value;
1177 break;
1178 case StubField::Type::Value:
1179 AsGCPtr<Value>(dest)->init(Value::fromRawBits(value));
1180 break;
1181 case StubField::Type::RawInt32:
1182 case StubField::Type::RawPointer:
1183 case StubField::Type::ICScript:
1184 case StubField::Type::AllocSite:
1185 case StubField::Type::Shape:
1186 case StubField::Type::WeakShape:
1187 case StubField::Type::WeakGetterSetter:
1188 case StubField::Type::JSObject:
1189 case StubField::Type::WeakObject:
1190 case StubField::Type::Symbol:
1191 case StubField::Type::String:
1192 case StubField::Type::WeakBaseScript:
1193 case StubField::Type::JitCode:
1194 case StubField::Type::Id:
1195 case StubField::Type::Limit:
1196 MOZ_CRASH("Invalid type");
1197 }
1198 }
1199
1200 void CacheIRWriter::copyStubData(uint8_t* dest) const {
1201 MOZ_ASSERT(!failed());
1202
1203 for (const StubField& field : stubFields_) {
1204 if (field.sizeIsWord()) {
1205 InitWordStubField(field.type(), dest, field.asWord());
1206 dest += sizeof(uintptr_t);
1207 } else {
1208 InitInt64StubField(field.type(), dest, field.asInt64());
1209 dest += sizeof(uint64_t);
1210 }
1211 }
1212 }
1213
1214 ICCacheIRStub* ICCacheIRStub::clone(JSRuntime* rt, ICStubSpace& newSpace,
1215 ICScriptHandling icScriptHandling) {
1216 const CacheIRStubInfo* info = stubInfo();
1217 MOZ_ASSERT(info->makesGCCalls());
1218
1219 size_t bytesNeeded = info->stubDataOffset() + info->stubDataSize();
1220
1221 AutoEnterOOMUnsafeRegion oomUnsafe;
1222 void* newStubMem = newSpace.alloc(bytesNeeded);
1223 if (!newStubMem) {
1224 oomUnsafe.crash("ICCacheIRStub::clone");
1225 }
1226
1227 ICCacheIRStub* newStub = new (newStubMem) ICCacheIRStub(*this);
1228
1229 const uint8_t* src = this->stubDataStart();
1230 uint8_t* dest = newStub->stubDataStart();
1231
1232 // Because this can be called during sweeping when discarding JIT code, we
1233 // have to lock the store buffer
1234 gc::AutoLockStoreBuffer lock(rt);
1235
1236 uint32_t field = 0;
1237 while (true) {
1238 StubField::Type type = info->fieldType(field);
1239 if (type == StubField::Type::Limit) {
1240 break; // Done.
1241 }
1242
1243 if (StubField::sizeIsWord(type)) {
1244 const uintptr_t* srcField = reinterpret_cast<const uintptr_t*>(src);
1245 InitWordStubField(type, dest, *srcField);
1246 src += sizeof(uintptr_t);
1247 dest += sizeof(uintptr_t);
1248 if (type == StubField::Type::ICScript) {
1249 auto* icScript = reinterpret_cast<ICScript*>(*srcField);
1250 if (icScriptHandling == ICScriptHandling::MarkActive) {
1251 icScript->setActive();
1252 } else {
1253 MOZ_ASSERT(icScriptHandling == ICScriptHandling::AssertActive);
1254 MOZ_RELEASE_ASSERT(icScript->active());
1255 }
1256 }
1257 } else {
1258 const uint64_t* srcField = reinterpret_cast<const uint64_t*>(src);
1259 InitInt64StubField(type, dest, *srcField);
1260 src += sizeof(uint64_t);
1261 dest += sizeof(uint64_t);
1262 }
1263
1264 field++;
1265 }
1266
1267 return newStub;
1268 }
1269
1270 template <typename T>
1271 static inline bool ShouldTraceWeakEdgeInStub(JSTracer* trc) {
1272 if constexpr (std::is_same_v<T, IonICStub>) {
1273 // 'Weak' edges are traced strongly in IonICs.
1274 return true;
1275 } else {
1276 static_assert(std::is_same_v<T, ICCacheIRStub>);
1277 return trc->traceWeakEdges();
1278 }
1279 }
1280
1281 template <typename T>
1282 void jit::TraceCacheIRStub(JSTracer* trc, T* stub,
1283 const CacheIRStubInfo* stubInfo) {
1284 using Type = StubField::Type;
1285
1286 uint32_t field = 0;
1287 size_t offset = 0;
1288 while (true) {
1289 Type fieldType = stubInfo->fieldType(field);
1290 switch (fieldType) {
1291 case Type::RawInt32:
1292 case Type::RawPointer:
1293 case Type::ICScript:
1294 case Type::RawInt64:
1295 case Type::Double:
1296 break;
1297 case Type::Shape: {
1298 // For CCW IC stubs, we can store same-zone but cross-compartment
1299 // shapes. Use TraceSameZoneCrossCompartmentEdge to not assert in the
1300 // GC. Note: CacheIRWriter::writeShapeField asserts we never store
1301 // cross-zone shapes.
1302 GCPtr<Shape*>& shapeField =
1303 stubInfo->getStubField<T, Type::Shape>(stub, offset);
1304 TraceSameZoneCrossCompartmentEdge(trc, &shapeField, "cacheir-shape");
1305 break;
1306 }
1307 case Type::WeakShape:
1308 if (ShouldTraceWeakEdgeInStub<T>(trc)) {
1309 WeakHeapPtr<Shape*>& shapeField =
1310 stubInfo->getStubField<T, Type::WeakShape>(stub, offset);
1311 if (shapeField) {
1312 TraceSameZoneCrossCompartmentEdge(trc, &shapeField,
1313 "cacheir-weak-shape");
1314 }
1315 }
1316 break;
1317 case Type::WeakGetterSetter:
1318 if (ShouldTraceWeakEdgeInStub<T>(trc)) {
1319 TraceNullableEdge(
1320 trc,
1321 &stubInfo->getStubField<T, Type::WeakGetterSetter>(stub, offset),
1322 "cacheir-weak-getter-setter");
1323 }
1324 break;
1325 case Type::JSObject: {
1326 TraceEdge(trc, &stubInfo->getStubField<T, Type::JSObject>(stub, offset),
1327 "cacheir-object");
1328 break;
1329 }
1330 case Type::WeakObject:
1331 if (ShouldTraceWeakEdgeInStub<T>(trc)) {
1332 TraceNullableEdge(
1333 trc, &stubInfo->getStubField<T, Type::WeakObject>(stub, offset),
1334 "cacheir-weak-object");
1335 }
1336 break;
1337 case Type::Symbol:
1338 TraceEdge(trc, &stubInfo->getStubField<T, Type::Symbol>(stub, offset),
1339 "cacheir-symbol");
1340 break;
1341 case Type::String:
1342 TraceEdge(trc, &stubInfo->getStubField<T, Type::String>(stub, offset),
1343 "cacheir-string");
1344 break;
1345 case Type::WeakBaseScript:
1346 if (ShouldTraceWeakEdgeInStub<T>(trc)) {
1347 TraceNullableEdge(
1348 trc,
1349 &stubInfo->getStubField<T, Type::WeakBaseScript>(stub, offset),
1350 "cacheir-weak-script");
1351 }
1352 break;
1353 case Type::JitCode:
1354 TraceEdge(trc, &stubInfo->getStubField<T, Type::JitCode>(stub, offset),
1355 "cacheir-jitcode");
1356 break;
1357 case Type::Id:
1358 TraceEdge(trc, &stubInfo->getStubField<T, Type::Id>(stub, offset),
1359 "cacheir-id");
1360 break;
1361 case Type::Value:
1362 TraceEdge(trc, &stubInfo->getStubField<T, Type::Value>(stub, offset),
1363 "cacheir-value");
1364 break;
1365 case Type::AllocSite: {
1366 gc::AllocSite* site =
1367 stubInfo->getPtrStubField<T, gc::AllocSite>(stub, offset);
1368 site->trace(trc);
1369 break;
1370 }
1371 case Type::Limit:
1372 return; // Done.
1373 }
1374 field++;
1375 offset += StubField::sizeInBytes(fieldType);
1376 }
1377 }
1378
1379 template void jit::TraceCacheIRStub(JSTracer* trc, ICCacheIRStub* stub,
1380 const CacheIRStubInfo* stubInfo);
1381
1382 template void jit::TraceCacheIRStub(JSTracer* trc, IonICStub* stub,
1383 const CacheIRStubInfo* stubInfo);
1384
1385 template <typename T>
1386 bool jit::TraceWeakCacheIRStub(JSTracer* trc, T* stub,
1387 const CacheIRStubInfo* stubInfo) {
1388 using Type = StubField::Type;
1389
1390 // Trace all fields before returning because this stub can be traced again
1391 // later through TraceBaselineStubFrame.
1392 bool isDead = false;
1393
1394 uint32_t field = 0;
1395 size_t offset = 0;
1396 while (true) {
1397 Type fieldType = stubInfo->fieldType(field);
1398 switch (fieldType) {
1399 case Type::WeakShape: {
1400 WeakHeapPtr<Shape*>& shapeField =
1401 stubInfo->getStubField<T, Type::WeakShape>(stub, offset);
1402 auto r = TraceWeakEdge(trc, &shapeField, "cacheir-weak-shape");
1403 if (r.isDead()) {
1404 isDead = true;
1405 }
1406 break;
1407 }
1408 case Type::WeakObject: {
1409 WeakHeapPtr<JSObject*>& objectField =
1410 stubInfo->getStubField<T, Type::WeakObject>(stub, offset);
1411 auto r = TraceWeakEdge(trc, &objectField, "cacheir-weak-object");
1412 if (r.isDead()) {
1413 isDead = true;
1414 }
1415 break;
1416 }
1417 case Type::WeakBaseScript: {
1418 WeakHeapPtr<BaseScript*>& scriptField =
1419 stubInfo->getStubField<T, Type::WeakBaseScript>(stub, offset);
1420 auto r = TraceWeakEdge(trc, &scriptField, "cacheir-weak-script");
1421 if (r.isDead()) {
1422 isDead = true;
1423 }
1424 break;
1425 }
1426 case Type::WeakGetterSetter: {
1427 WeakHeapPtr<GetterSetter*>& getterSetterField =
1428 stubInfo->getStubField<T, Type::WeakGetterSetter>(stub, offset);
1429 auto r = TraceWeakEdge(trc, &getterSetterField,
1430 "cacheir-weak-getter-setter");
1431 if (r.isDead()) {
1432 isDead = true;
1433 }
1434 break;
1435 }
1436 case Type::Limit:
1437 // Done.
1438 return !isDead;
1439 case Type::RawInt32:
1440 case Type::RawPointer:
1441 case Type::ICScript:
1442 case Type::Shape:
1443 case Type::JSObject:
1444 case Type::Symbol:
1445 case Type::String:
1446 case Type::JitCode:
1447 case Type::Id:
1448 case Type::AllocSite:
1449 case Type::RawInt64:
1450 case Type::Value:
1451 case Type::Double:
1452 break; // Skip non-weak fields.
1453 }
1454 field++;
1455 offset += StubField::sizeInBytes(fieldType);
1456 }
1457 }
1458
1459 template bool jit::TraceWeakCacheIRStub(JSTracer* trc, ICCacheIRStub* stub,
1460 const CacheIRStubInfo* stubInfo);
1461
1462 template bool jit::TraceWeakCacheIRStub(JSTracer* trc, IonICStub* stub,
1463 const CacheIRStubInfo* stubInfo);
1464
1465 bool CacheIRWriter::stubDataEquals(const uint8_t* stubData) const {
1466 MOZ_ASSERT(!failed());
1467
1468 const uintptr_t* stubDataWords = reinterpret_cast<const uintptr_t*>(stubData);
1469
1470 for (const StubField& field : stubFields_) {
1471 if (field.sizeIsWord()) {
1472 if (field.asWord() != *stubDataWords) {
1473 return false;
1474 }
1475 stubDataWords++;
1476 continue;
1477 }
1478
1479 if (field.asInt64() != *reinterpret_cast<const uint64_t*>(stubDataWords)) {
1480 return false;
1481 }
1482 stubDataWords += sizeof(uint64_t) / sizeof(uintptr_t);
1483 }
1484
1485 return true;
1486 }
1487
1488 bool CacheIRWriter::stubDataEqualsIgnoring(const uint8_t* stubData,
1489 uint32_t ignoreOffset) const {
1490 MOZ_ASSERT(!failed());
1491
1492 uint32_t offset = 0;
1493 for (const StubField& field : stubFields_) {
1494 if (offset != ignoreOffset) {
1495 if (field.sizeIsWord()) {
1496 uintptr_t raw = *reinterpret_cast<const uintptr_t*>(stubData + offset);
1497 if (field.asWord() != raw) {
1498 return false;
1499 }
1500 } else {
1501 uint64_t raw = *reinterpret_cast<const uint64_t*>(stubData + offset);
1502 if (field.asInt64() != raw) {
1503 return false;
1504 }
1505 }
1506 }
1507 offset += StubField::sizeInBytes(field.type());
1508 }
1509
1510 return true;
1511 }
1512
1513 HashNumber CacheIRStubKey::hash(const CacheIRStubKey::Lookup& l) {
1514 HashNumber hash = mozilla::HashBytes(l.code, l.length);
1515 hash = mozilla::AddToHash(hash, uint32_t(l.kind));
1516 hash = mozilla::AddToHash(hash, uint32_t(l.engine));
1517 return hash;
1518 }
1519
1520 bool CacheIRStubKey::match(const CacheIRStubKey& entry,
1521 const CacheIRStubKey::Lookup& l) {
1522 if (entry.stubInfo->kind() != l.kind) {
1523 return false;
1524 }
1525
1526 if (entry.stubInfo->engine() != l.engine) {
1527 return false;
1528 }
1529
1530 if (entry.stubInfo->codeLength() != l.length) {
1531 return false;
1532 }
1533
1534 if (!mozilla::ArrayEqual(entry.stubInfo->code(), l.code, l.length)) {
1535 return false;
1536 }
1537
1538 return true;
1539 }
1540
1541 CacheIRReader::CacheIRReader(const CacheIRStubInfo* stubInfo)
1542 : CacheIRReader(stubInfo->code(),
1543 stubInfo->code() + stubInfo->codeLength()) {}
1544
1545 CacheIRStubInfo* CacheIRStubInfo::New(CacheKind kind, ICStubEngine engine,
1546 bool makesGCCalls,
1547 uint32_t stubDataOffset,
1548 const CacheIRWriter& writer) {
1549 size_t numStubFields = writer.numStubFields();
1550 size_t bytesNeeded =
1551 sizeof(CacheIRStubInfo) + writer.codeLength() +
1552 (numStubFields + 1); // +1 for the GCType::Limit terminator.
1553 uint8_t* p = js_pod_malloc<uint8_t>(bytesNeeded);
1554 if (!p) {
1555 return nullptr;
1556 }
1557
1558 // Copy the CacheIR code.
1559 uint8_t* codeStart = p + sizeof(CacheIRStubInfo);
1560 mozilla::PodCopy(codeStart, writer.codeStart(), writer.codeLength());
1561
1562 static_assert(sizeof(StubField::Type) == sizeof(uint8_t),
1563 "StubField::Type must fit in uint8_t");
1564
1565 // Copy the stub field types.
1566 uint8_t* fieldTypes = codeStart + writer.codeLength();
1567 for (size_t i = 0; i < numStubFields; i++) {
1568 fieldTypes[i] = uint8_t(writer.stubFieldType(i));
1569 }
1570 fieldTypes[numStubFields] = uint8_t(StubField::Type::Limit);
1571
1572 return new (p) CacheIRStubInfo(kind, engine, makesGCCalls, stubDataOffset,
1573 writer.codeLength());
1574 }
1575
1576 bool OperandLocation::operator==(const OperandLocation& other) const {
1577 if (kind_ != other.kind_) {
1578 return false;
1579 }
1580
1581 switch (kind()) {
1582 case Uninitialized:
1583 return true;
1584 case PayloadReg:
1585 return payloadReg() == other.payloadReg() &&
1586 payloadType() == other.payloadType();
1587 case ValueReg:
1588 return valueReg() == other.valueReg();
1589 case PayloadStack:
1590 return payloadStack() == other.payloadStack() &&
1591 payloadType() == other.payloadType();
1592 case ValueStack:
1593 return valueStack() == other.valueStack();
1594 case BaselineFrame:
1595 return baselineFrameSlot() == other.baselineFrameSlot();
1596 case Constant:
1597 return constant() == other.constant();
1598 case DoubleReg:
1599 return doubleReg() == other.doubleReg();
1600 }
1601
1602 MOZ_CRASH("Invalid OperandLocation kind");
1603 }
1604
1605 AutoOutputRegister::AutoOutputRegister(CacheIRCompiler& compiler)
1606 : output_(compiler.outputUnchecked_.ref()), alloc_(compiler.allocator) {
1607 if (output_.hasValue()) {
1608 alloc_.allocateFixedValueRegister(compiler.masm, output_.valueReg());
1609 } else if (!output_.typedReg().isFloat()) {
1610 alloc_.allocateFixedRegister(compiler.masm, output_.typedReg().gpr());
1611 }
1612 }
1613
1614 AutoOutputRegister::~AutoOutputRegister() {
1615 if (output_.hasValue()) {
1616 alloc_.releaseValueRegister(output_.valueReg());
1617 } else if (!output_.typedReg().isFloat()) {
1618 alloc_.releaseRegister(output_.typedReg().gpr());
1619 }
1620 }
1621
1622 bool FailurePath::canShareFailurePath(const FailurePath& other) const {
1623 if (stackPushed_ != other.stackPushed_) {
1624 return false;
1625 }
1626
1627 if (spilledRegs_.length() != other.spilledRegs_.length()) {
1628 return false;
1629 }
1630
1631 for (size_t i = 0; i < spilledRegs_.length(); i++) {
1632 if (spilledRegs_[i] != other.spilledRegs_[i]) {
1633 return false;
1634 }
1635 }
1636
1637 MOZ_ASSERT(inputs_.length() == other.inputs_.length());
1638
1639 for (size_t i = 0; i < inputs_.length(); i++) {
1640 if (inputs_[i] != other.inputs_[i]) {
1641 return false;
1642 }
1643 }
1644 return true;
1645 }
1646
1647 bool CacheIRCompiler::addFailurePath(FailurePath** failure) {
1648 #ifdef DEBUG
1649 allocator.setAddedFailurePath();
1650 #endif
1651 MOZ_ASSERT(!allocator.hasAutoScratchFloatRegisterSpill());
1652
1653 FailurePath newFailure;
1654 for (size_t i = 0; i < writer_.numInputOperands(); i++) {
1655 if (!newFailure.appendInput(allocator.operandLocation(i))) {
1656 return false;
1657 }
1658 }
1659 if (!newFailure.setSpilledRegs(allocator.spilledRegs())) {
1660 return false;
1661 }
1662 newFailure.setStackPushed(allocator.stackPushed());
1663
1664 // Reuse the previous failure path if the current one is the same, to
1665 // avoid emitting duplicate code.
1666 if (failurePaths.length() > 0 &&
1667 failurePaths.back().canShareFailurePath(newFailure)) {
1668 *failure = &failurePaths.back();
1669 return true;
1670 }
1671
1672 if (!failurePaths.append(std::move(newFailure))) {
1673 return false;
1674 }
1675
1676 *failure = &failurePaths.back();
1677 return true;
1678 }
1679
1680 bool CacheIRCompiler::emitFailurePath(size_t index) {
1681 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1682 FailurePath& failure = failurePaths[index];
1683
1684 allocator.setStackPushed(failure.stackPushed());
1685
1686 for (size_t i = 0; i < writer_.numInputOperands(); i++) {
1687 allocator.setOperandLocation(i, failure.input(i));
1688 }
1689
1690 if (!allocator.setSpilledRegs(failure.spilledRegs())) {
1691 return false;
1692 }
1693
1694 masm.bind(failure.label());
1695 allocator.restoreInputState(masm);
1696 return true;
1697 }
1698
1699 bool CacheIRCompiler::emitGuardIsNumber(ValOperandId inputId) {
1700 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1701 JSValueType knownType = allocator.knownType(inputId);
1702
1703 // Doubles and ints are numbers!
1704 if (knownType == JSVAL_TYPE_DOUBLE || knownType == JSVAL_TYPE_INT32) {
1705 return true;
1706 }
1707
1708 ValueOperand input = allocator.useValueRegister(masm, inputId);
1709 FailurePath* failure;
1710 if (!addFailurePath(&failure)) {
1711 return false;
1712 }
1713
1714 masm.branchTestNumber(Assembler::NotEqual, input, failure->label());
1715 return true;
1716 }
1717
1718 bool CacheIRCompiler::emitGuardToObject(ValOperandId inputId) {
1719 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1720 if (allocator.knownType(inputId) == JSVAL_TYPE_OBJECT) {
1721 return true;
1722 }
1723
1724 ValueOperand input = allocator.useValueRegister(masm, inputId);
1725 FailurePath* failure;
1726 if (!addFailurePath(&failure)) {
1727 return false;
1728 }
1729 masm.branchTestObject(Assembler::NotEqual, input, failure->label());
1730 return true;
1731 }
1732
1733 bool CacheIRCompiler::emitGuardIsNullOrUndefined(ValOperandId inputId) {
1734 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1735 JSValueType knownType = allocator.knownType(inputId);
1736 if (knownType == JSVAL_TYPE_UNDEFINED || knownType == JSVAL_TYPE_NULL) {
1737 return true;
1738 }
1739
1740 ValueOperand input = allocator.useValueRegister(masm, inputId);
1741 FailurePath* failure;
1742 if (!addFailurePath(&failure)) {
1743 return false;
1744 }
1745
1746 Label success;
1747 masm.branchTestNull(Assembler::Equal, input, &success);
1748 masm.branchTestUndefined(Assembler::NotEqual, input, failure->label());
1749
1750 masm.bind(&success);
1751 return true;
1752 }
1753
1754 bool CacheIRCompiler::emitGuardIsNull(ValOperandId inputId) {
1755 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1756 JSValueType knownType = allocator.knownType(inputId);
1757 if (knownType == JSVAL_TYPE_NULL) {
1758 return true;
1759 }
1760
1761 ValueOperand input = allocator.useValueRegister(masm, inputId);
1762 FailurePath* failure;
1763 if (!addFailurePath(&failure)) {
1764 return false;
1765 }
1766
1767 masm.branchTestNull(Assembler::NotEqual, input, failure->label());
1768 return true;
1769 }
1770
1771 bool CacheIRCompiler::emitGuardIsUndefined(ValOperandId inputId) {
1772 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1773 JSValueType knownType = allocator.knownType(inputId);
1774 if (knownType == JSVAL_TYPE_UNDEFINED) {
1775 return true;
1776 }
1777
1778 ValueOperand input = allocator.useValueRegister(masm, inputId);
1779 FailurePath* failure;
1780 if (!addFailurePath(&failure)) {
1781 return false;
1782 }
1783
1784 masm.branchTestUndefined(Assembler::NotEqual, input, failure->label());
1785 return true;
1786 }
1787
1788 bool CacheIRCompiler::emitGuardIsNotUninitializedLexical(ValOperandId valId) {
1789 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1790
1791 ValueOperand val = allocator.useValueRegister(masm, valId);
1792
1793 FailurePath* failure;
1794 if (!addFailurePath(&failure)) {
1795 return false;
1796 }
1797
1798 masm.branchTestMagicValue(Assembler::Equal, val, JS_UNINITIALIZED_LEXICAL,
1799 failure->label());
1800 return true;
1801 }
1802
1803 bool CacheIRCompiler::emitGuardBooleanToInt32(ValOperandId inputId,
1804 Int32OperandId resultId) {
1805 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1806 Register output = allocator.defineRegister(masm, resultId);
1807
1808 if (allocator.knownType(inputId) == JSVAL_TYPE_BOOLEAN) {
1809 Register input =
1810 allocator.useRegister(masm, BooleanOperandId(inputId.id()));
1811 masm.move32(input, output);
1812 return true;
1813 }
1814 ValueOperand input = allocator.useValueRegister(masm, inputId);
1815
1816 FailurePath* failure;
1817 if (!addFailurePath(&failure)) {
1818 return false;
1819 }
1820
1821 masm.fallibleUnboxBoolean(input, output, failure->label());
1822 return true;
1823 }
1824
1825 bool CacheIRCompiler::emitGuardToString(ValOperandId inputId) {
1826 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1827 if (allocator.knownType(inputId) == JSVAL_TYPE_STRING) {
1828 return true;
1829 }
1830
1831 ValueOperand input = allocator.useValueRegister(masm, inputId);
1832 FailurePath* failure;
1833 if (!addFailurePath(&failure)) {
1834 return false;
1835 }
1836 masm.branchTestString(Assembler::NotEqual, input, failure->label());
1837 return true;
1838 }
1839
1840 bool CacheIRCompiler::emitGuardToSymbol(ValOperandId inputId) {
1841 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1842 if (allocator.knownType(inputId) == JSVAL_TYPE_SYMBOL) {
1843 return true;
1844 }
1845
1846 ValueOperand input = allocator.useValueRegister(masm, inputId);
1847 FailurePath* failure;
1848 if (!addFailurePath(&failure)) {
1849 return false;
1850 }
1851 masm.branchTestSymbol(Assembler::NotEqual, input, failure->label());
1852 return true;
1853 }
1854
1855 bool CacheIRCompiler::emitGuardToBigInt(ValOperandId inputId) {
1856 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1857 if (allocator.knownType(inputId) == JSVAL_TYPE_BIGINT) {
1858 return true;
1859 }
1860
1861 ValueOperand input = allocator.useValueRegister(masm, inputId);
1862 FailurePath* failure;
1863 if (!addFailurePath(&failure)) {
1864 return false;
1865 }
1866 masm.branchTestBigInt(Assembler::NotEqual, input, failure->label());
1867 return true;
1868 }
1869
1870 bool CacheIRCompiler::emitGuardToBoolean(ValOperandId inputId) {
1871 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1872
1873 if (allocator.knownType(inputId) == JSVAL_TYPE_BOOLEAN) {
1874 return true;
1875 }
1876
1877 ValueOperand input = allocator.useValueRegister(masm, inputId);
1878 FailurePath* failure;
1879 if (!addFailurePath(&failure)) {
1880 return false;
1881 }
1882 masm.branchTestBoolean(Assembler::NotEqual, input, failure->label());
1883 return true;
1884 }
1885
1886 bool CacheIRCompiler::emitGuardToInt32(ValOperandId inputId) {
1887 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1888
1889 if (allocator.knownType(inputId) == JSVAL_TYPE_INT32) {
1890 return true;
1891 }
1892
1893 ValueOperand input = allocator.useValueRegister(masm, inputId);
1894
1895 FailurePath* failure;
1896 if (!addFailurePath(&failure)) {
1897 return false;
1898 }
1899
1900 masm.branchTestInt32(Assembler::NotEqual, input, failure->label());
1901 return true;
1902 }
1903
1904 bool CacheIRCompiler::emitGuardToNonGCThing(ValOperandId inputId) {
1905 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1906
1907 ValueOperand input = allocator.useValueRegister(masm, inputId);
1908
1909 FailurePath* failure;
1910 if (!addFailurePath(&failure)) {
1911 return false;
1912 }
1913
1914 masm.branchTestGCThing(Assembler::Equal, input, failure->label());
1915 return true;
1916 }
1917
1918 // Infallible |emitDouble| emitters can use this implementation to avoid
1919 // generating extra clean-up instructions to restore the scratch float register.
1920 // To select this function simply omit the |Label* fail| parameter for the
1921 // emitter lambda function.
1922 template <typename EmitDouble>
1923 static std::enable_if_t<mozilla::FunctionTypeTraits<EmitDouble>::arity == 1,
1924 void>
1925 EmitGuardDouble(CacheIRCompiler* compiler, MacroAssembler& masm,
1926 ValueOperand input, FailurePath* failure,
1927 EmitDouble emitDouble) {
1928 AutoScratchFloatRegister floatReg(compiler);
1929
1930 masm.unboxDouble(input, floatReg);
1931 emitDouble(floatReg.get());
1932 }
1933
1934 template <typename EmitDouble>
1935 static std::enable_if_t<mozilla::FunctionTypeTraits<EmitDouble>::arity == 2,
1936 void>
1937 EmitGuardDouble(CacheIRCompiler* compiler, MacroAssembler& masm,
1938 ValueOperand input, FailurePath* failure,
1939 EmitDouble emitDouble) {
1940 AutoScratchFloatRegister floatReg(compiler, failure);
1941
1942 masm.unboxDouble(input, floatReg);
1943 emitDouble(floatReg.get(), floatReg.failure());
1944 }
1945
1946 template <typename EmitInt32, typename EmitDouble>
1947 static void EmitGuardInt32OrDouble(CacheIRCompiler* compiler,
1948 MacroAssembler& masm, ValueOperand input,
1949 Register output, FailurePath* failure,
1950 EmitInt32 emitInt32, EmitDouble emitDouble) {
1951 Label done;
1952
1953 {
1954 ScratchTagScope tag(masm, input);
1955 masm.splitTagForTest(input, tag);
1956
1957 Label notInt32;
1958 masm.branchTestInt32(Assembler::NotEqual, tag, &notInt32);
1959 {
1960 ScratchTagScopeRelease _(&tag);
1961
1962 masm.unboxInt32(input, output);
1963 emitInt32();
1964
1965 masm.jump(&done);
1966 }
1967 masm.bind(&notInt32);
1968
1969 masm.branchTestDouble(Assembler::NotEqual, tag, failure->label());
1970 {
1971 ScratchTagScopeRelease _(&tag);
1972
1973 EmitGuardDouble(compiler, masm, input, failure, emitDouble);
1974 }
1975 }
1976
1977 masm.bind(&done);
1978 }
1979
1980 bool CacheIRCompiler::emitGuardToInt32Index(ValOperandId inputId,
1981 Int32OperandId resultId) {
1982 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
1983 Register output = allocator.defineRegister(masm, resultId);
1984
1985 if (allocator.knownType(inputId) == JSVAL_TYPE_INT32) {
1986 Register input = allocator.useRegister(masm, Int32OperandId(inputId.id()));
1987 masm.move32(input, output);
1988 return true;
1989 }
1990
1991 ValueOperand input = allocator.useValueRegister(masm, inputId);
1992
1993 FailurePath* failure;
1994 if (!addFailurePath(&failure)) {
1995 return false;
1996 }
1997
1998 EmitGuardInt32OrDouble(
1999 this, masm, input, output, failure,
2000 []() {
2001 // No-op if the value is already an int32.
2002 },
2003 [&](FloatRegister floatReg, Label* fail) {
2004 // ToPropertyKey(-0.0) is "0", so we can truncate -0.0 to 0 here.
2005 masm.convertDoubleToInt32(floatReg, output, fail, false);
2006 });
2007
2008 return true;
2009 }
2010
2011 bool CacheIRCompiler::emitInt32ToIntPtr(Int32OperandId inputId,
2012 IntPtrOperandId resultId) {
2013 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2014
2015 Register input = allocator.useRegister(masm, inputId);
2016 Register output = allocator.defineRegister(masm, resultId);
2017
2018 masm.move32SignExtendToPtr(input, output);
2019 return true;
2020 }
2021
2022 bool CacheIRCompiler::emitGuardNumberToIntPtrIndex(NumberOperandId inputId,
2023 bool supportOOB,
2024 IntPtrOperandId resultId) {
2025 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2026
2027 Register output = allocator.defineRegister(masm, resultId);
2028
2029 FailurePath* failure = nullptr;
2030 if (!supportOOB) {
2031 if (!addFailurePath(&failure)) {
2032 return false;
2033 }
2034 }
2035
2036 AutoScratchFloatRegister floatReg(this, failure);
2037 allocator.ensureDoubleRegister(masm, inputId, floatReg);
2038
2039 // ToPropertyKey(-0.0) is "0", so we can truncate -0.0 to 0 here.
2040 if (supportOOB) {
2041 Label done, fail;
2042 masm.convertDoubleToPtr(floatReg, output, &fail, false);
2043 masm.jump(&done);
2044
2045 // Substitute the invalid index with an arbitrary out-of-bounds index.
2046 masm.bind(&fail);
2047 masm.movePtr(ImmWord(-1), output);
2048
2049 masm.bind(&done);
2050 } else {
2051 masm.convertDoubleToPtr(floatReg, output, floatReg.failure(), false);
2052 }
2053
2054 return true;
2055 }
2056
2057 static void TruncateDoubleModUint32(MacroAssembler& masm,
2058 FloatRegister floatReg, Register result,
2059 const LiveRegisterSet& liveVolatileRegs) {
2060 Label truncateABICall;
2061 masm.branchTruncateDoubleMaybeModUint32(floatReg, result, &truncateABICall);
2062
2063 if (truncateABICall.used()) {
2064 Label done;
2065 masm.jump(&done);
2066
2067 masm.bind(&truncateABICall);
2068 LiveRegisterSet save = liveVolatileRegs;
2069 save.takeUnchecked(floatReg);
2070 // Bug 1451976
2071 save.takeUnchecked(floatReg.asSingle());
2072 masm.PushRegsInMask(save);
2073
2074 using Fn = int32_t (*)(double);
2075 masm.setupUnalignedABICall(result);
2076 masm.passABIArg(floatReg, ABIType::Float64);
2077 masm.callWithABI<Fn, JS::ToInt32>(ABIType::General,
2078 CheckUnsafeCallWithABI::DontCheckOther);
2079 masm.storeCallInt32Result(result);
2080
2081 LiveRegisterSet ignore;
2082 ignore.add(result);
2083 masm.PopRegsInMaskIgnore(save, ignore);
2084
2085 masm.bind(&done);
2086 }
2087 }
2088
2089 bool CacheIRCompiler::emitGuardToInt32ModUint32(ValOperandId inputId,
2090 Int32OperandId resultId) {
2091 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2092 Register output = allocator.defineRegister(masm, resultId);
2093
2094 if (allocator.knownType(inputId) == JSVAL_TYPE_INT32) {
2095 ConstantOrRegister input = allocator.useConstantOrRegister(masm, inputId);
2096 if (input.constant()) {
2097 masm.move32(Imm32(input.value().toInt32()), output);
2098 } else {
2099 MOZ_ASSERT(input.reg().type() == MIRType::Int32);
2100 masm.move32(input.reg().typedReg().gpr(), output);
2101 }
2102 return true;
2103 }
2104
2105 ValueOperand input = allocator.useValueRegister(masm, inputId);
2106
2107 FailurePath* failure;
2108 if (!addFailurePath(&failure)) {
2109 return false;
2110 }
2111
2112 EmitGuardInt32OrDouble(
2113 this, masm, input, output, failure,
2114 []() {
2115 // No-op if the value is already an int32.
2116 },
2117 [&](FloatRegister floatReg) {
2118 TruncateDoubleModUint32(masm, floatReg, output, liveVolatileRegs());
2119 });
2120
2121 return true;
2122 }
2123
2124 bool CacheIRCompiler::emitGuardToUint8Clamped(ValOperandId inputId,
2125 Int32OperandId resultId) {
2126 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2127 Register output = allocator.defineRegister(masm, resultId);
2128
2129 if (allocator.knownType(inputId) == JSVAL_TYPE_INT32) {
2130 ConstantOrRegister input = allocator.useConstantOrRegister(masm, inputId);
2131 if (input.constant()) {
2132 masm.move32(Imm32(ClampDoubleToUint8(input.value().toInt32())), output);
2133 } else {
2134 MOZ_ASSERT(input.reg().type() == MIRType::Int32);
2135 masm.move32(input.reg().typedReg().gpr(), output);
2136 masm.clampIntToUint8(output);
2137 }
2138 return true;
2139 }
2140
2141 ValueOperand input = allocator.useValueRegister(masm, inputId);
2142
2143 FailurePath* failure;
2144 if (!addFailurePath(&failure)) {
2145 return false;
2146 }
2147
2148 EmitGuardInt32OrDouble(
2149 this, masm, input, output, failure,
2150 [&]() {
2151 // |output| holds the unboxed int32 value.
2152 masm.clampIntToUint8(output);
2153 },
2154 [&](FloatRegister floatReg) {
2155 masm.clampDoubleToUint8(floatReg, output);
2156 });
2157
2158 return true;
2159 }
2160
2161 bool CacheIRCompiler::emitGuardNonDoubleType(ValOperandId inputId,
2162 ValueType type) {
2163 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2164
2165 if (allocator.knownType(inputId) == JSValueType(type)) {
2166 return true;
2167 }
2168
2169 ValueOperand input = allocator.useValueRegister(masm, inputId);
2170
2171 FailurePath* failure;
2172 if (!addFailurePath(&failure)) {
2173 return false;
2174 }
2175
2176 switch (type) {
2177 case ValueType::String:
2178 masm.branchTestString(Assembler::NotEqual, input, failure->label());
2179 break;
2180 case ValueType::Symbol:
2181 masm.branchTestSymbol(Assembler::NotEqual, input, failure->label());
2182 break;
2183 case ValueType::BigInt:
2184 masm.branchTestBigInt(Assembler::NotEqual, input, failure->label());
2185 break;
2186 case ValueType::Int32:
2187 masm.branchTestInt32(Assembler::NotEqual, input, failure->label());
2188 break;
2189 case ValueType::Boolean:
2190 masm.branchTestBoolean(Assembler::NotEqual, input, failure->label());
2191 break;
2192 case ValueType::Undefined:
2193 masm.branchTestUndefined(Assembler::NotEqual, input, failure->label());
2194 break;
2195 case ValueType::Null:
2196 masm.branchTestNull(Assembler::NotEqual, input, failure->label());
2197 break;
2198 case ValueType::Double:
2199 case ValueType::Magic:
2200 case ValueType::PrivateGCThing:
2201 case ValueType::Object:
2202 MOZ_CRASH("unexpected type");
2203 }
2204
2205 return true;
2206 }
2207
2208 static const JSClass* ClassFor(JSContext* cx, GuardClassKind kind) {
2209 switch (kind) {
2210 case GuardClassKind::Array:
2211 case GuardClassKind::PlainObject:
2212 case GuardClassKind::FixedLengthArrayBuffer:
2213 case GuardClassKind::ResizableArrayBuffer:
2214 case GuardClassKind::FixedLengthSharedArrayBuffer:
2215 case GuardClassKind::GrowableSharedArrayBuffer:
2216 case GuardClassKind::FixedLengthDataView:
2217 case GuardClassKind::ResizableDataView:
2218 case GuardClassKind::MappedArguments:
2219 case GuardClassKind::UnmappedArguments:
2220 case GuardClassKind::Set:
2221 case GuardClassKind::Map:
2222 case GuardClassKind::BoundFunction:
2223 case GuardClassKind::Date:
2224 return ClassFor(kind);
2225 case GuardClassKind::WindowProxy:
2226 return cx->runtime()->maybeWindowProxyClass();
2227 case GuardClassKind::JSFunction:
2228 MOZ_CRASH("must be handled by caller");
2229 }
2230 MOZ_CRASH("unexpected kind");
2231 }
2232
2233 bool CacheIRCompiler::emitGuardClass(ObjOperandId objId, GuardClassKind kind) {
2234 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2235 Register obj = allocator.useRegister(masm, objId);
2236 AutoScratchRegister scratch(allocator, masm);
2237
2238 FailurePath* failure;
2239 if (!addFailurePath(&failure)) {
2240 return false;
2241 }
2242
2243 if (kind == GuardClassKind::JSFunction) {
2244 if (objectGuardNeedsSpectreMitigations(objId)) {
2245 masm.branchTestObjIsFunction(Assembler::NotEqual, obj, scratch, obj,
2246 failure->label());
2247 } else {
2248 masm.branchTestObjIsFunctionNoSpectreMitigations(
2249 Assembler::NotEqual, obj, scratch, failure->label());
2250 }
2251 return true;
2252 }
2253
2254 const JSClass* clasp = ClassFor(cx_, kind);
2255 MOZ_ASSERT(clasp);
2256
2257 if (objectGuardNeedsSpectreMitigations(objId)) {
2258 masm.branchTestObjClass(Assembler::NotEqual, obj, clasp, scratch, obj,
2259 failure->label());
2260 } else {
2261 masm.branchTestObjClassNoSpectreMitigations(Assembler::NotEqual, obj, clasp,
2262 scratch, failure->label());
2263 }
2264
2265 return true;
2266 }
2267
2268 bool CacheIRCompiler::emitGuardEitherClass(ObjOperandId objId,
2269 GuardClassKind kind1,
2270 GuardClassKind kind2) {
2271 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2272 Register obj = allocator.useRegister(masm, objId);
2273 AutoScratchRegister scratch(allocator, masm);
2274
2275 FailurePath* failure;
2276 if (!addFailurePath(&failure)) {
2277 return false;
2278 }
2279
2280 // We don't yet need this case, so it's unsupported for now.
2281 MOZ_ASSERT(kind1 != GuardClassKind::JSFunction &&
2282 kind2 != GuardClassKind::JSFunction);
2283
2284 const JSClass* clasp1 = ClassFor(cx_, kind1);
2285 MOZ_ASSERT(clasp1);
2286
2287 const JSClass* clasp2 = ClassFor(cx_, kind2);
2288 MOZ_ASSERT(clasp2);
2289
2290 if (objectGuardNeedsSpectreMitigations(objId)) {
2291 masm.branchTestObjClass(Assembler::NotEqual, obj, {clasp1, clasp2}, scratch,
2292 obj, failure->label());
2293 } else {
2294 masm.branchTestObjClassNoSpectreMitigations(
2295 Assembler::NotEqual, obj, {clasp1, clasp2}, scratch, failure->label());
2296 }
2297
2298 return true;
2299 }
2300
2301 bool CacheIRCompiler::emitGuardNullProto(ObjOperandId objId) {
2302 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2303 Register obj = allocator.useRegister(masm, objId);
2304 AutoScratchRegister scratch(allocator, masm);
2305
2306 FailurePath* failure;
2307 if (!addFailurePath(&failure)) {
2308 return false;
2309 }
2310
2311 masm.loadObjProto(obj, scratch);
2312 masm.branchTestPtr(Assembler::NonZero, scratch, scratch, failure->label());
2313 return true;
2314 }
2315
2316 bool CacheIRCompiler::emitGuardIsExtensible(ObjOperandId objId) {
2317 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2318 Register obj = allocator.useRegister(masm, objId);
2319 AutoScratchRegister scratch(allocator, masm);
2320
2321 FailurePath* failure;
2322 if (!addFailurePath(&failure)) {
2323 return false;
2324 }
2325
2326 masm.branchIfObjectNotExtensible(obj, scratch, failure->label());
2327 return true;
2328 }
2329
2330 bool CacheIRCompiler::emitGuardDynamicSlotIsSpecificObject(
2331 ObjOperandId objId, ObjOperandId expectedId, uint32_t slotOffset) {
2332 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2333 Register obj = allocator.useRegister(masm, objId);
2334 Register expectedObject = allocator.useRegister(masm, expectedId);
2335
2336 // Allocate registers before the failure path to make sure they're registered
2337 // by addFailurePath.
2338 AutoScratchRegister scratch1(allocator, masm);
2339 AutoScratchRegister scratch2(allocator, masm);
2340
2341 FailurePath* failure;
2342 if (!addFailurePath(&failure)) {
2343 return false;
2344 }
2345
2346 // Guard on the expected object.
2347 StubFieldOffset slot(slotOffset, StubField::Type::RawInt32);
2348 masm.loadPtr(Address(obj, NativeObject::offsetOfSlots()), scratch1);
2349 emitLoadStubField(slot, scratch2);
2350 BaseObjectSlotIndex expectedSlot(scratch1, scratch2);
2351 masm.fallibleUnboxObject(expectedSlot, scratch1, failure->label());
2352 masm.branchPtr(Assembler::NotEqual, expectedObject, scratch1,
2353 failure->label());
2354
2355 return true;
2356 }
2357
2358 bool CacheIRCompiler::emitGuardDynamicSlotIsNotObject(ObjOperandId objId,
2359 uint32_t slotOffset) {
2360 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2361 Register obj = allocator.useRegister(masm, objId);
2362
2363 AutoScratchRegister scratch1(allocator, masm);
2364 AutoScratchRegister scratch2(allocator, masm);
2365
2366 FailurePath* failure;
2367 if (!addFailurePath(&failure)) {
2368 return false;
2369 }
2370
2371 // Guard that the slot isn't an object.
2372 StubFieldOffset slot(slotOffset, StubField::Type::RawInt32);
2373 masm.loadPtr(Address(obj, NativeObject::offsetOfSlots()), scratch1);
2374 emitLoadStubField(slot, scratch2);
2375 BaseObjectSlotIndex expectedSlot(scratch1, scratch2);
2376 masm.branchTestObject(Assembler::Equal, expectedSlot, failure->label());
2377
2378 return true;
2379 }
2380
2381 bool CacheIRCompiler::emitGuardFixedSlotValue(ObjOperandId objId,
2382 uint32_t offsetOffset,
2383 uint32_t valOffset) {
2384 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2385
2386 Register obj = allocator.useRegister(masm, objId);
2387
2388 AutoScratchRegister scratch(allocator, masm);
2389 AutoScratchValueRegister scratchVal(allocator, masm);
2390
2391 FailurePath* failure;
2392 if (!addFailurePath(&failure)) {
2393 return false;
2394 }
2395
2396 StubFieldOffset offset(offsetOffset, StubField::Type::RawInt32);
2397 emitLoadStubField(offset, scratch);
2398
2399 StubFieldOffset val(valOffset, StubField::Type::Value);
2400 emitLoadValueStubField(val, scratchVal);
2401
2402 BaseIndex slotVal(obj, scratch, TimesOne);
2403 masm.branchTestValue(Assembler::NotEqual, slotVal, scratchVal,
2404 failure->label());
2405 return true;
2406 }
2407
2408 bool CacheIRCompiler::emitGuardDynamicSlotValue(ObjOperandId objId,
2409 uint32_t offsetOffset,
2410 uint32_t valOffset) {
2411 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2412
2413 Register obj = allocator.useRegister(masm, objId);
2414
2415 AutoScratchRegister scratch1(allocator, masm);
2416 AutoScratchRegister scratch2(allocator, masm);
2417 AutoScratchValueRegister scratchVal(allocator, masm);
2418
2419 FailurePath* failure;
2420 if (!addFailurePath(&failure)) {
2421 return false;
2422 }
2423
2424 masm.loadPtr(Address(obj, NativeObject::offsetOfSlots()), scratch1);
2425
2426 StubFieldOffset offset(offsetOffset, StubField::Type::RawInt32);
2427 emitLoadStubField(offset, scratch2);
2428
2429 StubFieldOffset val(valOffset, StubField::Type::Value);
2430 emitLoadValueStubField(val, scratchVal);
2431
2432 BaseIndex slotVal(scratch1, scratch2, TimesOne);
2433 masm.branchTestValue(Assembler::NotEqual, slotVal, scratchVal,
2434 failure->label());
2435 return true;
2436 }
2437
2438 bool CacheIRCompiler::emitLoadScriptedProxyHandler(ObjOperandId resultId,
2439 ObjOperandId objId) {
2440 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2441
2442 Register obj = allocator.useRegister(masm, objId);
2443 Register output = allocator.defineRegister(masm, resultId);
2444
2445 FailurePath* failure;
2446 if (!addFailurePath(&failure)) {
2447 return false;
2448 }
2449
2450 masm.loadPtr(Address(obj, ProxyObject::offsetOfReservedSlots()), output);
2451 Address handlerAddr(output, js::detail::ProxyReservedSlots::offsetOfSlot(
2452 ScriptedProxyHandler::HANDLER_EXTRA));
2453 masm.fallibleUnboxObject(handlerAddr, output, failure->label());
2454
2455 return true;
2456 }
2457
2458 bool CacheIRCompiler::emitIdToStringOrSymbol(ValOperandId resultId,
2459 ValOperandId idId) {
2460 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2461
2462 ValueOperand id = allocator.useValueRegister(masm, idId);
2463 ValueOperand output = allocator.defineValueRegister(masm, resultId);
2464 AutoScratchRegister scratch(allocator, masm);
2465
2466 FailurePath* failure;
2467 if (!addFailurePath(&failure)) {
2468 return false;
2469 }
2470
2471 masm.moveValue(id, output);
2472
2473 Label done, intDone, callVM;
2474 {
2475 ScratchTagScope tag(masm, output);
2476 masm.splitTagForTest(output, tag);
2477 masm.branchTestString(Assembler::Equal, tag, &done);
2478 masm.branchTestSymbol(Assembler::Equal, tag, &done);
2479 masm.branchTestInt32(Assembler::NotEqual, tag, failure->label());
2480 }
2481
2482 Register intReg = output.scratchReg();
2483 masm.unboxInt32(output, intReg);
2484
2485 // Fast path for small integers.
2486 masm.lookupStaticIntString(intReg, intReg, scratch, cx_->staticStrings(),
2487 &callVM);
2488 masm.jump(&intDone);
2489
2490 masm.bind(&callVM);
2491 LiveRegisterSet volatileRegs = liveVolatileRegs();
2492 masm.PushRegsInMask(volatileRegs);
2493
2494 using Fn = JSLinearString* (*)(JSContext* cx, int32_t i);
2495 masm.setupUnalignedABICall(scratch);
2496 masm.loadJSContext(scratch);
2497 masm.passABIArg(scratch);
2498 masm.passABIArg(intReg);
2499 masm.callWithABI<Fn, js::Int32ToStringPure>();
2500
2501 masm.storeCallPointerResult(intReg);
2502
2503 LiveRegisterSet ignore;
2504 ignore.add(intReg);
2505 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
2506
2507 masm.branchPtr(Assembler::Equal, intReg, ImmPtr(nullptr), failure->label());
2508
2509 masm.bind(&intDone);
2510 masm.tagValue(JSVAL_TYPE_STRING, intReg, output);
2511 masm.bind(&done);
2512
2513 return true;
2514 }
2515
2516 bool CacheIRCompiler::emitLoadFixedSlot(ValOperandId resultId,
2517 ObjOperandId objId,
2518 uint32_t offsetOffset) {
2519 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2520
2521 ValueOperand output = allocator.defineValueRegister(masm, resultId);
2522 Register obj = allocator.useRegister(masm, objId);
2523 AutoScratchRegister scratch(allocator, masm);
2524
2525 StubFieldOffset slotIndex(offsetOffset, StubField::Type::RawInt32);
2526 emitLoadStubField(slotIndex, scratch);
2527
2528 masm.loadValue(BaseIndex(obj, scratch, TimesOne), output);
2529 return true;
2530 }
2531
2532 bool CacheIRCompiler::emitLoadDynamicSlot(ValOperandId resultId,
2533 ObjOperandId objId,
2534 uint32_t slotOffset) {
2535 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2536
2537 ValueOperand output = allocator.defineValueRegister(masm, resultId);
2538 Register obj = allocator.useRegister(masm, objId);
2539 AutoScratchRegister scratch1(allocator, masm);
2540 Register scratch2 = output.scratchReg();
2541
2542 StubFieldOffset slotIndex(slotOffset, StubField::Type::RawInt32);
2543 emitLoadStubField(slotIndex, scratch2);
2544
2545 masm.loadPtr(Address(obj, NativeObject::offsetOfSlots()), scratch1);
2546 masm.loadValue(BaseObjectSlotIndex(scratch1, scratch2), output);
2547 return true;
2548 }
2549
2550 bool CacheIRCompiler::emitGuardIsNativeObject(ObjOperandId objId) {
2551 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2552
2553 Register obj = allocator.useRegister(masm, objId);
2554 AutoScratchRegister scratch(allocator, masm);
2555
2556 FailurePath* failure;
2557 if (!addFailurePath(&failure)) {
2558 return false;
2559 }
2560
2561 masm.branchIfNonNativeObj(obj, scratch, failure->label());
2562 return true;
2563 }
2564
2565 bool CacheIRCompiler::emitGuardIsProxy(ObjOperandId objId) {
2566 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2567
2568 Register obj = allocator.useRegister(masm, objId);
2569 AutoScratchRegister scratch(allocator, masm);
2570
2571 FailurePath* failure;
2572 if (!addFailurePath(&failure)) {
2573 return false;
2574 }
2575
2576 masm.branchTestObjectIsProxy(false, obj, scratch, failure->label());
2577 return true;
2578 }
2579
2580 bool CacheIRCompiler::emitGuardIsNotProxy(ObjOperandId objId) {
2581 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2582
2583 Register obj = allocator.useRegister(masm, objId);
2584 AutoScratchRegister scratch(allocator, masm);
2585
2586 FailurePath* failure;
2587 if (!addFailurePath(&failure)) {
2588 return false;
2589 }
2590
2591 masm.branchTestObjectIsProxy(true, obj, scratch, failure->label());
2592 return true;
2593 }
2594
2595 bool CacheIRCompiler::emitGuardIsNotArrayBufferMaybeShared(ObjOperandId objId) {
2596 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2597
2598 Register obj = allocator.useRegister(masm, objId);
2599 AutoScratchRegister scratch(allocator, masm);
2600
2601 FailurePath* failure;
2602 if (!addFailurePath(&failure)) {
2603 return false;
2604 }
2605
2606 masm.loadObjClassUnsafe(obj, scratch);
2607 masm.branchPtr(Assembler::Equal, scratch,
2608 ImmPtr(&FixedLengthArrayBufferObject::class_),
2609 failure->label());
2610 masm.branchPtr(Assembler::Equal, scratch,
2611 ImmPtr(&FixedLengthSharedArrayBufferObject::class_),
2612 failure->label());
2613 masm.branchPtr(Assembler::Equal, scratch,
2614 ImmPtr(&ResizableArrayBufferObject::class_), failure->label());
2615 masm.branchPtr(Assembler::Equal, scratch,
2616 ImmPtr(&GrowableSharedArrayBufferObject::class_),
2617 failure->label());
2618 return true;
2619 }
2620
2621 bool CacheIRCompiler::emitGuardIsTypedArray(ObjOperandId objId) {
2622 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2623
2624 Register obj = allocator.useRegister(masm, objId);
2625 AutoScratchRegister scratch(allocator, masm);
2626
2627 FailurePath* failure;
2628 if (!addFailurePath(&failure)) {
2629 return false;
2630 }
2631
2632 masm.loadObjClassUnsafe(obj, scratch);
2633 masm.branchIfClassIsNotTypedArray(scratch, failure->label());
2634 return true;
2635 }
2636
2637 bool CacheIRCompiler::emitGuardIsFixedLengthTypedArray(ObjOperandId objId) {
2638 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2639
2640 Register obj = allocator.useRegister(masm, objId);
2641 AutoScratchRegister scratch(allocator, masm);
2642
2643 FailurePath* failure;
2644 if (!addFailurePath(&failure)) {
2645 return false;
2646 }
2647
2648 masm.loadObjClassUnsafe(obj, scratch);
2649 masm.branchIfClassIsNotFixedLengthTypedArray(scratch, failure->label());
2650 return true;
2651 }
2652
2653 bool CacheIRCompiler::emitGuardIsResizableTypedArray(ObjOperandId objId) {
2654 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2655
2656 Register obj = allocator.useRegister(masm, objId);
2657 AutoScratchRegister scratch(allocator, masm);
2658
2659 FailurePath* failure;
2660 if (!addFailurePath(&failure)) {
2661 return false;
2662 }
2663
2664 masm.loadObjClassUnsafe(obj, scratch);
2665 masm.branchIfClassIsNotResizableTypedArray(scratch, failure->label());
2666 return true;
2667 }
2668
2669 bool CacheIRCompiler::emitGuardIsNotDOMProxy(ObjOperandId objId) {
2670 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2671 Register obj = allocator.useRegister(masm, objId);
2672 AutoScratchRegister scratch(allocator, masm);
2673
2674 FailurePath* failure;
2675 if (!addFailurePath(&failure)) {
2676 return false;
2677 }
2678
2679 masm.branchTestProxyHandlerFamily(Assembler::Equal, obj, scratch,
2680 GetDOMProxyHandlerFamily(),
2681 failure->label());
2682 return true;
2683 }
2684
2685 bool CacheIRCompiler::emitGuardNoDenseElements(ObjOperandId objId) {
2686 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2687 Register obj = allocator.useRegister(masm, objId);
2688 AutoScratchRegister scratch(allocator, masm);
2689
2690 FailurePath* failure;
2691 if (!addFailurePath(&failure)) {
2692 return false;
2693 }
2694
2695 // Load obj->elements.
2696 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
2697
2698 // Make sure there are no dense elements.
2699 Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
2700 masm.branch32(Assembler::NotEqual, initLength, Imm32(0), failure->label());
2701 return true;
2702 }
2703
2704 bool CacheIRCompiler::emitGuardSpecificInt32(Int32OperandId numId,
2705 int32_t expected) {
2706 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2707 Register num = allocator.useRegister(masm, numId);
2708
2709 FailurePath* failure;
2710 if (!addFailurePath(&failure)) {
2711 return false;
2712 }
2713
2714 masm.branch32(Assembler::NotEqual, num, Imm32(expected), failure->label());
2715 return true;
2716 }
2717
2718 bool CacheIRCompiler::emitGuardStringToInt32(StringOperandId strId,
2719 Int32OperandId resultId) {
2720 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2721 Register str = allocator.useRegister(masm, strId);
2722 Register output = allocator.defineRegister(masm, resultId);
2723 AutoScratchRegister scratch(allocator, masm);
2724
2725 FailurePath* failure;
2726 if (!addFailurePath(&failure)) {
2727 return false;
2728 }
2729
2730 masm.guardStringToInt32(str, output, scratch, liveVolatileRegs(),
2731 failure->label());
2732 return true;
2733 }
2734
2735 bool CacheIRCompiler::emitGuardStringToNumber(StringOperandId strId,
2736 NumberOperandId resultId) {
2737 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2738 Register str = allocator.useRegister(masm, strId);
2739 ValueOperand output = allocator.defineValueRegister(masm, resultId);
2740 AutoScratchRegister scratch(allocator, masm);
2741
2742 FailurePath* failure;
2743 if (!addFailurePath(&failure)) {
2744 return false;
2745 }
2746
2747 Label vmCall, done;
2748 // Use indexed value as fast path if possible.
2749 masm.loadStringIndexValue(str, scratch, &vmCall);
2750 masm.tagValue(JSVAL_TYPE_INT32, scratch, output);
2751 masm.jump(&done);
2752 {
2753 masm.bind(&vmCall);
2754
2755 // Reserve stack for holding the result value of the call.
2756 masm.reserveStack(sizeof(double));
2757 masm.moveStackPtrTo(output.payloadOrValueReg());
2758
2759 // We cannot use callVM, as callVM expects to be able to clobber all
2760 // operands, however, since this op is not the last in the generated IC, we
2761 // want to be able to reference other live values.
2762 LiveRegisterSet volatileRegs = liveVolatileRegs();
2763 masm.PushRegsInMask(volatileRegs);
2764
2765 using Fn = bool (*)(JSContext* cx, JSString* str, double* result);
2766 masm.setupUnalignedABICall(scratch);
2767 masm.loadJSContext(scratch);
2768 masm.passABIArg(scratch);
2769 masm.passABIArg(str);
2770 masm.passABIArg(output.payloadOrValueReg());
2771 masm.callWithABI<Fn, js::StringToNumberPure>();
2772 masm.storeCallPointerResult(scratch);
2773
2774 LiveRegisterSet ignore;
2775 ignore.add(scratch);
2776 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
2777
2778 Label ok;
2779 masm.branchIfTrueBool(scratch, &ok);
2780 {
2781 // OOM path, recovered by StringToNumberPure.
2782 //
2783 // Use addToStackPtr instead of freeStack as freeStack tracks stack height
2784 // flow-insensitively, and using it twice would confuse the stack height
2785 // tracking.
2786 masm.addToStackPtr(Imm32(sizeof(double)));
2787 masm.jump(failure->label());
2788 }
2789 masm.bind(&ok);
2790
2791 {
2792 ScratchDoubleScope fpscratch(masm);
2793 masm.loadDouble(Address(output.payloadOrValueReg(), 0), fpscratch);
2794 masm.boxDouble(fpscratch, output, fpscratch);
2795 }
2796 masm.freeStack(sizeof(double));
2797 }
2798 masm.bind(&done);
2799 return true;
2800 }
2801
2802 bool CacheIRCompiler::emitNumberParseIntResult(StringOperandId strId,
2803 Int32OperandId radixId) {
2804 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2805
2806 AutoCallVM callvm(masm, this, allocator);
2807
2808 Register str = allocator.useRegister(masm, strId);
2809 Register radix = allocator.useRegister(masm, radixId);
2810 AutoScratchRegisterMaybeOutput scratch(allocator, masm, callvm.output());
2811
2812 #ifdef DEBUG
2813 Label ok;
2814 masm.branch32(Assembler::Equal, radix, Imm32(0), &ok);
2815 masm.branch32(Assembler::Equal, radix, Imm32(10), &ok);
2816 masm.assumeUnreachable("radix must be 0 or 10 for indexed value fast path");
2817 masm.bind(&ok);
2818 #endif
2819
2820 // Discard the stack to ensure it's balanced when we skip the vm-call.
2821 allocator.discardStack(masm);
2822
2823 // Use indexed value as fast path if possible.
2824 Label vmCall, done;
2825 masm.loadStringIndexValue(str, scratch, &vmCall);
2826 masm.tagValue(JSVAL_TYPE_INT32, scratch, callvm.outputValueReg());
2827 masm.jump(&done);
2828 {
2829 masm.bind(&vmCall);
2830
2831 callvm.prepare();
2832 masm.Push(radix);
2833 masm.Push(str);
2834
2835 using Fn = bool (*)(JSContext*, HandleString, int32_t, MutableHandleValue);
2836 callvm.call<Fn, js::NumberParseInt>();
2837 }
2838 masm.bind(&done);
2839 return true;
2840 }
2841
2842 bool CacheIRCompiler::emitDoubleParseIntResult(NumberOperandId numId) {
2843 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2844
2845 AutoOutputRegister output(*this);
2846 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
2847 AutoAvailableFloatRegister floatScratch1(*this, FloatReg0);
2848 AutoAvailableFloatRegister floatScratch2(*this, FloatReg1);
2849
2850 FailurePath* failure;
2851 if (!addFailurePath(&failure)) {
2852 return false;
2853 }
2854
2855 allocator.ensureDoubleRegister(masm, numId, floatScratch1);
2856
2857 masm.branchDouble(Assembler::DoubleUnordered, floatScratch1, floatScratch1,
2858 failure->label());
2859 masm.branchTruncateDoubleToInt32(floatScratch1, scratch, failure->label());
2860
2861 Label ok;
2862 masm.branch32(Assembler::NotEqual, scratch, Imm32(0), &ok);
2863 {
2864 // Accept both +0 and -0 and return 0.
2865 masm.loadConstantDouble(0.0, floatScratch2);
2866 masm.branchDouble(Assembler::DoubleEqual, floatScratch1, floatScratch2,
2867 &ok);
2868
2869 // Fail if a non-zero input is in the exclusive range (-1, 1.0e-6).
2870 masm.loadConstantDouble(DOUBLE_DECIMAL_IN_SHORTEST_LOW, floatScratch2);
2871 masm.branchDouble(Assembler::DoubleLessThan, floatScratch1, floatScratch2,
2872 failure->label());
2873 }
2874 masm.bind(&ok);
2875
2876 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
2877 return true;
2878 }
2879
2880 bool CacheIRCompiler::emitStringToAtom(StringOperandId stringId) {
2881 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2882 Register str = allocator.useRegister(masm, stringId);
2883 AutoScratchRegister scratch(allocator, masm);
2884
2885 FailurePath* failure;
2886 if (!addFailurePath(&failure)) {
2887 return false;
2888 }
2889
2890 Label done, vmCall;
2891 masm.branchTest32(Assembler::NonZero, Address(str, JSString::offsetOfFlags()),
2892 Imm32(JSString::ATOM_BIT), &done);
2893
2894 masm.tryFastAtomize(str, scratch, str, &vmCall);
2895 masm.jump(&done);
2896
2897 masm.bind(&vmCall);
2898 LiveRegisterSet save = liveVolatileRegs();
2899 masm.PushRegsInMask(save);
2900
2901 using Fn = JSAtom* (*)(JSContext* cx, JSString* str);
2902 masm.setupUnalignedABICall(scratch);
2903 masm.loadJSContext(scratch);
2904 masm.passABIArg(scratch);
2905 masm.passABIArg(str);
2906 masm.callWithABI<Fn, jit::AtomizeStringNoGC>();
2907 masm.storeCallPointerResult(scratch);
2908
2909 LiveRegisterSet ignore;
2910 ignore.add(scratch);
2911 masm.PopRegsInMaskIgnore(save, ignore);
2912
2913 masm.branchPtr(Assembler::Equal, scratch, Imm32(0), failure->label());
2914 masm.movePtr(scratch.get(), str);
2915
2916 masm.bind(&done);
2917 return true;
2918 }
2919
2920 bool CacheIRCompiler::emitBooleanToNumber(BooleanOperandId booleanId,
2921 NumberOperandId resultId) {
2922 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2923 Register boolean = allocator.useRegister(masm, booleanId);
2924 ValueOperand output = allocator.defineValueRegister(masm, resultId);
2925 masm.tagValue(JSVAL_TYPE_INT32, boolean, output);
2926 return true;
2927 }
2928
2929 bool CacheIRCompiler::emitGuardStringToIndex(StringOperandId strId,
2930 Int32OperandId resultId) {
2931 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2932 Register str = allocator.useRegister(masm, strId);
2933 Register output = allocator.defineRegister(masm, resultId);
2934
2935 FailurePath* failure;
2936 if (!addFailurePath(&failure)) {
2937 return false;
2938 }
2939
2940 Label vmCall, done;
2941 masm.loadStringIndexValue(str, output, &vmCall);
2942 masm.jump(&done);
2943
2944 {
2945 masm.bind(&vmCall);
2946 LiveRegisterSet save = liveVolatileRegs();
2947 masm.PushRegsInMask(save);
2948
2949 using Fn = int32_t (*)(JSString* str);
2950 masm.setupUnalignedABICall(output);
2951 masm.passABIArg(str);
2952 masm.callWithABI<Fn, GetIndexFromString>();
2953 masm.storeCallInt32Result(output);
2954
2955 LiveRegisterSet ignore;
2956 ignore.add(output);
2957 masm.PopRegsInMaskIgnore(save, ignore);
2958
2959 // GetIndexFromString returns a negative value on failure.
2960 masm.branchTest32(Assembler::Signed, output, output, failure->label());
2961 }
2962
2963 masm.bind(&done);
2964 return true;
2965 }
2966
2967 bool CacheIRCompiler::emitLoadProto(ObjOperandId objId, ObjOperandId resultId) {
2968 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2969 Register obj = allocator.useRegister(masm, objId);
2970 Register reg = allocator.defineRegister(masm, resultId);
2971 masm.loadObjProto(obj, reg);
2972
2973 #ifdef DEBUG
2974 // We shouldn't encounter a null or lazy proto.
2975 MOZ_ASSERT(uintptr_t(TaggedProto::LazyProto) == 1);
2976
2977 Label done;
2978 masm.branchPtr(Assembler::Above, reg, ImmWord(1), &done);
2979 masm.assumeUnreachable("Unexpected null or lazy proto in CacheIR LoadProto");
2980 masm.bind(&done);
2981 #endif
2982 return true;
2983 }
2984
2985 bool CacheIRCompiler::emitLoadEnclosingEnvironment(ObjOperandId objId,
2986 ObjOperandId resultId) {
2987 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2988 Register obj = allocator.useRegister(masm, objId);
2989 Register reg = allocator.defineRegister(masm, resultId);
2990 masm.unboxObject(
2991 Address(obj, EnvironmentObject::offsetOfEnclosingEnvironment()), reg);
2992 return true;
2993 }
2994
2995 bool CacheIRCompiler::emitLoadWrapperTarget(ObjOperandId objId,
2996 ObjOperandId resultId,
2997 bool fallible) {
2998 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
2999 Register obj = allocator.useRegister(masm, objId);
3000 Register reg = allocator.defineRegister(masm, resultId);
3001
3002 FailurePath* failure;
3003 if (fallible && !addFailurePath(&failure)) {
3004 return false;
3005 }
3006
3007 masm.loadPtr(Address(obj, ProxyObject::offsetOfReservedSlots()), reg);
3008
3009 Address targetAddr(reg,
3010 js::detail::ProxyReservedSlots::offsetOfPrivateSlot());
3011 if (fallible) {
3012 masm.fallibleUnboxObject(targetAddr, reg, failure->label());
3013 } else {
3014 masm.unboxObject(targetAddr, reg);
3015 }
3016
3017 return true;
3018 }
3019
3020 bool CacheIRCompiler::emitLoadValueTag(ValOperandId valId,
3021 ValueTagOperandId resultId) {
3022 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3023 ValueOperand val = allocator.useValueRegister(masm, valId);
3024 Register res = allocator.defineRegister(masm, resultId);
3025
3026 Register tag = masm.extractTag(val, res);
3027 if (tag != res) {
3028 masm.mov(tag, res);
3029 }
3030 return true;
3031 }
3032
3033 bool CacheIRCompiler::emitLoadDOMExpandoValue(ObjOperandId objId,
3034 ValOperandId resultId) {
3035 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3036 Register obj = allocator.useRegister(masm, objId);
3037 ValueOperand val = allocator.defineValueRegister(masm, resultId);
3038
3039 masm.loadPtr(Address(obj, ProxyObject::offsetOfReservedSlots()),
3040 val.scratchReg());
3041 masm.loadValue(Address(val.scratchReg(),
3042 js::detail::ProxyReservedSlots::offsetOfPrivateSlot()),
3043 val);
3044 return true;
3045 }
3046
3047 bool CacheIRCompiler::emitLoadDOMExpandoValueIgnoreGeneration(
3048 ObjOperandId objId, ValOperandId resultId) {
3049 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3050 Register obj = allocator.useRegister(masm, objId);
3051 ValueOperand output = allocator.defineValueRegister(masm, resultId);
3052
3053 // Determine the expando's Address.
3054 Register scratch = output.scratchReg();
3055 masm.loadPtr(Address(obj, ProxyObject::offsetOfReservedSlots()), scratch);
3056 Address expandoAddr(scratch,
3057 js::detail::ProxyReservedSlots::offsetOfPrivateSlot());
3058
3059 #ifdef DEBUG
3060 // Private values are stored as doubles, so assert we have a double.
3061 Label ok;
3062 masm.branchTestDouble(Assembler::Equal, expandoAddr, &ok);
3063 masm.assumeUnreachable("DOM expando is not a PrivateValue!");
3064 masm.bind(&ok);
3065 #endif
3066
3067 // Load the ExpandoAndGeneration* from the PrivateValue.
3068 masm.loadPrivate(expandoAddr, scratch);
3069
3070 // Load expandoAndGeneration->expando into the output Value register.
3071 masm.loadValue(Address(scratch, ExpandoAndGeneration::offsetOfExpando()),
3072 output);
3073 return true;
3074 }
3075
3076 bool CacheIRCompiler::emitLoadUndefinedResult() {
3077 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3078 AutoOutputRegister output(*this);
3079 masm.moveValue(UndefinedValue(), output.valueReg());
3080 return true;
3081 }
3082
3083 static void EmitStoreBoolean(MacroAssembler& masm, bool b,
3084 const AutoOutputRegister& output) {
3085 if (output.hasValue()) {
3086 Value val = BooleanValue(b);
3087 masm.moveValue(val, output.valueReg());
3088 } else {
3089 MOZ_ASSERT(output.type() == JSVAL_TYPE_BOOLEAN);
3090 masm.movePtr(ImmWord(b), output.typedReg().gpr());
3091 }
3092 }
3093
3094 bool CacheIRCompiler::emitLoadBooleanResult(bool val) {
3095 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3096 AutoOutputRegister output(*this);
3097 EmitStoreBoolean(masm, val, output);
3098 return true;
3099 }
3100
3101 bool CacheIRCompiler::emitLoadOperandResult(ValOperandId inputId) {
3102 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3103 AutoOutputRegister output(*this);
3104 ValueOperand input = allocator.useValueRegister(masm, inputId);
3105 masm.moveValue(input, output.valueReg());
3106 return true;
3107 }
3108
3109 static void EmitStoreResult(MacroAssembler& masm, Register reg,
3110 JSValueType type,
3111 const AutoOutputRegister& output) {
3112 if (output.hasValue()) {
3113 masm.tagValue(type, reg, output.valueReg());
3114 return;
3115 }
3116 if (type == JSVAL_TYPE_INT32 && output.typedReg().isFloat()) {
3117 masm.convertInt32ToDouble(reg, output.typedReg().fpu());
3118 return;
3119 }
3120 if (type == output.type()) {
3121 masm.mov(reg, output.typedReg().gpr());
3122 return;
3123 }
3124 masm.assumeUnreachable("Should have monitored result");
3125 }
3126
3127 bool CacheIRCompiler::emitLoadInt32ArrayLengthResult(ObjOperandId objId) {
3128 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3129 AutoOutputRegister output(*this);
3130 Register obj = allocator.useRegister(masm, objId);
3131 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3132
3133 FailurePath* failure;
3134 if (!addFailurePath(&failure)) {
3135 return false;
3136 }
3137
3138 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
3139 masm.load32(Address(scratch, ObjectElements::offsetOfLength()), scratch);
3140
3141 // Guard length fits in an int32.
3142 masm.branchTest32(Assembler::Signed, scratch, scratch, failure->label());
3143 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3144 return true;
3145 }
3146
3147 bool CacheIRCompiler::emitLoadInt32ArrayLength(ObjOperandId objId,
3148 Int32OperandId resultId) {
3149 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3150 Register obj = allocator.useRegister(masm, objId);
3151 Register res = allocator.defineRegister(masm, resultId);
3152
3153 FailurePath* failure;
3154 if (!addFailurePath(&failure)) {
3155 return false;
3156 }
3157
3158 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), res);
3159 masm.load32(Address(res, ObjectElements::offsetOfLength()), res);
3160
3161 // Guard length fits in an int32.
3162 masm.branchTest32(Assembler::Signed, res, res, failure->label());
3163 return true;
3164 }
3165
3166 bool CacheIRCompiler::emitDoubleAddResult(NumberOperandId lhsId,
3167 NumberOperandId rhsId) {
3168 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3169 AutoOutputRegister output(*this);
3170
3171 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
3172 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
3173
3174 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
3175 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
3176
3177 masm.addDouble(floatScratch1, floatScratch0);
3178 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
3179
3180 return true;
3181 }
3182 bool CacheIRCompiler::emitDoubleSubResult(NumberOperandId lhsId,
3183 NumberOperandId rhsId) {
3184 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3185 AutoOutputRegister output(*this);
3186
3187 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
3188 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
3189
3190 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
3191 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
3192
3193 masm.subDouble(floatScratch1, floatScratch0);
3194 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
3195
3196 return true;
3197 }
3198 bool CacheIRCompiler::emitDoubleMulResult(NumberOperandId lhsId,
3199 NumberOperandId rhsId) {
3200 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3201 AutoOutputRegister output(*this);
3202
3203 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
3204 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
3205
3206 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
3207 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
3208
3209 masm.mulDouble(floatScratch1, floatScratch0);
3210 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
3211
3212 return true;
3213 }
3214 bool CacheIRCompiler::emitDoubleDivResult(NumberOperandId lhsId,
3215 NumberOperandId rhsId) {
3216 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3217 AutoOutputRegister output(*this);
3218
3219 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
3220 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
3221
3222 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
3223 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
3224
3225 masm.divDouble(floatScratch1, floatScratch0);
3226 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
3227
3228 return true;
3229 }
3230 bool CacheIRCompiler::emitDoubleModResult(NumberOperandId lhsId,
3231 NumberOperandId rhsId) {
3232 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3233 AutoOutputRegister output(*this);
3234 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3235
3236 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
3237 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
3238
3239 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
3240 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
3241
3242 LiveRegisterSet save = liveVolatileRegs();
3243 masm.PushRegsInMask(save);
3244
3245 using Fn = double (*)(double a, double b);
3246 masm.setupUnalignedABICall(scratch);
3247 masm.passABIArg(floatScratch0, ABIType::Float64);
3248 masm.passABIArg(floatScratch1, ABIType::Float64);
3249 masm.callWithABI<Fn, js::NumberMod>(ABIType::Float64);
3250 masm.storeCallFloatResult(floatScratch0);
3251
3252 LiveRegisterSet ignore;
3253 ignore.add(floatScratch0);
3254 masm.PopRegsInMaskIgnore(save, ignore);
3255
3256 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
3257
3258 return true;
3259 }
3260 bool CacheIRCompiler::emitDoublePowResult(NumberOperandId lhsId,
3261 NumberOperandId rhsId) {
3262 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3263 AutoOutputRegister output(*this);
3264 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3265
3266 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
3267 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
3268
3269 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
3270 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
3271
3272 LiveRegisterSet save = liveVolatileRegs();
3273 masm.PushRegsInMask(save);
3274
3275 using Fn = double (*)(double x, double y);
3276 masm.setupUnalignedABICall(scratch);
3277 masm.passABIArg(floatScratch0, ABIType::Float64);
3278 masm.passABIArg(floatScratch1, ABIType::Float64);
3279 masm.callWithABI<Fn, js::ecmaPow>(ABIType::Float64);
3280 masm.storeCallFloatResult(floatScratch0);
3281
3282 LiveRegisterSet ignore;
3283 ignore.add(floatScratch0);
3284 masm.PopRegsInMaskIgnore(save, ignore);
3285
3286 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
3287
3288 return true;
3289 }
3290
3291 bool CacheIRCompiler::emitInt32AddResult(Int32OperandId lhsId,
3292 Int32OperandId rhsId) {
3293 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3294 AutoOutputRegister output(*this);
3295 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3296
3297 Register lhs = allocator.useRegister(masm, lhsId);
3298 Register rhs = allocator.useRegister(masm, rhsId);
3299
3300 FailurePath* failure;
3301 if (!addFailurePath(&failure)) {
3302 return false;
3303 }
3304
3305 masm.mov(rhs, scratch);
3306 masm.branchAdd32(Assembler::Overflow, lhs, scratch, failure->label());
3307 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3308
3309 return true;
3310 }
3311 bool CacheIRCompiler::emitInt32SubResult(Int32OperandId lhsId,
3312 Int32OperandId rhsId) {
3313 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3314 AutoOutputRegister output(*this);
3315 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3316 Register lhs = allocator.useRegister(masm, lhsId);
3317 Register rhs = allocator.useRegister(masm, rhsId);
3318
3319 FailurePath* failure;
3320 if (!addFailurePath(&failure)) {
3321 return false;
3322 }
3323
3324 masm.mov(lhs, scratch);
3325 masm.branchSub32(Assembler::Overflow, rhs, scratch, failure->label());
3326 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3327
3328 return true;
3329 }
3330
3331 bool CacheIRCompiler::emitInt32MulResult(Int32OperandId lhsId,
3332 Int32OperandId rhsId) {
3333 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3334 AutoOutputRegister output(*this);
3335 Register lhs = allocator.useRegister(masm, lhsId);
3336 Register rhs = allocator.useRegister(masm, rhsId);
3337 AutoScratchRegister scratch(allocator, masm);
3338 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
3339
3340 FailurePath* failure;
3341 if (!addFailurePath(&failure)) {
3342 return false;
3343 }
3344
3345 Label maybeNegZero, done;
3346 masm.mov(lhs, scratch);
3347 masm.branchMul32(Assembler::Overflow, rhs, scratch, failure->label());
3348 masm.branchTest32(Assembler::Zero, scratch, scratch, &maybeNegZero);
3349 masm.jump(&done);
3350
3351 masm.bind(&maybeNegZero);
3352 masm.mov(lhs, scratch2);
3353 // Result is -0 if exactly one of lhs or rhs is negative.
3354 masm.or32(rhs, scratch2);
3355 masm.branchTest32(Assembler::Signed, scratch2, scratch2, failure->label());
3356
3357 masm.bind(&done);
3358 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3359 return true;
3360 }
3361
3362 bool CacheIRCompiler::emitInt32DivResult(Int32OperandId lhsId,
3363 Int32OperandId rhsId) {
3364 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3365 AutoOutputRegister output(*this);
3366 Register lhs = allocator.useRegister(masm, lhsId);
3367 Register rhs = allocator.useRegister(masm, rhsId);
3368 AutoScratchRegister rem(allocator, masm);
3369 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3370
3371 FailurePath* failure;
3372 if (!addFailurePath(&failure)) {
3373 return false;
3374 }
3375
3376 // Prevent division by 0.
3377 masm.branchTest32(Assembler::Zero, rhs, rhs, failure->label());
3378
3379 // Prevent -2147483648 / -1.
3380 Label notOverflow;
3381 masm.branch32(Assembler::NotEqual, lhs, Imm32(INT32_MIN), &notOverflow);
3382 masm.branch32(Assembler::Equal, rhs, Imm32(-1), failure->label());
3383 masm.bind(&notOverflow);
3384
3385 // Prevent negative 0.
3386 Label notZero;
3387 masm.branchTest32(Assembler::NonZero, lhs, lhs, &notZero);
3388 masm.branchTest32(Assembler::Signed, rhs, rhs, failure->label());
3389 masm.bind(&notZero);
3390
3391 masm.mov(lhs, scratch);
3392 masm.flexibleDivMod32(rhs, scratch, rem, false, liveVolatileRegs());
3393
3394 // A remainder implies a double result.
3395 masm.branchTest32(Assembler::NonZero, rem, rem, failure->label());
3396 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3397 return true;
3398 }
3399
3400 bool CacheIRCompiler::emitInt32ModResult(Int32OperandId lhsId,
3401 Int32OperandId rhsId) {
3402 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3403 AutoOutputRegister output(*this);
3404 Register lhs = allocator.useRegister(masm, lhsId);
3405 Register rhs = allocator.useRegister(masm, rhsId);
3406 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3407
3408 FailurePath* failure;
3409 if (!addFailurePath(&failure)) {
3410 return false;
3411 }
3412
3413 // x % 0 results in NaN
3414 masm.branchTest32(Assembler::Zero, rhs, rhs, failure->label());
3415
3416 // Prevent -2147483648 % -1.
3417 //
3418 // Traps on x86 and has undefined behavior on ARM32 (when __aeabi_idivmod is
3419 // called).
3420 Label notOverflow;
3421 masm.branch32(Assembler::NotEqual, lhs, Imm32(INT32_MIN), &notOverflow);
3422 masm.branch32(Assembler::Equal, rhs, Imm32(-1), failure->label());
3423 masm.bind(&notOverflow);
3424
3425 masm.mov(lhs, scratch);
3426 masm.flexibleRemainder32(rhs, scratch, false, liveVolatileRegs());
3427
3428 // Modulo takes the sign of the dividend; we can't return negative zero here.
3429 Label notZero;
3430 masm.branchTest32(Assembler::NonZero, scratch, scratch, &notZero);
3431 masm.branchTest32(Assembler::Signed, lhs, lhs, failure->label());
3432 masm.bind(&notZero);
3433
3434 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3435
3436 return true;
3437 }
3438
3439 bool CacheIRCompiler::emitInt32PowResult(Int32OperandId lhsId,
3440 Int32OperandId rhsId) {
3441 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3442 AutoOutputRegister output(*this);
3443 Register base = allocator.useRegister(masm, lhsId);
3444 Register power = allocator.useRegister(masm, rhsId);
3445 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
3446 AutoScratchRegisterMaybeOutputType scratch2(allocator, masm, output);
3447 AutoScratchRegister scratch3(allocator, masm);
3448
3449 FailurePath* failure;
3450 if (!addFailurePath(&failure)) {
3451 return false;
3452 }
3453
3454 masm.pow32(base, power, scratch1, scratch2, scratch3, failure->label());
3455
3456 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
3457 return true;
3458 }
3459
3460 bool CacheIRCompiler::emitInt32BitOrResult(Int32OperandId lhsId,
3461 Int32OperandId rhsId) {
3462 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3463 AutoOutputRegister output(*this);
3464 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3465
3466 Register lhs = allocator.useRegister(masm, lhsId);
3467 Register rhs = allocator.useRegister(masm, rhsId);
3468
3469 masm.mov(rhs, scratch);
3470 masm.or32(lhs, scratch);
3471 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3472
3473 return true;
3474 }
3475 bool CacheIRCompiler::emitInt32BitXorResult(Int32OperandId lhsId,
3476 Int32OperandId rhsId) {
3477 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3478 AutoOutputRegister output(*this);
3479 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3480
3481 Register lhs = allocator.useRegister(masm, lhsId);
3482 Register rhs = allocator.useRegister(masm, rhsId);
3483
3484 masm.mov(rhs, scratch);
3485 masm.xor32(lhs, scratch);
3486 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3487
3488 return true;
3489 }
3490 bool CacheIRCompiler::emitInt32BitAndResult(Int32OperandId lhsId,
3491 Int32OperandId rhsId) {
3492 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3493 AutoOutputRegister output(*this);
3494 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3495
3496 Register lhs = allocator.useRegister(masm, lhsId);
3497 Register rhs = allocator.useRegister(masm, rhsId);
3498
3499 masm.mov(rhs, scratch);
3500 masm.and32(lhs, scratch);
3501 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3502
3503 return true;
3504 }
3505 bool CacheIRCompiler::emitInt32LeftShiftResult(Int32OperandId lhsId,
3506 Int32OperandId rhsId) {
3507 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3508 AutoOutputRegister output(*this);
3509 Register lhs = allocator.useRegister(masm, lhsId);
3510 Register rhs = allocator.useRegister(masm, rhsId);
3511 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3512
3513 masm.mov(lhs, scratch);
3514 masm.flexibleLshift32(rhs, scratch);
3515 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3516
3517 return true;
3518 }
3519
3520 bool CacheIRCompiler::emitInt32RightShiftResult(Int32OperandId lhsId,
3521 Int32OperandId rhsId) {
3522 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3523 AutoOutputRegister output(*this);
3524 Register lhs = allocator.useRegister(masm, lhsId);
3525 Register rhs = allocator.useRegister(masm, rhsId);
3526 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3527
3528 masm.mov(lhs, scratch);
3529 masm.flexibleRshift32Arithmetic(rhs, scratch);
3530 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3531
3532 return true;
3533 }
3534
3535 bool CacheIRCompiler::emitInt32URightShiftResult(Int32OperandId lhsId,
3536 Int32OperandId rhsId,
3537 bool forceDouble) {
3538 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3539 AutoOutputRegister output(*this);
3540
3541 Register lhs = allocator.useRegister(masm, lhsId);
3542 Register rhs = allocator.useRegister(masm, rhsId);
3543 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3544
3545 FailurePath* failure;
3546 if (!addFailurePath(&failure)) {
3547 return false;
3548 }
3549
3550 masm.mov(lhs, scratch);
3551 masm.flexibleRshift32(rhs, scratch);
3552 if (forceDouble) {
3553 ScratchDoubleScope fpscratch(masm);
3554 masm.convertUInt32ToDouble(scratch, fpscratch);
3555 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
3556 } else {
3557 masm.branchTest32(Assembler::Signed, scratch, scratch, failure->label());
3558 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3559 }
3560 return true;
3561 }
3562
3563 bool CacheIRCompiler::emitInt32NegationResult(Int32OperandId inputId) {
3564 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3565 AutoOutputRegister output(*this);
3566 Register val = allocator.useRegister(masm, inputId);
3567 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3568
3569 FailurePath* failure;
3570 if (!addFailurePath(&failure)) {
3571 return false;
3572 }
3573
3574 // Guard against 0 and MIN_INT by checking if low 31-bits are all zero.
3575 // Both of these result in a double.
3576 masm.branchTest32(Assembler::Zero, val, Imm32(0x7fffffff), failure->label());
3577 masm.mov(val, scratch);
3578 masm.neg32(scratch);
3579 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3580 return true;
3581 }
3582
3583 bool CacheIRCompiler::emitInt32IncResult(Int32OperandId inputId) {
3584 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3585 AutoOutputRegister output(*this);
3586 Register input = allocator.useRegister(masm, inputId);
3587 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3588
3589 FailurePath* failure;
3590 if (!addFailurePath(&failure)) {
3591 return false;
3592 }
3593
3594 masm.mov(input, scratch);
3595 masm.branchAdd32(Assembler::Overflow, Imm32(1), scratch, failure->label());
3596 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3597
3598 return true;
3599 }
3600
3601 bool CacheIRCompiler::emitInt32DecResult(Int32OperandId inputId) {
3602 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3603 AutoOutputRegister output(*this);
3604 Register input = allocator.useRegister(masm, inputId);
3605 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3606
3607 FailurePath* failure;
3608 if (!addFailurePath(&failure)) {
3609 return false;
3610 }
3611
3612 masm.mov(input, scratch);
3613 masm.branchSub32(Assembler::Overflow, Imm32(1), scratch, failure->label());
3614 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3615
3616 return true;
3617 }
3618
3619 bool CacheIRCompiler::emitInt32NotResult(Int32OperandId inputId) {
3620 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3621 AutoOutputRegister output(*this);
3622 Register val = allocator.useRegister(masm, inputId);
3623 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
3624
3625 masm.mov(val, scratch);
3626 masm.not32(scratch);
3627 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
3628 return true;
3629 }
3630
3631 bool CacheIRCompiler::emitDoubleNegationResult(NumberOperandId inputId) {
3632 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3633 AutoOutputRegister output(*this);
3634
3635 AutoScratchFloatRegister floatReg(this);
3636
3637 allocator.ensureDoubleRegister(masm, inputId, floatReg);
3638
3639 masm.negateDouble(floatReg);
3640 masm.boxDouble(floatReg, output.valueReg(), floatReg);
3641
3642 return true;
3643 }
3644
3645 bool CacheIRCompiler::emitDoubleIncDecResult(bool isInc,
3646 NumberOperandId inputId) {
3647 AutoOutputRegister output(*this);
3648
3649 AutoScratchFloatRegister floatReg(this);
3650
3651 allocator.ensureDoubleRegister(masm, inputId, floatReg);
3652
3653 {
3654 ScratchDoubleScope fpscratch(masm);
3655 masm.loadConstantDouble(1.0, fpscratch);
3656 if (isInc) {
3657 masm.addDouble(fpscratch, floatReg);
3658 } else {
3659 masm.subDouble(fpscratch, floatReg);
3660 }
3661 }
3662 masm.boxDouble(floatReg, output.valueReg(), floatReg);
3663
3664 return true;
3665 }
3666
3667 bool CacheIRCompiler::emitDoubleIncResult(NumberOperandId inputId) {
3668 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3669 return emitDoubleIncDecResult(true, inputId);
3670 }
3671
3672 bool CacheIRCompiler::emitDoubleDecResult(NumberOperandId inputId) {
3673 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3674 return emitDoubleIncDecResult(false, inputId);
3675 }
3676
3677 template <typename Fn, Fn fn>
3678 bool CacheIRCompiler::emitBigIntBinaryOperationShared(BigIntOperandId lhsId,
3679 BigIntOperandId rhsId) {
3680 AutoCallVM callvm(masm, this, allocator);
3681 Register lhs = allocator.useRegister(masm, lhsId);
3682 Register rhs = allocator.useRegister(masm, rhsId);
3683
3684 callvm.prepare();
3685
3686 masm.Push(rhs);
3687 masm.Push(lhs);
3688
3689 callvm.call<Fn, fn>();
3690 return true;
3691 }
3692
3693 bool CacheIRCompiler::emitBigIntAddResult(BigIntOperandId lhsId,
3694 BigIntOperandId rhsId) {
3695 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3696 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3697 return emitBigIntBinaryOperationShared<Fn, BigInt::add>(lhsId, rhsId);
3698 }
3699
3700 bool CacheIRCompiler::emitBigIntSubResult(BigIntOperandId lhsId,
3701 BigIntOperandId rhsId) {
3702 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3703 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3704 return emitBigIntBinaryOperationShared<Fn, BigInt::sub>(lhsId, rhsId);
3705 }
3706
3707 bool CacheIRCompiler::emitBigIntMulResult(BigIntOperandId lhsId,
3708 BigIntOperandId rhsId) {
3709 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3710 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3711 return emitBigIntBinaryOperationShared<Fn, BigInt::mul>(lhsId, rhsId);
3712 }
3713
3714 bool CacheIRCompiler::emitBigIntDivResult(BigIntOperandId lhsId,
3715 BigIntOperandId rhsId) {
3716 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3717 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3718 return emitBigIntBinaryOperationShared<Fn, BigInt::div>(lhsId, rhsId);
3719 }
3720
3721 bool CacheIRCompiler::emitBigIntModResult(BigIntOperandId lhsId,
3722 BigIntOperandId rhsId) {
3723 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3724 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3725 return emitBigIntBinaryOperationShared<Fn, BigInt::mod>(lhsId, rhsId);
3726 }
3727
3728 bool CacheIRCompiler::emitBigIntPowResult(BigIntOperandId lhsId,
3729 BigIntOperandId rhsId) {
3730 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3731 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3732 return emitBigIntBinaryOperationShared<Fn, BigInt::pow>(lhsId, rhsId);
3733 }
3734
3735 bool CacheIRCompiler::emitBigIntBitAndResult(BigIntOperandId lhsId,
3736 BigIntOperandId rhsId) {
3737 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3738 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3739 return emitBigIntBinaryOperationShared<Fn, BigInt::bitAnd>(lhsId, rhsId);
3740 }
3741
3742 bool CacheIRCompiler::emitBigIntBitOrResult(BigIntOperandId lhsId,
3743 BigIntOperandId rhsId) {
3744 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3745 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3746 return emitBigIntBinaryOperationShared<Fn, BigInt::bitOr>(lhsId, rhsId);
3747 }
3748
3749 bool CacheIRCompiler::emitBigIntBitXorResult(BigIntOperandId lhsId,
3750 BigIntOperandId rhsId) {
3751 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3752 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3753 return emitBigIntBinaryOperationShared<Fn, BigInt::bitXor>(lhsId, rhsId);
3754 }
3755
3756 bool CacheIRCompiler::emitBigIntLeftShiftResult(BigIntOperandId lhsId,
3757 BigIntOperandId rhsId) {
3758 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3759 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3760 return emitBigIntBinaryOperationShared<Fn, BigInt::lsh>(lhsId, rhsId);
3761 }
3762
3763 bool CacheIRCompiler::emitBigIntRightShiftResult(BigIntOperandId lhsId,
3764 BigIntOperandId rhsId) {
3765 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3766 using Fn = BigInt* (*)(JSContext*, HandleBigInt, HandleBigInt);
3767 return emitBigIntBinaryOperationShared<Fn, BigInt::rsh>(lhsId, rhsId);
3768 }
3769
3770 template <typename Fn, Fn fn>
3771 bool CacheIRCompiler::emitBigIntUnaryOperationShared(BigIntOperandId inputId) {
3772 AutoCallVM callvm(masm, this, allocator);
3773 Register val = allocator.useRegister(masm, inputId);
3774
3775 callvm.prepare();
3776
3777 masm.Push(val);
3778
3779 callvm.call<Fn, fn>();
3780 return true;
3781 }
3782
3783 bool CacheIRCompiler::emitBigIntNotResult(BigIntOperandId inputId) {
3784 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3785 using Fn = BigInt* (*)(JSContext*, HandleBigInt);
3786 return emitBigIntUnaryOperationShared<Fn, BigInt::bitNot>(inputId);
3787 }
3788
3789 bool CacheIRCompiler::emitBigIntNegationResult(BigIntOperandId inputId) {
3790 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3791 using Fn = BigInt* (*)(JSContext*, HandleBigInt);
3792 return emitBigIntUnaryOperationShared<Fn, BigInt::neg>(inputId);
3793 }
3794
3795 bool CacheIRCompiler::emitBigIntIncResult(BigIntOperandId inputId) {
3796 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3797 using Fn = BigInt* (*)(JSContext*, HandleBigInt);
3798 return emitBigIntUnaryOperationShared<Fn, BigInt::inc>(inputId);
3799 }
3800
3801 bool CacheIRCompiler::emitBigIntDecResult(BigIntOperandId inputId) {
3802 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3803 using Fn = BigInt* (*)(JSContext*, HandleBigInt);
3804 return emitBigIntUnaryOperationShared<Fn, BigInt::dec>(inputId);
3805 }
3806
3807 bool CacheIRCompiler::emitBigIntToIntPtr(BigIntOperandId inputId,
3808 IntPtrOperandId resultId) {
3809 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3810
3811 Register input = allocator.useRegister(masm, inputId);
3812 Register output = allocator.defineRegister(masm, resultId);
3813
3814 FailurePath* failure;
3815 if (!addFailurePath(&failure)) {
3816 return false;
3817 }
3818
3819 masm.loadBigIntPtr(input, output, failure->label());
3820 return true;
3821 }
3822
3823 static gc::Heap InitialBigIntHeap(JSContext* cx) {
3824 JS::Zone* zone = cx->zone();
3825 return zone->allocNurseryBigInts() ? gc::Heap::Default : gc::Heap::Tenured;
3826 }
3827
3828 static void EmitAllocateBigInt(MacroAssembler& masm, Register result,
3829 Register temp, const LiveRegisterSet& liveSet,
3830 gc::Heap initialHeap, Label* fail) {
3831 Label fallback, done;
3832 masm.newGCBigInt(result, temp, initialHeap, &fallback);
3833 masm.jump(&done);
3834 {
3835 masm.bind(&fallback);
3836
3837 // Request a minor collection at a later time if nursery allocation failed.
3838 bool requestMinorGC = initialHeap == gc::Heap::Default;
3839
3840 masm.PushRegsInMask(liveSet);
3841 using Fn = void* (*)(JSContext* cx, bool requestMinorGC);
3842 masm.setupUnalignedABICall(temp);
3843 masm.loadJSContext(temp);
3844 masm.passABIArg(temp);
3845 masm.move32(Imm32(requestMinorGC), result);
3846 masm.passABIArg(result);
3847 masm.callWithABI<Fn, jit::AllocateBigIntNoGC>();
3848 masm.storeCallPointerResult(result);
3849
3850 masm.PopRegsInMask(liveSet);
3851 masm.branchPtr(Assembler::Equal, result, ImmWord(0), fail);
3852 }
3853 masm.bind(&done);
3854 }
3855
3856 bool CacheIRCompiler::emitIntPtrToBigIntResult(IntPtrOperandId inputId) {
3857 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3858
3859 AutoOutputRegister output(*this);
3860 Register input = allocator.useRegister(masm, inputId);
3861 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
3862 AutoScratchRegister scratch2(allocator, masm);
3863
3864 FailurePath* failure;
3865 if (!addFailurePath(&failure)) {
3866 return false;
3867 }
3868
3869 LiveRegisterSet save = liveVolatileRegs();
3870 save.takeUnchecked(scratch1);
3871 save.takeUnchecked(scratch2);
3872 save.takeUnchecked(output);
3873
3874 // Allocate a new BigInt. The code after this must be infallible.
3875 gc::Heap initialHeap = InitialBigIntHeap(cx_);
3876 EmitAllocateBigInt(masm, scratch1, scratch2, save, initialHeap,
3877 failure->label());
3878
3879 masm.movePtr(input, scratch2);
3880 masm.initializeBigIntPtr(scratch1, scratch2);
3881
3882 masm.tagValue(JSVAL_TYPE_BIGINT, scratch1, output.valueReg());
3883 return true;
3884 }
3885
3886 bool CacheIRCompiler::emitBigIntPtrAdd(IntPtrOperandId lhsId,
3887 IntPtrOperandId rhsId,
3888 IntPtrOperandId resultId) {
3889 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3890
3891 Register lhs = allocator.useRegister(masm, lhsId);
3892 Register rhs = allocator.useRegister(masm, rhsId);
3893 Register output = allocator.defineRegister(masm, resultId);
3894
3895 FailurePath* failure;
3896 if (!addFailurePath(&failure)) {
3897 return false;
3898 }
3899
3900 masm.movePtr(rhs, output);
3901 masm.branchAddPtr(Assembler::Overflow, lhs, output, failure->label());
3902 return true;
3903 }
3904
3905 bool CacheIRCompiler::emitBigIntPtrSub(IntPtrOperandId lhsId,
3906 IntPtrOperandId rhsId,
3907 IntPtrOperandId resultId) {
3908 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3909
3910 Register lhs = allocator.useRegister(masm, lhsId);
3911 Register rhs = allocator.useRegister(masm, rhsId);
3912 Register output = allocator.defineRegister(masm, resultId);
3913
3914 FailurePath* failure;
3915 if (!addFailurePath(&failure)) {
3916 return false;
3917 }
3918
3919 masm.movePtr(lhs, output);
3920 masm.branchSubPtr(Assembler::Overflow, rhs, output, failure->label());
3921 return true;
3922 }
3923
3924 bool CacheIRCompiler::emitBigIntPtrMul(IntPtrOperandId lhsId,
3925 IntPtrOperandId rhsId,
3926 IntPtrOperandId resultId) {
3927 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3928
3929 Register lhs = allocator.useRegister(masm, lhsId);
3930 Register rhs = allocator.useRegister(masm, rhsId);
3931 Register output = allocator.defineRegister(masm, resultId);
3932
3933 FailurePath* failure;
3934 if (!addFailurePath(&failure)) {
3935 return false;
3936 }
3937
3938 masm.movePtr(rhs, output);
3939 masm.branchMulPtr(Assembler::Overflow, lhs, output, failure->label());
3940 return true;
3941 }
3942
3943 bool CacheIRCompiler::emitBigIntPtrDiv(IntPtrOperandId lhsId,
3944 IntPtrOperandId rhsId,
3945 IntPtrOperandId resultId) {
3946 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3947
3948 Register lhs = allocator.useRegister(masm, lhsId);
3949 Register rhs = allocator.useRegister(masm, rhsId);
3950 Register output = allocator.defineRegister(masm, resultId);
3951
3952 FailurePath* failure;
3953 if (!addFailurePath(&failure)) {
3954 return false;
3955 }
3956
3957 static constexpr auto DigitMin = std::numeric_limits<
3958 mozilla::SignedStdintTypeForSize<sizeof(BigInt::Digit)>::Type>::min();
3959
3960 // Prevent division by 0.
3961 masm.branchTestPtr(Assembler::Zero, rhs, rhs, failure->label());
3962
3963 // Prevent INTPTR_MIN / -1.
3964 Label notOverflow;
3965 masm.branchPtr(Assembler::NotEqual, lhs, ImmWord(DigitMin), &notOverflow);
3966 masm.branchPtr(Assembler::Equal, rhs, Imm32(-1), failure->label());
3967 masm.bind(&notOverflow);
3968
3969 LiveRegisterSet volatileRegs(GeneralRegisterSet::Volatile(),
3970 liveVolatileFloatRegs());
3971 masm.movePtr(lhs, output);
3972 masm.flexibleQuotientPtr(rhs, output, false, volatileRegs);
3973 return true;
3974 }
3975
3976 bool CacheIRCompiler::emitBigIntPtrMod(IntPtrOperandId lhsId,
3977 IntPtrOperandId rhsId,
3978 IntPtrOperandId resultId) {
3979 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
3980
3981 Register lhs = allocator.useRegister(masm, lhsId);
3982 Register rhs = allocator.useRegister(masm, rhsId);
3983 Register output = allocator.defineRegister(masm, resultId);
3984
3985 FailurePath* failure;
3986 if (!addFailurePath(&failure)) {
3987 return false;
3988 }
3989
3990 static constexpr auto DigitMin = std::numeric_limits<
3991 mozilla::SignedStdintTypeForSize<sizeof(BigInt::Digit)>::Type>::min();
3992
3993 // Prevent division by 0.
3994 masm.branchTestPtr(Assembler::Zero, rhs, rhs, failure->label());
3995
3996 masm.movePtr(lhs, output);
3997
3998 // Prevent INTPTR_MIN / -1.
3999 Label notOverflow;
4000 masm.branchPtr(Assembler::NotEqual, lhs, ImmWord(DigitMin), &notOverflow);
4001 masm.branchPtr(Assembler::NotEqual, rhs, Imm32(-1), &notOverflow);
4002 masm.movePtr(ImmWord(0), output);
4003 masm.bind(&notOverflow);
4004
4005 LiveRegisterSet volatileRegs(GeneralRegisterSet::Volatile(),
4006 liveVolatileFloatRegs());
4007 masm.flexibleRemainderPtr(rhs, output, false, volatileRegs);
4008 return true;
4009 }
4010
4011 bool CacheIRCompiler::emitBigIntPtrPow(IntPtrOperandId lhsId,
4012 IntPtrOperandId rhsId,
4013 IntPtrOperandId resultId) {
4014 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4015
4016 Register lhs = allocator.useRegister(masm, lhsId);
4017 Register rhs = allocator.useRegister(masm, rhsId);
4018 Register output = allocator.defineRegister(masm, resultId);
4019 AutoScratchRegister scratch1(allocator, masm);
4020 AutoScratchRegister scratch2(allocator, masm);
4021
4022 FailurePath* failure;
4023 if (!addFailurePath(&failure)) {
4024 return false;
4025 }
4026
4027 masm.powPtr(lhs, rhs, output, scratch1, scratch2, failure->label());
4028 return true;
4029 }
4030
4031 bool CacheIRCompiler::emitBigIntPtrBitOr(IntPtrOperandId lhsId,
4032 IntPtrOperandId rhsId,
4033 IntPtrOperandId resultId) {
4034 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4035
4036 Register lhs = allocator.useRegister(masm, lhsId);
4037 Register rhs = allocator.useRegister(masm, rhsId);
4038 Register output = allocator.defineRegister(masm, resultId);
4039
4040 masm.movePtr(rhs, output);
4041 masm.orPtr(lhs, output);
4042 return true;
4043 }
4044
4045 bool CacheIRCompiler::emitBigIntPtrBitXor(IntPtrOperandId lhsId,
4046 IntPtrOperandId rhsId,
4047 IntPtrOperandId resultId) {
4048 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4049
4050 Register lhs = allocator.useRegister(masm, lhsId);
4051 Register rhs = allocator.useRegister(masm, rhsId);
4052 Register output = allocator.defineRegister(masm, resultId);
4053
4054 masm.movePtr(rhs, output);
4055 masm.xorPtr(lhs, output);
4056 return true;
4057 }
4058
4059 bool CacheIRCompiler::emitBigIntPtrBitAnd(IntPtrOperandId lhsId,
4060 IntPtrOperandId rhsId,
4061 IntPtrOperandId resultId) {
4062 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4063
4064 Register lhs = allocator.useRegister(masm, lhsId);
4065 Register rhs = allocator.useRegister(masm, rhsId);
4066 Register output = allocator.defineRegister(masm, resultId);
4067
4068 masm.movePtr(rhs, output);
4069 masm.andPtr(lhs, output);
4070 return true;
4071 }
4072
4073 bool CacheIRCompiler::emitBigIntPtrLeftShift(IntPtrOperandId lhsId,
4074 IntPtrOperandId rhsId,
4075 IntPtrOperandId resultId) {
4076 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4077
4078 Register lhs = allocator.useRegister(masm, lhsId);
4079 Register rhs = allocator.useRegister(masm, rhsId);
4080 Register output = allocator.defineRegister(masm, resultId);
4081 AutoScratchRegister scratch(allocator, masm);
4082
4083 FailurePath* failure;
4084 if (!addFailurePath(&failure)) {
4085 return false;
4086 }
4087
4088 Label done;
4089
4090 masm.movePtr(lhs, output);
4091
4092 // 0n << x == 0n
4093 masm.branchPtr(Assembler::Equal, lhs, Imm32(0), &done);
4094
4095 // x << DigitBits with x != 0n always exceeds pointer-sized storage.
4096 masm.branchPtr(Assembler::GreaterThanOrEqual, rhs, Imm32(BigInt::DigitBits),
4097 failure->label());
4098
4099 // x << -DigitBits == x >> DigitBits, which is either 0n or -1n.
4100 Label shift;
4101 masm.branchPtr(Assembler::GreaterThan, rhs,
4102 Imm32(-int32_t(BigInt::DigitBits)), &shift);
4103 {
4104 masm.rshiftPtrArithmetic(Imm32(BigInt::DigitBits - 1), output);
4105 masm.jump(&done);
4106 }
4107 masm.bind(&shift);
4108
4109 // |x << -y| is computed as |x >> y|.
4110 Label leftShift;
4111 masm.branchPtr(Assembler::GreaterThanOrEqual, rhs, Imm32(0), &leftShift);
4112 {
4113 masm.movePtr(rhs, scratch);
4114 masm.negPtr(scratch);
4115 masm.flexibleRshiftPtrArithmetic(scratch, output);
4116 masm.jump(&done);
4117 }
4118 masm.bind(&leftShift);
4119
4120 masm.flexibleLshiftPtr(rhs, output);
4121
4122 // Check for overflow: ((lhs << rhs) >> rhs) == lhs.
4123 masm.movePtr(output, scratch);
4124 masm.flexibleRshiftPtrArithmetic(rhs, scratch);
4125 masm.branchPtr(Assembler::NotEqual, scratch, lhs, failure->label());
4126
4127 masm.bind(&done);
4128 return true;
4129 }
4130
4131 bool CacheIRCompiler::emitBigIntPtrRightShift(IntPtrOperandId lhsId,
4132 IntPtrOperandId rhsId,
4133 IntPtrOperandId resultId) {
4134 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4135
4136 Register lhs = allocator.useRegister(masm, lhsId);
4137 Register rhs = allocator.useRegister(masm, rhsId);
4138 Register output = allocator.defineRegister(masm, resultId);
4139 AutoScratchRegister scratch1(allocator, masm);
4140 AutoScratchRegister scratch2(allocator, masm);
4141
4142 FailurePath* failure;
4143 if (!addFailurePath(&failure)) {
4144 return false;
4145 }
4146
4147 Label done;
4148
4149 masm.movePtr(lhs, output);
4150
4151 // 0n >> x == 0n
4152 masm.branchPtr(Assembler::Equal, lhs, Imm32(0), &done);
4153
4154 // x >> -DigitBits == x << DigitBits, which exceeds pointer-sized storage.
4155 masm.branchPtr(Assembler::LessThanOrEqual, rhs,
4156 Imm32(-int32_t(BigInt::DigitBits)), failure->label());
4157
4158 // x >> DigitBits is either 0n or -1n.
4159 Label shift;
4160 masm.branchPtr(Assembler::LessThan, rhs, Imm32(BigInt::DigitBits), &shift);
4161 {
4162 masm.rshiftPtrArithmetic(Imm32(BigInt::DigitBits - 1), output);
4163 masm.jump(&done);
4164 }
4165 masm.bind(&shift);
4166
4167 // |x >> -y| is computed as |x << y|.
4168 Label rightShift;
4169 masm.branchPtr(Assembler::GreaterThanOrEqual, rhs, Imm32(0), &rightShift);
4170 {
4171 masm.movePtr(rhs, scratch1);
4172 masm.negPtr(scratch1);
4173 masm.flexibleLshiftPtr(scratch1, output);
4174
4175 // Check for overflow: ((lhs << rhs) >> rhs) == lhs.
4176 masm.movePtr(output, scratch2);
4177 masm.flexibleRshiftPtrArithmetic(scratch1, scratch2);
4178 masm.branchPtr(Assembler::NotEqual, scratch2, lhs, failure->label());
4179
4180 masm.jump(&done);
4181 }
4182 masm.bind(&rightShift);
4183
4184 masm.flexibleRshiftPtrArithmetic(rhs, output);
4185
4186 masm.bind(&done);
4187 return true;
4188 }
4189
4190 bool CacheIRCompiler::emitBigIntPtrNegation(IntPtrOperandId inputId,
4191 IntPtrOperandId resultId) {
4192 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4193
4194 Register input = allocator.useRegister(masm, inputId);
4195 Register output = allocator.defineRegister(masm, resultId);
4196
4197 FailurePath* failure;
4198 if (!addFailurePath(&failure)) {
4199 return false;
4200 }
4201
4202 masm.movePtr(input, output);
4203 masm.branchNegPtr(Assembler::Overflow, output, failure->label());
4204 return true;
4205 }
4206
4207 bool CacheIRCompiler::emitBigIntPtrInc(IntPtrOperandId inputId,
4208 IntPtrOperandId resultId) {
4209 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4210
4211 Register input = allocator.useRegister(masm, inputId);
4212 Register output = allocator.defineRegister(masm, resultId);
4213
4214 FailurePath* failure;
4215 if (!addFailurePath(&failure)) {
4216 return false;
4217 }
4218
4219 masm.movePtr(input, output);
4220 masm.branchAddPtr(Assembler::Overflow, Imm32(1), output, failure->label());
4221 return true;
4222 }
4223
4224 bool CacheIRCompiler::emitBigIntPtrDec(IntPtrOperandId inputId,
4225 IntPtrOperandId resultId) {
4226 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4227
4228 Register input = allocator.useRegister(masm, inputId);
4229 Register output = allocator.defineRegister(masm, resultId);
4230
4231 FailurePath* failure;
4232 if (!addFailurePath(&failure)) {
4233 return false;
4234 }
4235
4236 masm.movePtr(input, output);
4237 masm.branchSubPtr(Assembler::Overflow, Imm32(1), output, failure->label());
4238 return true;
4239 }
4240
4241 bool CacheIRCompiler::emitBigIntPtrNot(IntPtrOperandId inputId,
4242 IntPtrOperandId resultId) {
4243 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4244
4245 Register input = allocator.useRegister(masm, inputId);
4246 Register output = allocator.defineRegister(masm, resultId);
4247
4248 masm.movePtr(input, output);
4249 masm.notPtr(output);
4250 return true;
4251 }
4252
4253 bool CacheIRCompiler::emitTruncateDoubleToUInt32(NumberOperandId inputId,
4254 Int32OperandId resultId) {
4255 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4256 Register res = allocator.defineRegister(masm, resultId);
4257
4258 AutoScratchFloatRegister floatReg(this);
4259
4260 allocator.ensureDoubleRegister(masm, inputId, floatReg);
4261
4262 TruncateDoubleModUint32(masm, floatReg, res, liveVolatileRegs());
4263 return true;
4264 }
4265
4266 bool CacheIRCompiler::emitDoubleToUint8Clamped(NumberOperandId inputId,
4267 Int32OperandId resultId) {
4268 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4269 Register res = allocator.defineRegister(masm, resultId);
4270
4271 AutoScratchFloatRegister floatReg(this);
4272
4273 allocator.ensureDoubleRegister(masm, inputId, floatReg);
4274
4275 masm.clampDoubleToUint8(floatReg, res);
4276 return true;
4277 }
4278
4279 bool CacheIRCompiler::emitLoadArgumentsObjectLengthResult(ObjOperandId objId) {
4280 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4281 AutoOutputRegister output(*this);
4282 Register obj = allocator.useRegister(masm, objId);
4283 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4284
4285 FailurePath* failure;
4286 if (!addFailurePath(&failure)) {
4287 return false;
4288 }
4289
4290 masm.loadArgumentsObjectLength(obj, scratch, failure->label());
4291
4292 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
4293 return true;
4294 }
4295
4296 bool CacheIRCompiler::emitLoadArgumentsObjectLength(ObjOperandId objId,
4297 Int32OperandId resultId) {
4298 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4299 Register obj = allocator.useRegister(masm, objId);
4300 Register res = allocator.defineRegister(masm, resultId);
4301
4302 FailurePath* failure;
4303 if (!addFailurePath(&failure)) {
4304 return false;
4305 }
4306
4307 masm.loadArgumentsObjectLength(obj, res, failure->label());
4308 return true;
4309 }
4310
4311 bool CacheIRCompiler::emitLoadArrayBufferByteLengthInt32Result(
4312 ObjOperandId objId) {
4313 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4314 AutoOutputRegister output(*this);
4315 Register obj = allocator.useRegister(masm, objId);
4316 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4317
4318 FailurePath* failure;
4319 if (!addFailurePath(&failure)) {
4320 return false;
4321 }
4322
4323 masm.loadArrayBufferByteLengthIntPtr(obj, scratch);
4324 masm.guardNonNegativeIntPtrToInt32(scratch, failure->label());
4325 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
4326 return true;
4327 }
4328
4329 bool CacheIRCompiler::emitLoadArrayBufferByteLengthDoubleResult(
4330 ObjOperandId objId) {
4331 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4332 AutoOutputRegister output(*this);
4333 Register obj = allocator.useRegister(masm, objId);
4334 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4335
4336 ScratchDoubleScope fpscratch(masm);
4337 masm.loadArrayBufferByteLengthIntPtr(obj, scratch);
4338 masm.convertIntPtrToDouble(scratch, fpscratch);
4339 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
4340 return true;
4341 }
4342
4343 bool CacheIRCompiler::emitLoadArrayBufferViewLengthInt32Result(
4344 ObjOperandId objId) {
4345 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4346 AutoOutputRegister output(*this);
4347 Register obj = allocator.useRegister(masm, objId);
4348 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4349
4350 FailurePath* failure;
4351 if (!addFailurePath(&failure)) {
4352 return false;
4353 }
4354
4355 masm.loadArrayBufferViewLengthIntPtr(obj, scratch);
4356 masm.guardNonNegativeIntPtrToInt32(scratch, failure->label());
4357 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
4358 return true;
4359 }
4360
4361 bool CacheIRCompiler::emitLoadArrayBufferViewLengthDoubleResult(
4362 ObjOperandId objId) {
4363 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4364 AutoOutputRegister output(*this);
4365 Register obj = allocator.useRegister(masm, objId);
4366 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4367
4368 ScratchDoubleScope fpscratch(masm);
4369 masm.loadArrayBufferViewLengthIntPtr(obj, scratch);
4370 masm.convertIntPtrToDouble(scratch, fpscratch);
4371 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
4372 return true;
4373 }
4374
4375 bool CacheIRCompiler::emitLoadBoundFunctionNumArgs(ObjOperandId objId,
4376 Int32OperandId resultId) {
4377 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4378
4379 Register obj = allocator.useRegister(masm, objId);
4380 Register output = allocator.defineRegister(masm, resultId);
4381
4382 masm.unboxInt32(Address(obj, BoundFunctionObject::offsetOfFlagsSlot()),
4383 output);
4384 masm.rshift32(Imm32(BoundFunctionObject::NumBoundArgsShift), output);
4385 return true;
4386 }
4387
4388 bool CacheIRCompiler::emitLoadBoundFunctionTarget(ObjOperandId objId,
4389 ObjOperandId resultId) {
4390 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4391
4392 Register obj = allocator.useRegister(masm, objId);
4393 Register output = allocator.defineRegister(masm, resultId);
4394
4395 masm.unboxObject(Address(obj, BoundFunctionObject::offsetOfTargetSlot()),
4396 output);
4397 return true;
4398 }
4399
4400 bool CacheIRCompiler::emitLoadBoundFunctionArgument(ObjOperandId objId,
4401 uint32_t index,
4402 ValOperandId resultId) {
4403 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4404
4405 Register obj = allocator.useRegister(masm, objId);
4406 ValueOperand output = allocator.defineValueRegister(masm, resultId);
4407 AutoScratchRegister scratch(allocator, masm);
4408
4409 constexpr size_t inlineArgsOffset =
4410 BoundFunctionObject::offsetOfFirstInlineBoundArg();
4411
4412 masm.unboxObject(Address(obj, inlineArgsOffset), scratch);
4413 masm.loadPtr(Address(scratch, NativeObject::offsetOfElements()), scratch);
4414 masm.loadValue(Address(scratch, index * sizeof(Value)), output);
4415 return true;
4416 }
4417
4418 bool CacheIRCompiler::emitGuardBoundFunctionIsConstructor(ObjOperandId objId) {
4419 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4420
4421 Register obj = allocator.useRegister(masm, objId);
4422
4423 FailurePath* failure;
4424 if (!addFailurePath(&failure)) {
4425 return false;
4426 }
4427
4428 Address flagsSlot(obj, BoundFunctionObject::offsetOfFlagsSlot());
4429 masm.branchTest32(Assembler::Zero, flagsSlot,
4430 Imm32(BoundFunctionObject::IsConstructorFlag),
4431 failure->label());
4432 return true;
4433 }
4434
4435 bool CacheIRCompiler::emitGuardObjectIdentity(ObjOperandId obj1Id,
4436 ObjOperandId obj2Id) {
4437 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4438
4439 Register obj1 = allocator.useRegister(masm, obj1Id);
4440 Register obj2 = allocator.useRegister(masm, obj2Id);
4441
4442 FailurePath* failure;
4443 if (!addFailurePath(&failure)) {
4444 return false;
4445 }
4446
4447 masm.branchPtr(Assembler::NotEqual, obj1, obj2, failure->label());
4448 return true;
4449 }
4450
4451 bool CacheIRCompiler::emitLoadFunctionLengthResult(ObjOperandId objId) {
4452 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4453 AutoOutputRegister output(*this);
4454 Register obj = allocator.useRegister(masm, objId);
4455 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4456
4457 FailurePath* failure;
4458 if (!addFailurePath(&failure)) {
4459 return false;
4460 }
4461
4462 // Get the JSFunction flags and arg count.
4463 masm.load32(Address(obj, JSFunction::offsetOfFlagsAndArgCount()), scratch);
4464
4465 // Functions with a SelfHostedLazyScript must be compiled with the slow-path
4466 // before the function length is known. If the length was previously resolved,
4467 // the length property may be shadowed.
4468 masm.branchTest32(
4469 Assembler::NonZero, scratch,
4470 Imm32(FunctionFlags::SELFHOSTLAZY | FunctionFlags::RESOLVED_LENGTH),
4471 failure->label());
4472
4473 masm.loadFunctionLength(obj, scratch, scratch, failure->label());
4474 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
4475 return true;
4476 }
4477
4478 bool CacheIRCompiler::emitLoadFunctionNameResult(ObjOperandId objId) {
4479 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4480 AutoOutputRegister output(*this);
4481 Register obj = allocator.useRegister(masm, objId);
4482 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4483
4484 FailurePath* failure;
4485 if (!addFailurePath(&failure)) {
4486 return false;
4487 }
4488
4489 masm.loadFunctionName(obj, scratch, ImmGCPtr(cx_->names().empty_),
4490 failure->label());
4491
4492 masm.tagValue(JSVAL_TYPE_STRING, scratch, output.valueReg());
4493 return true;
4494 }
4495
4496 bool CacheIRCompiler::emitLinearizeForCharAccess(StringOperandId strId,
4497 Int32OperandId indexId,
4498 StringOperandId resultId) {
4499 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4500 Register str = allocator.useRegister(masm, strId);
4501 Register index = allocator.useRegister(masm, indexId);
4502 Register result = allocator.defineRegister(masm, resultId);
4503 AutoScratchRegister scratch(allocator, masm);
4504
4505 FailurePath* failure;
4506 if (!addFailurePath(&failure)) {
4507 return false;
4508 }
4509
4510 Label done;
4511 masm.movePtr(str, result);
4512
4513 // We can omit the bounds check, because we only compare the index against the
4514 // string length. In the worst case we unnecessarily linearize the string
4515 // when the index is out-of-bounds.
4516
4517 masm.branchIfCanLoadStringChar(str, index, scratch, &done);
4518 {
4519 LiveRegisterSet volatileRegs = liveVolatileRegs();
4520 masm.PushRegsInMask(volatileRegs);
4521
4522 using Fn = JSLinearString* (*)(JSString*);
4523 masm.setupUnalignedABICall(scratch);
4524 masm.passABIArg(str);
4525 masm.callWithABI<Fn, js::jit::LinearizeForCharAccessPure>();
4526 masm.storeCallPointerResult(result);
4527
4528 LiveRegisterSet ignore;
4529 ignore.add(result);
4530 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
4531
4532 masm.branchTestPtr(Assembler::Zero, result, result, failure->label());
4533 }
4534
4535 masm.bind(&done);
4536 return true;
4537 }
4538
4539 bool CacheIRCompiler::emitLinearizeForCodePointAccess(
4540 StringOperandId strId, Int32OperandId indexId, StringOperandId resultId) {
4541 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4542 Register str = allocator.useRegister(masm, strId);
4543 Register index = allocator.useRegister(masm, indexId);
4544 Register result = allocator.defineRegister(masm, resultId);
4545 AutoScratchRegister scratch1(allocator, masm);
4546 AutoScratchRegister scratch2(allocator, masm);
4547
4548 FailurePath* failure;
4549 if (!addFailurePath(&failure)) {
4550 return false;
4551 }
4552
4553 Label done;
4554 masm.movePtr(str, result);
4555
4556 // We can omit the bounds check, because we only compare the index against the
4557 // string length. In the worst case we unnecessarily linearize the string
4558 // when the index is out-of-bounds.
4559
4560 masm.branchIfCanLoadStringCodePoint(str, index, scratch1, scratch2, &done);
4561 {
4562 LiveRegisterSet volatileRegs = liveVolatileRegs();
4563 masm.PushRegsInMask(volatileRegs);
4564
4565 using Fn = JSLinearString* (*)(JSString*);
4566 masm.setupUnalignedABICall(scratch1);
4567 masm.passABIArg(str);
4568 masm.callWithABI<Fn, js::jit::LinearizeForCharAccessPure>();
4569 masm.storeCallPointerResult(result);
4570
4571 LiveRegisterSet ignore;
4572 ignore.add(result);
4573 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
4574
4575 masm.branchTestPtr(Assembler::Zero, result, result, failure->label());
4576 }
4577
4578 masm.bind(&done);
4579 return true;
4580 }
4581
4582 bool CacheIRCompiler::emitToRelativeStringIndex(Int32OperandId indexId,
4583 StringOperandId strId,
4584 Int32OperandId resultId) {
4585 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4586 Register index = allocator.useRegister(masm, indexId);
4587 Register str = allocator.useRegister(masm, strId);
4588 Register result = allocator.defineRegister(masm, resultId);
4589
4590 // If |index| is non-negative, it's an index relative to the start of the
4591 // string. Otherwise it's an index relative to the end of the string.
4592 masm.move32(Imm32(0), result);
4593 masm.cmp32Load32(Assembler::LessThan, index, Imm32(0),
4594 Address(str, JSString::offsetOfLength()), result);
4595 masm.add32(index, result);
4596 return true;
4597 }
4598
4599 bool CacheIRCompiler::emitLoadStringLengthResult(StringOperandId strId) {
4600 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4601 AutoOutputRegister output(*this);
4602 Register str = allocator.useRegister(masm, strId);
4603 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
4604
4605 masm.loadStringLength(str, scratch);
4606 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
4607 return true;
4608 }
4609
4610 bool CacheIRCompiler::emitLoadStringCharCodeResult(StringOperandId strId,
4611 Int32OperandId indexId,
4612 bool handleOOB) {
4613 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4614 AutoOutputRegister output(*this);
4615 Register str = allocator.useRegister(masm, strId);
4616 Register index = allocator.useRegister(masm, indexId);
4617 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
4618 AutoScratchRegisterMaybeOutputType scratch2(allocator, masm, output);
4619 AutoScratchRegister scratch3(allocator, masm);
4620
4621 // Bounds check, load string char.
4622 Label done;
4623 if (!handleOOB) {
4624 FailurePath* failure;
4625 if (!addFailurePath(&failure)) {
4626 return false;
4627 }
4628
4629 masm.spectreBoundsCheck32(index, Address(str, JSString::offsetOfLength()),
4630 scratch1, failure->label());
4631 masm.loadStringChar(str, index, scratch1, scratch2, scratch3,
4632 failure->label());
4633 } else {
4634 // Return NaN for out-of-bounds access.
4635 masm.moveValue(JS::NaNValue(), output.valueReg());
4636
4637 // The bounds check mustn't use a scratch register which aliases the output.
4638 MOZ_ASSERT(!output.valueReg().aliases(scratch3));
4639
4640 // This CacheIR op is always preceded by |LinearizeForCharAccess|, so we're
4641 // guaranteed to see no nested ropes.
4642 Label loadFailed;
4643 masm.spectreBoundsCheck32(index, Address(str, JSString::offsetOfLength()),
4644 scratch3, &done);
4645 masm.loadStringChar(str, index, scratch1, scratch2, scratch3, &loadFailed);
4646
4647 Label loadedChar;
4648 masm.jump(&loadedChar);
4649 masm.bind(&loadFailed);
4650 masm.assumeUnreachable("loadStringChar can't fail for linear strings");
4651 masm.bind(&loadedChar);
4652 }
4653
4654 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
4655 masm.bind(&done);
4656 return true;
4657 }
4658
4659 bool CacheIRCompiler::emitLoadStringCodePointResult(StringOperandId strId,
4660 Int32OperandId indexId,
4661 bool handleOOB) {
4662 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4663 AutoOutputRegister output(*this);
4664 Register str = allocator.useRegister(masm, strId);
4665 Register index = allocator.useRegister(masm, indexId);
4666 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
4667 AutoScratchRegisterMaybeOutputType scratch2(allocator, masm, output);
4668 AutoScratchRegister scratch3(allocator, masm);
4669
4670 // Bounds check, load string char.
4671 Label done;
4672 if (!handleOOB) {
4673 FailurePath* failure;
4674 if (!addFailurePath(&failure)) {
4675 return false;
4676 }
4677
4678 masm.spectreBoundsCheck32(index, Address(str, JSString::offsetOfLength()),
4679 scratch1, failure->label());
4680 masm.loadStringCodePoint(str, index, scratch1, scratch2, scratch3,
4681 failure->label());
4682 } else {
4683 // Return undefined for out-of-bounds access.
4684 masm.moveValue(JS::UndefinedValue(), output.valueReg());
4685
4686 // The bounds check mustn't use a scratch register which aliases the output.
4687 MOZ_ASSERT(!output.valueReg().aliases(scratch3));
4688
4689 // This CacheIR op is always preceded by |LinearizeForCodePointAccess|, so
4690 // we're guaranteed to see no nested ropes or split surrogates.
4691 Label loadFailed;
4692 masm.spectreBoundsCheck32(index, Address(str, JSString::offsetOfLength()),
4693 scratch3, &done);
4694 masm.loadStringCodePoint(str, index, scratch1, scratch2, scratch3,
4695 &loadFailed);
4696
4697 Label loadedChar;
4698 masm.jump(&loadedChar);
4699 masm.bind(&loadFailed);
4700 masm.assumeUnreachable("loadStringCodePoint can't fail for linear strings");
4701 masm.bind(&loadedChar);
4702 }
4703
4704 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
4705 masm.bind(&done);
4706 return true;
4707 }
4708
4709 bool CacheIRCompiler::emitNewMapObjectResult(uint32_t templateObjectOffset) {
4710 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4711
4712 AutoCallVM callvm(masm, this, allocator);
4713
4714 callvm.prepare();
4715 masm.Push(ImmPtr(nullptr)); // proto
4716
4717 using Fn = MapObject* (*)(JSContext*, HandleObject);
4718 callvm.call<Fn, MapObject::create>();
4719 return true;
4720 }
4721
4722 bool CacheIRCompiler::emitNewSetObjectResult(uint32_t templateObjectOffset) {
4723 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4724
4725 AutoCallVM callvm(masm, this, allocator);
4726
4727 callvm.prepare();
4728 masm.Push(ImmPtr(nullptr)); // proto
4729
4730 using Fn = SetObject* (*)(JSContext*, HandleObject);
4731 callvm.call<Fn, SetObject::create>();
4732 return true;
4733 }
4734
4735 bool CacheIRCompiler::emitNewMapObjectFromIterableResult(
4736 uint32_t templateObjectOffset, ValOperandId iterableId) {
4737 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4738
4739 AutoCallVM callvm(masm, this, allocator);
4740 ValueOperand iterable = allocator.useValueRegister(masm, iterableId);
4741
4742 callvm.prepare();
4743 masm.Push(ImmPtr(nullptr)); // allocatedFromJit
4744 masm.Push(iterable);
4745 masm.Push(ImmPtr(nullptr)); // proto
4746
4747 using Fn = MapObject* (*)(JSContext*, Handle<JSObject*>, Handle<Value>,
4748 Handle<MapObject*>);
4749 callvm.call<Fn, MapObject::createFromIterable>();
4750 return true;
4751 }
4752
4753 bool CacheIRCompiler::emitNewSetObjectFromIterableResult(
4754 uint32_t templateObjectOffset, ValOperandId iterableId) {
4755 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4756
4757 AutoCallVM callvm(masm, this, allocator);
4758 ValueOperand iterable = allocator.useValueRegister(masm, iterableId);
4759
4760 callvm.prepare();
4761 masm.Push(ImmPtr(nullptr)); // allocatedFromJit
4762 masm.Push(iterable);
4763 masm.Push(ImmPtr(nullptr)); // proto
4764
4765 using Fn = SetObject* (*)(JSContext*, Handle<JSObject*>, Handle<Value>,
4766 Handle<SetObject*>);
4767 callvm.call<Fn, SetObject::createFromIterable>();
4768 return true;
4769 }
4770
4771 bool CacheIRCompiler::emitNewStringObjectResult(uint32_t templateObjectOffset,
4772 StringOperandId strId) {
4773 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4774
4775 AutoCallVM callvm(masm, this, allocator);
4776
4777 Register str = allocator.useRegister(masm, strId);
4778
4779 callvm.prepare();
4780 masm.Push(str);
4781
4782 using Fn = JSObject* (*)(JSContext*, HandleString);
4783 callvm.call<Fn, NewStringObject>();
4784 return true;
4785 }
4786
4787 bool CacheIRCompiler::emitStringIncludesResult(StringOperandId strId,
4788 StringOperandId searchStrId) {
4789 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4790
4791 AutoCallVM callvm(masm, this, allocator);
4792
4793 Register str = allocator.useRegister(masm, strId);
4794 Register searchStr = allocator.useRegister(masm, searchStrId);
4795
4796 callvm.prepare();
4797 masm.Push(searchStr);
4798 masm.Push(str);
4799
4800 using Fn = bool (*)(JSContext*, HandleString, HandleString, bool*);
4801 callvm.call<Fn, js::StringIncludes>();
4802 return true;
4803 }
4804
4805 bool CacheIRCompiler::emitStringIndexOfResult(StringOperandId strId,
4806 StringOperandId searchStrId) {
4807 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4808
4809 AutoCallVM callvm(masm, this, allocator);
4810
4811 Register str = allocator.useRegister(masm, strId);
4812 Register searchStr = allocator.useRegister(masm, searchStrId);
4813
4814 callvm.prepare();
4815 masm.Push(searchStr);
4816 masm.Push(str);
4817
4818 using Fn = bool (*)(JSContext*, HandleString, HandleString, int32_t*);
4819 callvm.call<Fn, js::StringIndexOf>();
4820 return true;
4821 }
4822
4823 bool CacheIRCompiler::emitStringLastIndexOfResult(StringOperandId strId,
4824 StringOperandId searchStrId) {
4825 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4826
4827 AutoCallVM callvm(masm, this, allocator);
4828
4829 Register str = allocator.useRegister(masm, strId);
4830 Register searchStr = allocator.useRegister(masm, searchStrId);
4831
4832 callvm.prepare();
4833 masm.Push(searchStr);
4834 masm.Push(str);
4835
4836 using Fn = bool (*)(JSContext*, HandleString, HandleString, int32_t*);
4837 callvm.call<Fn, js::StringLastIndexOf>();
4838 return true;
4839 }
4840
4841 bool CacheIRCompiler::emitStringStartsWithResult(StringOperandId strId,
4842 StringOperandId searchStrId) {
4843 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4844
4845 AutoCallVM callvm(masm, this, allocator);
4846
4847 Register str = allocator.useRegister(masm, strId);
4848 Register searchStr = allocator.useRegister(masm, searchStrId);
4849
4850 callvm.prepare();
4851 masm.Push(searchStr);
4852 masm.Push(str);
4853
4854 using Fn = bool (*)(JSContext*, HandleString, HandleString, bool*);
4855 callvm.call<Fn, js::StringStartsWith>();
4856 return true;
4857 }
4858
4859 bool CacheIRCompiler::emitStringEndsWithResult(StringOperandId strId,
4860 StringOperandId searchStrId) {
4861 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4862
4863 AutoCallVM callvm(masm, this, allocator);
4864
4865 Register str = allocator.useRegister(masm, strId);
4866 Register searchStr = allocator.useRegister(masm, searchStrId);
4867
4868 callvm.prepare();
4869 masm.Push(searchStr);
4870 masm.Push(str);
4871
4872 using Fn = bool (*)(JSContext*, HandleString, HandleString, bool*);
4873 callvm.call<Fn, js::StringEndsWith>();
4874 return true;
4875 }
4876
4877 bool CacheIRCompiler::emitStringToLowerCaseResult(StringOperandId strId) {
4878 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4879
4880 AutoCallVM callvm(masm, this, allocator);
4881
4882 Register str = allocator.useRegister(masm, strId);
4883
4884 callvm.prepare();
4885 masm.Push(str);
4886
4887 using Fn = JSLinearString* (*)(JSContext*, JSString*);
4888 callvm.call<Fn, js::StringToLowerCase>();
4889 return true;
4890 }
4891
4892 bool CacheIRCompiler::emitStringToUpperCaseResult(StringOperandId strId) {
4893 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4894
4895 AutoCallVM callvm(masm, this, allocator);
4896
4897 Register str = allocator.useRegister(masm, strId);
4898
4899 callvm.prepare();
4900 masm.Push(str);
4901
4902 using Fn = JSLinearString* (*)(JSContext*, JSString*);
4903 callvm.call<Fn, js::StringToUpperCase>();
4904 return true;
4905 }
4906
4907 bool CacheIRCompiler::emitStringTrimResult(StringOperandId strId) {
4908 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4909
4910 AutoCallVM callvm(masm, this, allocator);
4911
4912 Register str = allocator.useRegister(masm, strId);
4913
4914 callvm.prepare();
4915 masm.Push(str);
4916
4917 using Fn = JSString* (*)(JSContext*, HandleString);
4918 callvm.call<Fn, js::StringTrim>();
4919 return true;
4920 }
4921
4922 bool CacheIRCompiler::emitStringTrimStartResult(StringOperandId strId) {
4923 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4924
4925 AutoCallVM callvm(masm, this, allocator);
4926
4927 Register str = allocator.useRegister(masm, strId);
4928
4929 callvm.prepare();
4930 masm.Push(str);
4931
4932 using Fn = JSString* (*)(JSContext*, HandleString);
4933 callvm.call<Fn, js::StringTrimStart>();
4934 return true;
4935 }
4936
4937 bool CacheIRCompiler::emitStringTrimEndResult(StringOperandId strId) {
4938 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4939
4940 AutoCallVM callvm(masm, this, allocator);
4941
4942 Register str = allocator.useRegister(masm, strId);
4943
4944 callvm.prepare();
4945 masm.Push(str);
4946
4947 using Fn = JSString* (*)(JSContext*, HandleString);
4948 callvm.call<Fn, js::StringTrimEnd>();
4949 return true;
4950 }
4951
4952 bool CacheIRCompiler::emitLoadArgumentsObjectArgResult(ObjOperandId objId,
4953 Int32OperandId indexId) {
4954 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4955 AutoOutputRegister output(*this);
4956 Register obj = allocator.useRegister(masm, objId);
4957 Register index = allocator.useRegister(masm, indexId);
4958 AutoScratchRegister scratch(allocator, masm);
4959
4960 FailurePath* failure;
4961 if (!addFailurePath(&failure)) {
4962 return false;
4963 }
4964
4965 masm.loadArgumentsObjectElement(obj, index, output.valueReg(), scratch,
4966 failure->label());
4967 return true;
4968 }
4969
4970 bool CacheIRCompiler::emitLoadArgumentsObjectArgHoleResult(
4971 ObjOperandId objId, Int32OperandId indexId) {
4972 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4973 AutoOutputRegister output(*this);
4974 Register obj = allocator.useRegister(masm, objId);
4975 Register index = allocator.useRegister(masm, indexId);
4976 AutoScratchRegister scratch(allocator, masm);
4977
4978 FailurePath* failure;
4979 if (!addFailurePath(&failure)) {
4980 return false;
4981 }
4982
4983 masm.loadArgumentsObjectElementHole(obj, index, output.valueReg(), scratch,
4984 failure->label());
4985 return true;
4986 }
4987
4988 bool CacheIRCompiler::emitLoadArgumentsObjectArgExistsResult(
4989 ObjOperandId objId, Int32OperandId indexId) {
4990 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
4991 AutoOutputRegister output(*this);
4992 Register obj = allocator.useRegister(masm, objId);
4993 Register index = allocator.useRegister(masm, indexId);
4994 AutoScratchRegister scratch1(allocator, masm);
4995 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
4996
4997 FailurePath* failure;
4998 if (!addFailurePath(&failure)) {
4999 return false;
5000 }
5001
5002 masm.loadArgumentsObjectElementExists(obj, index, scratch2, scratch1,
5003 failure->label());
5004 EmitStoreResult(masm, scratch2, JSVAL_TYPE_BOOLEAN, output);
5005 return true;
5006 }
5007
5008 bool CacheIRCompiler::emitLoadDenseElementResult(ObjOperandId objId,
5009 Int32OperandId indexId) {
5010 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5011 AutoOutputRegister output(*this);
5012 Register obj = allocator.useRegister(masm, objId);
5013 Register index = allocator.useRegister(masm, indexId);
5014 AutoScratchRegister scratch1(allocator, masm);
5015 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
5016
5017 FailurePath* failure;
5018 if (!addFailurePath(&failure)) {
5019 return false;
5020 }
5021
5022 // Load obj->elements.
5023 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch1);
5024
5025 // Bounds check.
5026 Address initLength(scratch1, ObjectElements::offsetOfInitializedLength());
5027 masm.spectreBoundsCheck32(index, initLength, scratch2, failure->label());
5028
5029 // Hole check.
5030 BaseObjectElementIndex element(scratch1, index);
5031 masm.branchTestMagic(Assembler::Equal, element, failure->label());
5032 masm.loadTypedOrValue(element, output);
5033 return true;
5034 }
5035
5036 bool CacheIRCompiler::emitGuardInt32IsNonNegative(Int32OperandId indexId) {
5037 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5038 Register index = allocator.useRegister(masm, indexId);
5039
5040 FailurePath* failure;
5041 if (!addFailurePath(&failure)) {
5042 return false;
5043 }
5044
5045 masm.branch32(Assembler::LessThan, index, Imm32(0), failure->label());
5046 return true;
5047 }
5048
5049 bool CacheIRCompiler::emitGuardIndexIsNotDenseElement(ObjOperandId objId,
5050 Int32OperandId indexId) {
5051 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5052 Register obj = allocator.useRegister(masm, objId);
5053 Register index = allocator.useRegister(masm, indexId);
5054 AutoScratchRegister scratch(allocator, masm);
5055 AutoSpectreBoundsScratchRegister spectreScratch(allocator, masm);
5056
5057 FailurePath* failure;
5058 if (!addFailurePath(&failure)) {
5059 return false;
5060 }
5061
5062 // Load obj->elements.
5063 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
5064
5065 // Ensure index >= initLength or the element is a hole.
5066 Label notDense;
5067 Address capacity(scratch, ObjectElements::offsetOfInitializedLength());
5068 masm.spectreBoundsCheck32(index, capacity, spectreScratch, &notDense);
5069
5070 BaseValueIndex element(scratch, index);
5071 masm.branchTestMagic(Assembler::Equal, element, &notDense);
5072
5073 masm.jump(failure->label());
5074
5075 masm.bind(&notDense);
5076 return true;
5077 }
5078
5079 bool CacheIRCompiler::emitGuardIndexIsValidUpdateOrAdd(ObjOperandId objId,
5080 Int32OperandId indexId) {
5081 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5082 Register obj = allocator.useRegister(masm, objId);
5083 Register index = allocator.useRegister(masm, indexId);
5084 AutoScratchRegister scratch(allocator, masm);
5085 AutoSpectreBoundsScratchRegister spectreScratch(allocator, masm);
5086
5087 FailurePath* failure;
5088 if (!addFailurePath(&failure)) {
5089 return false;
5090 }
5091
5092 // Load obj->elements.
5093 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
5094
5095 Label success;
5096
5097 // If length is writable, branch to &success. All indices are writable.
5098 Address flags(scratch, ObjectElements::offsetOfFlags());
5099 masm.branchTest32(Assembler::Zero, flags,
5100 Imm32(ObjectElements::Flags::NONWRITABLE_ARRAY_LENGTH),
5101 &success);
5102
5103 // Otherwise, ensure index is in bounds.
5104 Address length(scratch, ObjectElements::offsetOfLength());
5105 masm.spectreBoundsCheck32(index, length, spectreScratch,
5106 /* failure = */ failure->label());
5107 masm.bind(&success);
5108 return true;
5109 }
5110
5111 bool CacheIRCompiler::emitGuardTagNotEqual(ValueTagOperandId lhsId,
5112 ValueTagOperandId rhsId) {
5113 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5114 Register lhs = allocator.useRegister(masm, lhsId);
5115 Register rhs = allocator.useRegister(masm, rhsId);
5116
5117 FailurePath* failure;
5118 if (!addFailurePath(&failure)) {
5119 return false;
5120 }
5121
5122 Label done;
5123 masm.branch32(Assembler::Equal, lhs, rhs, failure->label());
5124
5125 // If both lhs and rhs are numbers, can't use tag comparison to do inequality
5126 // comparison
5127 masm.branchTestNumber(Assembler::NotEqual, lhs, &done);
5128 masm.branchTestNumber(Assembler::NotEqual, rhs, &done);
5129 masm.jump(failure->label());
5130
5131 masm.bind(&done);
5132 return true;
5133 }
5134
5135 bool CacheIRCompiler::emitGuardXrayExpandoShapeAndDefaultProto(
5136 ObjOperandId objId, uint32_t shapeWrapperOffset) {
5137 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5138
5139 Register obj = allocator.useRegister(masm, objId);
5140 StubFieldOffset shapeWrapper(shapeWrapperOffset, StubField::Type::JSObject);
5141
5142 AutoScratchRegister scratch(allocator, masm);
5143 AutoScratchRegister scratch2(allocator, masm);
5144 AutoScratchRegister scratch3(allocator, masm);
5145
5146 FailurePath* failure;
5147 if (!addFailurePath(&failure)) {
5148 return false;
5149 }
5150
5151 masm.loadPtr(Address(obj, ProxyObject::offsetOfReservedSlots()), scratch);
5152 Address holderAddress(scratch,
5153 sizeof(Value) * GetXrayJitInfo()->xrayHolderSlot);
5154 Address expandoAddress(scratch, NativeObject::getFixedSlotOffset(
5155 GetXrayJitInfo()->holderExpandoSlot));
5156
5157 masm.fallibleUnboxObject(holderAddress, scratch, failure->label());
5158 masm.fallibleUnboxObject(expandoAddress, scratch, failure->label());
5159
5160 // Unwrap the expando before checking its shape.
5161 masm.loadPtr(Address(scratch, ProxyObject::offsetOfReservedSlots()), scratch);
5162 masm.unboxObject(
5163 Address(scratch, js::detail::ProxyReservedSlots::offsetOfPrivateSlot()),
5164 scratch);
5165
5166 emitLoadStubField(shapeWrapper, scratch2);
5167 LoadShapeWrapperContents(masm, scratch2, scratch2, failure->label());
5168 masm.branchTestObjShape(Assembler::NotEqual, scratch, scratch2, scratch3,
5169 scratch, failure->label());
5170
5171 // The reserved slots on the expando should all be in fixed slots.
5172 Address protoAddress(scratch, NativeObject::getFixedSlotOffset(
5173 GetXrayJitInfo()->expandoProtoSlot));
5174 masm.branchTestUndefined(Assembler::NotEqual, protoAddress, failure->label());
5175
5176 return true;
5177 }
5178
5179 bool CacheIRCompiler::emitGuardXrayNoExpando(ObjOperandId objId) {
5180 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5181
5182 Register obj = allocator.useRegister(masm, objId);
5183 AutoScratchRegister scratch(allocator, masm);
5184
5185 FailurePath* failure;
5186 if (!addFailurePath(&failure)) {
5187 return false;
5188 }
5189
5190 masm.loadPtr(Address(obj, ProxyObject::offsetOfReservedSlots()), scratch);
5191 Address holderAddress(scratch,
5192 sizeof(Value) * GetXrayJitInfo()->xrayHolderSlot);
5193 Address expandoAddress(scratch, NativeObject::getFixedSlotOffset(
5194 GetXrayJitInfo()->holderExpandoSlot));
5195
5196 Label done;
5197 masm.fallibleUnboxObject(holderAddress, scratch, &done);
5198 masm.branchTestObject(Assembler::Equal, expandoAddress, failure->label());
5199 masm.bind(&done);
5200
5201 return true;
5202 }
5203
5204 bool CacheIRCompiler::emitGuardNoAllocationMetadataBuilder(
5205 uint32_t builderAddrOffset) {
5206 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5207 AutoScratchRegister scratch(allocator, masm);
5208
5209 FailurePath* failure;
5210 if (!addFailurePath(&failure)) {
5211 return false;
5212 }
5213
5214 StubFieldOffset builderField(builderAddrOffset, StubField::Type::RawPointer);
5215 emitLoadStubField(builderField, scratch);
5216 masm.branchPtr(Assembler::NotEqual, Address(scratch, 0), ImmWord(0),
5217 failure->label());
5218
5219 return true;
5220 }
5221
5222 bool CacheIRCompiler::emitGuardFunctionHasJitEntry(ObjOperandId funId) {
5223 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5224 Register fun = allocator.useRegister(masm, funId);
5225
5226 FailurePath* failure;
5227 if (!addFailurePath(&failure)) {
5228 return false;
5229 }
5230
5231 masm.branchIfFunctionHasNoJitEntry(fun, failure->label());
5232 return true;
5233 }
5234
5235 bool CacheIRCompiler::emitGuardFunctionHasNoJitEntry(ObjOperandId funId) {
5236 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5237 Register obj = allocator.useRegister(masm, funId);
5238 AutoScratchRegister scratch(allocator, masm);
5239
5240 FailurePath* failure;
5241 if (!addFailurePath(&failure)) {
5242 return false;
5243 }
5244
5245 masm.branchIfFunctionHasJitEntry(obj, failure->label());
5246 return true;
5247 }
5248
5249 bool CacheIRCompiler::emitGuardFunctionIsNonBuiltinCtor(ObjOperandId funId) {
5250 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5251
5252 Register fun = allocator.useRegister(masm, funId);
5253 AutoScratchRegister scratch(allocator, masm);
5254
5255 FailurePath* failure;
5256 if (!addFailurePath(&failure)) {
5257 return false;
5258 }
5259
5260 masm.branchIfNotFunctionIsNonBuiltinCtor(fun, scratch, failure->label());
5261 return true;
5262 }
5263
5264 bool CacheIRCompiler::emitGuardFunctionIsConstructor(ObjOperandId funId) {
5265 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5266 Register funcReg = allocator.useRegister(masm, funId);
5267 AutoScratchRegister scratch(allocator, masm);
5268
5269 FailurePath* failure;
5270 if (!addFailurePath(&failure)) {
5271 return false;
5272 }
5273
5274 // Ensure obj is a constructor
5275 masm.branchTestFunctionFlags(funcReg, FunctionFlags::CONSTRUCTOR,
5276 Assembler::Zero, failure->label());
5277 return true;
5278 }
5279
5280 bool CacheIRCompiler::emitGuardNotClassConstructor(ObjOperandId funId) {
5281 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5282 Register fun = allocator.useRegister(masm, funId);
5283 AutoScratchRegister scratch(allocator, masm);
5284
5285 FailurePath* failure;
5286 if (!addFailurePath(&failure)) {
5287 return false;
5288 }
5289
5290 masm.branchFunctionKind(Assembler::Equal, FunctionFlags::ClassConstructor,
5291 fun, scratch, failure->label());
5292 return true;
5293 }
5294
5295 bool CacheIRCompiler::emitGuardArrayIsPacked(ObjOperandId arrayId) {
5296 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5297 Register array = allocator.useRegister(masm, arrayId);
5298 AutoScratchRegister scratch(allocator, masm);
5299 AutoScratchRegister scratch2(allocator, masm);
5300
5301 FailurePath* failure;
5302 if (!addFailurePath(&failure)) {
5303 return false;
5304 }
5305
5306 masm.branchArrayIsNotPacked(array, scratch, scratch2, failure->label());
5307 return true;
5308 }
5309
5310 bool CacheIRCompiler::emitGuardArgumentsObjectFlags(ObjOperandId objId,
5311 uint8_t flags) {
5312 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5313 Register obj = allocator.useRegister(masm, objId);
5314 AutoScratchRegister scratch(allocator, masm);
5315
5316 FailurePath* failure;
5317 if (!addFailurePath(&failure)) {
5318 return false;
5319 }
5320
5321 masm.branchTestArgumentsObjectFlags(obj, scratch, flags, Assembler::NonZero,
5322 failure->label());
5323 return true;
5324 }
5325
5326 bool CacheIRCompiler::emitGuardObjectHasSameRealm(ObjOperandId objId) {
5327 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5328
5329 Register obj = allocator.useRegister(masm, objId);
5330 AutoScratchRegister scratch(allocator, masm);
5331
5332 FailurePath* failure;
5333 if (!addFailurePath(&failure)) {
5334 return false;
5335 }
5336
5337 masm.guardObjectHasSameRealm(obj, scratch, failure->label());
5338 return true;
5339 }
5340
5341 bool CacheIRCompiler::emitLoadDenseElementHoleResult(ObjOperandId objId,
5342 Int32OperandId indexId) {
5343 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5344 AutoOutputRegister output(*this);
5345 Register obj = allocator.useRegister(masm, objId);
5346 Register index = allocator.useRegister(masm, indexId);
5347 AutoScratchRegister scratch1(allocator, masm);
5348 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
5349
5350 FailurePath* failure;
5351 if (!addFailurePath(&failure)) {
5352 return false;
5353 }
5354
5355 // Make sure the index is nonnegative.
5356 masm.branch32(Assembler::LessThan, index, Imm32(0), failure->label());
5357
5358 // Load obj->elements.
5359 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch1);
5360
5361 // Guard on the initialized length.
5362 Label hole;
5363 Address initLength(scratch1, ObjectElements::offsetOfInitializedLength());
5364 masm.spectreBoundsCheck32(index, initLength, scratch2, &hole);
5365
5366 // Load the value.
5367 Label done;
5368 masm.loadValue(BaseObjectElementIndex(scratch1, index), output.valueReg());
5369 masm.branchTestMagic(Assembler::NotEqual, output.valueReg(), &done);
5370
5371 // Load undefined for the hole.
5372 masm.bind(&hole);
5373 masm.moveValue(UndefinedValue(), output.valueReg());
5374
5375 masm.bind(&done);
5376 return true;
5377 }
5378
5379 bool CacheIRCompiler::emitLoadTypedArrayElementExistsResult(
5380 ObjOperandId objId, IntPtrOperandId indexId, ArrayBufferViewKind viewKind) {
5381 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5382 AutoOutputRegister output(*this);
5383 Register obj = allocator.useRegister(masm, objId);
5384 Register index = allocator.useRegister(masm, indexId);
5385 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5386 Maybe<AutoScratchRegister> scratch2;
5387 if (viewKind == ArrayBufferViewKind::Resizable) {
5388 scratch2.emplace(allocator, masm);
5389 }
5390
5391 Label outOfBounds, done;
5392
5393 // Bounds check.
5394 if (viewKind == ArrayBufferViewKind::FixedLength) {
5395 masm.loadArrayBufferViewLengthIntPtr(obj, scratch);
5396 } else {
5397 // Bounds check doesn't require synchronization. See IsValidIntegerIndex
5398 // abstract operation which reads the underlying buffer byte length using
5399 // "unordered" memory order.
5400 auto sync = Synchronization::None();
5401
5402 masm.loadResizableTypedArrayLengthIntPtr(sync, obj, scratch, *scratch2);
5403 }
5404 masm.branchPtr(Assembler::BelowOrEqual, scratch, index, &outOfBounds);
5405 EmitStoreBoolean(masm, true, output);
5406 masm.jump(&done);
5407
5408 masm.bind(&outOfBounds);
5409 EmitStoreBoolean(masm, false, output);
5410
5411 masm.bind(&done);
5412 return true;
5413 }
5414
5415 bool CacheIRCompiler::emitLoadDenseElementExistsResult(ObjOperandId objId,
5416 Int32OperandId indexId) {
5417 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5418 AutoOutputRegister output(*this);
5419 Register obj = allocator.useRegister(masm, objId);
5420 Register index = allocator.useRegister(masm, indexId);
5421 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5422
5423 FailurePath* failure;
5424 if (!addFailurePath(&failure)) {
5425 return false;
5426 }
5427
5428 // Load obj->elements.
5429 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
5430
5431 // Bounds check. Unsigned compare sends negative indices to next IC.
5432 Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
5433 masm.branch32(Assembler::BelowOrEqual, initLength, index, failure->label());
5434
5435 // Hole check.
5436 BaseObjectElementIndex element(scratch, index);
5437 masm.branchTestMagic(Assembler::Equal, element, failure->label());
5438
5439 EmitStoreBoolean(masm, true, output);
5440 return true;
5441 }
5442
5443 bool CacheIRCompiler::emitLoadDenseElementHoleExistsResult(
5444 ObjOperandId objId, Int32OperandId indexId) {
5445 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5446 AutoOutputRegister output(*this);
5447 Register obj = allocator.useRegister(masm, objId);
5448 Register index = allocator.useRegister(masm, indexId);
5449 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5450
5451 FailurePath* failure;
5452 if (!addFailurePath(&failure)) {
5453 return false;
5454 }
5455
5456 // Make sure the index is nonnegative.
5457 masm.branch32(Assembler::LessThan, index, Imm32(0), failure->label());
5458
5459 // Load obj->elements.
5460 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
5461
5462 // Guard on the initialized length.
5463 Label hole;
5464 Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
5465 masm.branch32(Assembler::BelowOrEqual, initLength, index, &hole);
5466
5467 // Load value and replace with true.
5468 Label done;
5469 BaseObjectElementIndex element(scratch, index);
5470 masm.branchTestMagic(Assembler::Equal, element, &hole);
5471 EmitStoreBoolean(masm, true, output);
5472 masm.jump(&done);
5473
5474 // Load false for the hole.
5475 masm.bind(&hole);
5476 EmitStoreBoolean(masm, false, output);
5477
5478 masm.bind(&done);
5479 return true;
5480 }
5481
5482 bool CacheIRCompiler::emitPackedArrayPopResult(ObjOperandId arrayId) {
5483 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5484
5485 AutoOutputRegister output(*this);
5486 Register array = allocator.useRegister(masm, arrayId);
5487 AutoScratchRegister scratch1(allocator, masm);
5488 AutoScratchRegister scratch2(allocator, masm);
5489
5490 FailurePath* failure;
5491 if (!addFailurePath(&failure)) {
5492 return false;
5493 }
5494
5495 masm.packedArrayPop(array, output.valueReg(), scratch1, scratch2,
5496 failure->label());
5497 return true;
5498 }
5499
5500 bool CacheIRCompiler::emitPackedArrayShiftResult(ObjOperandId arrayId) {
5501 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5502
5503 AutoOutputRegister output(*this);
5504 Register array = allocator.useRegister(masm, arrayId);
5505 AutoScratchRegister scratch1(allocator, masm);
5506 AutoScratchRegister scratch2(allocator, masm);
5507
5508 FailurePath* failure;
5509 if (!addFailurePath(&failure)) {
5510 return false;
5511 }
5512
5513 masm.packedArrayShift(array, output.valueReg(), scratch1, scratch2,
5514 liveVolatileRegs(), failure->label());
5515 return true;
5516 }
5517
5518 bool CacheIRCompiler::emitIsObjectResult(ValOperandId inputId) {
5519 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5520
5521 AutoOutputRegister output(*this);
5522 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5523
5524 ValueOperand val = allocator.useValueRegister(masm, inputId);
5525
5526 masm.testObjectSet(Assembler::Equal, val, scratch);
5527
5528 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
5529 return true;
5530 }
5531
5532 bool CacheIRCompiler::emitIsPackedArrayResult(ObjOperandId objId) {
5533 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5534
5535 AutoOutputRegister output(*this);
5536 Register obj = allocator.useRegister(masm, objId);
5537 AutoScratchRegister scratch(allocator, masm);
5538
5539 Register outputScratch = output.valueReg().scratchReg();
5540 masm.setIsPackedArray(obj, outputScratch, scratch);
5541 masm.tagValue(JSVAL_TYPE_BOOLEAN, outputScratch, output.valueReg());
5542 return true;
5543 }
5544
5545 bool CacheIRCompiler::emitIsCallableResult(ValOperandId inputId) {
5546 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5547
5548 AutoOutputRegister output(*this);
5549 AutoScratchRegister scratch1(allocator, masm);
5550 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
5551
5552 ValueOperand val = allocator.useValueRegister(masm, inputId);
5553
5554 Label isObject, done;
5555 masm.branchTestObject(Assembler::Equal, val, &isObject);
5556 // Primitives are never callable.
5557 masm.move32(Imm32(0), scratch2);
5558 masm.jump(&done);
5559
5560 masm.bind(&isObject);
5561 masm.unboxObject(val, scratch1);
5562
5563 Label isProxy;
5564 masm.isCallable(scratch1, scratch2, &isProxy);
5565 masm.jump(&done);
5566
5567 masm.bind(&isProxy);
5568 {
5569 LiveRegisterSet volatileRegs = liveVolatileRegs();
5570 masm.PushRegsInMask(volatileRegs);
5571
5572 using Fn = bool (*)(JSObject* obj);
5573 masm.setupUnalignedABICall(scratch2);
5574 masm.passABIArg(scratch1);
5575 masm.callWithABI<Fn, ObjectIsCallable>();
5576 masm.storeCallBoolResult(scratch2);
5577
5578 LiveRegisterSet ignore;
5579 ignore.add(scratch2);
5580 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
5581 }
5582
5583 masm.bind(&done);
5584 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
5585 return true;
5586 }
5587
5588 bool CacheIRCompiler::emitIsConstructorResult(ObjOperandId objId) {
5589 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5590
5591 AutoOutputRegister output(*this);
5592 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5593
5594 Register obj = allocator.useRegister(masm, objId);
5595
5596 Label isProxy, done;
5597 masm.isConstructor(obj, scratch, &isProxy);
5598 masm.jump(&done);
5599
5600 masm.bind(&isProxy);
5601 {
5602 LiveRegisterSet volatileRegs = liveVolatileRegs();
5603 masm.PushRegsInMask(volatileRegs);
5604
5605 using Fn = bool (*)(JSObject* obj);
5606 masm.setupUnalignedABICall(scratch);
5607 masm.passABIArg(obj);
5608 masm.callWithABI<Fn, ObjectIsConstructor>();
5609 masm.storeCallBoolResult(scratch);
5610
5611 LiveRegisterSet ignore;
5612 ignore.add(scratch);
5613 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
5614 }
5615
5616 masm.bind(&done);
5617 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
5618 return true;
5619 }
5620
5621 bool CacheIRCompiler::emitIsCrossRealmArrayConstructorResult(
5622 ObjOperandId objId) {
5623 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5624
5625 AutoOutputRegister output(*this);
5626 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5627 Register obj = allocator.useRegister(masm, objId);
5628
5629 masm.setIsCrossRealmArrayConstructor(obj, scratch);
5630 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
5631 return true;
5632 }
5633
5634 bool CacheIRCompiler::emitArrayBufferViewByteOffsetInt32Result(
5635 ObjOperandId objId) {
5636 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5637
5638 AutoOutputRegister output(*this);
5639 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5640 Register obj = allocator.useRegister(masm, objId);
5641
5642 FailurePath* failure;
5643 if (!addFailurePath(&failure)) {
5644 return false;
5645 }
5646
5647 masm.loadArrayBufferViewByteOffsetIntPtr(obj, scratch);
5648 masm.guardNonNegativeIntPtrToInt32(scratch, failure->label());
5649 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
5650 return true;
5651 }
5652
5653 bool CacheIRCompiler::emitArrayBufferViewByteOffsetDoubleResult(
5654 ObjOperandId objId) {
5655 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5656
5657 AutoOutputRegister output(*this);
5658 Register obj = allocator.useRegister(masm, objId);
5659 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5660
5661 ScratchDoubleScope fpscratch(masm);
5662 masm.loadArrayBufferViewByteOffsetIntPtr(obj, scratch);
5663 masm.convertIntPtrToDouble(scratch, fpscratch);
5664 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5665 return true;
5666 }
5667
5668 bool CacheIRCompiler::
5669 emitResizableTypedArrayByteOffsetMaybeOutOfBoundsInt32Result(
5670 ObjOperandId objId) {
5671 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5672
5673 AutoOutputRegister output(*this);
5674 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5675 AutoScratchRegister scratch2(allocator, masm);
5676 Register obj = allocator.useRegister(masm, objId);
5677
5678 FailurePath* failure;
5679 if (!addFailurePath(&failure)) {
5680 return false;
5681 }
5682
5683 masm.loadResizableTypedArrayByteOffsetMaybeOutOfBoundsIntPtr(obj, scratch1,
5684 scratch2);
5685 masm.guardNonNegativeIntPtrToInt32(scratch1, failure->label());
5686 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
5687 return true;
5688 }
5689
5690 bool CacheIRCompiler::
5691 emitResizableTypedArrayByteOffsetMaybeOutOfBoundsDoubleResult(
5692 ObjOperandId objId) {
5693 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5694
5695 AutoOutputRegister output(*this);
5696 Register obj = allocator.useRegister(masm, objId);
5697 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5698 AutoScratchRegister scratch2(allocator, masm);
5699
5700 ScratchDoubleScope fpscratch(masm);
5701 masm.loadResizableTypedArrayByteOffsetMaybeOutOfBoundsIntPtr(obj, scratch1,
5702 scratch2);
5703 masm.convertIntPtrToDouble(scratch1, fpscratch);
5704 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5705 return true;
5706 }
5707
5708 bool CacheIRCompiler::emitTypedArrayByteLengthInt32Result(ObjOperandId objId) {
5709 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5710
5711 AutoOutputRegister output(*this);
5712 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5713 AutoScratchRegister scratch2(allocator, masm);
5714 Register obj = allocator.useRegister(masm, objId);
5715
5716 FailurePath* failure;
5717 if (!addFailurePath(&failure)) {
5718 return false;
5719 }
5720
5721 masm.loadArrayBufferViewLengthIntPtr(obj, scratch1);
5722 masm.guardNonNegativeIntPtrToInt32(scratch1, failure->label());
5723 masm.typedArrayElementSize(obj, scratch2);
5724
5725 masm.branchMul32(Assembler::Overflow, scratch2.get(), scratch1,
5726 failure->label());
5727
5728 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
5729 return true;
5730 }
5731
5732 bool CacheIRCompiler::emitTypedArrayByteLengthDoubleResult(ObjOperandId objId) {
5733 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5734
5735 AutoOutputRegister output(*this);
5736 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5737 AutoScratchRegister scratch2(allocator, masm);
5738 Register obj = allocator.useRegister(masm, objId);
5739
5740 masm.loadArrayBufferViewLengthIntPtr(obj, scratch1);
5741 masm.typedArrayElementSize(obj, scratch2);
5742 masm.mulPtr(scratch2, scratch1);
5743
5744 ScratchDoubleScope fpscratch(masm);
5745 masm.convertIntPtrToDouble(scratch1, fpscratch);
5746 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5747 return true;
5748 }
5749
5750 bool CacheIRCompiler::emitResizableTypedArrayByteLengthInt32Result(
5751 ObjOperandId objId) {
5752 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5753
5754 AutoOutputRegister output(*this);
5755 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5756 AutoScratchRegister scratch2(allocator, masm);
5757 Register obj = allocator.useRegister(masm, objId);
5758
5759 FailurePath* failure;
5760 if (!addFailurePath(&failure)) {
5761 return false;
5762 }
5763
5764 // Explicit |byteLength| accesses are seq-consistent atomic loads.
5765 auto sync = Synchronization::Load();
5766
5767 masm.loadResizableTypedArrayLengthIntPtr(sync, obj, scratch1, scratch2);
5768 masm.guardNonNegativeIntPtrToInt32(scratch1, failure->label());
5769 masm.typedArrayElementSize(obj, scratch2);
5770
5771 masm.branchMul32(Assembler::Overflow, scratch2.get(), scratch1,
5772 failure->label());
5773
5774 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
5775 return true;
5776 }
5777
5778 bool CacheIRCompiler::emitResizableTypedArrayByteLengthDoubleResult(
5779 ObjOperandId objId) {
5780 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5781
5782 AutoOutputRegister output(*this);
5783 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5784 AutoScratchRegister scratch2(allocator, masm);
5785 Register obj = allocator.useRegister(masm, objId);
5786
5787 // Explicit |byteLength| accesses are seq-consistent atomic loads.
5788 auto sync = Synchronization::Load();
5789
5790 masm.loadResizableTypedArrayLengthIntPtr(sync, obj, scratch1, scratch2);
5791 masm.typedArrayElementSize(obj, scratch2);
5792 masm.mulPtr(scratch2, scratch1);
5793
5794 ScratchDoubleScope fpscratch(masm);
5795 masm.convertIntPtrToDouble(scratch1, fpscratch);
5796 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5797 return true;
5798 }
5799
5800 bool CacheIRCompiler::emitResizableTypedArrayLengthInt32Result(
5801 ObjOperandId objId) {
5802 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5803
5804 AutoOutputRegister output(*this);
5805 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5806 AutoScratchRegister scratch2(allocator, masm);
5807 Register obj = allocator.useRegister(masm, objId);
5808
5809 FailurePath* failure;
5810 if (!addFailurePath(&failure)) {
5811 return false;
5812 }
5813
5814 // Explicit |length| accesses are seq-consistent atomic loads.
5815 auto sync = Synchronization::Load();
5816
5817 masm.loadResizableTypedArrayLengthIntPtr(sync, obj, scratch1, scratch2);
5818 masm.guardNonNegativeIntPtrToInt32(scratch1, failure->label());
5819
5820 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
5821 return true;
5822 }
5823
5824 bool CacheIRCompiler::emitResizableTypedArrayLengthDoubleResult(
5825 ObjOperandId objId) {
5826 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5827
5828 AutoOutputRegister output(*this);
5829 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5830 AutoScratchRegister scratch2(allocator, masm);
5831 Register obj = allocator.useRegister(masm, objId);
5832
5833 // Explicit |length| accesses are seq-consistent atomic loads.
5834 auto sync = Synchronization::Load();
5835
5836 masm.loadResizableTypedArrayLengthIntPtr(sync, obj, scratch1, scratch2);
5837
5838 ScratchDoubleScope fpscratch(masm);
5839 masm.convertIntPtrToDouble(scratch1, fpscratch);
5840 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5841 return true;
5842 }
5843
5844 bool CacheIRCompiler::emitTypedArrayElementSizeResult(ObjOperandId objId) {
5845 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5846
5847 AutoOutputRegister output(*this);
5848 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5849 Register obj = allocator.useRegister(masm, objId);
5850
5851 masm.typedArrayElementSize(obj, scratch);
5852 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
5853 return true;
5854 }
5855
5856 bool CacheIRCompiler::emitResizableDataViewByteLengthInt32Result(
5857 ObjOperandId objId) {
5858 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5859
5860 AutoOutputRegister output(*this);
5861 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5862 AutoScratchRegister scratch2(allocator, masm);
5863 Register obj = allocator.useRegister(masm, objId);
5864
5865 FailurePath* failure;
5866 if (!addFailurePath(&failure)) {
5867 return false;
5868 }
5869
5870 // Explicit |byteLength| accesses are seq-consistent atomic loads.
5871 auto sync = Synchronization::Load();
5872
5873 masm.loadResizableDataViewByteLengthIntPtr(sync, obj, scratch1, scratch2);
5874 masm.guardNonNegativeIntPtrToInt32(scratch1, failure->label());
5875
5876 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
5877 return true;
5878 }
5879
5880 bool CacheIRCompiler::emitResizableDataViewByteLengthDoubleResult(
5881 ObjOperandId objId) {
5882 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5883
5884 AutoOutputRegister output(*this);
5885 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
5886 AutoScratchRegister scratch2(allocator, masm);
5887 Register obj = allocator.useRegister(masm, objId);
5888
5889 // Explicit |byteLength| accesses are seq-consistent atomic loads.
5890 auto sync = Synchronization::Load();
5891
5892 masm.loadResizableDataViewByteLengthIntPtr(sync, obj, scratch1, scratch2);
5893
5894 ScratchDoubleScope fpscratch(masm);
5895 masm.convertIntPtrToDouble(scratch1, fpscratch);
5896 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5897 return true;
5898 }
5899
5900 bool CacheIRCompiler::emitGrowableSharedArrayBufferByteLengthInt32Result(
5901 ObjOperandId objId) {
5902 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5903
5904 AutoOutputRegister output(*this);
5905 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5906 Register obj = allocator.useRegister(masm, objId);
5907
5908 FailurePath* failure;
5909 if (!addFailurePath(&failure)) {
5910 return false;
5911 }
5912
5913 // Explicit |byteLength| accesses are seq-consistent atomic loads.
5914 auto sync = Synchronization::Load();
5915
5916 masm.loadGrowableSharedArrayBufferByteLengthIntPtr(sync, obj, scratch);
5917 masm.guardNonNegativeIntPtrToInt32(scratch, failure->label());
5918
5919 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
5920 return true;
5921 }
5922
5923 bool CacheIRCompiler::emitGrowableSharedArrayBufferByteLengthDoubleResult(
5924 ObjOperandId objId) {
5925 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5926
5927 AutoOutputRegister output(*this);
5928 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5929 Register obj = allocator.useRegister(masm, objId);
5930
5931 // Explicit |byteLength| accesses are seq-consistent atomic loads.
5932 auto sync = Synchronization::Load();
5933
5934 masm.loadGrowableSharedArrayBufferByteLengthIntPtr(sync, obj, scratch);
5935
5936 ScratchDoubleScope fpscratch(masm);
5937 masm.convertIntPtrToDouble(scratch, fpscratch);
5938 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
5939 return true;
5940 }
5941
5942 bool CacheIRCompiler::emitGuardHasAttachedArrayBuffer(ObjOperandId objId) {
5943 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5944
5945 AutoScratchRegister scratch(allocator, masm);
5946 Register obj = allocator.useRegister(masm, objId);
5947
5948 FailurePath* failure;
5949 if (!addFailurePath(&failure)) {
5950 return false;
5951 }
5952
5953 masm.branchIfHasDetachedArrayBuffer(obj, scratch, failure->label());
5954 return true;
5955 }
5956
5957 bool CacheIRCompiler::emitGuardResizableArrayBufferViewInBounds(
5958 ObjOperandId objId) {
5959 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5960
5961 AutoScratchRegister scratch(allocator, masm);
5962 Register obj = allocator.useRegister(masm, objId);
5963
5964 FailurePath* failure;
5965 if (!addFailurePath(&failure)) {
5966 return false;
5967 }
5968
5969 masm.branchIfResizableArrayBufferViewOutOfBounds(obj, scratch,
5970 failure->label());
5971 return true;
5972 }
5973
5974 bool CacheIRCompiler::emitGuardResizableArrayBufferViewInBoundsOrDetached(
5975 ObjOperandId objId) {
5976 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5977
5978 AutoScratchRegister scratch(allocator, masm);
5979 Register obj = allocator.useRegister(masm, objId);
5980
5981 FailurePath* failure;
5982 if (!addFailurePath(&failure)) {
5983 return false;
5984 }
5985
5986 Label done;
5987 masm.branchIfResizableArrayBufferViewInBounds(obj, scratch, &done);
5988 masm.branchIfHasAttachedArrayBuffer(obj, scratch, failure->label());
5989 masm.bind(&done);
5990 return true;
5991 }
5992
5993 bool CacheIRCompiler::emitIsTypedArrayConstructorResult(ObjOperandId objId) {
5994 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
5995
5996 AutoOutputRegister output(*this);
5997 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
5998 Register obj = allocator.useRegister(masm, objId);
5999
6000 masm.setIsDefinitelyTypedArrayConstructor(obj, scratch);
6001 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
6002 return true;
6003 }
6004
6005 bool CacheIRCompiler::emitGetNextMapSetEntryForIteratorResult(
6006 ObjOperandId iterId, ObjOperandId resultArrId, bool isMap) {
6007 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6008
6009 AutoOutputRegister output(*this);
6010 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6011 Register iter = allocator.useRegister(masm, iterId);
6012 Register resultArr = allocator.useRegister(masm, resultArrId);
6013
6014 LiveRegisterSet save = liveVolatileRegs();
6015 save.takeUnchecked(output.valueReg());
6016 save.takeUnchecked(scratch);
6017 masm.PushRegsInMask(save);
6018
6019 masm.setupUnalignedABICall(scratch);
6020 masm.passABIArg(iter);
6021 masm.passABIArg(resultArr);
6022 if (isMap) {
6023 using Fn = bool (*)(MapIteratorObject* iter, ArrayObject* resultPairObj);
6024 masm.callWithABI<Fn, MapIteratorObject::next>();
6025 } else {
6026 using Fn = bool (*)(SetIteratorObject* iter, ArrayObject* resultObj);
6027 masm.callWithABI<Fn, SetIteratorObject::next>();
6028 }
6029 masm.storeCallBoolResult(scratch);
6030
6031 masm.PopRegsInMask(save);
6032
6033 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
6034 return true;
6035 }
6036
6037 void CacheIRCompiler::emitActivateIterator(Register objBeingIterated,
6038 Register iterObject,
6039 Register nativeIter,
6040 Register scratch, Register scratch2,
6041 uint32_t enumeratorsAddrOffset) {
6042 // 'objectBeingIterated_' must be nullptr, so we don't need a pre-barrier.
6043 Address iterObjAddr(nativeIter,
6044 NativeIterator::offsetOfObjectBeingIterated());
6045 #ifdef DEBUG
6046 Label ok;
6047 masm.branchPtr(Assembler::Equal, iterObjAddr, ImmPtr(nullptr), &ok);
6048 masm.assumeUnreachable("iterator with non-null object");
6049 masm.bind(&ok);
6050 #endif
6051
6052 // Mark iterator as active.
6053 Address iterFlagsAddr(nativeIter, NativeIterator::offsetOfFlagsAndCount());
6054 masm.storePtr(objBeingIterated, iterObjAddr);
6055 masm.or32(Imm32(NativeIterator::Flags::Active), iterFlagsAddr);
6056
6057 // Post-write barrier for stores to 'objectBeingIterated_'.
6058 emitPostBarrierSlot(
6059 iterObject,
6060 TypedOrValueRegister(MIRType::Object, AnyRegister(objBeingIterated)),
6061 scratch);
6062
6063 // Chain onto the active iterator stack.
6064 StubFieldOffset enumeratorsAddr(enumeratorsAddrOffset,
6065 StubField::Type::RawPointer);
6066 emitLoadStubField(enumeratorsAddr, scratch);
6067 masm.registerIterator(scratch, nativeIter, scratch2);
6068 }
6069
6070 bool CacheIRCompiler::emitObjectToIteratorResult(
6071 ObjOperandId objId, uint32_t enumeratorsAddrOffset) {
6072 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6073
6074 AutoCallVM callvm(masm, this, allocator);
6075 Register obj = allocator.useRegister(masm, objId);
6076
6077 AutoScratchRegister iterObj(allocator, masm);
6078 AutoScratchRegister scratch(allocator, masm);
6079 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, callvm.output());
6080 AutoScratchRegisterMaybeOutputType scratch3(allocator, masm, callvm.output());
6081
6082 Label callVM, done;
6083 masm.maybeLoadIteratorFromShape(obj, iterObj, scratch, scratch2, scratch3,
6084 &callVM);
6085
6086 masm.loadPrivate(
6087 Address(iterObj, PropertyIteratorObject::offsetOfIteratorSlot()),
6088 scratch);
6089
6090 emitActivateIterator(obj, iterObj, scratch, scratch2, scratch3,
6091 enumeratorsAddrOffset);
6092 masm.jump(&done);
6093
6094 masm.bind(&callVM);
6095 callvm.prepare();
6096 masm.Push(obj);
6097 using Fn = PropertyIteratorObject* (*)(JSContext*, HandleObject);
6098 callvm.call<Fn, GetIterator>();
6099 masm.storeCallPointerResult(iterObj);
6100
6101 masm.bind(&done);
6102 EmitStoreResult(masm, iterObj, JSVAL_TYPE_OBJECT, callvm.output());
6103 return true;
6104 }
6105
6106 bool CacheIRCompiler::emitValueToIteratorResult(ValOperandId valId) {
6107 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6108
6109 AutoCallVM callvm(masm, this, allocator);
6110
6111 ValueOperand val = allocator.useValueRegister(masm, valId);
6112
6113 callvm.prepare();
6114
6115 masm.Push(val);
6116
6117 using Fn = PropertyIteratorObject* (*)(JSContext*, HandleValue);
6118 callvm.call<Fn, ValueToIterator>();
6119 return true;
6120 }
6121
6122 bool CacheIRCompiler::emitNewArrayIteratorResult(
6123 uint32_t templateObjectOffset) {
6124 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6125
6126 AutoCallVM callvm(masm, this, allocator);
6127
6128 callvm.prepare();
6129
6130 using Fn = ArrayIteratorObject* (*)(JSContext*);
6131 callvm.call<Fn, NewArrayIterator>();
6132 return true;
6133 }
6134
6135 bool CacheIRCompiler::emitNewStringIteratorResult(
6136 uint32_t templateObjectOffset) {
6137 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6138
6139 AutoCallVM callvm(masm, this, allocator);
6140
6141 callvm.prepare();
6142
6143 using Fn = StringIteratorObject* (*)(JSContext*);
6144 callvm.call<Fn, NewStringIterator>();
6145 return true;
6146 }
6147
6148 bool CacheIRCompiler::emitNewRegExpStringIteratorResult(
6149 uint32_t templateObjectOffset) {
6150 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6151
6152 AutoCallVM callvm(masm, this, allocator);
6153
6154 callvm.prepare();
6155
6156 using Fn = RegExpStringIteratorObject* (*)(JSContext*);
6157 callvm.call<Fn, NewRegExpStringIterator>();
6158 return true;
6159 }
6160
6161 bool CacheIRCompiler::emitObjectCreateResult(uint32_t templateObjectOffset) {
6162 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6163
6164 AutoCallVM callvm(masm, this, allocator);
6165 AutoScratchRegister scratch(allocator, masm);
6166
6167 StubFieldOffset objectField(templateObjectOffset, StubField::Type::JSObject);
6168 emitLoadStubField(objectField, scratch);
6169
6170 callvm.prepare();
6171 masm.Push(scratch);
6172
6173 using Fn = PlainObject* (*)(JSContext*, Handle<PlainObject*>);
6174 callvm.call<Fn, ObjectCreateWithTemplate>();
6175 return true;
6176 }
6177
6178 bool CacheIRCompiler::emitObjectKeysResult(ObjOperandId objId) {
6179 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6180
6181 AutoCallVM callvm(masm, this, allocator);
6182 Register obj = allocator.useRegister(masm, objId);
6183
6184 // Our goal is only to record calls to Object.keys, to elide it when
6185 // partially used, not to provide an alternative implementation.
6186 {
6187 callvm.prepare();
6188 masm.Push(obj);
6189
6190 using Fn = JSObject* (*)(JSContext*, HandleObject);
6191 callvm.call<Fn, jit::ObjectKeys>();
6192 }
6193
6194 return true;
6195 }
6196
6197 bool CacheIRCompiler::emitNewArrayFromLengthResult(
6198 uint32_t templateObjectOffset, Int32OperandId lengthId,
6199 uint32_t siteOffset) {
6200 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6201
6202 AutoCallVM callvm(masm, this, allocator);
6203 AutoScratchRegister scratch(allocator, masm);
6204 AutoScratchRegister scratch2(allocator, masm);
6205 Register length = allocator.useRegister(masm, lengthId);
6206
6207 StubFieldOffset objectField(templateObjectOffset, StubField::Type::JSObject);
6208 emitLoadStubField(objectField, scratch);
6209
6210 StubFieldOffset siteField(siteOffset, StubField::Type::AllocSite);
6211 emitLoadStubField(siteField, scratch2);
6212
6213 callvm.prepare();
6214 masm.Push(scratch2);
6215 masm.Push(length);
6216 masm.Push(scratch);
6217
6218 using Fn = ArrayObject* (*)(JSContext*, Handle<ArrayObject*>, int32_t,
6219 gc::AllocSite*);
6220 callvm.call<Fn, ArrayConstructorOneArg>();
6221 return true;
6222 }
6223
6224 bool CacheIRCompiler::emitNewTypedArrayFromLengthResult(
6225 uint32_t templateObjectOffset, Int32OperandId lengthId) {
6226 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6227
6228 AutoCallVM callvm(masm, this, allocator);
6229 AutoScratchRegister scratch(allocator, masm);
6230 Register length = allocator.useRegister(masm, lengthId);
6231
6232 StubFieldOffset objectField(templateObjectOffset, StubField::Type::JSObject);
6233 emitLoadStubField(objectField, scratch);
6234
6235 callvm.prepare();
6236 masm.Push(length);
6237 masm.Push(scratch);
6238
6239 using Fn = TypedArrayObject* (*)(JSContext*, HandleObject, int32_t length);
6240 callvm.call<Fn, NewTypedArrayWithTemplateAndLength>();
6241 return true;
6242 }
6243
6244 bool CacheIRCompiler::emitNewTypedArrayFromArrayBufferResult(
6245 uint32_t templateObjectOffset, ObjOperandId bufferId,
6246 ValOperandId byteOffsetId, ValOperandId lengthId) {
6247 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6248
6249 #ifdef JS_CODEGEN_X86
6250 MOZ_CRASH("Instruction not supported on 32-bit x86, not enough registers");
6251 #endif
6252
6253 AutoCallVM callvm(masm, this, allocator);
6254 AutoScratchRegister scratch(allocator, masm);
6255 Register buffer = allocator.useRegister(masm, bufferId);
6256 ValueOperand byteOffset = allocator.useValueRegister(masm, byteOffsetId);
6257 ValueOperand length = allocator.useValueRegister(masm, lengthId);
6258
6259 StubFieldOffset objectField(templateObjectOffset, StubField::Type::JSObject);
6260 emitLoadStubField(objectField, scratch);
6261
6262 callvm.prepare();
6263 masm.Push(length);
6264 masm.Push(byteOffset);
6265 masm.Push(buffer);
6266 masm.Push(scratch);
6267
6268 using Fn = TypedArrayObject* (*)(JSContext*, HandleObject, HandleObject,
6269 HandleValue, HandleValue);
6270 callvm.call<Fn, NewTypedArrayWithTemplateAndBuffer>();
6271 return true;
6272 }
6273
6274 bool CacheIRCompiler::emitNewTypedArrayFromArrayResult(
6275 uint32_t templateObjectOffset, ObjOperandId arrayId) {
6276 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6277
6278 AutoCallVM callvm(masm, this, allocator);
6279 AutoScratchRegister scratch(allocator, masm);
6280 Register array = allocator.useRegister(masm, arrayId);
6281
6282 StubFieldOffset objectField(templateObjectOffset, StubField::Type::JSObject);
6283 emitLoadStubField(objectField, scratch);
6284
6285 callvm.prepare();
6286 masm.Push(array);
6287 masm.Push(scratch);
6288
6289 using Fn = TypedArrayObject* (*)(JSContext*, HandleObject, HandleObject);
6290 callvm.call<Fn, NewTypedArrayWithTemplateAndArray>();
6291 return true;
6292 }
6293
6294 bool CacheIRCompiler::emitAddSlotAndCallAddPropHook(ObjOperandId objId,
6295 ValOperandId rhsId,
6296 uint32_t newShapeOffset) {
6297 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6298
6299 AutoCallVM callvm(masm, this, allocator);
6300
6301 AutoScratchRegister scratch(allocator, masm);
6302 Register obj = allocator.useRegister(masm, objId);
6303 ValueOperand rhs = allocator.useValueRegister(masm, rhsId);
6304
6305 StubFieldOffset shapeField(newShapeOffset, StubField::Type::Shape);
6306 emitLoadStubField(shapeField, scratch);
6307
6308 callvm.prepare();
6309
6310 masm.Push(scratch);
6311 masm.Push(rhs);
6312 masm.Push(obj);
6313
6314 using Fn =
6315 bool (*)(JSContext*, Handle<NativeObject*>, HandleValue, Handle<Shape*>);
6316 callvm.callNoResult<Fn, AddSlotAndCallAddPropHook>();
6317 return true;
6318 }
6319
6320 bool CacheIRCompiler::emitMathAbsInt32Result(Int32OperandId inputId) {
6321 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6322
6323 AutoOutputRegister output(*this);
6324 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6325
6326 Register input = allocator.useRegister(masm, inputId);
6327
6328 FailurePath* failure;
6329 if (!addFailurePath(&failure)) {
6330 return false;
6331 }
6332
6333 masm.mov(input, scratch);
6334 // Don't negate already positive values.
6335 Label positive;
6336 masm.branchTest32(Assembler::NotSigned, scratch, scratch, &positive);
6337 // neg32 might overflow for INT_MIN.
6338 masm.branchNeg32(Assembler::Overflow, scratch, failure->label());
6339 masm.bind(&positive);
6340
6341 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6342 return true;
6343 }
6344
6345 bool CacheIRCompiler::emitMathAbsNumberResult(NumberOperandId inputId) {
6346 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6347
6348 AutoOutputRegister output(*this);
6349 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6350
6351 allocator.ensureDoubleRegister(masm, inputId, scratch);
6352
6353 masm.absDouble(scratch, scratch);
6354 masm.boxDouble(scratch, output.valueReg(), scratch);
6355 return true;
6356 }
6357
6358 bool CacheIRCompiler::emitMathClz32Result(Int32OperandId inputId) {
6359 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6360
6361 AutoOutputRegister output(*this);
6362 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6363 Register input = allocator.useRegister(masm, inputId);
6364
6365 masm.clz32(input, scratch, /* knownNotZero = */ false);
6366 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6367 return true;
6368 }
6369
6370 bool CacheIRCompiler::emitMathSignInt32Result(Int32OperandId inputId) {
6371 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6372
6373 AutoOutputRegister output(*this);
6374 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6375 Register input = allocator.useRegister(masm, inputId);
6376
6377 masm.signInt32(input, scratch);
6378 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6379 return true;
6380 }
6381
6382 bool CacheIRCompiler::emitMathSignNumberResult(NumberOperandId inputId) {
6383 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6384
6385 AutoOutputRegister output(*this);
6386 AutoAvailableFloatRegister floatScratch1(*this, FloatReg0);
6387 AutoAvailableFloatRegister floatScratch2(*this, FloatReg1);
6388
6389 allocator.ensureDoubleRegister(masm, inputId, floatScratch1);
6390
6391 masm.signDouble(floatScratch1, floatScratch2);
6392 masm.boxDouble(floatScratch2, output.valueReg(), floatScratch2);
6393 return true;
6394 }
6395
6396 bool CacheIRCompiler::emitMathSignNumberToInt32Result(NumberOperandId inputId) {
6397 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6398
6399 AutoOutputRegister output(*this);
6400 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6401 AutoAvailableFloatRegister floatScratch1(*this, FloatReg0);
6402 AutoAvailableFloatRegister floatScratch2(*this, FloatReg1);
6403
6404 FailurePath* failure;
6405 if (!addFailurePath(&failure)) {
6406 return false;
6407 }
6408
6409 allocator.ensureDoubleRegister(masm, inputId, floatScratch1);
6410
6411 masm.signDoubleToInt32(floatScratch1, scratch, floatScratch2,
6412 failure->label());
6413 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6414 return true;
6415 }
6416
6417 bool CacheIRCompiler::emitMathImulResult(Int32OperandId lhsId,
6418 Int32OperandId rhsId) {
6419 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6420
6421 AutoOutputRegister output(*this);
6422 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6423 Register lhs = allocator.useRegister(masm, lhsId);
6424 Register rhs = allocator.useRegister(masm, rhsId);
6425
6426 masm.mov(lhs, scratch);
6427 masm.mul32(rhs, scratch);
6428 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6429 return true;
6430 }
6431
6432 bool CacheIRCompiler::emitMathSqrtNumberResult(NumberOperandId inputId) {
6433 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6434
6435 AutoOutputRegister output(*this);
6436 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6437
6438 allocator.ensureDoubleRegister(masm, inputId, scratch);
6439
6440 masm.sqrtDouble(scratch, scratch);
6441 masm.boxDouble(scratch, output.valueReg(), scratch);
6442 return true;
6443 }
6444
6445 bool CacheIRCompiler::emitMathFloorNumberResult(NumberOperandId inputId) {
6446 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6447
6448 AutoOutputRegister output(*this);
6449 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6450
6451 allocator.ensureDoubleRegister(masm, inputId, scratch);
6452
6453 if (Assembler::HasRoundInstruction(RoundingMode::Down)) {
6454 masm.nearbyIntDouble(RoundingMode::Down, scratch, scratch);
6455 masm.boxDouble(scratch, output.valueReg(), scratch);
6456 return true;
6457 }
6458
6459 return emitMathFunctionNumberResultShared(UnaryMathFunction::Floor, scratch,
6460 output.valueReg());
6461 }
6462
6463 bool CacheIRCompiler::emitMathCeilNumberResult(NumberOperandId inputId) {
6464 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6465
6466 AutoOutputRegister output(*this);
6467 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6468
6469 allocator.ensureDoubleRegister(masm, inputId, scratch);
6470
6471 if (Assembler::HasRoundInstruction(RoundingMode::Up)) {
6472 masm.nearbyIntDouble(RoundingMode::Up, scratch, scratch);
6473 masm.boxDouble(scratch, output.valueReg(), scratch);
6474 return true;
6475 }
6476
6477 return emitMathFunctionNumberResultShared(UnaryMathFunction::Ceil, scratch,
6478 output.valueReg());
6479 }
6480
6481 bool CacheIRCompiler::emitMathTruncNumberResult(NumberOperandId inputId) {
6482 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6483
6484 AutoOutputRegister output(*this);
6485 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6486
6487 allocator.ensureDoubleRegister(masm, inputId, scratch);
6488
6489 if (Assembler::HasRoundInstruction(RoundingMode::TowardsZero)) {
6490 masm.nearbyIntDouble(RoundingMode::TowardsZero, scratch, scratch);
6491 masm.boxDouble(scratch, output.valueReg(), scratch);
6492 return true;
6493 }
6494
6495 return emitMathFunctionNumberResultShared(UnaryMathFunction::Trunc, scratch,
6496 output.valueReg());
6497 }
6498
6499 bool CacheIRCompiler::emitMathFRoundNumberResult(NumberOperandId inputId) {
6500 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6501
6502 AutoOutputRegister output(*this);
6503 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6504 FloatRegister scratchFloat32 = scratch.get().asSingle();
6505
6506 allocator.ensureDoubleRegister(masm, inputId, scratch);
6507
6508 masm.convertDoubleToFloat32(scratch, scratchFloat32);
6509 masm.convertFloat32ToDouble(scratchFloat32, scratch);
6510
6511 masm.boxDouble(scratch, output.valueReg(), scratch);
6512 return true;
6513 }
6514
6515 bool CacheIRCompiler::emitMathF16RoundNumberResult(NumberOperandId inputId) {
6516 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6517
6518 AutoOutputRegister output(*this);
6519 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6520 AutoAvailableFloatRegister floatScratch(*this, FloatReg0);
6521
6522 allocator.ensureDoubleRegister(masm, inputId, floatScratch);
6523
6524 if (MacroAssembler::SupportsFloat64To16()) {
6525 masm.convertDoubleToFloat16(floatScratch, floatScratch);
6526 masm.convertFloat16ToDouble(floatScratch, floatScratch);
6527 } else {
6528 LiveRegisterSet save = liveVolatileRegs();
6529 save.takeUnchecked(floatScratch);
6530 masm.PushRegsInMask(save);
6531
6532 using Fn = double (*)(double);
6533 masm.setupUnalignedABICall(scratch);
6534 masm.passABIArg(floatScratch, ABIType::Float64);
6535 masm.callWithABI<Fn, js::RoundFloat16>(ABIType::Float64);
6536 masm.storeCallFloatResult(floatScratch);
6537
6538 masm.PopRegsInMask(save);
6539 }
6540
6541 masm.boxDouble(floatScratch, output.valueReg(), floatScratch);
6542 return true;
6543 }
6544
6545 bool CacheIRCompiler::emitMathHypot2NumberResult(NumberOperandId first,
6546 NumberOperandId second) {
6547 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6548 AutoOutputRegister output(*this);
6549 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6550
6551 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
6552 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
6553
6554 allocator.ensureDoubleRegister(masm, first, floatScratch0);
6555 allocator.ensureDoubleRegister(masm, second, floatScratch1);
6556
6557 LiveRegisterSet save = liveVolatileRegs();
6558 masm.PushRegsInMask(save);
6559
6560 using Fn = double (*)(double x, double y);
6561 masm.setupUnalignedABICall(scratch);
6562 masm.passABIArg(floatScratch0, ABIType::Float64);
6563 masm.passABIArg(floatScratch1, ABIType::Float64);
6564
6565 masm.callWithABI<Fn, ecmaHypot>(ABIType::Float64);
6566 masm.storeCallFloatResult(floatScratch0);
6567
6568 LiveRegisterSet ignore;
6569 ignore.add(floatScratch0);
6570 masm.PopRegsInMaskIgnore(save, ignore);
6571
6572 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
6573 return true;
6574 }
6575
6576 bool CacheIRCompiler::emitMathHypot3NumberResult(NumberOperandId first,
6577 NumberOperandId second,
6578 NumberOperandId third) {
6579 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6580 AutoOutputRegister output(*this);
6581 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6582
6583 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
6584 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
6585 AutoAvailableFloatRegister floatScratch2(*this, FloatReg2);
6586
6587 allocator.ensureDoubleRegister(masm, first, floatScratch0);
6588 allocator.ensureDoubleRegister(masm, second, floatScratch1);
6589 allocator.ensureDoubleRegister(masm, third, floatScratch2);
6590
6591 LiveRegisterSet save = liveVolatileRegs();
6592 masm.PushRegsInMask(save);
6593
6594 using Fn = double (*)(double x, double y, double z);
6595 masm.setupUnalignedABICall(scratch);
6596 masm.passABIArg(floatScratch0, ABIType::Float64);
6597 masm.passABIArg(floatScratch1, ABIType::Float64);
6598 masm.passABIArg(floatScratch2, ABIType::Float64);
6599
6600 masm.callWithABI<Fn, hypot3>(ABIType::Float64);
6601 masm.storeCallFloatResult(floatScratch0);
6602
6603 LiveRegisterSet ignore;
6604 ignore.add(floatScratch0);
6605 masm.PopRegsInMaskIgnore(save, ignore);
6606
6607 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
6608 return true;
6609 }
6610
6611 bool CacheIRCompiler::emitMathHypot4NumberResult(NumberOperandId first,
6612 NumberOperandId second,
6613 NumberOperandId third,
6614 NumberOperandId fourth) {
6615 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6616 AutoOutputRegister output(*this);
6617 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6618
6619 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
6620 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
6621 AutoAvailableFloatRegister floatScratch2(*this, FloatReg2);
6622 AutoAvailableFloatRegister floatScratch3(*this, FloatReg3);
6623
6624 allocator.ensureDoubleRegister(masm, first, floatScratch0);
6625 allocator.ensureDoubleRegister(masm, second, floatScratch1);
6626 allocator.ensureDoubleRegister(masm, third, floatScratch2);
6627 allocator.ensureDoubleRegister(masm, fourth, floatScratch3);
6628
6629 LiveRegisterSet save = liveVolatileRegs();
6630 masm.PushRegsInMask(save);
6631
6632 using Fn = double (*)(double x, double y, double z, double w);
6633 masm.setupUnalignedABICall(scratch);
6634 masm.passABIArg(floatScratch0, ABIType::Float64);
6635 masm.passABIArg(floatScratch1, ABIType::Float64);
6636 masm.passABIArg(floatScratch2, ABIType::Float64);
6637 masm.passABIArg(floatScratch3, ABIType::Float64);
6638
6639 masm.callWithABI<Fn, hypot4>(ABIType::Float64);
6640 masm.storeCallFloatResult(floatScratch0);
6641
6642 LiveRegisterSet ignore;
6643 ignore.add(floatScratch0);
6644 masm.PopRegsInMaskIgnore(save, ignore);
6645
6646 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
6647 return true;
6648 }
6649
6650 bool CacheIRCompiler::emitMathAtan2NumberResult(NumberOperandId yId,
6651 NumberOperandId xId) {
6652 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6653 AutoOutputRegister output(*this);
6654 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6655
6656 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
6657 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
6658
6659 allocator.ensureDoubleRegister(masm, yId, floatScratch0);
6660 allocator.ensureDoubleRegister(masm, xId, floatScratch1);
6661
6662 LiveRegisterSet save = liveVolatileRegs();
6663 masm.PushRegsInMask(save);
6664
6665 using Fn = double (*)(double x, double y);
6666 masm.setupUnalignedABICall(scratch);
6667 masm.passABIArg(floatScratch0, ABIType::Float64);
6668 masm.passABIArg(floatScratch1, ABIType::Float64);
6669 masm.callWithABI<Fn, js::ecmaAtan2>(ABIType::Float64);
6670 masm.storeCallFloatResult(floatScratch0);
6671
6672 LiveRegisterSet ignore;
6673 ignore.add(floatScratch0);
6674 masm.PopRegsInMaskIgnore(save, ignore);
6675
6676 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
6677
6678 return true;
6679 }
6680
6681 bool CacheIRCompiler::emitMathFloorToInt32Result(NumberOperandId inputId) {
6682 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6683
6684 AutoOutputRegister output(*this);
6685 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6686
6687 AutoAvailableFloatRegister scratchFloat(*this, FloatReg0);
6688
6689 FailurePath* failure;
6690 if (!addFailurePath(&failure)) {
6691 return false;
6692 }
6693
6694 allocator.ensureDoubleRegister(masm, inputId, scratchFloat);
6695
6696 masm.floorDoubleToInt32(scratchFloat, scratch, failure->label());
6697
6698 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6699 return true;
6700 }
6701
6702 bool CacheIRCompiler::emitMathCeilToInt32Result(NumberOperandId inputId) {
6703 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6704
6705 AutoOutputRegister output(*this);
6706 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6707
6708 AutoAvailableFloatRegister scratchFloat(*this, FloatReg0);
6709
6710 FailurePath* failure;
6711 if (!addFailurePath(&failure)) {
6712 return false;
6713 }
6714
6715 allocator.ensureDoubleRegister(masm, inputId, scratchFloat);
6716
6717 masm.ceilDoubleToInt32(scratchFloat, scratch, failure->label());
6718
6719 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6720 return true;
6721 }
6722
6723 bool CacheIRCompiler::emitMathTruncToInt32Result(NumberOperandId inputId) {
6724 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6725
6726 AutoOutputRegister output(*this);
6727 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6728
6729 AutoAvailableFloatRegister scratchFloat(*this, FloatReg0);
6730
6731 FailurePath* failure;
6732 if (!addFailurePath(&failure)) {
6733 return false;
6734 }
6735
6736 allocator.ensureDoubleRegister(masm, inputId, scratchFloat);
6737
6738 masm.truncDoubleToInt32(scratchFloat, scratch, failure->label());
6739
6740 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6741 return true;
6742 }
6743
6744 bool CacheIRCompiler::emitMathRoundToInt32Result(NumberOperandId inputId) {
6745 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6746
6747 AutoOutputRegister output(*this);
6748 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
6749
6750 AutoAvailableFloatRegister scratchFloat0(*this, FloatReg0);
6751 AutoAvailableFloatRegister scratchFloat1(*this, FloatReg1);
6752
6753 FailurePath* failure;
6754 if (!addFailurePath(&failure)) {
6755 return false;
6756 }
6757
6758 allocator.ensureDoubleRegister(masm, inputId, scratchFloat0);
6759
6760 masm.roundDoubleToInt32(scratchFloat0, scratch, scratchFloat1,
6761 failure->label());
6762
6763 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
6764 return true;
6765 }
6766
6767 bool CacheIRCompiler::emitInt32MinMax(bool isMax, Int32OperandId firstId,
6768 Int32OperandId secondId,
6769 Int32OperandId resultId) {
6770 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6771
6772 Register first = allocator.useRegister(masm, firstId);
6773 Register second = allocator.useRegister(masm, secondId);
6774 Register result = allocator.defineRegister(masm, resultId);
6775
6776 Assembler::Condition cond =
6777 isMax ? Assembler::GreaterThan : Assembler::LessThan;
6778 masm.move32(first, result);
6779 masm.cmp32Move32(cond, second, first, second, result);
6780 return true;
6781 }
6782
6783 bool CacheIRCompiler::emitNumberMinMax(bool isMax, NumberOperandId firstId,
6784 NumberOperandId secondId,
6785 NumberOperandId resultId) {
6786 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6787
6788 ValueOperand output = allocator.defineValueRegister(masm, resultId);
6789
6790 AutoAvailableFloatRegister scratch1(*this, FloatReg0);
6791 AutoAvailableFloatRegister scratch2(*this, FloatReg1);
6792
6793 allocator.ensureDoubleRegister(masm, firstId, scratch1);
6794 allocator.ensureDoubleRegister(masm, secondId, scratch2);
6795
6796 if (isMax) {
6797 masm.maxDouble(scratch2, scratch1, /* handleNaN = */ true);
6798 } else {
6799 masm.minDouble(scratch2, scratch1, /* handleNaN = */ true);
6800 }
6801
6802 masm.boxDouble(scratch1, output, scratch1);
6803 return true;
6804 }
6805
6806 bool CacheIRCompiler::emitInt32MinMaxArrayResult(ObjOperandId arrayId,
6807 bool isMax) {
6808 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6809
6810 AutoOutputRegister output(*this);
6811 Register array = allocator.useRegister(masm, arrayId);
6812
6813 AutoScratchRegister scratch(allocator, masm);
6814 AutoScratchRegister scratch2(allocator, masm);
6815 AutoScratchRegisterMaybeOutputType scratch3(allocator, masm, output);
6816 AutoScratchRegisterMaybeOutput result(allocator, masm, output);
6817
6818 FailurePath* failure;
6819 if (!addFailurePath(&failure)) {
6820 return false;
6821 }
6822
6823 masm.minMaxArrayInt32(array, result, scratch, scratch2, scratch3, isMax,
6824 failure->label());
6825 masm.tagValue(JSVAL_TYPE_INT32, result, output.valueReg());
6826 return true;
6827 }
6828
6829 bool CacheIRCompiler::emitNumberMinMaxArrayResult(ObjOperandId arrayId,
6830 bool isMax) {
6831 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6832
6833 AutoOutputRegister output(*this);
6834 Register array = allocator.useRegister(masm, arrayId);
6835
6836 AutoAvailableFloatRegister result(*this, FloatReg0);
6837 AutoAvailableFloatRegister floatScratch(*this, FloatReg1);
6838
6839 AutoScratchRegister scratch1(allocator, masm);
6840 AutoScratchRegister scratch2(allocator, masm);
6841
6842 FailurePath* failure;
6843 if (!addFailurePath(&failure)) {
6844 return false;
6845 }
6846
6847 masm.minMaxArrayNumber(array, result, floatScratch, scratch1, scratch2, isMax,
6848 failure->label());
6849 masm.boxDouble(result, output.valueReg(), result);
6850 return true;
6851 }
6852
6853 bool CacheIRCompiler::emitMathFunctionNumberResultShared(
6854 UnaryMathFunction fun, FloatRegister inputScratch, ValueOperand output) {
6855 UnaryMathFunctionType funPtr = GetUnaryMathFunctionPtr(fun);
6856
6857 LiveRegisterSet save = liveVolatileRegs();
6858 save.takeUnchecked(inputScratch);
6859 masm.PushRegsInMask(save);
6860
6861 masm.setupUnalignedABICall(output.scratchReg());
6862 masm.passABIArg(inputScratch, ABIType::Float64);
6863 masm.callWithABI(DynamicFunction<UnaryMathFunctionType>(funPtr),
6864 ABIType::Float64);
6865 masm.storeCallFloatResult(inputScratch);
6866
6867 masm.PopRegsInMask(save);
6868
6869 masm.boxDouble(inputScratch, output, inputScratch);
6870 return true;
6871 }
6872
6873 bool CacheIRCompiler::emitMathFunctionNumberResult(NumberOperandId inputId,
6874 UnaryMathFunction fun) {
6875 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6876
6877 AutoOutputRegister output(*this);
6878 AutoAvailableFloatRegister scratch(*this, FloatReg0);
6879
6880 allocator.ensureDoubleRegister(masm, inputId, scratch);
6881
6882 return emitMathFunctionNumberResultShared(fun, scratch, output.valueReg());
6883 }
6884
6885 static void EmitStoreDenseElement(MacroAssembler& masm,
6886 const ConstantOrRegister& value,
6887 BaseObjectElementIndex target) {
6888 if (value.constant()) {
6889 Value v = value.value();
6890 masm.storeValue(v, target);
6891 return;
6892 }
6893
6894 TypedOrValueRegister reg = value.reg();
6895 masm.storeTypedOrValue(reg, target);
6896 }
6897
6898 bool CacheIRCompiler::emitStoreDenseElement(ObjOperandId objId,
6899 Int32OperandId indexId,
6900 ValOperandId rhsId) {
6901 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6902
6903 Register obj = allocator.useRegister(masm, objId);
6904 Register index = allocator.useRegister(masm, indexId);
6905 ConstantOrRegister val = allocator.useConstantOrRegister(masm, rhsId);
6906
6907 AutoScratchRegister scratch(allocator, masm);
6908
6909 FailurePath* failure;
6910 if (!addFailurePath(&failure)) {
6911 return false;
6912 }
6913
6914 // Load obj->elements in scratch.
6915 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
6916
6917 // Bounds check. Unfortunately we don't have more registers available on
6918 // x86, so use InvalidReg and emit slightly slower code on x86.
6919 Register spectreTemp = InvalidReg;
6920 Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
6921 masm.spectreBoundsCheck32(index, initLength, spectreTemp, failure->label());
6922
6923 // Hole check.
6924 BaseObjectElementIndex element(scratch, index);
6925 masm.branchTestMagic(Assembler::Equal, element, failure->label());
6926
6927 // Perform the store.
6928 EmitPreBarrier(masm, element, MIRType::Value);
6929 EmitStoreDenseElement(masm, val, element);
6930
6931 emitPostBarrierElement(obj, val, scratch, index);
6932 return true;
6933 }
6934
6935 static void EmitAssertExtensibleElements(MacroAssembler& masm,
6936 Register elementsReg) {
6937 #ifdef DEBUG
6938 // Preceding shape guards ensure the object elements are extensible.
6939 Address elementsFlags(elementsReg, ObjectElements::offsetOfFlags());
6940 Label ok;
6941 masm.branchTest32(Assembler::Zero, elementsFlags,
6942 Imm32(ObjectElements::Flags::NOT_EXTENSIBLE), &ok);
6943 masm.assumeUnreachable("Unexpected non-extensible elements");
6944 masm.bind(&ok);
6945 #endif
6946 }
6947
6948 static void EmitAssertWritableArrayLengthElements(MacroAssembler& masm,
6949 Register elementsReg) {
6950 #ifdef DEBUG
6951 // Preceding shape guards ensure the array length is writable.
6952 Address elementsFlags(elementsReg, ObjectElements::offsetOfFlags());
6953 Label ok;
6954 masm.branchTest32(Assembler::Zero, elementsFlags,
6955 Imm32(ObjectElements::Flags::NONWRITABLE_ARRAY_LENGTH),
6956 &ok);
6957 masm.assumeUnreachable("Unexpected non-writable array length elements");
6958 masm.bind(&ok);
6959 #endif
6960 }
6961
6962 bool CacheIRCompiler::emitStoreDenseElementHole(ObjOperandId objId,
6963 Int32OperandId indexId,
6964 ValOperandId rhsId,
6965 bool handleAdd) {
6966 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
6967
6968 Register obj = allocator.useRegister(masm, objId);
6969 Register index = allocator.useRegister(masm, indexId);
6970 ConstantOrRegister val = allocator.useConstantOrRegister(masm, rhsId);
6971
6972 AutoScratchRegister scratch(allocator, masm);
6973
6974 FailurePath* failure;
6975 if (!addFailurePath(&failure)) {
6976 return false;
6977 }
6978
6979 // Load obj->elements in scratch.
6980 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
6981
6982 EmitAssertExtensibleElements(masm, scratch);
6983 if (handleAdd) {
6984 EmitAssertWritableArrayLengthElements(masm, scratch);
6985 }
6986
6987 BaseObjectElementIndex element(scratch, index);
6988 Address initLength(scratch, ObjectElements::offsetOfInitializedLength());
6989 Address elementsFlags(scratch, ObjectElements::offsetOfFlags());
6990
6991 // We don't have enough registers on x86 so use InvalidReg. This will emit
6992 // slightly less efficient code on x86.
6993 Register spectreTemp = InvalidReg;
6994
6995 Label storeSkipPreBarrier;
6996 if (handleAdd) {
6997 // Bounds check.
6998 Label inBounds, outOfBounds;
6999 masm.spectreBoundsCheck32(index, initLength, spectreTemp, &outOfBounds);
7000 masm.jump(&inBounds);
7001
7002 masm.bind(&outOfBounds);
7003 masm.prepareOOBStoreElement(obj, index, scratch, spectreTemp,
7004 failure->label(), liveVolatileRegs());
7005
7006 // Skip EmitPreBarrier as the memory is uninitialized.
7007 masm.jump(&storeSkipPreBarrier);
7008
7009 masm.bind(&inBounds);
7010 } else {
7011 // Fail if index >= initLength.
7012 masm.spectreBoundsCheck32(index, initLength, spectreTemp, failure->label());
7013 }
7014
7015 EmitPreBarrier(masm, element, MIRType::Value);
7016
7017 masm.bind(&storeSkipPreBarrier);
7018 EmitStoreDenseElement(masm, val, element);
7019
7020 emitPostBarrierElement(obj, val, scratch, index);
7021 return true;
7022 }
7023
7024 bool CacheIRCompiler::emitArrayPush(ObjOperandId objId, ValOperandId rhsId) {
7025 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7026
7027 AutoOutputRegister output(*this);
7028 Register obj = allocator.useRegister(masm, objId);
7029 ValueOperand val = allocator.useValueRegister(masm, rhsId);
7030
7031 AutoScratchRegisterMaybeOutput scratchLength(allocator, masm, output);
7032 AutoScratchRegisterMaybeOutputType scratch(allocator, masm, output);
7033
7034 FailurePath* failure;
7035 if (!addFailurePath(&failure)) {
7036 return false;
7037 }
7038
7039 // Load obj->elements in scratch.
7040 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
7041
7042 EmitAssertExtensibleElements(masm, scratch);
7043 EmitAssertWritableArrayLengthElements(masm, scratch);
7044
7045 Address elementsInitLength(scratch,
7046 ObjectElements::offsetOfInitializedLength());
7047 Address elementsLength(scratch, ObjectElements::offsetOfLength());
7048 Address capacity(scratch, ObjectElements::offsetOfCapacity());
7049
7050 // Fail if length != initLength.
7051 masm.load32(elementsInitLength, scratchLength);
7052 masm.branch32(Assembler::NotEqual, elementsLength, scratchLength,
7053 failure->label());
7054
7055 // If scratchLength < capacity, we can add a dense element inline. If not we
7056 // need to allocate more elements.
7057 Label allocElement, addNewElement;
7058 masm.spectreBoundsCheck32(scratchLength, capacity, InvalidReg, &allocElement);
7059 masm.jump(&addNewElement);
7060
7061 masm.bind(&allocElement);
7062
7063 LiveRegisterSet save = liveVolatileRegs();
7064 save.takeUnchecked(scratch);
7065 masm.PushRegsInMask(save);
7066
7067 using Fn = bool (*)(JSContext* cx, NativeObject* obj);
7068 masm.setupUnalignedABICall(scratch);
7069 masm.loadJSContext(scratch);
7070 masm.passABIArg(scratch);
7071 masm.passABIArg(obj);
7072 masm.callWithABI<Fn, NativeObject::addDenseElementPure>();
7073 masm.storeCallPointerResult(scratch);
7074
7075 masm.PopRegsInMask(save);
7076 masm.branchIfFalseBool(scratch, failure->label());
7077
7078 // Load the reallocated elements pointer.
7079 masm.loadPtr(Address(obj, NativeObject::offsetOfElements()), scratch);
7080
7081 masm.bind(&addNewElement);
7082
7083 // Increment initLength and length.
7084 masm.add32(Imm32(1), elementsInitLength);
7085 masm.add32(Imm32(1), elementsLength);
7086
7087 // Store the value.
7088 BaseObjectElementIndex element(scratch, scratchLength);
7089 masm.storeValue(val, element);
7090 emitPostBarrierElement(obj, val, scratch, scratchLength);
7091
7092 // Return value is new length.
7093 masm.add32(Imm32(1), scratchLength);
7094 masm.tagValue(JSVAL_TYPE_INT32, scratchLength, output.valueReg());
7095
7096 return true;
7097 }
7098
7099 bool CacheIRCompiler::emitStoreTypedArrayElement(ObjOperandId objId,
7100 Scalar::Type elementType,
7101 IntPtrOperandId indexId,
7102 uint32_t rhsId, bool handleOOB,
7103 ArrayBufferViewKind viewKind) {
7104 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7105 Register obj = allocator.useRegister(masm, objId);
7106 Register index = allocator.useRegister(masm, indexId);
7107
7108 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
7109
7110 Maybe<Register> valInt32;
7111 Maybe<Register> valBigInt;
7112 switch (elementType) {
7113 case Scalar::Int8:
7114 case Scalar::Uint8:
7115 case Scalar::Int16:
7116 case Scalar::Uint16:
7117 case Scalar::Int32:
7118 case Scalar::Uint32:
7119 case Scalar::Uint8Clamped:
7120 valInt32.emplace(allocator.useRegister(masm, Int32OperandId(rhsId)));
7121 break;
7122
7123 case Scalar::Float16:
7124 case Scalar::Float32:
7125 case Scalar::Float64:
7126 allocator.ensureDoubleRegister(masm, NumberOperandId(rhsId),
7127 floatScratch0);
7128 break;
7129
7130 case Scalar::BigInt64:
7131 case Scalar::BigUint64:
7132 valBigInt.emplace(allocator.useRegister(masm, BigIntOperandId(rhsId)));
7133 break;
7134
7135 case Scalar::MaxTypedArrayViewType:
7136 case Scalar::Int64:
7137 case Scalar::Simd128:
7138 MOZ_CRASH("Unsupported TypedArray type");
7139 }
7140
7141 AutoScratchRegister scratch1(allocator, masm);
7142 Maybe<AutoScratchRegister> scratch2;
7143 Maybe<AutoSpectreBoundsScratchRegister> spectreScratch;
7144 if (Scalar::isBigIntType(elementType) || elementType == Scalar::Float16 ||
7145 viewKind == ArrayBufferViewKind::Resizable) {
7146 scratch2.emplace(allocator, masm);
7147 } else {
7148 spectreScratch.emplace(allocator, masm);
7149 }
7150
7151 FailurePath* failure = nullptr;
7152 if (!handleOOB) {
7153 if (!addFailurePath(&failure)) {
7154 return false;
7155 }
7156 }
7157
7158 // Bounds check.
7159 Label done;
7160 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch1, scratch2,
7161 spectreScratch,
7162 handleOOB ? &done : failure->label());
7163
7164 // Load the elements vector.
7165 masm.loadPtr(Address(obj, ArrayBufferViewObject::dataOffset()), scratch1);
7166
7167 BaseIndex dest(scratch1, index, ScaleFromScalarType(elementType));
7168
7169 if (Scalar::isBigIntType(elementType)) {
7170 #ifdef JS_PUNBOX64
7171 Register64 temp(scratch2->get());
7172 #else
7173 // We don't have more registers available on x86, so spill |obj|.
7174 masm.push(obj);
7175 Register64 temp(scratch2->get(), obj);
7176 #endif
7177
7178 masm.loadBigInt64(*valBigInt, temp);
7179 masm.storeToTypedBigIntArray(temp, dest);
7180
7181 #ifndef JS_PUNBOX64
7182 masm.pop(obj);
7183 #endif
7184 } else if (Scalar::isFloatingType(elementType)) {
7185 Register temp = scratch2 ? scratch2->get() : InvalidReg;
7186
7187 // Canonicalize floating point values for differential testing.
7188 if (js::SupportDifferentialTesting()) {
7189 masm.canonicalizeDouble(floatScratch0);
7190 }
7191
7192 masm.storeToTypedFloatArray(elementType, floatScratch0, dest, temp,
7193 liveVolatileRegs());
7194 } else {
7195 masm.storeToTypedIntArray(elementType, *valInt32, dest);
7196 }
7197
7198 masm.bind(&done);
7199 return true;
7200 }
7201
7202 void CacheIRCompiler::emitTypedArrayBoundsCheck(ArrayBufferViewKind viewKind,
7203 Register obj, Register index,
7204 Register scratch,
7205 Register maybeScratch,
7206 Register spectreScratch,
7207 Label* fail) {
7208 // |index| must not alias any scratch register.
7209 MOZ_ASSERT(index != scratch);
7210 MOZ_ASSERT(index != maybeScratch);
7211 MOZ_ASSERT(index != spectreScratch);
7212
7213 // Use |maybeScratch| when no explicit |spectreScratch| is present.
7214 if (spectreScratch == InvalidReg) {
7215 spectreScratch = maybeScratch;
7216 }
7217
7218 if (viewKind == ArrayBufferViewKind::FixedLength) {
7219 masm.loadArrayBufferViewLengthIntPtr(obj, scratch);
7220 masm.spectreBoundsCheckPtr(index, scratch, spectreScratch, fail);
7221 } else {
7222 if (maybeScratch == InvalidReg) {
7223 // Spill |index| to use it as an additional scratch register.
7224 masm.push(index);
7225
7226 maybeScratch = index;
7227 }
7228
7229 // Bounds check doesn't require synchronization. See IsValidIntegerIndex
7230 // abstract operation which reads the underlying buffer byte length using
7231 // "unordered" memory order.
7232 auto sync = Synchronization::None();
7233
7234 masm.loadResizableTypedArrayLengthIntPtr(sync, obj, scratch, maybeScratch);
7235
7236 if (maybeScratch == index) {
7237 // Restore |index|.
7238 masm.pop(index);
7239 }
7240
7241 masm.spectreBoundsCheckPtr(index, scratch, spectreScratch, fail);
7242 }
7243 }
7244
7245 void CacheIRCompiler::emitTypedArrayBoundsCheck(
7246 ArrayBufferViewKind viewKind, Register obj, Register index,
7247 Register scratch, mozilla::Maybe<Register> maybeScratch,
7248 mozilla::Maybe<Register> spectreScratch, Label* fail) {
7249 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch,
7250 maybeScratch.valueOr(InvalidReg),
7251 spectreScratch.valueOr(InvalidReg), fail);
7252 }
7253
7254 bool CacheIRCompiler::emitLoadTypedArrayElementResult(
7255 ObjOperandId objId, IntPtrOperandId indexId, Scalar::Type elementType,
7256 bool handleOOB, bool forceDoubleForUint32, ArrayBufferViewKind viewKind) {
7257 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7258 AutoOutputRegister output(*this);
7259 Register obj = allocator.useRegister(masm, objId);
7260 Register index = allocator.useRegister(masm, indexId);
7261
7262 AutoScratchRegister scratch1(allocator, masm);
7263 #ifdef JS_PUNBOX64
7264 AutoScratchRegister scratch2(allocator, masm);
7265 #else
7266 // There are too few registers available on x86, so we may need to reuse the
7267 // output's scratch register.
7268 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
7269 #endif
7270
7271 FailurePath* failure;
7272 if (!addFailurePath(&failure)) {
7273 return false;
7274 }
7275
7276 // Bounds check.
7277 Label outOfBounds;
7278 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch1, scratch2, scratch2,
7279 handleOOB ? &outOfBounds : failure->label());
7280
7281 // Allocate BigInt if needed. The code after this should be infallible.
7282 Maybe<Register> bigInt;
7283 if (Scalar::isBigIntType(elementType)) {
7284 bigInt.emplace(output.valueReg().scratchReg());
7285
7286 LiveRegisterSet save = liveVolatileRegs();
7287 save.takeUnchecked(scratch1);
7288 save.takeUnchecked(scratch2);
7289 save.takeUnchecked(output);
7290
7291 gc::Heap initialHeap = InitialBigIntHeap(cx_);
7292 EmitAllocateBigInt(masm, *bigInt, scratch1, save, initialHeap,
7293 failure->label());
7294 }
7295
7296 // Load the elements vector.
7297 masm.loadPtr(Address(obj, ArrayBufferViewObject::dataOffset()), scratch1);
7298
7299 // Load the value.
7300 BaseIndex source(scratch1, index, ScaleFromScalarType(elementType));
7301
7302 if (Scalar::isBigIntType(elementType)) {
7303 #ifdef JS_PUNBOX64
7304 Register64 temp(scratch2);
7305 #else
7306 // We don't have more registers available on x86, so spill |obj| and
7307 // additionally use the output's type register.
7308 MOZ_ASSERT(output.valueReg().scratchReg() != output.valueReg().typeReg());
7309 masm.push(obj);
7310 Register64 temp(output.valueReg().typeReg(), obj);
7311 #endif
7312
7313 masm.loadFromTypedBigIntArray(elementType, source, output.valueReg(),
7314 *bigInt, temp);
7315
7316 #ifndef JS_PUNBOX64
7317 masm.pop(obj);
7318 #endif
7319 } else {
7320 MacroAssembler::Uint32Mode uint32Mode =
7321 forceDoubleForUint32 ? MacroAssembler::Uint32Mode::ForceDouble
7322 : MacroAssembler::Uint32Mode::FailOnDouble;
7323 masm.loadFromTypedArray(elementType, source, output.valueReg(), uint32Mode,
7324 scratch1, failure->label(), liveVolatileRegs());
7325 }
7326
7327 if (handleOOB) {
7328 Label done;
7329 masm.jump(&done);
7330
7331 masm.bind(&outOfBounds);
7332 masm.moveValue(UndefinedValue(), output.valueReg());
7333
7334 masm.bind(&done);
7335 }
7336
7337 return true;
7338 }
7339
7340 void CacheIRCompiler::emitDataViewBoundsCheck(ArrayBufferViewKind viewKind,
7341 size_t byteSize, Register obj,
7342 Register offset, Register scratch,
7343 Register maybeScratch,
7344 Label* fail) {
7345 // |offset| must not alias any scratch register.
7346 MOZ_ASSERT(offset != scratch);
7347 MOZ_ASSERT(offset != maybeScratch);
7348
7349 if (viewKind == ArrayBufferViewKind::FixedLength) {
7350 masm.loadArrayBufferViewLengthIntPtr(obj, scratch);
7351 } else {
7352 if (maybeScratch == InvalidReg) {
7353 // Spill |offset| to use it as an additional scratch register.
7354 masm.push(offset);
7355
7356 maybeScratch = offset;
7357 }
7358
7359 // Bounds check doesn't require synchronization. See GetViewValue and
7360 // SetViewValue abstract operations which read the underlying buffer byte
7361 // length using "unordered" memory order.
7362 auto sync = Synchronization::None();
7363
7364 masm.loadResizableDataViewByteLengthIntPtr(sync, obj, scratch,
7365 maybeScratch);
7366
7367 if (maybeScratch == offset) {
7368 // Restore |offset|.
7369 masm.pop(offset);
7370 }
7371 }
7372
7373 // Ensure both offset < length and offset + (byteSize - 1) < length.
7374 if (byteSize == 1) {
7375 masm.spectreBoundsCheckPtr(offset, scratch, InvalidReg, fail);
7376 } else {
7377 // temp := length - (byteSize - 1)
7378 // if temp < 0: fail
7379 // if offset >= temp: fail
7380 masm.branchSubPtr(Assembler::Signed, Imm32(byteSize - 1), scratch, fail);
7381 masm.spectreBoundsCheckPtr(offset, scratch, InvalidReg, fail);
7382 }
7383 }
7384
7385 bool CacheIRCompiler::emitLoadDataViewValueResult(
7386 ObjOperandId objId, IntPtrOperandId offsetId,
7387 BooleanOperandId littleEndianId, Scalar::Type elementType,
7388 bool forceDoubleForUint32, ArrayBufferViewKind viewKind) {
7389 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7390
7391 AutoOutputRegister output(*this);
7392 Register obj = allocator.useRegister(masm, objId);
7393 Register offset = allocator.useRegister(masm, offsetId);
7394 Register littleEndian = allocator.useRegister(masm, littleEndianId);
7395
7396 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
7397
7398 Register64 outputReg64 = output.valueReg().toRegister64();
7399 Register outputScratch = outputReg64.scratchReg();
7400
7401 Register scratch2;
7402 #ifndef JS_CODEGEN_X86
7403 Maybe<AutoScratchRegister> maybeScratch2;
7404 if (viewKind == ArrayBufferViewKind::Resizable ||
7405 (elementType == Scalar::Float16 &&
7406 !MacroAssembler::SupportsFloat32To16())) {
7407 maybeScratch2.emplace(allocator, masm);
7408 scratch2 = *maybeScratch2;
7409 }
7410 #else
7411 // Not enough registers on x86, so use the other part of outputReg64.
7412 scratch2 = outputReg64.secondScratchReg();
7413 #endif
7414
7415 FailurePath* failure;
7416 if (!addFailurePath(&failure)) {
7417 return false;
7418 }
7419
7420 const size_t byteSize = Scalar::byteSize(elementType);
7421
7422 emitDataViewBoundsCheck(viewKind, byteSize, obj, offset, outputScratch,
7423 scratch2, failure->label());
7424
7425 masm.loadPtr(Address(obj, DataViewObject::dataOffset()), outputScratch);
7426
7427 // Load the value.
7428 BaseIndex source(outputScratch, offset, TimesOne);
7429 switch (elementType) {
7430 case Scalar::Int8:
7431 masm.load8SignExtend(source, outputScratch);
7432 break;
7433 case Scalar::Uint8:
7434 masm.load8ZeroExtend(source, outputScratch);
7435 break;
7436 case Scalar::Int16:
7437 masm.load16UnalignedSignExtend(source, outputScratch);
7438 break;
7439 case Scalar::Uint16:
7440 case Scalar::Float16:
7441 masm.load16UnalignedZeroExtend(source, outputScratch);
7442 break;
7443 case Scalar::Int32:
7444 case Scalar::Uint32:
7445 case Scalar::Float32:
7446 masm.load32Unaligned(source, outputScratch);
7447 break;
7448 case Scalar::Float64:
7449 case Scalar::BigInt64:
7450 case Scalar::BigUint64:
7451 masm.load64Unaligned(source, outputReg64);
7452 break;
7453 case Scalar::Uint8Clamped:
7454 default:
7455 MOZ_CRASH("Invalid typed array type");
7456 }
7457
7458 // Swap the bytes in the loaded value.
7459 if (byteSize > 1) {
7460 Label skip;
7461 masm.branch32(MOZ_LITTLE_ENDIAN() ? Assembler::NotEqual : Assembler::Equal,
7462 littleEndian, Imm32(0), &skip);
7463
7464 switch (elementType) {
7465 case Scalar::Int16:
7466 masm.byteSwap16SignExtend(outputScratch);
7467 break;
7468 case Scalar::Uint16:
7469 case Scalar::Float16:
7470 masm.byteSwap16ZeroExtend(outputScratch);
7471 break;
7472 case Scalar::Int32:
7473 case Scalar::Uint32:
7474 case Scalar::Float32:
7475 masm.byteSwap32(outputScratch);
7476 break;
7477 case Scalar::Float64:
7478 case Scalar::BigInt64:
7479 case Scalar::BigUint64:
7480 masm.byteSwap64(outputReg64);
7481 break;
7482 case Scalar::Int8:
7483 case Scalar::Uint8:
7484 case Scalar::Uint8Clamped:
7485 default:
7486 MOZ_CRASH("Invalid type");
7487 }
7488
7489 masm.bind(&skip);
7490 }
7491
7492 // Move the value into the output register.
7493 switch (elementType) {
7494 case Scalar::Int8:
7495 case Scalar::Uint8:
7496 case Scalar::Int16:
7497 case Scalar::Uint16:
7498 case Scalar::Int32:
7499 masm.tagValue(JSVAL_TYPE_INT32, outputScratch, output.valueReg());
7500 break;
7501 case Scalar::Uint32: {
7502 MacroAssembler::Uint32Mode uint32Mode =
7503 forceDoubleForUint32 ? MacroAssembler::Uint32Mode::ForceDouble
7504 : MacroAssembler::Uint32Mode::FailOnDouble;
7505 masm.boxUint32(outputScratch, output.valueReg(), uint32Mode,
7506 failure->label());
7507 break;
7508 }
7509 case Scalar::Float16: {
7510 FloatRegister scratchFloat32 = floatScratch0.get().asSingle();
7511 masm.moveGPRToFloat16(outputScratch, scratchFloat32, scratch2,
7512 liveVolatileRegs());
7513 masm.canonicalizeFloat(scratchFloat32);
7514 masm.convertFloat32ToDouble(scratchFloat32, floatScratch0);
7515 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
7516 break;
7517 }
7518 case Scalar::Float32: {
7519 FloatRegister scratchFloat32 = floatScratch0.get().asSingle();
7520 masm.moveGPRToFloat32(outputScratch, scratchFloat32);
7521 masm.canonicalizeFloat(scratchFloat32);
7522 masm.convertFloat32ToDouble(scratchFloat32, floatScratch0);
7523 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
7524 break;
7525 }
7526 case Scalar::Float64:
7527 masm.moveGPR64ToDouble(outputReg64, floatScratch0);
7528 masm.canonicalizeDouble(floatScratch0);
7529 masm.boxDouble(floatScratch0, output.valueReg(), floatScratch0);
7530 break;
7531 case Scalar::BigInt64:
7532 case Scalar::BigUint64: {
7533 // We need two extra registers. Reuse the obj/littleEndian registers.
7534 Register bigInt = obj;
7535 Register bigIntScratch = littleEndian;
7536 masm.push(bigInt);
7537 masm.push(bigIntScratch);
7538 Label fail, done;
7539 LiveRegisterSet save = liveVolatileRegs();
7540 save.takeUnchecked(bigInt);
7541 save.takeUnchecked(bigIntScratch);
7542 gc::Heap initialHeap = InitialBigIntHeap(cx_);
7543 EmitAllocateBigInt(masm, bigInt, bigIntScratch, save, initialHeap, &fail);
7544 masm.jump(&done);
7545
7546 masm.bind(&fail);
7547 masm.pop(bigIntScratch);
7548 masm.pop(bigInt);
7549 masm.jump(failure->label());
7550
7551 masm.bind(&done);
7552 masm.initializeBigInt64(elementType, bigInt, outputReg64);
7553 masm.tagValue(JSVAL_TYPE_BIGINT, bigInt, output.valueReg());
7554 masm.pop(bigIntScratch);
7555 masm.pop(bigInt);
7556 break;
7557 }
7558 case Scalar::Uint8Clamped:
7559 default:
7560 MOZ_CRASH("Invalid typed array type");
7561 }
7562
7563 return true;
7564 }
7565
7566 bool CacheIRCompiler::emitStoreDataViewValueResult(
7567 ObjOperandId objId, IntPtrOperandId offsetId, uint32_t valueId,
7568 BooleanOperandId littleEndianId, Scalar::Type elementType,
7569 ArrayBufferViewKind viewKind) {
7570 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7571
7572 AutoOutputRegister output(*this);
7573 #ifdef JS_CODEGEN_X86
7574 // Use a scratch register to avoid running out of the registers.
7575 Register obj = output.valueReg().typeReg();
7576 allocator.copyToScratchRegister(masm, objId, obj);
7577 #else
7578 Register obj = allocator.useRegister(masm, objId);
7579 #endif
7580 Register offset = allocator.useRegister(masm, offsetId);
7581 Register littleEndian = allocator.useRegister(masm, littleEndianId);
7582
7583 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
7584 Maybe<Register> valInt32;
7585 Maybe<Register> valBigInt;
7586 switch (elementType) {
7587 case Scalar::Int8:
7588 case Scalar::Uint8:
7589 case Scalar::Int16:
7590 case Scalar::Uint16:
7591 case Scalar::Int32:
7592 case Scalar::Uint32:
7593 case Scalar::Uint8Clamped:
7594 valInt32.emplace(allocator.useRegister(masm, Int32OperandId(valueId)));
7595 break;
7596
7597 case Scalar::Float16:
7598 case Scalar::Float32:
7599 case Scalar::Float64:
7600 allocator.ensureDoubleRegister(masm, NumberOperandId(valueId),
7601 floatScratch0);
7602 break;
7603
7604 case Scalar::BigInt64:
7605 case Scalar::BigUint64:
7606 valBigInt.emplace(allocator.useRegister(masm, BigIntOperandId(valueId)));
7607 break;
7608
7609 case Scalar::MaxTypedArrayViewType:
7610 case Scalar::Int64:
7611 case Scalar::Simd128:
7612 MOZ_CRASH("Unsupported type");
7613 }
7614
7615 Register scratch1 = output.valueReg().scratchReg();
7616 MOZ_ASSERT(scratch1 != obj, "scratchReg must not be typeReg");
7617
7618 // On platforms with enough registers, |scratch2| is an extra scratch register
7619 // (pair) used for byte-swapping the value.
7620 #ifndef JS_CODEGEN_X86
7621 mozilla::MaybeOneOf<AutoScratchRegister, AutoScratchRegister64> scratch2;
7622 switch (elementType) {
7623 case Scalar::Int8:
7624 case Scalar::Uint8:
7625 break;
7626 case Scalar::Int16:
7627 case Scalar::Uint16:
7628 case Scalar::Int32:
7629 case Scalar::Uint32:
7630 case Scalar::Float16:
7631 case Scalar::Float32:
7632 scratch2.construct<AutoScratchRegister>(allocator, masm);
7633 break;
7634 case Scalar::Float64:
7635 case Scalar::BigInt64:
7636 case Scalar::BigUint64:
7637 scratch2.construct<AutoScratchRegister64>(allocator, masm);
7638 break;
7639 case Scalar::Uint8Clamped:
7640 default:
7641 MOZ_CRASH("Invalid type");
7642 }
7643 #endif
7644
7645 Register boundsCheckScratch;
7646 #ifndef JS_CODEGEN_X86
7647 Maybe<AutoScratchRegister> maybeBoundsCheckScratch;
7648 if (viewKind == ArrayBufferViewKind::Resizable) {
7649 if (scratch2.constructed<AutoScratchRegister>()) {
7650 boundsCheckScratch = scratch2.ref<AutoScratchRegister>().get();
7651 } else if (scratch2.constructed<AutoScratchRegister64>()) {
7652 boundsCheckScratch =
7653 scratch2.ref<AutoScratchRegister64>().get().scratchReg();
7654 } else {
7655 maybeBoundsCheckScratch.emplace(allocator, masm);
7656 boundsCheckScratch = *maybeBoundsCheckScratch;
7657 }
7658 }
7659 #else
7660 // Not enough registers on x86.
7661 #endif
7662
7663 FailurePath* failure;
7664 if (!addFailurePath(&failure)) {
7665 return false;
7666 }
7667
7668 const size_t byteSize = Scalar::byteSize(elementType);
7669
7670 emitDataViewBoundsCheck(viewKind, byteSize, obj, offset, scratch1,
7671 boundsCheckScratch, failure->label());
7672
7673 masm.loadPtr(Address(obj, DataViewObject::dataOffset()), scratch1);
7674 BaseIndex dest(scratch1, offset, TimesOne);
7675
7676 if (byteSize == 1) {
7677 // Byte swapping has no effect, so just do the byte store.
7678 masm.store8(*valInt32, dest);
7679 masm.moveValue(UndefinedValue(), output.valueReg());
7680 return true;
7681 }
7682
7683 // On 32-bit x86, |obj| is already a scratch register so use that. If we need
7684 // a Register64 we also use the littleEndian register and use the stack
7685 // location for the check below.
7686 bool pushedLittleEndian = false;
7687 #ifdef JS_CODEGEN_X86
7688 if (byteSize == 8) {
7689 masm.push(littleEndian);
7690 pushedLittleEndian = true;
7691 }
7692 auto valScratch32 = [&]() -> Register { return obj; };
7693 auto valScratch64 = [&]() -> Register64 {
7694 return Register64(obj, littleEndian);
7695 };
7696 #else
7697 auto valScratch32 = [&]() -> Register {
7698 return scratch2.ref<AutoScratchRegister>();
7699 };
7700 auto valScratch64 = [&]() -> Register64 {
7701 return scratch2.ref<AutoScratchRegister64>();
7702 };
7703 #endif
7704
7705 // Canonicalize floating point values for differential testing.
7706 if (Scalar::isFloatingType(elementType) && js::SupportDifferentialTesting()) {
7707 masm.canonicalizeDouble(floatScratch0);
7708 }
7709
7710 // Load the value into a gpr register.
7711 switch (elementType) {
7712 case Scalar::Int16:
7713 case Scalar::Uint16:
7714 case Scalar::Int32:
7715 case Scalar::Uint32:
7716 masm.move32(*valInt32, valScratch32());
7717 break;
7718 case Scalar::Float16: {
7719 FloatRegister scratchFloat32 = floatScratch0.get().asSingle();
7720 masm.convertDoubleToFloat16(floatScratch0, scratchFloat32, valScratch32(),
7721 liveVolatileRegs());
7722 masm.moveFloat16ToGPR(scratchFloat32, valScratch32(), liveVolatileRegs());
7723 break;
7724 }
7725 case Scalar::Float32: {
7726 FloatRegister scratchFloat32 = floatScratch0.get().asSingle();
7727 masm.convertDoubleToFloat32(floatScratch0, scratchFloat32);
7728 masm.moveFloat32ToGPR(scratchFloat32, valScratch32());
7729 break;
7730 }
7731 case Scalar::Float64: {
7732 masm.moveDoubleToGPR64(floatScratch0, valScratch64());
7733 break;
7734 }
7735 case Scalar::BigInt64:
7736 case Scalar::BigUint64:
7737 masm.loadBigInt64(*valBigInt, valScratch64());
7738 break;
7739 case Scalar::Int8:
7740 case Scalar::Uint8:
7741 case Scalar::Uint8Clamped:
7742 default:
7743 MOZ_CRASH("Invalid type");
7744 }
7745
7746 // Swap the bytes in the loaded value.
7747 Label skip;
7748 if (pushedLittleEndian) {
7749 masm.branch32(MOZ_LITTLE_ENDIAN() ? Assembler::NotEqual : Assembler::Equal,
7750 Address(masm.getStackPointer(), 0), Imm32(0), &skip);
7751 } else {
7752 masm.branch32(MOZ_LITTLE_ENDIAN() ? Assembler::NotEqual : Assembler::Equal,
7753 littleEndian, Imm32(0), &skip);
7754 }
7755 switch (elementType) {
7756 case Scalar::Int16:
7757 masm.byteSwap16SignExtend(valScratch32());
7758 break;
7759 case Scalar::Uint16:
7760 case Scalar::Float16:
7761 masm.byteSwap16ZeroExtend(valScratch32());
7762 break;
7763 case Scalar::Int32:
7764 case Scalar::Uint32:
7765 case Scalar::Float32:
7766 masm.byteSwap32(valScratch32());
7767 break;
7768 case Scalar::Float64:
7769 case Scalar::BigInt64:
7770 case Scalar::BigUint64:
7771 masm.byteSwap64(valScratch64());
7772 break;
7773 case Scalar::Int8:
7774 case Scalar::Uint8:
7775 case Scalar::Uint8Clamped:
7776 default:
7777 MOZ_CRASH("Invalid type");
7778 }
7779 masm.bind(&skip);
7780
7781 // Store the value.
7782 switch (elementType) {
7783 case Scalar::Int16:
7784 case Scalar::Uint16:
7785 case Scalar::Float16:
7786 masm.store16Unaligned(valScratch32(), dest);
7787 break;
7788 case Scalar::Int32:
7789 case Scalar::Uint32:
7790 case Scalar::Float32:
7791 masm.store32Unaligned(valScratch32(), dest);
7792 break;
7793 case Scalar::Float64:
7794 case Scalar::BigInt64:
7795 case Scalar::BigUint64:
7796 masm.store64Unaligned(valScratch64(), dest);
7797 break;
7798 case Scalar::Int8:
7799 case Scalar::Uint8:
7800 case Scalar::Uint8Clamped:
7801 default:
7802 MOZ_CRASH("Invalid typed array type");
7803 }
7804
7805 #ifdef JS_CODEGEN_X86
7806 // Restore registers.
7807 if (pushedLittleEndian) {
7808 masm.pop(littleEndian);
7809 }
7810 #endif
7811
7812 masm.moveValue(UndefinedValue(), output.valueReg());
7813 return true;
7814 }
7815
7816 bool CacheIRCompiler::emitStoreFixedSlotUndefinedResult(ObjOperandId objId,
7817 uint32_t offsetOffset,
7818 ValOperandId rhsId) {
7819 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7820
7821 AutoOutputRegister output(*this);
7822 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
7823 Register obj = allocator.useRegister(masm, objId);
7824 ValueOperand val = allocator.useValueRegister(masm, rhsId);
7825
7826 StubFieldOffset offset(offsetOffset, StubField::Type::RawInt32);
7827 emitLoadStubField(offset, scratch);
7828
7829 BaseIndex slot(obj, scratch, TimesOne);
7830 EmitPreBarrier(masm, slot, MIRType::Value);
7831 masm.storeValue(val, slot);
7832 emitPostBarrierSlot(obj, val, scratch);
7833
7834 masm.moveValue(UndefinedValue(), output.valueReg());
7835 return true;
7836 }
7837
7838 bool CacheIRCompiler::emitLoadObjectResult(ObjOperandId objId) {
7839 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7840 AutoOutputRegister output(*this);
7841 Register obj = allocator.useRegister(masm, objId);
7842
7843 EmitStoreResult(masm, obj, JSVAL_TYPE_OBJECT, output);
7844
7845 return true;
7846 }
7847
7848 bool CacheIRCompiler::emitLoadStringResult(StringOperandId strId) {
7849 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7850 AutoOutputRegister output(*this);
7851 Register str = allocator.useRegister(masm, strId);
7852
7853 masm.tagValue(JSVAL_TYPE_STRING, str, output.valueReg());
7854
7855 return true;
7856 }
7857
7858 bool CacheIRCompiler::emitLoadSymbolResult(SymbolOperandId symId) {
7859 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7860 AutoOutputRegister output(*this);
7861 Register sym = allocator.useRegister(masm, symId);
7862
7863 masm.tagValue(JSVAL_TYPE_SYMBOL, sym, output.valueReg());
7864
7865 return true;
7866 }
7867
7868 bool CacheIRCompiler::emitLoadInt32Result(Int32OperandId valId) {
7869 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7870 AutoOutputRegister output(*this);
7871 Register val = allocator.useRegister(masm, valId);
7872
7873 masm.tagValue(JSVAL_TYPE_INT32, val, output.valueReg());
7874
7875 return true;
7876 }
7877
7878 bool CacheIRCompiler::emitLoadBigIntResult(BigIntOperandId valId) {
7879 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7880 AutoOutputRegister output(*this);
7881 Register val = allocator.useRegister(masm, valId);
7882
7883 masm.tagValue(JSVAL_TYPE_BIGINT, val, output.valueReg());
7884
7885 return true;
7886 }
7887
7888 bool CacheIRCompiler::emitLoadDoubleResult(NumberOperandId valId) {
7889 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7890 AutoOutputRegister output(*this);
7891 ValueOperand val = allocator.useValueRegister(masm, valId);
7892
7893 #ifdef DEBUG
7894 Label ok;
7895 masm.branchTestDouble(Assembler::Equal, val, &ok);
7896 masm.branchTestInt32(Assembler::Equal, val, &ok);
7897 masm.assumeUnreachable("input must be double or int32");
7898 masm.bind(&ok);
7899 #endif
7900
7901 masm.moveValue(val, output.valueReg());
7902 masm.convertInt32ValueToDouble(output.valueReg());
7903
7904 return true;
7905 }
7906
7907 bool CacheIRCompiler::emitLoadTypeOfObjectResult(ObjOperandId objId) {
7908 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7909 AutoOutputRegister output(*this);
7910 Register obj = allocator.useRegister(masm, objId);
7911 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
7912
7913 Label slowCheck, isObject, isCallable, isUndefined, done;
7914 masm.typeOfObject(obj, scratch, &slowCheck, &isObject, &isCallable,
7915 &isUndefined);
7916
7917 masm.bind(&isCallable);
7918 masm.moveValue(StringValue(cx_->names().function), output.valueReg());
7919 masm.jump(&done);
7920
7921 masm.bind(&isUndefined);
7922 masm.moveValue(StringValue(cx_->names().undefined), output.valueReg());
7923 masm.jump(&done);
7924
7925 masm.bind(&isObject);
7926 masm.moveValue(StringValue(cx_->names().object), output.valueReg());
7927 masm.jump(&done);
7928
7929 {
7930 masm.bind(&slowCheck);
7931 LiveRegisterSet save = liveVolatileRegs();
7932 masm.PushRegsInMask(save);
7933
7934 using Fn = JSString* (*)(JSObject* obj, JSRuntime* rt);
7935 masm.setupUnalignedABICall(scratch);
7936 masm.passABIArg(obj);
7937 masm.movePtr(ImmPtr(cx_->runtime()), scratch);
7938 masm.passABIArg(scratch);
7939 masm.callWithABI<Fn, TypeOfNameObject>();
7940 masm.storeCallPointerResult(scratch);
7941
7942 LiveRegisterSet ignore;
7943 ignore.add(scratch);
7944 masm.PopRegsInMaskIgnore(save, ignore);
7945
7946 masm.tagValue(JSVAL_TYPE_STRING, scratch, output.valueReg());
7947 }
7948
7949 masm.bind(&done);
7950 return true;
7951 }
7952
7953 bool CacheIRCompiler::emitLoadTypeOfEqObjectResult(ObjOperandId objId,
7954 TypeofEqOperand operand) {
7955 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
7956 AutoOutputRegister output(*this);
7957 Register obj = allocator.useRegister(masm, objId);
7958 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
7959 JSType type = operand.type();
7960 JSOp compareOp = operand.compareOp();
7961 bool result;
7962
7963 Label slowCheck, isObject, isCallable, isUndefined, done;
7964 masm.typeOfObject(obj, scratch, &slowCheck, &isObject, &isCallable,
7965 &isUndefined);
7966
7967 masm.bind(&isCallable);
7968 result = type == JSTYPE_FUNCTION;
7969 if (compareOp == JSOp::Ne) {
7970 result = !result;
7971 }
7972 masm.moveValue(BooleanValue(result), output.valueReg());
7973 masm.jump(&done);
7974
7975 masm.bind(&isUndefined);
7976 result = type == JSTYPE_UNDEFINED;
7977 if (compareOp == JSOp::Ne) {
7978 result = !result;
7979 }
7980 masm.moveValue(BooleanValue(result), output.valueReg());
7981 masm.jump(&done);
7982
7983 masm.bind(&isObject);
7984 result = type == JSTYPE_OBJECT;
7985 if (compareOp == JSOp::Ne) {
7986 result = !result;
7987 }
7988 masm.moveValue(BooleanValue(result), output.valueReg());
7989 masm.jump(&done);
7990
7991 {
7992 masm.bind(&slowCheck);
7993 LiveRegisterSet save = liveVolatileRegs();
7994 save.takeUnchecked(output.valueReg());
7995 save.takeUnchecked(scratch);
7996 masm.PushRegsInMask(save);
7997
7998 using Fn = bool (*)(JSObject* obj, TypeofEqOperand operand);
7999 masm.setupUnalignedABICall(scratch);
8000 masm.passABIArg(obj);
8001 masm.move32(Imm32(TypeofEqOperand(type, compareOp).rawValue()), scratch);
8002 masm.passABIArg(scratch);
8003 masm.callWithABI<Fn, TypeOfEqObject>();
8004 masm.storeCallBoolResult(scratch);
8005
8006 masm.PopRegsInMask(save);
8007
8008 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
8009 }
8010
8011 masm.bind(&done);
8012 return true;
8013 }
8014
8015 bool CacheIRCompiler::emitLoadInt32TruthyResult(ValOperandId inputId) {
8016 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8017 AutoOutputRegister output(*this);
8018 ValueOperand val = allocator.useValueRegister(masm, inputId);
8019
8020 Label ifFalse, done;
8021 masm.branchTestInt32Truthy(false, val, &ifFalse);
8022 masm.moveValue(BooleanValue(true), output.valueReg());
8023 masm.jump(&done);
8024
8025 masm.bind(&ifFalse);
8026 masm.moveValue(BooleanValue(false), output.valueReg());
8027
8028 masm.bind(&done);
8029 return true;
8030 }
8031
8032 bool CacheIRCompiler::emitLoadStringTruthyResult(StringOperandId strId) {
8033 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8034 AutoOutputRegister output(*this);
8035 Register str = allocator.useRegister(masm, strId);
8036
8037 Label ifFalse, done;
8038 masm.branch32(Assembler::Equal, Address(str, JSString::offsetOfLength()),
8039 Imm32(0), &ifFalse);
8040 masm.moveValue(BooleanValue(true), output.valueReg());
8041 masm.jump(&done);
8042
8043 masm.bind(&ifFalse);
8044 masm.moveValue(BooleanValue(false), output.valueReg());
8045
8046 masm.bind(&done);
8047 return true;
8048 }
8049
8050 bool CacheIRCompiler::emitLoadDoubleTruthyResult(NumberOperandId inputId) {
8051 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8052 AutoOutputRegister output(*this);
8053
8054 AutoScratchFloatRegister floatReg(this);
8055
8056 allocator.ensureDoubleRegister(masm, inputId, floatReg);
8057
8058 Label ifFalse, done;
8059
8060 masm.branchTestDoubleTruthy(false, floatReg, &ifFalse);
8061 masm.moveValue(BooleanValue(true), output.valueReg());
8062 masm.jump(&done);
8063
8064 masm.bind(&ifFalse);
8065 masm.moveValue(BooleanValue(false), output.valueReg());
8066
8067 masm.bind(&done);
8068 return true;
8069 }
8070
8071 bool CacheIRCompiler::emitLoadObjectTruthyResult(ObjOperandId objId) {
8072 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8073 AutoOutputRegister output(*this);
8074 Register obj = allocator.useRegister(masm, objId);
8075 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
8076
8077 Label emulatesUndefined, slowPath, done;
8078 masm.branchIfObjectEmulatesUndefined(obj, scratch, &slowPath,
8079 &emulatesUndefined);
8080 masm.moveValue(BooleanValue(true), output.valueReg());
8081 masm.jump(&done);
8082
8083 masm.bind(&emulatesUndefined);
8084 masm.moveValue(BooleanValue(false), output.valueReg());
8085 masm.jump(&done);
8086
8087 masm.bind(&slowPath);
8088 {
8089 LiveRegisterSet volatileRegs = liveVolatileRegs();
8090 volatileRegs.takeUnchecked(scratch);
8091 volatileRegs.takeUnchecked(output);
8092 masm.PushRegsInMask(volatileRegs);
8093
8094 using Fn = bool (*)(JSObject* obj);
8095 masm.setupUnalignedABICall(scratch);
8096 masm.passABIArg(obj);
8097 masm.callWithABI<Fn, js::EmulatesUndefined>();
8098 masm.storeCallBoolResult(scratch);
8099 masm.xor32(Imm32(1), scratch);
8100
8101 masm.PopRegsInMask(volatileRegs);
8102
8103 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
8104 }
8105
8106 masm.bind(&done);
8107 return true;
8108 }
8109
8110 bool CacheIRCompiler::emitLoadBigIntTruthyResult(BigIntOperandId bigIntId) {
8111 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8112 AutoOutputRegister output(*this);
8113 Register bigInt = allocator.useRegister(masm, bigIntId);
8114
8115 Label ifFalse, done;
8116 masm.branch32(Assembler::Equal,
8117 Address(bigInt, BigInt::offsetOfDigitLength()), Imm32(0),
8118 &ifFalse);
8119 masm.moveValue(BooleanValue(true), output.valueReg());
8120 masm.jump(&done);
8121
8122 masm.bind(&ifFalse);
8123 masm.moveValue(BooleanValue(false), output.valueReg());
8124
8125 masm.bind(&done);
8126 return true;
8127 }
8128
8129 bool CacheIRCompiler::emitLoadValueTruthyResult(ValOperandId inputId) {
8130 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8131
8132 AutoOutputRegister output(*this);
8133 ValueOperand value = allocator.useValueRegister(masm, inputId);
8134 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
8135 AutoScratchRegister scratch2(allocator, masm);
8136 AutoScratchFloatRegister floatReg(this);
8137
8138 Label ifFalse, ifTrue, done;
8139
8140 {
8141 ScratchTagScope tag(masm, value);
8142 masm.splitTagForTest(value, tag);
8143
8144 masm.branchTestUndefined(Assembler::Equal, tag, &ifFalse);
8145 masm.branchTestNull(Assembler::Equal, tag, &ifFalse);
8146
8147 Label notBoolean;
8148 masm.branchTestBoolean(Assembler::NotEqual, tag, &notBoolean);
8149 {
8150 ScratchTagScopeRelease _(&tag);
8151 masm.branchTestBooleanTruthy(false, value, &ifFalse);
8152 masm.jump(&ifTrue);
8153 }
8154 masm.bind(&notBoolean);
8155
8156 Label notInt32;
8157 masm.branchTestInt32(Assembler::NotEqual, tag, &notInt32);
8158 {
8159 ScratchTagScopeRelease _(&tag);
8160 masm.branchTestInt32Truthy(false, value, &ifFalse);
8161 masm.jump(&ifTrue);
8162 }
8163 masm.bind(&notInt32);
8164
8165 Label notObject;
8166 masm.branchTestObject(Assembler::NotEqual, tag, &notObject);
8167 {
8168 ScratchTagScopeRelease _(&tag);
8169
8170 Register obj = masm.extractObject(value, scratch1);
8171
8172 Label slowPath;
8173 masm.branchIfObjectEmulatesUndefined(obj, scratch2, &slowPath, &ifFalse);
8174 masm.jump(&ifTrue);
8175
8176 masm.bind(&slowPath);
8177 {
8178 LiveRegisterSet volatileRegs = liveVolatileRegs();
8179 volatileRegs.takeUnchecked(scratch1);
8180 volatileRegs.takeUnchecked(scratch2);
8181 volatileRegs.takeUnchecked(output);
8182 masm.PushRegsInMask(volatileRegs);
8183
8184 using Fn = bool (*)(JSObject* obj);
8185 masm.setupUnalignedABICall(scratch2);
8186 masm.passABIArg(obj);
8187 masm.callWithABI<Fn, js::EmulatesUndefined>();
8188 masm.storeCallPointerResult(scratch2);
8189
8190 masm.PopRegsInMask(volatileRegs);
8191
8192 masm.branchIfTrueBool(scratch2, &ifFalse);
8193 masm.jump(&ifTrue);
8194 }
8195 }
8196 masm.bind(&notObject);
8197
8198 Label notString;
8199 masm.branchTestString(Assembler::NotEqual, tag, &notString);
8200 {
8201 ScratchTagScopeRelease _(&tag);
8202 masm.branchTestStringTruthy(false, value, &ifFalse);
8203 masm.jump(&ifTrue);
8204 }
8205 masm.bind(&notString);
8206
8207 Label notBigInt;
8208 masm.branchTestBigInt(Assembler::NotEqual, tag, &notBigInt);
8209 {
8210 ScratchTagScopeRelease _(&tag);
8211 masm.branchTestBigIntTruthy(false, value, &ifFalse);
8212 masm.jump(&ifTrue);
8213 }
8214 masm.bind(&notBigInt);
8215
8216 masm.branchTestSymbol(Assembler::Equal, tag, &ifTrue);
8217
8218 #ifdef DEBUG
8219 Label isDouble;
8220 masm.branchTestDouble(Assembler::Equal, tag, &isDouble);
8221 masm.assumeUnreachable("Unexpected value type");
8222 masm.bind(&isDouble);
8223 #endif
8224
8225 {
8226 ScratchTagScopeRelease _(&tag);
8227 masm.unboxDouble(value, floatReg);
8228 masm.branchTestDoubleTruthy(false, floatReg, &ifFalse);
8229 }
8230
8231 // Fall through to true case.
8232 }
8233
8234 masm.bind(&ifTrue);
8235 masm.moveValue(BooleanValue(true), output.valueReg());
8236 masm.jump(&done);
8237
8238 masm.bind(&ifFalse);
8239 masm.moveValue(BooleanValue(false), output.valueReg());
8240
8241 masm.bind(&done);
8242 return true;
8243 }
8244
8245 bool CacheIRCompiler::emitComparePointerResultShared(JSOp op,
8246 TypedOperandId lhsId,
8247 TypedOperandId rhsId) {
8248 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8249 AutoOutputRegister output(*this);
8250
8251 Register left = allocator.useRegister(masm, lhsId);
8252 Register right = allocator.useRegister(masm, rhsId);
8253
8254 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
8255
8256 Label ifTrue, done;
8257 masm.branchPtr(JSOpToCondition(op, /* signed = */ true), left, right,
8258 &ifTrue);
8259
8260 EmitStoreBoolean(masm, false, output);
8261 masm.jump(&done);
8262
8263 masm.bind(&ifTrue);
8264 EmitStoreBoolean(masm, true, output);
8265 masm.bind(&done);
8266 return true;
8267 }
8268
8269 bool CacheIRCompiler::emitCompareObjectResult(JSOp op, ObjOperandId lhsId,
8270 ObjOperandId rhsId) {
8271 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8272 return emitComparePointerResultShared(op, lhsId, rhsId);
8273 }
8274
8275 bool CacheIRCompiler::emitCompareSymbolResult(JSOp op, SymbolOperandId lhsId,
8276 SymbolOperandId rhsId) {
8277 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8278 return emitComparePointerResultShared(op, lhsId, rhsId);
8279 }
8280
8281 bool CacheIRCompiler::emitCompareInt32Result(JSOp op, Int32OperandId lhsId,
8282 Int32OperandId rhsId) {
8283 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8284 AutoOutputRegister output(*this);
8285 Register left = allocator.useRegister(masm, lhsId);
8286 Register right = allocator.useRegister(masm, rhsId);
8287
8288 Label ifTrue, done;
8289 masm.branch32(JSOpToCondition(op, /* signed = */ true), left, right, &ifTrue);
8290
8291 EmitStoreBoolean(masm, false, output);
8292 masm.jump(&done);
8293
8294 masm.bind(&ifTrue);
8295 EmitStoreBoolean(masm, true, output);
8296 masm.bind(&done);
8297 return true;
8298 }
8299
8300 bool CacheIRCompiler::emitCompareDoubleResult(JSOp op, NumberOperandId lhsId,
8301 NumberOperandId rhsId) {
8302 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8303 AutoOutputRegister output(*this);
8304
8305 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
8306 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
8307
8308 FailurePath* failure;
8309 if (!addFailurePath(&failure)) {
8310 return false;
8311 }
8312
8313 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
8314 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
8315
8316 Label done, ifTrue;
8317 masm.branchDouble(JSOpToDoubleCondition(op), floatScratch0, floatScratch1,
8318 &ifTrue);
8319 EmitStoreBoolean(masm, false, output);
8320 masm.jump(&done);
8321
8322 masm.bind(&ifTrue);
8323 EmitStoreBoolean(masm, true, output);
8324 masm.bind(&done);
8325 return true;
8326 }
8327
8328 bool CacheIRCompiler::emitCompareBigIntResult(JSOp op, BigIntOperandId lhsId,
8329 BigIntOperandId rhsId) {
8330 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8331 AutoOutputRegister output(*this);
8332
8333 Register lhs = allocator.useRegister(masm, lhsId);
8334 Register rhs = allocator.useRegister(masm, rhsId);
8335
8336 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
8337
8338 LiveRegisterSet save = liveVolatileRegs();
8339 masm.PushRegsInMask(save);
8340
8341 masm.setupUnalignedABICall(scratch);
8342
8343 // Push the operands in reverse order for JSOp::Le and JSOp::Gt:
8344 // - |left <= right| is implemented as |right >= left|.
8345 // - |left > right| is implemented as |right < left|.
8346 if (op == JSOp::Le || op == JSOp::Gt) {
8347 masm.passABIArg(rhs);
8348 masm.passABIArg(lhs);
8349 } else {
8350 masm.passABIArg(lhs);
8351 masm.passABIArg(rhs);
8352 }
8353
8354 using Fn = bool (*)(BigInt*, BigInt*);
8355 Fn fn;
8356 if (op == JSOp::Eq || op == JSOp::StrictEq) {
8357 fn = jit::BigIntEqual<EqualityKind::Equal>;
8358 } else if (op == JSOp::Ne || op == JSOp::StrictNe) {
8359 fn = jit::BigIntEqual<EqualityKind::NotEqual>;
8360 } else if (op == JSOp::Lt || op == JSOp::Gt) {
8361 fn = jit::BigIntCompare<ComparisonKind::LessThan>;
8362 } else {
8363 MOZ_ASSERT(op == JSOp::Le || op == JSOp::Ge);
8364 fn = jit::BigIntCompare<ComparisonKind::GreaterThanOrEqual>;
8365 }
8366
8367 masm.callWithABI(DynamicFunction<Fn>(fn));
8368 masm.storeCallBoolResult(scratch);
8369
8370 LiveRegisterSet ignore;
8371 ignore.add(scratch);
8372 masm.PopRegsInMaskIgnore(save, ignore);
8373
8374 EmitStoreResult(masm, scratch, JSVAL_TYPE_BOOLEAN, output);
8375 return true;
8376 }
8377
8378 bool CacheIRCompiler::emitCompareBigIntInt32Result(JSOp op,
8379 BigIntOperandId lhsId,
8380 Int32OperandId rhsId) {
8381 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8382 AutoOutputRegister output(*this);
8383 Register bigInt = allocator.useRegister(masm, lhsId);
8384 Register int32 = allocator.useRegister(masm, rhsId);
8385
8386 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
8387 AutoScratchRegister scratch2(allocator, masm);
8388
8389 Label ifTrue, ifFalse;
8390 masm.compareBigIntAndInt32(op, bigInt, int32, scratch1, scratch2, &ifTrue,
8391 &ifFalse);
8392
8393 Label done;
8394 masm.bind(&ifFalse);
8395 EmitStoreBoolean(masm, false, output);
8396 masm.jump(&done);
8397
8398 masm.bind(&ifTrue);
8399 EmitStoreBoolean(masm, true, output);
8400
8401 masm.bind(&done);
8402 return true;
8403 }
8404
8405 bool CacheIRCompiler::emitCompareBigIntNumberResult(JSOp op,
8406 BigIntOperandId lhsId,
8407 NumberOperandId rhsId) {
8408 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8409 AutoOutputRegister output(*this);
8410
8411 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
8412
8413 Register lhs = allocator.useRegister(masm, lhsId);
8414 allocator.ensureDoubleRegister(masm, rhsId, floatScratch0);
8415
8416 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
8417
8418 LiveRegisterSet save = liveVolatileRegs();
8419 masm.PushRegsInMask(save);
8420
8421 masm.setupUnalignedABICall(scratch);
8422
8423 // Push the operands in reverse order for JSOp::Le and JSOp::Gt:
8424 // - |left <= right| is implemented as |right >= left|.
8425 // - |left > right| is implemented as |right < left|.
8426 if (op == JSOp::Le || op == JSOp::Gt) {
8427 masm.passABIArg(floatScratch0, ABIType::Float64);
8428 masm.passABIArg(lhs);
8429 } else {
8430 masm.passABIArg(lhs);
8431 masm.passABIArg(floatScratch0, ABIType::Float64);
8432 }
8433
8434 using FnBigIntNumber = bool (*)(BigInt*, double);
8435 using FnNumberBigInt = bool (*)(double, BigInt*);
8436 switch (op) {
8437 case JSOp::Eq: {
8438 masm.callWithABI<FnBigIntNumber,
8439 jit::BigIntNumberEqual<EqualityKind::Equal>>();
8440 break;
8441 }
8442 case JSOp::Ne: {
8443 masm.callWithABI<FnBigIntNumber,
8444 jit::BigIntNumberEqual<EqualityKind::NotEqual>>();
8445 break;
8446 }
8447 case JSOp::Lt: {
8448 masm.callWithABI<FnBigIntNumber,
8449 jit::BigIntNumberCompare<ComparisonKind::LessThan>>();
8450 break;
8451 }
8452 case JSOp::Gt: {
8453 masm.callWithABI<FnNumberBigInt,
8454 jit::NumberBigIntCompare<ComparisonKind::LessThan>>();
8455 break;
8456 }
8457 case JSOp::Le: {
8458 masm.callWithABI<
8459 FnNumberBigInt,
8460 jit::NumberBigIntCompare<ComparisonKind::GreaterThanOrEqual>>();
8461 break;
8462 }
8463 case JSOp::Ge: {
8464 masm.callWithABI<
8465 FnBigIntNumber,
8466 jit::BigIntNumberCompare<ComparisonKind::GreaterThanOrEqual>>();
8467 break;
8468 }
8469 default:
8470 MOZ_CRASH("unhandled op");
8471 }
8472
8473 masm.storeCallBoolResult(scratch);
8474
8475 LiveRegisterSet ignore;
8476 ignore.add(scratch);
8477 masm.PopRegsInMaskIgnore(save, ignore);
8478
8479 EmitStoreResult(masm, scratch, JSVAL_TYPE_BOOLEAN, output);
8480 return true;
8481 }
8482
8483 bool CacheIRCompiler::emitCompareBigIntStringResult(JSOp op,
8484 BigIntOperandId lhsId,
8485 StringOperandId rhsId) {
8486 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8487 AutoCallVM callvm(masm, this, allocator);
8488
8489 Register lhs = allocator.useRegister(masm, lhsId);
8490 Register rhs = allocator.useRegister(masm, rhsId);
8491
8492 callvm.prepare();
8493
8494 // Push the operands in reverse order for JSOp::Le and JSOp::Gt:
8495 // - |left <= right| is implemented as |right >= left|.
8496 // - |left > right| is implemented as |right < left|.
8497 if (op == JSOp::Le || op == JSOp::Gt) {
8498 masm.Push(lhs);
8499 masm.Push(rhs);
8500 } else {
8501 masm.Push(rhs);
8502 masm.Push(lhs);
8503 }
8504
8505 using FnBigIntString =
8506 bool (*)(JSContext*, HandleBigInt, HandleString, bool*);
8507 using FnStringBigInt =
8508 bool (*)(JSContext*, HandleString, HandleBigInt, bool*);
8509
8510 switch (op) {
8511 case JSOp::Eq: {
8512 constexpr auto Equal = EqualityKind::Equal;
8513 callvm.call<FnBigIntString, BigIntStringEqual<Equal>>();
8514 break;
8515 }
8516 case JSOp::Ne: {
8517 constexpr auto NotEqual = EqualityKind::NotEqual;
8518 callvm.call<FnBigIntString, BigIntStringEqual<NotEqual>>();
8519 break;
8520 }
8521 case JSOp::Lt: {
8522 constexpr auto LessThan = ComparisonKind::LessThan;
8523 callvm.call<FnBigIntString, BigIntStringCompare<LessThan>>();
8524 break;
8525 }
8526 case JSOp::Gt: {
8527 constexpr auto LessThan = ComparisonKind::LessThan;
8528 callvm.call<FnStringBigInt, StringBigIntCompare<LessThan>>();
8529 break;
8530 }
8531 case JSOp::Le: {
8532 constexpr auto GreaterThanOrEqual = ComparisonKind::GreaterThanOrEqual;
8533 callvm.call<FnStringBigInt, StringBigIntCompare<GreaterThanOrEqual>>();
8534 break;
8535 }
8536 case JSOp::Ge: {
8537 constexpr auto GreaterThanOrEqual = ComparisonKind::GreaterThanOrEqual;
8538 callvm.call<FnBigIntString, BigIntStringCompare<GreaterThanOrEqual>>();
8539 break;
8540 }
8541 default:
8542 MOZ_CRASH("unhandled op");
8543 }
8544 return true;
8545 }
8546
8547 bool CacheIRCompiler::emitCompareNullUndefinedResult(JSOp op, bool isUndefined,
8548 ValOperandId inputId) {
8549 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8550
8551 AutoOutputRegister output(*this);
8552 ValueOperand input = allocator.useValueRegister(masm, inputId);
8553 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
8554 AutoScratchRegister scratch2(allocator, masm);
8555
8556 if (IsStrictEqualityOp(op)) {
8557 if (isUndefined) {
8558 masm.testUndefinedSet(JSOpToCondition(op, false), input, scratch);
8559 } else {
8560 masm.testNullSet(JSOpToCondition(op, false), input, scratch);
8561 }
8562 EmitStoreResult(masm, scratch, JSVAL_TYPE_BOOLEAN, output);
8563 return true;
8564 }
8565
8566 FailurePath* failure;
8567 if (!addFailurePath(&failure)) {
8568 return false;
8569 }
8570
8571 MOZ_ASSERT(IsLooseEqualityOp(op));
8572
8573 Label nullOrLikeUndefined, notNullOrLikeUndefined, done;
8574 {
8575 ScratchTagScope tag(masm, input);
8576 masm.splitTagForTest(input, tag);
8577
8578 if (isUndefined) {
8579 masm.branchTestUndefined(Assembler::Equal, tag, &nullOrLikeUndefined);
8580 masm.branchTestNull(Assembler::Equal, tag, &nullOrLikeUndefined);
8581 } else {
8582 masm.branchTestNull(Assembler::Equal, tag, &nullOrLikeUndefined);
8583 masm.branchTestUndefined(Assembler::Equal, tag, &nullOrLikeUndefined);
8584 }
8585 masm.branchTestObject(Assembler::NotEqual, tag, &notNullOrLikeUndefined);
8586
8587 {
8588 ScratchTagScopeRelease _(&tag);
8589
8590 masm.unboxObject(input, scratch);
8591 masm.branchIfObjectEmulatesUndefined(scratch, scratch2, failure->label(),
8592 &nullOrLikeUndefined);
8593 masm.jump(&notNullOrLikeUndefined);
8594 }
8595 }
8596
8597 masm.bind(&nullOrLikeUndefined);
8598 EmitStoreBoolean(masm, op == JSOp::Eq, output);
8599 masm.jump(&done);
8600
8601 masm.bind(&notNullOrLikeUndefined);
8602 EmitStoreBoolean(masm, op == JSOp::Ne, output);
8603
8604 masm.bind(&done);
8605 return true;
8606 }
8607
8608 bool CacheIRCompiler::emitCompareDoubleSameValueResult(NumberOperandId lhsId,
8609 NumberOperandId rhsId) {
8610 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8611
8612 AutoOutputRegister output(*this);
8613 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
8614 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
8615 AutoAvailableFloatRegister floatScratch1(*this, FloatReg1);
8616 AutoAvailableFloatRegister floatScratch2(*this, FloatReg2);
8617
8618 allocator.ensureDoubleRegister(masm, lhsId, floatScratch0);
8619 allocator.ensureDoubleRegister(masm, rhsId, floatScratch1);
8620
8621 masm.sameValueDouble(floatScratch0, floatScratch1, floatScratch2, scratch);
8622 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
8623 return true;
8624 }
8625
8626 bool CacheIRCompiler::emitIndirectTruncateInt32Result(Int32OperandId valId) {
8627 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8628 AutoOutputRegister output(*this);
8629 Register val = allocator.useRegister(masm, valId);
8630
8631 if (output.hasValue()) {
8632 masm.tagValue(JSVAL_TYPE_INT32, val, output.valueReg());
8633 } else {
8634 masm.mov(val, output.typedReg().gpr());
8635 }
8636 return true;
8637 }
8638
8639 bool CacheIRCompiler::emitCallPrintString(const char* str) {
8640 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8641 masm.printf(str);
8642 return true;
8643 }
8644
8645 bool CacheIRCompiler::emitBreakpoint() {
8646 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8647 masm.breakpoint();
8648 return true;
8649 }
8650
8651 void CacheIRCompiler::emitPostBarrierShared(Register obj,
8652 const ConstantOrRegister& val,
8653 Register scratch,
8654 Register maybeIndex) {
8655 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8656
8657 if (val.constant()) {
8658 MOZ_ASSERT_IF(val.value().isGCThing(),
8659 !IsInsideNursery(val.value().toGCThing()));
8660 return;
8661 }
8662
8663 TypedOrValueRegister reg = val.reg();
8664 if (reg.hasTyped() && !NeedsPostBarrier(reg.type())) {
8665 return;
8666 }
8667
8668 Label skipBarrier;
8669 if (reg.hasValue()) {
8670 masm.branchValueIsNurseryCell(Assembler::NotEqual, reg.valueReg(), scratch,
8671 &skipBarrier);
8672 } else {
8673 masm.branchPtrInNurseryChunk(Assembler::NotEqual, reg.typedReg().gpr(),
8674 scratch, &skipBarrier);
8675 }
8676 masm.branchPtrInNurseryChunk(Assembler::Equal, obj, scratch, &skipBarrier);
8677
8678 // Check one element cache to avoid VM call.
8679 auto* lastCellAddr = cx_->runtime()->gc.addressOfLastBufferedWholeCell();
8680 masm.branchPtr(Assembler::Equal, AbsoluteAddress(lastCellAddr), obj,
8681 &skipBarrier);
8682
8683 // Call one of these, depending on maybeIndex:
8684 //
8685 // void PostWriteBarrier(JSRuntime* rt, JSObject* obj);
8686 // void PostWriteElementBarrier(JSRuntime* rt, JSObject* obj,
8687 // int32_t index);
8688 LiveRegisterSet save = liveVolatileRegs();
8689 masm.PushRegsInMask(save);
8690 masm.setupUnalignedABICall(scratch);
8691 masm.movePtr(ImmPtr(cx_->runtime()), scratch);
8692 masm.passABIArg(scratch);
8693 masm.passABIArg(obj);
8694 if (maybeIndex != InvalidReg) {
8695 masm.passABIArg(maybeIndex);
8696 using Fn = void (*)(JSRuntime* rt, JSObject* obj, int32_t index);
8697 masm.callWithABI<Fn, PostWriteElementBarrier>();
8698 } else {
8699 using Fn = void (*)(JSRuntime* rt, js::gc::Cell* cell);
8700 masm.callWithABI<Fn, PostWriteBarrier>();
8701 }
8702 masm.PopRegsInMask(save);
8703
8704 masm.bind(&skipBarrier);
8705 }
8706
8707 bool CacheIRCompiler::emitWrapResult() {
8708 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8709 AutoOutputRegister output(*this);
8710 AutoScratchRegister scratch(allocator, masm);
8711
8712 FailurePath* failure;
8713 if (!addFailurePath(&failure)) {
8714 return false;
8715 }
8716
8717 Label done;
8718 // We only have to wrap objects, because we are in the same zone.
8719 masm.branchTestObject(Assembler::NotEqual, output.valueReg(), &done);
8720
8721 Register obj = output.valueReg().scratchReg();
8722 masm.unboxObject(output.valueReg(), obj);
8723
8724 LiveRegisterSet save = liveVolatileRegs();
8725 masm.PushRegsInMask(save);
8726
8727 using Fn = JSObject* (*)(JSContext* cx, JSObject* obj);
8728 masm.setupUnalignedABICall(scratch);
8729 masm.loadJSContext(scratch);
8730 masm.passABIArg(scratch);
8731 masm.passABIArg(obj);
8732 masm.callWithABI<Fn, WrapObjectPure>();
8733 masm.storeCallPointerResult(obj);
8734
8735 LiveRegisterSet ignore;
8736 ignore.add(obj);
8737 masm.PopRegsInMaskIgnore(save, ignore);
8738
8739 // We could not get a wrapper for this object.
8740 masm.branchTestPtr(Assembler::Zero, obj, obj, failure->label());
8741
8742 // We clobbered the output register, so we have to retag.
8743 masm.tagValue(JSVAL_TYPE_OBJECT, obj, output.valueReg());
8744
8745 masm.bind(&done);
8746 return true;
8747 }
8748
8749 bool CacheIRCompiler::emitMegamorphicLoadSlotByValueResult(ObjOperandId objId,
8750 ValOperandId idId) {
8751 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8752 AutoOutputRegister output(*this);
8753
8754 Register obj = allocator.useRegister(masm, objId);
8755 ValueOperand idVal = allocator.useValueRegister(masm, idId);
8756
8757 #ifdef JS_CODEGEN_X86
8758 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
8759 AutoScratchRegisterMaybeOutputType scratch2(allocator, masm, output);
8760 #else
8761 AutoScratchRegister scratch1(allocator, masm);
8762 AutoScratchRegister scratch2(allocator, masm);
8763 AutoScratchRegister scratch3(allocator, masm);
8764 #endif
8765
8766 FailurePath* failure;
8767 if (!addFailurePath(&failure)) {
8768 return false;
8769 }
8770
8771 #ifdef JS_CODEGEN_X86
8772 masm.xorPtr(scratch2, scratch2);
8773 #else
8774 Label cacheHit;
8775 masm.emitMegamorphicCacheLookupByValue(
8776 idVal, obj, scratch1, scratch3, scratch2, output.valueReg(), &cacheHit);
8777 #endif
8778
8779 masm.branchIfNonNativeObj(obj, scratch1, failure->label());
8780
8781 // idVal will be in vp[0], result will be stored in vp[1].
8782 masm.reserveStack(sizeof(Value));
8783 masm.Push(idVal);
8784 masm.moveStackPtrTo(idVal.scratchReg());
8785
8786 LiveRegisterSet volatileRegs = liveVolatileRegs();
8787 volatileRegs.takeUnchecked(scratch1);
8788 volatileRegs.takeUnchecked(idVal);
8789 masm.PushRegsInMask(volatileRegs);
8790
8791 using Fn = bool (*)(JSContext* cx, JSObject* obj,
8792 MegamorphicCache::Entry* cacheEntry, Value* vp);
8793 masm.setupUnalignedABICall(scratch1);
8794 masm.loadJSContext(scratch1);
8795 masm.passABIArg(scratch1);
8796 masm.passABIArg(obj);
8797 masm.passABIArg(scratch2);
8798 masm.passABIArg(idVal.scratchReg());
8799 masm.callWithABI<Fn, GetNativeDataPropertyByValuePure>();
8800
8801 masm.storeCallPointerResult(scratch1);
8802 masm.PopRegsInMask(volatileRegs);
8803
8804 masm.Pop(idVal);
8805
8806 Label ok;
8807 uint32_t framePushed = masm.framePushed();
8808 masm.branchIfTrueBool(scratch1, &ok);
8809 masm.adjustStack(sizeof(Value));
8810 masm.jump(failure->label());
8811
8812 masm.bind(&ok);
8813 masm.setFramePushed(framePushed);
8814 masm.loadTypedOrValue(Address(masm.getStackPointer(), 0), output);
8815 masm.adjustStack(sizeof(Value));
8816
8817 #ifndef JS_CODEGEN_X86
8818 masm.bind(&cacheHit);
8819 #endif
8820 return true;
8821 }
8822
8823 bool CacheIRCompiler::emitMegamorphicLoadSlotByValuePermissiveResult(
8824 ObjOperandId objId, ValOperandId idId) {
8825 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8826
8827 AutoCallVM callvm(masm, this, allocator);
8828
8829 const AutoOutputRegister& output = callvm.output();
8830
8831 Register obj = allocator.useRegister(masm, objId);
8832 ValueOperand idVal = allocator.useValueRegister(masm, idId);
8833
8834 #ifdef JS_CODEGEN_X86
8835 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
8836 AutoScratchRegisterMaybeOutputType scratch2(allocator, masm, output);
8837 #else
8838 AutoScratchRegister scratch1(allocator, masm);
8839 AutoScratchRegister scratch2(allocator, masm);
8840 AutoScratchRegister scratch3(allocator, masm);
8841 #endif
8842
8843 #ifdef JS_CODEGEN_X86
8844 masm.xorPtr(scratch2, scratch2);
8845 #else
8846 Label cacheHit;
8847 masm.emitMegamorphicCacheLookupByValue(
8848 idVal, obj, scratch1, scratch3, scratch2, output.valueReg(), &cacheHit);
8849 #endif
8850
8851 callvm.prepare();
8852
8853 masm.Push(scratch2);
8854 masm.Push(idVal);
8855 masm.Push(obj);
8856
8857 using Fn = bool (*)(JSContext*, HandleObject, HandleValue,
8858 MegamorphicCacheEntry*, MutableHandleValue);
8859 callvm.call<Fn, GetElemMaybeCached>();
8860
8861 #ifndef JS_CODEGEN_X86
8862 masm.bind(&cacheHit);
8863 #endif
8864 return true;
8865 }
8866
8867 bool CacheIRCompiler::emitMegamorphicHasPropResult(ObjOperandId objId,
8868 ValOperandId idId,
8869 bool hasOwn) {
8870 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
8871 AutoOutputRegister output(*this);
8872
8873 Register obj = allocator.useRegister(masm, objId);
8874 ValueOperand idVal = allocator.useValueRegister(masm, idId);
8875
8876 #ifdef JS_CODEGEN_X86
8877 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
8878 AutoScratchRegisterMaybeOutputType scratch2(allocator, masm, output);
8879 #else
8880 AutoScratchRegister scratch1(allocator, masm);
8881 AutoScratchRegister scratch2(allocator, masm);
8882 AutoScratchRegister scratch3(allocator, masm);
8883 #endif
8884
8885 FailurePath* failure;
8886 if (!addFailurePath(&failure)) {
8887 return false;
8888 }
8889
8890 #ifndef JS_CODEGEN_X86
8891 Label cacheHit, done;
8892 masm.emitMegamorphicCacheLookupExists(idVal, obj, scratch1, scratch3,
8893 scratch2, output.maybeReg(), &cacheHit,
8894 hasOwn);
8895 #else
8896 masm.xorPtr(scratch2, scratch2);
8897 #endif
8898
8899 masm.branchIfNonNativeObj(obj, scratch1, failure->label());
8900
8901 // idVal will be in vp[0], result will be stored in vp[1].
8902 masm.reserveStack(sizeof(Value));
8903 masm.Push(idVal);
8904 masm.moveStackPtrTo(idVal.scratchReg());
8905
8906 LiveRegisterSet volatileRegs = liveVolatileRegs();
8907 volatileRegs.takeUnchecked(scratch1);
8908 volatileRegs.takeUnchecked(idVal);
8909 masm.PushRegsInMask(volatileRegs);
8910
8911 using Fn = bool (*)(JSContext* cx, JSObject* obj,
8912 MegamorphicCache::Entry* cacheEntry, Value* vp);
8913 masm.setupUnalignedABICall(scratch1);
8914 masm.loadJSContext(scratch1);
8915 masm.passABIArg(scratch1);
8916 masm.passABIArg(obj);
8917 masm.passABIArg(scratch2);
8918 masm.passABIArg(idVal.scratchReg());
8919 if (hasOwn) {
8920 masm.callWithABI<Fn, HasNativeDataPropertyPure<true>>();
8921 } else {
8922 masm.callWithABI<Fn, HasNativeDataPropertyPure<false>>();
8923 }
8924 masm.storeCallPointerResult(scratch1);
8925 masm.PopRegsInMask(volatileRegs);
8926
8927 masm.Pop(idVal);
8928
8929 Label ok;
8930 uint32_t framePushed = masm.framePushed();
8931 masm.branchIfTrueBool(scratch1, &ok);
8932 masm.adjustStack(sizeof(Value));
8933 masm.jump(failure->label());
8934
8935 masm.bind(&ok);
8936 masm.setFramePushed(framePushed);
8937 masm.loadTypedOrValue(Address(masm.getStackPointer(), 0), output);
8938 masm.adjustStack(sizeof(Value));
8939
8940 #ifndef JS_CODEGEN_X86
8941 masm.jump(&done);
8942 masm.bind(&cacheHit);
8943 if (output.hasValue()) {
8944 masm.tagValue(JSVAL_TYPE_BOOLEAN, output.valueReg().scratchReg(),
8945 output.valueReg());
8946 }
8947 masm.bind(&done);
8948 #endif
8949 return true;
8950 }
8951
8952 bool CacheIRCompiler::emitSmallObjectVariableKeyHasOwnResult(
8953 StringOperandId idId, uint32_t propNamesOffset, uint32_t shapeOffset) {
8954 StubFieldOffset propNames(propNamesOffset, StubField::Type::JSObject);
8955 AutoOutputRegister output(*this);
8956 Register id = allocator.useRegister(masm, idId);
8957 AutoScratchRegisterMaybeOutput propNamesReg(allocator, masm, output);
8958 AutoScratchRegister endScratch(allocator, masm);
8959 AutoScratchRegister scratch(allocator, masm);
8960
8961 FailurePath* failure;
8962 if (!addFailurePath(&failure)) {
8963 return false;
8964 }
8965
8966 emitLoadStubField(propNames, propNamesReg);
8967
8968 Label trueResult, falseResult, loop, done;
8969
8970 masm.loadPtr(Address(propNamesReg, NativeObject::offsetOfElements()),
8971 propNamesReg);
8972 // Compute end pointer.
8973 Address lengthAddr(propNamesReg, ObjectElements::offsetOfInitializedLength());
8974 masm.load32(lengthAddr, endScratch);
8975 masm.branch32(Assembler::Equal, endScratch, Imm32(0), &falseResult);
8976 BaseObjectElementIndex endPtrAddr(propNamesReg, endScratch);
8977 masm.computeEffectiveAddress(endPtrAddr, endScratch);
8978
8979 masm.bind(&loop);
8980
8981 Address atomAddr(propNamesReg.get(), 0);
8982
8983 masm.unboxString(atomAddr, scratch);
8984 masm.branchPtr(Assembler::Equal, scratch, id, &trueResult);
8985
8986 masm.addPtr(Imm32(sizeof(Value)), propNamesReg);
8987 masm.branchPtr(Assembler::Below, propNamesReg, endScratch, &loop);
8988
8989 masm.bind(&falseResult);
8990 if (output.hasValue()) {
8991 masm.moveValue(BooleanValue(false), output.valueReg());
8992 } else {
8993 masm.move32(Imm32(0), output.typedReg().gpr());
8994 }
8995 masm.jump(&done);
8996 masm.bind(&trueResult);
8997 if (output.hasValue()) {
8998 masm.moveValue(BooleanValue(true), output.valueReg());
8999 } else {
9000 masm.move32(Imm32(1), output.typedReg().gpr());
9001 }
9002 masm.bind(&done);
9003 return true;
9004 }
9005
9006 bool CacheIRCompiler::emitCallObjectHasSparseElementResult(
9007 ObjOperandId objId, Int32OperandId indexId) {
9008 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9009 AutoOutputRegister output(*this);
9010
9011 Register obj = allocator.useRegister(masm, objId);
9012 Register index = allocator.useRegister(masm, indexId);
9013
9014 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
9015 AutoScratchRegister scratch2(allocator, masm);
9016
9017 FailurePath* failure;
9018 if (!addFailurePath(&failure)) {
9019 return false;
9020 }
9021
9022 masm.reserveStack(sizeof(Value));
9023 masm.moveStackPtrTo(scratch2.get());
9024
9025 LiveRegisterSet volatileRegs = liveVolatileRegs();
9026 volatileRegs.takeUnchecked(scratch1);
9027 masm.PushRegsInMask(volatileRegs);
9028
9029 using Fn =
9030 bool (*)(JSContext* cx, NativeObject* obj, int32_t index, Value* vp);
9031 masm.setupUnalignedABICall(scratch1);
9032 masm.loadJSContext(scratch1);
9033 masm.passABIArg(scratch1);
9034 masm.passABIArg(obj);
9035 masm.passABIArg(index);
9036 masm.passABIArg(scratch2);
9037 masm.callWithABI<Fn, HasNativeElementPure>();
9038 masm.storeCallPointerResult(scratch1);
9039 masm.PopRegsInMask(volatileRegs);
9040
9041 Label ok;
9042 uint32_t framePushed = masm.framePushed();
9043 masm.branchIfTrueBool(scratch1, &ok);
9044 masm.adjustStack(sizeof(Value));
9045 masm.jump(failure->label());
9046
9047 masm.bind(&ok);
9048 masm.setFramePushed(framePushed);
9049 masm.loadTypedOrValue(Address(masm.getStackPointer(), 0), output);
9050 masm.adjustStack(sizeof(Value));
9051 return true;
9052 }
9053
9054 /*
9055 * Move a constant value into register dest.
9056 */
9057 void CacheIRCompiler::emitLoadStubFieldConstant(StubFieldOffset val,
9058 Register dest) {
9059 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9060 MOZ_ASSERT(mode_ == Mode::Ion);
9061 switch (val.getStubFieldType()) {
9062 case StubField::Type::Shape:
9063 masm.movePtr(ImmGCPtr(shapeStubField(val.getOffset())), dest);
9064 break;
9065 case StubField::Type::WeakGetterSetter:
9066 masm.movePtr(ImmGCPtr(weakGetterSetterStubField(val.getOffset())), dest);
9067 break;
9068 case StubField::Type::String:
9069 masm.movePtr(ImmGCPtr(stringStubField(val.getOffset())), dest);
9070 break;
9071 case StubField::Type::JSObject:
9072 masm.movePtr(ImmGCPtr(objectStubField(val.getOffset())), dest);
9073 break;
9074 case StubField::Type::RawPointer:
9075 masm.movePtr(ImmPtr(pointerStubField(val.getOffset())), dest);
9076 break;
9077 case StubField::Type::RawInt32:
9078 masm.move32(Imm32(int32StubField(val.getOffset())), dest);
9079 break;
9080 case StubField::Type::Id:
9081 masm.movePropertyKey(idStubField(val.getOffset()), dest);
9082 break;
9083 default:
9084 MOZ_CRASH("Unhandled stub field constant type");
9085 }
9086 }
9087
9088 /*
9089 * After this is done executing, dest contains the value; either through a
9090 * constant load or through the load from the stub data.
9091 *
9092 * The current policy is that Baseline will use loads from the stub data (to
9093 * allow IC sharing), where as Ion doesn't share ICs, and so we can safely use
9094 * constants in the IC.
9095 */
9096 void CacheIRCompiler::emitLoadStubField(StubFieldOffset val, Register dest) {
9097 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9098 if (stubFieldPolicy_ == StubFieldPolicy::Constant) {
9099 emitLoadStubFieldConstant(val, dest);
9100 } else {
9101 Address load(ICStubReg, stubDataOffset_ + val.getOffset());
9102
9103 switch (val.getStubFieldType()) {
9104 case StubField::Type::RawPointer:
9105 case StubField::Type::Shape:
9106 case StubField::Type::WeakGetterSetter:
9107 case StubField::Type::JSObject:
9108 case StubField::Type::Symbol:
9109 case StubField::Type::String:
9110 case StubField::Type::Id:
9111 case StubField::Type::AllocSite:
9112 masm.loadPtr(load, dest);
9113 break;
9114 case StubField::Type::RawInt32:
9115 masm.load32(load, dest);
9116 break;
9117 default:
9118 MOZ_CRASH("Unhandled stub field constant type");
9119 }
9120 }
9121 }
9122
9123 void CacheIRCompiler::emitLoadValueStubField(StubFieldOffset val,
9124 ValueOperand dest) {
9125 MOZ_ASSERT(val.getStubFieldType() == StubField::Type::Value);
9126
9127 if (stubFieldPolicy_ == StubFieldPolicy::Constant) {
9128 MOZ_ASSERT(mode_ == Mode::Ion);
9129 masm.moveValue(valueStubField(val.getOffset()), dest);
9130 } else {
9131 Address addr(ICStubReg, stubDataOffset_ + val.getOffset());
9132 masm.loadValue(addr, dest);
9133 }
9134 }
9135
9136 void CacheIRCompiler::emitLoadDoubleValueStubField(StubFieldOffset val,
9137 ValueOperand dest,
9138 FloatRegister scratch) {
9139 MOZ_ASSERT(val.getStubFieldType() == StubField::Type::Double);
9140
9141 if (stubFieldPolicy_ == StubFieldPolicy::Constant) {
9142 MOZ_ASSERT(mode_ == Mode::Ion);
9143 double d = doubleStubField(val.getOffset());
9144 masm.moveValue(DoubleValue(d), dest);
9145 } else {
9146 Address addr(ICStubReg, stubDataOffset_ + val.getOffset());
9147 masm.loadDouble(addr, scratch);
9148 masm.boxDouble(scratch, dest, scratch);
9149 }
9150 }
9151
9152 bool CacheIRCompiler::emitLoadInstanceOfObjectResult(ValOperandId lhsId,
9153 ObjOperandId protoId) {
9154 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9155 AutoOutputRegister output(*this);
9156 ValueOperand lhs = allocator.useValueRegister(masm, lhsId);
9157 Register proto = allocator.useRegister(masm, protoId);
9158
9159 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
9160
9161 FailurePath* failure;
9162 if (!addFailurePath(&failure)) {
9163 return false;
9164 }
9165
9166 Label returnFalse, returnTrue, done;
9167 masm.fallibleUnboxObject(lhs, scratch, &returnFalse);
9168
9169 // LHS is an object. Load its proto.
9170 masm.loadObjProto(scratch, scratch);
9171 {
9172 // Walk the proto chain until we either reach the target object,
9173 // nullptr or LazyProto.
9174 Label loop;
9175 masm.bind(&loop);
9176
9177 masm.branchPtr(Assembler::Equal, scratch, proto, &returnTrue);
9178 masm.branchTestPtr(Assembler::Zero, scratch, scratch, &returnFalse);
9179
9180 MOZ_ASSERT(uintptr_t(TaggedProto::LazyProto) == 1);
9181 masm.branchPtr(Assembler::Equal, scratch, ImmWord(1), failure->label());
9182
9183 masm.loadObjProto(scratch, scratch);
9184 masm.jump(&loop);
9185 }
9186
9187 masm.bind(&returnFalse);
9188 EmitStoreBoolean(masm, false, output);
9189 masm.jump(&done);
9190
9191 masm.bind(&returnTrue);
9192 EmitStoreBoolean(masm, true, output);
9193 // fallthrough
9194 masm.bind(&done);
9195 return true;
9196 }
9197
9198 bool CacheIRCompiler::emitMegamorphicLoadSlotResult(ObjOperandId objId,
9199 uint32_t idOffset) {
9200 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9201 AutoOutputRegister output(*this);
9202
9203 Register obj = allocator.useRegister(masm, objId);
9204 StubFieldOffset id(idOffset, StubField::Type::Id);
9205
9206 AutoScratchRegisterMaybeOutput idReg(allocator, masm, output);
9207 AutoScratchRegister scratch1(allocator, masm);
9208 AutoScratchRegister scratch2(allocator, masm);
9209 AutoScratchRegisterMaybeOutputType scratch3(allocator, masm, output);
9210
9211 #ifdef JS_CODEGEN_X86
9212 masm.xorPtr(scratch3, scratch3);
9213 #else
9214 Label cacheHit;
9215 emitLoadStubField(id, idReg);
9216 masm.emitMegamorphicCacheLookupByValue(idReg.get(), obj, scratch1, scratch2,
9217 scratch3, output.valueReg(),
9218 &cacheHit);
9219 #endif
9220
9221 FailurePath* failure;
9222 if (!addFailurePath(&failure)) {
9223 return false;
9224 }
9225
9226 masm.branchIfNonNativeObj(obj, scratch1, failure->label());
9227
9228 masm.Push(UndefinedValue());
9229 masm.moveStackPtrTo(idReg.get());
9230
9231 LiveRegisterSet volatileRegs = liveVolatileRegs();
9232 volatileRegs.takeUnchecked(scratch1);
9233 volatileRegs.takeUnchecked(scratch2);
9234 volatileRegs.takeUnchecked(scratch3);
9235 volatileRegs.takeUnchecked(idReg);
9236 masm.PushRegsInMask(volatileRegs);
9237
9238 using Fn = bool (*)(JSContext* cx, JSObject* obj, PropertyKey id,
9239 MegamorphicCache::Entry* cacheEntry, Value* vp);
9240 masm.setupUnalignedABICall(scratch1);
9241 masm.loadJSContext(scratch1);
9242 masm.passABIArg(scratch1);
9243 masm.passABIArg(obj);
9244 emitLoadStubField(id, scratch2);
9245 masm.passABIArg(scratch2);
9246 masm.passABIArg(scratch3);
9247 masm.passABIArg(idReg);
9248
9249 #ifdef JS_CODEGEN_X86
9250 masm.callWithABI<Fn, GetNativeDataPropertyPureWithCacheLookup>();
9251 #else
9252 masm.callWithABI<Fn, GetNativeDataPropertyPure>();
9253 #endif
9254
9255 masm.storeCallPointerResult(scratch2);
9256 masm.PopRegsInMask(volatileRegs);
9257
9258 masm.loadTypedOrValue(Address(masm.getStackPointer(), 0), output);
9259 masm.adjustStack(sizeof(Value));
9260
9261 masm.branchIfFalseBool(scratch2, failure->label());
9262 #ifndef JS_CODEGEN_X86
9263 masm.bind(&cacheHit);
9264 #endif
9265
9266 return true;
9267 }
9268
9269 bool CacheIRCompiler::emitMegamorphicLoadSlotPermissiveResult(
9270 ObjOperandId objId, uint32_t idOffset) {
9271 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9272 AutoCallVM callvm(masm, this, allocator);
9273
9274 const AutoOutputRegister& output = callvm.output();
9275
9276 Register obj = allocator.useRegister(masm, objId);
9277 StubFieldOffset id(idOffset, StubField::Type::Id);
9278
9279 AutoScratchRegisterMaybeOutput idReg(allocator, masm, output);
9280 AutoScratchRegister scratch1(allocator, masm);
9281 AutoScratchRegister scratch2(allocator, masm);
9282 AutoScratchRegisterMaybeOutputType scratch3(allocator, masm, output);
9283
9284 #ifdef JS_CODEGEN_X86
9285 masm.xorPtr(scratch3, scratch3);
9286 #else
9287 Label cacheHit;
9288 emitLoadStubField(id, idReg);
9289 masm.emitMegamorphicCacheLookupByValue(idReg.get(), obj, scratch1, scratch2,
9290 scratch3, output.valueReg(),
9291 &cacheHit);
9292 #endif
9293
9294 callvm.prepare();
9295
9296 emitLoadStubField(id, scratch2);
9297 masm.Push(scratch3);
9298 masm.Push(scratch2);
9299 masm.Push(obj);
9300
9301 using Fn = bool (*)(JSContext*, HandleObject, HandleId,
9302 MegamorphicCacheEntry*, MutableHandleValue);
9303 callvm.call<Fn, GetPropMaybeCached>();
9304
9305 #ifndef JS_CODEGEN_X86
9306 masm.bind(&cacheHit);
9307 #endif
9308
9309 return true;
9310 }
9311
9312 bool CacheIRCompiler::emitMegamorphicStoreSlot(ObjOperandId objId,
9313 uint32_t idOffset,
9314 ValOperandId rhsId,
9315 bool strict) {
9316 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9317
9318 AutoCallVM callvm(masm, this, allocator);
9319
9320 Register obj = allocator.useRegister(masm, objId);
9321 ConstantOrRegister val = allocator.useConstantOrRegister(masm, rhsId);
9322 StubFieldOffset id(idOffset, StubField::Type::Id);
9323 AutoScratchRegister scratch(allocator, masm);
9324
9325 callvm.prepare();
9326
9327 masm.Push(Imm32(strict));
9328 masm.Push(val);
9329 emitLoadStubField(id, scratch);
9330 masm.Push(scratch);
9331 masm.Push(obj);
9332
9333 using Fn = bool (*)(JSContext*, HandleObject, HandleId, HandleValue, bool);
9334 callvm.callNoResult<Fn, SetPropertyMegamorphic<false>>();
9335 return true;
9336 }
9337
9338 bool CacheIRCompiler::emitGuardHasGetterSetter(ObjOperandId objId,
9339 uint32_t idOffset,
9340 uint32_t getterSetterOffset) {
9341 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9342
9343 Register obj = allocator.useRegister(masm, objId);
9344
9345 StubFieldOffset id(idOffset, StubField::Type::Id);
9346 StubFieldOffset getterSetter(getterSetterOffset,
9347 StubField::Type::WeakGetterSetter);
9348
9349 AutoScratchRegister scratch1(allocator, masm);
9350 AutoScratchRegister scratch2(allocator, masm);
9351 AutoScratchRegister scratch3(allocator, masm);
9352
9353 FailurePath* failure;
9354 if (!addFailurePath(&failure)) {
9355 return false;
9356 }
9357
9358 LiveRegisterSet volatileRegs = liveVolatileRegs();
9359 volatileRegs.takeUnchecked(scratch1);
9360 volatileRegs.takeUnchecked(scratch2);
9361 masm.PushRegsInMask(volatileRegs);
9362
9363 using Fn = bool (*)(JSContext* cx, JSObject* obj, jsid id,
9364 GetterSetter* getterSetter);
9365 masm.setupUnalignedABICall(scratch1);
9366 masm.loadJSContext(scratch1);
9367 masm.passABIArg(scratch1);
9368 masm.passABIArg(obj);
9369 emitLoadStubField(id, scratch2);
9370 masm.passABIArg(scratch2);
9371 emitLoadStubField(getterSetter, scratch3);
9372 masm.passABIArg(scratch3);
9373 masm.callWithABI<Fn, ObjectHasGetterSetterPure>();
9374 masm.storeCallPointerResult(scratch1);
9375 masm.PopRegsInMask(volatileRegs);
9376
9377 masm.branchIfFalseBool(scratch1, failure->label());
9378 return true;
9379 }
9380
9381 bool CacheIRCompiler::emitGuardWasmArg(ValOperandId argId,
9382 wasm::ValType::Kind kind) {
9383 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9384
9385 // All values can be boxed as AnyRef.
9386 if (kind == wasm::ValType::Ref) {
9387 return true;
9388 }
9389 MOZ_ASSERT(kind != wasm::ValType::V128);
9390
9391 ValueOperand arg = allocator.useValueRegister(masm, argId);
9392
9393 FailurePath* failure;
9394 if (!addFailurePath(&failure)) {
9395 return false;
9396 }
9397
9398 // Check that the argument can be converted to the Wasm type in Warp code
9399 // without bailing out.
9400 Label done;
9401 switch (kind) {
9402 case wasm::ValType::I32:
9403 case wasm::ValType::F32:
9404 case wasm::ValType::F64: {
9405 // Argument must be number, bool, or undefined.
9406 masm.branchTestNumber(Assembler::Equal, arg, &done);
9407 masm.branchTestBoolean(Assembler::Equal, arg, &done);
9408 masm.branchTestUndefined(Assembler::NotEqual, arg, failure->label());
9409 break;
9410 }
9411 case wasm::ValType::I64: {
9412 // Argument must be bigint, bool, or string.
9413 masm.branchTestBigInt(Assembler::Equal, arg, &done);
9414 masm.branchTestBoolean(Assembler::Equal, arg, &done);
9415 masm.branchTestString(Assembler::NotEqual, arg, failure->label());
9416 break;
9417 }
9418 default:
9419 MOZ_CRASH("Unexpected kind");
9420 }
9421 masm.bind(&done);
9422
9423 return true;
9424 }
9425
9426 bool CacheIRCompiler::emitGuardMultipleShapes(ObjOperandId objId,
9427 uint32_t shapesOffset) {
9428 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9429 Register obj = allocator.useRegister(masm, objId);
9430 AutoScratchRegister shapes(allocator, masm);
9431 AutoScratchRegister scratch(allocator, masm);
9432 AutoScratchRegister scratch2(allocator, masm);
9433
9434 bool needSpectreMitigations = objectGuardNeedsSpectreMitigations(objId);
9435
9436 Register spectreScratch = InvalidReg;
9437 Maybe<AutoScratchRegister> maybeSpectreScratch;
9438 if (needSpectreMitigations) {
9439 maybeSpectreScratch.emplace(allocator, masm);
9440 spectreScratch = *maybeSpectreScratch;
9441 }
9442
9443 FailurePath* failure;
9444 if (!addFailurePath(&failure)) {
9445 return false;
9446 }
9447
9448 // The stub field contains a ListObject. Load its elements.
9449 StubFieldOffset shapeArray(shapesOffset, StubField::Type::JSObject);
9450 emitLoadStubField(shapeArray, shapes);
9451 masm.loadPtr(Address(shapes, NativeObject::offsetOfElements()), shapes);
9452
9453 masm.branchTestObjShapeList(Assembler::NotEqual, obj, shapes, scratch,
9454 scratch2, spectreScratch, failure->label());
9455 return true;
9456 }
9457
9458 bool CacheIRCompiler::emitLoadObject(ObjOperandId resultId,
9459 uint32_t objOffset) {
9460 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9461 Register reg = allocator.defineRegister(masm, resultId);
9462 StubFieldOffset obj(objOffset, StubField::Type::JSObject);
9463 emitLoadStubField(obj, reg);
9464 return true;
9465 }
9466
9467 bool CacheIRCompiler::emitLoadProtoObject(ObjOperandId resultId,
9468 uint32_t objOffset,
9469 ObjOperandId receiverObjId) {
9470 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9471 Register reg = allocator.defineRegister(masm, resultId);
9472 StubFieldOffset obj(objOffset, StubField::Type::JSObject);
9473 emitLoadStubField(obj, reg);
9474 return true;
9475 }
9476
9477 bool CacheIRCompiler::emitLoadInt32Constant(uint32_t valOffset,
9478 Int32OperandId resultId) {
9479 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9480 Register reg = allocator.defineRegister(masm, resultId);
9481 StubFieldOffset val(valOffset, StubField::Type::RawInt32);
9482 emitLoadStubField(val, reg);
9483 return true;
9484 }
9485
9486 bool CacheIRCompiler::emitLoadBooleanConstant(bool val,
9487 BooleanOperandId resultId) {
9488 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9489 Register reg = allocator.defineRegister(masm, resultId);
9490 masm.move32(Imm32(val), reg);
9491 return true;
9492 }
9493
9494 bool CacheIRCompiler::emitLoadDoubleConstant(uint32_t valOffset,
9495 NumberOperandId resultId) {
9496 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9497
9498 ValueOperand output = allocator.defineValueRegister(masm, resultId);
9499 StubFieldOffset val(valOffset, StubField::Type::Double);
9500
9501 AutoScratchFloatRegister floatReg(this);
9502
9503 emitLoadDoubleValueStubField(val, output, floatReg);
9504 return true;
9505 }
9506
9507 bool CacheIRCompiler::emitLoadUndefined(ValOperandId resultId) {
9508 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9509
9510 ValueOperand reg = allocator.defineValueRegister(masm, resultId);
9511 masm.moveValue(UndefinedValue(), reg);
9512 return true;
9513 }
9514
9515 bool CacheIRCompiler::emitLoadConstantString(uint32_t strOffset,
9516 StringOperandId resultId) {
9517 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9518 Register reg = allocator.defineRegister(masm, resultId);
9519 StubFieldOffset str(strOffset, StubField::Type::String);
9520 emitLoadStubField(str, reg);
9521 return true;
9522 }
9523
9524 bool CacheIRCompiler::emitCallInt32ToString(Int32OperandId inputId,
9525 StringOperandId resultId) {
9526 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9527 Register input = allocator.useRegister(masm, inputId);
9528 Register result = allocator.defineRegister(masm, resultId);
9529
9530 FailurePath* failure;
9531 if (!addFailurePath(&failure)) {
9532 return false;
9533 }
9534
9535 Label done, callVM;
9536
9537 {
9538 masm.lookupStaticIntString(input, result, cx_->staticStrings(), &callVM);
9539 masm.jump(&done);
9540 }
9541
9542 {
9543 masm.bind(&callVM);
9544 LiveRegisterSet volatileRegs = liveVolatileRegs();
9545 volatileRegs.takeUnchecked(result);
9546 masm.PushRegsInMask(volatileRegs);
9547
9548 using Fn = JSLinearString* (*)(JSContext* cx, int32_t i);
9549 masm.setupUnalignedABICall(result);
9550 masm.loadJSContext(result);
9551 masm.passABIArg(result);
9552 masm.passABIArg(input);
9553 masm.callWithABI<Fn, js::Int32ToStringPure>();
9554
9555 masm.storeCallPointerResult(result);
9556 masm.PopRegsInMask(volatileRegs);
9557
9558 masm.branchPtr(Assembler::Equal, result, ImmPtr(nullptr), failure->label());
9559 }
9560
9561 masm.bind(&done);
9562 return true;
9563 }
9564
9565 bool CacheIRCompiler::emitCallNumberToString(NumberOperandId inputId,
9566 StringOperandId resultId) {
9567 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9568
9569 AutoAvailableFloatRegister floatScratch0(*this, FloatReg0);
9570
9571 allocator.ensureDoubleRegister(masm, inputId, floatScratch0);
9572 Register result = allocator.defineRegister(masm, resultId);
9573
9574 FailurePath* failure;
9575 if (!addFailurePath(&failure)) {
9576 return false;
9577 }
9578
9579 LiveRegisterSet volatileRegs = liveVolatileRegs();
9580 volatileRegs.takeUnchecked(result);
9581 masm.PushRegsInMask(volatileRegs);
9582
9583 using Fn = JSString* (*)(JSContext* cx, double d);
9584 masm.setupUnalignedABICall(result);
9585 masm.loadJSContext(result);
9586 masm.passABIArg(result);
9587 masm.passABIArg(floatScratch0, ABIType::Float64);
9588 masm.callWithABI<Fn, js::NumberToStringPure>();
9589
9590 masm.storeCallPointerResult(result);
9591 masm.PopRegsInMask(volatileRegs);
9592
9593 masm.branchPtr(Assembler::Equal, result, ImmPtr(nullptr), failure->label());
9594 return true;
9595 }
9596
9597 bool CacheIRCompiler::emitInt32ToStringWithBaseResult(Int32OperandId inputId,
9598 Int32OperandId baseId) {
9599 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9600
9601 AutoCallVM callvm(masm, this, allocator);
9602 Register input = allocator.useRegister(masm, inputId);
9603 Register base = allocator.useRegister(masm, baseId);
9604
9605 FailurePath* failure;
9606 if (!addFailurePath(&failure)) {
9607 return false;
9608 }
9609
9610 // AutoCallVM's AutoSaveLiveRegisters aren't accounted for in FailurePath, so
9611 // we can't use both at the same time. This isn't an issue here, because Ion
9612 // doesn't support CallICs. If that ever changes, this code must be updated.
9613 MOZ_ASSERT(isBaseline(), "Can't use FailurePath with AutoCallVM in Ion ICs");
9614
9615 masm.branch32(Assembler::LessThan, base, Imm32(2), failure->label());
9616 masm.branch32(Assembler::GreaterThan, base, Imm32(36), failure->label());
9617
9618 // Use lower-case characters by default.
9619 constexpr bool lowerCase = true;
9620
9621 callvm.prepare();
9622
9623 masm.Push(Imm32(lowerCase));
9624 masm.Push(base);
9625 masm.Push(input);
9626
9627 using Fn = JSLinearString* (*)(JSContext*, int32_t, int32_t, bool);
9628 callvm.call<Fn, js::Int32ToStringWithBase<CanGC>>();
9629 return true;
9630 }
9631
9632 bool CacheIRCompiler::emitBooleanToString(BooleanOperandId inputId,
9633 StringOperandId resultId) {
9634 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9635 Register boolean = allocator.useRegister(masm, inputId);
9636 Register result = allocator.defineRegister(masm, resultId);
9637 const JSAtomState& names = cx_->names();
9638 Label true_, done;
9639
9640 masm.branchTest32(Assembler::NonZero, boolean, boolean, &true_);
9641
9642 // False case
9643 masm.movePtr(ImmGCPtr(names.false_), result);
9644 masm.jump(&done);
9645
9646 // True case
9647 masm.bind(&true_);
9648 masm.movePtr(ImmGCPtr(names.true_), result);
9649 masm.bind(&done);
9650
9651 return true;
9652 }
9653
9654 bool CacheIRCompiler::emitObjectToStringResult(ObjOperandId objId) {
9655 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9656
9657 AutoOutputRegister output(*this);
9658 Register obj = allocator.useRegister(masm, objId);
9659 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
9660
9661 FailurePath* failure;
9662 if (!addFailurePath(&failure)) {
9663 return false;
9664 }
9665
9666 LiveRegisterSet volatileRegs = liveVolatileRegs();
9667 volatileRegs.takeUnchecked(output.valueReg());
9668 volatileRegs.takeUnchecked(scratch);
9669 masm.PushRegsInMask(volatileRegs);
9670
9671 using Fn = JSString* (*)(JSContext*, JSObject*);
9672 masm.setupUnalignedABICall(scratch);
9673 masm.loadJSContext(scratch);
9674 masm.passABIArg(scratch);
9675 masm.passABIArg(obj);
9676 masm.callWithABI<Fn, js::ObjectClassToString>();
9677 masm.storeCallPointerResult(scratch);
9678
9679 masm.PopRegsInMask(volatileRegs);
9680
9681 masm.branchPtr(Assembler::Equal, scratch, ImmPtr(nullptr), failure->label());
9682 masm.tagValue(JSVAL_TYPE_STRING, scratch, output.valueReg());
9683
9684 return true;
9685 }
9686
9687 bool CacheIRCompiler::emitConcatStringsResult(StringOperandId lhsId,
9688 StringOperandId rhsId,
9689 uint32_t stubOffset) {
9690 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9691
9692 AutoCallVM callvm(masm, this, allocator);
9693 const AutoOutputRegister& output = callvm.output();
9694
9695 Register lhs = allocator.useRegister(masm, lhsId);
9696 Register rhs = allocator.useRegister(masm, rhsId);
9697
9698 // Discard the stack to ensure it's balanced when we skip the vm-call.
9699 allocator.discardStack(masm);
9700
9701 Label done;
9702 {
9703 // The StringConcat stub uses CallTemp registers 0 to 5.
9704 LiveGeneralRegisterSet liveRegs;
9705 liveRegs.add(CallTempReg0);
9706 liveRegs.add(CallTempReg1);
9707 liveRegs.add(CallTempReg2);
9708 liveRegs.add(CallTempReg3);
9709 liveRegs.add(CallTempReg4);
9710 liveRegs.add(CallTempReg5);
9711 #ifdef JS_USE_LINK_REGISTER
9712 liveRegs.add(ICTailCallReg);
9713 #endif
9714 liveRegs.takeUnchecked(output.valueReg());
9715 masm.PushRegsInMask(liveRegs);
9716
9717 // The stub expects lhs in CallTempReg0 and rhs in CallTempReg1.
9718 masm.moveRegPair(lhs, rhs, CallTempReg0, CallTempReg1);
9719
9720 uint32_t framePushed = masm.framePushed();
9721
9722 // Call cx->zone()->jitZone()->stringConcatStub. See also the comment and
9723 // code in CallRegExpStub.
9724 Label vmCall;
9725 Register temp = CallTempReg2;
9726 masm.loadJSContext(temp);
9727 masm.loadPtr(Address(temp, JSContext::offsetOfZone()), temp);
9728 masm.loadPtr(Address(temp, Zone::offsetOfJitZone()), temp);
9729 masm.loadPtr(Address(temp, JitZone::offsetOfStringConcatStub()), temp);
9730 masm.branchTestPtr(Assembler::Zero, temp, temp, &vmCall);
9731 masm.call(Address(temp, JitCode::offsetOfCode()));
9732
9733 // The result is returned in CallTempReg5 (nullptr on failure).
9734 masm.branchTestPtr(Assembler::Zero, CallTempReg5, CallTempReg5, &vmCall);
9735 masm.tagValue(JSVAL_TYPE_STRING, CallTempReg5, output.valueReg());
9736 masm.PopRegsInMask(liveRegs);
9737 masm.jump(&done);
9738
9739 masm.bind(&vmCall);
9740 masm.setFramePushed(framePushed);
9741 masm.PopRegsInMask(liveRegs);
9742 }
9743
9744 {
9745 callvm.prepare();
9746
9747 masm.Push(static_cast<js::jit::Imm32>(int32_t(js::gc::Heap::Default)));
9748 masm.Push(rhs);
9749 masm.Push(lhs);
9750
9751 using Fn =
9752 JSString* (*)(JSContext*, HandleString, HandleString, js::gc::Heap);
9753 callvm.call<Fn, ConcatStrings<CanGC>>();
9754 }
9755
9756 masm.bind(&done);
9757 return true;
9758 }
9759
9760 bool CacheIRCompiler::emitCallIsSuspendedGeneratorResult(ValOperandId valId) {
9761 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9762 AutoOutputRegister output(*this);
9763 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
9764 AutoScratchRegister scratch2(allocator, masm);
9765 ValueOperand input = allocator.useValueRegister(masm, valId);
9766
9767 // Test if it's an object.
9768 Label returnFalse, done;
9769 masm.fallibleUnboxObject(input, scratch, &returnFalse);
9770
9771 // Test if it's a GeneratorObject.
9772 masm.branchTestObjClass(Assembler::NotEqual, scratch,
9773 &GeneratorObject::class_, scratch2, scratch,
9774 &returnFalse);
9775
9776 // If the resumeIndex slot holds an int32 value < RESUME_INDEX_RUNNING,
9777 // the generator is suspended.
9778 Address addr(scratch, AbstractGeneratorObject::offsetOfResumeIndexSlot());
9779 masm.fallibleUnboxInt32(addr, scratch, &returnFalse);
9780 masm.branch32(Assembler::AboveOrEqual, scratch,
9781 Imm32(AbstractGeneratorObject::RESUME_INDEX_RUNNING),
9782 &returnFalse);
9783
9784 masm.moveValue(BooleanValue(true), output.valueReg());
9785 masm.jump(&done);
9786
9787 masm.bind(&returnFalse);
9788 masm.moveValue(BooleanValue(false), output.valueReg());
9789
9790 masm.bind(&done);
9791 return true;
9792 }
9793
9794 // This op generates no code. It is consumed by the transpiler.
9795 bool CacheIRCompiler::emitMetaScriptedThisShape(uint32_t) { return true; }
9796
9797 bool CacheIRCompiler::emitCallNativeGetElementResult(ObjOperandId objId,
9798 Int32OperandId indexId) {
9799 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9800 AutoCallVM callvm(masm, this, allocator);
9801
9802 Register obj = allocator.useRegister(masm, objId);
9803 Register index = allocator.useRegister(masm, indexId);
9804
9805 callvm.prepare();
9806
9807 masm.Push(index);
9808 masm.Push(TypedOrValueRegister(MIRType::Object, AnyRegister(obj)));
9809 masm.Push(obj);
9810
9811 using Fn = bool (*)(JSContext*, Handle<NativeObject*>, HandleValue, int32_t,
9812 MutableHandleValue);
9813 callvm.call<Fn, NativeGetElement>();
9814
9815 return true;
9816 }
9817
9818 bool CacheIRCompiler::emitCallNativeGetElementSuperResult(
9819 ObjOperandId objId, Int32OperandId indexId, ValOperandId receiverId) {
9820 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9821 AutoCallVM callvm(masm, this, allocator);
9822
9823 Register obj = allocator.useRegister(masm, objId);
9824 Register index = allocator.useRegister(masm, indexId);
9825 ValueOperand receiver = allocator.useValueRegister(masm, receiverId);
9826
9827 callvm.prepare();
9828
9829 masm.Push(index);
9830 masm.Push(receiver);
9831 masm.Push(obj);
9832
9833 using Fn = bool (*)(JSContext*, Handle<NativeObject*>, HandleValue, int32_t,
9834 MutableHandleValue);
9835 callvm.call<Fn, NativeGetElement>();
9836
9837 return true;
9838 }
9839
9840 bool CacheIRCompiler::emitProxyHasPropResult(ObjOperandId objId,
9841 ValOperandId idId, bool hasOwn) {
9842 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9843 AutoCallVM callvm(masm, this, allocator);
9844
9845 Register obj = allocator.useRegister(masm, objId);
9846 ValueOperand idVal = allocator.useValueRegister(masm, idId);
9847
9848 callvm.prepare();
9849
9850 masm.Push(idVal);
9851 masm.Push(obj);
9852
9853 using Fn = bool (*)(JSContext*, HandleObject, HandleValue, bool*);
9854 if (hasOwn) {
9855 callvm.call<Fn, ProxyHasOwn>();
9856 } else {
9857 callvm.call<Fn, ProxyHas>();
9858 }
9859 return true;
9860 }
9861
9862 bool CacheIRCompiler::emitProxyGetByValueResult(ObjOperandId objId,
9863 ValOperandId idId) {
9864 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9865 AutoCallVM callvm(masm, this, allocator);
9866
9867 Register obj = allocator.useRegister(masm, objId);
9868 ValueOperand idVal = allocator.useValueRegister(masm, idId);
9869
9870 callvm.prepare();
9871 masm.Push(idVal);
9872 masm.Push(obj);
9873
9874 using Fn =
9875 bool (*)(JSContext*, HandleObject, HandleValue, MutableHandleValue);
9876 callvm.call<Fn, ProxyGetPropertyByValue>();
9877 return true;
9878 }
9879
9880 bool CacheIRCompiler::emitCallGetSparseElementResult(ObjOperandId objId,
9881 Int32OperandId indexId) {
9882 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9883
9884 AutoCallVM callvm(masm, this, allocator);
9885
9886 Register obj = allocator.useRegister(masm, objId);
9887 Register id = allocator.useRegister(masm, indexId);
9888
9889 callvm.prepare();
9890 masm.Push(id);
9891 masm.Push(obj);
9892
9893 using Fn = bool (*)(JSContext* cx, Handle<NativeObject*> obj, int32_t int_id,
9894 MutableHandleValue result);
9895 callvm.call<Fn, GetSparseElementHelper>();
9896 return true;
9897 }
9898
9899 bool CacheIRCompiler::emitRegExpSearcherLastLimitResult() {
9900 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9901
9902 AutoOutputRegister output(*this);
9903 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
9904 AutoScratchRegister scratch2(allocator, masm);
9905
9906 masm.loadAndClearRegExpSearcherLastLimit(scratch1, scratch2);
9907
9908 masm.tagValue(JSVAL_TYPE_INT32, scratch1, output.valueReg());
9909 return true;
9910 }
9911
9912 bool CacheIRCompiler::emitRegExpFlagResult(ObjOperandId regexpId,
9913 int32_t flagsMask) {
9914 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9915
9916 AutoOutputRegister output(*this);
9917 Register regexp = allocator.useRegister(masm, regexpId);
9918 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
9919
9920 Address flagsAddr(
9921 regexp, NativeObject::getFixedSlotOffset(RegExpObject::flagsSlot()));
9922 masm.unboxInt32(flagsAddr, scratch);
9923
9924 Label ifFalse, done;
9925 masm.branchTest32(Assembler::Zero, scratch, Imm32(flagsMask), &ifFalse);
9926 masm.moveValue(BooleanValue(true), output.valueReg());
9927 masm.jump(&done);
9928
9929 masm.bind(&ifFalse);
9930 masm.moveValue(BooleanValue(false), output.valueReg());
9931
9932 masm.bind(&done);
9933 return true;
9934 }
9935
9936 bool CacheIRCompiler::emitCallSubstringKernelResult(StringOperandId strId,
9937 Int32OperandId beginId,
9938 Int32OperandId lengthId) {
9939 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9940
9941 AutoCallVM callvm(masm, this, allocator);
9942
9943 Register str = allocator.useRegister(masm, strId);
9944 Register begin = allocator.useRegister(masm, beginId);
9945 Register length = allocator.useRegister(masm, lengthId);
9946
9947 callvm.prepare();
9948 masm.Push(length);
9949 masm.Push(begin);
9950 masm.Push(str);
9951
9952 using Fn = JSString* (*)(JSContext* cx, HandleString str, int32_t begin,
9953 int32_t len);
9954 callvm.call<Fn, SubstringKernel>();
9955 return true;
9956 }
9957
9958 bool CacheIRCompiler::emitStringReplaceStringResult(
9959 StringOperandId strId, StringOperandId patternId,
9960 StringOperandId replacementId) {
9961 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9962
9963 AutoCallVM callvm(masm, this, allocator);
9964
9965 Register str = allocator.useRegister(masm, strId);
9966 Register pattern = allocator.useRegister(masm, patternId);
9967 Register replacement = allocator.useRegister(masm, replacementId);
9968
9969 callvm.prepare();
9970 masm.Push(replacement);
9971 masm.Push(pattern);
9972 masm.Push(str);
9973
9974 using Fn =
9975 JSString* (*)(JSContext*, HandleString, HandleString, HandleString);
9976 callvm.call<Fn, jit::StringReplace>();
9977 return true;
9978 }
9979
9980 bool CacheIRCompiler::emitStringSplitStringResult(StringOperandId strId,
9981 StringOperandId separatorId) {
9982 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
9983
9984 AutoCallVM callvm(masm, this, allocator);
9985
9986 Register str = allocator.useRegister(masm, strId);
9987 Register separator = allocator.useRegister(masm, separatorId);
9988
9989 callvm.prepare();
9990 masm.Push(Imm32(INT32_MAX));
9991 masm.Push(separator);
9992 masm.Push(str);
9993
9994 using Fn = ArrayObject* (*)(JSContext*, HandleString, HandleString, uint32_t);
9995 callvm.call<Fn, js::StringSplitString>();
9996 return true;
9997 }
9998
9999 bool CacheIRCompiler::emitGetFirstDollarIndexResult(StringOperandId strId) {
10000 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10001
10002 AutoCallVM callvm(masm, this, allocator);
10003
10004 Register str = allocator.useRegister(masm, strId);
10005
10006 callvm.prepare();
10007 masm.Push(str);
10008
10009 using Fn = bool (*)(JSContext*, JSString*, int32_t*);
10010 callvm.call<Fn, GetFirstDollarIndexRaw>();
10011 return true;
10012 }
10013
10014 bool CacheIRCompiler::emitAtomicsCompareExchangeResult(
10015 ObjOperandId objId, IntPtrOperandId indexId, uint32_t expectedId,
10016 uint32_t replacementId, Scalar::Type elementType,
10017 ArrayBufferViewKind viewKind) {
10018 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10019
10020 Maybe<AutoOutputRegister> output;
10021 Maybe<AutoCallVM> callvm;
10022 if (!Scalar::isBigIntType(elementType)) {
10023 output.emplace(*this);
10024 } else {
10025 callvm.emplace(masm, this, allocator);
10026 }
10027 #ifdef JS_CODEGEN_X86
10028 // Use a scratch register to avoid running out of registers.
10029 Register obj = output ? output->valueReg().typeReg()
10030 : callvm->outputValueReg().typeReg();
10031 allocator.copyToScratchRegister(masm, objId, obj);
10032 #else
10033 Register obj = allocator.useRegister(masm, objId);
10034 #endif
10035 Register index = allocator.useRegister(masm, indexId);
10036 Register expected;
10037 Register replacement;
10038 if (!Scalar::isBigIntType(elementType)) {
10039 expected = allocator.useRegister(masm, Int32OperandId(expectedId));
10040 replacement = allocator.useRegister(masm, Int32OperandId(replacementId));
10041 } else {
10042 expected = allocator.useRegister(masm, BigIntOperandId(expectedId));
10043 replacement = allocator.useRegister(masm, BigIntOperandId(replacementId));
10044 }
10045
10046 Register scratch = output ? output->valueReg().scratchReg()
10047 : callvm->outputValueReg().scratchReg();
10048 MOZ_ASSERT(scratch != obj, "scratchReg must not be typeReg");
10049
10050 Maybe<AutoScratchRegister> scratch2;
10051 if (viewKind == ArrayBufferViewKind::Resizable) {
10052 #ifdef JS_CODEGEN_X86
10053 // Not enough spare registers on x86.
10054 #else
10055 scratch2.emplace(allocator, masm);
10056 #endif
10057 }
10058
10059 // Not enough registers on X86.
10060 constexpr auto spectreTemp = mozilla::Nothing{};
10061
10062 FailurePath* failure;
10063 if (!addFailurePath(&failure)) {
10064 return false;
10065 }
10066
10067 // AutoCallVM's AutoSaveLiveRegisters aren't accounted for in FailurePath, so
10068 // we can't use both at the same time. This isn't an issue here, because Ion
10069 // doesn't support CallICs. If that ever changes, this code must be updated.
10070 MOZ_ASSERT(isBaseline(), "Can't use FailurePath with AutoCallVM in Ion ICs");
10071
10072 // Bounds check.
10073 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch, scratch2,
10074 spectreTemp, failure->label());
10075
10076 // Atomic operations are highly platform-dependent, for example x86/x64 has
10077 // specific requirements on which registers are used; MIPS needs multiple
10078 // additional temporaries. Therefore we're using either an ABI or VM call here
10079 // instead of handling each platform separately.
10080
10081 if (Scalar::isBigIntType(elementType)) {
10082 callvm->prepare();
10083
10084 masm.Push(replacement);
10085 masm.Push(expected);
10086 masm.Push(index);
10087 masm.Push(obj);
10088
10089 using Fn = BigInt* (*)(JSContext*, TypedArrayObject*, size_t, const BigInt*,
10090 const BigInt*);
10091 callvm->call<Fn, jit::AtomicsCompareExchange64>();
10092 return true;
10093 }
10094
10095 {
10096 LiveRegisterSet volatileRegs = liveVolatileRegs();
10097 volatileRegs.takeUnchecked(output->valueReg());
10098 volatileRegs.takeUnchecked(scratch);
10099 masm.PushRegsInMask(volatileRegs);
10100
10101 masm.setupUnalignedABICall(scratch);
10102 masm.passABIArg(obj);
10103 masm.passABIArg(index);
10104 masm.passABIArg(expected);
10105 masm.passABIArg(replacement);
10106 masm.callWithABI(DynamicFunction<AtomicsCompareExchangeFn>(
10107 AtomicsCompareExchange(elementType)));
10108 masm.storeCallInt32Result(scratch);
10109
10110 masm.PopRegsInMask(volatileRegs);
10111 }
10112
10113 if (elementType != Scalar::Uint32) {
10114 masm.tagValue(JSVAL_TYPE_INT32, scratch, output->valueReg());
10115 } else {
10116 ScratchDoubleScope fpscratch(masm);
10117 masm.convertUInt32ToDouble(scratch, fpscratch);
10118 masm.boxDouble(fpscratch, output->valueReg(), fpscratch);
10119 }
10120
10121 return true;
10122 }
10123
10124 bool CacheIRCompiler::emitAtomicsReadModifyWriteResult(
10125 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10126 Scalar::Type elementType, ArrayBufferViewKind viewKind,
10127 AtomicsReadWriteModifyFn fn) {
10128 AutoOutputRegister output(*this);
10129 Register obj = allocator.useRegister(masm, objId);
10130 Register index = allocator.useRegister(masm, indexId);
10131 Register value = allocator.useRegister(masm, Int32OperandId(valueId));
10132 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
10133 Maybe<AutoScratchRegisterMaybeOutputType> scratch2;
10134 if (viewKind == ArrayBufferViewKind::Resizable) {
10135 scratch2.emplace(allocator, masm, output);
10136 }
10137
10138 // Not enough registers on X86.
10139 constexpr auto spectreTemp = mozilla::Nothing{};
10140
10141 FailurePath* failure;
10142 if (!addFailurePath(&failure)) {
10143 return false;
10144 }
10145
10146 // Bounds check.
10147 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch, scratch2,
10148 spectreTemp, failure->label());
10149
10150 // See comment in emitAtomicsCompareExchange for why we use an ABI call.
10151 {
10152 LiveRegisterSet volatileRegs = liveVolatileRegs();
10153 volatileRegs.takeUnchecked(output.valueReg());
10154 volatileRegs.takeUnchecked(scratch);
10155 masm.PushRegsInMask(volatileRegs);
10156
10157 masm.setupUnalignedABICall(scratch);
10158 masm.passABIArg(obj);
10159 masm.passABIArg(index);
10160 masm.passABIArg(value);
10161 masm.callWithABI(DynamicFunction<AtomicsReadWriteModifyFn>(fn));
10162 masm.storeCallInt32Result(scratch);
10163
10164 masm.PopRegsInMask(volatileRegs);
10165 }
10166
10167 if (elementType != Scalar::Uint32) {
10168 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
10169 } else {
10170 ScratchDoubleScope fpscratch(masm);
10171 masm.convertUInt32ToDouble(scratch, fpscratch);
10172 masm.boxDouble(fpscratch, output.valueReg(), fpscratch);
10173 }
10174
10175 return true;
10176 }
10177
10178 template <CacheIRCompiler::AtomicsReadWriteModify64Fn fn>
10179 bool CacheIRCompiler::emitAtomicsReadModifyWriteResult64(
10180 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10181 ArrayBufferViewKind viewKind) {
10182 AutoCallVM callvm(masm, this, allocator);
10183 Register obj = allocator.useRegister(masm, objId);
10184 Register index = allocator.useRegister(masm, indexId);
10185 Register value = allocator.useRegister(masm, BigIntOperandId(valueId));
10186 AutoScratchRegisterMaybeOutput scratch(allocator, masm, callvm.output());
10187 Maybe<AutoScratchRegisterMaybeOutputType> scratch2;
10188 if (viewKind == ArrayBufferViewKind::Resizable) {
10189 scratch2.emplace(allocator, masm, callvm.output());
10190 }
10191
10192 // Not enough registers on X86.
10193 constexpr auto spectreTemp = mozilla::Nothing{};
10194
10195 FailurePath* failure;
10196 if (!addFailurePath(&failure)) {
10197 return false;
10198 }
10199
10200 // AutoCallVM's AutoSaveLiveRegisters aren't accounted for in FailurePath, so
10201 // we can't use both at the same time. This isn't an issue here, because Ion
10202 // doesn't support CallICs. If that ever changes, this code must be updated.
10203 MOZ_ASSERT(isBaseline(), "Can't use FailurePath with AutoCallVM in Ion ICs");
10204
10205 // Bounds check.
10206 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch, scratch2,
10207 spectreTemp, failure->label());
10208
10209 // See comment in emitAtomicsCompareExchange for why we use a VM call.
10210
10211 callvm.prepare();
10212
10213 masm.Push(value);
10214 masm.Push(index);
10215 masm.Push(obj);
10216
10217 callvm.call<AtomicsReadWriteModify64Fn, fn>();
10218 return true;
10219 }
10220
10221 bool CacheIRCompiler::emitAtomicsExchangeResult(ObjOperandId objId,
10222 IntPtrOperandId indexId,
10223 uint32_t valueId,
10224 Scalar::Type elementType,
10225 ArrayBufferViewKind viewKind) {
10226 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10227
10228 if (Scalar::isBigIntType(elementType)) {
10229 return emitAtomicsReadModifyWriteResult64<jit::AtomicsExchange64>(
10230 objId, indexId, valueId, viewKind);
10231 }
10232 return emitAtomicsReadModifyWriteResult(objId, indexId, valueId, elementType,
10233 viewKind,
10234 AtomicsExchange(elementType));
10235 }
10236
10237 bool CacheIRCompiler::emitAtomicsAddResult(
10238 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10239 Scalar::Type elementType, bool forEffect, ArrayBufferViewKind viewKind) {
10240 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10241
10242 if (Scalar::isBigIntType(elementType)) {
10243 return emitAtomicsReadModifyWriteResult64<jit::AtomicsAdd64>(
10244 objId, indexId, valueId, viewKind);
10245 }
10246 return emitAtomicsReadModifyWriteResult(objId, indexId, valueId, elementType,
10247 viewKind, AtomicsAdd(elementType));
10248 }
10249
10250 bool CacheIRCompiler::emitAtomicsSubResult(
10251 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10252 Scalar::Type elementType, bool forEffect, ArrayBufferViewKind viewKind) {
10253 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10254
10255 if (Scalar::isBigIntType(elementType)) {
10256 return emitAtomicsReadModifyWriteResult64<jit::AtomicsSub64>(
10257 objId, indexId, valueId, viewKind);
10258 }
10259 return emitAtomicsReadModifyWriteResult(objId, indexId, valueId, elementType,
10260 viewKind, AtomicsSub(elementType));
10261 }
10262
10263 bool CacheIRCompiler::emitAtomicsAndResult(
10264 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10265 Scalar::Type elementType, bool forEffect, ArrayBufferViewKind viewKind) {
10266 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10267
10268 if (Scalar::isBigIntType(elementType)) {
10269 return emitAtomicsReadModifyWriteResult64<jit::AtomicsAnd64>(
10270 objId, indexId, valueId, viewKind);
10271 }
10272 return emitAtomicsReadModifyWriteResult(objId, indexId, valueId, elementType,
10273 viewKind, AtomicsAnd(elementType));
10274 }
10275
10276 bool CacheIRCompiler::emitAtomicsOrResult(
10277 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10278 Scalar::Type elementType, bool forEffect, ArrayBufferViewKind viewKind) {
10279 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10280
10281 if (Scalar::isBigIntType(elementType)) {
10282 return emitAtomicsReadModifyWriteResult64<jit::AtomicsOr64>(
10283 objId, indexId, valueId, viewKind);
10284 }
10285 return emitAtomicsReadModifyWriteResult(objId, indexId, valueId, elementType,
10286 viewKind, AtomicsOr(elementType));
10287 }
10288
10289 bool CacheIRCompiler::emitAtomicsXorResult(
10290 ObjOperandId objId, IntPtrOperandId indexId, uint32_t valueId,
10291 Scalar::Type elementType, bool forEffect, ArrayBufferViewKind viewKind) {
10292 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10293
10294 if (Scalar::isBigIntType(elementType)) {
10295 return emitAtomicsReadModifyWriteResult64<jit::AtomicsXor64>(
10296 objId, indexId, valueId, viewKind);
10297 }
10298 return emitAtomicsReadModifyWriteResult(objId, indexId, valueId, elementType,
10299 viewKind, AtomicsXor(elementType));
10300 }
10301
10302 bool CacheIRCompiler::emitAtomicsLoadResult(ObjOperandId objId,
10303 IntPtrOperandId indexId,
10304 Scalar::Type elementType,
10305 ArrayBufferViewKind viewKind) {
10306 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10307
10308 Maybe<AutoOutputRegister> output;
10309 Maybe<AutoCallVM> callvm;
10310 if (!Scalar::isBigIntType(elementType)) {
10311 output.emplace(*this);
10312 } else {
10313 callvm.emplace(masm, this, allocator);
10314 }
10315 Register obj = allocator.useRegister(masm, objId);
10316 Register index = allocator.useRegister(masm, indexId);
10317 AutoScratchRegisterMaybeOutput scratch(allocator, masm,
10318 output ? *output : callvm->output());
10319 Maybe<AutoSpectreBoundsScratchRegister> spectreTemp;
10320 Maybe<AutoScratchRegister> scratch2;
10321 if (viewKind == ArrayBufferViewKind::FixedLength) {
10322 spectreTemp.emplace(allocator, masm);
10323 } else {
10324 scratch2.emplace(allocator, masm);
10325 }
10326 AutoAvailableFloatRegister floatReg(*this, FloatReg0);
10327
10328 FailurePath* failure;
10329 if (!addFailurePath(&failure)) {
10330 return false;
10331 }
10332
10333 // AutoCallVM's AutoSaveLiveRegisters aren't accounted for in FailurePath, so
10334 // we can't use both at the same time. This isn't an issue here, because Ion
10335 // doesn't support CallICs. If that ever changes, this code must be updated.
10336 MOZ_ASSERT(isBaseline(), "Can't use FailurePath with AutoCallVM in Ion ICs");
10337
10338 // Bounds check.
10339 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch, scratch2,
10340 spectreTemp, failure->label());
10341
10342 // Atomic operations are highly platform-dependent, for example x86/arm32 has
10343 // specific requirements on which registers are used. Therefore we're using a
10344 // VM call here instead of handling each platform separately.
10345 if (Scalar::isBigIntType(elementType)) {
10346 callvm->prepare();
10347
10348 masm.Push(index);
10349 masm.Push(obj);
10350
10351 using Fn = BigInt* (*)(JSContext*, TypedArrayObject*, size_t);
10352 callvm->call<Fn, jit::AtomicsLoad64>();
10353 return true;
10354 }
10355
10356 // Load the elements vector.
10357 masm.loadPtr(Address(obj, ArrayBufferViewObject::dataOffset()), scratch);
10358
10359 // Load the value.
10360 BaseIndex source(scratch, index, ScaleFromScalarType(elementType));
10361
10362 // NOTE: the generated code must match the assembly code in gen_load in
10363 // GenerateAtomicOperations.py
10364 auto sync = Synchronization::Load();
10365
10366 masm.memoryBarrierBefore(sync);
10367
10368 Label* failUint32 = nullptr;
10369 MacroAssembler::Uint32Mode mode = MacroAssembler::Uint32Mode::ForceDouble;
10370 masm.loadFromTypedArray(elementType, source, output->valueReg(), mode,
10371 InvalidReg, failUint32, LiveRegisterSet{});
10372 masm.memoryBarrierAfter(sync);
10373
10374 return true;
10375 }
10376
10377 bool CacheIRCompiler::emitAtomicsStoreResult(ObjOperandId objId,
10378 IntPtrOperandId indexId,
10379 uint32_t valueId,
10380 Scalar::Type elementType,
10381 ArrayBufferViewKind viewKind) {
10382 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10383
10384 AutoOutputRegister output(*this);
10385 Register obj = allocator.useRegister(masm, objId);
10386 Register index = allocator.useRegister(masm, indexId);
10387 Maybe<Register> valueInt32;
10388 Maybe<Register> valueBigInt;
10389 if (!Scalar::isBigIntType(elementType)) {
10390 valueInt32.emplace(allocator.useRegister(masm, Int32OperandId(valueId)));
10391 } else {
10392 valueBigInt.emplace(allocator.useRegister(masm, BigIntOperandId(valueId)));
10393 }
10394 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
10395 Maybe<AutoScratchRegisterMaybeOutputType> scratch2;
10396 if (viewKind == ArrayBufferViewKind::Resizable) {
10397 scratch2.emplace(allocator, masm, output);
10398 }
10399
10400 // Not enough registers on X86.
10401 constexpr auto spectreTemp = mozilla::Nothing{};
10402
10403 FailurePath* failure;
10404 if (!addFailurePath(&failure)) {
10405 return false;
10406 }
10407
10408 // Bounds check.
10409 emitTypedArrayBoundsCheck(viewKind, obj, index, scratch, scratch2,
10410 spectreTemp, failure->label());
10411
10412 if (!Scalar::isBigIntType(elementType)) {
10413 // Load the elements vector.
10414 masm.loadPtr(Address(obj, ArrayBufferViewObject::dataOffset()), scratch);
10415
10416 // Store the value.
10417 BaseIndex dest(scratch, index, ScaleFromScalarType(elementType));
10418
10419 // NOTE: the generated code must match the assembly code in gen_store in
10420 // GenerateAtomicOperations.py
10421 auto sync = Synchronization::Store();
10422
10423 masm.memoryBarrierBefore(sync);
10424 masm.storeToTypedIntArray(elementType, *valueInt32, dest);
10425 masm.memoryBarrierAfter(sync);
10426
10427 masm.tagValue(JSVAL_TYPE_INT32, *valueInt32, output.valueReg());
10428 } else {
10429 // See comment in emitAtomicsCompareExchange for why we use an ABI call.
10430
10431 LiveRegisterSet volatileRegs = liveVolatileRegs();
10432 volatileRegs.takeUnchecked(output.valueReg());
10433 volatileRegs.takeUnchecked(scratch);
10434 masm.PushRegsInMask(volatileRegs);
10435
10436 using Fn = void (*)(TypedArrayObject*, size_t, const BigInt*);
10437 masm.setupUnalignedABICall(scratch);
10438 masm.passABIArg(obj);
10439 masm.passABIArg(index);
10440 masm.passABIArg(*valueBigInt);
10441 masm.callWithABI<Fn, jit::AtomicsStore64>();
10442
10443 masm.PopRegsInMask(volatileRegs);
10444
10445 masm.tagValue(JSVAL_TYPE_BIGINT, *valueBigInt, output.valueReg());
10446 }
10447
10448 return true;
10449 }
10450
10451 bool CacheIRCompiler::emitAtomicsIsLockFreeResult(Int32OperandId valueId) {
10452 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10453
10454 AutoOutputRegister output(*this);
10455 Register value = allocator.useRegister(masm, valueId);
10456 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
10457
10458 masm.atomicIsLockFreeJS(value, scratch);
10459 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch, output.valueReg());
10460
10461 return true;
10462 }
10463
10464 bool CacheIRCompiler::emitAtomicsPauseResult() {
10465 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10466
10467 AutoOutputRegister output(*this);
10468
10469 masm.atomicPause();
10470 masm.moveValue(UndefinedValue(), output.valueReg());
10471
10472 return true;
10473 }
10474
10475 bool CacheIRCompiler::emitBigIntAsIntNResult(Int32OperandId bitsId,
10476 BigIntOperandId bigIntId) {
10477 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10478
10479 AutoCallVM callvm(masm, this, allocator);
10480
10481 Register bits = allocator.useRegister(masm, bitsId);
10482 Register bigInt = allocator.useRegister(masm, bigIntId);
10483
10484 callvm.prepare();
10485 masm.Push(bits);
10486 masm.Push(bigInt);
10487
10488 using Fn = BigInt* (*)(JSContext*, HandleBigInt, int32_t);
10489 callvm.call<Fn, jit::BigIntAsIntN>();
10490 return true;
10491 }
10492
10493 bool CacheIRCompiler::emitBigIntAsUintNResult(Int32OperandId bitsId,
10494 BigIntOperandId bigIntId) {
10495 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10496
10497 AutoCallVM callvm(masm, this, allocator);
10498
10499 Register bits = allocator.useRegister(masm, bitsId);
10500 Register bigInt = allocator.useRegister(masm, bigIntId);
10501
10502 callvm.prepare();
10503 masm.Push(bits);
10504 masm.Push(bigInt);
10505
10506 using Fn = BigInt* (*)(JSContext*, HandleBigInt, int32_t);
10507 callvm.call<Fn, jit::BigIntAsUintN>();
10508 return true;
10509 }
10510
10511 bool CacheIRCompiler::emitSetHasResult(ObjOperandId setId, ValOperandId valId) {
10512 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10513
10514 AutoCallVM callvm(masm, this, allocator);
10515
10516 Register set = allocator.useRegister(masm, setId);
10517 ValueOperand val = allocator.useValueRegister(masm, valId);
10518
10519 callvm.prepare();
10520 masm.Push(val);
10521 masm.Push(set);
10522
10523 using Fn = bool (*)(JSContext*, Handle<SetObject*>, HandleValue, bool*);
10524 callvm.call<Fn, jit::SetObjectHas>();
10525 return true;
10526 }
10527
10528 bool CacheIRCompiler::emitSetHasNonGCThingResult(ObjOperandId setId,
10529 ValOperandId valId) {
10530 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10531
10532 AutoOutputRegister output(*this);
10533 Register set = allocator.useRegister(masm, setId);
10534 ValueOperand val = allocator.useValueRegister(masm, valId);
10535
10536 AutoScratchRegister scratch1(allocator, masm);
10537 AutoScratchRegister scratch2(allocator, masm);
10538 AutoScratchRegister scratch3(allocator, masm);
10539 AutoScratchRegister scratch4(allocator, masm);
10540 AutoAvailableFloatRegister scratchFloat(*this, FloatReg0);
10541
10542 masm.toHashableNonGCThing(val, output.valueReg(), scratchFloat);
10543 masm.prepareHashNonGCThing(output.valueReg(), scratch1, scratch2);
10544
10545 masm.setObjectHasNonBigInt(set, output.valueReg(), scratch1, scratch2,
10546 scratch3, scratch4);
10547 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10548 return true;
10549 }
10550
10551 bool CacheIRCompiler::emitSetHasSymbolResult(ObjOperandId setId,
10552 SymbolOperandId symId) {
10553 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10554
10555 AutoOutputRegister output(*this);
10556 Register set = allocator.useRegister(masm, setId);
10557 Register sym = allocator.useRegister(masm, symId);
10558
10559 AutoScratchRegister scratch1(allocator, masm);
10560 AutoScratchRegister scratch2(allocator, masm);
10561 AutoScratchRegister scratch3(allocator, masm);
10562 AutoScratchRegister scratch4(allocator, masm);
10563
10564 masm.prepareHashSymbol(sym, scratch1);
10565
10566 masm.tagValue(JSVAL_TYPE_SYMBOL, sym, output.valueReg());
10567 masm.setObjectHasNonBigInt(set, output.valueReg(), scratch1, scratch2,
10568 scratch3, scratch4);
10569 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10570 return true;
10571 }
10572
10573 bool CacheIRCompiler::emitSetHasBigIntResult(ObjOperandId setId,
10574 BigIntOperandId bigIntId) {
10575 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10576
10577 AutoOutputRegister output(*this);
10578 Register set = allocator.useRegister(masm, setId);
10579 Register bigInt = allocator.useRegister(masm, bigIntId);
10580
10581 AutoScratchRegister scratch1(allocator, masm);
10582 AutoScratchRegister scratch2(allocator, masm);
10583 AutoScratchRegister scratch3(allocator, masm);
10584 AutoScratchRegister scratch4(allocator, masm);
10585 AutoScratchRegister scratch5(allocator, masm);
10586 #ifndef JS_CODEGEN_ARM
10587 AutoScratchRegister scratch6(allocator, masm);
10588 #else
10589 // We don't have more registers available on ARM32.
10590 Register scratch6 = set;
10591
10592 masm.push(set);
10593 #endif
10594
10595 masm.prepareHashBigInt(bigInt, scratch1, scratch2, scratch3, scratch4);
10596
10597 masm.tagValue(JSVAL_TYPE_BIGINT, bigInt, output.valueReg());
10598 masm.setObjectHasBigInt(set, output.valueReg(), scratch1, scratch2, scratch3,
10599 scratch4, scratch5, scratch6);
10600 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10601
10602 #ifdef JS_CODEGEN_ARM
10603 masm.pop(set);
10604 #endif
10605 return true;
10606 }
10607
10608 bool CacheIRCompiler::emitSetHasObjectResult(ObjOperandId setId,
10609 ObjOperandId objId) {
10610 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10611
10612 AutoOutputRegister output(*this);
10613 Register set = allocator.useRegister(masm, setId);
10614 Register obj = allocator.useRegister(masm, objId);
10615
10616 AutoScratchRegister scratch1(allocator, masm);
10617 AutoScratchRegister scratch2(allocator, masm);
10618 AutoScratchRegister scratch3(allocator, masm);
10619 AutoScratchRegister scratch4(allocator, masm);
10620 AutoScratchRegister scratch5(allocator, masm);
10621
10622 masm.tagValue(JSVAL_TYPE_OBJECT, obj, output.valueReg());
10623 masm.prepareHashObject(set, output.valueReg(), scratch1, scratch2, scratch3,
10624 scratch4, scratch5);
10625
10626 masm.setObjectHasNonBigInt(set, output.valueReg(), scratch1, scratch2,
10627 scratch3, scratch4);
10628 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10629 return true;
10630 }
10631
10632 bool CacheIRCompiler::emitSetDeleteResult(ObjOperandId setId,
10633 ValOperandId valId) {
10634 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10635
10636 AutoCallVM callvm(masm, this, allocator);
10637
10638 Register set = allocator.useRegister(masm, setId);
10639 ValueOperand val = allocator.useValueRegister(masm, valId);
10640
10641 callvm.prepare();
10642 masm.Push(val);
10643 masm.Push(set);
10644
10645 using Fn = bool (*)(JSContext*, Handle<SetObject*>, HandleValue, bool*);
10646 callvm.call<Fn, jit::SetObjectDelete>();
10647 return true;
10648 }
10649
10650 bool CacheIRCompiler::emitSetAddResult(ObjOperandId setId, ValOperandId keyId) {
10651 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10652
10653 AutoCallVM callvm(masm, this, allocator);
10654
10655 Register set = allocator.useRegister(masm, setId);
10656 ValueOperand key = allocator.useValueRegister(masm, keyId);
10657
10658 callvm.prepare();
10659 masm.Push(key);
10660 masm.Push(set);
10661
10662 using Fn =
10663 bool (*)(JSContext*, Handle<SetObject*>, HandleValue, MutableHandleValue);
10664 callvm.call<Fn, jit::SetObjectAddFromIC>();
10665 return true;
10666 }
10667
10668 bool CacheIRCompiler::emitSetSizeResult(ObjOperandId setId) {
10669 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10670
10671 AutoOutputRegister output(*this);
10672 Register set = allocator.useRegister(masm, setId);
10673 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
10674
10675 masm.loadSetObjectSize(set, scratch);
10676 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
10677 return true;
10678 }
10679
10680 bool CacheIRCompiler::emitMapHasResult(ObjOperandId mapId, ValOperandId valId) {
10681 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10682
10683 AutoCallVM callvm(masm, this, allocator);
10684
10685 Register map = allocator.useRegister(masm, mapId);
10686 ValueOperand val = allocator.useValueRegister(masm, valId);
10687
10688 callvm.prepare();
10689 masm.Push(val);
10690 masm.Push(map);
10691
10692 using Fn = bool (*)(JSContext*, Handle<MapObject*>, HandleValue, bool*);
10693 callvm.call<Fn, jit::MapObjectHas>();
10694 return true;
10695 }
10696
10697 bool CacheIRCompiler::emitMapHasNonGCThingResult(ObjOperandId mapId,
10698 ValOperandId valId) {
10699 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10700
10701 AutoOutputRegister output(*this);
10702 Register map = allocator.useRegister(masm, mapId);
10703 ValueOperand val = allocator.useValueRegister(masm, valId);
10704
10705 AutoScratchRegister scratch1(allocator, masm);
10706 AutoScratchRegister scratch2(allocator, masm);
10707 AutoScratchRegister scratch3(allocator, masm);
10708 AutoScratchRegister scratch4(allocator, masm);
10709 AutoAvailableFloatRegister scratchFloat(*this, FloatReg0);
10710
10711 masm.toHashableNonGCThing(val, output.valueReg(), scratchFloat);
10712 masm.prepareHashNonGCThing(output.valueReg(), scratch1, scratch2);
10713
10714 masm.mapObjectHasNonBigInt(map, output.valueReg(), scratch1, scratch2,
10715 scratch3, scratch4);
10716 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10717 return true;
10718 }
10719
10720 bool CacheIRCompiler::emitMapHasSymbolResult(ObjOperandId mapId,
10721 SymbolOperandId symId) {
10722 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10723
10724 AutoOutputRegister output(*this);
10725 Register map = allocator.useRegister(masm, mapId);
10726 Register sym = allocator.useRegister(masm, symId);
10727
10728 AutoScratchRegister scratch1(allocator, masm);
10729 AutoScratchRegister scratch2(allocator, masm);
10730 AutoScratchRegister scratch3(allocator, masm);
10731 AutoScratchRegister scratch4(allocator, masm);
10732
10733 masm.prepareHashSymbol(sym, scratch1);
10734
10735 masm.tagValue(JSVAL_TYPE_SYMBOL, sym, output.valueReg());
10736 masm.mapObjectHasNonBigInt(map, output.valueReg(), scratch1, scratch2,
10737 scratch3, scratch4);
10738 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10739 return true;
10740 }
10741
10742 bool CacheIRCompiler::emitMapHasBigIntResult(ObjOperandId mapId,
10743 BigIntOperandId bigIntId) {
10744 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10745
10746 AutoOutputRegister output(*this);
10747 Register map = allocator.useRegister(masm, mapId);
10748 Register bigInt = allocator.useRegister(masm, bigIntId);
10749
10750 AutoScratchRegister scratch1(allocator, masm);
10751 AutoScratchRegister scratch2(allocator, masm);
10752 AutoScratchRegister scratch3(allocator, masm);
10753 AutoScratchRegister scratch4(allocator, masm);
10754 AutoScratchRegister scratch5(allocator, masm);
10755 #ifndef JS_CODEGEN_ARM
10756 AutoScratchRegister scratch6(allocator, masm);
10757 #else
10758 // We don't have more registers available on ARM32.
10759 Register scratch6 = map;
10760
10761 masm.push(map);
10762 #endif
10763
10764 masm.prepareHashBigInt(bigInt, scratch1, scratch2, scratch3, scratch4);
10765
10766 masm.tagValue(JSVAL_TYPE_BIGINT, bigInt, output.valueReg());
10767 masm.mapObjectHasBigInt(map, output.valueReg(), scratch1, scratch2, scratch3,
10768 scratch4, scratch5, scratch6);
10769 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10770
10771 #ifdef JS_CODEGEN_ARM
10772 masm.pop(map);
10773 #endif
10774 return true;
10775 }
10776
10777 bool CacheIRCompiler::emitMapHasObjectResult(ObjOperandId mapId,
10778 ObjOperandId objId) {
10779 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10780
10781 AutoOutputRegister output(*this);
10782 Register map = allocator.useRegister(masm, mapId);
10783 Register obj = allocator.useRegister(masm, objId);
10784
10785 AutoScratchRegister scratch1(allocator, masm);
10786 AutoScratchRegister scratch2(allocator, masm);
10787 AutoScratchRegister scratch3(allocator, masm);
10788 AutoScratchRegister scratch4(allocator, masm);
10789 AutoScratchRegister scratch5(allocator, masm);
10790
10791 masm.tagValue(JSVAL_TYPE_OBJECT, obj, output.valueReg());
10792 masm.prepareHashObject(map, output.valueReg(), scratch1, scratch2, scratch3,
10793 scratch4, scratch5);
10794
10795 masm.mapObjectHasNonBigInt(map, output.valueReg(), scratch1, scratch2,
10796 scratch3, scratch4);
10797 masm.tagValue(JSVAL_TYPE_BOOLEAN, scratch2, output.valueReg());
10798 return true;
10799 }
10800
10801 bool CacheIRCompiler::emitMapGetResult(ObjOperandId mapId, ValOperandId valId) {
10802 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10803
10804 AutoCallVM callvm(masm, this, allocator);
10805
10806 Register map = allocator.useRegister(masm, mapId);
10807 ValueOperand val = allocator.useValueRegister(masm, valId);
10808
10809 callvm.prepare();
10810 masm.Push(val);
10811 masm.Push(map);
10812
10813 using Fn =
10814 bool (*)(JSContext*, Handle<MapObject*>, HandleValue, MutableHandleValue);
10815 callvm.call<Fn, jit::MapObjectGet>();
10816 return true;
10817 }
10818
10819 bool CacheIRCompiler::emitMapDeleteResult(ObjOperandId mapId,
10820 ValOperandId valId) {
10821 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10822
10823 AutoCallVM callvm(masm, this, allocator);
10824
10825 Register map = allocator.useRegister(masm, mapId);
10826 ValueOperand val = allocator.useValueRegister(masm, valId);
10827
10828 callvm.prepare();
10829 masm.Push(val);
10830 masm.Push(map);
10831
10832 using Fn = bool (*)(JSContext*, Handle<MapObject*>, HandleValue, bool*);
10833 callvm.call<Fn, jit::MapObjectDelete>();
10834 return true;
10835 }
10836
10837 bool CacheIRCompiler::emitMapSetResult(ObjOperandId mapId, ValOperandId keyId,
10838 ValOperandId valId) {
10839 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10840
10841 AutoCallVM callvm(masm, this, allocator);
10842
10843 Register map = allocator.useRegister(masm, mapId);
10844 ValueOperand key = allocator.useValueRegister(masm, keyId);
10845 ValueOperand val = allocator.useValueRegister(masm, valId);
10846
10847 callvm.prepare();
10848 masm.Push(val);
10849 masm.Push(key);
10850 masm.Push(map);
10851
10852 using Fn = bool (*)(JSContext*, Handle<MapObject*>, HandleValue, HandleValue,
10853 MutableHandleValue);
10854 callvm.call<Fn, jit::MapObjectSetFromIC>();
10855 return true;
10856 }
10857
10858 bool CacheIRCompiler::emitMapGetNonGCThingResult(ObjOperandId mapId,
10859 ValOperandId valId) {
10860 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10861
10862 AutoOutputRegister output(*this);
10863 Register map = allocator.useRegister(masm, mapId);
10864 ValueOperand val = allocator.useValueRegister(masm, valId);
10865
10866 AutoScratchRegister scratch1(allocator, masm);
10867 AutoScratchRegister scratch2(allocator, masm);
10868 AutoScratchRegister scratch3(allocator, masm);
10869 AutoScratchRegister scratch4(allocator, masm);
10870 AutoAvailableFloatRegister scratchFloat(*this, FloatReg0);
10871
10872 masm.toHashableNonGCThing(val, output.valueReg(), scratchFloat);
10873 masm.prepareHashNonGCThing(output.valueReg(), scratch1, scratch2);
10874
10875 masm.mapObjectGetNonBigInt(map, output.valueReg(), scratch1,
10876 output.valueReg(), scratch2, scratch3, scratch4);
10877 return true;
10878 }
10879
10880 bool CacheIRCompiler::emitMapGetSymbolResult(ObjOperandId mapId,
10881 SymbolOperandId symId) {
10882 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10883
10884 AutoOutputRegister output(*this);
10885 Register map = allocator.useRegister(masm, mapId);
10886 Register sym = allocator.useRegister(masm, symId);
10887
10888 AutoScratchRegister scratch1(allocator, masm);
10889 AutoScratchRegister scratch2(allocator, masm);
10890 AutoScratchRegister scratch3(allocator, masm);
10891 AutoScratchRegister scratch4(allocator, masm);
10892
10893 masm.prepareHashSymbol(sym, scratch1);
10894
10895 masm.tagValue(JSVAL_TYPE_SYMBOL, sym, output.valueReg());
10896 masm.mapObjectGetNonBigInt(map, output.valueReg(), scratch1,
10897 output.valueReg(), scratch2, scratch3, scratch4);
10898 return true;
10899 }
10900
10901 bool CacheIRCompiler::emitMapGetBigIntResult(ObjOperandId mapId,
10902 BigIntOperandId bigIntId) {
10903 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10904
10905 AutoOutputRegister output(*this);
10906 Register map = allocator.useRegister(masm, mapId);
10907 Register bigInt = allocator.useRegister(masm, bigIntId);
10908
10909 AutoScratchRegister scratch1(allocator, masm);
10910 AutoScratchRegister scratch2(allocator, masm);
10911 AutoScratchRegister scratch3(allocator, masm);
10912 AutoScratchRegister scratch4(allocator, masm);
10913 AutoScratchRegister scratch5(allocator, masm);
10914 #ifndef JS_CODEGEN_ARM
10915 AutoScratchRegister scratch6(allocator, masm);
10916 #else
10917 // We don't have more registers available on ARM32.
10918 Register scratch6 = map;
10919
10920 masm.push(map);
10921 #endif
10922
10923 masm.prepareHashBigInt(bigInt, scratch1, scratch2, scratch3, scratch4);
10924
10925 masm.tagValue(JSVAL_TYPE_BIGINT, bigInt, output.valueReg());
10926 masm.mapObjectGetBigInt(map, output.valueReg(), scratch1, output.valueReg(),
10927 scratch2, scratch3, scratch4, scratch5, scratch6);
10928
10929 #ifdef JS_CODEGEN_ARM
10930 masm.pop(map);
10931 #endif
10932 return true;
10933 }
10934
10935 bool CacheIRCompiler::emitMapGetObjectResult(ObjOperandId mapId,
10936 ObjOperandId objId) {
10937 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10938
10939 AutoOutputRegister output(*this);
10940 Register map = allocator.useRegister(masm, mapId);
10941 Register obj = allocator.useRegister(masm, objId);
10942
10943 AutoScratchRegister scratch1(allocator, masm);
10944 AutoScratchRegister scratch2(allocator, masm);
10945 AutoScratchRegister scratch3(allocator, masm);
10946 AutoScratchRegister scratch4(allocator, masm);
10947 AutoScratchRegister scratch5(allocator, masm);
10948
10949 masm.tagValue(JSVAL_TYPE_OBJECT, obj, output.valueReg());
10950 masm.prepareHashObject(map, output.valueReg(), scratch1, scratch2, scratch3,
10951 scratch4, scratch5);
10952
10953 masm.mapObjectGetNonBigInt(map, output.valueReg(), scratch1,
10954 output.valueReg(), scratch2, scratch3, scratch4);
10955 return true;
10956 }
10957
10958 bool CacheIRCompiler::emitMapSizeResult(ObjOperandId mapId) {
10959 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10960
10961 AutoOutputRegister output(*this);
10962 Register map = allocator.useRegister(masm, mapId);
10963 AutoScratchRegisterMaybeOutput scratch(allocator, masm, output);
10964
10965 masm.loadMapObjectSize(map, scratch);
10966 masm.tagValue(JSVAL_TYPE_INT32, scratch, output.valueReg());
10967 return true;
10968 }
10969
10970 bool CacheIRCompiler::emitDateFillLocalTimeSlots(ObjOperandId dateId) {
10971 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10972
10973 Register date = allocator.useRegister(masm, dateId);
10974 AutoScratchRegister scratch(allocator, masm);
10975
10976 masm.dateFillLocalTimeSlots(date, scratch, liveVolatileRegs());
10977 return true;
10978 }
10979
10980 bool CacheIRCompiler::emitDateHoursFromSecondsIntoYearResult(
10981 ValOperandId secondsIntoYearId) {
10982 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10983
10984 AutoOutputRegister output(*this);
10985 ValueOperand secondsIntoYear =
10986 allocator.useValueRegister(masm, secondsIntoYearId);
10987 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
10988 AutoScratchRegister scratch2(allocator, masm);
10989
10990 masm.dateHoursFromSecondsIntoYear(secondsIntoYear, output.valueReg(),
10991 scratch1, scratch2);
10992 return true;
10993 }
10994
10995 bool CacheIRCompiler::emitDateMinutesFromSecondsIntoYearResult(
10996 ValOperandId secondsIntoYearId) {
10997 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
10998
10999 AutoOutputRegister output(*this);
11000 ValueOperand secondsIntoYear =
11001 allocator.useValueRegister(masm, secondsIntoYearId);
11002 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
11003 AutoScratchRegister scratch2(allocator, masm);
11004
11005 masm.dateMinutesFromSecondsIntoYear(secondsIntoYear, output.valueReg(),
11006 scratch1, scratch2);
11007 return true;
11008 }
11009
11010 bool CacheIRCompiler::emitDateSecondsFromSecondsIntoYearResult(
11011 ValOperandId secondsIntoYearId) {
11012 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11013
11014 AutoOutputRegister output(*this);
11015 ValueOperand secondsIntoYear =
11016 allocator.useValueRegister(masm, secondsIntoYearId);
11017 AutoScratchRegisterMaybeOutput scratch1(allocator, masm, output);
11018 AutoScratchRegister scratch2(allocator, masm);
11019
11020 masm.dateSecondsFromSecondsIntoYear(secondsIntoYear, output.valueReg(),
11021 scratch1, scratch2);
11022 return true;
11023 }
11024
11025 bool CacheIRCompiler::emitArrayFromArgumentsObjectResult(ObjOperandId objId,
11026 uint32_t shapeOffset) {
11027 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11028
11029 AutoCallVM callvm(masm, this, allocator);
11030
11031 Register obj = allocator.useRegister(masm, objId);
11032
11033 callvm.prepare();
11034 masm.Push(obj);
11035
11036 using Fn = ArrayObject* (*)(JSContext*, Handle<ArgumentsObject*>);
11037 callvm.call<Fn, js::ArrayFromArgumentsObject>();
11038 return true;
11039 }
11040
11041 bool CacheIRCompiler::emitGuardGlobalGeneration(uint32_t expectedOffset,
11042 uint32_t generationAddrOffset) {
11043 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11044
11045 AutoScratchRegister scratch(allocator, masm);
11046 AutoScratchRegister scratch2(allocator, masm);
11047
11048 FailurePath* failure;
11049 if (!addFailurePath(&failure)) {
11050 return false;
11051 }
11052
11053 StubFieldOffset expected(expectedOffset, StubField::Type::RawInt32);
11054 emitLoadStubField(expected, scratch);
11055
11056 StubFieldOffset generationAddr(generationAddrOffset,
11057 StubField::Type::RawPointer);
11058 emitLoadStubField(generationAddr, scratch2);
11059
11060 masm.branch32(Assembler::NotEqual, Address(scratch2, 0), scratch,
11061 failure->label());
11062
11063 return true;
11064 }
11065
11066 bool CacheIRCompiler::emitGuardFuse(RealmFuses::FuseIndex fuseIndex) {
11067 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11068 AutoScratchRegister scratch(allocator, masm);
11069
11070 FailurePath* failure;
11071 if (!addFailurePath(&failure)) {
11072 return false;
11073 }
11074
11075 masm.loadRealmFuse(fuseIndex, scratch);
11076 masm.branchPtr(Assembler::NotEqual, scratch, ImmPtr(nullptr),
11077 failure->label());
11078 return true;
11079 }
11080
11081 bool CacheIRCompiler::emitBailout() {
11082 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11083
11084 // Generates no code.
11085
11086 return true;
11087 }
11088
11089 bool CacheIRCompiler::emitAssertFloat32Result(ValOperandId valId,
11090 bool mustFloat32) {
11091 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11092
11093 AutoOutputRegister output(*this);
11094
11095 // NOP when not in IonMonkey
11096 masm.moveValue(UndefinedValue(), output.valueReg());
11097
11098 return true;
11099 }
11100
11101 bool CacheIRCompiler::emitAssertRecoveredOnBailoutResult(ValOperandId valId,
11102 bool mustBeRecovered) {
11103 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11104
11105 AutoOutputRegister output(*this);
11106
11107 // NOP when not in IonMonkey
11108 masm.moveValue(UndefinedValue(), output.valueReg());
11109
11110 return true;
11111 }
11112
11113 bool CacheIRCompiler::emitAssertPropertyLookup(ObjOperandId objId,
11114 uint32_t idOffset,
11115 uint32_t slotOffset) {
11116 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11117
11118 Register obj = allocator.useRegister(masm, objId);
11119
11120 AutoScratchRegister id(allocator, masm);
11121 AutoScratchRegister slot(allocator, masm);
11122
11123 LiveRegisterSet save = liveVolatileRegs();
11124 masm.PushRegsInMask(save);
11125
11126 masm.setupUnalignedABICall(id);
11127
11128 StubFieldOffset idField(idOffset, StubField::Type::Id);
11129 emitLoadStubField(idField, id);
11130
11131 StubFieldOffset slotField(slotOffset, StubField::Type::RawInt32);
11132 emitLoadStubField(slotField, slot);
11133
11134 masm.passABIArg(obj);
11135 masm.passABIArg(id);
11136 masm.passABIArg(slot);
11137 using Fn = void (*)(NativeObject*, PropertyKey, uint32_t);
11138 masm.callWithABI<Fn, js::jit::AssertPropertyLookup>();
11139 masm.PopRegsInMask(save);
11140
11141 return true;
11142 }
11143
11144 #ifdef FUZZING_JS_FUZZILLI
11145 bool CacheIRCompiler::emitFuzzilliHashResult(ValOperandId valId) {
11146 JitSpew(JitSpew_Codegen, "%s", __FUNCTION__);
11147
11148 AutoCallVM callvm(masm, this, allocator);
11149 const AutoOutputRegister& output = callvm.output();
11150
11151 ValueOperand input = allocator.useValueRegister(masm, valId);
11152 AutoScratchRegister scratch(allocator, masm);
11153 AutoScratchRegisterMaybeOutput scratch2(allocator, masm, output);
11154 AutoScratchRegisterMaybeOutputType scratchJSContext(allocator, masm, output);
11155 AutoScratchFloatRegister floatReg(this);
11156
11157 Label hashDouble, updateHash, done;
11158
11159 Label isInt32, isDouble, isNull, isUndefined, isBoolean, isBigInt, isObject;
11160 {
11161 ScratchTagScope tag(masm, input);
11162 masm.splitTagForTest(input, tag);
11163
11164 masm.branchTestInt32(Assembler::Equal, tag, &isInt32);
11165 masm.branchTestDouble(Assembler::Equal, tag, &isDouble);
11166 masm.branchTestNull(Assembler::Equal, tag, &isNull);
11167 masm.branchTestUndefined(Assembler::Equal, tag, &isUndefined);
11168 masm.branchTestBoolean(Assembler::Equal, tag, &isBoolean);
11169 masm.branchTestBigInt(Assembler::Equal, tag, &isBigInt);
11170 masm.branchTestObject(Assembler::Equal, tag, &isObject);
11171
11172 // Symbol or String.
11173 masm.move32(Imm32(0), scratch);
11174 masm.jump(&updateHash);
11175 }
11176
11177 masm.bind(&isInt32);
11178 {
11179 masm.unboxInt32(input, scratch);
11180 masm.convertInt32ToDouble(scratch, floatReg);
11181 masm.jump(&hashDouble);
11182 }
11183
11184 masm.bind(&isDouble);
11185 {
11186 masm.unboxDouble(input, floatReg);
11187 masm.jump(&hashDouble);
11188 }
11189
11190 masm.bind(&isNull);
11191 {
11192 masm.loadConstantDouble(1.0, floatReg);
11193 masm.jump(&hashDouble);
11194 }
11195
11196 masm.bind(&isUndefined);
11197 {
11198 masm.loadConstantDouble(2.0, floatReg);
11199 masm.jump(&hashDouble);
11200 }
11201
11202 masm.bind(&isBoolean);
11203 {
11204 masm.unboxBoolean(input, scratch);
11205 masm.add32(Imm32(3), scratch);
11206 masm.convertInt32ToDouble(scratch, floatReg);
11207 masm.jump(&hashDouble);
11208 }
11209
11210 masm.bind(&isBigInt);
11211 {
11212 masm.unboxBigInt(input, scratch);
11213
11214 LiveRegisterSet volatileRegs = liveVolatileRegs();
11215 masm.PushRegsInMask(volatileRegs);
11216
11217 using Fn = uint32_t (*)(BigInt* bigInt);
11218 masm.setupUnalignedABICall(scratchJSContext);
11219 masm.loadJSContext(scratchJSContext);
11220 masm.passABIArg(scratch);
11221 masm.callWithABI<Fn, js::FuzzilliHashBigInt>();
11222 masm.storeCallInt32Result(scratch);
11223
11224 LiveRegisterSet ignore;
11225 ignore.add(scratch);
11226 ignore.add(scratchJSContext);
11227 masm.PopRegsInMaskIgnore(volatileRegs, ignore);
11228 masm.jump(&updateHash);
11229 }
11230
11231 masm.bind(&isObject);
11232 {
11233 masm.unboxObject(input, scratch);
11234
11235 callvm.prepare();
11236 masm.Push(scratch);
11237
11238 using Fn = void (*)(JSContext* cx, JSObject* o);
11239 callvm.callNoResult<Fn, js::FuzzilliHashObject>();
11240
11241 masm.jump(&done);
11242 }
11243
11244 masm.bind(&hashDouble);
11245 masm.fuzzilliHashDouble(floatReg, scratch, scratch2);
11246
11247 masm.bind(&updateHash);
11248 masm.fuzzilliStoreHash(scratch, scratchJSContext, scratch2);
11249
11250 masm.bind(&done);
11251
11252 masm.moveValue(UndefinedValue(), output.valueReg());
11253 return true;
11254 }
11255 #endif
11256
11257 template <typename Fn, Fn fn>
11258 void CacheIRCompiler::callVM(MacroAssembler& masm) {
11259 VMFunctionId id = VMFunctionToId<Fn, fn>::id;
11260 callVMInternal(masm, id);
11261 }
11262
11263 void CacheIRCompiler::callVMInternal(MacroAssembler& masm, VMFunctionId id) {
11264 MOZ_ASSERT(enteredStubFrame_);
11265 if (mode_ == Mode::Ion) {
11266 TrampolinePtr code = cx_->runtime()->jitRuntime()->getVMWrapper(id);
11267 const VMFunctionData& fun = GetVMFunction(id);
11268 uint32_t frameSize = fun.explicitStackSlots() * sizeof(void*);
11269 masm.PushFrameDescriptor(FrameType::IonICCall);
11270 masm.callJit(code);
11271
11272 // Pop rest of the exit frame and the arguments left on the stack.
11273 int framePop =
11274 sizeof(ExitFrameLayout) - ExitFrameLayout::bytesPoppedAfterCall();
11275 masm.implicitPop(frameSize + framePop);
11276
11277 masm.freeStack(asIon()->localTracingSlots() * sizeof(Value));
11278
11279 // Pop IonICCallFrameLayout.
11280 masm.Pop(FramePointer);
11281 masm.freeStack(IonICCallFrameLayout::Size() - sizeof(void*));
11282 return;
11283 }
11284
11285 MOZ_ASSERT(mode_ == Mode::Baseline);
11286
11287 TrampolinePtr code = cx_->runtime()->jitRuntime()->getVMWrapper(id);
11288
11289 EmitBaselineCallVM(code, masm);
11290 }
11291
11292 bool CacheIRCompiler::isBaseline() { return mode_ == Mode::Baseline; }
11293
11294 bool CacheIRCompiler::isIon() { return mode_ == Mode::Ion; }
11295
11296 BaselineCacheIRCompiler* CacheIRCompiler::asBaseline() {
11297 MOZ_ASSERT(this->isBaseline());
11298 return static_cast<BaselineCacheIRCompiler*>(this);
11299 }
11300
11301 IonCacheIRCompiler* CacheIRCompiler::asIon() {
11302 MOZ_ASSERT(this->isIon());
11303 return static_cast<IonCacheIRCompiler*>(this);
11304 }
11305
11306 #ifdef DEBUG
11307 void CacheIRCompiler::assertFloatRegisterAvailable(FloatRegister reg) {
11308 if (isBaseline()) {
11309 // Baseline does not have any FloatRegisters live when calling an IC stub.
11310 return;
11311 }
11312
11313 asIon()->assertFloatRegisterAvailable(reg);
11314 }
11315 #endif
11316
11317 AutoCallVM::AutoCallVM(MacroAssembler& masm, CacheIRCompiler* compiler,
11318 CacheRegisterAllocator& allocator)
11319 : masm_(masm), compiler_(compiler), allocator_(allocator) {
11320 // Ion needs to `enterStubFrame` before it can callVM and it also needs to
11321 // initialize AutoSaveLiveRegisters.
11322 if (compiler_->mode_ == CacheIRCompiler::Mode::Ion) {
11323 // Will need to use a downcast here as well, in order to pass the
11324 // stub to AutoSaveLiveRegisters
11325 save_.emplace(*compiler_->asIon());
11326 }
11327
11328 if (compiler->outputUnchecked_.isSome()) {
11329 output_.emplace(*compiler);
11330 }
11331
11332 if (compiler_->mode_ == CacheIRCompiler::Mode::Baseline) {
11333 stubFrame_.emplace(*compiler_->asBaseline());
11334 if (output_.isSome()) {
11335 scratch_.emplace(allocator_, masm_, output_.ref());
11336 } else {
11337 scratch_.emplace(allocator_, masm_);
11338 }
11339 }
11340 }
11341
11342 void AutoCallVM::prepare() {
11343 allocator_.discardStack(masm_);
11344 MOZ_ASSERT(compiler_ != nullptr);
11345 if (compiler_->mode_ == CacheIRCompiler::Mode::Ion) {
11346 compiler_->asIon()->enterStubFrame(masm_, *save_.ptr());
11347 return;
11348 }
11349 MOZ_ASSERT(compiler_->mode_ == CacheIRCompiler::Mode::Baseline);
11350 stubFrame_->enter(masm_, scratch_.ref());
11351 }
11352
11353 void AutoCallVM::storeResult(JSValueType returnType) {
11354 MOZ_ASSERT(returnType != JSVAL_TYPE_DOUBLE);
11355
11356 if (returnType == JSVAL_TYPE_UNKNOWN) {
11357 masm_.storeCallResultValue(output_.ref());
11358 } else {
11359 if (output_->hasValue()) {
11360 masm_.tagValue(returnType, ReturnReg, output_->valueReg());
11361 } else {
11362 masm_.storeCallPointerResult(output_->typedReg().gpr());
11363 }
11364 }
11365 }
11366
11367 void AutoCallVM::leaveBaselineStubFrame() {
11368 if (compiler_->mode_ == CacheIRCompiler::Mode::Baseline) {
11369 stubFrame_->leave(masm_);
11370 }
11371 }
11372
11373 template <typename...>
11374 struct VMFunctionReturnType;
11375
11376 template <class R, typename... Args>
11377 struct VMFunctionReturnType<R (*)(JSContext*, Args...)> {
11378 using LastArgument = typename LastArg<Args...>::Type;
11379
11380 // By convention VMFunctions returning `bool` use an output parameter.
11381 using ReturnType =
11382 std::conditional_t<std::is_same_v<R, bool>, LastArgument, R>;
11383 };
11384
11385 template <class>
11386 struct ReturnTypeToJSValueType;
11387
11388 // Definitions for the currently used return types.
11389 template <>
11390 struct ReturnTypeToJSValueType<MutableHandleValue> {
11391 static constexpr JSValueType result = JSVAL_TYPE_UNKNOWN;
11392 };
11393 template <>
11394 struct ReturnTypeToJSValueType<bool*> {
11395 static constexpr JSValueType result = JSVAL_TYPE_BOOLEAN;
11396 };
11397 template <>
11398 struct ReturnTypeToJSValueType<int32_t*> {
11399 static constexpr JSValueType result = JSVAL_TYPE_INT32;
11400 };
11401 template <>
11402 struct ReturnTypeToJSValueType<JSString*> {
11403 static constexpr JSValueType result = JSVAL_TYPE_STRING;
11404 };
11405 template <>
11406 struct ReturnTypeToJSValueType<JSLinearString*> {
11407 static constexpr JSValueType result = JSVAL_TYPE_STRING;
11408 };
11409 template <>
11410 struct ReturnTypeToJSValueType<JSAtom*> {
11411 static constexpr JSValueType result = JSVAL_TYPE_STRING;
11412 };
11413 template <>
11414 struct ReturnTypeToJSValueType<BigInt*> {
11415 static constexpr JSValueType result = JSVAL_TYPE_BIGINT;
11416 };
11417 template <>
11418 struct ReturnTypeToJSValueType<JSObject*> {
11419 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11420 };
11421 template <>
11422 struct ReturnTypeToJSValueType<PropertyIteratorObject*> {
11423 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11424 };
11425 template <>
11426 struct ReturnTypeToJSValueType<ArrayIteratorObject*> {
11427 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11428 };
11429 template <>
11430 struct ReturnTypeToJSValueType<StringIteratorObject*> {
11431 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11432 };
11433 template <>
11434 struct ReturnTypeToJSValueType<RegExpStringIteratorObject*> {
11435 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11436 };
11437 template <>
11438 struct ReturnTypeToJSValueType<PlainObject*> {
11439 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11440 };
11441 template <>
11442 struct ReturnTypeToJSValueType<ArrayObject*> {
11443 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11444 };
11445 template <>
11446 struct ReturnTypeToJSValueType<TypedArrayObject*> {
11447 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11448 };
11449 template <>
11450 struct ReturnTypeToJSValueType<MapObject*> {
11451 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11452 };
11453 template <>
11454 struct ReturnTypeToJSValueType<SetObject*> {
11455 static constexpr JSValueType result = JSVAL_TYPE_OBJECT;
11456 };
11457
11458 template <typename Fn>
11459 void AutoCallVM::storeResult() {
11460 using ReturnType = typename VMFunctionReturnType<Fn>::ReturnType;
11461 storeResult(ReturnTypeToJSValueType<ReturnType>::result);
11462 }
11463
11464 AutoScratchFloatRegister::AutoScratchFloatRegister(CacheIRCompiler* compiler,
11465 FailurePath* failure)
11466 : compiler_(compiler), failure_(failure) {
11467 // If we're compiling a Baseline IC, FloatReg0 is always available.
11468 if (!compiler_->isBaseline()) {
11469 MacroAssembler& masm = compiler_->masm;
11470 masm.push(FloatReg0);
11471 compiler->allocator.setHasAutoScratchFloatRegisterSpill(true);
11472 }
11473
11474 if (failure_) {
11475 failure_->setHasAutoScratchFloatRegister();
11476 }
11477 }
11478
11479 AutoScratchFloatRegister::~AutoScratchFloatRegister() {
11480 if (failure_) {
11481 failure_->clearHasAutoScratchFloatRegister();
11482 }
11483
11484 if (!compiler_->isBaseline()) {
11485 MacroAssembler& masm = compiler_->masm;
11486 masm.pop(FloatReg0);
11487 compiler_->allocator.setHasAutoScratchFloatRegisterSpill(false);
11488
11489 if (failure_) {
11490 Label done;
11491 masm.jump(&done);
11492 masm.bind(&failurePopReg_);
11493 masm.pop(FloatReg0);
11494 masm.jump(failure_->label());
11495 masm.bind(&done);
11496 }
11497 }
11498 }
11499
11500 Label* AutoScratchFloatRegister::failure() {
11501 MOZ_ASSERT(failure_);
11502
11503 if (!compiler_->isBaseline()) {
11504 return &failurePopReg_;
11505 }
11506 return failure_->labelUnchecked();
11507 }