Debian Codegraph @

js/src/jit/x86/MacroAssembler-x86-inl.h

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 #ifndef jit_x86_MacroAssembler_x86_inl_h
8 #define jit_x86_MacroAssembler_x86_inl_h
9
10 #include "jit/x86/MacroAssembler-x86.h"
11
12 #include "jit/x86-shared/MacroAssembler-x86-shared-inl.h"
13
14 namespace js {
15 namespace jit {
16
17 //{{{ check_macroassembler_style
18
19 void MacroAssembler::move64(Imm64 imm, Register64 dest) {
20 move32(Imm32(imm.value & 0xFFFFFFFFL), dest.low);
21 move32(Imm32((imm.value >> 32) & 0xFFFFFFFFL), dest.high);
22 }
23
24 void MacroAssembler::move64(Register64 src, Register64 dest) {
25 movl(src.low, dest.low);
26 movl(src.high, dest.high);
27 }
28
29 void MacroAssembler::moveDoubleToGPR64(FloatRegister src, Register64 dest) {
30 ScratchDoubleScope scratch(*this);
31
32 if (Assembler::HasSSE41()) {
33 vmovd(src, dest.low);
34 vpextrd(1, src, dest.high);
35 } else {
36 vmovd(src, dest.low);
37 moveDouble(src, scratch);
38 vpsrldq(Imm32(4), scratch, scratch);
39 vmovd(scratch, dest.high);
40 }
41 }
42
43 void MacroAssembler::moveGPR64ToDouble(Register64 src, FloatRegister dest) {
44 if (Assembler::HasSSE41()) {
45 vmovd(src.low, dest);
46 vpinsrd(1, src.high, dest, dest);
47 } else {
48 ScratchDoubleScope fpscratch(*this);
49 vmovd(src.low, dest);
50 vmovd(src.high, fpscratch);
51 vunpcklps(fpscratch, dest, dest);
52 }
53 }
54
55 void MacroAssembler::move64To32(Register64 src, Register dest) {
56 if (src.low != dest) {
57 movl(src.low, dest);
58 }
59 }
60
61 void MacroAssembler::move32To64ZeroExtend(Register src, Register64 dest) {
62 if (src != dest.low) {
63 movl(src, dest.low);
64 }
65 movl(Imm32(0), dest.high);
66 }
67
68 void MacroAssembler::move8To64SignExtend(Register src, Register64 dest) {
69 move8SignExtend(src, dest.low);
70 if (dest.low == eax && dest.high == edx) {
71 masm.cdq();
72 } else {
73 movl(dest.low, dest.high);
74 sarl(Imm32(31), dest.high);
75 }
76 }
77
78 void MacroAssembler::move16To64SignExtend(Register src, Register64 dest) {
79 move16SignExtend(src, dest.low);
80 if (dest.low == eax && dest.high == edx) {
81 masm.cdq();
82 } else {
83 movl(dest.low, dest.high);
84 sarl(Imm32(31), dest.high);
85 }
86 }
87
88 void MacroAssembler::move32To64SignExtend(Register src, Register64 dest) {
89 if (src != dest.low) {
90 movl(src, dest.low);
91 }
92 if (dest.low == eax && dest.high == edx) {
93 masm.cdq();
94 } else {
95 movl(dest.low, dest.high);
96 sarl(Imm32(31), dest.high);
97 }
98 }
99
100 void MacroAssembler::move8SignExtendToPtr(Register src, Register dest) {
101 move8SignExtend(src, dest);
102 }
103
104 void MacroAssembler::move16SignExtendToPtr(Register src, Register dest) {
105 move16SignExtend(src, dest);
106 }
107
108 void MacroAssembler::move32SignExtendToPtr(Register src, Register dest) {
109 movl(src, dest);
110 }
111
112 void MacroAssembler::move32ZeroExtendToPtr(Register src, Register dest) {
113 movl(src, dest);
114 }
115
116 // ===============================================================
117 // Load instructions
118
119 void MacroAssembler::load32SignExtendToPtr(const Address& src, Register dest) {
120 load32(src, dest);
121 }
122
123 // ===============================================================
124 // Logical functions
125
126 void MacroAssembler::notPtr(Register reg) { notl(reg); }
127
128 void MacroAssembler::andPtr(Register src, Register dest) { andl(src, dest); }
129
130 void MacroAssembler::andPtr(Imm32 imm, Register dest) { andl(imm, dest); }
131
132 void MacroAssembler::andPtr(Imm32 imm, Register src, Register dest) {
133 if (src != dest) {
134 movl(src, dest);
135 }
136 andl(imm, dest);
137 }
138
139 void MacroAssembler::and64(Imm64 imm, Register64 dest) {
140 if (imm.low().value != int32_t(0xFFFFFFFF)) {
141 andl(imm.low(), dest.low);
142 }
143 if (imm.hi().value != int32_t(0xFFFFFFFF)) {
144 andl(imm.hi(), dest.high);
145 }
146 }
147
148 void MacroAssembler::or64(Imm64 imm, Register64 dest) {
149 if (imm.low().value != 0) {
150 orl(imm.low(), dest.low);
151 }
152 if (imm.hi().value != 0) {
153 orl(imm.hi(), dest.high);
154 }
155 }
156
157 void MacroAssembler::xor64(Imm64 imm, Register64 dest) {
158 if (imm.low().value != 0) {
159 xorl(imm.low(), dest.low);
160 }
161 if (imm.hi().value != 0) {
162 xorl(imm.hi(), dest.high);
163 }
164 }
165
166 void MacroAssembler::orPtr(Register src, Register dest) { orl(src, dest); }
167
168 void MacroAssembler::orPtr(Imm32 imm, Register dest) { orl(imm, dest); }
169
170 void MacroAssembler::orPtr(Imm32 imm, Register src, Register dest) {
171 if (src != dest) {
172 movl(src, dest);
173 }
174 orl(imm, dest);
175 }
176
177 void MacroAssembler::and64(Register64 src, Register64 dest) {
178 andl(src.low, dest.low);
179 andl(src.high, dest.high);
180 }
181
182 void MacroAssembler::or64(Register64 src, Register64 dest) {
183 orl(src.low, dest.low);
184 orl(src.high, dest.high);
185 }
186
187 void MacroAssembler::xor64(Register64 src, Register64 dest) {
188 xorl(src.low, dest.low);
189 xorl(src.high, dest.high);
190 }
191
192 void MacroAssembler::xorPtr(Register src, Register dest) { xorl(src, dest); }
193
194 void MacroAssembler::xorPtr(Imm32 imm, Register dest) { xorl(imm, dest); }
195
196 void MacroAssembler::xorPtr(Imm32 imm, Register src, Register dest) {
197 if (src != dest) {
198 movl(src, dest);
199 }
200 xorl(imm, dest);
201 }
202
203 // ===============================================================
204 // Swap instructions
205
206 void MacroAssembler::byteSwap64(Register64 reg) {
207 bswapl(reg.low);
208 bswapl(reg.high);
209 xchgl(reg.low, reg.high);
210 }
211
212 // ===============================================================
213 // Arithmetic functions
214
215 void MacroAssembler::addPtr(Register src, Register dest) { addl(src, dest); }
216
217 void MacroAssembler::addPtr(Imm32 imm, Register dest) { addl(imm, dest); }
218
219 void MacroAssembler::addPtr(ImmWord imm, Register dest) {
220 addl(Imm32(imm.value), dest);
221 }
222
223 void MacroAssembler::addPtr(Imm32 imm, const Address& dest) {
224 addl(imm, Operand(dest));
225 }
226
227 void MacroAssembler::addPtr(Imm32 imm, const AbsoluteAddress& dest) {
228 addl(imm, Operand(dest));
229 }
230
231 void MacroAssembler::addPtr(const Address& src, Register dest) {
232 addl(Operand(src), dest);
233 }
234
235 void MacroAssembler::add64(Register64 src, Register64 dest) {
236 addl(src.low, dest.low);
237 adcl(src.high, dest.high);
238 }
239
240 void MacroAssembler::add64(Imm32 imm, Register64 dest) {
241 addl(imm, dest.low);
242 adcl(Imm32(0), dest.high);
243 }
244
245 void MacroAssembler::add64(Imm64 imm, Register64 dest) {
246 if (imm.low().value == 0) {
247 addl(imm.hi(), dest.high);
248 return;
249 }
250 addl(imm.low(), dest.low);
251 adcl(imm.hi(), dest.high);
252 }
253
254 void MacroAssembler::addConstantDouble(double d, FloatRegister dest) {
255 Double* dbl = getDouble(d);
256 if (!dbl) {
257 return;
258 }
259 masm.vaddsd_mr(nullptr, dest.encoding(), dest.encoding());
260 propagateOOM(dbl->uses.append(CodeOffset(masm.size())));
261 }
262
263 CodeOffset MacroAssembler::sub32FromStackPtrWithPatch(Register dest) {
264 moveStackPtrTo(dest);
265 addlWithPatch(Imm32(0), dest);
266 return CodeOffset(currentOffset());
267 }
268
269 void MacroAssembler::patchSub32FromStackPtr(CodeOffset offset, Imm32 imm) {
270 patchAddl(offset, -imm.value);
271 }
272
273 void MacroAssembler::subPtr(Register src, Register dest) { subl(src, dest); }
274
275 void MacroAssembler::subPtr(Register src, const Address& dest) {
276 subl(src, Operand(dest));
277 }
278
279 void MacroAssembler::subPtr(Imm32 imm, Register dest) { subl(imm, dest); }
280
281 void MacroAssembler::subPtr(const Address& addr, Register dest) {
282 subl(Operand(addr), dest);
283 }
284
285 void MacroAssembler::sub64(Register64 src, Register64 dest) {
286 subl(src.low, dest.low);
287 sbbl(src.high, dest.high);
288 }
289
290 void MacroAssembler::sub64(Imm64 imm, Register64 dest) {
291 if (imm.low().value == 0) {
292 subl(imm.hi(), dest.high);
293 return;
294 }
295 subl(imm.low(), dest.low);
296 sbbl(imm.hi(), dest.high);
297 }
298
299 void MacroAssembler::mulHighUnsigned32(Imm32 imm, Register src, Register dest) {
300 // Preserve edx:eax, unless they're the destination register.
301 if (edx != dest) {
302 push(edx);
303 }
304 if (eax != dest) {
305 push(eax);
306 }
307
308 if (src != eax) {
309 // Compute edx:eax := eax ∗ src
310 movl(imm, eax);
311 mull(src);
312 } else {
313 // Compute edx:eax := eax ∗ edx
314 movl(imm, edx);
315 mull(edx);
316 }
317
318 // Move the high word from edx into |dest|.
319 if (edx != dest) {
320 movl(edx, dest);
321 }
322
323 // Restore edx:eax.
324 if (eax != dest) {
325 pop(eax);
326 }
327 if (edx != dest) {
328 pop(edx);
329 }
330 }
331
332 void MacroAssembler::mulPtr(Register rhs, Register srcDest) {
333 imull(rhs, srcDest);
334 }
335
336 // Note: this function clobbers eax and edx.
337 void MacroAssembler::mul64(Imm64 imm, const Register64& dest) {
338 // LOW32 = LOW(LOW(dest) * LOW(imm));
339 // HIGH32 = LOW(HIGH(dest) * LOW(imm)) [multiply imm into upper bits]
340 // + LOW(LOW(dest) * HIGH(imm)) [multiply dest into upper bits]
341 // + HIGH(LOW(dest) * LOW(imm)) [carry]
342
343 MOZ_ASSERT(dest.low != eax && dest.low != edx);
344 MOZ_ASSERT(dest.high != eax && dest.high != edx);
345
346 // HIGH(dest) = LOW(HIGH(dest) * LOW(imm));
347 movl(Imm32(imm.value & 0xFFFFFFFFL), edx);
348 imull(edx, dest.high);
349
350 // edx:eax = LOW(dest) * LOW(imm);
351 movl(Imm32(imm.value & 0xFFFFFFFFL), edx);
352 movl(dest.low, eax);
353 mull(edx);
354
355 // HIGH(dest) += edx;
356 addl(edx, dest.high);
357
358 // HIGH(dest) += LOW(LOW(dest) * HIGH(imm));
359 if (((imm.value >> 32) & 0xFFFFFFFFL) == 5) {
360 leal(Operand(dest.low, dest.low, TimesFour), edx);
361 } else {
362 MOZ_CRASH("Unsupported imm");
363 }
364 addl(edx, dest.high);
365
366 // LOW(dest) = eax;
367 movl(eax, dest.low);
368 }
369
370 void MacroAssembler::mul64(Imm64 imm, const Register64& dest,
371 const Register temp) {
372 // LOW32 = LOW(LOW(dest) * LOW(src)); (1)
373 // HIGH32 = LOW(HIGH(dest) * LOW(src)) [multiply src into upper bits] (2)
374 // + LOW(LOW(dest) * HIGH(src)) [multiply dest into upper bits] (3)
375 // + HIGH(LOW(dest) * LOW(src)) [carry] (4)
376
377 MOZ_ASSERT(dest == Register64(edx, eax));
378 MOZ_ASSERT(temp != edx && temp != eax);
379
380 movl(dest.low, temp);
381
382 // Compute mul64
383 imull(imm.low(), dest.high); // (2)
384 imull(imm.hi(), temp); // (3)
385 addl(dest.high, temp);
386 movl(imm.low(), dest.high);
387 mull(dest.high /*, dest.low*/); // (4) + (1) output in edx:eax
388 // (dest_hi:dest_lo)
389 addl(temp, dest.high);
390 }
391
392 void MacroAssembler::mul64(const Register64& src, const Register64& dest,
393 const Register temp) {
394 // LOW32 = LOW(LOW(dest) * LOW(src)); (1)
395 // HIGH32 = LOW(HIGH(dest) * LOW(src)) [multiply src into upper bits] (2)
396 // + LOW(LOW(dest) * HIGH(src)) [multiply dest into upper bits] (3)
397 // + HIGH(LOW(dest) * LOW(src)) [carry] (4)
398
399 MOZ_ASSERT(dest == Register64(edx, eax));
400 MOZ_ASSERT(src != Register64(edx, eax) && src != Register64(eax, edx));
401
402 // Make sure the rhs.high isn't the dest.high register anymore.
403 // This saves us from doing other register moves.
404 movl(dest.low, temp);
405
406 // Compute mul64
407 imull(src.low, dest.high); // (2)
408 imull(src.high, temp); // (3)
409 addl(dest.high, temp);
410 movl(src.low, dest.high);
411 mull(dest.high /*, dest.low*/); // (4) + (1) output in edx:eax
412 // (dest_hi:dest_lo)
413 addl(temp, dest.high);
414 }
415
416 void MacroAssembler::mulBy3(Register src, Register dest) {
417 lea(Operand(src, src, TimesTwo), dest);
418 }
419
420 void MacroAssembler::mulDoublePtr(ImmPtr imm, Register temp,
421 FloatRegister dest) {
422 movl(imm, temp);
423 vmulsd(Operand(temp, 0), dest, dest);
424 }
425
426 void MacroAssembler::inc64(AbsoluteAddress dest) {
427 addl(Imm32(1), Operand(dest));
428 Label noOverflow;
429 j(NonZero, &noOverflow);
430 addl(Imm32(1), Operand(dest.offset(4)));
431 bind(&noOverflow);
432 }
433
434 void MacroAssembler::neg64(Register64 reg) {
435 negl(reg.low);
436 adcl(Imm32(0), reg.high);
437 negl(reg.high);
438 }
439
440 void MacroAssembler::negPtr(Register reg) { negl(reg); }
441
442 // ===============================================================
443 // Shift functions
444
445 void MacroAssembler::lshiftPtr(Imm32 imm, Register dest) {
446 lshift32(imm, dest);
447 }
448
449 void MacroAssembler::lshiftPtr(Imm32 imm, Register src, Register dest) {
450 lshift32(imm, src, dest);
451 }
452
453 void MacroAssembler::lshiftPtr(Register shift, Register srcDest) {
454 lshift32(shift, srcDest);
455 }
456
457 void MacroAssembler::flexibleLshiftPtr(Register shift, Register srcDest) {
458 flexibleLshift32(shift, srcDest);
459 }
460
461 void MacroAssembler::lshift64(Imm32 imm, Register64 dest) {
462 MOZ_ASSERT(0 <= imm.value && imm.value < 64);
463 if (imm.value < 32) {
464 shldl(imm, dest.low, dest.high);
465 shll(imm, dest.low);
466 return;
467 }
468
469 mov(dest.low, dest.high);
470 shll(Imm32(imm.value & 0x1f), dest.high);
471 xorl(dest.low, dest.low);
472 }
473
474 void MacroAssembler::lshift64(Register shift, Register64 srcDest) {
475 MOZ_ASSERT(shift == ecx);
476 MOZ_ASSERT(srcDest.low != ecx && srcDest.high != ecx);
477
478 Label done;
479
480 shldl_cl(srcDest.low, srcDest.high);
481 shll_cl(srcDest.low);
482
483 testl(Imm32(0x20), ecx);
484 j(Condition::Equal, &done);
485
486 // 32 - 63 bit shift
487 movl(srcDest.low, srcDest.high);
488 xorl(srcDest.low, srcDest.low);
489
490 bind(&done);
491 }
492
493 void MacroAssembler::rshiftPtr(Imm32 imm, Register dest) {
494 rshift32(imm, dest);
495 }
496
497 void MacroAssembler::rshiftPtr(Imm32 imm, Register src, Register dest) {
498 rshift32(imm, src, dest);
499 }
500
501 void MacroAssembler::rshiftPtr(Register shift, Register srcDest) {
502 rshift32(shift, srcDest);
503 }
504
505 void MacroAssembler::flexibleRshiftPtr(Register shift, Register srcDest) {
506 flexibleRshift32(shift, srcDest);
507 }
508
509 void MacroAssembler::rshift64(Imm32 imm, Register64 dest) {
510 MOZ_ASSERT(0 <= imm.value && imm.value < 64);
511 if (imm.value < 32) {
512 shrdl(imm, dest.high, dest.low);
513 shrl(imm, dest.high);
514 return;
515 }
516
517 movl(dest.high, dest.low);
518 shrl(Imm32(imm.value & 0x1f), dest.low);
519 xorl(dest.high, dest.high);
520 }
521
522 void MacroAssembler::rshift64(Register shift, Register64 srcDest) {
523 MOZ_ASSERT(shift == ecx);
524 MOZ_ASSERT(srcDest.low != ecx && srcDest.high != ecx);
525
526 Label done;
527
528 shrdl_cl(srcDest.high, srcDest.low);
529 shrl_cl(srcDest.high);
530
531 testl(Imm32(0x20), ecx);
532 j(Condition::Equal, &done);
533
534 // 32 - 63 bit shift
535 movl(srcDest.high, srcDest.low);
536 xorl(srcDest.high, srcDest.high);
537
538 bind(&done);
539 }
540
541 void MacroAssembler::rshiftPtrArithmetic(Imm32 imm, Register dest) {
542 rshift32Arithmetic(imm, dest);
543 }
544
545 void MacroAssembler::rshiftPtrArithmetic(Imm32 imm, Register src,
546 Register dest) {
547 rshift32Arithmetic(imm, src, dest);
548 }
549
550 void MacroAssembler::rshiftPtrArithmetic(Register shift, Register srcDest) {
551 rshift32Arithmetic(shift, srcDest);
552 }
553
554 void MacroAssembler::flexibleRshiftPtrArithmetic(Register shift,
555 Register srcDest) {
556 flexibleRshift32Arithmetic(shift, srcDest);
557 }
558
559 void MacroAssembler::rshift64Arithmetic(Imm32 imm, Register64 dest) {
560 MOZ_ASSERT(0 <= imm.value && imm.value < 64);
561 if (imm.value < 32) {
562 shrdl(imm, dest.high, dest.low);
563 sarl(imm, dest.high);
564 return;
565 }
566
567 movl(dest.high, dest.low);
568 sarl(Imm32(imm.value & 0x1f), dest.low);
569 sarl(Imm32(0x1f), dest.high);
570 }
571
572 void MacroAssembler::rshift64Arithmetic(Register shift, Register64 srcDest) {
573 MOZ_ASSERT(shift == ecx);
574 MOZ_ASSERT(srcDest.low != ecx && srcDest.high != ecx);
575
576 Label done;
577
578 shrdl_cl(srcDest.high, srcDest.low);
579 sarl_cl(srcDest.high);
580
581 testl(Imm32(0x20), ecx);
582 j(Condition::Equal, &done);
583
584 // 32 - 63 bit shift
585 movl(srcDest.high, srcDest.low);
586 sarl(Imm32(0x1f), srcDest.high);
587
588 bind(&done);
589 }
590
591 // ===============================================================
592 // Rotation functions
593
594 void MacroAssembler::rotateLeft64(Register count, Register64 src,
595 Register64 dest, Register temp) {
596 MOZ_ASSERT(src == dest, "defineReuseInput");
597 MOZ_ASSERT(count == ecx, "defineFixed(ecx)");
598
599 Label done;
600
601 movl(dest.high, temp);
602 shldl_cl(dest.low, dest.high);
603 shldl_cl(temp, dest.low);
604
605 testl(Imm32(0x20), count);
606 j(Condition::Equal, &done);
607 xchgl(dest.high, dest.low);
608
609 bind(&done);
610 }
611
612 void MacroAssembler::rotateRight64(Register count, Register64 src,
613 Register64 dest, Register temp) {
614 MOZ_ASSERT(src == dest, "defineReuseInput");
615 MOZ_ASSERT(count == ecx, "defineFixed(ecx)");
616
617 Label done;
618
619 movl(dest.high, temp);
620 shrdl_cl(dest.low, dest.high);
621 shrdl_cl(temp, dest.low);
622
623 testl(Imm32(0x20), count);
624 j(Condition::Equal, &done);
625 xchgl(dest.high, dest.low);
626
627 bind(&done);
628 }
629
630 void MacroAssembler::rotateLeft64(Imm32 count, Register64 src, Register64 dest,
631 Register temp) {
632 MOZ_ASSERT(src == dest, "defineReuseInput");
633
634 int32_t amount = count.value & 0x3f;
635 if ((amount & 0x1f) != 0) {
636 movl(dest.high, temp);
637 shldl(Imm32(amount & 0x1f), dest.low, dest.high);
638 shldl(Imm32(amount & 0x1f), temp, dest.low);
639 }
640
641 if (!!(amount & 0x20)) {
642 xchgl(dest.high, dest.low);
643 }
644 }
645
646 void MacroAssembler::rotateRight64(Imm32 count, Register64 src, Register64 dest,
647 Register temp) {
648 MOZ_ASSERT(src == dest, "defineReuseInput");
649
650 int32_t amount = count.value & 0x3f;
651 if ((amount & 0x1f) != 0) {
652 movl(dest.high, temp);
653 shrdl(Imm32(amount & 0x1f), dest.low, dest.high);
654 shrdl(Imm32(amount & 0x1f), temp, dest.low);
655 }
656
657 if (!!(amount & 0x20)) {
658 xchgl(dest.high, dest.low);
659 }
660 }
661
662 // ===============================================================
663 // Bit counting functions
664
665 void MacroAssembler::clz64(Register64 src, Register64 dest) {
666 if (AssemblerX86Shared::HasLZCNT()) {
667 Label nonzero, zero;
668
669 testl(src.high, src.high);
670 j(Assembler::Zero, &zero);
671
672 lzcntl(src.high, dest.low);
673 jump(&nonzero);
674
675 bind(&zero);
676 lzcntl(src.low, dest.low);
677 addl(Imm32(32), dest.low);
678
679 bind(&nonzero);
680 xorl(dest.high, dest.high);
681 return;
682 }
683
684 // Because |dest.low| may be equal to |src.low|, we rely on BSR not modifying
685 // its output when the input is zero. AMD ISA documents BSR not modifying the
686 // output and current Intel CPUs follow AMD.
687
688 Label nonzero, zero;
689
690 bsrl(src.high, dest.low);
691 j(Assembler::Zero, &zero);
692 orl(Imm32(32), dest.low);
693 jump(&nonzero);
694
695 bind(&zero);
696 bsrl(src.low, dest.low);
697 j(Assembler::NonZero, &nonzero);
698 movl(Imm32(0x7F), dest.low);
699
700 bind(&nonzero);
701 xorl(Imm32(0x3F), dest.low);
702 xorl(dest.high, dest.high);
703 }
704
705 void MacroAssembler::ctz64(Register64 src, Register64 dest) {
706 if (AssemblerX86Shared::HasBMI1()) {
707 Label nonzero, zero;
708
709 testl(src.low, src.low);
710 j(Assembler::Zero, &zero);
711
712 tzcntl(src.low, dest.low);
713 jump(&nonzero);
714
715 bind(&zero);
716 tzcntl(src.high, dest.low);
717 addl(Imm32(32), dest.low);
718
719 bind(&nonzero);
720 xorl(dest.high, dest.high);
721 return;
722 }
723
724 // Because |dest.low| may be equal to |src.low|, we rely on BSF not modifying
725 // its output when the input is zero. AMD ISA documents BSF not modifying the
726 // output and current Intel CPUs follow AMD.
727
728 Label done, nonzero;
729
730 bsfl(src.low, dest.low);
731 j(Assembler::NonZero, &done);
732 bsfl(src.high, dest.low);
733 j(Assembler::NonZero, &nonzero);
734 movl(Imm32(64), dest.low);
735 jump(&done);
736
737 bind(&nonzero);
738 orl(Imm32(32), dest.low);
739
740 bind(&done);
741 xorl(dest.high, dest.high);
742 }
743
744 void MacroAssembler::popcnt64(Register64 src, Register64 dest, Register tmp) {
745 // The tmp register is only needed if there is no native POPCNT.
746
747 MOZ_ASSERT(src.low != tmp && src.high != tmp);
748 MOZ_ASSERT(dest.low != tmp && dest.high != tmp);
749
750 if (dest.low != src.high) {
751 popcnt32(src.low, dest.low, tmp);
752 popcnt32(src.high, dest.high, tmp);
753 } else {
754 MOZ_ASSERT(dest.high != src.high);
755 popcnt32(src.low, dest.high, tmp);
756 popcnt32(src.high, dest.low, tmp);
757 }
758 addl(dest.high, dest.low);
759 xorl(dest.high, dest.high);
760 }
761
762 // ===============================================================
763 // Condition functions
764
765 void MacroAssembler::cmp64Set(Condition cond, Register64 lhs, Register64 rhs,
766 Register dest) {
767 if (lhs.high == dest || lhs.low == dest || rhs.high == dest ||
768 rhs.low == dest) {
769 cmp64SetAliased(cond, lhs, rhs, dest);
770 } else {
771 cmp64SetNonAliased(cond, lhs, rhs, dest);
772 }
773 }
774
775 void MacroAssembler::cmp64Set(Condition cond, Register64 lhs, Imm64 rhs,
776 Register dest) {
777 if (rhs.value == 0 &&
778 (cond == Assembler::Equal || cond == Assembler::NotEqual)) {
779 if (lhs.high == dest) {
780 or32(lhs.low, dest);
781 } else if (lhs.low == dest) {
782 or32(lhs.high, dest);
783 } else {
784 move32(lhs.high, dest);
785 or32(lhs.low, dest);
786 }
787 cmp32Set(cond, dest, Imm32(0), dest);
788 } else if (lhs.high == dest || lhs.low == dest) {
789 cmp64SetAliased(cond, lhs, rhs, dest);
790 } else {
791 cmp64SetNonAliased(cond, lhs, rhs, dest);
792 }
793 }
794
795 void MacroAssembler::cmp64Set(Condition cond, Address lhs, Register64 rhs,
796 Register dest) {
797 if (lhs.base == dest || rhs.high == dest || rhs.low == dest) {
798 cmp64SetAliased(cond, lhs, rhs, dest);
799 } else {
800 cmp64SetNonAliased(cond, lhs, rhs, dest);
801 }
802 }
803
804 void MacroAssembler::cmp64Set(Condition cond, Address lhs, Imm64 rhs,
805 Register dest) {
806 if (lhs.base == dest) {
807 cmp64SetAliased(cond, lhs, rhs, dest);
808 } else {
809 cmp64SetNonAliased(cond, lhs, rhs, dest);
810 }
811 }
812
813 template <typename T1, typename T2>
814 void MacroAssembler::cmpPtrSet(Condition cond, T1 lhs, T2 rhs, Register dest) {
815 bool destIsZero = maybeEmitSetZeroByteRegister(lhs, rhs, dest);
816 cmpPtr(lhs, rhs);
817 emitSet(cond, dest, destIsZero);
818 }
819
820 // ===============================================================
821 // Branch functions
822
823 void MacroAssembler::branch32(Condition cond, const AbsoluteAddress& lhs,
824 Register rhs, Label* label) {
825 cmp32(Operand(lhs), rhs);
826 j(cond, label);
827 }
828
829 void MacroAssembler::branch32(Condition cond, const AbsoluteAddress& lhs,
830 Imm32 rhs, Label* label) {
831 cmp32(Operand(lhs), rhs);
832 j(cond, label);
833 }
834
835 void MacroAssembler::branch32(Condition cond, wasm::SymbolicAddress lhs,
836 Imm32 rhs, Label* label) {
837 cmpl(rhs, lhs);
838 j(cond, label);
839 }
840
841 void MacroAssembler::branch64(Condition cond, Register64 lhs, Imm64 val,
842 Label* success, Label* fail) {
843 branch64Impl(cond, lhs, val, success, fail);
844 }
845
846 void MacroAssembler::branch64(Condition cond, Register64 lhs, Register64 rhs,
847 Label* success, Label* fail) {
848 branch64Impl(cond, lhs, rhs, success, fail);
849 }
850
851 void MacroAssembler::branch64(Condition cond, const Address& lhs, Imm64 val,
852 Label* success, Label* fail) {
853 branch64Impl(cond, lhs, val, success, fail);
854 }
855
856 void MacroAssembler::branch64(Condition cond, const Address& lhs,
857 Register64 rhs, Label* success, Label* fail) {
858 branch64Impl(cond, lhs, rhs, success, fail);
859 }
860
861 void MacroAssembler::branch64(Condition cond, const Address& lhs,
862 const Address& rhs, Register scratch,
863 Label* label) {
864 MOZ_ASSERT(cond == Assembler::NotEqual || cond == Assembler::Equal,
865 "other condition codes not supported");
866 MOZ_ASSERT(lhs.base != scratch);
867 MOZ_ASSERT(rhs.base != scratch);
868
869 Label done;
870
871 load32(LowWord(rhs), scratch);
872 if (cond == Assembler::Equal) {
873 branch32(Assembler::NotEqual, LowWord(lhs), scratch, &done);
874 } else {
875 branch32(Assembler::NotEqual, LowWord(lhs), scratch, label);
876 }
877
878 load32(HighWord(rhs), scratch);
879 branch32(cond, HighWord(lhs), scratch, label);
880
881 bind(&done);
882 }
883
884 void MacroAssembler::branchTestPtr(Condition cond, Register lhs, ImmWord rhs,
885 Label* label) {
886 branchTestPtr(cond, lhs, Imm32(rhs.value), label);
887 }
888
889 void MacroAssembler::branchPtr(Condition cond, const AbsoluteAddress& lhs,
890 Register rhs, Label* label) {
891 branchPtrImpl(cond, lhs, rhs, label);
892 }
893
894 void MacroAssembler::branchPtr(Condition cond, const AbsoluteAddress& lhs,
895 ImmWord rhs, Label* label) {
896 branchPtrImpl(cond, lhs, rhs, label);
897 }
898
899 void MacroAssembler::branchPtr(Condition cond, wasm::SymbolicAddress lhs,
900 Register rhs, Label* label) {
901 cmpl(rhs, lhs);
902 j(cond, label);
903 }
904
905 void MacroAssembler::branchPrivatePtr(Condition cond, const Address& lhs,
906 Register rhs, Label* label) {
907 branchPtr(cond, lhs, rhs, label);
908 }
909
910 void MacroAssembler::branchTruncateFloat32ToPtr(FloatRegister src,
911 Register dest, Label* fail) {
912 branchTruncateFloat32ToInt32(src, dest, fail);
913 }
914
915 void MacroAssembler::branchTruncateFloat32MaybeModUint32(FloatRegister src,
916 Register dest,
917 Label* fail) {
918 branchTruncateFloat32ToInt32(src, dest, fail);
919 }
920
921 void MacroAssembler::branchTruncateFloat32ToInt32(FloatRegister src,
922 Register dest, Label* fail) {
923 vcvttss2si(src, dest);
924
925 // vcvttss2si returns 0x80000000 on failure. Test for it by
926 // subtracting 1 and testing overflow (this permits the use of a
927 // smaller immediate field).
928 cmp32(dest, Imm32(1));
929 j(Assembler::Overflow, fail);
930 }
931
932 void MacroAssembler::branchTruncateDoubleToPtr(FloatRegister src, Register dest,
933 Label* fail) {
934 branchTruncateDoubleToInt32(src, dest, fail);
935 }
936
937 void MacroAssembler::branchTruncateDoubleMaybeModUint32(FloatRegister src,
938 Register dest,
939 Label* fail) {
940 // TODO: X64 supports supports integers up till 64bits. Here we only support
941 // 32bits, before failing. Implementing this for x86 might give a x86 kraken
942 // win.
943 branchTruncateDoubleToInt32(src, dest, fail);
944 }
945
946 void MacroAssembler::branchTruncateDoubleToInt32(FloatRegister src,
947 Register dest, Label* fail) {
948 vcvttsd2si(src, dest);
949
950 // vcvttsd2si returns 0x80000000 on failure. Test for it by
951 // subtracting 1 and testing overflow (this permits the use of a
952 // smaller immediate field).
953 cmp32(dest, Imm32(1));
954 j(Assembler::Overflow, fail);
955 }
956
957 void MacroAssembler::branchInt64NotInPtrRange(Register64 src, Label* label) {
958 // The high-word needs to be either all zero or all one, depending on the MSB
959 // of the low-word.
960 push(src.low);
961 sarl(Imm32(31), src.low);
962 cmp32(src.low, src.high);
963 pop(src.low);
964 j(Assembler::NotEqual, label);
965 }
966
967 void MacroAssembler::branchUInt64NotInPtrRange(Register64 src, Label* label) {
968 // The low-word MSB and all bits in the high-word must be zero.
969 branchTest32(Assembler::Signed, src.low, src.low, label);
970 branchTest32(Assembler::NonZero, src.high, src.high, label);
971 }
972
973 void MacroAssembler::branchAdd64(Condition cond, Imm64 imm, Register64 dest,
974 Label* label) {
975 add64(imm, dest);
976 j(cond, label);
977 }
978
979 void MacroAssembler::branchTest32(Condition cond, const AbsoluteAddress& lhs,
980 Imm32 rhs, Label* label) {
981 test32(Operand(lhs), rhs);
982 j(cond, label);
983 }
984
985 void MacroAssembler::branchTest64(Condition cond, Register64 lhs,
986 Register64 rhs, Register temp, Label* success,
987 Label* fail) {
988 bool fallthrough = false;
989 Label fallthroughLabel;
990
991 if (!fail) {
992 fail = &fallthroughLabel;
993 fallthrough = true;
994 }
995
996 if (cond == Assembler::Zero || cond == Assembler::NonZero) {
997 if (lhs == rhs && temp != InvalidReg) {
998 movl(lhs.low, temp);
999 orl(lhs.high, temp);
1000 branchTest32(cond, temp, temp, success);
1001 } else if (cond == Assembler::Zero) {
1002 branchTest32(Assembler::NonZero, lhs.low, rhs.low, fail);
1003 branchTest32(Assembler::Zero, lhs.high, rhs.high, success);
1004 } else {
1005 branchTest32(Assembler::NonZero, lhs.low, rhs.low, success);
1006 branchTest32(Assembler::NonZero, lhs.high, rhs.high, success);
1007 }
1008 } else if (cond == Assembler::Signed || cond == Assembler::NotSigned) {
1009 branchTest32(cond, lhs.high, rhs.high, success);
1010 } else {
1011 MOZ_CRASH("Unsupported condition");
1012 }
1013
1014 if (fallthrough) {
1015 bind(fail);
1016 } else {
1017 jump(fail);
1018 }
1019 }
1020
1021 void MacroAssembler::branchTest64(Condition cond, Register64 lhs, Imm64 rhs,
1022 Label* success, Label* fail) {
1023 bool fallthrough = false;
1024 Label fallthroughLabel;
1025
1026 if (!fail) {
1027 fail = &fallthroughLabel;
1028 fallthrough = true;
1029 }
1030
1031 if (cond == Assembler::Zero || cond == Assembler::NonZero) {
1032 if (rhs.hi().value == 0) {
1033 branchTest32(cond, lhs.low, rhs.low(), success);
1034 } else if (rhs.low().value == 0) {
1035 branchTest32(cond, lhs.high, rhs.hi(), success);
1036 } else if (cond == Assembler::Zero) {
1037 branchTest32(Assembler::NonZero, lhs.low, rhs.low(), fail);
1038 branchTest32(Assembler::Zero, lhs.high, rhs.hi(), success);
1039 } else {
1040 branchTest32(Assembler::NonZero, lhs.low, rhs.low(), success);
1041 branchTest32(Assembler::NonZero, lhs.high, rhs.hi(), success);
1042 }
1043 } else {
1044 MOZ_CRASH("Unsupported condition");
1045 }
1046
1047 if (fallthrough) {
1048 bind(fail);
1049 } else {
1050 jump(fail);
1051 }
1052 }
1053
1054 void MacroAssembler::branchTestBooleanTruthy(bool truthy,
1055 const ValueOperand& value,
1056 Label* label) {
1057 test32(value.payloadReg(), value.payloadReg());
1058 j(truthy ? NonZero : Zero, label);
1059 }
1060
1061 void MacroAssembler::branchTestMagic(Condition cond, const Address& valaddr,
1062 JSWhyMagic why, Label* label) {
1063 MOZ_ASSERT(cond == Assembler::Equal || cond == Assembler::NotEqual);
1064
1065 Label notMagic;
1066 if (cond == Assembler::Equal) {
1067 branchTestMagic(Assembler::NotEqual, valaddr, &notMagic);
1068 } else {
1069 branchTestMagic(Assembler::NotEqual, valaddr, label);
1070 }
1071
1072 branch32(cond, ToPayload(valaddr), Imm32(why), label);
1073 bind(&notMagic);
1074 }
1075
1076 template <typename T>
1077 void MacroAssembler::branchTestValue(Condition cond, const T& lhs,
1078 const ValueOperand& rhs, Label* label) {
1079 MOZ_ASSERT(cond == Assembler::Equal || cond == Assembler::NotEqual);
1080
1081 Label notSameValue;
1082 if (cond == Assembler::Equal) {
1083 branch32(Assembler::NotEqual, ToType(lhs), rhs.typeReg(), &notSameValue);
1084 } else {
1085 branch32(Assembler::NotEqual, ToType(lhs), rhs.typeReg(), label);
1086 }
1087
1088 branch32(cond, ToPayload(lhs), rhs.payloadReg(), label);
1089 bind(&notSameValue);
1090 }
1091
1092 void MacroAssembler::branchToComputedAddress(const BaseIndex& addr) {
1093 jmp(Operand(addr));
1094 }
1095
1096 void MacroAssembler::cmp32MovePtr(Condition cond, Register lhs, Imm32 rhs,
1097 Register src, Register dest) {
1098 cmp32(lhs, rhs);
1099 cmovCCl(cond, Operand(src), dest);
1100 }
1101
1102 void MacroAssembler::cmp32LoadPtr(Condition cond, const Address& lhs, Imm32 rhs,
1103 const Address& src, Register dest) {
1104 cmp32(lhs, rhs);
1105 cmovCCl(cond, Operand(src), dest);
1106 }
1107
1108 void MacroAssembler::cmpPtrMovePtr(Condition cond, Register lhs, Imm32 rhs,
1109 Register src, Register dest) {
1110 cmp32MovePtr(cond, lhs, rhs, src, dest);
1111 }
1112
1113 void MacroAssembler::cmpPtrMovePtr(Condition cond, Register lhs, Register rhs,
1114 Register src, Register dest) {
1115 cmp32Move32(cond, lhs, rhs, src, dest);
1116 }
1117
1118 void MacroAssembler::cmpPtrMovePtr(Condition cond, Register lhs,
1119 const Address& rhs, Register src,
1120 Register dest) {
1121 cmp32Move32(cond, lhs, rhs, src, dest);
1122 }
1123
1124 void MacroAssembler::test32LoadPtr(Condition cond, const Address& addr,
1125 Imm32 mask, const Address& src,
1126 Register dest) {
1127 MOZ_ASSERT(cond == Assembler::Zero || cond == Assembler::NonZero);
1128 test32(addr, mask);
1129 cmovCCl(cond, Operand(src), dest);
1130 }
1131
1132 void MacroAssembler::test32MovePtr(Condition cond, Register operand, Imm32 mask,
1133 Register src, Register dest) {
1134 MOZ_ASSERT(cond == Assembler::Zero || cond == Assembler::NonZero);
1135 test32(operand, mask);
1136 cmovCCl(cond, Operand(src), dest);
1137 }
1138
1139 void MacroAssembler::test32MovePtr(Condition cond, const Address& addr,
1140 Imm32 mask, Register src, Register dest) {
1141 MOZ_ASSERT(cond == Assembler::Zero || cond == Assembler::NonZero);
1142 test32(addr, mask);
1143 cmovCCl(cond, Operand(src), dest);
1144 }
1145
1146 void MacroAssembler::spectreMovePtr(Condition cond, Register src,
1147 Register dest) {
1148 cmovCCl(cond, Operand(src), dest);
1149 }
1150
1151 void MacroAssembler::spectreBoundsCheck32(Register index, const Operand& length,
1152 Register maybeScratch,
1153 Label* failure) {
1154 Label failurePopValue;
1155 bool pushedValue = false;
1156 if (JitOptions.spectreIndexMasking) {
1157 if (maybeScratch == InvalidReg) {
1158 push(Imm32(0));
1159 pushedValue = true;
1160 } else {
1161 move32(Imm32(0), maybeScratch);
1162 }
1163 }
1164
1165 cmp32(index, length);
1166 j(Assembler::AboveOrEqual, pushedValue ? &failurePopValue : failure);
1167
1168 if (JitOptions.spectreIndexMasking) {
1169 if (maybeScratch == InvalidReg) {
1170 Label done;
1171 cmovCCl(Assembler::AboveOrEqual, Operand(StackPointer, 0), index);
1172 lea(Operand(StackPointer, sizeof(void*)), StackPointer);
1173 jump(&done);
1174
1175 bind(&failurePopValue);
1176 lea(Operand(StackPointer, sizeof(void*)), StackPointer);
1177 jump(failure);
1178
1179 bind(&done);
1180 } else {
1181 cmovCCl(Assembler::AboveOrEqual, maybeScratch, index);
1182 }
1183 }
1184 }
1185
1186 void MacroAssembler::spectreBoundsCheck32(Register index, Register length,
1187 Register maybeScratch,
1188 Label* failure) {
1189 MOZ_ASSERT(length != maybeScratch);
1190 MOZ_ASSERT(index != maybeScratch);
1191
1192 spectreBoundsCheck32(index, Operand(length), maybeScratch, failure);
1193 }
1194
1195 void MacroAssembler::spectreBoundsCheck32(Register index, const Address& length,
1196 Register maybeScratch,
1197 Label* failure) {
1198 MOZ_ASSERT(index != length.base);
1199 MOZ_ASSERT(length.base != maybeScratch);
1200 MOZ_ASSERT(index != maybeScratch);
1201
1202 spectreBoundsCheck32(index, Operand(length), maybeScratch, failure);
1203 }
1204
1205 void MacroAssembler::spectreBoundsCheckPtr(Register index, Register length,
1206 Register maybeScratch,
1207 Label* failure) {
1208 spectreBoundsCheck32(index, length, maybeScratch, failure);
1209 }
1210
1211 void MacroAssembler::spectreBoundsCheckPtr(Register index,
1212 const Address& length,
1213 Register maybeScratch,
1214 Label* failure) {
1215 spectreBoundsCheck32(index, length, maybeScratch, failure);
1216 }
1217
1218 // ========================================================================
1219 // SIMD
1220
1221 void MacroAssembler::extractLaneInt64x2(uint32_t lane, FloatRegister src,
1222 Register64 dest) {
1223 if (lane == 0) {
1224 vmovd(src, dest.low);
1225 } else {
1226 vpextrd(2 * lane, src, dest.low);
1227 }
1228 vpextrd(2 * lane + 1, src, dest.high);
1229 }
1230
1231 void MacroAssembler::replaceLaneInt64x2(unsigned lane, Register64 rhs,
1232 FloatRegister lhsDest) {
1233 vpinsrd(2 * lane, rhs.low, lhsDest, lhsDest);
1234 vpinsrd(2 * lane + 1, rhs.high, lhsDest, lhsDest);
1235 }
1236
1237 void MacroAssembler::replaceLaneInt64x2(unsigned lane, FloatRegister lhs,
1238 Register64 rhs, FloatRegister dest) {
1239 vpinsrd(2 * lane, rhs.low, lhs, dest);
1240 vpinsrd(2 * lane + 1, rhs.high, dest, dest);
1241 }
1242
1243 void MacroAssembler::splatX2(Register64 src, FloatRegister dest) {
1244 vmovd(src.low, dest);
1245 vpinsrd(1, src.high, dest, dest);
1246 vpunpcklqdq(dest, dest, dest);
1247 }
1248
1249 // ========================================================================
1250 // Truncate floating point.
1251
1252 void MacroAssembler::truncateFloat32ToUInt64(Address src, Address dest,
1253 Register temp,
1254 FloatRegister floatTemp) {
1255 Label done;
1256
1257 loadFloat32(src, floatTemp);
1258
1259 truncateFloat32ToInt64(src, dest, temp);
1260
1261 // For unsigned conversion the case of [INT64, UINT64] needs to get handle
1262 // seperately.
1263 load32(HighWord(dest), temp);
1264 branch32(Assembler::Condition::NotSigned, temp, Imm32(0), &done);
1265
1266 // Move the value inside INT64 range.
1267 storeFloat32(floatTemp, dest);
1268 loadConstantFloat32(double(int64_t(0x8000000000000000)), floatTemp);
1269 vaddss(Operand(dest), floatTemp, floatTemp);
1270 storeFloat32(floatTemp, dest);
1271 truncateFloat32ToInt64(dest, dest, temp);
1272
1273 load32(HighWord(dest), temp);
1274 orl(Imm32(0x80000000), temp);
1275 store32(temp, HighWord(dest));
1276
1277 bind(&done);
1278 }
1279
1280 void MacroAssembler::truncateDoubleToUInt64(Address src, Address dest,
1281 Register temp,
1282 FloatRegister floatTemp) {
1283 Label done;
1284
1285 loadDouble(src, floatTemp);
1286
1287 truncateDoubleToInt64(src, dest, temp);
1288
1289 // For unsigned conversion the case of [INT64, UINT64] needs to get handle
1290 // seperately.
1291 load32(HighWord(dest), temp);
1292 branch32(Assembler::Condition::NotSigned, temp, Imm32(0), &done);
1293
1294 // Move the value inside INT64 range.
1295 storeDouble(floatTemp, dest);
1296 loadConstantDouble(double(int64_t(0x8000000000000000)), floatTemp);
1297 vaddsd(Operand(dest), floatTemp, floatTemp);
1298 storeDouble(floatTemp, dest);
1299 truncateDoubleToInt64(dest, dest, temp);
1300
1301 load32(HighWord(dest), temp);
1302 orl(Imm32(0x80000000), temp);
1303 store32(temp, HighWord(dest));
1304
1305 bind(&done);
1306 }
1307
1308 template <typename T>
1309 void MacroAssemblerX86::fallibleUnboxPtrImpl(const T& src, Register dest,
1310 JSValueType type, Label* fail) {
1311 switch (type) {
1312 case JSVAL_TYPE_OBJECT:
1313 asMasm().branchTestObject(Assembler::NotEqual, src, fail);
1314 break;
1315 case JSVAL_TYPE_STRING:
1316 asMasm().branchTestString(Assembler::NotEqual, src, fail);
1317 break;
1318 case JSVAL_TYPE_SYMBOL:
1319 asMasm().branchTestSymbol(Assembler::NotEqual, src, fail);
1320 break;
1321 case JSVAL_TYPE_BIGINT:
1322 asMasm().branchTestBigInt(Assembler::NotEqual, src, fail);
1323 break;
1324 default:
1325 MOZ_CRASH("Unexpected type");
1326 }
1327 unboxNonDouble(src, dest, type);
1328 }
1329
1330 void MacroAssembler::fallibleUnboxPtr(const ValueOperand& src, Register dest,
1331 JSValueType type, Label* fail) {
1332 fallibleUnboxPtrImpl(src, dest, type, fail);
1333 }
1334
1335 void MacroAssembler::fallibleUnboxPtr(const Address& src, Register dest,
1336 JSValueType type, Label* fail) {
1337 fallibleUnboxPtrImpl(src, dest, type, fail);
1338 }
1339
1340 void MacroAssembler::fallibleUnboxPtr(const BaseIndex& src, Register dest,
1341 JSValueType type, Label* fail) {
1342 fallibleUnboxPtrImpl(src, dest, type, fail);
1343 }
1344
1345 //}}} check_macroassembler_style
1346 // ===============================================================
1347
1348 // Note: this function clobbers the source register.
1349 void MacroAssemblerX86::convertUInt32ToDouble(Register src,
1350 FloatRegister dest) {
1351 // src is [0, 2^32-1]
1352 subl(Imm32(0x80000000), src);
1353
1354 // Now src is [-2^31, 2^31-1] - int range, but not the same value.
1355 convertInt32ToDouble(src, dest);
1356
1357 // dest is now a double with the int range.
1358 // correct the double value by adding 0x80000000.
1359 asMasm().addConstantDouble(2147483648.0, dest);
1360 }
1361
1362 // Note: this function clobbers the source register.
1363 void MacroAssemblerX86::convertUInt32ToFloat32(Register src,
1364 FloatRegister dest) {
1365 convertUInt32ToDouble(src, dest);
1366 convertDoubleToFloat32(dest, dest);
1367 }
1368
1369 void MacroAssemblerX86::unboxValue(const ValueOperand& src, AnyRegister dest,
1370 JSValueType) {
1371 if (dest.isFloat()) {
1372 Label notInt32, end;
1373 asMasm().branchTestInt32(Assembler::NotEqual, src, &notInt32);
1374 convertInt32ToDouble(src.payloadReg(), dest.fpu());
1375 jump(&end);
1376 bind(&notInt32);
1377 unboxDouble(src, dest.fpu());
1378 bind(&end);
1379 } else {
1380 if (src.payloadReg() != dest.gpr()) {
1381 movl(src.payloadReg(), dest.gpr());
1382 }
1383 }
1384 }
1385
1386 template <typename T>
1387 void MacroAssemblerX86::loadInt32OrDouble(const T& src, FloatRegister dest) {
1388 Label notInt32, end;
1389 asMasm().branchTestInt32(Assembler::NotEqual, src, &notInt32);
1390 convertInt32ToDouble(ToPayload(src), dest);
1391 jump(&end);
1392 bind(&notInt32);
1393 loadDouble(src, dest);
1394 bind(&end);
1395 }
1396
1397 template <typename T>
1398 void MacroAssemblerX86::loadUnboxedValue(const T& src, MIRType type,
1399 AnyRegister dest) {
1400 if (dest.isFloat()) {
1401 loadInt32OrDouble(src, dest.fpu());
1402 } else {
1403 movl(Operand(src), dest.gpr());
1404 }
1405 }
1406
1407 template <typename T1, typename T2>
1408 void MacroAssemblerX86::cmp64SetAliased(Condition cond, T1 lhs, T2 rhs,
1409 Register dest) {
1410 auto& masm = asMasm();
1411
1412 Label success, done;
1413
1414 masm.branch64(cond, lhs, rhs, &success);
1415 masm.move32(Imm32(0), dest);
1416 masm.jump(&done);
1417 masm.bind(&success);
1418 masm.move32(Imm32(1), dest);
1419 masm.bind(&done);
1420 }
1421
1422 template <typename T1, typename T2>
1423 void MacroAssemblerX86::cmp64SetNonAliased(Condition cond, T1 lhs, T2 rhs,
1424 Register dest) {
1425 auto& masm = asMasm();
1426
1427 Label done;
1428
1429 masm.move32(Imm32(1), dest);
1430 masm.branch64(cond, lhs, rhs, &done);
1431 masm.move32(Imm32(0), dest);
1432 masm.bind(&done);
1433 }
1434
1435 template <typename T1, typename T2>
1436 void MacroAssemblerX86::branch64Impl(Condition cond, T1 lhs, T2 rhs,
1437 Label* success, Label* fail) {
1438 auto& masm = asMasm();
1439
1440 auto words = [](auto operand) {
1441 using Operand = decltype(operand);
1442 if constexpr (std::is_same_v<Operand, Imm64>) {
1443 return std::pair{operand.hi(), operand.low()};
1444 } else if constexpr (std::is_same_v<Operand, Register64>) {
1445 return std::pair{operand.high, operand.low};
1446 } else {
1447 return std::pair{HighWord(operand), LowWord(operand)};
1448 }
1449 };
1450
1451 auto [lhsHigh, lhsLow] = words(lhs);
1452 auto [rhsHigh, rhsLow] = words(rhs);
1453
1454 bool fallthrough = false;
1455 Label fallthroughLabel;
1456
1457 if (!fail) {
1458 fail = &fallthroughLabel;
1459 fallthrough = true;
1460 }
1461
1462 switch (cond) {
1463 case Assembler::Equal:
1464 masm.branch32(Assembler::NotEqual, lhsLow, rhsLow, fail);
1465 masm.branch32(Assembler::Equal, lhsHigh, rhsHigh, success);
1466 break;
1467 case Assembler::NotEqual:
1468 masm.branch32(Assembler::NotEqual, lhsLow, rhsLow, success);
1469 masm.branch32(Assembler::NotEqual, lhsHigh, rhsHigh, success);
1470 break;
1471 case Assembler::LessThan:
1472 case Assembler::LessThanOrEqual:
1473 case Assembler::GreaterThan:
1474 case Assembler::GreaterThanOrEqual:
1475 case Assembler::Below:
1476 case Assembler::BelowOrEqual:
1477 case Assembler::Above:
1478 case Assembler::AboveOrEqual: {
1479 Assembler::Condition cond1 = Assembler::ConditionWithoutEqual(cond);
1480 Assembler::Condition cond2 =
1481 Assembler::ConditionWithoutEqual(Assembler::InvertCondition(cond));
1482 Assembler::Condition cond3 = Assembler::UnsignedCondition(cond);
1483
1484 cmp32(lhsHigh, rhsHigh);
1485 j(cond1, success);
1486 j(cond2, fail);
1487 cmp32(lhsLow, rhsLow);
1488 j(cond3, success);
1489 break;
1490 }
1491 default:
1492 MOZ_CRASH("Condition code not supported");
1493 break;
1494 }
1495
1496 if (fallthrough) {
1497 bind(fail);
1498 } else {
1499 jump(fail);
1500 }
1501 }
1502
1503 } // namespace jit
1504 } // namespace js
1505
1506 #endif /* jit_x86_MacroAssembler_x86_inl_h */