Debian Codegraph @

js/src/jit/arm64/vixl/MacroAssembler-vixl.cpp

raw
1 // Copyright 2015, ARM Limited
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "jit/arm64/vixl/MacroAssembler-vixl.h"
28
29 #include <ctype.h>
30 #include <limits>
31
32 #include "jit/RegisterSets.h"
33
34 namespace vixl {
35
36 MacroAssembler::MacroAssembler()
37 : js::jit::Assembler(),
38 sp_(x28),
39 tmp_list_(ip0, ip1),
40 fptmp_list_(d31)
41 {
42 }
43
44
45 void MacroAssembler::FinalizeCode() {
46 Assembler::FinalizeCode();
47 }
48
49
50 int MacroAssembler::MoveImmediateHelper(MacroAssembler* masm,
51 const Register &rd,
52 uint64_t imm) {
53 bool emit_code = (masm != NULL);
54 VIXL_ASSERT(IsUint32(imm) || IsInt32(imm) || rd.Is64Bits());
55 // The worst case for size is mov 64-bit immediate to sp:
56 // * up to 4 instructions to materialise the constant
57 // * 1 instruction to move to sp
58 MacroEmissionCheckScope guard(masm);
59
60 // Immediates on Aarch64 can be produced using an initial value, and zero to
61 // three move keep operations.
62 //
63 // Initial values can be generated with:
64 // 1. 64-bit move zero (movz).
65 // 2. 32-bit move inverted (movn).
66 // 3. 64-bit move inverted.
67 // 4. 32-bit orr immediate.
68 // 5. 64-bit orr immediate.
69 // Move-keep may then be used to modify each of the 16-bit half words.
70 //
71 // The code below supports all five initial value generators, and
72 // applying move-keep operations to move-zero and move-inverted initial
73 // values.
74
75 // Try to move the immediate in one instruction, and if that fails, switch to
76 // using multiple instructions.
77 if (OneInstrMoveImmediateHelper(masm, rd, imm)) {
78 return 1;
79 } else {
80 int instruction_count = 0;
81 unsigned reg_size = rd.size();
82
83 // Generic immediate case. Imm will be represented by
84 // [imm3, imm2, imm1, imm0], where each imm is 16 bits.
85 // A move-zero or move-inverted is generated for the first non-zero or
86 // non-0xffff immX, and a move-keep for subsequent non-zero immX.
87
88 uint64_t ignored_halfword = 0;
89 bool invert_move = false;
90 // If the number of 0xffff halfwords is greater than the number of 0x0000
91 // halfwords, it's more efficient to use move-inverted.
92 if (CountClearHalfWords(~imm, reg_size) >
93 CountClearHalfWords(imm, reg_size)) {
94 ignored_halfword = 0xffff;
95 invert_move = true;
96 }
97
98 // Mov instructions can't move values into the stack pointer, so set up a
99 // temporary register, if needed.
100 UseScratchRegisterScope temps;
101 Register temp;
102 if (emit_code) {
103 temps.Open(masm);
104 temp = rd.IsSP() ? temps.AcquireSameSizeAs(rd) : rd;
105 }
106
107 // Iterate through the halfwords. Use movn/movz for the first non-ignored
108 // halfword, and movk for subsequent halfwords.
109 VIXL_ASSERT((reg_size % 16) == 0);
110 bool first_mov_done = false;
111 for (unsigned i = 0; i < (temp.size() / 16); i++) {
112 uint64_t imm16 = (imm >> (16 * i)) & 0xffff;
113 if (imm16 != ignored_halfword) {
114 if (!first_mov_done) {
115 if (invert_move) {
116 if (emit_code) masm->movn(temp, ~imm16 & 0xffff, 16 * i);
117 instruction_count++;
118 } else {
119 if (emit_code) masm->movz(temp, imm16, 16 * i);
120 instruction_count++;
121 }
122 first_mov_done = true;
123 } else {
124 // Construct a wider constant.
125 if (emit_code) masm->movk(temp, imm16, 16 * i);
126 instruction_count++;
127 }
128 }
129 }
130
131 VIXL_ASSERT(first_mov_done);
132
133 // Move the temporary if the original destination register was the stack
134 // pointer.
135 if (rd.IsSP()) {
136 if (emit_code) masm->mov(rd, temp);
137 instruction_count++;
138 }
139 return instruction_count;
140 }
141 }
142
143
144 bool MacroAssembler::OneInstrMoveImmediateHelper(MacroAssembler* masm,
145 const Register& dst,
146 int64_t imm) {
147 bool emit_code = masm != NULL;
148 unsigned n, imm_s, imm_r;
149 int reg_size = dst.size();
150
151 if (IsImmMovz(imm, reg_size) && !dst.IsSP()) {
152 // Immediate can be represented in a move zero instruction. Movz can't write
153 // to the stack pointer.
154 if (emit_code) {
155 masm->movz(dst, imm);
156 }
157 return true;
158 } else if (IsImmMovn(imm, reg_size) && !dst.IsSP()) {
159 // Immediate can be represented in a move negative instruction. Movn can't
160 // write to the stack pointer.
161 if (emit_code) {
162 masm->movn(dst, dst.Is64Bits() ? ~imm : (~imm & kWRegMask));
163 }
164 return true;
165 } else if (IsImmLogical(imm, reg_size, &n, &imm_s, &imm_r)) {
166 // Immediate can be represented in a logical orr instruction.
167 VIXL_ASSERT(!dst.IsZero());
168 if (emit_code) {
169 masm->LogicalImmediate(
170 dst, AppropriateZeroRegFor(dst), n, imm_s, imm_r, ORR);
171 }
172 return true;
173 }
174 return false;
175 }
176
177
178 void MacroAssembler::B(Label* label, BranchType type, Register reg, int bit) {
179 VIXL_ASSERT((reg.Is(NoReg) || (type >= kBranchTypeFirstUsingReg)) &&
180 ((bit == -1) || (type >= kBranchTypeFirstUsingBit)));
181 if (kBranchTypeFirstCondition <= type && type <= kBranchTypeLastCondition) {
182 B(static_cast<Condition>(type), label);
183 } else {
184 switch (type) {
185 case always: B(label); break;
186 case never: break;
187 case reg_zero: Cbz(reg, label); break;
188 case reg_not_zero: Cbnz(reg, label); break;
189 case reg_bit_clear: Tbz(reg, bit, label); break;
190 case reg_bit_set: Tbnz(reg, bit, label); break;
191 default:
192 VIXL_UNREACHABLE();
193 }
194 }
195 }
196
197
198 void MacroAssembler::B(Label* label) {
199 SingleEmissionCheckScope guard(this);
200 b(label);
201 }
202
203
204 void MacroAssembler::B(Label* label, Condition cond) {
205 VIXL_ASSERT((cond != al) && (cond != nv));
206 EmissionCheckScope guard(this, 2 * kInstructionSize);
207
208 if (label->bound() && LabelIsOutOfRange(label, CondBranchType)) {
209 Label done;
210 b(&done, InvertCondition(cond));
211 b(label);
212 bind(&done);
213 } else {
214 b(label, cond);
215 }
216 }
217
218
219 void MacroAssembler::Cbnz(const Register& rt, Label* label) {
220 VIXL_ASSERT(!rt.IsZero());
221 EmissionCheckScope guard(this, 2 * kInstructionSize);
222
223 if (label->bound() && LabelIsOutOfRange(label, CondBranchType)) {
224 Label done;
225 cbz(rt, &done);
226 b(label);
227 bind(&done);
228 } else {
229 cbnz(rt, label);
230 }
231 }
232
233
234 void MacroAssembler::Cbz(const Register& rt, Label* label) {
235 VIXL_ASSERT(!rt.IsZero());
236 EmissionCheckScope guard(this, 2 * kInstructionSize);
237
238 if (label->bound() && LabelIsOutOfRange(label, CondBranchType)) {
239 Label done;
240 cbnz(rt, &done);
241 b(label);
242 bind(&done);
243 } else {
244 cbz(rt, label);
245 }
246 }
247
248
249 void MacroAssembler::Tbnz(const Register& rt, unsigned bit_pos, Label* label) {
250 VIXL_ASSERT(!rt.IsZero());
251 EmissionCheckScope guard(this, 2 * kInstructionSize);
252
253 if (label->bound() && LabelIsOutOfRange(label, TestBranchType)) {
254 Label done;
255 tbz(rt, bit_pos, &done);
256 b(label);
257 bind(&done);
258 } else {
259 tbnz(rt, bit_pos, label);
260 }
261 }
262
263
264 void MacroAssembler::Tbz(const Register& rt, unsigned bit_pos, Label* label) {
265 VIXL_ASSERT(!rt.IsZero());
266 EmissionCheckScope guard(this, 2 * kInstructionSize);
267
268 if (label->bound() && LabelIsOutOfRange(label, TestBranchType)) {
269 Label done;
270 tbnz(rt, bit_pos, &done);
271 b(label);
272 bind(&done);
273 } else {
274 tbz(rt, bit_pos, label);
275 }
276 }
277
278
279 void MacroAssembler::And(const Register& rd,
280 const Register& rn,
281 const Operand& operand) {
282 LogicalMacro(rd, rn, operand, AND);
283 }
284
285
286 void MacroAssembler::Ands(const Register& rd,
287 const Register& rn,
288 const Operand& operand) {
289 LogicalMacro(rd, rn, operand, ANDS);
290 }
291
292
293 void MacroAssembler::Tst(const Register& rn,
294 const Operand& operand) {
295 Ands(AppropriateZeroRegFor(rn), rn, operand);
296 }
297
298
299 void MacroAssembler::Bic(const Register& rd,
300 const Register& rn,
301 const Operand& operand) {
302 LogicalMacro(rd, rn, operand, BIC);
303 }
304
305
306 void MacroAssembler::Bics(const Register& rd,
307 const Register& rn,
308 const Operand& operand) {
309 LogicalMacro(rd, rn, operand, BICS);
310 }
311
312
313 void MacroAssembler::Orr(const Register& rd,
314 const Register& rn,
315 const Operand& operand) {
316 LogicalMacro(rd, rn, operand, ORR);
317 }
318
319
320 void MacroAssembler::Orn(const Register& rd,
321 const Register& rn,
322 const Operand& operand) {
323 LogicalMacro(rd, rn, operand, ORN);
324 }
325
326
327 void MacroAssembler::Eor(const Register& rd,
328 const Register& rn,
329 const Operand& operand) {
330 LogicalMacro(rd, rn, operand, EOR);
331 }
332
333
334 void MacroAssembler::Eon(const Register& rd,
335 const Register& rn,
336 const Operand& operand) {
337 LogicalMacro(rd, rn, operand, EON);
338 }
339
340
341 void MacroAssembler::LogicalMacro(const Register& rd,
342 const Register& rn,
343 const Operand& operand,
344 LogicalOp op) {
345 // The worst case for size is logical immediate to sp:
346 // * up to 4 instructions to materialise the constant
347 // * 1 instruction to do the operation
348 // * 1 instruction to move to sp
349 MacroEmissionCheckScope guard(this);
350 UseScratchRegisterScope temps(this);
351
352 if (operand.IsImmediate()) {
353 int64_t immediate = operand.immediate();
354 unsigned reg_size = rd.size();
355
356 // If the operation is NOT, invert the operation and immediate.
357 if ((op & NOT) == NOT) {
358 op = static_cast<LogicalOp>(op & ~NOT);
359 immediate = ~immediate;
360 }
361
362 // Ignore the top 32 bits of an immediate if we're moving to a W register.
363 if (rd.Is32Bits()) {
364 // Check that the top 32 bits are consistent.
365 VIXL_ASSERT(((immediate >> kWRegSize) == 0) ||
366 ((immediate >> kWRegSize) == -1));
367 immediate &= kWRegMask;
368 }
369
370 VIXL_ASSERT(rd.Is64Bits() || IsUint32(immediate));
371
372 // Special cases for all set or all clear immediates.
373 if (immediate == 0) {
374 switch (op) {
375 case AND:
376 Mov(rd, 0);
377 return;
378 case ORR:
379 VIXL_FALLTHROUGH();
380 case EOR:
381 Mov(rd, rn);
382 return;
383 case ANDS:
384 VIXL_FALLTHROUGH();
385 case BICS:
386 break;
387 default:
388 VIXL_UNREACHABLE();
389 }
390 } else if ((rd.Is64Bits() && (immediate == -1)) ||
391 (rd.Is32Bits() && (immediate == 0xffffffff))) {
392 switch (op) {
393 case AND:
394 Mov(rd, rn);
395 return;
396 case ORR:
397 Mov(rd, immediate);
398 return;
399 case EOR:
400 Mvn(rd, rn);
401 return;
402 case ANDS:
403 VIXL_FALLTHROUGH();
404 case BICS:
405 break;
406 default:
407 VIXL_UNREACHABLE();
408 }
409 }
410
411 unsigned n, imm_s, imm_r;
412 if (IsImmLogical(immediate, reg_size, &n, &imm_s, &imm_r)) {
413 // Immediate can be encoded in the instruction.
414 LogicalImmediate(rd, rn, n, imm_s, imm_r, op);
415 } else {
416 // Immediate can't be encoded: synthesize using move immediate.
417 Register temp = temps.AcquireSameSizeAs(rn);
418
419 // If the left-hand input is the stack pointer, we can't pre-shift the
420 // immediate, as the encoding won't allow the subsequent post shift.
421 PreShiftImmMode mode = rn.IsSP() ? kNoShift : kAnyShift;
422 Operand imm_operand = MoveImmediateForShiftedOp(temp, immediate, mode);
423
424 // VIXL can acquire temp registers. Assert that the caller is aware.
425 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn));
426 VIXL_ASSERT(!temp.Is(operand.maybeReg()));
427
428 if (rd.Is(sp)) {
429 // If rd is the stack pointer we cannot use it as the destination
430 // register so we use the temp register as an intermediate again.
431 Logical(temp, rn, imm_operand, op);
432 Mov(sp, temp);
433 } else {
434 Logical(rd, rn, imm_operand, op);
435 }
436 }
437 } else if (operand.IsExtendedRegister()) {
438 VIXL_ASSERT(operand.reg().size() <= rd.size());
439 // Add/sub extended supports shift <= 4. We want to support exactly the
440 // same modes here.
441 VIXL_ASSERT(operand.shift_amount() <= 4);
442 VIXL_ASSERT(operand.reg().Is64Bits() ||
443 ((operand.extend() != UXTX) && (operand.extend() != SXTX)));
444
445 temps.Exclude(operand.reg());
446 Register temp = temps.AcquireSameSizeAs(rn);
447
448 // VIXL can acquire temp registers. Assert that the caller is aware.
449 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn));
450 VIXL_ASSERT(!temp.Is(operand.maybeReg()));
451
452 EmitExtendShift(temp, operand.reg(), operand.extend(),
453 operand.shift_amount());
454 Logical(rd, rn, Operand(temp), op);
455 } else {
456 // The operand can be encoded in the instruction.
457 VIXL_ASSERT(operand.IsShiftedRegister());
458 Logical(rd, rn, operand, op);
459 }
460 }
461
462
463 void MacroAssembler::Mov(const Register& rd,
464 const Operand& operand,
465 DiscardMoveMode discard_mode) {
466 // The worst case for size is mov immediate with up to 4 instructions.
467 MacroEmissionCheckScope guard(this);
468
469 if (operand.IsImmediate()) {
470 // Call the macro assembler for generic immediates.
471 Mov(rd, operand.immediate());
472 } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) {
473 // Emit a shift instruction if moving a shifted register. This operation
474 // could also be achieved using an orr instruction (like orn used by Mvn),
475 // but using a shift instruction makes the disassembly clearer.
476 EmitShift(rd, operand.reg(), operand.shift(), operand.shift_amount());
477 } else if (operand.IsExtendedRegister()) {
478 // Emit an extend instruction if moving an extended register. This handles
479 // extend with post-shift operations, too.
480 EmitExtendShift(rd, operand.reg(), operand.extend(),
481 operand.shift_amount());
482 } else {
483 // Otherwise, emit a register move only if the registers are distinct, or
484 // if they are not X registers.
485 //
486 // Note that mov(w0, w0) is not a no-op because it clears the top word of
487 // x0. A flag is provided (kDiscardForSameWReg) if a move between the same W
488 // registers is not required to clear the top word of the X register. In
489 // this case, the instruction is discarded.
490 //
491 // If the sp is an operand, add #0 is emitted, otherwise, orr #0.
492 if (!rd.Is(operand.reg()) || (rd.Is32Bits() &&
493 (discard_mode == kDontDiscardForSameWReg))) {
494 mov(rd, operand.reg());
495 }
496 }
497 }
498
499
500 void MacroAssembler::Movi16bitHelper(const VRegister& vd, uint64_t imm) {
501 VIXL_ASSERT(IsUint16(imm));
502 int byte1 = (imm & 0xff);
503 int byte2 = ((imm >> 8) & 0xff);
504 if (byte1 == byte2) {
505 movi(vd.Is64Bits() ? vd.V8B() : vd.V16B(), byte1);
506 } else if (byte1 == 0) {
507 movi(vd, byte2, LSL, 8);
508 } else if (byte2 == 0) {
509 movi(vd, byte1);
510 } else if (byte1 == 0xff) {
511 mvni(vd, ~byte2 & 0xff, LSL, 8);
512 } else if (byte2 == 0xff) {
513 mvni(vd, ~byte1 & 0xff);
514 } else {
515 UseScratchRegisterScope temps(this);
516 Register temp = temps.AcquireW();
517 movz(temp, imm);
518 dup(vd, temp);
519 }
520 }
521
522
523 void MacroAssembler::Movi32bitHelper(const VRegister& vd, uint64_t imm) {
524 VIXL_ASSERT(IsUint32(imm));
525
526 uint8_t bytes[sizeof(imm)];
527 memcpy(bytes, &imm, sizeof(imm));
528
529 // All bytes are either 0x00 or 0xff.
530 {
531 bool all0orff = true;
532 for (int i = 0; i < 4; ++i) {
533 if ((bytes[i] != 0) && (bytes[i] != 0xff)) {
534 all0orff = false;
535 break;
536 }
537 }
538
539 if (all0orff == true) {
540 movi(vd.Is64Bits() ? vd.V1D() : vd.V2D(), ((imm << 32) | imm));
541 return;
542 }
543 }
544
545 // Of the 4 bytes, only one byte is non-zero.
546 for (int i = 0; i < 4; i++) {
547 if ((imm & (0xff << (i * 8))) == imm) {
548 movi(vd, bytes[i], LSL, i * 8);
549 return;
550 }
551 }
552
553 // Of the 4 bytes, only one byte is not 0xff.
554 for (int i = 0; i < 4; i++) {
555 uint32_t mask = ~(0xff << (i * 8));
556 if ((imm & mask) == mask) {
557 mvni(vd, ~bytes[i] & 0xff, LSL, i * 8);
558 return;
559 }
560 }
561
562 // Immediate is of the form 0x00MMFFFF.
563 if ((imm & 0xff00ffff) == 0x0000ffff) {
564 movi(vd, bytes[2], MSL, 16);
565 return;
566 }
567
568 // Immediate is of the form 0x0000MMFF.
569 if ((imm & 0xffff00ff) == 0x000000ff) {
570 movi(vd, bytes[1], MSL, 8);
571 return;
572 }
573
574 // Immediate is of the form 0xFFMM0000.
575 if ((imm & 0xff00ffff) == 0xff000000) {
576 mvni(vd, ~bytes[2] & 0xff, MSL, 16);
577 return;
578 }
579 // Immediate is of the form 0xFFFFMM00.
580 if ((imm & 0xffff00ff) == 0xffff0000) {
581 mvni(vd, ~bytes[1] & 0xff, MSL, 8);
582 return;
583 }
584
585 // Top and bottom 16-bits are equal.
586 if (((imm >> 16) & 0xffff) == (imm & 0xffff)) {
587 Movi16bitHelper(vd.Is64Bits() ? vd.V4H() : vd.V8H(), imm & 0xffff);
588 return;
589 }
590
591 // Default case.
592 {
593 UseScratchRegisterScope temps(this);
594 Register temp = temps.AcquireW();
595 Mov(temp, imm);
596 dup(vd, temp);
597 }
598 }
599
600
601 void MacroAssembler::Movi64bitHelper(const VRegister& vd, uint64_t imm) {
602 // All bytes are either 0x00 or 0xff.
603 {
604 bool all0orff = true;
605 for (int i = 0; i < 8; ++i) {
606 int byteval = (imm >> (i * 8)) & 0xff;
607 if (byteval != 0 && byteval != 0xff) {
608 all0orff = false;
609 break;
610 }
611 }
612 if (all0orff == true) {
613 movi(vd, imm);
614 return;
615 }
616 }
617
618 // Top and bottom 32-bits are equal.
619 if (((imm >> 32) & 0xffffffff) == (imm & 0xffffffff)) {
620 Movi32bitHelper(vd.Is64Bits() ? vd.V2S() : vd.V4S(), imm & 0xffffffff);
621 return;
622 }
623
624 // Default case.
625 {
626 UseScratchRegisterScope temps(this);
627 Register temp = temps.AcquireX();
628 Mov(temp, imm);
629 if (vd.Is1D()) {
630 mov(vd.D(), 0, temp);
631 } else {
632 dup(vd.V2D(), temp);
633 }
634 }
635 }
636
637
638 void MacroAssembler::Movi(const VRegister& vd,
639 uint64_t imm,
640 Shift shift,
641 int shift_amount) {
642 MacroEmissionCheckScope guard(this);
643 if (shift_amount != 0 || shift != LSL) {
644 movi(vd, imm, shift, shift_amount);
645 } else if (vd.Is8B() || vd.Is16B()) {
646 // 8-bit immediate.
647 VIXL_ASSERT(IsUint8(imm));
648 movi(vd, imm);
649 } else if (vd.Is4H() || vd.Is8H()) {
650 // 16-bit immediate.
651 Movi16bitHelper(vd, imm);
652 } else if (vd.Is2S() || vd.Is4S()) {
653 // 32-bit immediate.
654 Movi32bitHelper(vd, imm);
655 } else {
656 // 64-bit immediate.
657 Movi64bitHelper(vd, imm);
658 }
659 }
660
661
662 void MacroAssembler::Movi(const VRegister& vd,
663 uint64_t hi,
664 uint64_t lo) {
665 VIXL_ASSERT(vd.Is128Bits());
666 UseScratchRegisterScope temps(this);
667
668 // When hi == lo, the following generates good code.
669 //
670 // In situations where the constants are complex and hi != lo, the following
671 // can turn into up to 10 instructions: 2*(mov + 3*movk + dup/insert). To do
672 // any better, we could try to estimate whether splatting the high value and
673 // updating the low value would generate fewer instructions than vice versa
674 // (what we do now).
675 //
676 // (A PC-relative load from memory to the vector register (ADR + LD2) is going
677 // to have fairly high latency but is fairly compact; not clear what the best
678 // tradeoff is.)
679
680 Movi(vd.V2D(), lo);
681 if (hi != lo) {
682 Register temp = temps.AcquireX();
683 Mov(temp, hi);
684 Ins(vd.V2D(), 1, temp);
685 }
686 }
687
688
689 void MacroAssembler::Mvn(const Register& rd, const Operand& operand) {
690 // The worst case for size is mvn immediate with up to 4 instructions.
691 MacroEmissionCheckScope guard(this);
692
693 if (operand.IsImmediate()) {
694 // Call the macro assembler for generic immediates.
695 Mvn(rd, operand.immediate());
696 } else if (operand.IsExtendedRegister()) {
697 UseScratchRegisterScope temps(this);
698 temps.Exclude(operand.reg());
699
700 // Emit two instructions for the extend case. This differs from Mov, as
701 // the extend and invert can't be achieved in one instruction.
702 Register temp = temps.AcquireSameSizeAs(rd);
703
704 // VIXL can acquire temp registers. Assert that the caller is aware.
705 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(operand.maybeReg()));
706
707 EmitExtendShift(temp, operand.reg(), operand.extend(),
708 operand.shift_amount());
709 mvn(rd, Operand(temp));
710 } else {
711 // Otherwise, register and shifted register cases can be handled by the
712 // assembler directly, using orn.
713 mvn(rd, operand);
714 }
715 }
716
717
718 void MacroAssembler::Mov(const Register& rd, uint64_t imm) {
719 MoveImmediateHelper(this, rd, imm);
720 }
721
722
723 void MacroAssembler::Ccmp(const Register& rn,
724 const Operand& operand,
725 StatusFlags nzcv,
726 Condition cond) {
727 if (operand.IsImmediate()) {
728 int64_t imm = operand.immediate();
729 if (imm < 0 && imm != std::numeric_limits<int64_t>::min()) {
730 ConditionalCompareMacro(rn, -imm, nzcv, cond, CCMN);
731 return;
732 }
733 }
734 ConditionalCompareMacro(rn, operand, nzcv, cond, CCMP);
735 }
736
737
738 void MacroAssembler::Ccmn(const Register& rn,
739 const Operand& operand,
740 StatusFlags nzcv,
741 Condition cond) {
742 if (operand.IsImmediate()) {
743 int64_t imm = operand.immediate();
744 if (imm < 0 && imm != std::numeric_limits<int64_t>::min()) {
745 ConditionalCompareMacro(rn, -imm, nzcv, cond, CCMP);
746 return;
747 }
748 }
749 ConditionalCompareMacro(rn, operand, nzcv, cond, CCMN);
750 }
751
752
753 void MacroAssembler::ConditionalCompareMacro(const Register& rn,
754 const Operand& operand,
755 StatusFlags nzcv,
756 Condition cond,
757 ConditionalCompareOp op) {
758 VIXL_ASSERT((cond != al) && (cond != nv));
759 // The worst case for size is ccmp immediate:
760 // * up to 4 instructions to materialise the constant
761 // * 1 instruction for ccmp
762 MacroEmissionCheckScope guard(this);
763
764 if ((operand.IsShiftedRegister() && (operand.shift_amount() == 0)) ||
765 (operand.IsImmediate() && IsImmConditionalCompare(operand.immediate()))) {
766 // The immediate can be encoded in the instruction, or the operand is an
767 // unshifted register: call the assembler.
768 ConditionalCompare(rn, operand, nzcv, cond, op);
769 } else {
770 UseScratchRegisterScope temps(this);
771 // The operand isn't directly supported by the instruction: perform the
772 // operation on a temporary register.
773 Register temp = temps.AcquireSameSizeAs(rn);
774 VIXL_ASSERT(!temp.Is(rn) && !temp.Is(operand.maybeReg()));
775 Mov(temp, operand);
776 ConditionalCompare(rn, temp, nzcv, cond, op);
777 }
778 }
779
780
781 void MacroAssembler::Csel(const Register& rd,
782 const Register& rn,
783 const Operand& operand,
784 Condition cond) {
785 VIXL_ASSERT(!rd.IsZero());
786 VIXL_ASSERT(!rn.IsZero());
787 VIXL_ASSERT((cond != al) && (cond != nv));
788 // The worst case for size is csel immediate:
789 // * up to 4 instructions to materialise the constant
790 // * 1 instruction for csel
791 MacroEmissionCheckScope guard(this);
792
793 if (operand.IsImmediate()) {
794 // Immediate argument. Handle special cases of 0, 1 and -1 using zero
795 // register.
796 int64_t imm = operand.immediate();
797 Register zr = AppropriateZeroRegFor(rn);
798 if (imm == 0) {
799 csel(rd, rn, zr, cond);
800 } else if (imm == 1) {
801 csinc(rd, rn, zr, cond);
802 } else if (imm == -1) {
803 csinv(rd, rn, zr, cond);
804 } else {
805 UseScratchRegisterScope temps(this);
806 Register temp = temps.AcquireSameSizeAs(rn);
807 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn));
808 VIXL_ASSERT(!temp.Is(operand.maybeReg()));
809 Mov(temp, operand.immediate());
810 csel(rd, rn, temp, cond);
811 }
812 } else if (operand.IsShiftedRegister() && (operand.shift_amount() == 0)) {
813 // Unshifted register argument.
814 csel(rd, rn, operand.reg(), cond);
815 } else {
816 // All other arguments.
817 UseScratchRegisterScope temps(this);
818 Register temp = temps.AcquireSameSizeAs(rn);
819 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn));
820 VIXL_ASSERT(!temp.Is(operand.maybeReg()));
821 Mov(temp, operand);
822 csel(rd, rn, temp, cond);
823 }
824 }
825
826
827 void MacroAssembler::Add(const Register& rd,
828 const Register& rn,
829 const Operand& operand,
830 FlagsUpdate S) {
831 if (operand.IsImmediate()) {
832 int64_t imm = operand.immediate();
833 if (imm < 0 && imm != std::numeric_limits<int64_t>::min() &&
834 IsImmAddSub(-imm)) {
835 AddSubMacro(rd, rn, -imm, S, SUB);
836 return;
837 }
838 }
839 AddSubMacro(rd, rn, operand, S, ADD);
840 }
841
842
843 void MacroAssembler::Adds(const Register& rd,
844 const Register& rn,
845 const Operand& operand) {
846 Add(rd, rn, operand, SetFlags);
847 }
848
849
850 void MacroAssembler::Sub(const Register& rd,
851 const Register& rn,
852 const Operand& operand,
853 FlagsUpdate S) {
854 if (operand.IsImmediate()) {
855 int64_t imm = operand.immediate();
856 if (imm < 0 && imm != std::numeric_limits<int64_t>::min() &&
857 IsImmAddSub(-imm)) {
858 AddSubMacro(rd, rn, -imm, S, ADD);
859 return;
860 }
861 }
862 AddSubMacro(rd, rn, operand, S, SUB);
863 }
864
865
866 void MacroAssembler::Subs(const Register& rd,
867 const Register& rn,
868 const Operand& operand) {
869 Sub(rd, rn, operand, SetFlags);
870 }
871
872
873 void MacroAssembler::Cmn(const Register& rn, const Operand& operand) {
874 Adds(AppropriateZeroRegFor(rn), rn, operand);
875 }
876
877
878 void MacroAssembler::Cmp(const Register& rn, const Operand& operand) {
879 Subs(AppropriateZeroRegFor(rn), rn, operand);
880 }
881
882
883 void MacroAssembler::Fcmp(const FPRegister& fn, double value,
884 FPTrapFlags trap) {
885 // The worst case for size is:
886 // * 1 to materialise the constant, using literal pool if necessary
887 // * 1 instruction for fcmp{e}
888 MacroEmissionCheckScope guard(this);
889 if (value != 0.0) {
890 UseScratchRegisterScope temps(this);
891 FPRegister tmp = temps.AcquireSameSizeAs(fn);
892 VIXL_ASSERT(!tmp.Is(fn));
893 Fmov(tmp, value);
894 FPCompareMacro(fn, tmp, trap);
895 } else {
896 FPCompareMacro(fn, value, trap);
897 }
898 }
899
900
901 void MacroAssembler::Fcmpe(const FPRegister& fn, double value) {
902 Fcmp(fn, value, EnableTrap);
903 }
904
905
906 void MacroAssembler::Fmov(VRegister vd, double imm) {
907 // Floating point immediates are loaded through the literal pool.
908 MacroEmissionCheckScope guard(this);
909
910 if (vd.Is1S() || vd.Is2S() || vd.Is4S()) {
911 Fmov(vd, static_cast<float>(imm));
912 return;
913 }
914
915 VIXL_ASSERT(vd.Is1D() || vd.Is2D());
916 if (IsImmFP64(imm)) {
917 fmov(vd, imm);
918 } else {
919 uint64_t rawbits = DoubleToRawbits(imm);
920 if (vd.IsScalar()) {
921 if (rawbits == 0) {
922 fmov(vd, xzr);
923 } else {
924 Assembler::fImmPool64(vd, imm);
925 }
926 } else {
927 // TODO: consider NEON support for load literal.
928 Movi(vd, rawbits);
929 }
930 }
931 }
932
933
934 void MacroAssembler::Fmov(VRegister vd, float imm) {
935 // Floating point immediates are loaded through the literal pool.
936 MacroEmissionCheckScope guard(this);
937
938 if (vd.Is1D() || vd.Is2D()) {
939 Fmov(vd, static_cast<double>(imm));
940 return;
941 }
942
943 VIXL_ASSERT(vd.Is1S() || vd.Is2S() || vd.Is4S());
944 if (IsImmFP32(imm)) {
945 fmov(vd, imm);
946 } else {
947 uint32_t rawbits = FloatToRawbits(imm);
948 if (vd.IsScalar()) {
949 if (rawbits == 0) {
950 fmov(vd, wzr);
951 } else {
952 Assembler::fImmPool32(vd, imm);
953 }
954 } else {
955 // TODO: consider NEON support for load literal.
956 Movi(vd, rawbits);
957 }
958 }
959 }
960
961
962
963 void MacroAssembler::Neg(const Register& rd,
964 const Operand& operand) {
965 if (operand.IsImmediate()) {
966 int64_t imm = operand.immediate();
967 if (imm != std::numeric_limits<int64_t>::min()) {
968 Mov(rd, -imm);
969 return;
970 }
971 }
972 Sub(rd, AppropriateZeroRegFor(rd), operand);
973 }
974
975
976 void MacroAssembler::Negs(const Register& rd,
977 const Operand& operand) {
978 Subs(rd, AppropriateZeroRegFor(rd), operand);
979 }
980
981
982 bool MacroAssembler::TryOneInstrMoveImmediate(const Register& dst,
983 int64_t imm) {
984 return OneInstrMoveImmediateHelper(this, dst, imm);
985 }
986
987
988 Operand MacroAssembler::MoveImmediateForShiftedOp(const Register& dst,
989 int64_t imm,
990 PreShiftImmMode mode) {
991 int reg_size = dst.size();
992
993 // Encode the immediate in a single move instruction, if possible.
994 if (TryOneInstrMoveImmediate(dst, imm)) {
995 // The move was successful; nothing to do here.
996 } else {
997 // Pre-shift the immediate to the least-significant bits of the register.
998 int shift_low = CountTrailingZeros(imm, reg_size);
999 if (mode == kLimitShiftForSP) {
1000 // When applied to the stack pointer, the subsequent arithmetic operation
1001 // can use the extend form to shift left by a maximum of four bits. Right
1002 // shifts are not allowed, so we filter them out later before the new
1003 // immediate is tested.
1004 shift_low = std::min(shift_low, 4);
1005 }
1006
1007 int64_t imm_low = imm >> shift_low;
1008
1009 // Pre-shift the immediate to the most-significant bits of the register,
1010 // inserting set bits in the least-significant bits.
1011 int shift_high = CountLeadingZeros(imm, reg_size);
1012 int64_t imm_high = (imm << shift_high) | ((INT64_C(1) << shift_high) - 1);
1013
1014 if ((mode != kNoShift) && TryOneInstrMoveImmediate(dst, imm_low)) {
1015 // The new immediate has been moved into the destination's low bits:
1016 // return a new leftward-shifting operand.
1017 return Operand(dst, LSL, shift_low);
1018 } else if ((mode == kAnyShift) && TryOneInstrMoveImmediate(dst, imm_high)) {
1019 // The new immediate has been moved into the destination's high bits:
1020 // return a new rightward-shifting operand.
1021 return Operand(dst, LSR, shift_high);
1022 } else {
1023 Mov(dst, imm);
1024 }
1025 }
1026 return Operand(dst);
1027 }
1028
1029
1030 void MacroAssembler::ComputeAddress(const Register& dst,
1031 const MemOperand& mem_op) {
1032 // We cannot handle pre-indexing or post-indexing.
1033 VIXL_ASSERT(mem_op.addrmode() == Offset);
1034 Register base = mem_op.base();
1035 if (mem_op.IsImmediateOffset()) {
1036 Add(dst, base, mem_op.offset());
1037 } else {
1038 VIXL_ASSERT(mem_op.IsRegisterOffset());
1039 Register reg_offset = mem_op.regoffset();
1040 Shift shift = mem_op.shift();
1041 Extend extend = mem_op.extend();
1042 if (shift == NO_SHIFT) {
1043 VIXL_ASSERT(extend != NO_EXTEND);
1044 Add(dst, base, Operand(reg_offset, extend, mem_op.shift_amount()));
1045 } else {
1046 VIXL_ASSERT(extend == NO_EXTEND);
1047 Add(dst, base, Operand(reg_offset, shift, mem_op.shift_amount()));
1048 }
1049 }
1050 }
1051
1052
1053 void MacroAssembler::AddSubMacro(const Register& rd,
1054 const Register& rn,
1055 const Operand& operand,
1056 FlagsUpdate S,
1057 AddSubOp op) {
1058 // Worst case is add/sub immediate:
1059 // * up to 4 instructions to materialise the constant
1060 // * 1 instruction for add/sub
1061 MacroEmissionCheckScope guard(this);
1062
1063 if (operand.IsZero() && rd.Is(rn) && rd.Is64Bits() && rn.Is64Bits() &&
1064 (S == LeaveFlags)) {
1065 // The instruction would be a nop. Avoid generating useless code.
1066 return;
1067 }
1068
1069 if ((operand.IsImmediate() && !IsImmAddSub(operand.immediate())) ||
1070 (rn.IsZero() && !operand.IsShiftedRegister()) ||
1071 (operand.IsShiftedRegister() && (operand.shift() == ROR))) {
1072 UseScratchRegisterScope temps(this);
1073 Register temp = temps.AcquireSameSizeAs(rn);
1074 if (operand.IsImmediate()) {
1075 PreShiftImmMode mode = kAnyShift;
1076
1077 // If the destination or source register is the stack pointer, we can
1078 // only pre-shift the immediate right by values supported in the add/sub
1079 // extend encoding.
1080 if (rd.IsSP()) {
1081 // If the destination is SP and flags will be set, we can't pre-shift
1082 // the immediate at all.
1083 mode = (S == SetFlags) ? kNoShift : kLimitShiftForSP;
1084 } else if (rn.IsSP()) {
1085 mode = kLimitShiftForSP;
1086 }
1087
1088 Operand imm_operand =
1089 MoveImmediateForShiftedOp(temp, operand.immediate(), mode);
1090 AddSub(rd, rn, imm_operand, S, op);
1091 } else {
1092 Mov(temp, operand);
1093 AddSub(rd, rn, temp, S, op);
1094 }
1095 } else {
1096 AddSub(rd, rn, operand, S, op);
1097 }
1098 }
1099
1100
1101 void MacroAssembler::Adc(const Register& rd,
1102 const Register& rn,
1103 const Operand& operand) {
1104 AddSubWithCarryMacro(rd, rn, operand, LeaveFlags, ADC);
1105 }
1106
1107
1108 void MacroAssembler::Adcs(const Register& rd,
1109 const Register& rn,
1110 const Operand& operand) {
1111 AddSubWithCarryMacro(rd, rn, operand, SetFlags, ADC);
1112 }
1113
1114
1115 void MacroAssembler::Sbc(const Register& rd,
1116 const Register& rn,
1117 const Operand& operand) {
1118 AddSubWithCarryMacro(rd, rn, operand, LeaveFlags, SBC);
1119 }
1120
1121
1122 void MacroAssembler::Sbcs(const Register& rd,
1123 const Register& rn,
1124 const Operand& operand) {
1125 AddSubWithCarryMacro(rd, rn, operand, SetFlags, SBC);
1126 }
1127
1128
1129 void MacroAssembler::Ngc(const Register& rd,
1130 const Operand& operand) {
1131 Register zr = AppropriateZeroRegFor(rd);
1132 Sbc(rd, zr, operand);
1133 }
1134
1135
1136 void MacroAssembler::Ngcs(const Register& rd,
1137 const Operand& operand) {
1138 Register zr = AppropriateZeroRegFor(rd);
1139 Sbcs(rd, zr, operand);
1140 }
1141
1142
1143 void MacroAssembler::AddSubWithCarryMacro(const Register& rd,
1144 const Register& rn,
1145 const Operand& operand,
1146 FlagsUpdate S,
1147 AddSubWithCarryOp op) {
1148 VIXL_ASSERT(rd.size() == rn.size());
1149 // Worst case is addc/subc immediate:
1150 // * up to 4 instructions to materialise the constant
1151 // * 1 instruction for add/sub
1152 MacroEmissionCheckScope guard(this);
1153 UseScratchRegisterScope temps(this);
1154
1155 if (operand.IsImmediate() ||
1156 (operand.IsShiftedRegister() && (operand.shift() == ROR))) {
1157 // Add/sub with carry (immediate or ROR shifted register.)
1158 Register temp = temps.AcquireSameSizeAs(rn);
1159 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn) && !temp.Is(operand.maybeReg()));
1160 Mov(temp, operand);
1161 AddSubWithCarry(rd, rn, Operand(temp), S, op);
1162 } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) {
1163 // Add/sub with carry (shifted register).
1164 VIXL_ASSERT(operand.reg().size() == rd.size());
1165 VIXL_ASSERT(operand.shift() != ROR);
1166 VIXL_ASSERT(IsUintN(rd.size() == kXRegSize ? kXRegSizeLog2 : kWRegSizeLog2,
1167 operand.shift_amount()));
1168 temps.Exclude(operand.reg());
1169 Register temp = temps.AcquireSameSizeAs(rn);
1170 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn) && !temp.Is(operand.maybeReg()));
1171 EmitShift(temp, operand.reg(), operand.shift(), operand.shift_amount());
1172 AddSubWithCarry(rd, rn, Operand(temp), S, op);
1173 } else if (operand.IsExtendedRegister()) {
1174 // Add/sub with carry (extended register).
1175 VIXL_ASSERT(operand.reg().size() <= rd.size());
1176 // Add/sub extended supports a shift <= 4. We want to support exactly the
1177 // same modes.
1178 VIXL_ASSERT(operand.shift_amount() <= 4);
1179 VIXL_ASSERT(operand.reg().Is64Bits() ||
1180 ((operand.extend() != UXTX) && (operand.extend() != SXTX)));
1181 temps.Exclude(operand.reg());
1182 Register temp = temps.AcquireSameSizeAs(rn);
1183 VIXL_ASSERT(!temp.Is(rd) && !temp.Is(rn) && !temp.Is(operand.maybeReg()));
1184 EmitExtendShift(temp, operand.reg(), operand.extend(),
1185 operand.shift_amount());
1186 AddSubWithCarry(rd, rn, Operand(temp), S, op);
1187 } else {
1188 // The addressing mode is directly supported by the instruction.
1189 AddSubWithCarry(rd, rn, operand, S, op);
1190 }
1191 }
1192
1193 #define DEFINE_FUNCTION(FN, REGTYPE, REG, OP) \
1194 js::wasm::FaultingCodeOffset MacroAssembler::FN(const REGTYPE REG, \
1195 const MemOperand& addr) { \
1196 return LoadStoreMacro(REG, addr, OP); \
1197 }
1198 LS_MACRO_LIST(DEFINE_FUNCTION)
1199 #undef DEFINE_FUNCTION
1200
1201 js::wasm::FaultingCodeOffset MacroAssembler::LoadStoreMacro(
1202 const CPURegister& rt,
1203 const MemOperand& addr,
1204 LoadStoreOp op) {
1205 // Worst case is ldr/str pre/post index:
1206 // * 1 instruction for ldr/str
1207 // * up to 4 instructions to materialise the constant
1208 // * 1 instruction to update the base
1209 MacroEmissionCheckScope guard(this);
1210
1211 int64_t offset = addr.offset();
1212 unsigned access_size = CalcLSDataSize(op);
1213
1214 // Check if an immediate offset fits in the immediate field of the
1215 // appropriate instruction. If not, emit two instructions to perform
1216 // the operation.
1217 js::wasm::FaultingCodeOffset fco;
1218 if (addr.IsImmediateOffset() && !IsImmLSScaled(offset, access_size) &&
1219 !IsImmLSUnscaled(offset)) {
1220 // Immediate offset that can't be encoded using unsigned or unscaled
1221 // addressing modes.
1222 VIXL_ASSERT(addr.regoffset().Is(NoReg));
1223 UseScratchRegisterScope temps(this);
1224 int64_t offset = addr.offset();
1225 Register recycle0 = NoReg;
1226 Register recycle1 = NoReg;
1227 if (!temps.HasAvailableRegister()) {
1228 // Recycle the first pair of registers which are not aliasing the operands.
1229 js::jit::AllocatableGeneralRegisterSet
1230 freeRegs(js::jit::GeneralRegisterSet::Volatile());
1231 freeRegs.takeUnchecked(addr.base().asUnsized());
1232 freeRegs.takeUnchecked(rt.X().asUnsized());
1233 recycle0 = Register(freeRegs.takeFirst(), kXRegSize);
1234 recycle1 = Register(freeRegs.takeFirst(), kXRegSize);
1235 Push(recycle0, recycle1);
1236 temps.Include(recycle0, recycle1);
1237 if (addr.base().Is(GetStackPointer64())) {
1238 offset += 2 * sizeof(uintptr_t);
1239 }
1240 }
1241 Register temp = temps.AcquireSameSizeAs(addr.base());
1242 VIXL_ASSERT(!temp.Is(rt));
1243 VIXL_ASSERT(!temp.Is(addr.base()));
1244 Mov(temp, offset);
1245 {
1246 js::jit::AutoForbidPoolsAndNops afp(this, 1);
1247 fco = js::wasm::FaultingCodeOffset(currentOffset());
1248 LoadStore(rt, MemOperand(addr.base(), temp), op);
1249 }
1250 if (!recycle0.Is(NoReg)) {
1251 temps.Exclude(recycle0, recycle1);
1252 Pop(recycle1, recycle0);
1253 }
1254 } else if (addr.IsPostIndex() && !IsImmLSUnscaled(offset)) {
1255 // Post-index beyond unscaled addressing range.
1256 {
1257 js::jit::AutoForbidPoolsAndNops afp(this, 1);
1258 fco = js::wasm::FaultingCodeOffset(currentOffset());
1259 LoadStore(rt, MemOperand(addr.base()), op);
1260 }
1261 Add(addr.base(), addr.base(), Operand(offset));
1262 } else if (addr.IsPreIndex() && !IsImmLSUnscaled(offset)) {
1263 // Pre-index beyond unscaled addressing range.
1264 Add(addr.base(), addr.base(), Operand(offset));
1265 {
1266 js::jit::AutoForbidPoolsAndNops afp(this, 1);
1267 fco = js::wasm::FaultingCodeOffset(currentOffset());
1268 LoadStore(rt, MemOperand(addr.base()), op);
1269 }
1270 } else {
1271 // Encodable in one load/store instruction.
1272 js::jit::AutoForbidPoolsAndNops afp(this, 1);
1273 fco = js::wasm::FaultingCodeOffset(currentOffset());
1274 LoadStore(rt, addr, op);
1275 }
1276
1277 return fco;
1278 }
1279
1280 #define DEFINE_FUNCTION(FN, REGTYPE, REG, REG2, OP) \
1281 void MacroAssembler::FN(const REGTYPE REG, \
1282 const REGTYPE REG2, \
1283 const MemOperand& addr) { \
1284 LoadStorePairMacro(REG, REG2, addr, OP); \
1285 }
1286 LSPAIR_MACRO_LIST(DEFINE_FUNCTION)
1287 #undef DEFINE_FUNCTION
1288
1289 void MacroAssembler::LoadStorePairMacro(const CPURegister& rt,
1290 const CPURegister& rt2,
1291 const MemOperand& addr,
1292 LoadStorePairOp op) {
1293 // TODO(all): Should we support register offset for load-store-pair?
1294 VIXL_ASSERT(!addr.IsRegisterOffset());
1295 // Worst case is ldp/stp immediate:
1296 // * 1 instruction for ldp/stp
1297 // * up to 4 instructions to materialise the constant
1298 // * 1 instruction to update the base
1299 MacroEmissionCheckScope guard(this);
1300
1301 int64_t offset = addr.offset();
1302 unsigned access_size = CalcLSPairDataSize(op);
1303
1304 // Check if the offset fits in the immediate field of the appropriate
1305 // instruction. If not, emit two instructions to perform the operation.
1306 if (IsImmLSPair(offset, access_size)) {
1307 // Encodable in one load/store pair instruction.
1308 LoadStorePair(rt, rt2, addr, op);
1309 } else {
1310 Register base = addr.base();
1311 if (addr.IsImmediateOffset()) {
1312 UseScratchRegisterScope temps(this);
1313 Register temp = temps.AcquireSameSizeAs(base);
1314 Add(temp, base, offset);
1315 LoadStorePair(rt, rt2, MemOperand(temp), op);
1316 } else if (addr.IsPostIndex()) {
1317 LoadStorePair(rt, rt2, MemOperand(base), op);
1318 Add(base, base, offset);
1319 } else {
1320 VIXL_ASSERT(addr.IsPreIndex());
1321 Add(base, base, offset);
1322 LoadStorePair(rt, rt2, MemOperand(base), op);
1323 }
1324 }
1325 }
1326
1327
1328 void MacroAssembler::Prfm(PrefetchOperation op, const MemOperand& addr) {
1329 MacroEmissionCheckScope guard(this);
1330
1331 // There are no pre- or post-index modes for prfm.
1332 VIXL_ASSERT(addr.IsImmediateOffset() || addr.IsRegisterOffset());
1333
1334 // The access size is implicitly 8 bytes for all prefetch operations.
1335 unsigned size = kXRegSizeInBytesLog2;
1336
1337 // Check if an immediate offset fits in the immediate field of the
1338 // appropriate instruction. If not, emit two instructions to perform
1339 // the operation.
1340 if (addr.IsImmediateOffset() && !IsImmLSScaled(addr.offset(), size) &&
1341 !IsImmLSUnscaled(addr.offset())) {
1342 // Immediate offset that can't be encoded using unsigned or unscaled
1343 // addressing modes.
1344 UseScratchRegisterScope temps(this);
1345 Register temp = temps.AcquireSameSizeAs(addr.base());
1346 Mov(temp, addr.offset());
1347 Prefetch(op, MemOperand(addr.base(), temp));
1348 } else {
1349 // Simple register-offsets are encodable in one instruction.
1350 Prefetch(op, addr);
1351 }
1352 }
1353
1354
1355 void MacroAssembler::PushStackPointer() {
1356 PrepareForPush(1, 8);
1357
1358 // Pushing a stack pointer leads to implementation-defined
1359 // behavior, which may be surprising. In particular,
1360 // str x28, [x28, #-8]!
1361 // pre-decrements the stack pointer, storing the decremented value.
1362 // Additionally, sp is read as xzr in this context, so it cannot be pushed.
1363 // So we must use a scratch register.
1364 UseScratchRegisterScope temps(this);
1365 Register scratch = temps.AcquireX();
1366
1367 Mov(scratch, GetStackPointer64());
1368 str(scratch, MemOperand(GetStackPointer64(), -8, PreIndex));
1369 }
1370
1371
1372 void MacroAssembler::Push(const CPURegister& src0, const CPURegister& src1,
1373 const CPURegister& src2, const CPURegister& src3) {
1374 VIXL_ASSERT(AreSameSizeAndType(src0, src1, src2, src3));
1375 VIXL_ASSERT(src0.IsValid());
1376
1377 int count = 1 + src1.IsValid() + src2.IsValid() + src3.IsValid();
1378 int size = src0.SizeInBytes();
1379
1380 if (src0.Is(GetStackPointer64())) {
1381 VIXL_ASSERT(count == 1);
1382 VIXL_ASSERT(size == 8);
1383 PushStackPointer();
1384 return;
1385 }
1386
1387 PrepareForPush(count, size);
1388 PushHelper(count, size, src0, src1, src2, src3);
1389 }
1390
1391
1392 void MacroAssembler::Pop(const CPURegister& dst0, const CPURegister& dst1,
1393 const CPURegister& dst2, const CPURegister& dst3) {
1394 // It is not valid to pop into the same register more than once in one
1395 // instruction, not even into the zero register.
1396 VIXL_ASSERT(!AreAliased(dst0, dst1, dst2, dst3));
1397 VIXL_ASSERT(AreSameSizeAndType(dst0, dst1, dst2, dst3));
1398 VIXL_ASSERT(dst0.IsValid());
1399
1400 int count = 1 + dst1.IsValid() + dst2.IsValid() + dst3.IsValid();
1401 int size = dst0.SizeInBytes();
1402
1403 PrepareForPop(count, size);
1404 PopHelper(count, size, dst0, dst1, dst2, dst3);
1405 }
1406
1407
1408 void MacroAssembler::PushCPURegList(CPURegList registers) {
1409 VIXL_ASSERT(!registers.Overlaps(*TmpList()));
1410 VIXL_ASSERT(!registers.Overlaps(*FPTmpList()));
1411
1412 int reg_size = registers.RegisterSizeInBytes();
1413 PrepareForPush(registers.Count(), reg_size);
1414
1415 // Bump the stack pointer and store two registers at the bottom.
1416 int size = registers.TotalSizeInBytes();
1417 const CPURegister& bottom_0 = registers.PopLowestIndex();
1418 const CPURegister& bottom_1 = registers.PopLowestIndex();
1419 if (bottom_0.IsValid() && bottom_1.IsValid()) {
1420 Stp(bottom_0, bottom_1, MemOperand(GetStackPointer64(), -size, PreIndex));
1421 } else if (bottom_0.IsValid()) {
1422 Str(bottom_0, MemOperand(GetStackPointer64(), -size, PreIndex));
1423 }
1424
1425 int offset = 2 * reg_size;
1426 while (!registers.IsEmpty()) {
1427 const CPURegister& src0 = registers.PopLowestIndex();
1428 const CPURegister& src1 = registers.PopLowestIndex();
1429 if (src1.IsValid()) {
1430 Stp(src0, src1, MemOperand(GetStackPointer64(), offset));
1431 } else {
1432 Str(src0, MemOperand(GetStackPointer64(), offset));
1433 }
1434 offset += 2 * reg_size;
1435 }
1436 }
1437
1438
1439 void MacroAssembler::PopCPURegList(CPURegList registers) {
1440 VIXL_ASSERT(!registers.Overlaps(*TmpList()));
1441 VIXL_ASSERT(!registers.Overlaps(*FPTmpList()));
1442
1443 int reg_size = registers.RegisterSizeInBytes();
1444 PrepareForPop(registers.Count(), reg_size);
1445
1446
1447 int size = registers.TotalSizeInBytes();
1448 const CPURegister& bottom_0 = registers.PopLowestIndex();
1449 const CPURegister& bottom_1 = registers.PopLowestIndex();
1450
1451 int offset = 2 * reg_size;
1452 while (!registers.IsEmpty()) {
1453 const CPURegister& dst0 = registers.PopLowestIndex();
1454 const CPURegister& dst1 = registers.PopLowestIndex();
1455 if (dst1.IsValid()) {
1456 Ldp(dst0, dst1, MemOperand(GetStackPointer64(), offset));
1457 } else {
1458 Ldr(dst0, MemOperand(GetStackPointer64(), offset));
1459 }
1460 offset += 2 * reg_size;
1461 }
1462
1463 // Load the two registers at the bottom and drop the stack pointer.
1464 if (bottom_0.IsValid() && bottom_1.IsValid()) {
1465 Ldp(bottom_0, bottom_1, MemOperand(GetStackPointer64(), size, PostIndex));
1466 } else if (bottom_0.IsValid()) {
1467 Ldr(bottom_0, MemOperand(GetStackPointer64(), size, PostIndex));
1468 }
1469 }
1470
1471
1472 void MacroAssembler::PushMultipleTimes(int count, Register src) {
1473 int size = src.SizeInBytes();
1474
1475 PrepareForPush(count, size);
1476 // Push up to four registers at a time if possible because if the current
1477 // stack pointer is sp and the register size is 32, registers must be pushed
1478 // in blocks of four in order to maintain the 16-byte alignment for sp.
1479 while (count >= 4) {
1480 PushHelper(4, size, src, src, src, src);
1481 count -= 4;
1482 }
1483 if (count >= 2) {
1484 PushHelper(2, size, src, src, NoReg, NoReg);
1485 count -= 2;
1486 }
1487 if (count == 1) {
1488 PushHelper(1, size, src, NoReg, NoReg, NoReg);
1489 count -= 1;
1490 }
1491 VIXL_ASSERT(count == 0);
1492 }
1493
1494
1495 void MacroAssembler::PushHelper(int count, int size,
1496 const CPURegister& src0,
1497 const CPURegister& src1,
1498 const CPURegister& src2,
1499 const CPURegister& src3) {
1500 // Ensure that we don't unintentionally modify scratch or debug registers.
1501 // Worst case for size is 2 stp.
1502 InstructionAccurateScope scope(this, 2,
1503 InstructionAccurateScope::kMaximumSize);
1504
1505 VIXL_ASSERT(AreSameSizeAndType(src0, src1, src2, src3));
1506 VIXL_ASSERT(size == src0.SizeInBytes());
1507
1508 // Pushing the stack pointer has unexpected behavior. See PushStackPointer().
1509 VIXL_ASSERT(!src0.Is(GetStackPointer64()) && !src0.Is(sp));
1510 VIXL_ASSERT(!src1.Is(GetStackPointer64()) && !src1.Is(sp));
1511 VIXL_ASSERT(!src2.Is(GetStackPointer64()) && !src2.Is(sp));
1512 VIXL_ASSERT(!src3.Is(GetStackPointer64()) && !src3.Is(sp));
1513
1514 // The JS engine should never push 4 bytes.
1515 VIXL_ASSERT(size >= 8);
1516
1517 // When pushing multiple registers, the store order is chosen such that
1518 // Push(a, b) is equivalent to Push(a) followed by Push(b).
1519 switch (count) {
1520 case 1:
1521 VIXL_ASSERT(src1.IsNone() && src2.IsNone() && src3.IsNone());
1522 str(src0, MemOperand(GetStackPointer64(), -1 * size, PreIndex));
1523 break;
1524 case 2:
1525 VIXL_ASSERT(src2.IsNone() && src3.IsNone());
1526 stp(src1, src0, MemOperand(GetStackPointer64(), -2 * size, PreIndex));
1527 break;
1528 case 3:
1529 VIXL_ASSERT(src3.IsNone());
1530 stp(src2, src1, MemOperand(GetStackPointer64(), -3 * size, PreIndex));
1531 str(src0, MemOperand(GetStackPointer64(), 2 * size));
1532 break;
1533 case 4:
1534 // Skip over 4 * size, then fill in the gap. This allows four W registers
1535 // to be pushed using sp, whilst maintaining 16-byte alignment for sp at
1536 // all times.
1537 stp(src3, src2, MemOperand(GetStackPointer64(), -4 * size, PreIndex));
1538 stp(src1, src0, MemOperand(GetStackPointer64(), 2 * size));
1539 break;
1540 default:
1541 VIXL_UNREACHABLE();
1542 }
1543 }
1544
1545
1546 void MacroAssembler::PopHelper(int count, int size,
1547 const CPURegister& dst0,
1548 const CPURegister& dst1,
1549 const CPURegister& dst2,
1550 const CPURegister& dst3) {
1551 // Ensure that we don't unintentionally modify scratch or debug registers.
1552 // Worst case for size is 2 ldp.
1553 InstructionAccurateScope scope(this, 2,
1554 InstructionAccurateScope::kMaximumSize);
1555
1556 VIXL_ASSERT(AreSameSizeAndType(dst0, dst1, dst2, dst3));
1557 VIXL_ASSERT(size == dst0.SizeInBytes());
1558
1559 // When popping multiple registers, the load order is chosen such that
1560 // Pop(a, b) is equivalent to Pop(a) followed by Pop(b).
1561 switch (count) {
1562 case 1:
1563 VIXL_ASSERT(dst1.IsNone() && dst2.IsNone() && dst3.IsNone());
1564 ldr(dst0, MemOperand(GetStackPointer64(), 1 * size, PostIndex));
1565 break;
1566 case 2:
1567 VIXL_ASSERT(dst2.IsNone() && dst3.IsNone());
1568 ldp(dst0, dst1, MemOperand(GetStackPointer64(), 2 * size, PostIndex));
1569 break;
1570 case 3:
1571 VIXL_ASSERT(dst3.IsNone());
1572 ldr(dst2, MemOperand(GetStackPointer64(), 2 * size));
1573 ldp(dst0, dst1, MemOperand(GetStackPointer64(), 3 * size, PostIndex));
1574 break;
1575 case 4:
1576 // Load the higher addresses first, then load the lower addresses and skip
1577 // the whole block in the second instruction. This allows four W registers
1578 // to be popped using sp, whilst maintaining 16-byte alignment for sp at
1579 // all times.
1580 ldp(dst2, dst3, MemOperand(GetStackPointer64(), 2 * size));
1581 ldp(dst0, dst1, MemOperand(GetStackPointer64(), 4 * size, PostIndex));
1582 break;
1583 default:
1584 VIXL_UNREACHABLE();
1585 }
1586 }
1587
1588
1589 void MacroAssembler::PrepareForPush(int count, int size) {
1590 if (sp.Is(GetStackPointer64())) {
1591 // If the current stack pointer is sp, then it must be aligned to 16 bytes
1592 // on entry and the total size of the specified registers must also be a
1593 // multiple of 16 bytes.
1594 VIXL_ASSERT((count * size) % 16 == 0);
1595 } else {
1596 // Even if the current stack pointer is not the system stack pointer (sp),
1597 // the system stack pointer will still be modified in order to comply with
1598 // ABI rules about accessing memory below the system stack pointer.
1599 BumpSystemStackPointer(count * size);
1600 }
1601 }
1602
1603
1604 void MacroAssembler::PrepareForPop(int count, int size) {
1605 USE(count, size);
1606 if (sp.Is(GetStackPointer64())) {
1607 // If the current stack pointer is sp, then it must be aligned to 16 bytes
1608 // on entry and the total size of the specified registers must also be a
1609 // multiple of 16 bytes.
1610 VIXL_ASSERT((count * size) % 16 == 0);
1611 }
1612 }
1613
1614 void MacroAssembler::Poke(const Register& src, const Operand& offset) {
1615 if (offset.IsImmediate()) {
1616 VIXL_ASSERT(offset.immediate() >= 0);
1617 }
1618
1619 Str(src, MemOperand(GetStackPointer64(), offset));
1620 }
1621
1622
1623 void MacroAssembler::Peek(const Register& dst, const Operand& offset) {
1624 if (offset.IsImmediate()) {
1625 VIXL_ASSERT(offset.immediate() >= 0);
1626 }
1627
1628 Ldr(dst, MemOperand(GetStackPointer64(), offset));
1629 }
1630
1631
1632 void MacroAssembler::Claim(const Operand& size) {
1633
1634 if (size.IsZero()) {
1635 return;
1636 }
1637
1638 if (size.IsImmediate()) {
1639 VIXL_ASSERT(size.immediate() > 0);
1640 if (sp.Is(GetStackPointer64())) {
1641 VIXL_ASSERT((size.immediate() % 16) == 0);
1642 }
1643 }
1644
1645 Sub(GetStackPointer64(), GetStackPointer64(), size);
1646
1647 // Make sure the real stack pointer reflects the claimed stack space.
1648 // We can't use stack memory below the stack pointer, it could be clobbered by
1649 // interupts and signal handlers.
1650 if (!sp.Is(GetStackPointer64())) {
1651 Mov(sp, GetStackPointer64());
1652 }
1653 }
1654
1655
1656 void MacroAssembler::Drop(const Operand& size) {
1657
1658 if (size.IsZero()) {
1659 return;
1660 }
1661
1662 if (size.IsImmediate()) {
1663 VIXL_ASSERT(size.immediate() > 0);
1664 if (sp.Is(GetStackPointer64())) {
1665 VIXL_ASSERT((size.immediate() % 16) == 0);
1666 }
1667 }
1668
1669 Add(GetStackPointer64(), GetStackPointer64(), size);
1670 }
1671
1672
1673 void MacroAssembler::PushCalleeSavedRegisters() {
1674 // Ensure that the macro-assembler doesn't use any scratch registers.
1675 // 10 stp will be emitted.
1676 // TODO(all): Should we use GetCalleeSaved and SavedFP.
1677 InstructionAccurateScope scope(this, 10);
1678
1679 // This method must not be called unless the current stack pointer is sp.
1680 VIXL_ASSERT(sp.Is(GetStackPointer64()));
1681
1682 MemOperand tos(sp, -2 * static_cast<int>(kXRegSizeInBytes), PreIndex);
1683
1684 stp(x29, x30, tos);
1685 stp(x27, x28, tos);
1686 stp(x25, x26, tos);
1687 stp(x23, x24, tos);
1688 stp(x21, x22, tos);
1689 stp(x19, x20, tos);
1690
1691 stp(d14, d15, tos);
1692 stp(d12, d13, tos);
1693 stp(d10, d11, tos);
1694 stp(d8, d9, tos);
1695 }
1696
1697
1698 void MacroAssembler::PopCalleeSavedRegisters() {
1699 // Ensure that the macro-assembler doesn't use any scratch registers.
1700 // 10 ldp will be emitted.
1701 // TODO(all): Should we use GetCalleeSaved and SavedFP.
1702 InstructionAccurateScope scope(this, 10);
1703
1704 // This method must not be called unless the current stack pointer is sp.
1705 VIXL_ASSERT(sp.Is(GetStackPointer64()));
1706
1707 MemOperand tos(sp, 2 * kXRegSizeInBytes, PostIndex);
1708
1709 ldp(d8, d9, tos);
1710 ldp(d10, d11, tos);
1711 ldp(d12, d13, tos);
1712 ldp(d14, d15, tos);
1713
1714 ldp(x19, x20, tos);
1715 ldp(x21, x22, tos);
1716 ldp(x23, x24, tos);
1717 ldp(x25, x26, tos);
1718 ldp(x27, x28, tos);
1719 ldp(x29, x30, tos);
1720 }
1721
1722 void MacroAssembler::LoadCPURegList(CPURegList registers,
1723 const MemOperand& src) {
1724 LoadStoreCPURegListHelper(kLoad, registers, src);
1725 }
1726
1727 void MacroAssembler::StoreCPURegList(CPURegList registers,
1728 const MemOperand& dst) {
1729 LoadStoreCPURegListHelper(kStore, registers, dst);
1730 }
1731
1732
1733 void MacroAssembler::LoadStoreCPURegListHelper(LoadStoreCPURegListAction op,
1734 CPURegList registers,
1735 const MemOperand& mem) {
1736 // We do not handle pre-indexing or post-indexing.
1737 VIXL_ASSERT(!(mem.IsPreIndex() || mem.IsPostIndex()));
1738 VIXL_ASSERT(!registers.Overlaps(tmp_list_));
1739 VIXL_ASSERT(!registers.Overlaps(fptmp_list_));
1740 VIXL_ASSERT(!registers.IncludesAliasOf(sp));
1741
1742 UseScratchRegisterScope temps(this);
1743
1744 MemOperand loc = BaseMemOperandForLoadStoreCPURegList(registers,
1745 mem,
1746 &temps);
1747
1748 while (registers.Count() >= 2) {
1749 const CPURegister& dst0 = registers.PopLowestIndex();
1750 const CPURegister& dst1 = registers.PopLowestIndex();
1751 if (op == kStore) {
1752 Stp(dst0, dst1, loc);
1753 } else {
1754 VIXL_ASSERT(op == kLoad);
1755 Ldp(dst0, dst1, loc);
1756 }
1757 loc.AddOffset(2 * registers.RegisterSizeInBytes());
1758 }
1759 if (!registers.IsEmpty()) {
1760 if (op == kStore) {
1761 Str(registers.PopLowestIndex(), loc);
1762 } else {
1763 VIXL_ASSERT(op == kLoad);
1764 Ldr(registers.PopLowestIndex(), loc);
1765 }
1766 }
1767 }
1768
1769 MemOperand MacroAssembler::BaseMemOperandForLoadStoreCPURegList(
1770 const CPURegList& registers,
1771 const MemOperand& mem,
1772 UseScratchRegisterScope* scratch_scope) {
1773 // If necessary, pre-compute the base address for the accesses.
1774 if (mem.IsRegisterOffset()) {
1775 Register reg_base = scratch_scope->AcquireX();
1776 ComputeAddress(reg_base, mem);
1777 return MemOperand(reg_base);
1778
1779 } else if (mem.IsImmediateOffset()) {
1780 int reg_size = registers.RegisterSizeInBytes();
1781 int total_size = registers.TotalSizeInBytes();
1782 int64_t min_offset = mem.offset();
1783 int64_t max_offset = mem.offset() + std::max(0, total_size - 2 * reg_size);
1784 if ((registers.Count() >= 2) &&
1785 (!Assembler::IsImmLSPair(min_offset, WhichPowerOf2(reg_size)) ||
1786 !Assembler::IsImmLSPair(max_offset, WhichPowerOf2(reg_size)))) {
1787 Register reg_base = scratch_scope->AcquireX();
1788 ComputeAddress(reg_base, mem);
1789 return MemOperand(reg_base);
1790 }
1791 }
1792
1793 return mem;
1794 }
1795
1796 void MacroAssembler::BumpSystemStackPointer(const Operand& space) {
1797 VIXL_ASSERT(!sp.Is(GetStackPointer64()));
1798 // TODO: Several callers rely on this not using scratch registers, so we use
1799 // the assembler directly here. However, this means that large immediate
1800 // values of 'space' cannot be handled.
1801 InstructionAccurateScope scope(this, 1);
1802 sub(sp, GetStackPointer64(), space);
1803 }
1804
1805
1806 void MacroAssembler::Trace(TraceParameters parameters, TraceCommand command) {
1807
1808 #ifdef JS_SIMULATOR_ARM64
1809 // The arguments to the trace pseudo instruction need to be contiguous in
1810 // memory, so make sure we don't try to emit a literal pool.
1811 InstructionAccurateScope scope(this, kTraceLength / kInstructionSize);
1812
1813 Label start;
1814 bind(&start);
1815
1816 // Refer to simulator-a64.h for a description of the marker and its
1817 // arguments.
1818 hlt(kTraceOpcode);
1819
1820 // VIXL_ASSERT(SizeOfCodeGeneratedSince(&start) == kTraceParamsOffset);
1821 dc32(parameters);
1822
1823 // VIXL_ASSERT(SizeOfCodeGeneratedSince(&start) == kTraceCommandOffset);
1824 dc32(command);
1825 #else
1826 // Emit nothing on real hardware.
1827 USE(parameters, command);
1828 #endif
1829 }
1830
1831
1832 void MacroAssembler::Log(TraceParameters parameters) {
1833
1834 #ifdef JS_SIMULATOR_ARM64
1835 // The arguments to the log pseudo instruction need to be contiguous in
1836 // memory, so make sure we don't try to emit a literal pool.
1837 InstructionAccurateScope scope(this, kLogLength / kInstructionSize);
1838
1839 Label start;
1840 bind(&start);
1841
1842 // Refer to simulator-a64.h for a description of the marker and its
1843 // arguments.
1844 hlt(kLogOpcode);
1845
1846 // VIXL_ASSERT(SizeOfCodeGeneratedSince(&start) == kLogParamsOffset);
1847 dc32(parameters);
1848 #else
1849 // Emit nothing on real hardware.
1850 USE(parameters);
1851 #endif
1852 }
1853
1854
1855 void MacroAssembler::EnableInstrumentation() {
1856 VIXL_ASSERT(!isprint(InstrumentStateEnable));
1857 InstructionAccurateScope scope(this, 1);
1858 movn(xzr, InstrumentStateEnable);
1859 }
1860
1861
1862 void MacroAssembler::DisableInstrumentation() {
1863 VIXL_ASSERT(!isprint(InstrumentStateDisable));
1864 InstructionAccurateScope scope(this, 1);
1865 movn(xzr, InstrumentStateDisable);
1866 }
1867
1868
1869 void MacroAssembler::AnnotateInstrumentation(const char* marker_name) {
1870 VIXL_ASSERT(strlen(marker_name) == 2);
1871
1872 // We allow only printable characters in the marker names. Unprintable
1873 // characters are reserved for controlling features of the instrumentation.
1874 VIXL_ASSERT(isprint(marker_name[0]) && isprint(marker_name[1]));
1875
1876 InstructionAccurateScope scope(this, 1);
1877 movn(xzr, (marker_name[1] << 8) | marker_name[0]);
1878 }
1879
1880
1881 void UseScratchRegisterScope::Open(MacroAssembler* masm) {
1882 VIXL_ASSERT(!initialised_);
1883 available_ = masm->TmpList();
1884 availablefp_ = masm->FPTmpList();
1885 old_available_ = available_->list();
1886 old_availablefp_ = availablefp_->list();
1887 VIXL_ASSERT(available_->type() == CPURegister::kRegister);
1888 VIXL_ASSERT(availablefp_->type() == CPURegister::kVRegister);
1889 #ifdef DEBUG
1890 initialised_ = true;
1891 #endif
1892 }
1893
1894
1895 void UseScratchRegisterScope::Close() {
1896 if (available_) {
1897 available_->set_list(old_available_);
1898 available_ = NULL;
1899 }
1900 if (availablefp_) {
1901 availablefp_->set_list(old_availablefp_);
1902 availablefp_ = NULL;
1903 }
1904 #ifdef DEBUG
1905 initialised_ = false;
1906 #endif
1907 }
1908
1909
1910 UseScratchRegisterScope::UseScratchRegisterScope(MacroAssembler* masm) {
1911 #ifdef DEBUG
1912 initialised_ = false;
1913 #endif
1914 Open(masm);
1915 }
1916
1917 // This allows deferred (and optional) initialisation of the scope.
1918 UseScratchRegisterScope::UseScratchRegisterScope()
1919 : available_(NULL), availablefp_(NULL),
1920 old_available_(0), old_availablefp_(0) {
1921 #ifdef DEBUG
1922 initialised_ = false;
1923 #endif
1924 }
1925
1926 UseScratchRegisterScope::~UseScratchRegisterScope() {
1927 Close();
1928 }
1929
1930
1931 bool UseScratchRegisterScope::IsAvailable(const CPURegister& reg) const {
1932 return available_->IncludesAliasOf(reg) || availablefp_->IncludesAliasOf(reg);
1933 }
1934
1935 bool UseScratchRegisterScope::HasAvailableRegister() const {
1936 return !available_->IsEmpty();
1937 }
1938
1939 Register UseScratchRegisterScope::AcquireSameSizeAs(const Register& reg) {
1940 int code = AcquireNextAvailable(available_).code();
1941 return Register(code, reg.size());
1942 }
1943
1944
1945 FPRegister UseScratchRegisterScope::AcquireSameSizeAs(const FPRegister& reg) {
1946 int code = AcquireNextAvailable(availablefp_).code();
1947 return FPRegister(code, reg.size());
1948 }
1949
1950
1951 void UseScratchRegisterScope::Release(const CPURegister& reg) {
1952 VIXL_ASSERT(initialised_);
1953 if (reg.IsRegister()) {
1954 ReleaseByCode(available_, reg.code());
1955 } else if (reg.IsFPRegister()) {
1956 ReleaseByCode(availablefp_, reg.code());
1957 } else {
1958 VIXL_ASSERT(reg.IsNone());
1959 }
1960 }
1961
1962
1963 void UseScratchRegisterScope::Include(const CPURegList& list) {
1964 VIXL_ASSERT(initialised_);
1965 if (list.type() == CPURegister::kRegister) {
1966 // Make sure that neither sp nor xzr are included the list.
1967 IncludeByRegList(available_, list.list() & ~(xzr.Bit() | sp.Bit()));
1968 } else {
1969 VIXL_ASSERT(list.type() == CPURegister::kVRegister);
1970 IncludeByRegList(availablefp_, list.list());
1971 }
1972 }
1973
1974
1975 void UseScratchRegisterScope::Include(const Register& reg1,
1976 const Register& reg2,
1977 const Register& reg3,
1978 const Register& reg4) {
1979 VIXL_ASSERT(initialised_);
1980 RegList include = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit();
1981 // Make sure that neither sp nor xzr are included the list.
1982 include &= ~(xzr.Bit() | sp.Bit());
1983
1984 IncludeByRegList(available_, include);
1985 }
1986
1987
1988 void UseScratchRegisterScope::Include(const FPRegister& reg1,
1989 const FPRegister& reg2,
1990 const FPRegister& reg3,
1991 const FPRegister& reg4) {
1992 RegList include = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit();
1993 IncludeByRegList(availablefp_, include);
1994 }
1995
1996
1997 void UseScratchRegisterScope::Exclude(const CPURegList& list) {
1998 if (list.type() == CPURegister::kRegister) {
1999 ExcludeByRegList(available_, list.list());
2000 } else {
2001 VIXL_ASSERT(list.type() == CPURegister::kVRegister);
2002 ExcludeByRegList(availablefp_, list.list());
2003 }
2004 }
2005
2006
2007 void UseScratchRegisterScope::Exclude(const Register& reg1,
2008 const Register& reg2,
2009 const Register& reg3,
2010 const Register& reg4) {
2011 RegList exclude = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit();
2012 ExcludeByRegList(available_, exclude);
2013 }
2014
2015
2016 void UseScratchRegisterScope::Exclude(const FPRegister& reg1,
2017 const FPRegister& reg2,
2018 const FPRegister& reg3,
2019 const FPRegister& reg4) {
2020 RegList excludefp = reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit();
2021 ExcludeByRegList(availablefp_, excludefp);
2022 }
2023
2024
2025 void UseScratchRegisterScope::Exclude(const CPURegister& reg1,
2026 const CPURegister& reg2,
2027 const CPURegister& reg3,
2028 const CPURegister& reg4) {
2029 RegList exclude = 0;
2030 RegList excludefp = 0;
2031
2032 const CPURegister regs[] = {reg1, reg2, reg3, reg4};
2033
2034 for (unsigned i = 0; i < (sizeof(regs) / sizeof(regs[0])); i++) {
2035 if (regs[i].IsRegister()) {
2036 exclude |= regs[i].Bit();
2037 } else if (regs[i].IsFPRegister()) {
2038 excludefp |= regs[i].Bit();
2039 } else {
2040 VIXL_ASSERT(regs[i].IsNone());
2041 }
2042 }
2043
2044 ExcludeByRegList(available_, exclude);
2045 ExcludeByRegList(availablefp_, excludefp);
2046 }
2047
2048
2049 void UseScratchRegisterScope::ExcludeAll() {
2050 ExcludeByRegList(available_, available_->list());
2051 ExcludeByRegList(availablefp_, availablefp_->list());
2052 }
2053
2054
2055 CPURegister UseScratchRegisterScope::AcquireNextAvailable(
2056 CPURegList* available) {
2057 VIXL_CHECK(!available->IsEmpty());
2058 CPURegister result = available->PopLowestIndex();
2059 VIXL_ASSERT(!AreAliased(result, xzr, sp));
2060 return result;
2061 }
2062
2063
2064 void UseScratchRegisterScope::ReleaseByCode(CPURegList* available, int code) {
2065 ReleaseByRegList(available, static_cast<RegList>(1) << code);
2066 }
2067
2068
2069 void UseScratchRegisterScope::ReleaseByRegList(CPURegList* available,
2070 RegList regs) {
2071 available->set_list(available->list() | regs);
2072 }
2073
2074
2075 void UseScratchRegisterScope::IncludeByRegList(CPURegList* available,
2076 RegList regs) {
2077 available->set_list(available->list() | regs);
2078 }
2079
2080
2081 void UseScratchRegisterScope::ExcludeByRegList(CPURegList* available,
2082 RegList exclude) {
2083 available->set_list(available->list() & ~exclude);
2084 }
2085
2086 } // namespace vixl