V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
macro-assembler-arm64.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if V8_TARGET_ARCH_ARM64
6 
7 #include "src/assembler.h"
8 #include "src/base/bits.h"
9 #include "src/base/division-by-constant.h"
10 #include "src/bootstrapper.h"
11 #include "src/callable.h"
12 #include "src/code-factory.h"
13 #include "src/code-stubs.h"
14 #include "src/counters.h"
15 #include "src/debug/debug.h"
16 #include "src/external-reference-table.h"
17 #include "src/frame-constants.h"
18 #include "src/frames-inl.h"
19 #include "src/macro-assembler-inl.h"
20 #include "src/register-configuration.h"
21 #include "src/runtime/runtime.h"
22 #include "src/snapshot/embedded-data.h"
23 #include "src/snapshot/snapshot.h"
24 #include "src/wasm/wasm-code-manager.h"
25 
26 // Satisfy cpplint check, but don't include platform-specific header. It is
27 // included recursively via macro-assembler.h.
28 #if 0
29 #include "src/arm64/macro-assembler-arm64.h"
30 #endif
31 
32 namespace v8 {
33 namespace internal {
34 
35 MacroAssembler::MacroAssembler(Isolate* isolate,
36  const AssemblerOptions& options, void* buffer,
37  int size, CodeObjectRequired create_code_object)
38  : TurboAssembler(isolate, options, buffer, size, create_code_object) {
39  if (create_code_object == CodeObjectRequired::kYes) {
40  // Unlike TurboAssembler, which can be used off the main thread and may not
41  // allocate, macro assembler creates its own copy of the self-reference
42  // marker in order to disambiguate between self-references during nested
43  // code generation (e.g.: codegen of the current object triggers stub
44  // compilation through CodeStub::GetCode()).
45  code_object_ = Handle<HeapObject>::New(
46  *isolate->factory()->NewSelfReferenceMarker(), isolate);
47  }
48 }
49 
50 CPURegList TurboAssembler::DefaultTmpList() { return CPURegList(ip0, ip1); }
51 
52 CPURegList TurboAssembler::DefaultFPTmpList() {
53  return CPURegList(fp_scratch1, fp_scratch2);
54 }
55 
56 int TurboAssembler::RequiredStackSizeForCallerSaved(SaveFPRegsMode fp_mode,
57  Register exclusion) const {
58  int bytes = 0;
59  auto list = kCallerSaved;
60  // We only allow one exclusion register, so if the list is of even length
61  // before exclusions, it must still be afterwards, to maintain alignment.
62  // Therefore, we can ignore the exclusion register in the computation.
63  // However, we leave it in the argument list to mirror the prototype for
64  // Push/PopCallerSaved().
65 
66 #if defined(V8_OS_WIN)
67  // X18 is excluded from caller-saved register list on Windows ARM64 which
68  // makes caller-saved registers in odd number. padreg is used accordingly
69  // to maintain the alignment.
70  DCHECK_EQ(list.Count() % 2, 1);
71  if (exclusion.Is(no_reg)) {
72  bytes += kXRegSizeInBits / 8;
73  } else {
74  bytes -= kXRegSizeInBits / 8;
75  }
76 #else
77  DCHECK_EQ(list.Count() % 2, 0);
78  USE(exclusion);
79 #endif
80 
81  bytes += list.Count() * kXRegSizeInBits / 8;
82 
83  if (fp_mode == kSaveFPRegs) {
84  DCHECK_EQ(kCallerSavedV.Count() % 2, 0);
85  bytes += kCallerSavedV.Count() * kDRegSizeInBits / 8;
86  }
87  return bytes;
88 }
89 
90 int TurboAssembler::PushCallerSaved(SaveFPRegsMode fp_mode,
91  Register exclusion) {
92  int bytes = 0;
93  auto list = kCallerSaved;
94 
95 #if defined(V8_OS_WIN)
96  // X18 is excluded from caller-saved register list on Windows ARM64, use
97  // padreg accordingly to maintain alignment.
98  if (!exclusion.Is(no_reg)) {
99  list.Remove(exclusion);
100  } else {
101  list.Combine(padreg);
102  }
103 #else
104  if (!exclusion.Is(no_reg)) {
105  // Replace the excluded register with padding to maintain alignment.
106  list.Remove(exclusion);
107  list.Combine(padreg);
108  }
109 #endif
110 
111  DCHECK_EQ(list.Count() % 2, 0);
112  PushCPURegList(list);
113  bytes += list.Count() * kXRegSizeInBits / 8;
114 
115  if (fp_mode == kSaveFPRegs) {
116  DCHECK_EQ(kCallerSavedV.Count() % 2, 0);
117  PushCPURegList(kCallerSavedV);
118  bytes += kCallerSavedV.Count() * kDRegSizeInBits / 8;
119  }
120  return bytes;
121 }
122 
123 int TurboAssembler::PopCallerSaved(SaveFPRegsMode fp_mode, Register exclusion) {
124  int bytes = 0;
125  if (fp_mode == kSaveFPRegs) {
126  DCHECK_EQ(kCallerSavedV.Count() % 2, 0);
127  PopCPURegList(kCallerSavedV);
128  bytes += kCallerSavedV.Count() * kDRegSizeInBits / 8;
129  }
130 
131  auto list = kCallerSaved;
132 
133 #if defined(V8_OS_WIN)
134  // X18 is excluded from caller-saved register list on Windows ARM64, use
135  // padreg accordingly to maintain alignment.
136  if (!exclusion.Is(no_reg)) {
137  list.Remove(exclusion);
138  } else {
139  list.Combine(padreg);
140  }
141 #else
142  if (!exclusion.Is(no_reg)) {
143  // Replace the excluded register with padding to maintain alignment.
144  list.Remove(exclusion);
145  list.Combine(padreg);
146  }
147 #endif
148 
149  DCHECK_EQ(list.Count() % 2, 0);
150  PopCPURegList(list);
151  bytes += list.Count() * kXRegSizeInBits / 8;
152 
153  return bytes;
154 }
155 
156 void TurboAssembler::LogicalMacro(const Register& rd, const Register& rn,
157  const Operand& operand, LogicalOp op) {
158  UseScratchRegisterScope temps(this);
159 
160  if (operand.NeedsRelocation(this)) {
161  Register temp = temps.AcquireX();
162  Ldr(temp, operand.immediate());
163  Logical(rd, rn, temp, op);
164 
165  } else if (operand.IsImmediate()) {
166  int64_t immediate = operand.ImmediateValue();
167  unsigned reg_size = rd.SizeInBits();
168 
169  // If the operation is NOT, invert the operation and immediate.
170  if ((op & NOT) == NOT) {
171  op = static_cast<LogicalOp>(op & ~NOT);
172  immediate = ~immediate;
173  }
174 
175  // Ignore the top 32 bits of an immediate if we're moving to a W register.
176  if (rd.Is32Bits()) {
177  // Check that the top 32 bits are consistent.
178  DCHECK(((immediate >> kWRegSizeInBits) == 0) ||
179  ((immediate >> kWRegSizeInBits) == -1));
180  immediate &= kWRegMask;
181  }
182 
183  DCHECK(rd.Is64Bits() || is_uint32(immediate));
184 
185  // Special cases for all set or all clear immediates.
186  if (immediate == 0) {
187  switch (op) {
188  case AND:
189  Mov(rd, 0);
190  return;
191  case ORR: // Fall through.
192  case EOR:
193  Mov(rd, rn);
194  return;
195  case ANDS: // Fall through.
196  case BICS:
197  break;
198  default:
199  UNREACHABLE();
200  }
201  } else if ((rd.Is64Bits() && (immediate == -1L)) ||
202  (rd.Is32Bits() && (immediate == 0xFFFFFFFFL))) {
203  switch (op) {
204  case AND:
205  Mov(rd, rn);
206  return;
207  case ORR:
208  Mov(rd, immediate);
209  return;
210  case EOR:
211  Mvn(rd, rn);
212  return;
213  case ANDS: // Fall through.
214  case BICS:
215  break;
216  default:
217  UNREACHABLE();
218  }
219  }
220 
221  unsigned n, imm_s, imm_r;
222  if (IsImmLogical(immediate, reg_size, &n, &imm_s, &imm_r)) {
223  // Immediate can be encoded in the instruction.
224  LogicalImmediate(rd, rn, n, imm_s, imm_r, op);
225  } else {
226  // Immediate can't be encoded: synthesize using move immediate.
227  Register temp = temps.AcquireSameSizeAs(rn);
228 
229  // If the left-hand input is the stack pointer, we can't pre-shift the
230  // immediate, as the encoding won't allow the subsequent post shift.
231  PreShiftImmMode mode = rn.Is(sp) ? kNoShift : kAnyShift;
232  Operand imm_operand = MoveImmediateForShiftedOp(temp, immediate, mode);
233 
234  if (rd.IsSP()) {
235  // If rd is the stack pointer we cannot use it as the destination
236  // register so we use the temp register as an intermediate again.
237  Logical(temp, rn, imm_operand, op);
238  Mov(sp, temp);
239  } else {
240  Logical(rd, rn, imm_operand, op);
241  }
242  }
243 
244  } else if (operand.IsExtendedRegister()) {
245  DCHECK(operand.reg().SizeInBits() <= rd.SizeInBits());
246  // Add/sub extended supports shift <= 4. We want to support exactly the
247  // same modes here.
248  DCHECK_LE(operand.shift_amount(), 4);
249  DCHECK(operand.reg().Is64Bits() ||
250  ((operand.extend() != UXTX) && (operand.extend() != SXTX)));
251  Register temp = temps.AcquireSameSizeAs(rn);
252  EmitExtendShift(temp, operand.reg(), operand.extend(),
253  operand.shift_amount());
254  Logical(rd, rn, temp, op);
255 
256  } else {
257  // The operand can be encoded in the instruction.
258  DCHECK(operand.IsShiftedRegister());
259  Logical(rd, rn, operand, op);
260  }
261 }
262 
263 void TurboAssembler::Mov(const Register& rd, uint64_t imm) {
264  DCHECK(allow_macro_instructions());
265  DCHECK(is_uint32(imm) || is_int32(imm) || rd.Is64Bits());
266  DCHECK(!rd.IsZero());
267 
268  // TODO(all) extend to support more immediates.
269  //
270  // Immediates on Aarch64 can be produced using an initial value, and zero to
271  // three move keep operations.
272  //
273  // Initial values can be generated with:
274  // 1. 64-bit move zero (movz).
275  // 2. 32-bit move inverted (movn).
276  // 3. 64-bit move inverted.
277  // 4. 32-bit orr immediate.
278  // 5. 64-bit orr immediate.
279  // Move-keep may then be used to modify each of the 16-bit half-words.
280  //
281  // The code below supports all five initial value generators, and
282  // applying move-keep operations to move-zero and move-inverted initial
283  // values.
284 
285  // Try to move the immediate in one instruction, and if that fails, switch to
286  // using multiple instructions.
287  if (!TryOneInstrMoveImmediate(rd, imm)) {
288  unsigned reg_size = rd.SizeInBits();
289 
290  // Generic immediate case. Imm will be represented by
291  // [imm3, imm2, imm1, imm0], where each imm is 16 bits.
292  // A move-zero or move-inverted is generated for the first non-zero or
293  // non-0xFFFF immX, and a move-keep for subsequent non-zero immX.
294 
295  uint64_t ignored_halfword = 0;
296  bool invert_move = false;
297  // If the number of 0xFFFF halfwords is greater than the number of 0x0000
298  // halfwords, it's more efficient to use move-inverted.
299  if (CountClearHalfWords(~imm, reg_size) >
300  CountClearHalfWords(imm, reg_size)) {
301  ignored_halfword = 0xFFFFL;
302  invert_move = true;
303  }
304 
305  // Mov instructions can't move immediate values into the stack pointer, so
306  // set up a temporary register, if needed.
307  UseScratchRegisterScope temps(this);
308  Register temp = rd.IsSP() ? temps.AcquireSameSizeAs(rd) : rd;
309 
310  // Iterate through the halfwords. Use movn/movz for the first non-ignored
311  // halfword, and movk for subsequent halfwords.
312  DCHECK_EQ(reg_size % 16, 0);
313  bool first_mov_done = false;
314  for (int i = 0; i < (rd.SizeInBits() / 16); i++) {
315  uint64_t imm16 = (imm >> (16 * i)) & 0xFFFFL;
316  if (imm16 != ignored_halfword) {
317  if (!first_mov_done) {
318  if (invert_move) {
319  movn(temp, (~imm16) & 0xFFFFL, 16 * i);
320  } else {
321  movz(temp, imm16, 16 * i);
322  }
323  first_mov_done = true;
324  } else {
325  // Construct a wider constant.
326  movk(temp, imm16, 16 * i);
327  }
328  }
329  }
330  DCHECK(first_mov_done);
331 
332  // Move the temporary if the original destination register was the stack
333  // pointer.
334  if (rd.IsSP()) {
335  mov(rd, temp);
336  }
337  }
338 }
339 
340 void TurboAssembler::Mov(const Register& rd, const Operand& operand,
341  DiscardMoveMode discard_mode) {
342  DCHECK(allow_macro_instructions());
343  DCHECK(!rd.IsZero());
344 
345  // Provide a swap register for instructions that need to write into the
346  // system stack pointer (and can't do this inherently).
347  UseScratchRegisterScope temps(this);
348  Register dst = (rd.IsSP()) ? temps.AcquireSameSizeAs(rd) : rd;
349 
350  if (operand.NeedsRelocation(this)) {
351  if (FLAG_embedded_builtins) {
352  if (root_array_available_ && options().isolate_independent_code) {
353  if (operand.ImmediateRMode() == RelocInfo::EXTERNAL_REFERENCE) {
354  Address addr = static_cast<Address>(operand.ImmediateValue());
355  ExternalReference reference = bit_cast<ExternalReference>(addr);
356  IndirectLoadExternalReference(rd, reference);
357  return;
358  } else if (operand.ImmediateRMode() == RelocInfo::EMBEDDED_OBJECT) {
359  Handle<HeapObject> x(
360  reinterpret_cast<Address*>(operand.ImmediateValue()));
361  IndirectLoadConstant(rd, x);
362  return;
363  }
364  }
365  }
366  Ldr(dst, operand);
367  } else if (operand.IsImmediate()) {
368  // Call the macro assembler for generic immediates.
369  Mov(dst, operand.ImmediateValue());
370  } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) {
371  // Emit a shift instruction if moving a shifted register. This operation
372  // could also be achieved using an orr instruction (like orn used by Mvn),
373  // but using a shift instruction makes the disassembly clearer.
374  EmitShift(dst, operand.reg(), operand.shift(), operand.shift_amount());
375  } else if (operand.IsExtendedRegister()) {
376  // Emit an extend instruction if moving an extended register. This handles
377  // extend with post-shift operations, too.
378  EmitExtendShift(dst, operand.reg(), operand.extend(),
379  operand.shift_amount());
380  } else {
381  // Otherwise, emit a register move only if the registers are distinct, or
382  // if they are not X registers.
383  //
384  // Note that mov(w0, w0) is not a no-op because it clears the top word of
385  // x0. A flag is provided (kDiscardForSameWReg) if a move between the same W
386  // registers is not required to clear the top word of the X register. In
387  // this case, the instruction is discarded.
388  //
389  // If sp is an operand, add #0 is emitted, otherwise, orr #0.
390  if (!rd.Is(operand.reg()) || (rd.Is32Bits() &&
391  (discard_mode == kDontDiscardForSameWReg))) {
392  Assembler::mov(rd, operand.reg());
393  }
394  // This case can handle writes into the system stack pointer directly.
395  dst = rd;
396  }
397 
398  // Copy the result to the system stack pointer.
399  if (!dst.Is(rd)) {
400  DCHECK(rd.IsSP());
401  Assembler::mov(rd, dst);
402  }
403 }
404 
405 void TurboAssembler::Movi16bitHelper(const VRegister& vd, uint64_t imm) {
406  DCHECK(is_uint16(imm));
407  int byte1 = (imm & 0xFF);
408  int byte2 = ((imm >> 8) & 0xFF);
409  if (byte1 == byte2) {
410  movi(vd.Is64Bits() ? vd.V8B() : vd.V16B(), byte1);
411  } else if (byte1 == 0) {
412  movi(vd, byte2, LSL, 8);
413  } else if (byte2 == 0) {
414  movi(vd, byte1);
415  } else if (byte1 == 0xFF) {
416  mvni(vd, ~byte2 & 0xFF, LSL, 8);
417  } else if (byte2 == 0xFF) {
418  mvni(vd, ~byte1 & 0xFF);
419  } else {
420  UseScratchRegisterScope temps(this);
421  Register temp = temps.AcquireW();
422  movz(temp, imm);
423  dup(vd, temp);
424  }
425 }
426 
427 void TurboAssembler::Movi32bitHelper(const VRegister& vd, uint64_t imm) {
428  DCHECK(is_uint32(imm));
429 
430  uint8_t bytes[sizeof(imm)];
431  memcpy(bytes, &imm, sizeof(imm));
432 
433  // All bytes are either 0x00 or 0xFF.
434  {
435  bool all0orff = true;
436  for (int i = 0; i < 4; ++i) {
437  if ((bytes[i] != 0) && (bytes[i] != 0xFF)) {
438  all0orff = false;
439  break;
440  }
441  }
442 
443  if (all0orff == true) {
444  movi(vd.Is64Bits() ? vd.V1D() : vd.V2D(), ((imm << 32) | imm));
445  return;
446  }
447  }
448 
449  // Of the 4 bytes, only one byte is non-zero.
450  for (int i = 0; i < 4; i++) {
451  if ((imm & (0xFF << (i * 8))) == imm) {
452  movi(vd, bytes[i], LSL, i * 8);
453  return;
454  }
455  }
456 
457  // Of the 4 bytes, only one byte is not 0xFF.
458  for (int i = 0; i < 4; i++) {
459  uint32_t mask = ~(0xFF << (i * 8));
460  if ((imm & mask) == mask) {
461  mvni(vd, ~bytes[i] & 0xFF, LSL, i * 8);
462  return;
463  }
464  }
465 
466  // Immediate is of the form 0x00MMFFFF.
467  if ((imm & 0xFF00FFFF) == 0x0000FFFF) {
468  movi(vd, bytes[2], MSL, 16);
469  return;
470  }
471 
472  // Immediate is of the form 0x0000MMFF.
473  if ((imm & 0xFFFF00FF) == 0x000000FF) {
474  movi(vd, bytes[1], MSL, 8);
475  return;
476  }
477 
478  // Immediate is of the form 0xFFMM0000.
479  if ((imm & 0xFF00FFFF) == 0xFF000000) {
480  mvni(vd, ~bytes[2] & 0xFF, MSL, 16);
481  return;
482  }
483  // Immediate is of the form 0xFFFFMM00.
484  if ((imm & 0xFFFF00FF) == 0xFFFF0000) {
485  mvni(vd, ~bytes[1] & 0xFF, MSL, 8);
486  return;
487  }
488 
489  // Top and bottom 16-bits are equal.
490  if (((imm >> 16) & 0xFFFF) == (imm & 0xFFFF)) {
491  Movi16bitHelper(vd.Is64Bits() ? vd.V4H() : vd.V8H(), imm & 0xFFFF);
492  return;
493  }
494 
495  // Default case.
496  {
497  UseScratchRegisterScope temps(this);
498  Register temp = temps.AcquireW();
499  Mov(temp, imm);
500  dup(vd, temp);
501  }
502 }
503 
504 void TurboAssembler::Movi64bitHelper(const VRegister& vd, uint64_t imm) {
505  // All bytes are either 0x00 or 0xFF.
506  {
507  bool all0orff = true;
508  for (int i = 0; i < 8; ++i) {
509  int byteval = (imm >> (i * 8)) & 0xFF;
510  if (byteval != 0 && byteval != 0xFF) {
511  all0orff = false;
512  break;
513  }
514  }
515  if (all0orff == true) {
516  movi(vd, imm);
517  return;
518  }
519  }
520 
521  // Top and bottom 32-bits are equal.
522  if (((imm >> 32) & 0xFFFFFFFF) == (imm & 0xFFFFFFFF)) {
523  Movi32bitHelper(vd.Is64Bits() ? vd.V2S() : vd.V4S(), imm & 0xFFFFFFFF);
524  return;
525  }
526 
527  // Default case.
528  {
529  UseScratchRegisterScope temps(this);
530  Register temp = temps.AcquireX();
531  Mov(temp, imm);
532  if (vd.Is1D()) {
533  mov(vd.D(), 0, temp);
534  } else {
535  dup(vd.V2D(), temp);
536  }
537  }
538 }
539 
540 void TurboAssembler::Movi(const VRegister& vd, uint64_t imm, Shift shift,
541  int shift_amount) {
542  DCHECK(allow_macro_instructions());
543  if (shift_amount != 0 || shift != LSL) {
544  movi(vd, imm, shift, shift_amount);
545  } else if (vd.Is8B() || vd.Is16B()) {
546  // 8-bit immediate.
547  DCHECK(is_uint8(imm));
548  movi(vd, imm);
549  } else if (vd.Is4H() || vd.Is8H()) {
550  // 16-bit immediate.
551  Movi16bitHelper(vd, imm);
552  } else if (vd.Is2S() || vd.Is4S()) {
553  // 32-bit immediate.
554  Movi32bitHelper(vd, imm);
555  } else {
556  // 64-bit immediate.
557  Movi64bitHelper(vd, imm);
558  }
559 }
560 
561 void TurboAssembler::Movi(const VRegister& vd, uint64_t hi, uint64_t lo) {
562  // TODO(all): Move 128-bit values in a more efficient way.
563  DCHECK(vd.Is128Bits());
564  UseScratchRegisterScope temps(this);
565  Movi(vd.V2D(), lo);
566  Register temp = temps.AcquireX();
567  Mov(temp, hi);
568  Ins(vd.V2D(), 1, temp);
569 }
570 
571 void TurboAssembler::Mvn(const Register& rd, const Operand& operand) {
572  DCHECK(allow_macro_instructions());
573 
574  if (operand.NeedsRelocation(this)) {
575  Ldr(rd, operand.immediate());
576  mvn(rd, rd);
577 
578  } else if (operand.IsImmediate()) {
579  // Call the macro assembler for generic immediates.
580  Mov(rd, ~operand.ImmediateValue());
581 
582  } else if (operand.IsExtendedRegister()) {
583  // Emit two instructions for the extend case. This differs from Mov, as
584  // the extend and invert can't be achieved in one instruction.
585  EmitExtendShift(rd, operand.reg(), operand.extend(),
586  operand.shift_amount());
587  mvn(rd, rd);
588 
589  } else {
590  mvn(rd, operand);
591  }
592 }
593 
594 unsigned TurboAssembler::CountClearHalfWords(uint64_t imm, unsigned reg_size) {
595  DCHECK_EQ(reg_size % 8, 0);
596  int count = 0;
597  for (unsigned i = 0; i < (reg_size / 16); i++) {
598  if ((imm & 0xFFFF) == 0) {
599  count++;
600  }
601  imm >>= 16;
602  }
603  return count;
604 }
605 
606 
607 // The movz instruction can generate immediates containing an arbitrary 16-bit
608 // half-word, with remaining bits clear, eg. 0x00001234, 0x0000123400000000.
609 bool TurboAssembler::IsImmMovz(uint64_t imm, unsigned reg_size) {
610  DCHECK((reg_size == kXRegSizeInBits) || (reg_size == kWRegSizeInBits));
611  return CountClearHalfWords(imm, reg_size) >= ((reg_size / 16) - 1);
612 }
613 
614 // The movn instruction can generate immediates containing an arbitrary 16-bit
615 // half-word, with remaining bits set, eg. 0xFFFF1234, 0xFFFF1234FFFFFFFF.
616 bool TurboAssembler::IsImmMovn(uint64_t imm, unsigned reg_size) {
617  return IsImmMovz(~imm, reg_size);
618 }
619 
620 void TurboAssembler::ConditionalCompareMacro(const Register& rn,
621  const Operand& operand,
622  StatusFlags nzcv, Condition cond,
623  ConditionalCompareOp op) {
624  DCHECK((cond != al) && (cond != nv));
625  if (operand.NeedsRelocation(this)) {
626  UseScratchRegisterScope temps(this);
627  Register temp = temps.AcquireX();
628  Ldr(temp, operand.immediate());
629  ConditionalCompareMacro(rn, temp, nzcv, cond, op);
630 
631  } else if ((operand.IsShiftedRegister() && (operand.shift_amount() == 0)) ||
632  (operand.IsImmediate() &&
633  IsImmConditionalCompare(operand.ImmediateValue()))) {
634  // The immediate can be encoded in the instruction, or the operand is an
635  // unshifted register: call the assembler.
636  ConditionalCompare(rn, operand, nzcv, cond, op);
637 
638  } else {
639  // The operand isn't directly supported by the instruction: perform the
640  // operation on a temporary register.
641  UseScratchRegisterScope temps(this);
642  Register temp = temps.AcquireSameSizeAs(rn);
643  Mov(temp, operand);
644  ConditionalCompare(rn, temp, nzcv, cond, op);
645  }
646 }
647 
648 void TurboAssembler::Csel(const Register& rd, const Register& rn,
649  const Operand& operand, Condition cond) {
650  DCHECK(allow_macro_instructions());
651  DCHECK(!rd.IsZero());
652  DCHECK((cond != al) && (cond != nv));
653  if (operand.IsImmediate()) {
654  // Immediate argument. Handle special cases of 0, 1 and -1 using zero
655  // register.
656  int64_t imm = operand.ImmediateValue();
657  Register zr = AppropriateZeroRegFor(rn);
658  if (imm == 0) {
659  csel(rd, rn, zr, cond);
660  } else if (imm == 1) {
661  csinc(rd, rn, zr, cond);
662  } else if (imm == -1) {
663  csinv(rd, rn, zr, cond);
664  } else {
665  UseScratchRegisterScope temps(this);
666  Register temp = temps.AcquireSameSizeAs(rn);
667  Mov(temp, imm);
668  csel(rd, rn, temp, cond);
669  }
670  } else if (operand.IsShiftedRegister() && (operand.shift_amount() == 0)) {
671  // Unshifted register argument.
672  csel(rd, rn, operand.reg(), cond);
673  } else {
674  // All other arguments.
675  UseScratchRegisterScope temps(this);
676  Register temp = temps.AcquireSameSizeAs(rn);
677  Mov(temp, operand);
678  csel(rd, rn, temp, cond);
679  }
680 }
681 
682 bool TurboAssembler::TryOneInstrMoveImmediate(const Register& dst,
683  int64_t imm) {
684  unsigned n, imm_s, imm_r;
685  int reg_size = dst.SizeInBits();
686  if (IsImmMovz(imm, reg_size) && !dst.IsSP()) {
687  // Immediate can be represented in a move zero instruction. Movz can't write
688  // to the stack pointer.
689  movz(dst, imm);
690  return true;
691  } else if (IsImmMovn(imm, reg_size) && !dst.IsSP()) {
692  // Immediate can be represented in a move not instruction. Movn can't write
693  // to the stack pointer.
694  movn(dst, dst.Is64Bits() ? ~imm : (~imm & kWRegMask));
695  return true;
696  } else if (IsImmLogical(imm, reg_size, &n, &imm_s, &imm_r)) {
697  // Immediate can be represented in a logical orr instruction.
698  LogicalImmediate(dst, AppropriateZeroRegFor(dst), n, imm_s, imm_r, ORR);
699  return true;
700  }
701  return false;
702 }
703 
704 Operand TurboAssembler::MoveImmediateForShiftedOp(const Register& dst,
705  int64_t imm,
706  PreShiftImmMode mode) {
707  int reg_size = dst.SizeInBits();
708  // Encode the immediate in a single move instruction, if possible.
709  if (TryOneInstrMoveImmediate(dst, imm)) {
710  // The move was successful; nothing to do here.
711  } else {
712  // Pre-shift the immediate to the least-significant bits of the register.
713  int shift_low = CountTrailingZeros(imm, reg_size);
714  if (mode == kLimitShiftForSP) {
715  // When applied to the stack pointer, the subsequent arithmetic operation
716  // can use the extend form to shift left by a maximum of four bits. Right
717  // shifts are not allowed, so we filter them out later before the new
718  // immediate is tested.
719  shift_low = std::min(shift_low, 4);
720  }
721  int64_t imm_low = imm >> shift_low;
722 
723  // Pre-shift the immediate to the most-significant bits of the register. We
724  // insert set bits in the least-significant bits, as this creates a
725  // different immediate that may be encodable using movn or orr-immediate.
726  // If this new immediate is encodable, the set bits will be eliminated by
727  // the post shift on the following instruction.
728  int shift_high = CountLeadingZeros(imm, reg_size);
729  int64_t imm_high = (imm << shift_high) | ((INT64_C(1) << shift_high) - 1);
730 
731  if ((mode != kNoShift) && TryOneInstrMoveImmediate(dst, imm_low)) {
732  // The new immediate has been moved into the destination's low bits:
733  // return a new leftward-shifting operand.
734  return Operand(dst, LSL, shift_low);
735  } else if ((mode == kAnyShift) && TryOneInstrMoveImmediate(dst, imm_high)) {
736  // The new immediate has been moved into the destination's high bits:
737  // return a new rightward-shifting operand.
738  return Operand(dst, LSR, shift_high);
739  } else {
740  // Use the generic move operation to set up the immediate.
741  Mov(dst, imm);
742  }
743  }
744  return Operand(dst);
745 }
746 
747 void TurboAssembler::AddSubMacro(const Register& rd, const Register& rn,
748  const Operand& operand, FlagsUpdate S,
749  AddSubOp op) {
750  if (operand.IsZero() && rd.Is(rn) && rd.Is64Bits() && rn.Is64Bits() &&
751  !operand.NeedsRelocation(this) && (S == LeaveFlags)) {
752  // The instruction would be a nop. Avoid generating useless code.
753  return;
754  }
755 
756  if (operand.NeedsRelocation(this)) {
757  UseScratchRegisterScope temps(this);
758  Register temp = temps.AcquireX();
759  Ldr(temp, operand.immediate());
760  AddSubMacro(rd, rn, temp, S, op);
761  } else if ((operand.IsImmediate() &&
762  !IsImmAddSub(operand.ImmediateValue())) ||
763  (rn.IsZero() && !operand.IsShiftedRegister()) ||
764  (operand.IsShiftedRegister() && (operand.shift() == ROR))) {
765  UseScratchRegisterScope temps(this);
766  Register temp = temps.AcquireSameSizeAs(rn);
767  if (operand.IsImmediate()) {
768  PreShiftImmMode mode = kAnyShift;
769 
770  // If the destination or source register is the stack pointer, we can
771  // only pre-shift the immediate right by values supported in the add/sub
772  // extend encoding.
773  if (rd.Is(sp)) {
774  // If the destination is SP and flags will be set, we can't pre-shift
775  // the immediate at all.
776  mode = (S == SetFlags) ? kNoShift : kLimitShiftForSP;
777  } else if (rn.Is(sp)) {
778  mode = kLimitShiftForSP;
779  }
780 
781  Operand imm_operand =
782  MoveImmediateForShiftedOp(temp, operand.ImmediateValue(), mode);
783  AddSub(rd, rn, imm_operand, S, op);
784  } else {
785  Mov(temp, operand);
786  AddSub(rd, rn, temp, S, op);
787  }
788  } else {
789  AddSub(rd, rn, operand, S, op);
790  }
791 }
792 
793 void TurboAssembler::AddSubWithCarryMacro(const Register& rd,
794  const Register& rn,
795  const Operand& operand, FlagsUpdate S,
796  AddSubWithCarryOp op) {
797  DCHECK(rd.SizeInBits() == rn.SizeInBits());
798  UseScratchRegisterScope temps(this);
799 
800  if (operand.NeedsRelocation(this)) {
801  Register temp = temps.AcquireX();
802  Ldr(temp, operand.immediate());
803  AddSubWithCarryMacro(rd, rn, temp, S, op);
804 
805  } else if (operand.IsImmediate() ||
806  (operand.IsShiftedRegister() && (operand.shift() == ROR))) {
807  // Add/sub with carry (immediate or ROR shifted register.)
808  Register temp = temps.AcquireSameSizeAs(rn);
809  Mov(temp, operand);
810  AddSubWithCarry(rd, rn, temp, S, op);
811 
812  } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) {
813  // Add/sub with carry (shifted register).
814  DCHECK(operand.reg().SizeInBits() == rd.SizeInBits());
815  DCHECK(operand.shift() != ROR);
816  DCHECK(is_uintn(operand.shift_amount(),
817  rd.SizeInBits() == kXRegSizeInBits ? kXRegSizeInBitsLog2
818  : kWRegSizeInBitsLog2));
819  Register temp = temps.AcquireSameSizeAs(rn);
820  EmitShift(temp, operand.reg(), operand.shift(), operand.shift_amount());
821  AddSubWithCarry(rd, rn, temp, S, op);
822 
823  } else if (operand.IsExtendedRegister()) {
824  // Add/sub with carry (extended register).
825  DCHECK(operand.reg().SizeInBits() <= rd.SizeInBits());
826  // Add/sub extended supports a shift <= 4. We want to support exactly the
827  // same modes.
828  DCHECK_LE(operand.shift_amount(), 4);
829  DCHECK(operand.reg().Is64Bits() ||
830  ((operand.extend() != UXTX) && (operand.extend() != SXTX)));
831  Register temp = temps.AcquireSameSizeAs(rn);
832  EmitExtendShift(temp, operand.reg(), operand.extend(),
833  operand.shift_amount());
834  AddSubWithCarry(rd, rn, temp, S, op);
835 
836  } else {
837  // The addressing mode is directly supported by the instruction.
838  AddSubWithCarry(rd, rn, operand, S, op);
839  }
840 }
841 
842 void TurboAssembler::LoadStoreMacro(const CPURegister& rt,
843  const MemOperand& addr, LoadStoreOp op) {
844  int64_t offset = addr.offset();
845  unsigned size = CalcLSDataSize(op);
846 
847  // Check if an immediate offset fits in the immediate field of the
848  // appropriate instruction. If not, emit two instructions to perform
849  // the operation.
850  if (addr.IsImmediateOffset() && !IsImmLSScaled(offset, size) &&
851  !IsImmLSUnscaled(offset)) {
852  // Immediate offset that can't be encoded using unsigned or unscaled
853  // addressing modes.
854  UseScratchRegisterScope temps(this);
855  Register temp = temps.AcquireSameSizeAs(addr.base());
856  Mov(temp, addr.offset());
857  LoadStore(rt, MemOperand(addr.base(), temp), op);
858  } else if (addr.IsPostIndex() && !IsImmLSUnscaled(offset)) {
859  // Post-index beyond unscaled addressing range.
860  LoadStore(rt, MemOperand(addr.base()), op);
861  add(addr.base(), addr.base(), offset);
862  } else if (addr.IsPreIndex() && !IsImmLSUnscaled(offset)) {
863  // Pre-index beyond unscaled addressing range.
864  add(addr.base(), addr.base(), offset);
865  LoadStore(rt, MemOperand(addr.base()), op);
866  } else {
867  // Encodable in one load/store instruction.
868  LoadStore(rt, addr, op);
869  }
870 }
871 
872 void TurboAssembler::LoadStorePairMacro(const CPURegister& rt,
873  const CPURegister& rt2,
874  const MemOperand& addr,
875  LoadStorePairOp op) {
876  // TODO(all): Should we support register offset for load-store-pair?
877  DCHECK(!addr.IsRegisterOffset());
878 
879  int64_t offset = addr.offset();
880  unsigned size = CalcLSPairDataSize(op);
881 
882  // Check if the offset fits in the immediate field of the appropriate
883  // instruction. If not, emit two instructions to perform the operation.
884  if (IsImmLSPair(offset, size)) {
885  // Encodable in one load/store pair instruction.
886  LoadStorePair(rt, rt2, addr, op);
887  } else {
888  Register base = addr.base();
889  if (addr.IsImmediateOffset()) {
890  UseScratchRegisterScope temps(this);
891  Register temp = temps.AcquireSameSizeAs(base);
892  Add(temp, base, offset);
893  LoadStorePair(rt, rt2, MemOperand(temp), op);
894  } else if (addr.IsPostIndex()) {
895  LoadStorePair(rt, rt2, MemOperand(base), op);
896  Add(base, base, offset);
897  } else {
898  DCHECK(addr.IsPreIndex());
899  Add(base, base, offset);
900  LoadStorePair(rt, rt2, MemOperand(base), op);
901  }
902  }
903 }
904 
905 bool TurboAssembler::NeedExtraInstructionsOrRegisterBranch(
906  Label* label, ImmBranchType b_type) {
907  bool need_longer_range = false;
908  // There are two situations in which we care about the offset being out of
909  // range:
910  // - The label is bound but too far away.
911  // - The label is not bound but linked, and the previous branch
912  // instruction in the chain is too far away.
913  if (label->is_bound() || label->is_linked()) {
914  need_longer_range =
915  !Instruction::IsValidImmPCOffset(b_type, label->pos() - pc_offset());
916  }
917  if (!need_longer_range && !label->is_bound()) {
918  int max_reachable_pc = pc_offset() + Instruction::ImmBranchRange(b_type);
919  unresolved_branches_.insert(
920  std::pair<int, FarBranchInfo>(max_reachable_pc,
921  FarBranchInfo(pc_offset(), label)));
922  // Also maintain the next pool check.
923  next_veneer_pool_check_ =
924  Min(next_veneer_pool_check_,
925  max_reachable_pc - kVeneerDistanceCheckMargin);
926  }
927  return need_longer_range;
928 }
929 
930 void TurboAssembler::Adr(const Register& rd, Label* label, AdrHint hint) {
931  DCHECK(allow_macro_instructions());
932  DCHECK(!rd.IsZero());
933 
934  if (hint == kAdrNear) {
935  adr(rd, label);
936  return;
937  }
938 
939  DCHECK_EQ(hint, kAdrFar);
940  if (label->is_bound()) {
941  int label_offset = label->pos() - pc_offset();
942  if (Instruction::IsValidPCRelOffset(label_offset)) {
943  adr(rd, label);
944  } else {
945  DCHECK_LE(label_offset, 0);
946  int min_adr_offset = -(1 << (Instruction::ImmPCRelRangeBitwidth - 1));
947  adr(rd, min_adr_offset);
948  Add(rd, rd, label_offset - min_adr_offset);
949  }
950  } else {
951  UseScratchRegisterScope temps(this);
952  Register scratch = temps.AcquireX();
953 
954  InstructionAccurateScope scope(
955  this, PatchingAssembler::kAdrFarPatchableNInstrs);
956  adr(rd, label);
957  for (int i = 0; i < PatchingAssembler::kAdrFarPatchableNNops; ++i) {
958  nop(ADR_FAR_NOP);
959  }
960  movz(scratch, 0);
961  }
962 }
963 
964 void TurboAssembler::B(Label* label, BranchType type, Register reg, int bit) {
965  DCHECK((reg.Is(NoReg) || type >= kBranchTypeFirstUsingReg) &&
966  (bit == -1 || type >= kBranchTypeFirstUsingBit));
967  if (kBranchTypeFirstCondition <= type && type <= kBranchTypeLastCondition) {
968  B(static_cast<Condition>(type), label);
969  } else {
970  switch (type) {
971  case always: B(label); break;
972  case never: break;
973  case reg_zero: Cbz(reg, label); break;
974  case reg_not_zero: Cbnz(reg, label); break;
975  case reg_bit_clear: Tbz(reg, bit, label); break;
976  case reg_bit_set: Tbnz(reg, bit, label); break;
977  default:
978  UNREACHABLE();
979  }
980  }
981 }
982 
983 void TurboAssembler::B(Label* label, Condition cond) {
984  DCHECK(allow_macro_instructions());
985  DCHECK((cond != al) && (cond != nv));
986 
987  Label done;
988  bool need_extra_instructions =
989  NeedExtraInstructionsOrRegisterBranch(label, CondBranchType);
990 
991  if (need_extra_instructions) {
992  b(&done, NegateCondition(cond));
993  B(label);
994  } else {
995  b(label, cond);
996  }
997  bind(&done);
998 }
999 
1000 void TurboAssembler::Tbnz(const Register& rt, unsigned bit_pos, Label* label) {
1001  DCHECK(allow_macro_instructions());
1002 
1003  Label done;
1004  bool need_extra_instructions =
1005  NeedExtraInstructionsOrRegisterBranch(label, TestBranchType);
1006 
1007  if (need_extra_instructions) {
1008  tbz(rt, bit_pos, &done);
1009  B(label);
1010  } else {
1011  tbnz(rt, bit_pos, label);
1012  }
1013  bind(&done);
1014 }
1015 
1016 void TurboAssembler::Tbz(const Register& rt, unsigned bit_pos, Label* label) {
1017  DCHECK(allow_macro_instructions());
1018 
1019  Label done;
1020  bool need_extra_instructions =
1021  NeedExtraInstructionsOrRegisterBranch(label, TestBranchType);
1022 
1023  if (need_extra_instructions) {
1024  tbnz(rt, bit_pos, &done);
1025  B(label);
1026  } else {
1027  tbz(rt, bit_pos, label);
1028  }
1029  bind(&done);
1030 }
1031 
1032 void TurboAssembler::Cbnz(const Register& rt, Label* label) {
1033  DCHECK(allow_macro_instructions());
1034 
1035  Label done;
1036  bool need_extra_instructions =
1037  NeedExtraInstructionsOrRegisterBranch(label, CompareBranchType);
1038 
1039  if (need_extra_instructions) {
1040  cbz(rt, &done);
1041  B(label);
1042  } else {
1043  cbnz(rt, label);
1044  }
1045  bind(&done);
1046 }
1047 
1048 void TurboAssembler::Cbz(const Register& rt, Label* label) {
1049  DCHECK(allow_macro_instructions());
1050 
1051  Label done;
1052  bool need_extra_instructions =
1053  NeedExtraInstructionsOrRegisterBranch(label, CompareBranchType);
1054 
1055  if (need_extra_instructions) {
1056  cbnz(rt, &done);
1057  B(label);
1058  } else {
1059  cbz(rt, label);
1060  }
1061  bind(&done);
1062 }
1063 
1064 
1065 // Pseudo-instructions.
1066 
1067 void TurboAssembler::Abs(const Register& rd, const Register& rm,
1068  Label* is_not_representable, Label* is_representable) {
1069  DCHECK(allow_macro_instructions());
1070  DCHECK(AreSameSizeAndType(rd, rm));
1071 
1072  Cmp(rm, 1);
1073  Cneg(rd, rm, lt);
1074 
1075  // If the comparison sets the v flag, the input was the smallest value
1076  // representable by rm, and the mathematical result of abs(rm) is not
1077  // representable using two's complement.
1078  if ((is_not_representable != nullptr) && (is_representable != nullptr)) {
1079  B(is_not_representable, vs);
1080  B(is_representable);
1081  } else if (is_not_representable != nullptr) {
1082  B(is_not_representable, vs);
1083  } else if (is_representable != nullptr) {
1084  B(is_representable, vc);
1085  }
1086 }
1087 
1088 
1089 // Abstracted stack operations.
1090 
1091 void TurboAssembler::Push(const CPURegister& src0, const CPURegister& src1,
1092  const CPURegister& src2, const CPURegister& src3) {
1093  DCHECK(AreSameSizeAndType(src0, src1, src2, src3));
1094 
1095  int count = 1 + src1.IsValid() + src2.IsValid() + src3.IsValid();
1096  int size = src0.SizeInBytes();
1097  DCHECK_EQ(0, (size * count) % 16);
1098 
1099  PushHelper(count, size, src0, src1, src2, src3);
1100 }
1101 
1102 void TurboAssembler::Push(const CPURegister& src0, const CPURegister& src1,
1103  const CPURegister& src2, const CPURegister& src3,
1104  const CPURegister& src4, const CPURegister& src5,
1105  const CPURegister& src6, const CPURegister& src7) {
1106  DCHECK(AreSameSizeAndType(src0, src1, src2, src3, src4, src5, src6, src7));
1107 
1108  int count = 5 + src5.IsValid() + src6.IsValid() + src6.IsValid();
1109  int size = src0.SizeInBytes();
1110  DCHECK_EQ(0, (size * count) % 16);
1111 
1112  PushHelper(4, size, src0, src1, src2, src3);
1113  PushHelper(count - 4, size, src4, src5, src6, src7);
1114 }
1115 
1116 void TurboAssembler::Pop(const CPURegister& dst0, const CPURegister& dst1,
1117  const CPURegister& dst2, const CPURegister& dst3) {
1118  // It is not valid to pop into the same register more than once in one
1119  // instruction, not even into the zero register.
1120  DCHECK(!AreAliased(dst0, dst1, dst2, dst3));
1121  DCHECK(AreSameSizeAndType(dst0, dst1, dst2, dst3));
1122  DCHECK(dst0.IsValid());
1123 
1124  int count = 1 + dst1.IsValid() + dst2.IsValid() + dst3.IsValid();
1125  int size = dst0.SizeInBytes();
1126  DCHECK_EQ(0, (size * count) % 16);
1127 
1128  PopHelper(count, size, dst0, dst1, dst2, dst3);
1129 }
1130 
1131 void TurboAssembler::Pop(const CPURegister& dst0, const CPURegister& dst1,
1132  const CPURegister& dst2, const CPURegister& dst3,
1133  const CPURegister& dst4, const CPURegister& dst5,
1134  const CPURegister& dst6, const CPURegister& dst7) {
1135  // It is not valid to pop into the same register more than once in one
1136  // instruction, not even into the zero register.
1137  DCHECK(!AreAliased(dst0, dst1, dst2, dst3, dst4, dst5, dst6, dst7));
1138  DCHECK(AreSameSizeAndType(dst0, dst1, dst2, dst3, dst4, dst5, dst6, dst7));
1139  DCHECK(dst0.IsValid());
1140 
1141  int count = 5 + dst5.IsValid() + dst6.IsValid() + dst7.IsValid();
1142  int size = dst0.SizeInBytes();
1143  DCHECK_EQ(0, (size * count) % 16);
1144 
1145  PopHelper(4, size, dst0, dst1, dst2, dst3);
1146  PopHelper(count - 4, size, dst4, dst5, dst6, dst7);
1147 }
1148 
1149 void TurboAssembler::Push(const Register& src0, const VRegister& src1) {
1150  int size = src0.SizeInBytes() + src1.SizeInBytes();
1151  DCHECK_EQ(0, size % 16);
1152 
1153  // Reserve room for src0 and push src1.
1154  str(src1, MemOperand(sp, -size, PreIndex));
1155  // Fill the gap with src0.
1156  str(src0, MemOperand(sp, src1.SizeInBytes()));
1157 }
1158 
1159 void MacroAssembler::PushPopQueue::PushQueued() {
1160  DCHECK_EQ(0, size_ % 16);
1161  if (queued_.empty()) return;
1162 
1163  size_t count = queued_.size();
1164  size_t index = 0;
1165  while (index < count) {
1166  // PushHelper can only handle registers with the same size and type, and it
1167  // can handle only four at a time. Batch them up accordingly.
1168  CPURegister batch[4] = {NoReg, NoReg, NoReg, NoReg};
1169  int batch_index = 0;
1170  do {
1171  batch[batch_index++] = queued_[index++];
1172  } while ((batch_index < 4) && (index < count) &&
1173  batch[0].IsSameSizeAndType(queued_[index]));
1174 
1175  masm_->PushHelper(batch_index, batch[0].SizeInBytes(),
1176  batch[0], batch[1], batch[2], batch[3]);
1177  }
1178 
1179  queued_.clear();
1180 }
1181 
1182 
1183 void MacroAssembler::PushPopQueue::PopQueued() {
1184  DCHECK_EQ(0, size_ % 16);
1185  if (queued_.empty()) return;
1186 
1187  size_t count = queued_.size();
1188  size_t index = 0;
1189  while (index < count) {
1190  // PopHelper can only handle registers with the same size and type, and it
1191  // can handle only four at a time. Batch them up accordingly.
1192  CPURegister batch[4] = {NoReg, NoReg, NoReg, NoReg};
1193  int batch_index = 0;
1194  do {
1195  batch[batch_index++] = queued_[index++];
1196  } while ((batch_index < 4) && (index < count) &&
1197  batch[0].IsSameSizeAndType(queued_[index]));
1198 
1199  masm_->PopHelper(batch_index, batch[0].SizeInBytes(),
1200  batch[0], batch[1], batch[2], batch[3]);
1201  }
1202 
1203  queued_.clear();
1204 }
1205 
1206 void TurboAssembler::PushCPURegList(CPURegList registers) {
1207  int size = registers.RegisterSizeInBytes();
1208  DCHECK_EQ(0, (size * registers.Count()) % 16);
1209 
1210  // Push up to four registers at a time.
1211  while (!registers.IsEmpty()) {
1212  int count_before = registers.Count();
1213  const CPURegister& src0 = registers.PopHighestIndex();
1214  const CPURegister& src1 = registers.PopHighestIndex();
1215  const CPURegister& src2 = registers.PopHighestIndex();
1216  const CPURegister& src3 = registers.PopHighestIndex();
1217  int count = count_before - registers.Count();
1218  PushHelper(count, size, src0, src1, src2, src3);
1219  }
1220 }
1221 
1222 void TurboAssembler::PopCPURegList(CPURegList registers) {
1223  int size = registers.RegisterSizeInBytes();
1224  DCHECK_EQ(0, (size * registers.Count()) % 16);
1225 
1226  // Pop up to four registers at a time.
1227  while (!registers.IsEmpty()) {
1228  int count_before = registers.Count();
1229  const CPURegister& dst0 = registers.PopLowestIndex();
1230  const CPURegister& dst1 = registers.PopLowestIndex();
1231  const CPURegister& dst2 = registers.PopLowestIndex();
1232  const CPURegister& dst3 = registers.PopLowestIndex();
1233  int count = count_before - registers.Count();
1234  PopHelper(count, size, dst0, dst1, dst2, dst3);
1235  }
1236 }
1237 
1238 void MacroAssembler::PushMultipleTimes(CPURegister src, Register count) {
1239  UseScratchRegisterScope temps(this);
1240  Register temp = temps.AcquireSameSizeAs(count);
1241 
1242  if (FLAG_optimize_for_size) {
1243  Label loop, done;
1244 
1245  Subs(temp, count, 1);
1246  B(mi, &done);
1247 
1248  // Push all registers individually, to save code size.
1249  Bind(&loop);
1250  Subs(temp, temp, 1);
1251  PushHelper(1, src.SizeInBytes(), src, NoReg, NoReg, NoReg);
1252  B(pl, &loop);
1253 
1254  Bind(&done);
1255  } else {
1256  Label loop, leftover2, leftover1, done;
1257 
1258  Subs(temp, count, 4);
1259  B(mi, &leftover2);
1260 
1261  // Push groups of four first.
1262  Bind(&loop);
1263  Subs(temp, temp, 4);
1264  PushHelper(4, src.SizeInBytes(), src, src, src, src);
1265  B(pl, &loop);
1266 
1267  // Push groups of two.
1268  Bind(&leftover2);
1269  Tbz(count, 1, &leftover1);
1270  PushHelper(2, src.SizeInBytes(), src, src, NoReg, NoReg);
1271 
1272  // Push the last one (if required).
1273  Bind(&leftover1);
1274  Tbz(count, 0, &done);
1275  PushHelper(1, src.SizeInBytes(), src, NoReg, NoReg, NoReg);
1276 
1277  Bind(&done);
1278  }
1279 }
1280 
1281 void TurboAssembler::PushHelper(int count, int size, const CPURegister& src0,
1282  const CPURegister& src1,
1283  const CPURegister& src2,
1284  const CPURegister& src3) {
1285  // Ensure that we don't unintentially modify scratch or debug registers.
1286  InstructionAccurateScope scope(this);
1287 
1288  DCHECK(AreSameSizeAndType(src0, src1, src2, src3));
1289  DCHECK(size == src0.SizeInBytes());
1290 
1291  // When pushing multiple registers, the store order is chosen such that
1292  // Push(a, b) is equivalent to Push(a) followed by Push(b).
1293  switch (count) {
1294  case 1:
1295  DCHECK(src1.IsNone() && src2.IsNone() && src3.IsNone());
1296  str(src0, MemOperand(sp, -1 * size, PreIndex));
1297  break;
1298  case 2:
1299  DCHECK(src2.IsNone() && src3.IsNone());
1300  stp(src1, src0, MemOperand(sp, -2 * size, PreIndex));
1301  break;
1302  case 3:
1303  DCHECK(src3.IsNone());
1304  stp(src2, src1, MemOperand(sp, -3 * size, PreIndex));
1305  str(src0, MemOperand(sp, 2 * size));
1306  break;
1307  case 4:
1308  // Skip over 4 * size, then fill in the gap. This allows four W registers
1309  // to be pushed using sp, whilst maintaining 16-byte alignment for sp
1310  // at all times.
1311  stp(src3, src2, MemOperand(sp, -4 * size, PreIndex));
1312  stp(src1, src0, MemOperand(sp, 2 * size));
1313  break;
1314  default:
1315  UNREACHABLE();
1316  }
1317 }
1318 
1319 void TurboAssembler::PopHelper(int count, int size, const CPURegister& dst0,
1320  const CPURegister& dst1, const CPURegister& dst2,
1321  const CPURegister& dst3) {
1322  // Ensure that we don't unintentially modify scratch or debug registers.
1323  InstructionAccurateScope scope(this);
1324 
1325  DCHECK(AreSameSizeAndType(dst0, dst1, dst2, dst3));
1326  DCHECK(size == dst0.SizeInBytes());
1327 
1328  // When popping multiple registers, the load order is chosen such that
1329  // Pop(a, b) is equivalent to Pop(a) followed by Pop(b).
1330  switch (count) {
1331  case 1:
1332  DCHECK(dst1.IsNone() && dst2.IsNone() && dst3.IsNone());
1333  ldr(dst0, MemOperand(sp, 1 * size, PostIndex));
1334  break;
1335  case 2:
1336  DCHECK(dst2.IsNone() && dst3.IsNone());
1337  ldp(dst0, dst1, MemOperand(sp, 2 * size, PostIndex));
1338  break;
1339  case 3:
1340  DCHECK(dst3.IsNone());
1341  ldr(dst2, MemOperand(sp, 2 * size));
1342  ldp(dst0, dst1, MemOperand(sp, 3 * size, PostIndex));
1343  break;
1344  case 4:
1345  // Load the higher addresses first, then load the lower addresses and
1346  // skip the whole block in the second instruction. This allows four W
1347  // registers to be popped using sp, whilst maintaining 16-byte alignment
1348  // for sp at all times.
1349  ldp(dst2, dst3, MemOperand(sp, 2 * size));
1350  ldp(dst0, dst1, MemOperand(sp, 4 * size, PostIndex));
1351  break;
1352  default:
1353  UNREACHABLE();
1354  }
1355 }
1356 
1357 void TurboAssembler::Poke(const CPURegister& src, const Operand& offset) {
1358  if (offset.IsImmediate()) {
1359  DCHECK_GE(offset.ImmediateValue(), 0);
1360  } else if (emit_debug_code()) {
1361  Cmp(xzr, offset);
1362  Check(le, AbortReason::kStackAccessBelowStackPointer);
1363  }
1364 
1365  Str(src, MemOperand(sp, offset));
1366 }
1367 
1368 void TurboAssembler::Peek(const CPURegister& dst, const Operand& offset) {
1369  if (offset.IsImmediate()) {
1370  DCHECK_GE(offset.ImmediateValue(), 0);
1371  } else if (emit_debug_code()) {
1372  Cmp(xzr, offset);
1373  Check(le, AbortReason::kStackAccessBelowStackPointer);
1374  }
1375 
1376  Ldr(dst, MemOperand(sp, offset));
1377 }
1378 
1379 void TurboAssembler::PokePair(const CPURegister& src1, const CPURegister& src2,
1380  int offset) {
1381  DCHECK(AreSameSizeAndType(src1, src2));
1382  DCHECK((offset >= 0) && ((offset % src1.SizeInBytes()) == 0));
1383  Stp(src1, src2, MemOperand(sp, offset));
1384 }
1385 
1386 
1387 void MacroAssembler::PeekPair(const CPURegister& dst1,
1388  const CPURegister& dst2,
1389  int offset) {
1390  DCHECK(AreSameSizeAndType(dst1, dst2));
1391  DCHECK((offset >= 0) && ((offset % dst1.SizeInBytes()) == 0));
1392  Ldp(dst1, dst2, MemOperand(sp, offset));
1393 }
1394 
1395 
1396 void MacroAssembler::PushCalleeSavedRegisters() {
1397  // Ensure that the macro-assembler doesn't use any scratch registers.
1398  InstructionAccurateScope scope(this);
1399 
1400  MemOperand tos(sp, -2 * static_cast<int>(kXRegSize), PreIndex);
1401 
1402  stp(d14, d15, tos);
1403  stp(d12, d13, tos);
1404  stp(d10, d11, tos);
1405  stp(d8, d9, tos);
1406 
1407  stp(x29, x30, tos);
1408  stp(x27, x28, tos);
1409  stp(x25, x26, tos);
1410  stp(x23, x24, tos);
1411  stp(x21, x22, tos);
1412  stp(x19, x20, tos);
1413 }
1414 
1415 
1416 void MacroAssembler::PopCalleeSavedRegisters() {
1417  // Ensure that the macro-assembler doesn't use any scratch registers.
1418  InstructionAccurateScope scope(this);
1419 
1420  MemOperand tos(sp, 2 * kXRegSize, PostIndex);
1421 
1422  ldp(x19, x20, tos);
1423  ldp(x21, x22, tos);
1424  ldp(x23, x24, tos);
1425  ldp(x25, x26, tos);
1426  ldp(x27, x28, tos);
1427  ldp(x29, x30, tos);
1428 
1429  ldp(d8, d9, tos);
1430  ldp(d10, d11, tos);
1431  ldp(d12, d13, tos);
1432  ldp(d14, d15, tos);
1433 }
1434 
1435 void TurboAssembler::AssertSpAligned() {
1436  if (emit_debug_code()) {
1437  HardAbortScope hard_abort(this); // Avoid calls to Abort.
1438  // Arm64 requires the stack pointer to be 16-byte aligned prior to address
1439  // calculation.
1440  UseScratchRegisterScope scope(this);
1441  Register temp = scope.AcquireX();
1442  Mov(temp, sp);
1443  Tst(temp, 15);
1444  Check(eq, AbortReason::kUnexpectedStackPointer);
1445  }
1446 }
1447 
1448 void TurboAssembler::CopySlots(int dst, Register src, Register slot_count) {
1449  DCHECK(!src.IsZero());
1450  UseScratchRegisterScope scope(this);
1451  Register dst_reg = scope.AcquireX();
1452  SlotAddress(dst_reg, dst);
1453  SlotAddress(src, src);
1454  CopyDoubleWords(dst_reg, src, slot_count);
1455 }
1456 
1457 void TurboAssembler::CopySlots(Register dst, Register src,
1458  Register slot_count) {
1459  DCHECK(!dst.IsZero() && !src.IsZero());
1460  SlotAddress(dst, dst);
1461  SlotAddress(src, src);
1462  CopyDoubleWords(dst, src, slot_count);
1463 }
1464 
1465 void TurboAssembler::CopyDoubleWords(Register dst, Register src, Register count,
1466  CopyDoubleWordsMode mode) {
1467  DCHECK(!AreAliased(dst, src, count));
1468 
1469  if (emit_debug_code()) {
1470  Register pointer1 = dst;
1471  Register pointer2 = src;
1472  if (mode == kSrcLessThanDst) {
1473  pointer1 = src;
1474  pointer2 = dst;
1475  }
1476  // Copy requires pointer1 < pointer2 || (pointer1 - pointer2) >= count.
1477  Label pointer1_below_pointer2;
1478  Subs(pointer1, pointer1, pointer2);
1479  B(lt, &pointer1_below_pointer2);
1480  Cmp(pointer1, count);
1481  Check(ge, AbortReason::kOffsetOutOfRange);
1482  Bind(&pointer1_below_pointer2);
1483  Add(pointer1, pointer1, pointer2);
1484  }
1485  static_assert(kPointerSize == kDRegSize,
1486  "pointers must be the same size as doubles");
1487 
1488  int direction = (mode == kDstLessThanSrc) ? 1 : -1;
1489  UseScratchRegisterScope scope(this);
1490  VRegister temp0 = scope.AcquireD();
1491  VRegister temp1 = scope.AcquireD();
1492 
1493  Label pairs, loop, done;
1494 
1495  Tbz(count, 0, &pairs);
1496  Ldr(temp0, MemOperand(src, direction * kPointerSize, PostIndex));
1497  Sub(count, count, 1);
1498  Str(temp0, MemOperand(dst, direction * kPointerSize, PostIndex));
1499 
1500  Bind(&pairs);
1501  if (mode == kSrcLessThanDst) {
1502  // Adjust pointers for post-index ldp/stp with negative offset:
1503  Sub(dst, dst, kPointerSize);
1504  Sub(src, src, kPointerSize);
1505  }
1506  Bind(&loop);
1507  Cbz(count, &done);
1508  Ldp(temp0, temp1, MemOperand(src, 2 * direction * kPointerSize, PostIndex));
1509  Sub(count, count, 2);
1510  Stp(temp0, temp1, MemOperand(dst, 2 * direction * kPointerSize, PostIndex));
1511  B(&loop);
1512 
1513  // TODO(all): large copies may benefit from using temporary Q registers
1514  // to copy four double words per iteration.
1515 
1516  Bind(&done);
1517 }
1518 
1519 void TurboAssembler::SlotAddress(Register dst, int slot_offset) {
1520  Add(dst, sp, slot_offset << kPointerSizeLog2);
1521 }
1522 
1523 void TurboAssembler::SlotAddress(Register dst, Register slot_offset) {
1524  Add(dst, sp, Operand(slot_offset, LSL, kPointerSizeLog2));
1525 }
1526 
1527 void TurboAssembler::AssertFPCRState(Register fpcr) {
1528  if (emit_debug_code()) {
1529  Label unexpected_mode, done;
1530  UseScratchRegisterScope temps(this);
1531  if (fpcr.IsNone()) {
1532  fpcr = temps.AcquireX();
1533  Mrs(fpcr, FPCR);
1534  }
1535 
1536  // Settings left to their default values:
1537  // - Assert that flush-to-zero is not set.
1538  Tbnz(fpcr, FZ_offset, &unexpected_mode);
1539  // - Assert that the rounding mode is nearest-with-ties-to-even.
1540  STATIC_ASSERT(FPTieEven == 0);
1541  Tst(fpcr, RMode_mask);
1542  B(eq, &done);
1543 
1544  Bind(&unexpected_mode);
1545  Abort(AbortReason::kUnexpectedFPCRMode);
1546 
1547  Bind(&done);
1548  }
1549 }
1550 
1551 void TurboAssembler::CanonicalizeNaN(const VRegister& dst,
1552  const VRegister& src) {
1553  AssertFPCRState();
1554 
1555  // Subtracting 0.0 preserves all inputs except for signalling NaNs, which
1556  // become quiet NaNs. We use fsub rather than fadd because fsub preserves -0.0
1557  // inputs: -0.0 + 0.0 = 0.0, but -0.0 - 0.0 = -0.0.
1558  Fsub(dst, src, fp_zero);
1559 }
1560 
1561 void TurboAssembler::LoadRoot(Register destination, RootIndex index) {
1562  // TODO(jbramley): Most root values are constants, and can be synthesized
1563  // without a load. Refer to the ARM back end for details.
1564  Ldr(destination,
1565  MemOperand(kRootRegister, RootRegisterOffsetForRootIndex(index)));
1566 }
1567 
1568 
1569 void MacroAssembler::LoadObject(Register result, Handle<Object> object) {
1570  AllowDeferredHandleDereference heap_object_check;
1571  if (object->IsHeapObject()) {
1572  Mov(result, Handle<HeapObject>::cast(object));
1573  } else {
1574  Mov(result, Operand(Smi::cast(*object)));
1575  }
1576 }
1577 
1578 void TurboAssembler::Move(Register dst, Smi src) { Mov(dst, src); }
1579 
1580 void TurboAssembler::Swap(Register lhs, Register rhs) {
1581  DCHECK(lhs.IsSameSizeAndType(rhs));
1582  DCHECK(!lhs.Is(rhs));
1583  UseScratchRegisterScope temps(this);
1584  Register temp = temps.AcquireX();
1585  Mov(temp, rhs);
1586  Mov(rhs, lhs);
1587  Mov(lhs, temp);
1588 }
1589 
1590 void TurboAssembler::Swap(VRegister lhs, VRegister rhs) {
1591  DCHECK(lhs.IsSameSizeAndType(rhs));
1592  DCHECK(!lhs.Is(rhs));
1593  UseScratchRegisterScope temps(this);
1594  VRegister temp = VRegister::no_reg();
1595  if (lhs.IsS()) {
1596  temp = temps.AcquireS();
1597  } else if (lhs.IsD()) {
1598  temp = temps.AcquireD();
1599  } else {
1600  DCHECK(lhs.IsQ());
1601  temp = temps.AcquireQ();
1602  }
1603  Mov(temp, rhs);
1604  Mov(rhs, lhs);
1605  Mov(lhs, temp);
1606 }
1607 
1608 void TurboAssembler::AssertSmi(Register object, AbortReason reason) {
1609  if (emit_debug_code()) {
1610  STATIC_ASSERT(kSmiTag == 0);
1611  Tst(object, kSmiTagMask);
1612  Check(eq, reason);
1613  }
1614 }
1615 
1616 void MacroAssembler::AssertNotSmi(Register object, AbortReason reason) {
1617  if (emit_debug_code()) {
1618  STATIC_ASSERT(kSmiTag == 0);
1619  Tst(object, kSmiTagMask);
1620  Check(ne, reason);
1621  }
1622 }
1623 
1624 void MacroAssembler::AssertConstructor(Register object) {
1625  if (emit_debug_code()) {
1626  AssertNotSmi(object, AbortReason::kOperandIsASmiAndNotAConstructor);
1627 
1628  UseScratchRegisterScope temps(this);
1629  Register temp = temps.AcquireX();
1630 
1631  Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
1632  Ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
1633  Tst(temp, Operand(Map::IsConstructorBit::kMask));
1634 
1635  Check(ne, AbortReason::kOperandIsNotAConstructor);
1636  }
1637 }
1638 
1639 void MacroAssembler::AssertFunction(Register object) {
1640  if (emit_debug_code()) {
1641  AssertNotSmi(object, AbortReason::kOperandIsASmiAndNotAFunction);
1642 
1643  UseScratchRegisterScope temps(this);
1644  Register temp = temps.AcquireX();
1645 
1646  CompareObjectType(object, temp, temp, JS_FUNCTION_TYPE);
1647  Check(eq, AbortReason::kOperandIsNotAFunction);
1648  }
1649 }
1650 
1651 
1652 void MacroAssembler::AssertBoundFunction(Register object) {
1653  if (emit_debug_code()) {
1654  AssertNotSmi(object, AbortReason::kOperandIsASmiAndNotABoundFunction);
1655 
1656  UseScratchRegisterScope temps(this);
1657  Register temp = temps.AcquireX();
1658 
1659  CompareObjectType(object, temp, temp, JS_BOUND_FUNCTION_TYPE);
1660  Check(eq, AbortReason::kOperandIsNotABoundFunction);
1661  }
1662 }
1663 
1664 void MacroAssembler::AssertGeneratorObject(Register object) {
1665  if (!emit_debug_code()) return;
1666  AssertNotSmi(object, AbortReason::kOperandIsASmiAndNotAGeneratorObject);
1667 
1668  // Load map
1669  UseScratchRegisterScope temps(this);
1670  Register temp = temps.AcquireX();
1671  Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
1672 
1673  Label do_check;
1674  // Load instance type and check if JSGeneratorObject
1675  CompareInstanceType(temp, temp, JS_GENERATOR_OBJECT_TYPE);
1676  B(eq, &do_check);
1677 
1678  // Check if JSAsyncFunctionObject
1679  Cmp(temp, JS_ASYNC_FUNCTION_OBJECT_TYPE);
1680  B(eq, &do_check);
1681 
1682  // Check if JSAsyncGeneratorObject
1683  Cmp(temp, JS_ASYNC_GENERATOR_OBJECT_TYPE);
1684 
1685  bind(&do_check);
1686  // Restore generator object to register and perform assertion
1687  Check(eq, AbortReason::kOperandIsNotAGeneratorObject);
1688 }
1689 
1690 void MacroAssembler::AssertUndefinedOrAllocationSite(Register object) {
1691  if (emit_debug_code()) {
1692  UseScratchRegisterScope temps(this);
1693  Register scratch = temps.AcquireX();
1694  Label done_checking;
1695  AssertNotSmi(object);
1696  JumpIfRoot(object, RootIndex::kUndefinedValue, &done_checking);
1697  Ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
1698  CompareInstanceType(scratch, scratch, ALLOCATION_SITE_TYPE);
1699  Assert(eq, AbortReason::kExpectedUndefinedOrCell);
1700  Bind(&done_checking);
1701  }
1702 }
1703 
1704 void TurboAssembler::AssertPositiveOrZero(Register value) {
1705  if (emit_debug_code()) {
1706  Label done;
1707  int sign_bit = value.Is64Bits() ? kXSignBit : kWSignBit;
1708  Tbz(value, sign_bit, &done);
1709  Abort(AbortReason::kUnexpectedNegativeValue);
1710  Bind(&done);
1711  }
1712 }
1713 
1714 void MacroAssembler::CallStub(CodeStub* stub) {
1715  DCHECK(AllowThisStubCall(stub)); // Stub calls are not allowed in some stubs.
1716  Call(stub->GetCode(), RelocInfo::CODE_TARGET);
1717 }
1718 
1719 void MacroAssembler::TailCallStub(CodeStub* stub) {
1720  Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
1721 }
1722 
1723 void TurboAssembler::CallRuntimeWithCEntry(Runtime::FunctionId fid,
1724  Register centry) {
1725  const Runtime::Function* f = Runtime::FunctionForId(fid);
1726  // TODO(1236192): Most runtime routines don't need the number of
1727  // arguments passed in because it is constant. At some point we
1728  // should remove this need and make the runtime routine entry code
1729  // smarter.
1730  Mov(x0, f->nargs);
1731  Mov(x1, ExternalReference::Create(f));
1732  DCHECK(!AreAliased(centry, x0, x1));
1733  Add(centry, centry, Operand(Code::kHeaderSize - kHeapObjectTag));
1734  Call(centry);
1735 }
1736 
1737 void MacroAssembler::CallRuntime(const Runtime::Function* f,
1738  int num_arguments,
1739  SaveFPRegsMode save_doubles) {
1740  // All arguments must be on the stack before this function is called.
1741  // x0 holds the return value after the call.
1742 
1743  // Check that the number of arguments matches what the function expects.
1744  // If f->nargs is -1, the function can accept a variable number of arguments.
1745  CHECK(f->nargs < 0 || f->nargs == num_arguments);
1746 
1747  // Place the necessary arguments.
1748  Mov(x0, num_arguments);
1749  Mov(x1, ExternalReference::Create(f));
1750 
1751  Handle<Code> code =
1752  CodeFactory::CEntry(isolate(), f->result_size, save_doubles);
1753  Call(code, RelocInfo::CODE_TARGET);
1754 }
1755 
1756 void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin,
1757  bool builtin_exit_frame) {
1758  Mov(x1, builtin);
1759  Handle<Code> code = CodeFactory::CEntry(isolate(), 1, kDontSaveFPRegs,
1760  kArgvOnStack, builtin_exit_frame);
1761  Jump(code, RelocInfo::CODE_TARGET);
1762 }
1763 
1764 void MacroAssembler::JumpToInstructionStream(Address entry) {
1765  Ldr(kOffHeapTrampolineRegister, Operand(entry, RelocInfo::OFF_HEAP_TARGET));
1766  Br(kOffHeapTrampolineRegister);
1767 }
1768 
1769 void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid) {
1770  const Runtime::Function* function = Runtime::FunctionForId(fid);
1771  DCHECK_EQ(1, function->result_size);
1772  if (function->nargs >= 0) {
1773  // TODO(1236192): Most runtime routines don't need the number of
1774  // arguments passed in because it is constant. At some point we
1775  // should remove this need and make the runtime routine entry code
1776  // smarter.
1777  Mov(x0, function->nargs);
1778  }
1779  JumpToExternalReference(ExternalReference::Create(fid));
1780 }
1781 
1782 int TurboAssembler::ActivationFrameAlignment() {
1783 #if V8_HOST_ARCH_ARM64
1784  // Running on the real platform. Use the alignment as mandated by the local
1785  // environment.
1786  // Note: This will break if we ever start generating snapshots on one ARM
1787  // platform for another ARM platform with a different alignment.
1788  return base::OS::ActivationFrameAlignment();
1789 #else // V8_HOST_ARCH_ARM64
1790  // If we are using the simulator then we should always align to the expected
1791  // alignment. As the simulator is used to generate snapshots we do not know
1792  // if the target platform will need alignment, so this is controlled from a
1793  // flag.
1794  return FLAG_sim_stack_alignment;
1795 #endif // V8_HOST_ARCH_ARM64
1796 }
1797 
1798 void TurboAssembler::CallCFunction(ExternalReference function,
1799  int num_of_reg_args) {
1800  CallCFunction(function, num_of_reg_args, 0);
1801 }
1802 
1803 void TurboAssembler::CallCFunction(ExternalReference function,
1804  int num_of_reg_args,
1805  int num_of_double_args) {
1806  UseScratchRegisterScope temps(this);
1807  Register temp = temps.AcquireX();
1808  Mov(temp, function);
1809  CallCFunction(temp, num_of_reg_args, num_of_double_args);
1810 }
1811 
1812 static const int kRegisterPassedArguments = 8;
1813 
1814 void TurboAssembler::CallCFunction(Register function, int num_of_reg_args,
1815  int num_of_double_args) {
1816  DCHECK_LE(num_of_reg_args + num_of_double_args, kMaxCParameters);
1817  DCHECK(has_frame());
1818 
1819  // If we're passing doubles, we're limited to the following prototypes
1820  // (defined by ExternalReference::Type):
1821  // BUILTIN_COMPARE_CALL: int f(double, double)
1822  // BUILTIN_FP_FP_CALL: double f(double, double)
1823  // BUILTIN_FP_CALL: double f(double)
1824  // BUILTIN_FP_INT_CALL: double f(double, int)
1825  if (num_of_double_args > 0) {
1826  DCHECK_LE(num_of_reg_args, 1);
1827  DCHECK_LE(num_of_double_args + num_of_reg_args, 2);
1828  }
1829 
1830  // Call directly. The function called cannot cause a GC, or allow preemption,
1831  // so the return address in the link register stays correct.
1832  Call(function);
1833 
1834  if (num_of_reg_args > kRegisterPassedArguments) {
1835  // Drop the register passed arguments.
1836  int claim_slots = RoundUp(num_of_reg_args - kRegisterPassedArguments, 2);
1837  Drop(claim_slots);
1838  }
1839 }
1840 
1841 void TurboAssembler::LoadFromConstantsTable(Register destination,
1842  int constant_index) {
1843  DCHECK(RootsTable::IsImmortalImmovable(RootIndex::kBuiltinsConstantsTable));
1844  LoadRoot(destination, RootIndex::kBuiltinsConstantsTable);
1845  Ldr(destination,
1846  FieldMemOperand(destination,
1847  FixedArray::kHeaderSize + constant_index * kPointerSize));
1848 }
1849 
1850 void TurboAssembler::LoadRootRelative(Register destination, int32_t offset) {
1851  Ldr(destination, MemOperand(kRootRegister, offset));
1852 }
1853 
1854 void TurboAssembler::LoadRootRegisterOffset(Register destination,
1855  intptr_t offset) {
1856  if (offset == 0) {
1857  Mov(destination, kRootRegister);
1858  } else {
1859  Add(destination, kRootRegister, offset);
1860  }
1861 }
1862 
1863 void TurboAssembler::Jump(Register target, Condition cond) {
1864  if (cond == nv) return;
1865  Label done;
1866  if (cond != al) B(NegateCondition(cond), &done);
1867  Br(target);
1868  Bind(&done);
1869 }
1870 
1871 void TurboAssembler::JumpHelper(int64_t offset, RelocInfo::Mode rmode,
1872  Condition cond) {
1873  if (cond == nv) return;
1874  Label done;
1875  if (cond != al) B(NegateCondition(cond), &done);
1876  if (CanUseNearCallOrJump(rmode)) {
1877  DCHECK(IsNearCallOffset(offset));
1878  near_jump(static_cast<int>(offset), rmode);
1879  } else {
1880  UseScratchRegisterScope temps(this);
1881  Register temp = temps.AcquireX();
1882  uint64_t imm = reinterpret_cast<uint64_t>(pc_) + offset * kInstrSize;
1883  Mov(temp, Immediate(imm, rmode));
1884  Br(temp);
1885  }
1886  Bind(&done);
1887 }
1888 
1889 namespace {
1890 
1891 // The calculated offset is either:
1892 // * the 'target' input unmodified if this is a WASM call, or
1893 // * the offset of the target from the current PC, in instructions, for any
1894 // other type of call.
1895 static int64_t CalculateTargetOffset(Address target, RelocInfo::Mode rmode,
1896  byte* pc) {
1897  int64_t offset = static_cast<int64_t>(target);
1898  // The target of WebAssembly calls is still an index instead of an actual
1899  // address at this point, and needs to be encoded as-is.
1900  if (rmode != RelocInfo::WASM_CALL && rmode != RelocInfo::WASM_STUB_CALL) {
1901  offset -= reinterpret_cast<int64_t>(pc);
1902  DCHECK_EQ(offset % kInstrSize, 0);
1903  offset = offset / static_cast<int>(kInstrSize);
1904  }
1905  return offset;
1906 }
1907 } // namespace
1908 
1909 void TurboAssembler::Jump(Address target, RelocInfo::Mode rmode,
1910  Condition cond) {
1911  JumpHelper(CalculateTargetOffset(target, rmode, pc_), rmode, cond);
1912 }
1913 
1914 void TurboAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
1915  Condition cond) {
1916  DCHECK(RelocInfo::IsCodeTarget(rmode));
1917  if (FLAG_embedded_builtins) {
1918  if (root_array_available_ && options().isolate_independent_code &&
1919  !Builtins::IsIsolateIndependentBuiltin(*code)) {
1920  // Calls to embedded targets are initially generated as standard
1921  // pc-relative calls below. When creating the embedded blob, call offsets
1922  // are patched up to point directly to the off-heap instruction start.
1923  // Note: It is safe to dereference {code} above since code generation
1924  // for builtins and code stubs happens on the main thread.
1925  UseScratchRegisterScope temps(this);
1926  Register scratch = temps.AcquireX();
1927  IndirectLoadConstant(scratch, code);
1928  Add(scratch, scratch, Operand(Code::kHeaderSize - kHeapObjectTag));
1929  Jump(scratch, cond);
1930  return;
1931  } else if (options().inline_offheap_trampolines) {
1932  int builtin_index = Builtins::kNoBuiltinId;
1933  if (isolate()->builtins()->IsBuiltinHandle(code, &builtin_index) &&
1934  Builtins::IsIsolateIndependent(builtin_index)) {
1935  // Inline the trampoline.
1936  RecordCommentForOffHeapTrampoline(builtin_index);
1937  CHECK_NE(builtin_index, Builtins::kNoBuiltinId);
1938  UseScratchRegisterScope temps(this);
1939  Register scratch = temps.AcquireX();
1940  EmbeddedData d = EmbeddedData::FromBlob();
1941  Address entry = d.InstructionStartOfBuiltin(builtin_index);
1942  Ldr(scratch, Operand(entry, RelocInfo::OFF_HEAP_TARGET));
1943  Jump(scratch, cond);
1944  return;
1945  }
1946  }
1947  }
1948  if (CanUseNearCallOrJump(rmode)) {
1949  JumpHelper(static_cast<int64_t>(AddCodeTarget(code)), rmode, cond);
1950  } else {
1951  Jump(code.address(), rmode, cond);
1952  }
1953 }
1954 
1955 void TurboAssembler::Call(Register target) {
1956  BlockPoolsScope scope(this);
1957  Blr(target);
1958 }
1959 
1960 void TurboAssembler::Call(Address target, RelocInfo::Mode rmode) {
1961  BlockPoolsScope scope(this);
1962 
1963  if (CanUseNearCallOrJump(rmode)) {
1964  int64_t offset = CalculateTargetOffset(target, rmode, pc_);
1965  DCHECK(IsNearCallOffset(offset));
1966  near_call(static_cast<int>(offset), rmode);
1967  } else {
1968  IndirectCall(target, rmode);
1969  }
1970 }
1971 
1972 void TurboAssembler::Call(Handle<Code> code, RelocInfo::Mode rmode) {
1973  BlockPoolsScope scope(this);
1974 
1975  if (FLAG_embedded_builtins) {
1976  if (root_array_available_ && options().isolate_independent_code &&
1977  !Builtins::IsIsolateIndependentBuiltin(*code)) {
1978  // Calls to embedded targets are initially generated as standard
1979  // pc-relative calls below. When creating the embedded blob, call offsets
1980  // are patched up to point directly to the off-heap instruction start.
1981  // Note: It is safe to dereference {code} above since code generation
1982  // for builtins and code stubs happens on the main thread.
1983  UseScratchRegisterScope temps(this);
1984  Register scratch = temps.AcquireX();
1985  IndirectLoadConstant(scratch, code);
1986  Add(scratch, scratch, Operand(Code::kHeaderSize - kHeapObjectTag));
1987  Call(scratch);
1988  return;
1989  } else if (options().inline_offheap_trampolines) {
1990  int builtin_index = Builtins::kNoBuiltinId;
1991  if (isolate()->builtins()->IsBuiltinHandle(code, &builtin_index) &&
1992  Builtins::IsIsolateIndependent(builtin_index)) {
1993  // Inline the trampoline.
1994  RecordCommentForOffHeapTrampoline(builtin_index);
1995  CHECK_NE(builtin_index, Builtins::kNoBuiltinId);
1996  UseScratchRegisterScope temps(this);
1997  Register scratch = temps.AcquireX();
1998  EmbeddedData d = EmbeddedData::FromBlob();
1999  Address entry = d.InstructionStartOfBuiltin(builtin_index);
2000  Ldr(scratch, Operand(entry, RelocInfo::OFF_HEAP_TARGET));
2001  Call(scratch);
2002  return;
2003  }
2004  }
2005  }
2006  if (CanUseNearCallOrJump(rmode)) {
2007  near_call(AddCodeTarget(code), rmode);
2008  } else {
2009  IndirectCall(code.address(), rmode);
2010  }
2011 }
2012 
2013 void TurboAssembler::Call(ExternalReference target) {
2014  UseScratchRegisterScope temps(this);
2015  Register temp = temps.AcquireX();
2016  Mov(temp, target);
2017  Call(temp);
2018 }
2019 
2020 void TurboAssembler::IndirectCall(Address target, RelocInfo::Mode rmode) {
2021  UseScratchRegisterScope temps(this);
2022  Register temp = temps.AcquireX();
2023  Mov(temp, Immediate(target, rmode));
2024  Blr(temp);
2025 }
2026 
2027 bool TurboAssembler::IsNearCallOffset(int64_t offset) {
2028  return is_int26(offset);
2029 }
2030 
2031 void TurboAssembler::CallForDeoptimization(Address target, int deopt_id,
2032  RelocInfo::Mode rmode) {
2033  DCHECK_EQ(rmode, RelocInfo::RUNTIME_ENTRY);
2034 
2035  BlockPoolsScope scope(this);
2036 #ifdef DEBUG
2037  Label start;
2038  Bind(&start);
2039 #endif
2040  // The deoptimizer requires the deoptimization id to be in x16.
2041  UseScratchRegisterScope temps(this);
2042  Register temp = temps.AcquireX();
2043  DCHECK(temp.Is(x16));
2044  // Make sure that the deopt id can be encoded in 16 bits, so can be encoded
2045  // in a single movz instruction with a zero shift.
2046  DCHECK(is_uint16(deopt_id));
2047  movz(temp, deopt_id);
2048  int64_t offset = static_cast<int64_t>(target) -
2049  static_cast<int64_t>(options().code_range_start);
2050  DCHECK_EQ(offset % kInstrSize, 0);
2051  offset = offset / static_cast<int>(kInstrSize);
2052  DCHECK(IsNearCallOffset(offset));
2053  near_call(static_cast<int>(offset), RelocInfo::RUNTIME_ENTRY);
2054 }
2055 
2056 void MacroAssembler::TryRepresentDoubleAsInt(Register as_int, VRegister value,
2057  VRegister scratch_d,
2058  Label* on_successful_conversion,
2059  Label* on_failed_conversion) {
2060  // Convert to an int and back again, then compare with the original value.
2061  Fcvtzs(as_int, value);
2062  Scvtf(scratch_d, as_int);
2063  Fcmp(value, scratch_d);
2064 
2065  if (on_successful_conversion) {
2066  B(on_successful_conversion, eq);
2067  }
2068  if (on_failed_conversion) {
2069  B(on_failed_conversion, ne);
2070  }
2071 }
2072 
2073 void TurboAssembler::PrepareForTailCall(const ParameterCount& callee_args_count,
2074  Register caller_args_count_reg,
2075  Register scratch0, Register scratch1) {
2076 #if DEBUG
2077  if (callee_args_count.is_reg()) {
2078  DCHECK(!AreAliased(callee_args_count.reg(), caller_args_count_reg, scratch0,
2079  scratch1));
2080  } else {
2081  DCHECK(!AreAliased(caller_args_count_reg, scratch0, scratch1));
2082  }
2083 #endif
2084 
2085  // Calculate the end of destination area where we will put the arguments
2086  // after we drop current frame. We add kPointerSize to count the receiver
2087  // argument which is not included into formal parameters count.
2088  Register dst_reg = scratch0;
2089  Add(dst_reg, fp, Operand(caller_args_count_reg, LSL, kPointerSizeLog2));
2090  Add(dst_reg, dst_reg, StandardFrameConstants::kCallerSPOffset + kPointerSize);
2091  // Round dst_reg up to a multiple of 16 bytes, so that we overwrite any
2092  // potential padding.
2093  Add(dst_reg, dst_reg, 15);
2094  Bic(dst_reg, dst_reg, 15);
2095 
2096  Register src_reg = caller_args_count_reg;
2097  // Calculate the end of source area. +kPointerSize is for the receiver.
2098  if (callee_args_count.is_reg()) {
2099  Add(src_reg, sp, Operand(callee_args_count.reg(), LSL, kPointerSizeLog2));
2100  Add(src_reg, src_reg, kPointerSize);
2101  } else {
2102  Add(src_reg, sp, (callee_args_count.immediate() + 1) * kPointerSize);
2103  }
2104 
2105  // Round src_reg up to a multiple of 16 bytes, so we include any potential
2106  // padding in the copy.
2107  Add(src_reg, src_reg, 15);
2108  Bic(src_reg, src_reg, 15);
2109 
2110  if (FLAG_debug_code) {
2111  Cmp(src_reg, dst_reg);
2112  Check(lo, AbortReason::kStackAccessBelowStackPointer);
2113  }
2114 
2115  // Restore caller's frame pointer and return address now as they will be
2116  // overwritten by the copying loop.
2117  Ldr(lr, MemOperand(fp, StandardFrameConstants::kCallerPCOffset));
2118  Ldr(fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2119 
2120  // Now copy callee arguments to the caller frame going backwards to avoid
2121  // callee arguments corruption (source and destination areas could overlap).
2122 
2123  // Both src_reg and dst_reg are pointing to the word after the one to copy,
2124  // so they must be pre-decremented in the loop.
2125  Register tmp_reg = scratch1;
2126  Label loop, entry;
2127  B(&entry);
2128  bind(&loop);
2129  Ldr(tmp_reg, MemOperand(src_reg, -kPointerSize, PreIndex));
2130  Str(tmp_reg, MemOperand(dst_reg, -kPointerSize, PreIndex));
2131  bind(&entry);
2132  Cmp(sp, src_reg);
2133  B(ne, &loop);
2134 
2135  // Leave current frame.
2136  Mov(sp, dst_reg);
2137 }
2138 
2139 void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2140  const ParameterCount& actual, Label* done,
2141  InvokeFlag flag,
2142  bool* definitely_mismatches) {
2143  bool definitely_matches = false;
2144  *definitely_mismatches = false;
2145  Label regular_invoke;
2146 
2147  // Check whether the expected and actual arguments count match. If not,
2148  // setup registers according to contract with ArgumentsAdaptorTrampoline:
2149  // x0: actual arguments count.
2150  // x1: function (passed through to callee).
2151  // x2: expected arguments count.
2152 
2153  // The code below is made a lot easier because the calling code already sets
2154  // up actual and expected registers according to the contract if values are
2155  // passed in registers.
2156  DCHECK(actual.is_immediate() || actual.reg().is(x0));
2157  DCHECK(expected.is_immediate() || expected.reg().is(x2));
2158 
2159  if (expected.is_immediate()) {
2160  DCHECK(actual.is_immediate());
2161  Mov(x0, actual.immediate());
2162  if (expected.immediate() == actual.immediate()) {
2163  definitely_matches = true;
2164 
2165  } else {
2166  if (expected.immediate() ==
2167  SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
2168  // Don't worry about adapting arguments for builtins that
2169  // don't want that done. Skip adaption code by making it look
2170  // like we have a match between expected and actual number of
2171  // arguments.
2172  definitely_matches = true;
2173  } else {
2174  *definitely_mismatches = true;
2175  // Set up x2 for the argument adaptor.
2176  Mov(x2, expected.immediate());
2177  }
2178  }
2179 
2180  } else { // expected is a register.
2181  Operand actual_op = actual.is_immediate() ? Operand(actual.immediate())
2182  : Operand(actual.reg());
2183  Mov(x0, actual_op);
2184  // If actual == expected perform a regular invocation.
2185  Cmp(expected.reg(), actual_op);
2186  B(eq, &regular_invoke);
2187  }
2188 
2189  // If the argument counts may mismatch, generate a call to the argument
2190  // adaptor.
2191  if (!definitely_matches) {
2192  Handle<Code> adaptor = BUILTIN_CODE(isolate(), ArgumentsAdaptorTrampoline);
2193  if (flag == CALL_FUNCTION) {
2194  Call(adaptor);
2195  if (!*definitely_mismatches) {
2196  // If the arg counts don't match, no extra code is emitted by
2197  // MAsm::InvokeFunctionCode and we can just fall through.
2198  B(done);
2199  }
2200  } else {
2201  Jump(adaptor, RelocInfo::CODE_TARGET);
2202  }
2203  }
2204  Bind(&regular_invoke);
2205 }
2206 
2207 void MacroAssembler::CheckDebugHook(Register fun, Register new_target,
2208  const ParameterCount& expected,
2209  const ParameterCount& actual) {
2210  Label skip_hook;
2211 
2212  Mov(x4, ExternalReference::debug_hook_on_function_call_address(isolate()));
2213  Ldrsb(x4, MemOperand(x4));
2214  Cbz(x4, &skip_hook);
2215 
2216  {
2217  // Load receiver to pass it later to DebugOnFunctionCall hook.
2218  Operand actual_op = actual.is_immediate() ? Operand(actual.immediate())
2219  : Operand(actual.reg());
2220  Mov(x4, actual_op);
2221  Ldr(x4, MemOperand(sp, x4, LSL, kPointerSizeLog2));
2222  FrameScope frame(this,
2223  has_frame() ? StackFrame::NONE : StackFrame::INTERNAL);
2224 
2225  Register expected_reg = padreg;
2226  Register actual_reg = padreg;
2227  if (expected.is_reg()) expected_reg = expected.reg();
2228  if (actual.is_reg()) actual_reg = actual.reg();
2229  if (!new_target.is_valid()) new_target = padreg;
2230 
2231  // Save values on stack.
2232  SmiTag(expected_reg);
2233  SmiTag(actual_reg);
2234  Push(expected_reg, actual_reg, new_target, fun);
2235  Push(fun, x4);
2236  CallRuntime(Runtime::kDebugOnFunctionCall);
2237 
2238  // Restore values from stack.
2239  Pop(fun, new_target, actual_reg, expected_reg);
2240  SmiUntag(actual_reg);
2241  SmiUntag(expected_reg);
2242  }
2243  Bind(&skip_hook);
2244 }
2245 
2246 void MacroAssembler::InvokeFunctionCode(Register function, Register new_target,
2247  const ParameterCount& expected,
2248  const ParameterCount& actual,
2249  InvokeFlag flag) {
2250  // You can't call a function without a valid frame.
2251  DCHECK(flag == JUMP_FUNCTION || has_frame());
2252  DCHECK(function.is(x1));
2253  DCHECK_IMPLIES(new_target.is_valid(), new_target.is(x3));
2254 
2255  // On function call, call into the debugger if necessary.
2256  CheckDebugHook(function, new_target, expected, actual);
2257 
2258  // Clear the new.target register if not given.
2259  if (!new_target.is_valid()) {
2260  LoadRoot(x3, RootIndex::kUndefinedValue);
2261  }
2262 
2263  Label done;
2264  bool definitely_mismatches = false;
2265  InvokePrologue(expected, actual, &done, flag, &definitely_mismatches);
2266 
2267  // If we are certain that actual != expected, then we know InvokePrologue will
2268  // have handled the call through the argument adaptor mechanism.
2269  // The called function expects the call kind in x5.
2270  if (!definitely_mismatches) {
2271  // We call indirectly through the code field in the function to
2272  // allow recompilation to take effect without changing any of the
2273  // call sites.
2274  Register code = kJavaScriptCallCodeStartRegister;
2275  Ldr(code, FieldMemOperand(function, JSFunction::kCodeOffset));
2276  Add(code, code, Operand(Code::kHeaderSize - kHeapObjectTag));
2277  if (flag == CALL_FUNCTION) {
2278  Call(code);
2279  } else {
2280  DCHECK(flag == JUMP_FUNCTION);
2281  Jump(code);
2282  }
2283  }
2284 
2285  // Continue here if InvokePrologue does handle the invocation due to
2286  // mismatched parameter counts.
2287  Bind(&done);
2288 }
2289 
2290 void MacroAssembler::InvokeFunction(Register function, Register new_target,
2291  const ParameterCount& actual,
2292  InvokeFlag flag) {
2293  // You can't call a function without a valid frame.
2294  DCHECK(flag == JUMP_FUNCTION || has_frame());
2295 
2296  // Contract with called JS functions requires that function is passed in x1.
2297  // (See FullCodeGenerator::Generate().)
2298  DCHECK(function.is(x1));
2299 
2300  Register expected_reg = x2;
2301 
2302  Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
2303  // The number of arguments is stored as an int32_t, and -1 is a marker
2304  // (SharedFunctionInfo::kDontAdaptArgumentsSentinel), so we need sign
2305  // extension to correctly handle it.
2306  Ldr(expected_reg, FieldMemOperand(function,
2307  JSFunction::kSharedFunctionInfoOffset));
2308  Ldrh(expected_reg,
2309  FieldMemOperand(expected_reg,
2310  SharedFunctionInfo::kFormalParameterCountOffset));
2311 
2312  ParameterCount expected(expected_reg);
2313  InvokeFunctionCode(function, new_target, expected, actual, flag);
2314 }
2315 
2316 void MacroAssembler::InvokeFunction(Register function,
2317  const ParameterCount& expected,
2318  const ParameterCount& actual,
2319  InvokeFlag flag) {
2320  // You can't call a function without a valid frame.
2321  DCHECK(flag == JUMP_FUNCTION || has_frame());
2322 
2323  // Contract with called JS functions requires that function is passed in x1.
2324  // (See FullCodeGenerator::Generate().)
2325  DCHECK(function.Is(x1));
2326 
2327  // Set up the context.
2328  Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
2329 
2330  InvokeFunctionCode(function, no_reg, expected, actual, flag);
2331 }
2332 
2333 void TurboAssembler::TryConvertDoubleToInt64(Register result,
2334  DoubleRegister double_input,
2335  Label* done) {
2336  // Try to convert with an FPU convert instruction. It's trivial to compute
2337  // the modulo operation on an integer register so we convert to a 64-bit
2338  // integer.
2339  //
2340  // Fcvtzs will saturate to INT64_MIN (0x800...00) or INT64_MAX (0x7FF...FF)
2341  // when the double is out of range. NaNs and infinities will be converted to 0
2342  // (as ECMA-262 requires).
2343  Fcvtzs(result.X(), double_input);
2344 
2345  // The values INT64_MIN (0x800...00) or INT64_MAX (0x7FF...FF) are not
2346  // representable using a double, so if the result is one of those then we know
2347  // that saturation occurred, and we need to manually handle the conversion.
2348  //
2349  // It is easy to detect INT64_MIN and INT64_MAX because adding or subtracting
2350  // 1 will cause signed overflow.
2351  Cmp(result.X(), 1);
2352  Ccmp(result.X(), -1, VFlag, vc);
2353 
2354  B(vc, done);
2355 }
2356 
2357 void TurboAssembler::TruncateDoubleToI(Isolate* isolate, Zone* zone,
2358  Register result,
2359  DoubleRegister double_input,
2360  StubCallMode stub_mode) {
2361  Label done;
2362 
2363  // Try to convert the double to an int64. If successful, the bottom 32 bits
2364  // contain our truncated int32 result.
2365  TryConvertDoubleToInt64(result, double_input, &done);
2366 
2367  // If we fell through then inline version didn't succeed - call stub instead.
2368  Push(lr, double_input);
2369 
2370  // DoubleToI preserves any registers it needs to clobber.
2371  if (stub_mode == StubCallMode::kCallWasmRuntimeStub) {
2372  Call(wasm::WasmCode::kDoubleToI, RelocInfo::WASM_STUB_CALL);
2373  } else {
2374  Call(BUILTIN_CODE(isolate, DoubleToI), RelocInfo::CODE_TARGET);
2375  }
2376  Ldr(result, MemOperand(sp, 0));
2377 
2378  DCHECK_EQ(xzr.SizeInBytes(), double_input.SizeInBytes());
2379  Pop(xzr, lr); // xzr to drop the double input on the stack.
2380 
2381  Bind(&done);
2382  // Keep our invariant that the upper 32 bits are zero.
2383  Uxtw(result.W(), result.W());
2384 }
2385 
2386 void TurboAssembler::Prologue() {
2387  Push(lr, fp, cp, x1);
2388  Add(fp, sp, StandardFrameConstants::kFixedFrameSizeFromFp);
2389 }
2390 
2391 void TurboAssembler::EnterFrame(StackFrame::Type type) {
2392  UseScratchRegisterScope temps(this);
2393 
2394  if (type == StackFrame::INTERNAL) {
2395  Register type_reg = temps.AcquireX();
2396  Mov(type_reg, StackFrame::TypeToMarker(type));
2397  // type_reg pushed twice for alignment.
2398  Push(lr, fp, type_reg, type_reg);
2399  const int kFrameSize =
2400  TypedFrameConstants::kFixedFrameSizeFromFp + kPointerSize;
2401  Add(fp, sp, kFrameSize);
2402  // sp[3] : lr
2403  // sp[2] : fp
2404  // sp[1] : type
2405  // sp[0] : for alignment
2406  } else if (type == StackFrame::WASM_COMPILED ||
2407  type == StackFrame::WASM_COMPILE_LAZY) {
2408  Register type_reg = temps.AcquireX();
2409  Mov(type_reg, StackFrame::TypeToMarker(type));
2410  Push(lr, fp);
2411  Mov(fp, sp);
2412  Push(type_reg, padreg);
2413  // sp[3] : lr
2414  // sp[2] : fp
2415  // sp[1] : type
2416  // sp[0] : for alignment
2417  } else {
2418  DCHECK_EQ(type, StackFrame::CONSTRUCT);
2419  Register type_reg = temps.AcquireX();
2420  Mov(type_reg, StackFrame::TypeToMarker(type));
2421 
2422  // Users of this frame type push a context pointer after the type field,
2423  // so do it here to keep the stack pointer aligned.
2424  Push(lr, fp, type_reg, cp);
2425 
2426  // The context pointer isn't part of the fixed frame, so add an extra slot
2427  // to account for it.
2428  Add(fp, sp, TypedFrameConstants::kFixedFrameSizeFromFp + kPointerSize);
2429  // sp[3] : lr
2430  // sp[2] : fp
2431  // sp[1] : type
2432  // sp[0] : cp
2433  }
2434 }
2435 
2436 void TurboAssembler::LeaveFrame(StackFrame::Type type) {
2437  // Drop the execution stack down to the frame pointer and restore
2438  // the caller frame pointer and return address.
2439  Mov(sp, fp);
2440  Pop(fp, lr);
2441 }
2442 
2443 
2444 void MacroAssembler::ExitFramePreserveFPRegs() {
2445  DCHECK_EQ(kCallerSavedV.Count() % 2, 0);
2446  PushCPURegList(kCallerSavedV);
2447 }
2448 
2449 
2450 void MacroAssembler::ExitFrameRestoreFPRegs() {
2451  // Read the registers from the stack without popping them. The stack pointer
2452  // will be reset as part of the unwinding process.
2453  CPURegList saved_fp_regs = kCallerSavedV;
2454  DCHECK_EQ(saved_fp_regs.Count() % 2, 0);
2455 
2456  int offset = ExitFrameConstants::kLastExitFrameField;
2457  while (!saved_fp_regs.IsEmpty()) {
2458  const CPURegister& dst0 = saved_fp_regs.PopHighestIndex();
2459  const CPURegister& dst1 = saved_fp_regs.PopHighestIndex();
2460  offset -= 2 * kDRegSize;
2461  Ldp(dst1, dst0, MemOperand(fp, offset));
2462  }
2463 }
2464 
2465 void MacroAssembler::EnterExitFrame(bool save_doubles, const Register& scratch,
2466  int extra_space,
2467  StackFrame::Type frame_type) {
2468  DCHECK(frame_type == StackFrame::EXIT ||
2469  frame_type == StackFrame::BUILTIN_EXIT);
2470 
2471  // Set up the new stack frame.
2472  Push(lr, fp);
2473  Mov(fp, sp);
2474  Mov(scratch, StackFrame::TypeToMarker(frame_type));
2475  Push(scratch, xzr);
2476  Mov(scratch, CodeObject());
2477  Push(scratch, padreg);
2478  // fp[8]: CallerPC (lr)
2479  // fp -> fp[0]: CallerFP (old fp)
2480  // fp[-8]: STUB marker
2481  // fp[-16]: Space reserved for SPOffset.
2482  // fp[-24]: CodeObject()
2483  // sp -> fp[-32]: padding
2484  STATIC_ASSERT((2 * kPointerSize) == ExitFrameConstants::kCallerSPOffset);
2485  STATIC_ASSERT((1 * kPointerSize) == ExitFrameConstants::kCallerPCOffset);
2486  STATIC_ASSERT((0 * kPointerSize) == ExitFrameConstants::kCallerFPOffset);
2487  STATIC_ASSERT((-2 * kPointerSize) == ExitFrameConstants::kSPOffset);
2488  STATIC_ASSERT((-3 * kPointerSize) == ExitFrameConstants::kCodeOffset);
2489  STATIC_ASSERT((-4 * kPointerSize) == ExitFrameConstants::kPaddingOffset);
2490 
2491  // Save the frame pointer and context pointer in the top frame.
2492  Mov(scratch,
2493  ExternalReference::Create(IsolateAddressId::kCEntryFPAddress, isolate()));
2494  Str(fp, MemOperand(scratch));
2495  Mov(scratch,
2496  ExternalReference::Create(IsolateAddressId::kContextAddress, isolate()));
2497  Str(cp, MemOperand(scratch));
2498 
2499  STATIC_ASSERT((-4 * kPointerSize) == ExitFrameConstants::kLastExitFrameField);
2500  if (save_doubles) {
2501  ExitFramePreserveFPRegs();
2502  }
2503 
2504  // Round the number of space we need to claim to a multiple of two.
2505  int slots_to_claim = RoundUp(extra_space + 1, 2);
2506 
2507  // Reserve space for the return address and for user requested memory.
2508  // We do this before aligning to make sure that we end up correctly
2509  // aligned with the minimum of wasted space.
2510  Claim(slots_to_claim, kXRegSize);
2511  // fp[8]: CallerPC (lr)
2512  // fp -> fp[0]: CallerFP (old fp)
2513  // fp[-8]: STUB marker
2514  // fp[-16]: Space reserved for SPOffset.
2515  // fp[-24]: CodeObject()
2516  // fp[-24 - fp_size]: Saved doubles (if save_doubles is true).
2517  // sp[8]: Extra space reserved for caller (if extra_space != 0).
2518  // sp -> sp[0]: Space reserved for the return address.
2519 
2520  // ExitFrame::GetStateForFramePointer expects to find the return address at
2521  // the memory address immediately below the pointer stored in SPOffset.
2522  // It is not safe to derive much else from SPOffset, because the size of the
2523  // padding can vary.
2524  Add(scratch, sp, kXRegSize);
2525  Str(scratch, MemOperand(fp, ExitFrameConstants::kSPOffset));
2526 }
2527 
2528 
2529 // Leave the current exit frame.
2530 void MacroAssembler::LeaveExitFrame(bool restore_doubles,
2531  const Register& scratch,
2532  const Register& scratch2) {
2533  if (restore_doubles) {
2534  ExitFrameRestoreFPRegs();
2535  }
2536 
2537  // Restore the context pointer from the top frame.
2538  Mov(scratch,
2539  ExternalReference::Create(IsolateAddressId::kContextAddress, isolate()));
2540  Ldr(cp, MemOperand(scratch));
2541 
2542  if (emit_debug_code()) {
2543  // Also emit debug code to clear the cp in the top frame.
2544  Mov(scratch2, Operand(Context::kInvalidContext));
2545  Mov(scratch, ExternalReference::Create(IsolateAddressId::kContextAddress,
2546  isolate()));
2547  Str(scratch2, MemOperand(scratch));
2548  }
2549  // Clear the frame pointer from the top frame.
2550  Mov(scratch,
2551  ExternalReference::Create(IsolateAddressId::kCEntryFPAddress, isolate()));
2552  Str(xzr, MemOperand(scratch));
2553 
2554  // Pop the exit frame.
2555  // fp[8]: CallerPC (lr)
2556  // fp -> fp[0]: CallerFP (old fp)
2557  // fp[...]: The rest of the frame.
2558  Mov(sp, fp);
2559  Pop(fp, lr);
2560 }
2561 
2562 void MacroAssembler::LoadGlobalProxy(Register dst) {
2563  LoadNativeContextSlot(Context::GLOBAL_PROXY_INDEX, dst);
2564 }
2565 
2566 void MacroAssembler::LoadWeakValue(Register out, Register in,
2567  Label* target_if_cleared) {
2568  CompareAndBranch(in.W(), Operand(kClearedWeakHeapObjectLower32), eq,
2569  target_if_cleared);
2570 
2571  and_(out, in, Operand(~kWeakHeapObjectMask));
2572 }
2573 
2574 void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2575  Register scratch1, Register scratch2) {
2576  DCHECK_NE(value, 0);
2577  if (FLAG_native_code_counters && counter->Enabled()) {
2578  Mov(scratch2, ExternalReference::Create(counter));
2579  Ldr(scratch1.W(), MemOperand(scratch2));
2580  Add(scratch1.W(), scratch1.W(), value);
2581  Str(scratch1.W(), MemOperand(scratch2));
2582  }
2583 }
2584 
2585 
2586 void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2587  Register scratch1, Register scratch2) {
2588  IncrementCounter(counter, -value, scratch1, scratch2);
2589 }
2590 
2591 void MacroAssembler::MaybeDropFrames() {
2592  // Check whether we need to drop frames to restart a function on the stack.
2593  Mov(x1, ExternalReference::debug_restart_fp_address(isolate()));
2594  Ldr(x1, MemOperand(x1));
2595  Tst(x1, x1);
2596  Jump(BUILTIN_CODE(isolate(), FrameDropperTrampoline), RelocInfo::CODE_TARGET,
2597  ne);
2598 }
2599 
2600 void MacroAssembler::JumpIfObjectType(Register object,
2601  Register map,
2602  Register type_reg,
2603  InstanceType type,
2604  Label* if_cond_pass,
2605  Condition cond) {
2606  CompareObjectType(object, map, type_reg, type);
2607  B(cond, if_cond_pass);
2608 }
2609 
2610 
2611 // Sets condition flags based on comparison, and returns type in type_reg.
2612 void MacroAssembler::CompareObjectType(Register object,
2613  Register map,
2614  Register type_reg,
2615  InstanceType type) {
2616  Ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
2617  CompareInstanceType(map, type_reg, type);
2618 }
2619 
2620 
2621 // Sets condition flags based on comparison, and returns type in type_reg.
2622 void MacroAssembler::CompareInstanceType(Register map,
2623  Register type_reg,
2624  InstanceType type) {
2625  Ldrh(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
2626  Cmp(type_reg, type);
2627 }
2628 
2629 
2630 void MacroAssembler::LoadElementsKindFromMap(Register result, Register map) {
2631  // Load the map's "bit field 2".
2632  Ldrb(result, FieldMemOperand(map, Map::kBitField2Offset));
2633  // Retrieve elements_kind from bit field 2.
2634  DecodeField<Map::ElementsKindBits>(result);
2635 }
2636 
2637 void MacroAssembler::CompareRoot(const Register& obj, RootIndex index) {
2638  UseScratchRegisterScope temps(this);
2639  Register temp = temps.AcquireX();
2640  DCHECK(!AreAliased(obj, temp));
2641  LoadRoot(temp, index);
2642  Cmp(obj, temp);
2643 }
2644 
2645 void MacroAssembler::JumpIfRoot(const Register& obj, RootIndex index,
2646  Label* if_equal) {
2647  CompareRoot(obj, index);
2648  B(eq, if_equal);
2649 }
2650 
2651 void MacroAssembler::JumpIfNotRoot(const Register& obj, RootIndex index,
2652  Label* if_not_equal) {
2653  CompareRoot(obj, index);
2654  B(ne, if_not_equal);
2655 }
2656 
2657 
2658 void MacroAssembler::CompareAndSplit(const Register& lhs,
2659  const Operand& rhs,
2660  Condition cond,
2661  Label* if_true,
2662  Label* if_false,
2663  Label* fall_through) {
2664  if ((if_true == if_false) && (if_false == fall_through)) {
2665  // Fall through.
2666  } else if (if_true == if_false) {
2667  B(if_true);
2668  } else if (if_false == fall_through) {
2669  CompareAndBranch(lhs, rhs, cond, if_true);
2670  } else if (if_true == fall_through) {
2671  CompareAndBranch(lhs, rhs, NegateCondition(cond), if_false);
2672  } else {
2673  CompareAndBranch(lhs, rhs, cond, if_true);
2674  B(if_false);
2675  }
2676 }
2677 
2678 
2679 void MacroAssembler::TestAndSplit(const Register& reg,
2680  uint64_t bit_pattern,
2681  Label* if_all_clear,
2682  Label* if_any_set,
2683  Label* fall_through) {
2684  if ((if_all_clear == if_any_set) && (if_any_set == fall_through)) {
2685  // Fall through.
2686  } else if (if_all_clear == if_any_set) {
2687  B(if_all_clear);
2688  } else if (if_all_clear == fall_through) {
2689  TestAndBranchIfAnySet(reg, bit_pattern, if_any_set);
2690  } else if (if_any_set == fall_through) {
2691  TestAndBranchIfAllClear(reg, bit_pattern, if_all_clear);
2692  } else {
2693  TestAndBranchIfAnySet(reg, bit_pattern, if_any_set);
2694  B(if_all_clear);
2695  }
2696 }
2697 
2698 bool TurboAssembler::AllowThisStubCall(CodeStub* stub) {
2699  return has_frame() || !stub->SometimesSetsUpAFrame();
2700 }
2701 
2702 void MacroAssembler::PopSafepointRegisters() {
2703  const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
2704  DCHECK_GE(num_unsaved, 0);
2705  DCHECK_EQ(num_unsaved % 2, 0);
2706  DCHECK_EQ(kSafepointSavedRegisters % 2, 0);
2707  PopXRegList(kSafepointSavedRegisters);
2708  Drop(num_unsaved);
2709 }
2710 
2711 
2712 void MacroAssembler::PushSafepointRegisters() {
2713  // Safepoints expect a block of kNumSafepointRegisters values on the stack, so
2714  // adjust the stack for unsaved registers.
2715  const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
2716  DCHECK_GE(num_unsaved, 0);
2717  DCHECK_EQ(num_unsaved % 2, 0);
2718  DCHECK_EQ(kSafepointSavedRegisters % 2, 0);
2719  Claim(num_unsaved);
2720  PushXRegList(kSafepointSavedRegisters);
2721 }
2722 
2723 int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2724  // Make sure the safepoint registers list is what we expect.
2725  DCHECK_EQ(CPURegList::GetSafepointSavedRegisters().list(), 0x6FFCFFFF);
2726 
2727  // Safepoint registers are stored contiguously on the stack, but not all the
2728  // registers are saved. The following registers are excluded:
2729  // - x16 and x17 (ip0 and ip1) because they shouldn't be preserved outside of
2730  // the macro assembler.
2731  // - x31 (sp) because the system stack pointer doesn't need to be included
2732  // in safepoint registers.
2733  //
2734  // This function implements the mapping of register code to index into the
2735  // safepoint register slots.
2736  if ((reg_code >= 0) && (reg_code <= 15)) {
2737  return reg_code;
2738  } else if ((reg_code >= 18) && (reg_code <= 30)) {
2739  // Skip ip0 and ip1.
2740  return reg_code - 2;
2741  } else {
2742  // This register has no safepoint register slot.
2743  UNREACHABLE();
2744  }
2745 }
2746 
2747 void MacroAssembler::CheckPageFlag(const Register& object,
2748  const Register& scratch, int mask,
2749  Condition cc, Label* condition_met) {
2750  And(scratch, object, ~kPageAlignmentMask);
2751  Ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
2752  if (cc == eq) {
2753  TestAndBranchIfAnySet(scratch, mask, condition_met);
2754  } else {
2755  TestAndBranchIfAllClear(scratch, mask, condition_met);
2756  }
2757 }
2758 
2759 void TurboAssembler::CheckPageFlagSet(const Register& object,
2760  const Register& scratch, int mask,
2761  Label* if_any_set) {
2762  And(scratch, object, ~kPageAlignmentMask);
2763  Ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
2764  TestAndBranchIfAnySet(scratch, mask, if_any_set);
2765 }
2766 
2767 void TurboAssembler::CheckPageFlagClear(const Register& object,
2768  const Register& scratch, int mask,
2769  Label* if_all_clear) {
2770  And(scratch, object, ~kPageAlignmentMask);
2771  Ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
2772  TestAndBranchIfAllClear(scratch, mask, if_all_clear);
2773 }
2774 
2775 void MacroAssembler::RecordWriteField(Register object, int offset,
2776  Register value, Register scratch,
2777  LinkRegisterStatus lr_status,
2778  SaveFPRegsMode save_fp,
2779  RememberedSetAction remembered_set_action,
2780  SmiCheck smi_check) {
2781  // First, check if a write barrier is even needed. The tests below
2782  // catch stores of Smis.
2783  Label done;
2784 
2785  // Skip the barrier if writing a smi.
2786  if (smi_check == INLINE_SMI_CHECK) {
2787  JumpIfSmi(value, &done);
2788  }
2789 
2790  // Although the object register is tagged, the offset is relative to the start
2791  // of the object, so offset must be a multiple of kPointerSize.
2792  DCHECK(IsAligned(offset, kPointerSize));
2793 
2794  Add(scratch, object, offset - kHeapObjectTag);
2795  if (emit_debug_code()) {
2796  Label ok;
2797  Tst(scratch, kPointerSize - 1);
2798  B(eq, &ok);
2799  Abort(AbortReason::kUnalignedCellInWriteBarrier);
2800  Bind(&ok);
2801  }
2802 
2803  RecordWrite(object, scratch, value, lr_status, save_fp, remembered_set_action,
2804  OMIT_SMI_CHECK);
2805 
2806  Bind(&done);
2807 
2808  // Clobber clobbered input registers when running with the debug-code flag
2809  // turned on to provoke errors.
2810  if (emit_debug_code()) {
2811  Mov(value, Operand(bit_cast<int64_t>(kZapValue + 4)));
2812  Mov(scratch, Operand(bit_cast<int64_t>(kZapValue + 8)));
2813  }
2814 }
2815 
2816 void TurboAssembler::SaveRegisters(RegList registers) {
2817  DCHECK_GT(NumRegs(registers), 0);
2818  CPURegList regs(lr);
2819  for (int i = 0; i < Register::kNumRegisters; ++i) {
2820  if ((registers >> i) & 1u) {
2821  regs.Combine(Register::XRegFromCode(i));
2822  }
2823  }
2824 
2825  PushCPURegList(regs);
2826 }
2827 
2828 void TurboAssembler::RestoreRegisters(RegList registers) {
2829  DCHECK_GT(NumRegs(registers), 0);
2830  CPURegList regs(lr);
2831  for (int i = 0; i < Register::kNumRegisters; ++i) {
2832  if ((registers >> i) & 1u) {
2833  regs.Combine(Register::XRegFromCode(i));
2834  }
2835  }
2836 
2837  PopCPURegList(regs);
2838 }
2839 
2840 void TurboAssembler::CallRecordWriteStub(
2841  Register object, Register address,
2842  RememberedSetAction remembered_set_action, SaveFPRegsMode fp_mode) {
2843  CallRecordWriteStub(
2844  object, address, remembered_set_action, fp_mode,
2845  isolate()->builtins()->builtin_handle(Builtins::kRecordWrite),
2846  kNullAddress);
2847 }
2848 
2849 void TurboAssembler::CallRecordWriteStub(
2850  Register object, Register address,
2851  RememberedSetAction remembered_set_action, SaveFPRegsMode fp_mode,
2852  Address wasm_target) {
2853  CallRecordWriteStub(object, address, remembered_set_action, fp_mode,
2854  Handle<Code>::null(), wasm_target);
2855 }
2856 
2857 void TurboAssembler::CallRecordWriteStub(
2858  Register object, Register address,
2859  RememberedSetAction remembered_set_action, SaveFPRegsMode fp_mode,
2860  Handle<Code> code_target, Address wasm_target) {
2861  DCHECK_NE(code_target.is_null(), wasm_target == kNullAddress);
2862  // TODO(albertnetymk): For now we ignore remembered_set_action and fp_mode,
2863  // i.e. always emit remember set and save FP registers in RecordWriteStub. If
2864  // large performance regression is observed, we should use these values to
2865  // avoid unnecessary work.
2866 
2867  RecordWriteDescriptor descriptor;
2868  RegList registers = descriptor.allocatable_registers();
2869 
2870  SaveRegisters(registers);
2871 
2872  Register object_parameter(
2873  descriptor.GetRegisterParameter(RecordWriteDescriptor::kObject));
2874  Register slot_parameter(
2875  descriptor.GetRegisterParameter(RecordWriteDescriptor::kSlot));
2876  Register remembered_set_parameter(
2877  descriptor.GetRegisterParameter(RecordWriteDescriptor::kRememberedSet));
2878  Register fp_mode_parameter(
2879  descriptor.GetRegisterParameter(RecordWriteDescriptor::kFPMode));
2880 
2881  Push(object, address);
2882 
2883  Pop(slot_parameter, object_parameter);
2884 
2885  Mov(remembered_set_parameter, Smi::FromEnum(remembered_set_action));
2886  Mov(fp_mode_parameter, Smi::FromEnum(fp_mode));
2887  if (code_target.is_null()) {
2888  Call(wasm_target, RelocInfo::WASM_STUB_CALL);
2889  } else {
2890  Call(code_target, RelocInfo::CODE_TARGET);
2891  }
2892 
2893  RestoreRegisters(registers);
2894 }
2895 
2896 // Will clobber: object, address, value.
2897 // If lr_status is kLRHasBeenSaved, lr will also be clobbered.
2898 //
2899 // The register 'object' contains a heap object pointer. The heap object tag is
2900 // shifted away.
2901 void MacroAssembler::RecordWrite(Register object, Register address,
2902  Register value, LinkRegisterStatus lr_status,
2903  SaveFPRegsMode fp_mode,
2904  RememberedSetAction remembered_set_action,
2905  SmiCheck smi_check) {
2906  ASM_LOCATION_IN_ASSEMBLER("MacroAssembler::RecordWrite");
2907  DCHECK(!AreAliased(object, value));
2908 
2909  if (emit_debug_code()) {
2910  UseScratchRegisterScope temps(this);
2911  Register temp = temps.AcquireX();
2912 
2913  Ldr(temp, MemOperand(address));
2914  Cmp(temp, value);
2915  Check(eq, AbortReason::kWrongAddressOrValuePassedToRecordWrite);
2916  }
2917 
2918  // First, check if a write barrier is even needed. The tests below
2919  // catch stores of smis and stores into the young generation.
2920  Label done;
2921 
2922  if (smi_check == INLINE_SMI_CHECK) {
2923  DCHECK_EQ(0, kSmiTag);
2924  JumpIfSmi(value, &done);
2925  }
2926 
2927  CheckPageFlagClear(value,
2928  value, // Used as scratch.
2929  MemoryChunk::kPointersToHereAreInterestingMask, &done);
2930  CheckPageFlagClear(object,
2931  value, // Used as scratch.
2932  MemoryChunk::kPointersFromHereAreInterestingMask,
2933  &done);
2934 
2935  // Record the actual write.
2936  if (lr_status == kLRHasNotBeenSaved) {
2937  Push(padreg, lr);
2938  }
2939  CallRecordWriteStub(object, address, remembered_set_action, fp_mode);
2940  if (lr_status == kLRHasNotBeenSaved) {
2941  Pop(lr, padreg);
2942  }
2943 
2944  Bind(&done);
2945 
2946  // Count number of write barriers in generated code.
2947  isolate()->counters()->write_barriers_static()->Increment();
2948  IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, address,
2949  value);
2950 
2951  // Clobber clobbered registers when running with the debug-code flag
2952  // turned on to provoke errors.
2953  if (emit_debug_code()) {
2954  Mov(address, Operand(bit_cast<int64_t>(kZapValue + 12)));
2955  Mov(value, Operand(bit_cast<int64_t>(kZapValue + 16)));
2956  }
2957 }
2958 
2959 void TurboAssembler::Assert(Condition cond, AbortReason reason) {
2960  if (emit_debug_code()) {
2961  Check(cond, reason);
2962  }
2963 }
2964 
2965 void TurboAssembler::AssertUnreachable(AbortReason reason) {
2966  if (emit_debug_code()) Abort(reason);
2967 }
2968 
2969 void MacroAssembler::AssertRegisterIsRoot(Register reg, RootIndex index,
2970  AbortReason reason) {
2971  if (emit_debug_code()) {
2972  CompareRoot(reg, index);
2973  Check(eq, reason);
2974  }
2975 }
2976 
2977 void TurboAssembler::Check(Condition cond, AbortReason reason) {
2978  Label ok;
2979  B(cond, &ok);
2980  Abort(reason);
2981  // Will not return here.
2982  Bind(&ok);
2983 }
2984 
2985 void TurboAssembler::Abort(AbortReason reason) {
2986 #ifdef DEBUG
2987  RecordComment("Abort message: ");
2988  RecordComment(GetAbortReason(reason));
2989 #endif
2990 
2991  // Avoid emitting call to builtin if requested.
2992  if (trap_on_abort()) {
2993  Brk(0);
2994  return;
2995  }
2996 
2997  // We need some scratch registers for the MacroAssembler, so make sure we have
2998  // some. This is safe here because Abort never returns.
2999  RegList old_tmp_list = TmpList()->list();
3000  TmpList()->Combine(MacroAssembler::DefaultTmpList());
3001 
3002  if (should_abort_hard()) {
3003  // We don't care if we constructed a frame. Just pretend we did.
3004  FrameScope assume_frame(this, StackFrame::NONE);
3005  Mov(w0, static_cast<int>(reason));
3006  Call(ExternalReference::abort_with_reason());
3007  return;
3008  }
3009 
3010  // Avoid infinite recursion; Push contains some assertions that use Abort.
3011  HardAbortScope hard_aborts(this);
3012 
3013  Mov(x1, Smi::FromInt(static_cast<int>(reason)));
3014 
3015  if (!has_frame_) {
3016  // We don't actually want to generate a pile of code for this, so just
3017  // claim there is a stack frame, without generating one.
3018  FrameScope scope(this, StackFrame::NONE);
3019  Call(BUILTIN_CODE(isolate(), Abort), RelocInfo::CODE_TARGET);
3020  } else {
3021  Call(BUILTIN_CODE(isolate(), Abort), RelocInfo::CODE_TARGET);
3022  }
3023 
3024  TmpList()->set_list(old_tmp_list);
3025 }
3026 
3027 void MacroAssembler::LoadNativeContextSlot(int index, Register dst) {
3028  Ldr(dst, NativeContextMemOperand());
3029  Ldr(dst, ContextMemOperand(dst, index));
3030 }
3031 
3032 
3033 // This is the main Printf implementation. All other Printf variants call
3034 // PrintfNoPreserve after setting up one or more PreserveRegisterScopes.
3035 void MacroAssembler::PrintfNoPreserve(const char * format,
3036  const CPURegister& arg0,
3037  const CPURegister& arg1,
3038  const CPURegister& arg2,
3039  const CPURegister& arg3) {
3040  // We cannot handle a caller-saved stack pointer. It doesn't make much sense
3041  // in most cases anyway, so this restriction shouldn't be too serious.
3042  DCHECK(!kCallerSaved.IncludesAliasOf(sp));
3043 
3044  // The provided arguments, and their proper procedure-call standard registers.
3045  CPURegister args[kPrintfMaxArgCount] = {arg0, arg1, arg2, arg3};
3046  CPURegister pcs[kPrintfMaxArgCount] = {NoReg, NoReg, NoReg, NoReg};
3047 
3048  int arg_count = kPrintfMaxArgCount;
3049 
3050  // The PCS varargs registers for printf. Note that x0 is used for the printf
3051  // format string.
3052  static const CPURegList kPCSVarargs =
3053  CPURegList(CPURegister::kRegister, kXRegSizeInBits, 1, arg_count);
3054  static const CPURegList kPCSVarargsFP =
3055  CPURegList(CPURegister::kVRegister, kDRegSizeInBits, 0, arg_count - 1);
3056 
3057  // We can use caller-saved registers as scratch values, except for the
3058  // arguments and the PCS registers where they might need to go.
3059  CPURegList tmp_list = kCallerSaved;
3060  tmp_list.Remove(x0); // Used to pass the format string.
3061  tmp_list.Remove(kPCSVarargs);
3062  tmp_list.Remove(arg0, arg1, arg2, arg3);
3063 
3064  CPURegList fp_tmp_list = kCallerSavedV;
3065  fp_tmp_list.Remove(kPCSVarargsFP);
3066  fp_tmp_list.Remove(arg0, arg1, arg2, arg3);
3067 
3068  // Override the MacroAssembler's scratch register list. The lists will be
3069  // reset automatically at the end of the UseScratchRegisterScope.
3070  UseScratchRegisterScope temps(this);
3071  TmpList()->set_list(tmp_list.list());
3072  FPTmpList()->set_list(fp_tmp_list.list());
3073 
3074  // Copies of the printf vararg registers that we can pop from.
3075  CPURegList pcs_varargs = kPCSVarargs;
3076  CPURegList pcs_varargs_fp = kPCSVarargsFP;
3077 
3078  // Place the arguments. There are lots of clever tricks and optimizations we
3079  // could use here, but Printf is a debug tool so instead we just try to keep
3080  // it simple: Move each input that isn't already in the right place to a
3081  // scratch register, then move everything back.
3082  for (unsigned i = 0; i < kPrintfMaxArgCount; i++) {
3083  // Work out the proper PCS register for this argument.
3084  if (args[i].IsRegister()) {
3085  pcs[i] = pcs_varargs.PopLowestIndex().X();
3086  // We might only need a W register here. We need to know the size of the
3087  // argument so we can properly encode it for the simulator call.
3088  if (args[i].Is32Bits()) pcs[i] = pcs[i].W();
3089  } else if (args[i].IsVRegister()) {
3090  // In C, floats are always cast to doubles for varargs calls.
3091  pcs[i] = pcs_varargs_fp.PopLowestIndex().D();
3092  } else {
3093  DCHECK(args[i].IsNone());
3094  arg_count = i;
3095  break;
3096  }
3097 
3098  // If the argument is already in the right place, leave it where it is.
3099  if (args[i].Aliases(pcs[i])) continue;
3100 
3101  // Otherwise, if the argument is in a PCS argument register, allocate an
3102  // appropriate scratch register and then move it out of the way.
3103  if (kPCSVarargs.IncludesAliasOf(args[i]) ||
3104  kPCSVarargsFP.IncludesAliasOf(args[i])) {
3105  if (args[i].IsRegister()) {
3106  Register old_arg = args[i].Reg();
3107  Register new_arg = temps.AcquireSameSizeAs(old_arg);
3108  Mov(new_arg, old_arg);
3109  args[i] = new_arg;
3110  } else {
3111  VRegister old_arg = args[i].VReg();
3112  VRegister new_arg = temps.AcquireSameSizeAs(old_arg);
3113  Fmov(new_arg, old_arg);
3114  args[i] = new_arg;
3115  }
3116  }
3117  }
3118 
3119  // Do a second pass to move values into their final positions and perform any
3120  // conversions that may be required.
3121  for (int i = 0; i < arg_count; i++) {
3122  DCHECK(pcs[i].type() == args[i].type());
3123  if (pcs[i].IsRegister()) {
3124  Mov(pcs[i].Reg(), args[i].Reg(), kDiscardForSameWReg);
3125  } else {
3126  DCHECK(pcs[i].IsVRegister());
3127  if (pcs[i].SizeInBytes() == args[i].SizeInBytes()) {
3128  Fmov(pcs[i].VReg(), args[i].VReg());
3129  } else {
3130  Fcvt(pcs[i].VReg(), args[i].VReg());
3131  }
3132  }
3133  }
3134 
3135  // Load the format string into x0, as per the procedure-call standard.
3136  //
3137  // To make the code as portable as possible, the format string is encoded
3138  // directly in the instruction stream. It might be cleaner to encode it in a
3139  // literal pool, but since Printf is usually used for debugging, it is
3140  // beneficial for it to be minimally dependent on other features.
3141  Label format_address;
3142  Adr(x0, &format_address);
3143 
3144  // Emit the format string directly in the instruction stream.
3145  { BlockPoolsScope scope(this);
3146  Label after_data;
3147  B(&after_data);
3148  Bind(&format_address);
3149  EmitStringData(format);
3150  Unreachable();
3151  Bind(&after_data);
3152  }
3153 
3154  CallPrintf(arg_count, pcs);
3155 }
3156 
3157 void TurboAssembler::CallPrintf(int arg_count, const CPURegister* args) {
3158 // A call to printf needs special handling for the simulator, since the system
3159 // printf function will use a different instruction set and the procedure-call
3160 // standard will not be compatible.
3161 #ifdef USE_SIMULATOR
3162  {
3163  InstructionAccurateScope scope(this, kPrintfLength / kInstrSize);
3164  hlt(kImmExceptionIsPrintf);
3165  dc32(arg_count); // kPrintfArgCountOffset
3166 
3167  // Determine the argument pattern.
3168  uint32_t arg_pattern_list = 0;
3169  for (int i = 0; i < arg_count; i++) {
3170  uint32_t arg_pattern;
3171  if (args[i].IsRegister()) {
3172  arg_pattern = args[i].Is32Bits() ? kPrintfArgW : kPrintfArgX;
3173  } else {
3174  DCHECK(args[i].Is64Bits());
3175  arg_pattern = kPrintfArgD;
3176  }
3177  DCHECK(arg_pattern < (1 << kPrintfArgPatternBits));
3178  arg_pattern_list |= (arg_pattern << (kPrintfArgPatternBits * i));
3179  }
3180  dc32(arg_pattern_list); // kPrintfArgPatternListOffset
3181  }
3182 #else
3183  Call(ExternalReference::printf_function());
3184 #endif
3185 }
3186 
3187 
3188 void MacroAssembler::Printf(const char * format,
3189  CPURegister arg0,
3190  CPURegister arg1,
3191  CPURegister arg2,
3192  CPURegister arg3) {
3193  // Printf is expected to preserve all registers, so make sure that none are
3194  // available as scratch registers until we've preserved them.
3195  RegList old_tmp_list = TmpList()->list();
3196  RegList old_fp_tmp_list = FPTmpList()->list();
3197  TmpList()->set_list(0);
3198  FPTmpList()->set_list(0);
3199 
3200  // Preserve all caller-saved registers as well as NZCV.
3201  // PushCPURegList asserts that the size of each list is a multiple of 16
3202  // bytes.
3203  PushCPURegList(kCallerSaved);
3204  PushCPURegList(kCallerSavedV);
3205 
3206  // We can use caller-saved registers as scratch values (except for argN).
3207  CPURegList tmp_list = kCallerSaved;
3208  CPURegList fp_tmp_list = kCallerSavedV;
3209  tmp_list.Remove(arg0, arg1, arg2, arg3);
3210  fp_tmp_list.Remove(arg0, arg1, arg2, arg3);
3211  TmpList()->set_list(tmp_list.list());
3212  FPTmpList()->set_list(fp_tmp_list.list());
3213 
3214  { UseScratchRegisterScope temps(this);
3215  // If any of the arguments are the current stack pointer, allocate a new
3216  // register for them, and adjust the value to compensate for pushing the
3217  // caller-saved registers.
3218  bool arg0_sp = sp.Aliases(arg0);
3219  bool arg1_sp = sp.Aliases(arg1);
3220  bool arg2_sp = sp.Aliases(arg2);
3221  bool arg3_sp = sp.Aliases(arg3);
3222  if (arg0_sp || arg1_sp || arg2_sp || arg3_sp) {
3223  // Allocate a register to hold the original stack pointer value, to pass
3224  // to PrintfNoPreserve as an argument.
3225  Register arg_sp = temps.AcquireX();
3226  Add(arg_sp, sp,
3227  kCallerSaved.TotalSizeInBytes() + kCallerSavedV.TotalSizeInBytes());
3228  if (arg0_sp) arg0 = Register::Create(arg_sp.code(), arg0.SizeInBits());
3229  if (arg1_sp) arg1 = Register::Create(arg_sp.code(), arg1.SizeInBits());
3230  if (arg2_sp) arg2 = Register::Create(arg_sp.code(), arg2.SizeInBits());
3231  if (arg3_sp) arg3 = Register::Create(arg_sp.code(), arg3.SizeInBits());
3232  }
3233 
3234  // Preserve NZCV.
3235  { UseScratchRegisterScope temps(this);
3236  Register tmp = temps.AcquireX();
3237  Mrs(tmp, NZCV);
3238  Push(tmp, xzr);
3239  }
3240 
3241  PrintfNoPreserve(format, arg0, arg1, arg2, arg3);
3242 
3243  // Restore NZCV.
3244  { UseScratchRegisterScope temps(this);
3245  Register tmp = temps.AcquireX();
3246  Pop(xzr, tmp);
3247  Msr(NZCV, tmp);
3248  }
3249  }
3250 
3251  PopCPURegList(kCallerSavedV);
3252  PopCPURegList(kCallerSaved);
3253 
3254  TmpList()->set_list(old_tmp_list);
3255  FPTmpList()->set_list(old_fp_tmp_list);
3256 }
3257 
3258 UseScratchRegisterScope::~UseScratchRegisterScope() {
3259  available_->set_list(old_available_);
3260  availablefp_->set_list(old_availablefp_);
3261 }
3262 
3263 
3264 Register UseScratchRegisterScope::AcquireSameSizeAs(const Register& reg) {
3265  int code = AcquireNextAvailable(available_).code();
3266  return Register::Create(code, reg.SizeInBits());
3267 }
3268 
3269 VRegister UseScratchRegisterScope::AcquireSameSizeAs(const VRegister& reg) {
3270  int code = AcquireNextAvailable(availablefp_).code();
3271  return VRegister::Create(code, reg.SizeInBits());
3272 }
3273 
3274 
3275 CPURegister UseScratchRegisterScope::AcquireNextAvailable(
3276  CPURegList* available) {
3277  CHECK(!available->IsEmpty());
3278  CPURegister result = available->PopLowestIndex();
3279  DCHECK(!AreAliased(result, xzr, sp));
3280  return result;
3281 }
3282 
3283 
3284 MemOperand ContextMemOperand(Register context, int index) {
3285  return MemOperand(context, Context::SlotOffset(index));
3286 }
3287 
3288 MemOperand NativeContextMemOperand() {
3289  return ContextMemOperand(cp, Context::NATIVE_CONTEXT_INDEX);
3290 }
3291 
3292 #define __ masm->
3293 
3294 void InlineSmiCheckInfo::Emit(MacroAssembler* masm, const Register& reg,
3295  const Label* smi_check) {
3296  Assembler::BlockPoolsScope scope(masm);
3297  if (reg.IsValid()) {
3298  DCHECK(smi_check->is_bound());
3299  DCHECK(reg.Is64Bits());
3300 
3301  // Encode the register (x0-x30) in the lowest 5 bits, then the offset to
3302  // 'check' in the other bits. The possible offset is limited in that we
3303  // use BitField to pack the data, and the underlying data type is a
3304  // uint32_t.
3305  uint32_t delta =
3306  static_cast<uint32_t>(__ InstructionsGeneratedSince(smi_check));
3307  __ InlineData(RegisterBits::encode(reg.code()) | DeltaBits::encode(delta));
3308  } else {
3309  DCHECK(!smi_check->is_bound());
3310 
3311  // An offset of 0 indicates that there is no patch site.
3312  __ InlineData(0);
3313  }
3314 }
3315 
3316 InlineSmiCheckInfo::InlineSmiCheckInfo(Address info)
3317  : reg_(NoReg), smi_check_delta_(0), smi_check_(nullptr) {
3318  InstructionSequence* inline_data = InstructionSequence::At(info);
3319  DCHECK(inline_data->IsInlineData());
3320  if (inline_data->IsInlineData()) {
3321  uint64_t payload = inline_data->InlineData();
3322  // We use BitField to decode the payload, and BitField can only handle
3323  // 32-bit values.
3324  DCHECK(is_uint32(payload));
3325  if (payload != 0) {
3326  uint32_t payload32 = static_cast<uint32_t>(payload);
3327  int reg_code = RegisterBits::decode(payload32);
3328  reg_ = Register::XRegFromCode(reg_code);
3329  smi_check_delta_ = DeltaBits::decode(payload32);
3330  DCHECK_NE(0, smi_check_delta_);
3331  smi_check_ = inline_data->preceding(smi_check_delta_);
3332  }
3333  }
3334 }
3335 
3336 void TurboAssembler::ComputeCodeStartAddress(const Register& rd) {
3337  // We can use adr to load a pc relative location.
3338  adr(rd, -pc_offset());
3339 }
3340 
3341 void TurboAssembler::ResetSpeculationPoisonRegister() {
3342  Mov(kSpeculationPoisonRegister, -1);
3343 }
3344 
3345 #undef __
3346 
3347 
3348 } // namespace internal
3349 } // namespace v8
3350 
3351 #endif // V8_TARGET_ARCH_ARM64
Definition: libplatform.h:13